Skip to content

Commit 8152767

Browse files
committed
New features: Binary transform for translation
1 parent 4aef91c commit 8152767

32 files changed

+916
-550
lines changed

index_translate.js

Lines changed: 137 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
#!/usr/bin/env node
2-
2+
const NBTFILE_LIB = require("./nbtfile_lib.js")
33
const translation_getkey = require("./transformations/translation_getkey.js");
44
const translation_applykey = require("./transformations/translation_applykey.js");
55
const translation_reskey = require("./transformations/translation_reskey.js");
66
const { Settings, writeDebugLine, writeLine } = require("./inputSystem.js");
77
const readlineSync = require('readline-sync');
88
var transform_version = "get";
9+
10+
const MOVE_LEFT = Buffer.from('1b5b3130303044', 'hex').toString();
11+
const MOVE_UP = Buffer.from('1b5b3141', 'hex').toString();
12+
const CLEAR_LINE = Buffer.from('1b5b304b', 'hex').toString();
13+
14+
function clearnLine(n) {
15+
for (let index = 0; index < n; index++) {
16+
process.stdout.write(MOVE_LEFT + CLEAR_LINE + MOVE_UP);
17+
}
18+
}
19+
920
function transformCommand(cmd) {
1021
if (transform_version == "get") {
1122
return translation_getkey.transformCommand(cmd);
@@ -166,6 +177,52 @@ function transformReskeyFile(input, output, reskeyOutput, overwrite = false) {
166177
if (!fs.existsSync(pathLib.dirname(reskeyOutput))) fs.mkdirSync(pathLib.dirname(reskeyOutput), { recursive: true });
167178
fs.writeFileSync(reskeyOutput, JSON.stringify(Settings.result, null, 4));
168179
}
180+
function transformReskeyMCNBT(data) {
181+
let basename = pathLib.basename(Settings.OutputFilePath);
182+
let extname = pathLib.extname(Settings.OutputFilePath);
183+
if (basename == 'scoreboard.dat') {
184+
let dt = data['data'];
185+
if (dt['Teams'] != null) {
186+
let ddt = dt['Teams'];
187+
for (let i = 0; i < ddt.length; i++) {
188+
let ele = ddt[i];
189+
if (ele['MemberNamePrefix'] != null) {
190+
ele['MemberNamePrefix'] = translation_reskey.transformRawMsg(ele['MemberNamePrefix']);
191+
}
192+
if (ele['MemberNameSuffix'] != null) {
193+
ele['MemberNameSuffix'] = translation_reskey.transformRawMsg(ele['MemberNameSuffix']);
194+
}
195+
ddt[i] = ele;
196+
}
197+
}
198+
if (dt['Objectives'] != null) {
199+
let ddt = dt['Objectives'];
200+
for (let i = 0; i < ddt.length; i++) {
201+
let ele = ddt[i];
202+
if (ele['DisplayName'] != null) {
203+
ele['DisplayName'] = translation_reskey.transformRawMsg(ele['DisplayName']);
204+
}
205+
ddt[i] = ele;
206+
}
207+
}
208+
} else if (extname == '.mca') {
209+
if (data['block_entities'] != null) {
210+
let dt = data['block_entities'];
211+
for (let i = 0; i < dt.length; i++) {
212+
dt[i] = translation_reskey.transformBlockTags(dt[i]);
213+
}
214+
data['block_entities'] = dt;
215+
}
216+
if (data['Entities'] != null) {
217+
let dt = data['Entities'];
218+
for (let i = 0; i < dt.length; i++) {
219+
dt[i] = translation_reskey.transformEntityTags(dt[i]);
220+
}
221+
data['Entities'] = dt;
222+
}
223+
}
224+
return data;
225+
}
169226
function transformReskeyFile_true(input) {
170227
// Settings.noWarnings = false;
171228
if (pathLib.extname(input) == '.mcfunction') {
@@ -203,7 +260,7 @@ function transformReskeyFile_true(input) {
203260
}
204261
} else if (pathLib.extname(input) == '.json') {
205262
try {
206-
content = fs.readFileSync(input, { encoding: "utf8" });
263+
var content = fs.readFileSync(input, { encoding: "utf8" });
207264

208265
Settings.nowLine = 0;
209266
Settings.nowFile = input;
@@ -216,6 +273,72 @@ function transformReskeyFile_true(input) {
216273
writeDebugLine(e);
217274
console.error(`## ERROR: Reading file failed: ${e.message}`);
218275
}
276+
} else {
277+
if (!Settings.enableBinary)
278+
return;
279+
280+
if (['.nbt', '.dat'].includes(pathLib.extname(input))) {
281+
try {
282+
let parser = new NBTFILE_LIB.NBTFILE_PARSER();
283+
let saver = new NBTFILE_LIB.NBTFILE_SAVER();
284+
let isGziped = parser.try_load_file_with_gzip(input);
285+
Settings.nowLine = 0;
286+
Settings.nowFile = input;
287+
Settings.hasLog = false
288+
let data = parser.parse();
289+
let snbt = NBTFILE_LIB.NBTFILE_SNBT_TOOL.ToSNBT(data);
290+
snbt = transformReskeyMCNBT(snbt);
291+
data = NBTFILE_LIB.NBTFILE_SNBT_TOOL.ToMCNBT(snbt);
292+
293+
saver.fromMCNBT(data);
294+
// Settings.beforeLog_once = "#!#[file=" + JSON.stringify(pathLib.basename(input)) + ",line=" + j + "]" + "\r\n";
295+
if (isGziped)
296+
saver.save_with_gzip(Settings.OutputFilePath);
297+
else saver.save_nogzip(Settings.OutputFilePath);
298+
// Settings.OutputFile.write(translation_reskey.transformJSON(content))
299+
} catch (e) {
300+
writeDebugLine(e);
301+
// throw new Error(e);
302+
console.error(`## ERROR: Reading file failed: ${e.message}`);
303+
}
304+
305+
306+
} else if (pathLib.extname(input) == '.mca') {
307+
try {
308+
let parser = new NBTFILE_LIB.MCAFILE_PARSER();
309+
let saver = new NBTFILE_LIB.MCAFILE_SAVER();
310+
parser.load_file(input);
311+
parser.parse_header();
312+
313+
Settings.nowLine = 0;
314+
Settings.nowFile = input;
315+
Settings.hasLog = false
316+
console.log("Loading MCA Regions...")
317+
let LEN = parser.headers.length;
318+
for (let i = 0; i < LEN; i++) {
319+
if ((i + 1) % (LEN / 16) == 0) {
320+
clearnLine(1);
321+
console.log(`Loading MCA [${(i + 1)}/${parser.headers.length}]...`)
322+
}
323+
324+
let tester = parser.parse_region_data(parser.headers[i]);
325+
let snbt = NBTFILE_LIB.NBTFILE_SNBT_TOOL.ToSNBT(new NBTFILE_LIB.NBTFILE_PARSER(tester.content).parse());
326+
327+
snbt = transformReskeyMCNBT(snbt);
328+
// console.log(snbt)
329+
let p = new NBTFILE_LIB.NBTFILE_SAVER();
330+
p.fromMCNBT(NBTFILE_LIB.NBTFILE_SNBT_TOOL.ToMCNBT(snbt));
331+
parser.headers[i].data = p.get_raw();
332+
}
333+
clearnLine(1);
334+
saver.headers = parser.headers;
335+
saver.save_to_file(Settings.OutputFilePath, 2);
336+
// Settings.OutputFile.write(translation_reskey.transformJSON(content))
337+
} catch (e) {
338+
writeDebugLine(e);
339+
console.error(`## ERROR: Reading file failed: ${e.message}`);
340+
}
341+
}
219342
}
220343
}
221344
function transformReskeyFolder(dir, output, reskeyOutput, overwrite = false) {
@@ -261,12 +384,17 @@ function transformReskeyFolder(dir, output, reskeyOutput, overwrite = false) {
261384
if (fs.existsSync(outputPath) == true && !overwrite) {
262385
Settings.warningMessages += "\n" + ("## WARNING: File " + output + " already exists. Skip it. If you want to overwrite it, please use '-y' after the arguments.")
263386
} else {
264-
if (!fs.existsSync(pathLib.dirname(outputPath))) fs.mkdirSync(pathLib.dirname(outputPath), { recursive: true });
387+
if (!fs.existsSync(pathLib.dirname(outputPath)))
388+
fs.mkdirSync(pathLib.dirname(outputPath), { recursive: true });
265389
if (pathLib.extname(outputPath) == '.mcfunction' || pathLib.extname(outputPath) == '.json') {
266390
Settings.OutputFile = fs.createWriteStream(outputPath);
267391
Settings.OutputFilePath = outputPath;
268392
transformReskeyFile_true(files[i]);
269393
Settings.OutputFile.end();
394+
} else {
395+
Settings.OutputFilePath = outputPath;
396+
Settings.OutputFile = null;
397+
transformReskeyFile_true(files[i]);
270398
}
271399

272400

@@ -376,6 +504,7 @@ Supported commands:
376504
-debug Show debug messages
377505
-c <commands> Get keys from a command. Use '\\n' to transform multiline commands.
378506
-nowarnings Do not print error messages.
507+
-enablebinary [ALPHA] Transform binary files.
379508
-norepeat Merge repeat texts.
380509
381510
For option get:
@@ -460,7 +589,11 @@ while (i < argvs.length) {
460589
} else if (arg == '-h') {
461590
console.log(HELP_CONTENT);
462591
return;
463-
} else if (arg == '-nowarnings') {
592+
} else if (arg == '-enablebinary') {
593+
Settings.enableBinary = !Settings.enableBinary;
594+
console.log("## Enable ALPHA Features [Binary Transformation]: " + Settings.enableBinary);
595+
}
596+
else if (arg == '-nowarnings') {
464597
Settings.noWarnings = !Settings.noWarnings;
465598
console.log("## No Warnings Mode: " + Settings.noWarnings);
466599
} else if (arg == '-norepeat') {

inputSystem.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ var Settings = {
1212
result: null,
1313
warningMessagesRaw: "",
1414
relativeFilePath: null,
15-
noRepeat:false
15+
noRepeat:false,
16+
enableBinary:false
1617
};
1718
function writeLine(...lines) {
1819
for (let i = 0; i < lines.length; i++) {

0 commit comments

Comments
 (0)