Import cartridge .sav files with verified structure, checksums and clock (Closes #44)
Savegames → Import backup now reads an English Crystal .sav (from an emulator, a cartridge dump or the 3DS Virtual Console's sav.dat) and converts it into a native save after a preview. - world/cartridge-save.ts: picks the copy TryLoadSaveFile would load (check values and checksums of the main copy, else the backup), reads the saved WRAM block, the numbered boxes, mail backups, Hall of Fame and Battle Tower by PRET labels (SRAM, SAVE_BLOCK in the profile, checked by check:reference with the SAV constants), engine flags via EngineFlags, scenes via MapScenes, and the map's objects and masks. - Clock: an emulator MBC3 footer plus the time since it was written, FixDays/FixTime with wStart*, mapped to the minute and weekday offsets; without one, the device's time and the saved wCurDay. Day countdowns move with the day difference. - Refused with the reason: unported maps, the Bug-Catching Contest, a Battle Tower challenge, characters outside the naming screen. - Names keep <PK>/<MN> as ᴾᴷ/ᴹᴺ (one character, one tile); trainer names accept the naming screen's alphabet; boxed Eggs validate; native states up to 256 kB load (a full PC exceeded 64 kB). - Tests: the committed debug saves, layout offsets against the symbols, backup fallback, clock footers, day counters, refusals, slot session and the Savegames flow in Chromium and WebKit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
87b05b45f4
commit
592ca43a40
20 files changed
+1821
-40
No files matched your search
@@ -218,8 +218,8 @@ for (const [key, symbol] of Object.entries(ramNames)) {
|
||||
if (!match || Number(match[1]) !== ram.get(symbol))
|
||||
throw new Error(`Profile RAM mismatch: ${key} / ${symbol}`);
|
||||
}
|
||||
// SAVED_WRAM and WRAM entries are work RAM labels.
|
||||
for (const table of ["SAVED_WRAM", "WRAM"]) {
|
||||
// SAVED_WRAM, WRAM and SAVE_BLOCK entries are work RAM labels.
|
||||
for (const table of ["SAVED_WRAM", "WRAM", "SAVE_BLOCK"]) {
|
||||
const block = new RegExp(`export const ${table} = \\{([\\s\\S]*?)\\} as const;`).exec(profile)?.[1];
|
||||
if (block === undefined) throw new Error(`${table} symbol table is missing.`);
|
||||
for (const [, quoted, bare, literal] of block.matchAll(/^\s+(?:"([^"]+)"|(\w+)):\s*(0x[0-9a-f]+),/gm)) {
|
||||
@@ -228,6 +228,25 @@ for (const table of ["SAVED_WRAM", "WRAM"]) {
|
||||
throw new Error(`${table} RAM mismatch: ${label}`);
|
||||
}
|
||||
}
|
||||
// SRAM entries are offsets into a 32 KiB .sav file: bank * $2000 plus the
|
||||
// address within $a000-$bfff.
|
||||
{
|
||||
const sram = new Map(
|
||||
readFileSync(`${root}/pokecrystal11.sym`, "utf8")
|
||||
.split("\n")
|
||||
.flatMap((line) => {
|
||||
const match = /^0([0-3]):([ab][0-9a-f]{3}) (\S+)$/.exec(line);
|
||||
return match
|
||||
? [[match[3], Number(match[1]) * 0x2000 + parseInt(match[2], 16) - 0xa000]]
|
||||
: [];
|
||||
}),
|
||||
);
|
||||
const block = /export const SRAM = \{([\s\S]*?)\} as const;/.exec(profile)?.[1];
|
||||
if (block === undefined) throw new Error("SRAM symbol table is missing.");
|
||||
for (const [, label, literal] of block.matchAll(/^\s+(\w+):\s*(0x[0-9a-f]+),/gm))
|
||||
if (sram.get(label) !== Number(literal))
|
||||
throw new Error(`SRAM mismatch: ${label}`);
|
||||
}
|
||||
// BATTLE, OVERWORLD, PC, INTRO, STATS, MENU, POKEGEAR, MOM, TRAINER_CARD, POKEDEX, OPTIONS, JOHTO and AUDIO entries are keyed by their PRET labels.
|
||||
for (const table of ["BATTLE", "OVERWORLD", "PC", "INTRO", "STATS", "MENU", "POKEGEAR", "MOM", "TRAINER_CARD", "POKEDEX", "OPTIONS", "JOHTO", "AUDIO", "PHONE", "RADIO", "MAIL", "EGGS"]) {
|
||||
const block = new RegExp(`export const ${table} = \\{([\\s\\S]*?)\\} as const;`).exec(profile)?.[1];
|
||||
@@ -372,6 +391,17 @@ for (const [, block] of battleConstants.matchAll(/export const \w+ = \{([\s\S]*?
|
||||
throw new Error(`Battle constant mismatch: ${name} is ${literal}, PRET has ${expected}`);
|
||||
}
|
||||
}
|
||||
// The cartridge save converter's PRET constants (its LAYOUT holds struct
|
||||
// offsets, compared with the symbols by tests/cartridge-save.test.ts).
|
||||
{
|
||||
const block = /export const SAV = \{([\s\S]*?)\} as const;/.exec(
|
||||
readFileSync("src/game/world/cartridge-save.ts", "utf8"),
|
||||
)?.[1];
|
||||
if (block === undefined) throw new Error("SAV constant table is missing.");
|
||||
for (const [, name, literal] of block.matchAll(/^\s+(\w+):\s*(0x[0-9a-f]+|\d+),/gim))
|
||||
if (pretConstants.get(name) !== Number(literal))
|
||||
throw new Error(`Cartridge save constant mismatch: ${name} is ${literal}, PRET has ${pretConstants.get(name)}`);
|
||||
}
|
||||
if (!profile.includes(`pokegear: ${pretConstants.get("ENGINE_POKEGEAR")},`))
|
||||
throw new Error("ENGINE_POKEGEAR does not match the reference.");
|
||||
if (!profile.includes(`pokedex: ${pretConstants.get("ENGINE_POKEDEX")},`))
|
||||
|
||||
Reference in new issue
Block a user