const fs=require('fs'),assert=require('assert/strict'),path=require('path'); const root=path.resolve(__dirname,'..'),romfile=process.argv[2],out=process.argv[3]; const C=require(root+'/src/core.js'),F=require(root+'/src/feld.js'),E=require(root+'/src/emu.js');require(root+'/src/menu-auto-data.js');require(root+'/src/menu-auto.js'); const rom=new Uint8Array(fs.readFileSync(romfile)),original=rom.slice(),emb={feld:fs.readFileSync(root+'/feld/feld-auto.bin'),stub:fs.readFileSync(root+'/feld/stub_feld_tpl.bin')}; const image=F.feldApply(rom,{fields:true,bag:true,tama:true,saves:true},null,emb,E),info=F.feldFind(image,emb.stub); const u=(a)=>(image[a]|image[a+1]<<8|image[a+2]<<16|image[a+3]<<24)>>>0; const lp=info.catOff+288+74*info.catN;assert.equal(u(lp),0x31504c41);const pack=info.catOff+u(lp+4);assert.equal(u(pack),0x324c5041); const stripped=F.feldStrip(image,emb,E);assert.deepEqual(F.feldApply(stripped,{fields:true,bag:true,tama:true,saves:true},null,emb,E),image); assert.deepEqual(image.subarray(0xd49000),rom.subarray(0xd49000));assert.deepEqual(rom,original); const empty=F.feldApply(rom,{fields:true},null,emb,E);assert.deepEqual(F.feldApply(F.feldStrip(empty,emb,E),{fields:true},null,emb,E),empty); if(out){fs.writeFileSync(out,image);fs.writeFileSync(out+'.json',JSON.stringify(info));} console.log(JSON.stringify({languages:9,entries:info.catN,labels:u(pack+4),glyphs:u(pack+16),menuBytes:emb.feld.length,savePreserved:true,stripRoundtrip:true})); if(process.argv[4]){ const ctx=C.openRom(rom),tables=C.textTablesOf(rom).tabs,indexes=[...F.itemCatalog(rom).map(c=>c.text),...C.charTable(ctx.files[0]).filter(c=>c.id>1000&&c.id<5000&&c.stage>=2&&c.stage<=5).map(c=>c.name)]; const oracle=JSON.parse(fs.readFileSync(process.argv[4]));let count=0; for(let l=0;l<9;l++)for(let j=0;j