OpenTTD Source 20261112-master-gbcbec5e63f
newgrf_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 "newgrf_sl.h"
17#include "../fios.h"
18
19#include "../safeguards.h"
20
29
34{
36
37 for (uint i = 0; i < this->mapping.GetMaxMapping(); i++) {
38 if (this->mapping.mappings[i].grfid.Empty() &&
39 this->mapping.mappings[i].entity_id == 0) continue;
40 SlSetArrayIndex(i);
41 SlObject(&this->mapping.mappings[i], _newgrf_mapping_desc);
42 }
43}
44
49{
50 const std::vector<SaveLoad> slt = SlCompatTableHeader(_newgrf_mapping_desc, _newgrf_mapping_sl_compat);
51
52 /* Clear the current mapping stored.
53 * This will create the manager if ever it is not yet done */
54 this->mapping.ResetMapping();
55
56 uint max_id = this->mapping.GetMaxMapping();
57
58 int index;
59 while ((index = SlIterateArray()) != -1) {
60 if ((uint)index >= max_id) SlErrorCorrupt("Too many NewGRF entity mappings");
61 SlObject(&this->mapping.mappings[index], slt);
62 }
63}
64
65struct NGRFChunkHandler : ChunkHandler {
66 NGRFChunkHandler() : ChunkHandler("NGRF", ChunkType::Table) {}
67
68 static inline std::array<uint32_t, GRFConfig::MAX_NUM_PARAMS> param;
69 static inline uint8_t num_params;
70
71 static inline const SaveLoad description[] = {
72 SaveLoad::String("filename", SLE_OBJECT_ADDRESS(GRFConfig, filename)),
79 };
80
81 void SaveParameters(const GRFConfig &config) const
82 {
83 /* Transfer config to fixed array, ensure unused entries are blanked. */
84 param.fill(0);
85 num_params = static_cast<uint8_t>(std::size(config.param));
86 std::copy(std::begin(config.param), std::end(config.param), std::begin(param));
87 }
88
89 void Save() const override
90 {
91 SlTableHeader(description);
92
93 int index = 0;
94
95 for (const auto &c : _grfconfig) {
96 if (c->flags.Any({GRFConfigFlag::Static, GRFConfigFlag::InitOnly})) continue;
97 this->SaveParameters(*c);
98 SlSetArrayIndex(index++);
99 SlObject(c.get(), description);
100 }
101 }
102
103 void LoadParameters(GRFConfig &config) const
104 {
105 /* Transfer used part of fixed array to config. */
106 auto last = std::begin(param) + std::min<size_t>(std::size(param), num_params);
107 config.param.assign(std::begin(param), last);
108 }
109
110 void LoadCommon(GRFConfigList &grfconfig) const
111 {
112 const std::vector<SaveLoad> slt = SlCompatTableHeader(description, _grfconfig_sl_compat);
113
114 ClearGRFConfigList(grfconfig);
115 while (SlIterateArray() != -1) {
116 auto c = std::make_unique<GRFConfig>();
117 SlObject(c.get(), slt);
118 if (IsSavegameVersionBefore(SaveLoadVersion::NewGRFPalette)) c->SetSuitablePalette();
119 this->LoadParameters(*c);
120 AppendToGRFConfigList(grfconfig, std::move(c));
121 }
122 }
123
124 void Load() const override
125 {
126 this->LoadCommon(_grfconfig);
127
128 if (_game_mode == GameMode::Menu) {
129 /* Intro game must not have NewGRF. */
130 if (!_grfconfig.empty()) SlErrorCorrupt("The intro game must not use NewGRF");
131
132 /* Activate intro NewGRFs (townnames) */
133 ResetGRFConfig(false);
134 } else {
135 /* Append static NewGRF configuration */
137 }
138 }
139
140 void LoadCheck(size_t) const override
141 {
142 this->LoadCommon(_load_check_data.grfconfig);
143 }
144};
145
146static const NGRFChunkHandler NGRF;
147static const ChunkHandlerRef newgrf_chunk_handlers[] = {
148 NGRF,
149};
150
151extern const ChunkHandlerTable _newgrf_chunk_handlers(newgrf_chunk_handlers);
Declarations for savegames operations.
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition fios_gui.cpp:42
GRFConfigList _grfconfig
First item in list of current GRF set up.
void ResetGRFConfig(bool defaults)
Reset the current GRF Config to either blank or newgame settings.
void AppendStaticGRFConfigs(GRFConfigList &dst)
Appends the static GRFs to a list of GRFs.
void AppendToGRFConfigList(GRFConfigList &dst, std::unique_ptr< GRFConfig > &&el)
Appends an element to a list of GRFs.
void ClearGRFConfigList(GRFConfigList &config)
Clear a GRF Config list, freeing all nodes.
static const SaveLoad _newgrf_mapping_desc[]
Save and load the mapping between a spec and the NewGRF it came from.
Definition newgrf_sl.cpp:22
Code handling saving and loading of NewGRF mappings.
Loading of newgrf chunks before table headers were added.
const SaveLoadCompat _grfconfig_sl_compat[]
Original field order for _newgrf_desc.
const SaveLoadCompat _newgrf_mapping_sl_compat[]
Original field order for _newgrf_mapping_desc.
@ Menu
In the main menu.
Definition openttd.h:19
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
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
#define SLE_GLOBAL_ADDRESS(variable)
Macro to be able to pass the variable type and construct the SaveLoad::AddressFunction given a global...
Definition saveload.h:760
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
@ ExtendEntityMapping
Saveload version: 311, GitHub pull request: 10672 Extend entity mapping range.
Definition saveload.h:358
@ NewGRFPalette
Saveload version: 101, SVN revision: 14233 Store palette used by each of the NewGRFs.
Definition saveload.h:170
@ MinVersion
First savegame version.
Definition saveload.h:36
@ StoreNewGRFVersion
Saveload version: 151, SVN revision: 20918 Store the version of the used NewGRFs.
Definition saveload.h:230
@ 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.
ChunkHandler(ChunkId id, ChunkType type)
Create this ChunkHandler.
Definition saveload.h:469
Maps an entity id stored on the map to a GRF file.
Information about GRF, used in the game and (part of it) in savegames.
std::vector< uint32_t > param
GRF parameters.
void LoadCheck(size_t) const override
Load the chunk for game preview.
void Save() const override
Save the chunk.
Definition newgrf_sl.cpp:89
void Load() const override
Load the chunk.
void Load() const override
Load a GRF ID + local id -> OpenTTD's id mapping.
Definition newgrf_sl.cpp:48
OverrideManagerBase & mapping
The override manager to save/load.
Definition newgrf_sl.h:18
void Save() const override
Save a GRF ID + local id -> OpenTTD's id mapping.
Definition newgrf_sl.cpp:33
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
static SaveLoad Array(std::string name, AddressFunction< T > address_func, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion, size_t extra=0)
Storage of an array in some savegame versions.
Definition saveload.h:1025
static SaveLoad String(std::string name, AddressFunction< T > address_func, StringValidationSettings string_validation_settings={}, SaveLoadVersion from=SaveLoadVersion::MinVersion, SaveLoadVersion to=SaveLoadVersion::MaxVersion, size_t extra=0)
Storage of a string in some savegame versions.
Definition saveload.h:976