OpenTTD Source  12.0-beta2
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 };
37 
38 
40  CHTSChunkHandler() : ChunkHandler('CHTS', CH_TABLE) {}
41 
42  void Save() const override
43  {
45 
46  SlSetArrayIndex(0);
48  }
49 
50  void Load() const override
51  {
52  std::vector<SaveLoad> slt = SlCompatTableHeader(_cheats_desc, _cheats_sl_compat);
53 
55  size_t count = SlGetFieldLength();
56  std::vector<SaveLoad> oslt;
57 
58  /* Cheats were added over the years without a savegame bump. They are
59  * stored as 2 SLE_BOOLs per entry. "count" indicates how many SLE_BOOLs
60  * are stored for this savegame. So read only "count" SLE_BOOLs (and in
61  * result "count / 2" cheats). */
62  for (auto &sld : slt) {
63  count--;
64  oslt.push_back(sld);
65 
66  if (count == 0) break;
67  }
68  slt = oslt;
69  }
70 
72  SlObject(&_cheats, slt);
73  if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many CHTS entries");
74  }
75 };
76 
77 static const CHTSChunkHandler CHTS;
78 static const ChunkHandlerRef cheat_chunk_handlers[] = {
79  CHTS,
80 };
81 
82 extern const ChunkHandlerTable _cheat_chunk_handlers(cheat_chunk_handlers);
SLV_RIFF_TO_ARRAY
@ SLV_RIFF_TO_ARRAY
294 PR#9375 Changed many CH_RIFF chunks to CH_ARRAY chunks.
Definition: saveload.h:336
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:443
_cheats_desc
static WindowDesc _cheats_desc(WDP_AUTO, "cheats", 0, 0, WC_CHEATS, WC_NONE, 0, _nested_cheat_widgets, lengthof(_nested_cheat_widgets))
Window description of the cheats GUI.
saveload.h
_cheats_sl_compat
const SaveLoadCompat _cheats_sl_compat[]
Original field order for _cheats_desc.
Definition: cheat_sl_compat.h:16
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:406
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
SLV_TABLE_CHUNKS
@ SLV_TABLE_CHUNKS
295 PR#9322 Introduction of CH_TABLE and CH_SPARSE_TABLE.
Definition: saveload.h:338
CHTSChunkHandler
Definition: cheat_sl.cpp:39
_cheats
Cheats _cheats
All the cheats.
Definition: cheat.cpp:16
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1024
Cheats
WARNING! Do not remove entries in Cheats struct or change the order of the existing ones!...
Definition: cheat_type.h:26
CHTSChunkHandler::Save
void Save() const override
Save the chunk.
Definition: cheat_sl.cpp:42
cheat_sl_compat.h
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:772
SlGetFieldLength
size_t SlGetFieldLength()
Get the length of the current object.
Definition: saveload.cpp:792
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:364
CHTSChunkHandler::Load
void Load() const override
Load the chunk.
Definition: cheat_sl.cpp:50
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:2029
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1838
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1891
SaveLoad
SaveLoad type struct.
Definition: saveload.h:653
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:670