OpenTTD Source: src/saveload/cheat_sl.cpp Source File

OpenTTD Source 15.0-beta1

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
19static 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
41 struct CHTSChunkHandler : ChunkHandler {
42 CHTSChunkHandler() : ChunkHandler('CHTS', CH_TABLE) {}
43
44 void Save() const override
45 {
46 SlTableHeader(_cheats_desc);
47
48 SlSetArrayIndex(0);
49 SlObject(&_cheats, _cheats_desc);
50 }
51
52 void Load() const override
53 {
54 std::vector<SaveLoad> slt = SlCompatTableHeader(_cheats_desc, _cheats_sl_compat);
55
56 if (IsSavegameVersionBefore(SLV_TABLE_CHUNKS)) {
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
73 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() == -1) return;
74 SlObject(&_cheats, slt);
75 if (!IsSavegameVersionBefore(SLV_RIFF_TO_ARRAY) && SlIterateArray() != -1) SlErrorCorrupt("Too many CHTS entries");
76 }
77};
78
79static const CHTSChunkHandler CHTS;
80static const ChunkHandlerRef cheat_chunk_handlers[] = {
81 CHTS,
82};
83
84extern const ChunkHandlerTable _cheat_chunk_handlers(cheat_chunk_handlers);
_cheats
Cheats _cheats
All the cheats.
Definition cheat.cpp:16
cheat_sl_compat.h
Loading for cheat chunks before table headers were added.
_cheats_sl_compat
const SaveLoadCompat _cheats_sl_compat[]
Original field order for _cheats_desc.
Definition cheat_sl_compat.h:16
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition saveload.cpp:1924
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:659
SlGetFieldLength
size_t SlGetFieldLength()
Get the length of the current object.
Definition saveload.cpp:782
SlErrorCorrupt
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:351
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition saveload.cpp:1733
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition saveload.cpp:1786
saveload.h
Functions/types related to saving and loading games.
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:509
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:512
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:868
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1263
SLV_RIFF_TO_ARRAY
@ SLV_RIFF_TO_ARRAY
294 PR#9375 Changed many CH_RIFF chunks to CH_ARRAY chunks.
Definition saveload.h:332
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition saveload.h:399
SLV_STATION_RATING_CHEAT
@ SLV_STATION_RATING_CHEAT
320 PR#11346 Add cheat to fix station ratings at 100%.
Definition saveload.h:364
SLV_TABLE_CHUNKS
@ SLV_TABLE_CHUNKS
295 PR#9322 Introduction of CH_TABLE and CH_SPARSE_TABLE.
Definition saveload.h:334
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1002
CHTSChunkHandler
Definition cheat_sl.cpp:41
CHTSChunkHandler::Save
void Save() const override
Save the chunk.
Definition cheat_sl.cpp:44
CHTSChunkHandler::Load
void Load() const override
Load the chunk.
Definition cheat_sl.cpp:52
Cheats
WARNING! Do not remove entries in Cheats struct or change the order of the existing ones!...
Definition cheat_type.h:26
ChunkHandler
Handlers and description of chunk.
Definition saveload.h:463
SaveLoad
SaveLoad type struct.
Definition saveload.h:717