From ea7b64786f839e7dbda09b2a102a3250382e1874 Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 11 Feb 2024 08:29:54 +0100 Subject: [PATCH] Feature: Show revision of newer savegames that have this information --- src/lang/english.txt | 2 +- src/saveload/saveload.cpp | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/lang/english.txt b/src/lang/english.txt index 93dea7e8c4..c4732bdfd8 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -4902,7 +4902,7 @@ STR_ERROR_UNABLE_TO_DELETE_FILE :{WHITE}Unable t STR_ERROR_GAME_LOAD_FAILED :{WHITE}Game Load Failed{}{STRING1} STR_GAME_SAVELOAD_ERROR_BROKEN_INTERNAL_ERROR :Internal error: {RAW_STRING} STR_GAME_SAVELOAD_ERROR_BROKEN_SAVEGAME :Broken savegame - {RAW_STRING} -STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME :Savegame is made with newer version +STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME_WITH_REVISION :Savegame is made with newer/unsupported version {RAW_STRING} STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE :File not readable STR_GAME_SAVELOAD_ERROR_FILE_NOT_WRITEABLE :File not writeable STR_GAME_SAVELOAD_ERROR_DATA_INTEGRITY_CHECK_FAILED :Data integrity check failed diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index 22b24748f9..42992835b7 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -42,6 +42,7 @@ #include "../string_func.h" #include "../fios.h" #include "../error.h" +#include "../rev.h" #include #ifdef __EMSCRIPTEN__ # include @@ -2774,9 +2775,11 @@ static SaveOrLoadResult SaveFileToDisk(bool threaded) byte compression; const SaveLoadFormat *fmt = GetSavegameFormat(_savegame_format, &compression); + byte revision_length = ClampTo(strlen(_openttd_revision) + 1); /* We have written our stuff to memory, now write it to file! */ - uint32_t hdr[2] = { fmt->tag, TO_BE32(SAVEGAME_VERSION << 16) }; + uint32_t hdr[2] = { fmt->tag, TO_BE32(SAVEGAME_VERSION << 16 | revision_length) }; _sl.sf->Write((byte*)hdr, sizeof(hdr)); + _sl.sf->Write((const byte*)_openttd_revision, revision_length); _sl.sf = fmt->init_write(_sl.sf, compression); _sl.dumper->Flush(_sl.sf); @@ -2920,10 +2923,18 @@ static SaveOrLoadResult DoLoad(std::shared_ptr reader, bool load_che * Therefore it is loaded, but never saved (or, it saves a 0 in any scenario). */ _sl_minor_version = (TO_BE32(hdr[1]) >> 8) & 0xFF; - Debug(sl, 1, "Loading savegame version {}", _sl_version); + byte revision_length = static_cast(TO_BE32(hdr[1]) & 0xFF); + std::string revision; + if (_sl_version > SLV_END_PATCHPACKS && revision_length > 0) { + char buffer[0xFF]; + if (_sl.lf->Read((byte*)buffer, revision_length) != revision_length) SlError(STR_GAME_SAVELOAD_ERROR_FILE_NOT_READABLE); + revision = StrMakeValid(std::string_view(buffer, revision_length)); + } + + Debug(sl, 1, "Loading savegame version {} ({})", _sl_version, revision); /* Is the version higher than the current? */ - if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME); + if (_sl_version > SAVEGAME_VERSION) SlError(STR_GAME_SAVELOAD_ERROR_TOO_NEW_SAVEGAME_WITH_REVISION, revision); if (_sl_version >= SLV_START_PATCHPACKS && _sl_version <= SLV_END_PATCHPACKS) SlError(STR_GAME_SAVELOAD_ERROR_PATCHPACK); break; }