summaryrefslogtreecommitdiff
path: root/src/saveload
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-06-06 14:01:42 +0200
committerPatric Stout <github@truebrain.nl>2021-06-10 19:18:24 +0200
commit174952440a1e0440ed61ca8cdbc9fa36cf66d757 (patch)
tree80b729674f2e014c2309435a85c0f907f4b72203 /src/saveload
parent8f323855b11faa039ce547cff9826be8e5437555 (diff)
downloadopenttd-174952440a1e0440ed61ca8cdbc9fa36cf66d757.tar.xz
Codechange: rework CHTS-chunk save/load to be more like the others
Diffstat (limited to 'src/saveload')
-rw-r--r--src/saveload/cheat_sl.cpp59
1 files changed, 41 insertions, 18 deletions
diff --git a/src/saveload/cheat_sl.cpp b/src/saveload/cheat_sl.cpp
index 970e5f3be..fde1fc176 100644
--- a/src/saveload/cheat_sl.cpp
+++ b/src/saveload/cheat_sl.cpp
@@ -14,21 +14,38 @@
#include "../safeguards.h"
+static const SaveLoad _cheats_desc[] = {
+ SLE_VAR(Cheats, magic_bulldozer.been_used, SLE_BOOL),
+ SLE_VAR(Cheats, magic_bulldozer.value, SLE_BOOL),
+ SLE_VAR(Cheats, switch_company.been_used, SLE_BOOL),
+ SLE_VAR(Cheats, switch_company.value, SLE_BOOL),
+ SLE_VAR(Cheats, money.been_used, SLE_BOOL),
+ SLE_VAR(Cheats, money.value, SLE_BOOL),
+ SLE_VAR(Cheats, crossing_tunnels.been_used, SLE_BOOL),
+ SLE_VAR(Cheats, crossing_tunnels.value, SLE_BOOL),
+ SLE_NULL(1),
+ SLE_NULL(1), // Needs to be two NULL fields. See Load_CHTS().
+ SLE_VAR(Cheats, no_jetcrash.been_used, SLE_BOOL),
+ SLE_VAR(Cheats, no_jetcrash.value, SLE_BOOL),
+ SLE_NULL(1),
+ SLE_NULL(1), // Needs to be two NULL fields. See Load_CHTS().
+ SLE_VAR(Cheats, change_date.been_used, SLE_BOOL),
+ SLE_VAR(Cheats, change_date.value, SLE_BOOL),
+ SLE_VAR(Cheats, setup_prod.been_used, SLE_BOOL),
+ SLE_VAR(Cheats, setup_prod.value, SLE_BOOL),
+ SLE_NULL(1),
+ SLE_NULL(1), // Needs to be two NULL fields. See Load_CHTS().
+ SLE_VAR(Cheats, edit_max_hl.been_used, SLE_BOOL),
+ SLE_VAR(Cheats, edit_max_hl.value, SLE_BOOL),
+};
+
/**
* Save the cheat values.
*/
static void Save_CHTS()
{
- /* Cannot use lengthof because _cheats is of type Cheats, not Cheat */
- byte count = sizeof(_cheats) / sizeof(Cheat);
- Cheat *cht = (Cheat*) &_cheats;
- Cheat *cht_last = &cht[count];
-
- SlSetLength(count * 2);
- for (; cht != cht_last; cht++) {
- SlWriteByte(cht->been_used);
- SlWriteByte(cht->value);
- }
+ SlSetLength(std::size(_cheats_desc));
+ SlObject(&_cheats, _cheats_desc);
}
/**
@@ -36,15 +53,21 @@ static void Save_CHTS()
*/
static void Load_CHTS()
{
- Cheat *cht = (Cheat*)&_cheats;
- size_t count = SlGetFieldLength() / 2;
- /* Cannot use lengthof because _cheats is of type Cheats, not Cheat */
- if (count > sizeof(_cheats) / sizeof(Cheat)) SlErrorCorrupt("Too many cheat values");
-
- for (uint i = 0; i < count; i++) {
- cht[i].been_used = (SlReadByte() != 0);
- cht[i].value = (SlReadByte() != 0);
+ size_t count = SlGetFieldLength();
+ std::vector<SaveLoad> slt;
+
+ /* Cheats were added over the years without a savegame bump. They are
+ * stored as 2 SLE_BOOLs per entry. "count" indicates how many SLE_BOOLs
+ * are stored for this savegame. So read only "count" SLE_BOOLs (and in
+ * result "count / 2" cheats). */
+ for (auto &sld : _cheats_desc) {
+ count--;
+ slt.push_back(sld);
+
+ if (count == 0) break;
}
+
+ SlObject(&_cheats, slt);
}
/** Chunk handlers related to cheats. */