OpenTTD Source  20241121-master-g67a0fccfad
cheat_sl.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../stdafx.h"
11 
12 #include "saveload.h"
13 #include "compat/cheat_sl_compat.h"
14 
15 #include "../cheat_type.h"
16 
17 #include "../safeguards.h"
18 
19 static const SaveLoad _cheats_desc[] = {
20  SLE_VAR(Cheats, magic_bulldozer.been_used, SLE_BOOL),
21  SLE_VAR(Cheats, magic_bulldozer.value, SLE_BOOL),
22  SLE_VAR(Cheats, switch_company.been_used, SLE_BOOL),
23  SLE_VAR(Cheats, switch_company.value, SLE_BOOL),
24  SLE_VAR(Cheats, money.been_used, SLE_BOOL),
25  SLE_VAR(Cheats, money.value, SLE_BOOL),
26  SLE_VAR(Cheats, crossing_tunnels.been_used, SLE_BOOL),
27  SLE_VAR(Cheats, crossing_tunnels.value, SLE_BOOL),
28  SLE_VAR(Cheats, no_jetcrash.been_used, SLE_BOOL),
29  SLE_VAR(Cheats, no_jetcrash.value, SLE_BOOL),
30  SLE_VAR(Cheats, change_date.been_used, SLE_BOOL),
31  SLE_VAR(Cheats, change_date.value, SLE_BOOL),
32  SLE_VAR(Cheats, setup_prod.been_used, SLE_BOOL),
33  SLE_VAR(Cheats, setup_prod.value, SLE_BOOL),
34  SLE_VAR(Cheats, edit_max_hl.been_used, SLE_BOOL),
35  SLE_VAR(Cheats, edit_max_hl.value, SLE_BOOL),
36  SLE_CONDVAR(Cheats, station_rating.been_used, SLE_BOOL, SLV_STATION_RATING_CHEAT, SL_MAX_VERSION),
37  SLE_CONDVAR(Cheats, station_rating.value, SLE_BOOL, SLV_STATION_RATING_CHEAT, SL_MAX_VERSION),
38 };
39 
40 
42  CHTSChunkHandler() : ChunkHandler('CHTS', CH_TABLE) {}
43 
44  void Save() const override
45  {
47 
48  SlSetArrayIndex(0);
50  }
51 
52  void Load() const override
53  {
54  std::vector<SaveLoad> slt = SlCompatTableHeader(_cheats_desc, _cheats_sl_compat);
55 
57  size_t count = SlGetFieldLength();
58  std::vector<SaveLoad> oslt;
59 
60  /* Cheats were added over the years without a savegame bump. They are
61  * stored as 2 SLE_BOOLs per entry. "count" indicates how many SLE_BOOLs
62  * are stored for this savegame. So read only "count" SLE_BOOLs (and in
63  * result "count / 2" cheats). */
64  for (auto &sld : slt) {
65  count--;
66  oslt.push_back(sld);
67 
68  if (count == 0) break;
69  }
70  slt = oslt;
71  }
72 
74  SlObject(&_cheats, slt);
75  if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many CHTS entries");
76  }
77 };
78 
79 static const CHTSChunkHandler CHTS;
80 static const ChunkHandlerRef cheat_chunk_handlers[] = {
81  CHTS,
82 };
83 
84 extern const ChunkHandlerTable _cheat_chunk_handlers(cheat_chunk_handlers);
Cheats _cheats
All the cheats.
Definition: cheat.cpp:16
static WindowDesc _cheats_desc(WDP_AUTO, "cheats", 0, 0, WC_CHEATS, WC_NONE, 0, _nested_cheat_widgets)
Window description of the cheats GUI.
Loading for cheat chunks before table headers were added.
const SaveLoadCompat _cheats_sl_compat[]
Original field order for _cheats_desc.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:781
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:351
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1893
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1755
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1702
Functions/types related to saving and loading games.
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:507
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:510
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:863
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1239
@ SLV_RIFF_TO_ARRAY
294 PR#9375 Changed many CH_RIFF chunks to CH_ARRAY chunks.
Definition: saveload.h:332
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:397
@ SLV_STATION_RATING_CHEAT
320 PR#11346 Add cheat to fix station ratings at 100%.
Definition: saveload.h:364
@ SLV_TABLE_CHUNKS
295 PR#9322 Introduction of CH_TABLE and CH_SPARSE_TABLE.
Definition: saveload.h:334
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:987
void Save() const override
Save the chunk.
Definition: cheat_sl.cpp:44
void Load() const override
Load the chunk.
Definition: cheat_sl.cpp:52
WARNING! Do not remove entries in Cheats struct or change the order of the existing ones!...
Definition: cheat_type.h:26
Handlers and description of chunk.
Definition: saveload.h:461
SaveLoad type struct.
Definition: saveload.h:713