/** * Save format versions. Each change to what a save holds adds one upgrade step from the * previous version, so a save made by any earlier release still opens. The steps themselves * belong to the rules layer (profile/profile-upgrades.ts); this layer only runs them in order. */ import type { BlkBlock } from '@wto/blk'; /** Turns a profile of one format version into the next version, in place. */ export type SaveUpgrade = (profile: BlkBlock) => void; export class SaveUpgrades { /** steps[n] turns a version n+1 profile into a version n+2 profile. */ readonly #steps: readonly SaveUpgrade[]; constructor(steps: readonly SaveUpgrade[]) { this.#steps = steps; } get currentFormat(): number { return this.#steps.length + 1; } upgrade(profile: BlkBlock, fromVersion: number): BlkBlock { if (!Number.isInteger(fromVersion) || fromVersion < 1 || fromVersion > this.currentFormat) { throw new Error(`save format version ${fromVersion} is not supported by this version (current: ${this.currentFormat})`); } for (let version = fromVersion; version < this.currentFormat; version++) this.#steps[version - 1]!(profile); return profile; } }