diff --git a/feld/feld-de.bin b/feld/feld-de.bin index 6c034af..647c522 100644 Binary files a/feld/feld-de.bin and b/feld/feld-de.bin differ diff --git a/feld/feld-ko.bin b/feld/feld-ko.bin index 6365b32..7c63bc9 100644 Binary files a/feld/feld-ko.bin and b/feld/feld-ko.bin differ diff --git a/feld/feld.bin b/feld/feld.bin index ee39285..3149244 100644 Binary files a/feld/feld.bin and b/feld/feld.bin differ diff --git a/feld/feldc-de.bin b/feld/feldc-de.bin index b1f5947..097a25a 100644 Binary files a/feld/feldc-de.bin and b/feld/feldc-de.bin differ diff --git a/feld/feldc-ko.bin b/feld/feldc-ko.bin index 2ab5862..e660c7e 100644 Binary files a/feld/feldc-ko.bin and b/feld/feldc-ko.bin differ diff --git a/feld/feldc.bin b/feld/feldc.bin index 27f98c9..c5e5ed4 100644 Binary files a/feld/feldc.bin and b/feld/feldc.bin differ diff --git a/feld/src/feld/main-de.c b/feld/src/feld/main-de.c index acaad15..52fcb71 100644 --- a/feld/src/feld/main-de.c +++ b/feld/src/feld/main-de.c @@ -1442,32 +1442,22 @@ static int chr_row(u32 i, char *name, char *right, u16 *col){ } /* the pet's own file (pages of the save with the character in the header): character there too */ static u32 ko_names(void); -static u32 tama_files(u32 old, u32 nw){ - u32 n=0; - for(u32 a=g_cfg.user_start; a+PAGE<=g_cfg.user_end && a255u || (rd16(a+14u)!=old && rd16(a+12u)!=old)) continue; } -#else - if(rd32(a+8u)!=1u || rd16(a+14u)!=old) continue; -#endif - flash_read(a, g_page, PAGE); - u32 before=0;for(u32 z=8;z<0x180;z+=4)before+=*(u32*)(g_page+z); -#ifdef FELD_TAMA_ANY - if((g_page[14]|(g_page[15]<<8))==old) -#endif - { g_page[14]=(u8)nw; g_page[15]=(u8)(nw>>8); } - if((g_page[12]|(g_page[13]<<8))==old){ g_page[12]=(u8)nw; g_page[13]=(u8)(nw>>8); } - if((g_page[0x10A]|(g_page[0x10B]<<8))==old){ g_page[0x10A]=(u8)nw; g_page[0x10B]=(u8)(nw>>8); } - if(rd32(a)+rd32(a+4u)==0u){ - int k=cat_find(nw);u32 names=ko_names();if(k>=0&&names){for(u32 j=0;j<26;j++)g_page[96u+j]=fl(names+26u*(u32)k+j)[0];g_page[0xfcu]=fl(g_cfg.cat_off+8u+28u*(u32)k+6u)[0];g_page[0xfdu]=0;} - u32 sum=rd32(a)-before;for(u32 z=8;z<0x180;z+=4)sum+=*(u32*)(g_page+z);*(u32*)g_page=sum;*(u32*)(g_page+4u)=0u-sum; - } - flash_prog4k(a, g_page); n++; - } - return n; -} +static u32 ghost_rd32(u32 a){const volatile u8 *p=fl(a);return (u32)p[0]|((u32)p[1]<<8)|((u32)p[2]<<16)|((u32)p[3]<<24);} +static u32 tama_files(u32 old,u32 nw){ + int k=cat_find(nw);u32 names=ko_names();if(k<0||!names)return 0; + u32 tab=names+30u*g_cfg.cat_n;if(ghost_rd32(tab)!=0x314b5047u||ghost_rd32(tab+4u)!=g_cfg.cat_n)return 0; + u32 rel=ghost_rd32(tab+8u+4u*(u32)k);if(!rel)return 0;u32 src=g_cfg.cat_off+rel,len=ghost_rd32(src+0x10cu); + if(len<0x1d34u||len>0x4000u||src>=g_cfg.user_start||len>g_cfg.user_start-src||ghost_rd32(src+8u)!=1u||(ghost_rd32(src+0x108u)>>16)!=nw||ghost_rd32(src)+ghost_rd32(src+4u)!=0u)return 0; + for(u32 a=g_cfg.user_start;a+0x4000u<=g_cfg.user_end&&a+0x4000u<=SET_A;a+=PAGE){ + if(rd32(a+8u)!=1u||rd16(a+12u)!=4017u||rd32(a)+rd32(a+4u)!=0u)continue; + u32 n=rd32(a+0x10cu);if(n<0x1d34u||n>0x4000u||rd32(a+0x118u)!=0x1d34u)continue; + // The first native genes slot is the current pet. Never alter later friend slots. + if(rd16(a+0x10au)!=old&&rd16(a+14u)!=old)return 0; + for(u32 off=0;off<0x4000u;off+=PAGE){for(u32 j=0;j64||h>64)return;int left=(128-(int)(w*sc))/2,top=27+(64-(int)(h*sc))/2;for(u32 y=0;y>8); g_set[to+6]=(u8)nw; g_set[to+7]=(u8)(nw>>8); g_set[to+0x18u]=(u8)((g_set[to+0x18u]&~0x1Cu)|((st&7u)<<2)); + g_set[to+0x19u]=(u8)((g_set[to+0x19u]&0xe0u)|31u); #ifdef FELD_TAMA_ANY #else if(old && old!=nw) tama_files(old, nw); diff --git a/feld/src/feld/main-en.c b/feld/src/feld/main-en.c index 668dbf0..04f76bf 100644 --- a/feld/src/feld/main-en.c +++ b/feld/src/feld/main-en.c @@ -1441,32 +1441,22 @@ static int chr_row(u32 i, char *name, char *right, u16 *col){ } /* the pet's own file (pages of the save with the character in the header): character there too */ static u32 ko_names(void); -static u32 tama_files(u32 old, u32 nw){ - u32 n=0; - for(u32 a=g_cfg.user_start; a+PAGE<=g_cfg.user_end && a255u || (rd16(a+14u)!=old && rd16(a+12u)!=old)) continue; } -#else - if(rd32(a+8u)!=1u || rd16(a+14u)!=old) continue; -#endif - flash_read(a, g_page, PAGE); - u32 before=0;for(u32 z=8;z<0x180;z+=4)before+=*(u32*)(g_page+z); -#ifdef FELD_TAMA_ANY - if((g_page[14]|(g_page[15]<<8))==old) -#endif - { g_page[14]=(u8)nw; g_page[15]=(u8)(nw>>8); } - if((g_page[12]|(g_page[13]<<8))==old){ g_page[12]=(u8)nw; g_page[13]=(u8)(nw>>8); } - if((g_page[0x10A]|(g_page[0x10B]<<8))==old){ g_page[0x10A]=(u8)nw; g_page[0x10B]=(u8)(nw>>8); } - if(rd32(a)+rd32(a+4u)==0u){ - int k=cat_find(nw);u32 names=ko_names();if(k>=0&&names){for(u32 j=0;j<26;j++)g_page[226u+j]=fl(names+26u*(u32)k+j)[0];g_page[0xfcu]=fl(g_cfg.cat_off+8u+28u*(u32)k+6u)[0];g_page[0xfdu]=0;} - u32 sum=rd32(a)-before;for(u32 z=8;z<0x180;z+=4)sum+=*(u32*)(g_page+z);*(u32*)g_page=sum;*(u32*)(g_page+4u)=0u-sum; - } - flash_prog4k(a, g_page); n++; - } - return n; -} +static u32 ghost_rd32(u32 a){const volatile u8 *p=fl(a);return (u32)p[0]|((u32)p[1]<<8)|((u32)p[2]<<16)|((u32)p[3]<<24);} +static u32 tama_files(u32 old,u32 nw){ + int k=cat_find(nw);u32 names=ko_names();if(k<0||!names)return 0; + u32 tab=names+30u*g_cfg.cat_n;if(ghost_rd32(tab)!=0x314b5047u||ghost_rd32(tab+4u)!=g_cfg.cat_n)return 0; + u32 rel=ghost_rd32(tab+8u+4u*(u32)k);if(!rel)return 0;u32 src=g_cfg.cat_off+rel,len=ghost_rd32(src+0x10cu); + if(len<0x1d34u||len>0x4000u||src>=g_cfg.user_start||len>g_cfg.user_start-src||ghost_rd32(src+8u)!=1u||(ghost_rd32(src+0x108u)>>16)!=nw||ghost_rd32(src)+ghost_rd32(src+4u)!=0u)return 0; + for(u32 a=g_cfg.user_start;a+0x4000u<=g_cfg.user_end&&a+0x4000u<=SET_A;a+=PAGE){ + if(rd32(a+8u)!=1u||rd16(a+12u)!=4017u||rd32(a)+rd32(a+4u)!=0u)continue; + u32 n=rd32(a+0x10cu);if(n<0x1d34u||n>0x4000u||rd32(a+0x118u)!=0x1d34u)continue; + // The first native genes slot is the current pet. Never alter later friend slots. + if(rd16(a+0x10au)!=old&&rd16(a+14u)!=old)return 0; + for(u32 off=0;off<0x4000u;off+=PAGE){for(u32 j=0;j64||h>64)return;int left=(128-(int)(w*sc))/2,top=27+(64-(int)(h*sc))/2;for(u32 y=0;y>8); g_set[to+6]=(u8)nw; g_set[to+7]=(u8)(nw>>8); g_set[to+0x18u]=(u8)((g_set[to+0x18u]&~0x1Cu)|((st&7u)<<2)); + g_set[to+0x19u]=(u8)((g_set[to+0x19u]&0xe0u)|31u); #ifdef FELD_TAMA_ANY #else if(old && old!=nw) tama_files(old, nw); diff --git a/feld/src/feld/main-ko.c b/feld/src/feld/main-ko.c index 0bec171..1399cec 100644 --- a/feld/src/feld/main-ko.c +++ b/feld/src/feld/main-ko.c @@ -1467,32 +1467,22 @@ static int chr_row(u32 i, char *name, char *right, u16 *col){ } /* the pet's own file (pages of the save with the character in the header): character there too */ static u32 ko_names(void); -static u32 tama_files(u32 old, u32 nw){ - u32 n=0; - for(u32 a=g_cfg.user_start; a+PAGE<=g_cfg.user_end && a255u || (rd16(a+14u)!=old && rd16(a+12u)!=old)) continue; } -#else - if(rd32(a+8u)!=1u || rd16(a+14u)!=old) continue; -#endif - flash_read(a, g_page, PAGE); - u32 before=0;for(u32 z=8;z<0x180;z+=4)before+=*(u32*)(g_page+z); -#ifdef FELD_TAMA_ANY - if((g_page[14]|(g_page[15]<<8))==old) -#endif - { g_page[14]=(u8)nw; g_page[15]=(u8)(nw>>8); } - if((g_page[12]|(g_page[13]<<8))==old){ g_page[12]=(u8)nw; g_page[13]=(u8)(nw>>8); } - if((g_page[0x10A]|(g_page[0x10B]<<8))==old){ g_page[0x10A]=(u8)nw; g_page[0x10B]=(u8)(nw>>8); } - if(rd32(a)+rd32(a+4u)==0u){ - int k=cat_find(nw);u32 names=ko_names();if(k>=0&&names){for(u32 j=0;j<26;j++)g_page[226u+j]=fl(names+26u*(u32)k+j)[0];g_page[0xfcu]=fl(g_cfg.cat_off+8u+28u*(u32)k+6u)[0];g_page[0xfdu]=0;} - u32 sum=rd32(a)-before;for(u32 z=8;z<0x180;z+=4)sum+=*(u32*)(g_page+z);*(u32*)g_page=sum;*(u32*)(g_page+4u)=0u-sum; - } - flash_prog4k(a, g_page); n++; - } - return n; -} +static u32 ghost_rd32(u32 a){const volatile u8 *p=fl(a);return (u32)p[0]|((u32)p[1]<<8)|((u32)p[2]<<16)|((u32)p[3]<<24);} +static u32 tama_files(u32 old,u32 nw){ + int k=cat_find(nw);u32 names=ko_names();if(k<0||!names)return 0; + u32 tab=names+30u*g_cfg.cat_n;if(ghost_rd32(tab)!=0x314b5047u||ghost_rd32(tab+4u)!=g_cfg.cat_n)return 0; + u32 rel=ghost_rd32(tab+8u+4u*(u32)k);if(!rel)return 0;u32 src=g_cfg.cat_off+rel,len=ghost_rd32(src+0x10cu); + if(len<0x1d34u||len>0x4000u||src>=g_cfg.user_start||len>g_cfg.user_start-src||ghost_rd32(src+8u)!=1u||(ghost_rd32(src+0x108u)>>16)!=nw||ghost_rd32(src)+ghost_rd32(src+4u)!=0u)return 0; + for(u32 a=g_cfg.user_start;a+0x4000u<=g_cfg.user_end&&a+0x4000u<=SET_A;a+=PAGE){ + if(rd32(a+8u)!=1u||rd16(a+12u)!=4017u||rd32(a)+rd32(a+4u)!=0u)continue; + u32 n=rd32(a+0x10cu);if(n<0x1d34u||n>0x4000u||rd32(a+0x118u)!=0x1d34u)continue; + // The first native genes slot is the current pet. Never alter later friend slots. + if(rd16(a+0x10au)!=old&&rd16(a+14u)!=old)return 0; + for(u32 off=0;off<0x4000u;off+=PAGE){for(u32 j=0;j64||h>64)return;int left=(128-(int)(w*sc))/2,top=27+(64-(int)(h*sc))/2;for(u32 y=0;y>8); g_set[to+6]=(u8)nw; g_set[to+7]=(u8)(nw>>8); g_set[to+0x18u]=(u8)((g_set[to+0x18u]&~0x1Cu)|((st&7u)<<2)); + g_set[to+0x19u]=(u8)((g_set[to+0x19u]&0xe0u)|31u); #ifdef FELD_TAMA_ANY #else if(old && old!=nw) tama_files(old, nw); diff --git a/feld/src/feld/qa-character-genes.h b/feld/src/feld/qa-character-genes.h new file mode 100644 index 0000000..aa73ff2 --- /dev/null +++ b/feld/src/feld/qa-character-genes.h @@ -0,0 +1,19 @@ +/* Exercise the actual A+C flash writer against every catalog character. */ +static void verify_character_genes(void){ + u8 *before=malloc(0x1000000);memcpy(before,host_flash,0x1000000); + u32 old=t_id(),slot=0,names=ko_names(),tab=names+30u*g_cfg.cat_n,count=0; + for(u32 a=g_cfg.user_start;a+0x4000<=g_cfg.user_end&&a+0x4000<=SET_A;a+=PAGE) + if(rd32(a+8)==1&&rd16(a+12)==4017&&rd32(a)+rd32(a+4)==0&&rd32(a+0x118)==0x1d34){slot=a;break;} + assert(slot);assert(ghost_rd32(tab)==0x314b5047u); + for(u32 k=0;k=60);memcpy(host_flash,before,0x1000000);free(before); + printf("PASS: %u native character packages, all nine names, palettes, eye sprites, composite frames, same-ID repair and unchanged friends\n",count); +} diff --git a/feld/src/feld/qa-native-de.c b/feld/src/feld/qa-native-de.c index c649bdc..b32e50c 100644 --- a/feld/src/feld/qa-native-de.c +++ b/feld/src/feld/qa-native-de.c @@ -17,12 +17,13 @@ static void fields(void){field_menu(0,0);} static void persist(void){set_write("OK");} static u32 quantity_result,quantity_owned; static void quantity(void){quantity_result=count_add(5001,quantity_owned);} +#include "qa-character-genes.h" int main(int argc,char **argv){ if(argc!=4)return 2; host_flash=malloc(0x1000000);FILE *f=fopen(argv[1],"rb");if(!f)return 3; if(fread(host_flash,1,0x1000000,f)!=0x1000000)return 4;fclose(f); u32 offset=(u32)strtoul(argv[2],0,0);memcpy((void*)&g_cfg,host_flash+offset+0x140,sizeof g_cfg); - proof_root=argv[3];for(u32 i=0;i=3u&&t_stage()<=5u?t_stage():5u);u32 expected=rd16(g_cfg.cat_off+8u+28u*g_chr[1]); proof("character-confirm",chr_pick,"bnnb");expected=rd16(g_cfg.cat_off+8u+28u*g_chr[2]);assert(t_id()==expected); - int checked=0;for(u32 a=g_cfg.user_start;a+PAGE<=g_cfg.user_end&&a=0);assert(!memcmp(fl(a+96u),fl(ko_names()+26u*(u32)ci),26));assert(!memcmp(fl(a+226u),flash_snapshot+a+226u,26));checked++;}}assert(checked>0); + int checked=0;for(u32 a=g_cfg.user_start;a+PAGE<=g_cfg.user_end&&a=0);u32 tab=ko_names()+30u*g_cfg.cat_n,src=g_cfg.cat_off+ghost_rd32(tab+8u+4u*(u32)ci);assert(!memcmp(fl(a+96u),fl(ko_names()+26u*(u32)ci),26));assert(!memcmp(fl(a+18u),fl(src+18u),9u*26u));checked++;}}assert(checked>0); memcpy(host_flash,flash_snapshot,0x1000000);memcpy(g_set,snapshot,PAGE);free(flash_snapshot); proof("persist",persist,"b");memset(g_set,0,PAGE);assert(set_load()==0);assert(list_id(0)==item);assert(count_get(0)==7); printf("PASS: text lists, picture dial selection and bounds, unchanged flash on cancellation, B commits pictured character, quantity 1..15, additive batch inventory\n");free(host_flash);return 0; diff --git a/feld/src/feld/qa-native-en.c b/feld/src/feld/qa-native-en.c index c31553c..4b3801f 100644 --- a/feld/src/feld/qa-native-en.c +++ b/feld/src/feld/qa-native-en.c @@ -17,12 +17,13 @@ static void fields(void){field_menu(0,0);} static void persist(void){set_write("OK");} static u32 quantity_result,quantity_owned; static void quantity(void){quantity_result=count_add(5001,quantity_owned);} +#include "qa-character-genes.h" int main(int argc,char **argv){ if(argc!=4)return 2; host_flash=malloc(0x1000000);FILE *f=fopen(argv[1],"rb");if(!f)return 3; if(fread(host_flash,1,0x1000000,f)!=0x1000000)return 4;fclose(f); u32 offset=(u32)strtoul(argv[2],0,0);memcpy((void*)&g_cfg,host_flash+offset+0x140,sizeof g_cfg); - proof_root=argv[3];for(u32 i=0;i0x4000u||src>=g_cfg.user_start||len>g_cfg.user_start-src||ghost_rd32(src+8u)!=1u||(ghost_rd32(src+0x108u)>>16)!=nw||ghost_rd32(src)+ghost_rd32(src+4u)!=0u)return 0; + for(u32 a=g_cfg.user_start;a+0x4000u<=g_cfg.user_end&&a+0x4000u<=SET_A;a+=PAGE){ + if(rd32(a+8u)!=1u||rd16(a+12u)!=4017u||rd32(a)+rd32(a+4u)!=0u)continue; + u32 n=rd32(a+0x10cu);if(n<0x1d34u||n>0x4000u||rd32(a+0x118u)!=0x1d34u)continue; + // The first native genes slot is the current pet. Never alter later friend slots. + if(rd16(a+0x10au)!=old&&rd16(a+14u)!=old)return 0; + for(u32 off=0;off<0x4000u;off+=PAGE){for(u32 j=0;j=0 && fl(g_cfg.cat_off+8u+28u*(u32)k+2u)[0]==2u){ char t[64]; chr_name((u32)k,t); t[15]=0;', 'if(k>=0 && fl(g_cfg.cat_off+8u+28u*(u32)k+2u)[0]==2u){ char t[64]; chr_name((u32)k,t); int z=0;while(t[z]&&z+(ko_pair(t+z)?2:1)<=14)z+=ko_pair(t+z)?2:1;t[z]=0;'); return s; diff --git a/src/app.js b/src/app.js index 07fc5e1..7cac4ca 100644 --- a/src/app.js +++ b/src/app.js @@ -4424,10 +4424,10 @@ $('btnPetSave').onclick = () => { if (!petSave) return; const ed = petSave.edition, pet = SV.paraPet(petSave, ed), ch = {}; const lv = +$('petLevel').value; if (lv !== pet.level) ch.level = lv; - const v = $('petChar').value; if (v) { const [id, st] = v.split(':').map(Number); if (id !== pet.id || st !== pet.stage) { ch.id = id; ch.stage = st; } } + const v = $('petChar').value; if (v) { const [id, st] = v.split(':').map(Number); ch.id = id; ch.stage = st; } for(const [key,id]of [['hunger','petHunger'],['happy','petHappy']]){const v=Number($(id).value);if(!$(id).checkValidity()){ $(id).reportValidity();return;}if(v!==pet[key])ch[key]=v;} if($('petHealth').value==='0' && pet.sick)ch.sick=0; - if(ch.id){const def=C.charTable(state.ctx.files[0]).find(c=>c.id===ch.id);if(def)ch.koreanName=C.textGet(state.ctx.rom,def.name,'ko');} + if(ch.id)ch.ghost=C.characterGhost(state.ctx.rom,ch.id,state.ctx); if ($('petCare').checked) ch.care = true; if (!Object.keys(ch).length) { toast(t('petNothing')); return; } if (petFromRom) { diff --git a/src/core.js b/src/core.js index 74a1aaa..3252225 100644 --- a/src/core.js +++ b/src/core.js @@ -1166,6 +1166,31 @@ function charTable(dp) { } return []; } +// Rebuild the current pet's native genes package from this ROM. The firmware +// caches palettes and eye sprites separately from the two character IDs. +// Native current-pet genes: nine names, two palettes, 54 five-layer frames and +// large/small eye sprites. Header-only ID changes retain the previous pet's look. +// Layout and checksum are checked against independent firmware-generated exports. +function characterGhost(rom,id,ctx=null,texts=null){ + ctx=ctx||openRom(rom);texts=texts||textTablesOf(rom);const dp=ctx.files[0],table=i=>u32(dp,i*4)*2; + let rec=null;for(let o=table(11);ou16(dp,o+2*k));break;} + if(!rec)throw Error('Character definition is missing'); + const chars=charTable(dp).filter(c=>c.id>1000&&c.id<9000),body=ctx.groups[chars.findIndex(c=>c.id===id)+1]; + if(body===undefined)throw Error('Character sprites are missing'); + const eye=ctx.entries[body+1],small=ctx.entries[body+4],align=n=>(n+3)&~3; + const out=new Uint8Array(0x1d34+align(eye.length)+align(small.length)); + if(out.length>0x4000)throw Error('Character genes exceed the native slot'); + w32(out,8,1);w16(out,12,4017);w16(out,14,id);out[16]=Math.min(15,rec[14]); + w16(out,0xfc,rec[3]);w16(out,0xfe,rec[5]);w16(out,0x100,rec[7]);w16(out,0x102,rec[8]);w16(out,0x10a,id);w32(out,0x10c,out.length); + for(let lang=0;lang<9;lang++){const t=texts.tabs[lang],index=rec[2]+1;if(!t||index>=t.n)throw Error('Native name table is missing');const start=t.offs[index]*2,end=index+132)throw Error('Invalid character frame'); + for(let j=0;j>>0;for(let i=0x600;i>>0;w32(out,0,sum);w32(out,4,-sum>>>0);return out; +} function fieldSets(ctx) { const chars = charTable(ctx.files[0]).filter((c) => c.id > 1000 && c.id < 9000); if (chars.length + 1 !== ctx.groups.length) return []; @@ -1299,7 +1324,7 @@ function nativePetNames(out,edition,names,language){ return out; } -root.ParamixCore = { koreanPetNames, paraLayers, faceFirst, facePoses, layersMove, EYE_V2, smaDetect, smaLoad, eyeFirst, eyeRelGet, shiftEyes, textClean, textGet, textApply, textTablesOf, TXT_ENC, DE_ENC, KO_CHARS, KO_ENC, +root.ParamixCore = { characterGhost, koreanPetNames, paraLayers, faceFirst, facePoses, layersMove, EYE_V2, smaDetect, smaLoad, eyeFirst, eyeRelGet, shiftEyes, textClean, textGet, textApply, textTablesOf, TXT_ENC, DE_ENC, KO_CHARS, KO_ENC, fieldSets, charTable, FIELD_NAMES, ASSET_BASE, EDITIONS, GROUP_PARTS, GROUP_SHEET_W, GROUP_SHEET_H, arc2Parse, arc2Files, arc2Build, arc2Checksum, packSplit, packJoin, spriteHeader, spriteDecode, spriteEncode, spriteBlank, spriteTiny, spriteAlign, partOptions, launcherKeep, launcherChanges, isLauncher, diff --git a/src/feld.js b/src/feld.js index 1037130..e804dd2 100644 --- a/src/feld.js +++ b/src/feld.js @@ -571,13 +571,13 @@ function charCatalog(d, P, language='en', pictures=false) { const C = typeof ParamixCore !== 'undefined' ? ParamixCore : (typeof require !== 'undefined' ? require('./core.js') : null); if (!C) return []; let ctx; try { ctx = C.openRom(d); } catch (e) { return []; } - const tt = textTabelle(d), out = []; + const tt = textTabelle(d), out = [], ghostTexts=pictures?C.textTablesOf(d):null; const all=C.charTable(ctx.files[0]).filter(c=>c.id>1000&&c.id<9000); for (const c of C.charTable(ctx.files[0])) { if (c.id < 1001 || c.id > 4999 || c.stage < 2 || c.stage > 5) continue; const nm = (language==='ko'||language==='de' ? C.textGet(d,c.name,language) : tt >= 0 ? zeichenkette(d,tt,c.name) : '') || ('NO. ' + c.id); const fi = P.names.indexOf(TYPE_NAMES[c.field]); - out.push({ id: c.id, fixed: 2, pic: 0, sum: (c.stage & 255) | ((fi + 1) << 8), at: null, nativeName:C.textGet(d,c.name,language), thumbnail:pictures?portrait(C,ctx,ctx.groups[all.findIndex(v=>v.id===c.id)+1]):null, name: nm.replace(/\s+/g, ' ') }); + out.push({ id: c.id, fixed: 2, pic: 0, sum: (c.stage & 255) | ((fi + 1) << 8), at: null, ghost:pictures?C.characterGhost(d,c.id,ctx,ghostTexts):null, nativeName:C.textGet(d,c.name,language), thumbnail:pictures?portrait(C,ctx,ctx.groups[all.findIndex(v=>v.id===c.id)+1]):null, name: nm.replace(/\s+/g, ' ') }); } return out; } @@ -625,10 +625,11 @@ function catalogBlob(cat, korean=false, rom=null, englishPictures=false, german= const font=korean ? nativeMenuFont(rom) : new Uint8Array(englishPictures?8:0); if(englishPictures){w32(font,0,0x31545045);} const encoded=cat.map(c=>korean?globalThis.ParamixKoreanMenu.encode(c.name||('항목 '+c.id),60):(german?(c.name||('NR. '+c.id)):c.name).slice(0,german?60:20)); - const base=x+4+12*cat.length+font.length, tails=korean||englishPictures?26*cat.length+4*cat.length+cat.reduce((n,c)=>n+(c.thumbnail?.length||0),0)+(korean||german?encoded.reduce((n,c)=>n+c.length+1,0):0):0; + const base=x+4+12*cat.length+font.length, tails=korean||englishPictures?26*cat.length+4*cat.length+8+4*cat.length+cat.reduce((n,c)=>n+(c.ghost?.length||0),0)+cat.reduce((n,c)=>n+(c.thumbnail?.length||0),0)+(korean||german?encoded.reduce((n,c)=>n+c.length+1,0):0):0; const b = new Uint8Array(base+tails); const nameOffsets=[]; - if(korean||englishPictures){let pos=base+30*cat.length;cat.forEach((c,i)=>{ + if(korean||englishPictures){const gp=base+30*cat.length;w32(b,gp,0x314b5047);w32(b,gp+4,cat.length);let pos=gp+8+4*cat.length;cat.forEach((c,i)=>{ + if(c.ghost){w32(b,gp+8+4*i,pos);b.set(c.ghost,pos);pos+=c.ghost.length;} [...(c.nativeName||c.name)].slice(0,12).forEach((ch,j)=>w16(b,base+i*26+j*2,(german?globalThis.ParamixCore.DE_ENC:globalThis.ParamixCore.TXT_ENC)[ch]??globalThis.ParamixCore.KO_ENC[ch]??0)); if(c.thumbnail){w32(b,base+26*cat.length+i*4,pos);b.set(c.thumbnail,pos);pos+=c.thumbnail.length;} });if(korean||german)encoded.forEach(n=>{nameOffsets.push(pos);for(const c of n)b[pos++]=c.charCodeAt(0);b[pos++]=0;}); diff --git a/src/saves.js b/src/saves.js index 19559c7..e7f48e2 100644 --- a/src/saves.js +++ b/src/saves.js @@ -147,10 +147,9 @@ function paraPet(save, edition) { return { level: p[o.level] & 15, id: u16(p, t + 4), stage: (p[t + 0x18] >> 2) & 7, hunger: p[t + 0x1C], happy: p[t + 0x1D], sick: p[t + 0x10] }; } /** - * Changed copy of the save. ch: { level, id, stage, care } (each optional). A new character goes - * into the settings block and into the pet's own file (pages with the old character in the header). + * Changed copy of the save. A character change requires the complete native genes + * package from the ROM. Only the current pet's slot is replaced; friends are preserved. */ -const TAMA_ANY = true; function paraPetSet(save, edition, ch) { const p0 = paraSettings(save), o = PET[family(edition)]; if (!p0 || !o) throw new Error('the save has no settings block'); @@ -160,25 +159,19 @@ function paraPetSet(save, edition, ch) { if (ch.id) { p[t + 4] = ch.id & 255; p[t + 5] = ch.id >> 8; p[t + 6] = ch.id & 255; p[t + 7] = ch.id >> 8; if (ch.stage) p[t + 0x18] = (p[t + 0x18] & ~0x1C) | ((ch.stage & 7) << 2); - let found = 0; - if (old && old !== ch.id) for (const [a, d] of save.pages) { - if (a === SET_A || a === SET_B || d.length !== PAGE) continue; - // 1.7.2 TAMA_ANY: a bred Tama's file was not found (GitHub): small number at +8, character at +0x0C or +0x0E - if (TAMA_ANY ? (u32(d, 8) === 0 || u32(d, 8) > 255 || (u16(d, 14) !== old && u16(d, 12) !== old)) : (u32(d, 8) !== 1 || u16(d, 14) !== old)) continue; - const n = d.slice(); found++; - if (!TAMA_ANY || u16(n, 14) === old) { n[14] = ch.id & 255; n[15] = ch.id >> 8; } - if (u16(n, 12) === old) { n[12] = ch.id & 255; n[13] = ch.id >> 8; } - if (u16(n, 0x10A) === old) { n[0x10A] = ch.id & 255; n[0x10B] = ch.id >> 8; } - if(((u32(d,0)+u32(d,4))>>>0)===0){ - const names=ch.koreanName; - if(names){const C=globalThis.ParamixCore; n.fill(0,18+8*26,18+9*26);[...names].slice(0,12).forEach((c,i)=>{const v=C.TXT_ENC[c]??C.KO_ENC[c];n[18+8*26+i*2]=v&255;n[19+8*26+i*2]=v>>8;});} - if(ch.stage){n[0xFC]=ch.stage;n[0xFD]=0;} - let delta=0;for(let i=8;i<0x180;i+=4)delta=(delta+u32(n,i)-u32(d,i))>>>0; - const sum=(u32(d,0)+delta)>>>0;w32(n,0,sum);w32(n,4,-sum>>>0); - } - pages.set(a, n); + const ghost=ch.ghost; + if(!(ghost instanceof Uint8Array)||ghost.length<0x1d34||ghost.length>0x4000||u32(ghost,8)!==1||u16(ghost,0x10a)!==ch.id||u32(ghost,0x10c)!==ghost.length||((u32(ghost,0)+u32(ghost,4))>>>0)!==0)throw Error('Character appearance data is missing or invalid'); + let found=false; + for(const [a,d] of [...save.pages].sort((a,b)=>a[0]-b[0])){ + if(a===SET_A||a===SET_B||d.length!==PAGE||u32(d,8)!==1||u16(d,12)!==4017||((u32(d,0)+u32(d,4))>>>0)!==0)continue; + const n=u32(d,0x10c);if(n<0x1d34||n>0x4000||u32(d,0x118)!==0x1d34)continue; + if(u16(d,0x10a)!==old&&u16(d,14)!==old)throw Error('Current character genes do not match this save'); + for(let i=0;i<0x4000;i+=PAGE){if(a+i>=SET_A||!save.pages.has(a+i))throw Error('Current character slot is incomplete');const pg=new Uint8Array(PAGE);pg.fill(255);pg.set(ghost.subarray(i,i+PAGE));pages.set(a+i,pg);} + found=true;break; } - if (TAMA_ANY && old && old !== ch.id && !found) throw new Error('the Tama\'s own file is not in the save, the Tama would not change'); + if(!found)throw Error('Current character appearance data was not found'); + p[t+0x19]=(p[t+0x19]&0xe0)|31; + } for(const [key,off,max]of [['hunger',0x1C,6],['happy',0x1D,20],['sick',0x10,1]]){ if(ch[key]!==undefined){if(!Number.isInteger(ch[key])||ch[key]<0||ch[key]>max)throw Error('저장 데이터 값의 범위를 확인하세요.');p[t+off]=ch[key];} diff --git a/test/character-match-test.cjs b/test/character-match-test.cjs new file mode 100644 index 0000000..b2556fa --- /dev/null +++ b/test/character-match-test.cjs @@ -0,0 +1,35 @@ +/* Supply a private ROM and optionally independent native-firmware oracle exports. */ +const fs=require('fs'),path=require('path'),assert=require('assert/strict'); +const C=require('../src/core.js'),S=require('../src/saves.js'),F=require('../src/feld.js'); +const rom=new Uint8Array(fs.readFileSync(process.argv[2])),original=rom.slice(),ctx=C.openRom(rom); +const chars=F.characters(rom),save={pages:S.paraFromImage(rom,ctx.editionId),edition:ctx.editionId}; +const dv=b=>new DataView(b.buffer,b.byteOffset,b.byteLength),u16=(b,a)=>dv(b).getUint16(a,true),u32=(b,a)=>dv(b).getUint32(a,true); +const old=S.paraPet(save,ctx.editionId).id; +const slot=[...save.pages].sort((a,b)=>a[0]-b[0]).find(([a,b])=>u32(b,8)===1&&u16(b,12)===4017&&u16(b,0x10a)===old&&u32(b,0x118)===0x1d34)[0]; +let compared=0; +for(const c of chars){ + const ghost=C.characterGhost(rom,c.id,ctx),out=S.paraPetSet(save,ctx.editionId,{id:c.id,stage:c.stage,ghost}); + assert.equal(S.paraPet(out,ctx.editionId).id,c.id);assert.equal(S.paraPet(out,ctx.editionId).stage,c.stage); + const stored=new Uint8Array(0x4000);for(let i=0;i<0x4000;i+=4096)stored.set(out.pages.get(slot+i),i); + assert.deepEqual(stored.subarray(0,ghost.length),ghost);assert(stored.subarray(ghost.length).every(v=>v===255)); + for(const[a,b]of save.pages)if(!(a>=slot&&ac.id===4062),ghost=C.characterGhost(rom,gem.id,ctx); +const changed=S.paraPetSet({...save,pages:friends},ctx.editionId,{id:gem.id,stage:gem.stage,ghost}); +for(let i=0;i<0x4000;i+=4096)assert.deepEqual(changed.pages.get(friend+i),friends.get(friend+i),'Friend data changed'); +assert.throws(()=>S.paraPetSet(save,ctx.editionId,{id:gem.id}),/appearance/); +const def=C.charTable(ctx.files[0]).find(c=>c.id===gem.id); +for(const[lang,name,index]of[['ko','한글이',8],['en','GEM TEST',1],['de','BÄRCHEN',3]]){ + const edited=C.textApply(rom,new Map([[def.name,name]]),lang),g=C.characterGhost(edited,gem.id),enc=lang==='ko'?{...C.TXT_ENC,...C.KO_ENC}:lang==='de'?C.DE_ENC:C.TXT_ENC; + for(let i=0;ix.id===5001).name,'MILCH'); fs.writeFileSync(process.argv[3]+'.de.bin',german.rom); await w.__loadRom(file(german.rom));language('en');const back=await w.__composeRom();assert(back&&!back.same);const backInfo=F.feldFind(back.rom,F.embedded().stub);assert(Buffer.from(back.rom.subarray(backInfo.off,backInfo.off+0x8000)).includes(Buffer.from('ITOSM_EN_MENU_V1'))); + for(const lang of ['ko','en','de']){ + await w.__loadRom(file(rom));language(lang);d.getElementById('btnPetRom').click(); + const select=d.getElementById('petChar');select.value='4062:5';d.getElementById('btnPetSave').click(); + const C=w.ParamixCore,S=w.ParamixSaves,state=w.__state,expected=C.characterGhost(state.ctx.rom,4062); + assert.equal(S.paraPet({pages:S.paraFromImage(state.ctx.rom,state.ctx.editionId)},state.ctx.editionId).id,4062); + assert.deepEqual(Buffer.from(state.ctx.rom.subarray(0xdee000,0xdee000+expected.length)),Buffer.from(expected),'Browser did not apply complete appearance: '+lang); + d.getElementById('btnPetSave').click();assert.deepEqual(Buffer.from(state.ctx.rom.subarray(0xdee000,0xdee000+expected.length)),Buffer.from(expected),'Same-character repair failed: '+lang); + } assert.deepEqual(errors,[]);w.close(); - console.log('PASS: KO/EN/DE ROM builds, EN-DE-EN menu rebuilds, German item names, late API response and source preservation; no device write'); + console.log('PASS: KO/EN/DE ROM builds, menu rebuilds, native character appearance and same-ID repair through the browser save button, German names, late API response and source preservation; no device write'); })().catch(e=>{console.error(e);process.exit(1);});