OpenTTD Source 20260917-master-gec444f7315
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#include "../stdafx.h"
11
12#include "saveload.h"
14
15#include "../cheat_type.h"
16
17#include "../safeguards.h"
18
19static const SaveLoad _cheats_desc[] = {
20 SaveLoad::Variable<VarFileType::Bool>("magic_bulldozer.been_used", SLE_OBJECT_ADDRESS(Cheats, magic_bulldozer.been_used)),
21 SaveLoad::Variable<VarFileType::Bool>("magic_bulldozer.value", SLE_OBJECT_ADDRESS(Cheats, magic_bulldozer.value)),
22 SaveLoad::Variable<VarFileType::Bool>("switch_company.been_used", SLE_OBJECT_ADDRESS(Cheats, switch_company.been_used)),
23 SaveLoad::Variable<VarFileType::Bool>("switch_company.value", SLE_OBJECT_ADDRESS(Cheats, switch_company.value)),
24 SaveLoad::Variable<VarFileType::Bool>("money.been_used", SLE_OBJECT_ADDRESS(Cheats, money.been_used)),
26 SaveLoad::Variable<VarFileType::Bool>("crossing_tunnels.been_used", SLE_OBJECT_ADDRESS(Cheats, crossing_tunnels.been_used)),
27 SaveLoad::Variable<VarFileType::Bool>("crossing_tunnels.value", SLE_OBJECT_ADDRESS(Cheats, crossing_tunnels.value)),
28 SaveLoad::Variable<VarFileType::Bool>("no_jetcrash.been_used", SLE_OBJECT_ADDRESS(Cheats, no_jetcrash.been_used)),
29 SaveLoad::Variable<VarFileType::Bool>("no_jetcrash.value", SLE_OBJECT_ADDRESS(Cheats, no_jetcrash.value)),
30 SaveLoad::Variable<VarFileType::Bool>("change_date.been_used", SLE_OBJECT_ADDRESS(Cheats, change_date.been_used)),
31 SaveLoad::Variable<VarFileType::Bool>("change_date.value", SLE_OBJECT_ADDRESS(Cheats, change_date.value)),
32 SaveLoad::Variable<VarFileType::Bool>("setup_prod.been_used", SLE_OBJECT_ADDRESS(Cheats, setup_prod.been_used)),
33 SaveLoad::Variable<VarFileType::Bool>("setup_prod.value", SLE_OBJECT_ADDRESS(Cheats, setup_prod.value)),
34 SaveLoad::Variable<VarFileType::Bool>("edit_max_hl.been_used", SLE_OBJECT_ADDRESS(Cheats, edit_max_hl.been_used)),
35 SaveLoad::Variable<VarFileType::Bool>("edit_max_hl.value", SLE_OBJECT_ADDRESS(Cheats, edit_max_hl.value)),
36 SaveLoad::Variable<VarFileType::Bool>("station_rating.been_used", SLE_OBJECT_ADDRESS(Cheats, station_rating.been_used), SaveLoadVersion::StationRatingCheat),
38};
39
40
41struct CHTSChunkHandler : ChunkHandler {
42 CHTSChunkHandler() : ChunkHandler("CHTS", ChunkType::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
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 VarTypes::BOOLs per entry. "count" indicates how many VarTypes::BOOLs
62 * are stored for this savegame. So read only "count" VarTypes::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 = std::move(oslt);
71 }
72
74 SlObject(&_cheats, slt);
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
All the cheats.
Definition cheat.cpp:16
static WindowDesc _cheats_desc(WindowPosition::Automatic, "cheats", 0, 0, WindowClass::Cheat, WindowClass::None, {}, _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.
Types related to cheating.
A number of safeguards to prevent using unsafe methods.
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:745
size_t SlGetFieldLength()
Get the length of the current object.
Definition saveload.cpp:871
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:369
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Functions/types related to saving and loading games.
#define SLE_OBJECT_ADDRESS(base, variable)
Macro to be able to pass the variable type and construct the SaveLoad::AddressFunction given a base c...
Definition saveload.h:754
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:513
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:516
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1226
@ StationRatingCheat
Saveload version: 320, GitHub pull request: 11346 Add cheat to fix station ratings at 100%.
Definition saveload.h:370
@ TableChunks
Saveload version: 295, GitHub pull request: 9322 Introduction of ChunkType::Table and ChunkType::Spa...
Definition saveload.h:340
@ RiffToArray
Saveload version: 294, GitHub pull request: 9375 Changed many ChunkType::Riff chunks to ChunkType::A...
Definition saveload.h:338
@ Table
An Array with a header describing the elements.
Definition saveload.h:450
Definition of base types and functions in a cross-platform compatible way.
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!
Definition cheat_type.h:26
ChunkHandler(ChunkId id, ChunkType type)
Create this ChunkHandler.
Definition saveload.h:470
SaveLoad type struct.
Definition saveload.h:764
static SaveLoad Variable(std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion, size_t extra=0)
Storage of a variable in some savegame versions.
Definition saveload.h:926