diff options
author | Patric Stout <truebrain@openttd.org> | 2021-06-06 23:23:12 +0200 |
---|---|---|
committer | Patric Stout <github@truebrain.nl> | 2021-06-15 16:45:04 +0200 |
commit | a146bcfe93a6c913781cfe471166359e744195d7 (patch) | |
tree | 1c4d5065705dfe540620ca974214bf36664a5361 /src/saveload/game_sl.cpp | |
parent | d0bcb9839a9080326ade985e4b109752fd88e2a6 (diff) | |
download | openttd-a146bcfe93a6c913781cfe471166359e744195d7.tar.xz |
Change: store length of SL_STRUCTLIST in the savegame
This wasn't consistently done, and often variables were used that
were read by an earlier blob. By moving it next to the struct
itself, the code becomes a bit more self-contained and easier to
read.
Additionally, this allows for external tooling to know how many
structs to expect, instead of having to know where to find the
length-field or a hard-coded value that can change at any moment.
Diffstat (limited to 'src/saveload/game_sl.cpp')
-rw-r--r-- | src/saveload/game_sl.cpp | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/src/saveload/game_sl.cpp b/src/saveload/game_sl.cpp index af01d5bd0..76a81e167 100644 --- a/src/saveload/game_sl.cpp +++ b/src/saveload/game_sl.cpp @@ -121,6 +121,8 @@ public: void Save(LanguageStrings *ls) const override { + SlSetStructListLength(ls->lines.size()); + for (const auto &string : ls->lines) { _game_saveload_string = string; SlObject(nullptr, this->GetDescription()); @@ -129,7 +131,9 @@ public: void Load(LanguageStrings *ls) const override { - for (uint32 i = 0; i < _game_saveload_strings; i++) { + uint32 length = IsSavegameVersionBefore(SLV_SAVELOAD_LIST_LENGTH) ? _game_saveload_strings : (uint32)SlGetStructListLength(UINT32_MAX); + + for (uint32 i = 0; i < length; i++) { SlObject(nullptr, this->GetDescription()); ls->lines.emplace_back(_game_saveload_string); } @@ -138,7 +142,7 @@ public: static const SaveLoad _game_language_desc[] = { SLE_SSTR(LanguageStrings, language, SLE_STR), - SLEG_VAR(_game_saveload_strings, SLE_UINT32), + SLEG_CONDVAR(_game_saveload_strings, SLE_UINT32, SL_MIN_VERSION, SLV_SAVELOAD_LIST_LENGTH), SLEG_STRUCTLIST(SlGameLanguageString), }; @@ -170,9 +174,7 @@ static void Save_GSTR() for (uint i = 0; i < _current_data->raw_strings.size(); i++) { SlSetArrayIndex(i); - LanguageStrings *ls = &_current_data->raw_strings[i]; - _game_saveload_strings = (uint32)ls->lines.size(); - SlObject(ls, _game_language_desc); + SlObject(&_current_data->raw_strings[i], _game_language_desc); } } |