From e56ed353e53b5dad1a512cb564b9cd7e99e7b283 Mon Sep 17 00:00:00 2001 From: David Walker Date: Tue, 23 Apr 2024 17:40:59 -0700 Subject: [PATCH] (Partial) fix for #795 (#1223) --- src/engine.tsx | 41 ++++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/src/engine.tsx b/src/engine.tsx index 7195cd6eb..3db900e50 100644 --- a/src/engine.tsx +++ b/src/engine.tsx @@ -114,15 +114,17 @@ const Engine: { // Sleeves Player.sleeves.forEach((sleeve) => sleeve.process(numCycles)); - // Counters - Engine.decrementAllCounters(numCycles); - Engine.checkCounters(); - // Update the running time of all active scripts updateOnlineScriptTimes(numCycles); // Hacknet Nodes processHacknetEarnings(numCycles); + + // Counters + Engine.decrementAllCounters(numCycles); + // This **MUST** be the last call in the function, because checkCounters() + // can invoke the autosave, so any work done after here risks not getting saved! + Engine.checkCounters(); }, /** @@ -158,19 +160,6 @@ const Engine: { * is necessary and then resets the counter */ checkCounters: function () { - if (Engine.Counters.autoSaveCounter <= 0) { - if (Settings.AutosaveInterval == null) { - Settings.AutosaveInterval = 60; - } - if (Settings.AutosaveInterval === 0) { - warnAutosaveDisabled(); - Engine.Counters.autoSaveCounter = 60 * 5; // Let's check back in a bit - } else { - Engine.Counters.autoSaveCounter = Settings.AutosaveInterval * 5; - saveObject.saveGame(!Settings.SuppressSavedGameToast); - } - } - if (Engine.Counters.checkFactionInvitations <= 0) { const invitedFactions = Player.checkForFactionInvitations(); if (invitedFactions.length > 0) { @@ -217,6 +206,24 @@ const Engine: { calculateAchievements(); Engine.Counters.achievementsCounter = 300; } + + // This **MUST** remain the last block in the function! + // Otherwise, any work done after this point won't be saved! + // Due to the way most of these counters are reset, that would probably be + // OK, but it's much simpler to reason about if we can assume that the + // entire function has been performed after a save. + if (Engine.Counters.autoSaveCounter <= 0) { + if (Settings.AutosaveInterval == null) { + Settings.AutosaveInterval = 60; + } + if (Settings.AutosaveInterval === 0) { + warnAutosaveDisabled(); + Engine.Counters.autoSaveCounter = 60 * 5; // Let's check back in a bit + } else { + Engine.Counters.autoSaveCounter = Settings.AutosaveInterval * 5; + saveObject.saveGame(!Settings.SuppressSavedGameToast); + } + } }, load: async function (saveData) {