From e52fe36a2710ba6eb1142b7df910cf193fa327c6 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Tue, 23 Apr 2024 21:56:12 +0200 Subject: [PATCH] Correctly handle module files missing from a previously patched save Resolves #611 --- packages/host/src/patch.ts | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/host/src/patch.ts b/packages/host/src/patch.ts index 05497637..0e36c480 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; } @@ -280,7 +287,7 @@ function reorderDependencies(modules: SaveModule[]) { throw new Error(`Module ${module.info.name} requires ${dependency} ${requirement}`); } - // We have an unmet dependency, take it out and continue + // We have an unmet dependency, take it out and continue } else { if (hold.has(dependency)) { hold.get(dependency)!.push(module); @@ -369,7 +376,7 @@ export async function patch(savePath: string, modules: SaveModule[]) { ); } - // No info file present, try to detect if it's a known compatible scenario. + // No info file present, try to detect if it's a known compatible scenario. } else { let controlFile = root.file("control.lua"); if (!controlFile) { @@ -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 = [];