11#!/usr/bin/env node
2+ // 检测当前内存限制
3+ const v8 = require ( 'v8' ) ;
4+ console . log ( 'Memory Limit:' , v8 . getHeapStatistics ( ) . heap_size_limit / 1024 / 1024 , 'MB' ) ;
5+
6+
27const NBTFILE_LIB = require ( "./nbtfile_lib.js" )
38const translation_getkey = require ( "./transformations/translation_getkey.js" ) ;
49const translation_applykey = require ( "./transformations/translation_applykey.js" ) ;
510const translation_reskey = require ( "./transformations/translation_reskey.js" ) ;
611const { Settings, writeDebugLine, writeLine } = require ( "./inputSystem.js" ) ;
712const readlineSync = require ( 'readline-sync' ) ;
8- var transform_version = "get" ;
13+ var transform_version = "reskey" ;
14+
15+ // const MCAFILE_PARSER = new NBTFILE_LIB.MCAFILE_PARSER();
16+ // const MCAFILE_SAVER = new NBTFILE_LIB.MCAFILE_SAVER();
17+ // const NBTFILE_PARSER = new NBTFILE_LIB.NBTFILE_PARSER()
18+ // const NBTFILE_SAVER = new NBTFILE_LIB.NBTFILE_SAVER()
19+
20+ function sleep ( ms ) {
21+ // 创建共享内存和视图
22+ const buffer = new SharedArrayBuffer ( 4 ) ;
23+ const view = new Int32Array ( buffer ) ;
24+
25+ // 使用Atomics.wait阻塞,直到超时
26+ Atomics . wait ( view , 0 , 0 , ms ) ;
27+ }
928
1029const MOVE_LEFT = Buffer . from ( '1b5b3130303044' , 'hex' ) . toString ( ) ;
1130const MOVE_UP = Buffer . from ( '1b5b3141' , 'hex' ) . toString ( ) ;
@@ -185,43 +204,36 @@ function transformReskeyMCNBT(data) {
185204 if ( dt [ 'Teams' ] != null ) {
186205 let ddt = dt [ 'Teams' ] ;
187206 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' ] ) ;
207+ if ( ddt [ i ] [ 'MemberNamePrefix' ] != null ) {
208+ translation_reskey . transformRawMsg ( ddt [ i ] [ 'MemberNamePrefix' ] ) ;
191209 }
192- if ( ele [ 'MemberNameSuffix' ] != null ) {
193- ele [ 'MemberNameSuffix' ] = translation_reskey . transformRawMsg ( ele [ 'MemberNameSuffix' ] ) ;
210+ if ( ddt [ i ] [ 'MemberNameSuffix' ] != null ) {
211+ translation_reskey . transformRawMsg ( ddt [ i ] [ 'MemberNameSuffix' ] ) ;
194212 }
195- ddt [ i ] = ele ;
196213 }
197214 }
198215 if ( dt [ 'Objectives' ] != null ) {
199216 let ddt = dt [ 'Objectives' ] ;
200217 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' ] ) ;
218+ if ( ddt [ i ] [ 'DisplayName' ] != null ) {
219+ translation_reskey . transformRawMsg ( ddt [ i ] [ 'DisplayName' ] ) ;
204220 }
205- ddt [ i ] = ele ;
206221 }
207222 }
208223 } else if ( extname == '.mca' ) {
209224 if ( data [ 'block_entities' ] != null ) {
210225 let dt = data [ 'block_entities' ] ;
211226 for ( let i = 0 ; i < dt . length ; i ++ ) {
212- dt [ i ] = translation_reskey . transformBlockTags ( dt [ i ] ) ;
227+ translation_reskey . transformBlockTags ( dt [ i ] ) ;
213228 }
214- data [ 'block_entities' ] = dt ;
215229 }
216230 if ( data [ 'Entities' ] != null ) {
217231 let dt = data [ 'Entities' ] ;
218232 for ( let i = 0 ; i < dt . length ; i ++ ) {
219- dt [ i ] = translation_reskey . transformEntityTags ( dt [ i ] ) ;
233+ translation_reskey . transformEntityTags ( dt [ i ] ) ;
220234 }
221- data [ 'Entities' ] = dt ;
222235 }
223236 }
224- return data ;
225237}
226238function transformReskeyFile_true ( input ) {
227239 // Settings.noWarnings = false;
@@ -269,6 +281,7 @@ function transformReskeyFile_true(input) {
269281
270282 Settings . OutputFile . write ( translation_reskey . transformJSON ( content ) )
271283
284+
272285 } catch ( e ) {
273286 writeDebugLine ( e ) ;
274287 console . error ( `## ERROR: Reading file failed: ${ e . message } ` ) ;
@@ -278,7 +291,12 @@ function transformReskeyFile_true(input) {
278291 return ;
279292
280293 if ( [ '.nbt' , '.dat' ] . includes ( pathLib . extname ( input ) ) ) {
294+ if ( fs . statSync ( input ) . size == 0 ) return ;
281295 try {
296+ if ( global . gc ) {
297+ global . gc ( ) ;
298+
299+ }
282300 let parser = new NBTFILE_LIB . NBTFILE_PARSER ( ) ;
283301 let saver = new NBTFILE_LIB . NBTFILE_SAVER ( ) ;
284302 let isGziped = parser . try_load_file_with_gzip ( input ) ;
@@ -304,9 +322,11 @@ function transformReskeyFile_true(input) {
304322
305323
306324 } else if ( pathLib . extname ( input ) == '.mca' ) {
325+ if ( fs . statSync ( input ) . size == 0 ) return ;
326+
327+ let parser = new NBTFILE_LIB . MCAFILE_PARSER ( ) ;
328+ let saver = new NBTFILE_LIB . MCAFILE_SAVER ( ) ;
307329 try {
308- let parser = new NBTFILE_LIB . MCAFILE_PARSER ( ) ;
309- let saver = new NBTFILE_LIB . MCAFILE_SAVER ( ) ;
310330 parser . load_file ( input ) ;
311331 parser . parse_header ( ) ;
312332
@@ -315,31 +335,53 @@ function transformReskeyFile_true(input) {
315335 Settings . hasLog = false
316336 console . log ( "Loading MCA Regions..." )
317337 let LEN = parser . headers . length ;
338+ saver . headers = parser . headers ;
339+ parser . headers = null ;
318340 for ( let i = 0 ; i < LEN ; i ++ ) {
319341 if ( ( i + 1 ) % ( LEN / 16 ) == 0 ) {
320342 clearnLine ( 1 ) ;
321- console . log ( `Loading MCA [${ ( i + 1 ) } /${ parser . headers . length } ]...` )
343+ console . log ( `Loading MCA [${ ( i + 1 ) } /${ saver . headers . length } ]...` )
322344 }
323345
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 ( ) ) ;
346+ let tester = parser . parse_region_data ( saver . headers [ i ] ) ;
347+ let NBTparser = new NBTFILE_LIB . NBTFILE_PARSER ( ) ;
348+ NBTparser . load_from_raw_data ( tester ) ;
349+ let temp = NBTparser . parse ( ) ;
350+ let snbt = NBTFILE_LIB . NBTFILE_SNBT_TOOL . ToSNBT ( temp ) ;
326351
327- snbt = transformReskeyMCNBT ( snbt ) ;
328- // console.log(snbt)
352+ transformReskeyMCNBT ( snbt ) ;
353+ // // console.log(snbt)
329354 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 ( ) ;
355+ let q = NBTFILE_LIB . NBTFILE_SNBT_TOOL . ToMCNBT ( snbt ) ;
356+ p . fromMCNBT ( q ) ;
357+ saver . headers [ i ] . data = p . get_raw ( ) ;
358+ // saver.headers[i].data = tester;
359+ NBTparser . dispose ( ) ;
360+ // p.raw_data = null;
332361 }
333362 clearnLine ( 1 ) ;
334- saver . headers = parser . headers ;
335363 saver . save_to_file ( Settings . OutputFilePath , 2 ) ;
336364 // Settings.OutputFile.write(translation_reskey.transformJSON(content))
365+
366+ for ( let i in saver . headers ) {
367+ saver . headers [ i ] . data = null ;
368+ }
369+ parser . dispose ( ) ;
370+ saver . headers . length = 0 ;
371+ parser . content = null ;
372+ parser . headers = null ;
373+ saver . headers = null ;
337374 } catch ( e ) {
375+
376+ parser . content = null ;
377+ parser . headers = null ;
378+ saver . headers = null ;
338379 writeDebugLine ( e ) ;
339380 console . error ( `## ERROR: Reading file failed: ${ e . message } ` ) ;
340381 }
341382 }
342383 }
384+ return ;
343385}
344386function transformReskeyFolder ( dir , output , reskeyOutput , overwrite = false ) {
345387
@@ -358,9 +400,7 @@ function transformReskeyFolder(dir, output, reskeyOutput, overwrite = false) {
358400 if ( ! output . endsWith ( "/" ) ) output = output + "/" ;
359401
360402 for ( let i = 0 ; i < files . length ; i ++ ) {
361-
362403 // readline.clearLine(process.stdout, 0); //移动光标到行首
363-
364404 let percent = ( i / files . length ) . toFixed ( 4 ) ;
365405 var cell_num = Math . floor ( percent * 25 ) ; // 计算需要多少个 █ 符号来拼凑图案
366406
@@ -391,6 +431,7 @@ function transformReskeyFolder(dir, output, reskeyOutput, overwrite = false) {
391431 Settings . OutputFilePath = outputPath ;
392432 transformReskeyFile_true ( files [ i ] ) ;
393433 Settings . OutputFile . end ( ) ;
434+ Settings . OutputFile = null ;
394435 } else {
395436 Settings . OutputFilePath = outputPath ;
396437 Settings . OutputFile = null ;
@@ -426,6 +467,7 @@ function transformFile(input, output, overwrite = false) {
426467 if ( ! fs . existsSync ( pathLib . dirname ( output ) ) ) fs . mkdirSync ( pathLib . dirname ( output ) , { recursive : true } ) ;
427468 content = fs . readFileSync ( input , { encoding : "utf8" } ) ;
428469 Settings . OutputFile = fs . createWriteStream ( output ) ;
470+
429471 Settings . OutputFilePath = output ;
430472 writeLine ( "##" )
431473 writeLine ( "## Datapack Upgrader v" + package . version + " by " + package . author )
0 commit comments