From ec0e41e6b4e97de9ff497c30926e6c8fe447492a Mon Sep 17 00:00:00 2001 From: Lordmau5 Date: Tue, 27 Aug 2024 18:18:52 +0200 Subject: [PATCH] Refactor autosave and quicksave They now use a `std::chrono::steady_clock` instead of GTA's internal timers - this should help with time drift and not being able to quicksave when loading a save that's "in the future" --- src/gtasa/util/GameHandler.h | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/gtasa/util/GameHandler.h b/src/gtasa/util/GameHandler.h index adeb4ab8..94b208f6 100644 --- a/src/gtasa/util/GameHandler.h +++ b/src/gtasa/util/GameHandler.h @@ -25,9 +25,11 @@ class GameHandler static inline bool didTryLoadAutoSave = false; - static inline int lastMissionsPassed = -1; - static inline int lastSaved = 0; - static inline int lastQuickSave = 0; + static inline int lastMissionsPassed = -1; + static inline std::chrono::steady_clock::time_point lastSaved + = std::chrono::steady_clock::now (); + static inline std::chrono::steady_clock::time_point lastQuickSave + = std::chrono::steady_clock::now (); static inline CStuntJump *¤tStuntJump = *(CStuntJump **) 0xA9A88C; @@ -130,8 +132,6 @@ class GameHandler if (!CONFIG ("Chaos.AutosaveAfterMissionPassed", true)) return; int missionsPassed = GameUtil::GetRealMissionsPassed (); - int currentTime = std::max (CTimer::m_snTimeInMillisecondsNonClipped, - (unsigned int) lastMissionsPassed); if (lastMissionsPassed == -1) { @@ -142,6 +142,7 @@ class GameHandler lastMissionsPassed = missionsPassed; } + auto currentTime = std::chrono::steady_clock::now (); if (missionsPassed > lastMissionsPassed && lastSaved < currentTime && !CTheScripts::IsPlayerOnAMission ()) { @@ -155,7 +156,7 @@ class GameHandler EffectHandler::HandleFunction (json); - lastSaved = currentTime + 1000; + lastSaved = currentTime + std::chrono::milliseconds{10000}; } } @@ -164,13 +165,12 @@ class GameHandler { if (!CONFIG ("Chaos.QuickSave", false)) return; - int currentTime = std::max (CTimer::m_snTimeInMillisecondsNonClipped, - (unsigned int) lastQuickSave); + auto currentTime = std::chrono::steady_clock::now (); if (!FrontEndMenuManager.m_bMenuActive && KeyPressed (VK_F7) && lastQuickSave < currentTime) { - lastQuickSave = currentTime + 1000; + lastQuickSave = currentTime + std::chrono::milliseconds{10000}; nlohmann::json json; @@ -246,7 +246,6 @@ class GameHandler Hooked_CTheScripts_Load (auto &&cb) { lastMissionsPassed = -1; - lastSaved = 0; return cb (); }