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:
kawaandClaude Opus 5.5 committed 2026-10-17 12:03:01 +02:00
1 parent 87b05b45f4
commit 592ca43a40
20 files changed
+1821 -40

No files matched your search

+54
View File
@@ -41,6 +41,17 @@ class FakeRuntime implements SlotRuntime {
inspectState(state: Uint8Array) {
return { summary: summarize(this.parse(state)), schema: 3 };
}
/** A "cartridge save" here is the game's JSON prefixed with "SAV". */
convertCartridge(file: Uint8Array) {
const text = new TextDecoder().decode(file);
if (!text.startsWith("SAV")) throw new Error("Not a cartridge save.");
return {
state: new TextEncoder().encode(text.slice(3)),
copy: "backup" as const,
clock: "device" as const,
notes: ["A note."],
};
}
private parse(state: Uint8Array) {
const game = JSON.parse(new TextDecoder().decode(state));
if (typeof game.player !== "string" || !Number.isInteger(game.steps))
@@ -436,6 +447,49 @@ describe("save slot import", () => {
).toEqual([2, 8]);
});
it("previews a cartridge save and imports it as a new slot", async () => {
const { store, runtime, session } = await loaded();
const first = session.active!;
const file = new TextEncoder().encode(
`SAV${JSON.stringify({ player: "GOLD", id: 9, steps: 4 })}`,
);
const modified = new Date("2026-10-02T08:00:00.000Z");
const preview = await session.previewCartridge(file, modified);
if (!preview.ok) throw new Error(preview.reason);
expect(preview.summary).toMatchObject({ player: "GOLD", minutes: 4 });
expect(preview.record.savedAt).toBe(modified.toISOString());
expect(preview.cartridge).toEqual({
copy: "backup",
clock: "device",
notes: ["A note."],
});
expect(preview.conflicts).toEqual([]);
const slot = await session.importBackup(preview, "new");
expect(slot.name).toBe("GOLD");
expect(runtime.game).toEqual({ player: "GOLD", id: 9, steps: 4 });
expect((await store.list(ROM.sha1)).map((s) => s.id)).toContain(first.id);
});
it("refuses a cartridge save that cannot be converted or loaded", async () => {
const { store, runtime, session } = await loaded();
const slot = session.active!;
const before = await store.records(slot.id);
const refused = await session.previewCartridge(
new TextEncoder().encode("{}"),
new Date(),
);
expect(!refused.ok && refused.reason).toBe("Not a cartridge save.");
const unreadable = await session.previewCartridge(
new TextEncoder().encode(`SAV${JSON.stringify({ player: 1 })}`),
new Date(),
);
expect(!unreadable.ok && unreadable.reason).toContain(
"did not pass the port's checks",
);
expect(await store.records(slot.id)).toEqual(before);
expect(runtime.game.steps).toBe(2);
});
it("tells a conflicting slot that moved on from an identical one", async () => {
const { session, store, runtime } = await loaded();
const first = session.active!;