From a70aa5df4938a6a4459690c4666a5c39a3f5bf98 Mon Sep 17 00:00:00 2001 From: Stephan <5377412+Taschi120@users.noreply.github.com> Date: Fri, 9 Jul 2021 21:44:02 +0200 Subject: Add #9188: netsave now keeps multiple version around, similar to autosave (#9395) --- src/network/network_client.cpp | 8 ++------ src/openttd.cpp | 20 ++------------------ src/saveload/saveload.cpp | 34 ++++++++++++++++++++++++++++++++++ src/saveload/saveload.h | 2 ++ 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/src/network/network_client.cpp b/src/network/network_client.cpp index 779ffe6fd..fe4d1249c 100644 --- a/src/network/network_client.cpp +++ b/src/network/network_client.cpp @@ -132,12 +132,8 @@ struct PacketReader : LoadFilter { */ void ClientNetworkEmergencySave() { - if (!_settings_client.gui.autosave_on_network_disconnect) return; - if (!_networking) return; - - const char *filename = "netsave.sav"; - Debug(net, 3, "Performing emergency save: {}", filename); - SaveOrLoad(filename, SLO_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR, false); + static int _netsave_ctr = 0; + DoAutoOrNetsave(_netsave_ctr, true); } diff --git a/src/openttd.cpp b/src/openttd.cpp index aba1ae986..904cdbbb1 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1392,24 +1392,8 @@ void StateGameLoop() */ static void DoAutosave() { - char buf[MAX_PATH]; - - if (_settings_client.gui.keep_all_autosave) { - GenerateDefaultSaveName(buf, lastof(buf)); - strecat(buf, ".sav", lastof(buf)); - } else { - static int _autosave_ctr = 0; - - /* generate a savegame name and number according to _settings_client.gui.max_num_autosaves */ - seprintf(buf, lastof(buf), "autosave%d.sav", _autosave_ctr); - - if (++_autosave_ctr >= _settings_client.gui.max_num_autosaves) _autosave_ctr = 0; - } - - Debug(sl, 2, "Autosaving to '{}'", buf); - if (SaveOrLoad(buf, SLO_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR) != SL_OK) { - ShowErrorMessage(STR_ERROR_AUTOSAVE_FAILED, INVALID_STRING_ID, WL_ERROR); - } + static int _autosave_ctr = 0; + DoAutoOrNetsave(_autosave_ctr); } /** diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 74978c4db..d771bf526 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -3323,6 +3323,40 @@ SaveOrLoadResult SaveOrLoad(const std::string &filename, SaveLoadOperation fop, } } +/** + * Create an autosave or netsave. + * @param counter A reference to the counter variable to be used for rotating the file name. + * @param netsave Indicates if this is a regular autosave or a netsave. + */ +void DoAutoOrNetsave(int &counter, bool netsave) +{ + char buf[MAX_PATH]; + + if (_settings_client.gui.keep_all_autosave) { + GenerateDefaultSaveName(buf, lastof(buf)); + if (!netsave) { + strecat(buf, ".sav", lastof(buf)); + } else { + strecat(buf, "-netsave.sav", lastof(buf)); + } + } else { + /* Generate a savegame name and number according to _settings_client.gui.max_num_autosaves. */ + if (!netsave) { + seprintf(buf, lastof(buf), "autosave%d.sav", counter); + } else { + seprintf(buf, lastof(buf), "netsave%d.sav", counter); + } + + if (++counter >= _settings_client.gui.max_num_autosaves) counter = 0; + } + + Debug(sl, 2, "Autosaving to '{}'", buf); + if (SaveOrLoad(buf, SLO_SAVE, DFT_GAME_FILE, AUTOSAVE_DIR) != SL_OK) { + ShowErrorMessage(STR_ERROR_AUTOSAVE_FAILED, INVALID_STRING_ID, WL_ERROR); + } +} + + /** Do a save when exiting the game (_settings_client.gui.autosave_on_exit) */ void DoExitSave() { diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 7fc802713..d79bc1416 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -381,6 +381,8 @@ void WaitTillSaved(); void ProcessAsyncSaveFinish(); void DoExitSave(); +void DoAutoOrNetsave(int &counter, bool netsave = false); + SaveOrLoadResult SaveWithFilter(struct SaveFilter *writer, bool threaded); SaveOrLoadResult LoadWithFilter(struct LoadFilter *reader); -- cgit v1.2.3-70-g09d2