/* Paradise progress flags. Only audited firmware layouts are accepted.
* The Jade RAM payload starts four bytes after the settings page CRC pair.
* Book getter 0x100046d0: payload + 0x84 + field*4.
* Mission getter/setter 0x10021b08/0x10021be8: payload + 0xe8.
* Seen and reward-claimed bits are deliberately preserved.
*/
(function(root){
'use strict';
const S=root.ParamixSaves, C=root.ParamixCore;
const u16=(b,o)=>b[o]|b[o+1]<<8;
const u32=(b,o)=>(b[o]|b[o+1]<<8|b[o+2]<<16|b[o+3]<<24)>>>0;
function w32(b,o,v){for(let i=0;i<4;i++)b[o+i]=(v>>>(8*i))&255;}
// Recognize complete Thumb routines, allowing relocation and decoded data addresses.
// Never infer a save offset from an edition number alone.
function pattern(hex,holes){const bytes=Uint8Array.from(hex.match(/../g),x=>parseInt(x,16)),mask=new Uint8Array(bytes.length).fill(255);for(const [a,n]of holes)mask.fill(0,a,a+n);return {bytes,mask};}
const BOOK=pattern('80b586b0adf8140000200490ffe7049802283fd8ffe7049949f69810c1f2080050f83100039000200290ffe70298049a49f69811c1f2080101ebc2014968884223d2ffe70398029930f81100bdf81410884215d1ffe70298fff774ff0190049940f6a030c1f6000000eb8100d0f8840001990840002818bf01208df817000de0ffe7029801300290d0e7ffe7049801300490bce700208df81700ffe79df8170006b080bd',[[0x18,8],[0x30,8],[0x58,4],[0x60,8],[0x6e,1]]);
const GET=pattern('80b582b00190ffe70198162803d3ffe73cf0f4feffe7ffe7019800f01f0101208840009040f6a030c1f60000d0f8e80000990840002818bf012002b080bd0000',[[0x10,4],[0x24,8],[0x2e,1]]);
const SET=pattern('80b584b003908df80b10ffe70398162803d3ffe73cf082feffe7ffe7039800f01f0101208840019040f6a031c1f60001d1f8e800019a9043c1f8e8009df80b0058b1ffe7019a40f6a031c1f60001d1f8e8001043c1f8e800ffe704b080bd0000',[[0x14,4],[0x28,8],[0x46,8],[0x32,1],[0x3a,1],[0x50,1],[0x56,1]]);
const BIT=pattern('82b00190019800f01f01012088400090009802b07047',[]);
function matches(b,o,p){if(o<0x11000||o+p.bytes.length>0x110000)return false;for(let i=0;i
((x&15)<<12)|((x&0x400)<<1)|((y&0x7000)>>>4)|(y&255);
return (imm(a,c)|(imm(d,e)<<16))>>>0;
}
function callTarget(b,o){const a=u16(b,o),c=u16(b,o+2);if((a&0xf800)!==0xf000||(c&0xd000)!==0xd000)return -1;const s=(a>>>10)&1,i1=(~((c>>>13)^s))&1,i2=(~((c>>>11)^s))&1;let n=(s<<24)|(i1<<23)|(i2<<22)|((a&1023)<<12)|((c&2047)<<1);if(s)n-=0x2000000;return o+4+n;}
function layout(rom){
let result=null;
const books=scan(rom,BOOK).map(o=>({at:o,table:mov32(rom,o+0x18,0),table2:mov32(rom,o+0x30,1),ram:mov32(rom,o+0x60,0),offset:rom[o+0x6e]+4,helper:callTarget(rom,o+0x58)})).filter(x=>x.table===x.table2&&x.ram>=0x18000000&&x.ram<0x18004000&&x.offset>=0x64&&x.offset<0x104&&x.offset%4===0&&matches(rom,x.helper,BIT));
const missions=[];
for(const o of scan(rom,GET)){
const ram=mov32(rom,o+0x24,0),offset=rom[o+0x2e]+4;
if(!ram||offset<0xa0||offset>0xf8||offset%4||!matches(rom,o+64,GET)||mov32(rom,o+64+0x24,0)!==ram||rom[o+64+0x2e]+4!==offset+8)continue;
if([0,64].some(d=>callTarget(rom,o+d+0x10)<0x11000||callTarget(rom,o+d+0x10)>=0x110000))continue;
let valid=true;
for(const [d,extra]of [[128,4],[224,0],[320,8]]){
const a=o+d,off=offset+extra-4;
if(!matches(rom,a,SET)||mov32(rom,a+0x28,1)!==ram||mov32(rom,a+0x46,1)!==ram||[0x32,0x3a,0x50,0x56].some(k=>rom[a+k]!==off)||callTarget(rom,a+0x14)<0x11000||callTarget(rom,a+0x14)>=0x110000)valid=false;
}
if(valid)missions.push({at:o,ram,offset,claimedOffset:offset+8,count:22});
}
if(books.length===1&&missions.length===1&&books[0].ram===missions[0].ram&&books[0].offset+12<=missions[0].offset)result={book:books[0],mission:missions[0]};
return result;
}
function profile(rom,edition,language='ko'){
if(![1,2,3,4,5,6].includes(edition)||rom.length!==0x1000000||root.ParamixFeld.feldId(rom)!==edition||root.ParamixFeld.isClean(rom))return null;
const found=layout(rom);if(!found)return null;
const chars=new Map(root.ParamixFeld.characters(rom,language).map(x=>[x.id,x]));
const table=found.book.table-0x10000000+0x11000;if(table<0x11000||table+24>0x110000)return null;
const book=[],seen=new Set();
for(let g=0;g<3;g++){
const t=table+g*8,p=u32(rom,t)-0x10000000+0x11000,n=u32(rom,t+4);
if(n<1||n>32||p<0x11000||p+2*n>0x110000)return null;
for(let i=0;i({id,name:C.textGet(rom,first+id,language)}));
if(missions.some(x=>!x.name))return null;
return {edition,book,missions,missionOffset:found.mission.offset,claimedOffset:found.mission.claimedOffset};
}
function settings(save){
for(const a of [0xefe000,0xeff000]){const p=save.pages.get(a);if(p&&p.length===4096&&S.settingsOk(p))return p;}
throw Error('저장 데이터를 읽을 수 없습니다.');
}
function read(save,info){
if(!info||save.edition!==info.edition)throw Error('지원하지 않는 도감·미션 저장 데이터입니다.');
const p=settings(save);
return {book:info.book.map(x=>({...x,done:!!(u32(p,x.offset)&(1<({...x,done:!!(u32(p,info.missionOffset)&(1<[x.id,x]));
for(const id of change.book||[]){const x=books.get(id);if(!x)throw Error('도감 항목이 올바르지 않습니다.');w32(p,x.offset,u32(p,x.offset)|(1<=info.missions.length)throw Error('미션 항목이 올바르지 않습니다.');w32(p,info.missionOffset,u32(p,info.missionOffset)|(1<>>8;p[2]=(~crc)&255;p[3]=((~crc)>>>8)&255;
const pages=new Map(save.pages);pages.set(0xefe000,p);pages.set(0xeff000,p.slice());return {...save,pages};
}
root.ParamixProgress={profile,read,apply};if(typeof module!=='undefined')module.exports=root.ParamixProgress;
})(typeof window!=='undefined'?window:globalThis);