diff --git a/packages/slave/slave.js b/packages/slave/slave.js index 18f3ea92..2d057303 100755 --- a/packages/slave/slave.js +++ b/packages/slave/slave.js @@ -461,6 +461,23 @@ class Instance extends libLink.Link { saveName = "world.zip"; } + // Load a copy if it's autosave to prevent overwriting the autosave + if (saveName.startsWith("_autosave")) { + this.logger.info("Copying autosave"); + let now = new Date(); + let newName = util.format( + "%s-%s-%s %s%s %s", + now.getUTCFullYear(), + (now.getUTCMonth() + 1).toLocaleString("en", { minimumIntegerDigits: 2 }), + now.getUTCDate().toLocaleString("en", { minimumIntegerDigits: 2 }), + now.getUTCHours().toLocaleString("en", { minimumIntegerDigits: 2 }), + now.getUTCMinutes().toLocaleString("en", { minimumIntegerDigits: 2 }), + saveName, + ); + await fs.copy(this.path("saves", saveName), this.path("saves", newName)); + saveName = newName; + } + if (!this.config.get("factorio.enable_save_patching")) { return saveName; } diff --git a/test/integration/clusterio.js b/test/integration/clusterio.js index 8e7018d1..3468cc3f 100644 --- a/test/integration/clusterio.js +++ b/test/integration/clusterio.js @@ -173,11 +173,21 @@ describe("Integration of Clusterio", function() { }); describe("instance start", function() { - it("starts the instance", async function() { + it("starts the given save", async function() { slowTest(this); - await execCtl("instance start test"); + await execCtl("instance start test --save world.zip"); await checkInstanceStatus(44, "running"); }); + it("copies the save if an autosave is the target", async function() { + slowTest(this); + await execCtl("instance stop 44"); + let savesDir = path.join("temp", "test", "instances", "test", "saves"); + await fs.copy(path.join(savesDir, "world.zip"), path.join(savesDir, "_autosave1.zip")); + await execCtl("instance start test"); + let result = await libLink.messages.listSaves.send(getControl(), { instance_id: 44 }); + let running = result.list.find(s => s.loaded); + assert(running.name !== "_autosave1.zip"); + }); }); describe("instance send-rcon", function() {