From fbf0bf78d6fa0fd9dcbf1f8b7ee569846e1a9afe Mon Sep 17 00:00:00 2001 From: Dydzio Date: Sat, 8 Jul 2023 23:04:01 +0200 Subject: [PATCH] Configurable autosave limit and prefix - ingame handling --- client/CPlayerInterface.cpp | 42 +++++++++++++++++++++++++----------- client/CPlayerInterface.h | 1 - config/schemas/settings.json | 17 ++++++++++++++- 3 files changed, 45 insertions(+), 15 deletions(-) diff --git a/client/CPlayerInterface.cpp b/client/CPlayerInterface.cpp index c4d180b7d..8e1611b09 100644 --- a/client/CPlayerInterface.cpp +++ b/client/CPlayerInterface.cpp @@ -56,6 +56,7 @@ #include "../lib/mapObjects/CGTownInstance.h" #include "../lib/mapObjects/MiscObjects.h" #include "../lib/mapObjects/ObjectTemplate.h" +#include "../lib/mapping/CMapHeader.h" #include "../lib/pathfinder/CGPathNode.h" #include "../lib/CStack.h" #include "../lib/JsonNode.h" @@ -192,24 +193,39 @@ void CPlayerInterface::playerStartsTurn(PlayerColor player) void CPlayerInterface::performAutosave() { - std::string prefix = settings["session"]["saveprefix"].String(); int frequency = static_cast(settings["general"]["saveFrequency"].Integer()); - if(firstCall) + if(frequency > 0 && cb->getDate() % frequency == 0) { - autosaveCount = getLastIndex(prefix + "Autosave_"); + bool usePrefix = settings["general"]["useSavePrefix"].Bool(); + std::string prefix = std::string(); - if(firstCall > 0) //new game, not loaded + if(usePrefix) { - int index = getLastIndex(prefix + "Newgame_"); - index %= SAVES_COUNT; - cb->save("Saves/" + prefix + "Newgame_Autosave_" + std::to_string(index + 1)); + prefix = settings["general"]["savePrefix"].String(); + if(prefix.empty()) + { + prefix = cb->getMapHeader()->name.substr(0, 5) + "_"; + } + } + + autosaveCount++; + + int autosaveCountLimit = settings["general"]["autosaveCountLimit"].Integer(); + if(autosaveCountLimit > 0) + { + cb->save("Saves/" + prefix + "Autosave_" + std::to_string(autosaveCount)); + autosaveCount %= autosaveCountLimit; + } + else + { + std::string stringifiedDate = (cb->getDate(Date::MONTH) < 10 + ? std::string("0") + std::to_string(cb->getDate(Date::MONTH)) + : std::to_string(cb->getDate(Date::MONTH))) + + std::to_string(cb->getDate(Date::WEEK)) + + std::to_string(cb->getDate(Date::DAY_OF_WEEK)); + + cb->save("Saves/" + prefix + "Autosave_" + stringifiedDate); } - firstCall = 0; - } - else if(frequency > 0 && cb->getDate() % frequency == 0) - { - cb->save("Saves/" + prefix + "Autosave_" + std::to_string(autosaveCount++ + 1)); - autosaveCount %= 5; } } diff --git a/client/CPlayerInterface.h b/client/CPlayerInterface.h index 877ff2146..43ab4f7c5 100644 --- a/client/CPlayerInterface.h +++ b/client/CPlayerInterface.h @@ -64,7 +64,6 @@ class CPlayerInterface : public CGameInterface, public IUpdateable // -1 - just loaded game; 1 - just started game; 0 otherwise int firstCall; int autosaveCount; - static const int SAVES_COUNT = 5; std::pair lastBattleArmies; bool allowBattleReplay = false; diff --git a/config/schemas/settings.json b/config/schemas/settings.json index 71832ae99..4336dc217 100644 --- a/config/schemas/settings.json +++ b/config/schemas/settings.json @@ -33,7 +33,10 @@ "extraDump", "userRelativePointer", "relativePointerSpeedMultiplier", - "longTouchTimeMilliseconds" + "longTouchTimeMilliseconds", + "autosaveCountLimit", + "useSavePrefix", + "savePrefix" ], "properties" : { "playerName" : { @@ -101,6 +104,18 @@ "longTouchTimeMilliseconds" : { "type" : "number", "default" : 1000 + }, + "autosaveCountLimit" : { + "type" : "number", + "default": 5 + }, + "useSavePrefix" : { + "type": "boolean", + "default": false + }, + "savePrefix" : { + "type": "string", + "default": "" } } },