From f0fb471ea13898e879d0f3b47203fc52760c0aa0 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Tue, 23 Apr 2024 21:56:12 +0200 Subject: [PATCH 1/2] Correctly handle module files missing from a previously patched save Resolves #611 --- packages/host/src/patch.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/host/src/patch.ts b/packages/host/src/patch.ts index 05497637..85850181 100644 --- a/packages/host/src/patch.ts +++ b/packages/host/src/patch.ts @@ -78,7 +78,14 @@ export class SaveModule { static async fromSave(json: Static, root: JSZip) { const module = new this(lib.ModuleInfo.fromJSON(json)); module.files = new Map( - await Promise.all(json.files.map(async f => [f, await root.file(f)!.async("nodebuffer")] as const)) + await Promise.all(json.files.map(async filename => { + const file = root.file(filename); + if (file === null) { + lib.logger.warn(`Missing file ${filename} in save`); + return [filename, Buffer.alloc(0)] as const; + } + return [filename, await file.async("nodebuffer")] as const; + })) ); return module; } @@ -395,8 +402,11 @@ export async function patch(savePath: string, modules: SaveModule[]) { } } else { for (let module of patchInfo.modules) { - for (let file of module.files.keys()) { - zip.remove(root.file(file)!.name); + for (let filepath of module.files.keys()) { + const file = root.file(filepath); + if (file !== null) { + zip.remove(file.name); + } } } patchInfo.modules = []; From 8831b33c019d26272a44f77014fcacdb0b4fb26d Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sat, 27 Apr 2024 22:09:16 +0200 Subject: [PATCH 2/2] Filter out missing files on load --- packages/host/src/patch.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/host/src/patch.ts b/packages/host/src/patch.ts index 85850181..c0047667 100644 --- a/packages/host/src/patch.ts +++ b/packages/host/src/patch.ts @@ -77,16 +77,16 @@ export class SaveModule { static async fromSave(json: Static, root: JSZip) { const module = new this(lib.ModuleInfo.fromJSON(json)); - module.files = new Map( - await Promise.all(json.files.map(async filename => { - const file = root.file(filename); + module.files = new Map(await Promise.all(json.files + .map(filename => ({filename, file: root.file(filename)})) + .filter(({filename, file}) => { if (file === null) { lib.logger.warn(`Missing file ${filename} in save`); - return [filename, Buffer.alloc(0)] as const; } - return [filename, await file.async("nodebuffer")] as const; - })) - ); + return file !== null; + }) + .map(async ({filename, file}) => [filename, await file!.async("nodebuffer")] as const) + )); return module; }