diff options
author | Patric Stout <truebrain@openttd.org> | 2021-06-06 09:59:33 +0200 |
---|---|---|
committer | Patric Stout <github@truebrain.nl> | 2021-06-15 19:36:15 +0200 |
commit | 88edfd4ef16bedd98a07a2142e693ab50fbdcef2 (patch) | |
tree | f39b00764786763b3350dd06c2c4014605678ca1 | |
parent | b9ab9e4d051eea7d6aedfb60930b039b778568af (diff) | |
download | openttd-88edfd4ef16bedd98a07a2142e693ab50fbdcef2.tar.xz |
Change: rework several CH_RIFF chunks to use CH_ARRAY instead
This adds two byte extra to those chunks, and might feel a bit
silly at first. But in later changes we will prefix CH_ARRAY with
a table header, and then this change shines.
Without this, we could still add headers to these chunks, but any
external reader wouldn't know if the CH_RIFF has them or not. This
way is much more practical, as they are now more like any other
chunk.
-rw-r--r-- | src/saveload/animated_tile_sl.cpp | 25 | ||||
-rw-r--r-- | src/saveload/cheat_sl.cpp | 6 | ||||
-rw-r--r-- | src/saveload/economy_sl.cpp | 6 | ||||
-rw-r--r-- | src/saveload/game_sl.cpp | 6 | ||||
-rw-r--r-- | src/saveload/industry_sl.cpp | 23 | ||||
-rw-r--r-- | src/saveload/linkgraph_sl.cpp | 5 | ||||
-rw-r--r-- | src/saveload/map_sl.cpp | 18 | ||||
-rw-r--r-- | src/saveload/misc_sl.cpp | 32 | ||||
-rw-r--r-- | src/saveload/saveload.h | 1 | ||||
-rw-r--r-- | src/settings.cpp | 7 |
10 files changed, 97 insertions, 32 deletions
diff --git a/src/saveload/animated_tile_sl.cpp b/src/saveload/animated_tile_sl.cpp index 89eb881b1..7f05eaeeb 100644 --- a/src/saveload/animated_tile_sl.cpp +++ b/src/saveload/animated_tile_sl.cpp @@ -18,13 +18,17 @@ extern std::vector<TileIndex> _animated_tiles; +static const SaveLoad _animated_tile_desc[] = { + SLEG_VECTOR(_animated_tiles, SLE_UINT32), +}; + /** * Save the ANIT chunk. */ static void Save_ANIT() { - SlSetLength(_animated_tiles.size() * sizeof(_animated_tiles.front())); - SlCopy(_animated_tiles.data(), _animated_tiles.size(), SLE_UINT32); + SlSetArrayIndex(0); + SlGlobList(_animated_tile_desc); } /** @@ -45,10 +49,17 @@ static void Load_ANIT() return; } - uint count = (uint)SlGetFieldLength() / sizeof(_animated_tiles.front()); - _animated_tiles.clear(); - _animated_tiles.resize(_animated_tiles.size() + count); - SlCopy(_animated_tiles.data(), count, SLE_UINT32); + if (IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY)) { + size_t count = SlGetFieldLength() / sizeof(_animated_tiles.front()); + _animated_tiles.clear(); + _animated_tiles.resize(_animated_tiles.size() + count); + SlCopy(_animated_tiles.data(), count, SLE_UINT32); + return; + } + + if (SlIterateArray() == -1) return; + SlGlobList(_animated_tile_desc); + if (SlIterateArray() != -1) SlErrorCorrupt("Too many ANIT entries"); } /** @@ -56,7 +67,7 @@ static void Load_ANIT() * the animated tile table. */ static const ChunkHandler animated_tile_chunk_handlers[] = { - { 'ANIT', Save_ANIT, Load_ANIT, nullptr, nullptr, CH_RIFF }, + { 'ANIT', Save_ANIT, Load_ANIT, nullptr, nullptr, CH_ARRAY }, }; extern const ChunkHandlerTable _animated_tile_chunk_handlers(animated_tile_chunk_handlers); diff --git a/src/saveload/cheat_sl.cpp b/src/saveload/cheat_sl.cpp index fde1fc176..51df50314 100644 --- a/src/saveload/cheat_sl.cpp +++ b/src/saveload/cheat_sl.cpp @@ -44,6 +44,8 @@ static const SaveLoad _cheats_desc[] = { */ static void Save_CHTS() { + SlSetArrayIndex(0); + SlSetLength(std::size(_cheats_desc)); SlObject(&_cheats, _cheats_desc); } @@ -67,12 +69,14 @@ static void Load_CHTS() if (count == 0) break; } + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return; SlObject(&_cheats, slt); + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many CHTS entries"); } /** Chunk handlers related to cheats. */ static const ChunkHandler cheat_chunk_handlers[] = { - { 'CHTS', Save_CHTS, Load_CHTS, nullptr, nullptr, CH_RIFF }, + { 'CHTS', Save_CHTS, Load_CHTS, nullptr, nullptr, CH_ARRAY }, }; extern const ChunkHandlerTable _cheat_chunk_handlers(cheat_chunk_handlers); diff --git a/src/saveload/economy_sl.cpp b/src/saveload/economy_sl.cpp index 82e3014ad..7a0f536b2 100644 --- a/src/saveload/economy_sl.cpp +++ b/src/saveload/economy_sl.cpp @@ -51,13 +51,17 @@ static const SaveLoad _economy_desc[] = { /** Economy variables */ static void Save_ECMY() { + SlSetArrayIndex(0); SlObject(&_economy, _economy_desc); } /** Economy variables */ static void Load_ECMY() { + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return; SlObject(&_economy, _economy_desc); + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many ECMY entries"); + StartupIndustryDailyChanges(IsSavegameVersionBefore(SLV_102)); // old savegames will need to be initialized } @@ -98,7 +102,7 @@ static const ChunkHandler economy_chunk_handlers[] = { { 'CAPY', Save_CAPY, Load_CAPY, Ptrs_CAPY, nullptr, CH_ARRAY }, { 'PRIC', nullptr, Load_PRIC, nullptr, nullptr, CH_RIFF }, { 'CAPR', nullptr, Load_CAPR, nullptr, nullptr, CH_RIFF }, - { 'ECMY', Save_ECMY, Load_ECMY, nullptr, nullptr, CH_RIFF }, + { 'ECMY', Save_ECMY, Load_ECMY, nullptr, nullptr, CH_ARRAY }, }; extern const ChunkHandlerTable _economy_chunk_handlers(economy_chunk_handlers); diff --git a/src/saveload/game_sl.cpp b/src/saveload/game_sl.cpp index 76a81e167..48281ed66 100644 --- a/src/saveload/game_sl.cpp +++ b/src/saveload/game_sl.cpp @@ -57,14 +57,14 @@ static void Load_GSDT() /* Free all current data */ GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME)->Change(nullptr); - if ((CompanyID)SlIterateArray() == (CompanyID)-1) return; + if (SlIterateArray() == -1) return; _game_saveload_version = -1; SlObject(nullptr, _game_script); if (_networking && !_network_server) { GameInstance::LoadEmpty(); - if ((CompanyID)SlIterateArray() != (CompanyID)-1) SlErrorCorrupt("Too many GameScript configs"); + if (SlIterateArray() != -1) SlErrorCorrupt("Too many GameScript configs"); return; } @@ -99,7 +99,7 @@ static void Load_GSDT() Game::StartNew(); Game::Load(_game_saveload_version); - if ((CompanyID)SlIterateArray() != (CompanyID)-1) SlErrorCorrupt("Too many GameScript configs"); + if (SlIterateArray() != -1) SlErrorCorrupt("Too many GameScript configs"); } static void Save_GSDT() diff --git a/src/saveload/industry_sl.cpp b/src/saveload/industry_sl.cpp index c75674069..1043023b1 100644 --- a/src/saveload/industry_sl.cpp +++ b/src/saveload/industry_sl.cpp @@ -139,12 +139,21 @@ static const SaveLoad _industry_builder_desc[] = { SLEG_VAR(_industry_builder.wanted_inds, SLE_UINT32), }; -/** Load/save industry builder. */ -static void LoadSave_IBLD() +/** Save industry builder. */ +static void Save_IBLD() { + SlSetArrayIndex(0); SlGlobList(_industry_builder_desc); } +/** Load industry builder. */ +static void Load_IBLD() +{ + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return; + SlGlobList(_industry_builder_desc); + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many IBLD entries"); +} + /** Description of the data to save and load in #IndustryTypeBuildData. */ static const SaveLoad _industrytype_builder_desc[] = { SLE_VAR(IndustryTypeBuildData, probability, SLE_UINT32), @@ -177,11 +186,11 @@ static void Load_ITBL() } static const ChunkHandler industry_chunk_handlers[] = { - { 'INDY', Save_INDY, Load_INDY, Ptrs_INDY, nullptr, CH_ARRAY }, - { 'IIDS', Save_IIDS, Load_IIDS, nullptr, nullptr, CH_ARRAY }, - { 'TIDS', Save_TIDS, Load_TIDS, nullptr, nullptr, CH_ARRAY }, - { 'IBLD', LoadSave_IBLD, LoadSave_IBLD, nullptr, nullptr, CH_RIFF }, - { 'ITBL', Save_ITBL, Load_ITBL, nullptr, nullptr, CH_ARRAY }, + { 'INDY', Save_INDY, Load_INDY, Ptrs_INDY, nullptr, CH_ARRAY }, + { 'IIDS', Save_IIDS, Load_IIDS, nullptr, nullptr, CH_ARRAY }, + { 'TIDS', Save_TIDS, Load_TIDS, nullptr, nullptr, CH_ARRAY }, + { 'IBLD', Save_IBLD, Load_IBLD, nullptr, nullptr, CH_ARRAY }, + { 'ITBL', Save_ITBL, Load_ITBL, nullptr, nullptr, CH_ARRAY }, }; extern const ChunkHandlerTable _industry_chunk_handlers(industry_chunk_handlers); diff --git a/src/saveload/linkgraph_sl.cpp b/src/saveload/linkgraph_sl.cpp index fc72d4383..7000946da 100644 --- a/src/saveload/linkgraph_sl.cpp +++ b/src/saveload/linkgraph_sl.cpp @@ -287,6 +287,7 @@ static void Load_LGRJ() */ static void Save_LGRS() { + SlSetArrayIndex(0); SlObject(&LinkGraphSchedule::instance, GetLinkGraphScheduleDesc()); } @@ -295,7 +296,9 @@ static void Save_LGRS() */ static void Load_LGRS() { + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return; SlObject(&LinkGraphSchedule::instance, GetLinkGraphScheduleDesc()); + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many LGRS entries"); } /** @@ -309,7 +312,7 @@ static void Ptrs_LGRS() static const ChunkHandler linkgraph_chunk_handlers[] = { { 'LGRP', Save_LGRP, Load_LGRP, nullptr, nullptr, CH_ARRAY }, { 'LGRJ', Save_LGRJ, Load_LGRJ, nullptr, nullptr, CH_ARRAY }, - { 'LGRS', Save_LGRS, Load_LGRS, Ptrs_LGRS, nullptr, CH_RIFF } + { 'LGRS', Save_LGRS, Load_LGRS, Ptrs_LGRS, nullptr, CH_ARRAY }, }; extern const ChunkHandlerTable _linkgraph_chunk_handlers(linkgraph_chunk_handlers); diff --git a/src/saveload/map_sl.cpp b/src/saveload/map_sl.cpp index 5924f4440..de043d513 100644 --- a/src/saveload/map_sl.cpp +++ b/src/saveload/map_sl.cpp @@ -20,7 +20,7 @@ static uint32 _map_dim_x; static uint32 _map_dim_y; -static const SaveLoad _map_dimensions[] = { +static const SaveLoad _map_desc[] = { SLEG_CONDVAR(_map_dim_x, SLE_UINT32, SLV_6, SL_MAX_VERSION), SLEG_CONDVAR(_map_dim_y, SLE_UINT32, SLV_6, SL_MAX_VERSION), }; @@ -29,18 +29,26 @@ static void Save_MAPS() { _map_dim_x = MapSizeX(); _map_dim_y = MapSizeY(); - SlGlobList(_map_dimensions); + + SlSetArrayIndex(0); + SlGlobList(_map_desc); } static void Load_MAPS() { - SlGlobList(_map_dimensions); + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return; + SlGlobList(_map_desc); + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many MAPS entries"); + AllocateMap(_map_dim_x, _map_dim_y); } static void Check_MAPS() { - SlGlobList(_map_dimensions); + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return; + SlGlobList(_map_desc); + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many MAPS entries"); + _load_check_data.map_size_x = _map_dim_x; _load_check_data.map_size_y = _map_dim_y; } @@ -295,7 +303,7 @@ static void Save_MAP8() static const ChunkHandler map_chunk_handlers[] = { - { 'MAPS', Save_MAPS, Load_MAPS, nullptr, Check_MAPS, CH_RIFF }, + { 'MAPS', Save_MAPS, Load_MAPS, nullptr, Check_MAPS, CH_ARRAY }, { 'MAPT', Save_MAPT, Load_MAPT, nullptr, nullptr, CH_RIFF }, { 'MAPH', Save_MAPH, Load_MAPH, nullptr, nullptr, CH_RIFF }, { 'MAPO', Save_MAP1, Load_MAP1, nullptr, nullptr, CH_RIFF }, diff --git a/src/saveload/misc_sl.cpp b/src/saveload/misc_sl.cpp index 027d68bae..4c66f4e26 100644 --- a/src/saveload/misc_sl.cpp +++ b/src/saveload/misc_sl.cpp @@ -118,14 +118,28 @@ static const SaveLoad _date_check_desc[] = { /* Save load date related variables as well as persistent tick counters * XXX: currently some unrelated stuff is just put here */ -static void SaveLoad_DATE() +static void Save_DATE() { + SlSetArrayIndex(0); SlGlobList(_date_desc); } +static void Load_DATE_common(const SaveLoadTable &slt) +{ + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return; + SlGlobList(slt); + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many DATE entries"); +} + +static void Load_DATE() +{ + Load_DATE_common(_date_desc); +} + static void Check_DATE() { - SlGlobList(_date_check_desc); + Load_DATE_common(_date_check_desc); + if (IsSavegameVersionBefore(SLV_31)) { _load_check_data.current_date += DAYS_TILL_ORIGINAL_BASE_YEAR; } @@ -140,14 +154,22 @@ static const SaveLoad _view_desc[] = { SLEG_VAR(_saved_scrollpos_zoom, SLE_UINT8), }; -static void SaveLoad_VIEW() +static void Save_VIEW() +{ + SlSetArrayIndex(0); + SlGlobList(_view_desc); +} + +static void Load_VIEW() { + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return; SlGlobList(_view_desc); + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many DATE entries"); } static const ChunkHandler misc_chunk_handlers[] = { - { 'DATE', SaveLoad_DATE, SaveLoad_DATE, nullptr, Check_DATE, CH_RIFF }, - { 'VIEW', SaveLoad_VIEW, SaveLoad_VIEW, nullptr, nullptr, CH_RIFF }, + { 'DATE', Save_DATE, Load_DATE, nullptr, Check_DATE, CH_ARRAY }, + { 'VIEW', Save_VIEW, Load_VIEW, nullptr, nullptr, CH_ARRAY }, }; extern const ChunkHandlerTable _misc_chunk_handlers(misc_chunk_handlers); diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 559876b97..1a4cb46e8 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -331,6 +331,7 @@ enum SaveLoadVersion : uint16 { SLV_GROUP_REPLACE_WAGON_REMOVAL, ///< 291 PR#7441 Per-group wagon removal flag. SLV_CUSTOM_SUBSIDY_DURATION, ///< 292 PR#9081 Configurable subsidy duration. SLV_SAVELOAD_LIST_LENGTH, ///< 293 PR#9374 Consistency in list length with SL_STRUCT / SL_STRUCTLIST / SL_DEQUE / SL_REFLIST. + SLV_RIFF_TO_ARRAY, ///< 294 PR#9375 Changed many CH_RIFF chunks to CH_ARRAY chunks. SL_MAX_VERSION, ///< Highest possible saveload version }; diff --git a/src/settings.cpp b/src/settings.cpp index 54189a0fc..169549955 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -2045,7 +2045,9 @@ static void LoadSettings(const SettingTable &settings, void *object) { const std::vector<SaveLoad> slt = GetSettingsDesc(settings, true); + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return; SlObject(object, slt); + if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many settings entries"); /* Ensure all IntSettings are valid (min/max could have changed between versions etc). */ for (auto &sd : settings) { @@ -2070,6 +2072,7 @@ static void SaveSettings(const SettingTable &settings, void *object) { const std::vector<SaveLoad> slt = GetSettingsDesc(settings, false); + SlSetArrayIndex(0); SlObject(object, slt); } @@ -2102,8 +2105,8 @@ static void Save_PATS() } static const ChunkHandler setting_chunk_handlers[] = { - { 'OPTS', nullptr, Load_OPTS, nullptr, nullptr, CH_RIFF }, - { 'PATS', Save_PATS, Load_PATS, nullptr, Check_PATS, CH_RIFF }, + { 'OPTS', nullptr, Load_OPTS, nullptr, nullptr, CH_RIFF }, + { 'PATS', Save_PATS, Load_PATS, nullptr, Check_PATS, CH_ARRAY }, }; extern const ChunkHandlerTable _setting_chunk_handlers(setting_chunk_handlers); |