Recover legacy campaign saves

This commit is contained in:
2026-07-05 08:58:58 +09:00
parent 5a76eb07ad
commit d92d5f9061
5 changed files with 152 additions and 16 deletions

View File

@@ -780,8 +780,11 @@ function ensureCampaignState() {
return campaignState;
}
function normalizeCampaignState(state: CampaignState): CampaignState {
const normalized = cloneCampaignState(state);
function normalizeCampaignState(state: Partial<CampaignState>): CampaignState {
const normalized = {
...createInitialCampaignState(),
...cloneCampaignState(state as CampaignState)
};
normalized.version = 1;
normalized.updatedAt = normalized.updatedAt || new Date().toISOString();
normalized.activeSaveSlot = normalizeSlot(normalized.activeSaveSlot);
@@ -837,8 +840,11 @@ function readStoredCampaignState(slot?: number) {
}
try {
const parsed = JSON.parse(raw) as CampaignState;
if (!parsed || parsed.version !== 1) {
const parsed = JSON.parse(raw) as Partial<CampaignState> | null;
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
return undefined;
}
if (parsed.version !== undefined && parsed.version !== 1) {
return undefined;
}
return normalizeCampaignState(parsed);