OpenTTD Source 20261112-master-gbcbec5e63f
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"
13#include "saveload_error.hpp"
15
16#include "../cheat_type.h"
17
18#include "../safeguards.h"
19
20static const SaveLoad _cheats_desc[] = {
21 SaveLoad::Variable<VarFileType::Bool>("magic_bulldozer.been_used", SLE_OBJECT_ADDRESS(Cheats, magic_bulldozer.been_used)),
22 SaveLoad::Variable<VarFileType::Bool>("magic_bulldozer.value", SLE_OBJECT_ADDRESS(Cheats, magic_bulldozer.value)),
23 SaveLoad::Variable<VarFileType::Bool>("switch_company.been_used", SLE_OBJECT_ADDRESS(Cheats, switch_company.been_used)),
24 SaveLoad::Variable<VarFileType::Bool>("switch_company.value", SLE_OBJECT_ADDRESS(Cheats, switch_company.value)),
25 SaveLoad::Variable<VarFileType::Bool>("money.been_used", SLE_OBJECT_ADDRESS(Cheats, money.been_used)),
27 SaveLoad::Variable<VarFileType::Bool>("crossing_tunnels.been_used", SLE_OBJECT_ADDRESS(Cheats, crossing_tunnels.been_used)),
28 SaveLoad::Variable<VarFileType::Bool>("crossing_tunnels.value", SLE_OBJECT_ADDRESS(Cheats, crossing_tunnels.value)),
29 SaveLoad::Variable<VarFileType::Bool>("no_jetcrash.been_used", SLE_OBJECT_ADDRESS(Cheats, no_jetcrash.been_used)),
30 SaveLoad::Variable<VarFileType::Bool>("no_jetcrash.value", SLE_OBJECT_ADDRESS(Cheats, no_jetcrash.value)),
31 SaveLoad::Variable<VarFileType::Bool>("change_date.been_used", SLE_OBJECT_ADDRESS(Cheats, change_date.been_used)),
32 SaveLoad::Variable<VarFileType::Bool>("change_date.value", SLE_OBJECT_ADDRESS(Cheats, change_date.value)),
33 SaveLoad::Variable<VarFileType::Bool>("setup_prod.been_used", SLE_OBJECT_ADDRESS(Cheats, setup_prod.been_used)),
34 SaveLoad::Variable<VarFileType::Bool>("setup_prod.value", SLE_OBJECT_ADDRESS(Cheats, setup_prod.value)),
35 SaveLoad::Variable<VarFileType::Bool>("edit_max_hl.been_used", SLE_OBJECT_ADDRESS(Cheats, edit_max_hl.been_used)),
36 SaveLoad::Variable<VarFileType::Bool>("edit_max_hl.value", SLE_OBJECT_ADDRESS(Cheats, edit_max_hl.value)),
37 SaveLoad::Variable<VarFileType::Bool>("station_rating.been_used", SLE_OBJECT_ADDRESS(Cheats, station_rating.been_used), SaveLoadVersion::StationRatingCheat),
39};
40
41
42struct CHTSChunkHandler : ChunkHandler {
43 CHTSChunkHandler() : ChunkHandler("CHTS", ChunkType::Table) {}
44
45 void Save() const override
46 {
47 SlTableHeader(_cheats_desc);
48
49 SlSetArrayIndex(0);
50 SlObject(&_cheats, _cheats_desc);
51 }
52
53 void Load() const override
54 {
55 std::vector<SaveLoad> slt = SlCompatTableHeader(_cheats_desc, _cheats_sl_compat);
56
58 size_t count = SlGetFieldLength();
59 std::vector<SaveLoad> oslt;
60
61 /* Cheats were added over the years without a savegame bump. They are
62 * stored as 2 VarTypes::BOOLs per entry. "count" indicates how many VarTypes::BOOLs
63 * are stored for this savegame. So read only "count" VarTypes::BOOLs (and in
64 * result "count / 2" cheats). */
65 for (auto &sld : slt) {
66 count--;
67 oslt.push_back(sld);
68
69 if (count == 0) break;
70 }
71 slt = std::move(oslt);
72 }
73
75 SlObject(&_cheats, slt);
77 }
78};
79
80static const CHTSChunkHandler CHTS;
81static const ChunkHandlerRef cheat_chunk_handlers[] = {
82 CHTS,
83};
84
85extern 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:748
size_t SlGetFieldLength()
Get the length of the current object.
Definition saveload.cpp:874
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:372
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:753
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:512
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:515
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1225
@ StationRatingCheat
Saveload version: 320, GitHub pull request: 11346 Add cheat to fix station ratings at 100%.
Definition saveload.h:369
@ TableChunks
Saveload version: 295, GitHub pull request: 9322 Introduction of ChunkType::Table and ChunkType::Spa...
Definition saveload.h:339
@ RiffToArray
Saveload version: 294, GitHub pull request: 9375 Changed many ChunkType::Riff chunks to ChunkType::A...
Definition saveload.h:337
@ Table
An Array with a header describing the elements.
Definition saveload.h:449
Functions/types related to errors from savegames.
Definition of base types and functions in a cross-platform compatible way.
void Save() const override
Save the chunk.
Definition cheat_sl.cpp:45
void Load() const override
Load the chunk.
Definition cheat_sl.cpp:53
WARNING!
Definition cheat_type.h:26
ChunkHandler(ChunkId id, ChunkType type)
Create this ChunkHandler.
Definition saveload.h:469
SaveLoad type struct.
Definition saveload.h:763
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:925