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

OpenTTD Source 15.0-RC2

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
10#include "../stdafx.h"
11
12#include "saveload.h"
13#include "compat/newgrf_sl_compat.h"
14
15#include "newgrf_sl.h"
16#include "../fios.h"
17
18#include "../safeguards.h"
19
21 static const SaveLoad _newgrf_mapping_desc[] = {
22 SLE_VAR(EntityIDMapping, grfid, SLE_UINT32),
23 SLE_CONDVAR(EntityIDMapping, entity_id, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_EXTEND_ENTITY_MAPPING),
24 SLE_CONDVAR(EntityIDMapping, entity_id, SLE_UINT16, SLV_EXTEND_ENTITY_MAPPING, SL_MAX_VERSION),
25 SLE_CONDVAR(EntityIDMapping, substitute_id, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_EXTEND_ENTITY_MAPPING),
26 SLE_CONDVAR(EntityIDMapping, substitute_id, SLE_UINT16, SLV_EXTEND_ENTITY_MAPPING, SL_MAX_VERSION),
27};
28
32 void NewGRFMappingChunkHandler::Save() const
33{
34 SlTableHeader(_newgrf_mapping_desc);
35
36 for (uint i = 0; i < this->mapping.GetMaxMapping(); i++) {
37 if (this->mapping.mappings[i].grfid == 0 &&
38 this->mapping.mappings[i].entity_id == 0) continue;
39 SlSetArrayIndex(i);
40 SlObject(&this->mapping.mappings[i], _newgrf_mapping_desc);
41 }
42}
43
47 void NewGRFMappingChunkHandler::Load() const
48{
49 const std::vector<SaveLoad> slt = SlCompatTableHeader(_newgrf_mapping_desc, _newgrf_mapping_sl_compat);
50
51 /* Clear the current mapping stored.
52 * This will create the manager if ever it is not yet done */
53 this->mapping.ResetMapping();
54
55 uint max_id = this->mapping.GetMaxMapping();
56
57 int index;
58 while ((index = SlIterateArray()) != -1) {
59 if ((uint)index >= max_id) SlErrorCorrupt("Too many NewGRF entity mappings");
60 SlObject(&this->mapping.mappings[index], slt);
61 }
62}
63
64 struct NGRFChunkHandler : ChunkHandler {
65 NGRFChunkHandler() : ChunkHandler('NGRF', CH_TABLE) {}
66
67 static inline std::array<uint32_t, GRFConfig::MAX_NUM_PARAMS> param;
68 static inline uint8_t num_params;
69
70 static inline const SaveLoad description[] = {
71 SLE_SSTR(GRFConfig, filename, SLE_STR),
72 SLE_VAR(GRFConfig, ident.grfid, SLE_UINT32),
73 SLE_ARR(GRFConfig, ident.md5sum, SLE_UINT8, 16),
74 SLE_CONDVAR(GRFConfig, version, SLE_UINT32, SLV_151, SL_MAX_VERSION),
75 SLEG_ARR("param", param, SLE_UINT32, std::size(param)),
76 SLEG_VAR("num_params", num_params, SLE_UINT8),
77 SLE_CONDVAR(GRFConfig, palette, SLE_UINT8, SLV_101, SL_MAX_VERSION),
78 };
79
80 void SaveParameters(const GRFConfig &config) const
81 {
82 /* Transfer config to fixed array, ensure unused entries are blanked. */
83 param.fill(0);
84 num_params = static_cast<uint8_t>(std::size(config.param));
85 std::copy(std::begin(config.param), std::end(config.param), std::begin(param));
86 }
87
88 void Save() const override
89 {
90 SlTableHeader(description);
91
92 int index = 0;
93
94 for (const auto &c : _grfconfig) {
95 if (c->flags.Any({GRFConfigFlag::Static, GRFConfigFlag::InitOnly})) continue;
96 this->SaveParameters(*c);
97 SlSetArrayIndex(index++);
98 SlObject(c.get(), description);
99 }
100 }
101
102 void LoadParameters(GRFConfig &config) const
103 {
104 /* Transfer used part of fixed array to config. */
105 auto last = std::begin(param) + std::min<size_t>(std::size(param), num_params);
106 config.param.assign(std::begin(param), last);
107 }
108
109 void LoadCommon(GRFConfigList &grfconfig) const
110 {
111 const std::vector<SaveLoad> slt = SlCompatTableHeader(description, _grfconfig_sl_compat);
112
113 ClearGRFConfigList(grfconfig);
114 while (SlIterateArray() != -1) {
115 auto c = std::make_unique<GRFConfig>();
116 SlObject(c.get(), slt);
117 if (IsSavegameVersionBefore(SLV_101)) c->SetSuitablePalette();
118 this->LoadParameters(*c);
119 AppendToGRFConfigList(grfconfig, std::move(c));
120 }
121 }
122
123 void Load() const override
124 {
125 this->LoadCommon(_grfconfig);
126
127 if (_game_mode == GM_MENU) {
128 /* Intro game must not have NewGRF. */
129 if (!_grfconfig.empty()) SlErrorCorrupt("The intro game must not use NewGRF");
130
131 /* Activate intro NewGRFs (townnames) */
132 ResetGRFConfig(false);
133 } else {
134 /* Append static NewGRF configuration */
135 AppendStaticGRFConfigs(_grfconfig);
136 }
137 }
138
139 void LoadCheck(size_t) const override
140 {
141 this->LoadCommon(_load_check_data.grfconfig);
142 }
143};
144
145static const NGRFChunkHandler NGRF;
146static const ChunkHandlerRef newgrf_chunk_handlers[] = {
147 NGRF,
148};
149
150extern const ChunkHandlerTable _newgrf_chunk_handlers(newgrf_chunk_handlers);
OverrideManagerBase::ResetMapping
void ResetMapping()
Resets the mapping, which is used while initializing game.
Definition newgrf_commons.cpp:73
OverrideManagerBase::mappings
std::vector< EntityIDMapping > mappings
mapping of ids from grf files. Public out of convenience
Definition newgrf_commons.h:210
_load_check_data
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition fios_gui.cpp:41
_grfconfig
GRFConfigList _grfconfig
First item in list of current GRF set up.
Definition newgrf_config.cpp:154
ResetGRFConfig
void ResetGRFConfig(bool defaults)
Reset the current GRF Config to either blank or newgame settings.
Definition newgrf_config.cpp:407
AppendStaticGRFConfigs
void AppendStaticGRFConfigs(GRFConfigList &dst)
Appends the static GRFs to a list of GRFs.
Definition newgrf_config.cpp:388
AppendToGRFConfigList
void AppendToGRFConfigList(GRFConfigList &dst, std::unique_ptr< GRFConfig > &&el)
Appends an element to a list of GRFs.
Definition newgrf_config.cpp:399
ClearGRFConfigList
void ClearGRFConfigList(GRFConfigList &config)
Clear a GRF Config list, freeing all nodes.
Definition newgrf_config.cpp:324
_newgrf_mapping_desc
static const SaveLoad _newgrf_mapping_desc[]
Save and load the mapping between a spec and the NewGRF it came from.
Definition newgrf_sl.cpp:21
newgrf_sl.h
Code handling saving and loading of NewGRF mappings.
newgrf_sl_compat.h
Loading of newgrf chunks before table headers were added.
_grfconfig_sl_compat
const SaveLoadCompat _grfconfig_sl_compat[]
Original field order for _newgrf_desc.
Definition newgrf_sl_compat.h:23
_newgrf_mapping_sl_compat
const SaveLoadCompat _newgrf_mapping_sl_compat[]
Original field order for _newgrf_mapping_desc.
Definition newgrf_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:2037
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition saveload.cpp:677
SlErrorCorrupt
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition saveload.cpp:369
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition saveload.cpp:1846
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition saveload.cpp:1899
saveload.h
Functions/types related to saving and loading games.
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition saveload.h:524
ChunkHandlerTable
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition saveload.h:527
SLE_ARR
#define SLE_ARR(base, variable, type, length)
Storage of fixed-size array of SL_VAR elements in every version of a savegame.
Definition saveload.h:1043
SLEG_VAR
#define SLEG_VAR(name, variable, type)
Storage of a global variable in every savegame version.
Definition saveload.h:1201
SLEG_ARR
#define SLEG_ARR(name, variable, type, length)
Storage of a global fixed-size array of SL_VAR elements in every savegame version.
Definition saveload.h:1218
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition saveload.h:883
IsSavegameVersionBefore
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition saveload.h:1278
SLE_SSTR
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition saveload.h:1061
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition saveload.h:416
SL_MIN_VERSION
@ SL_MIN_VERSION
First savegame version.
Definition saveload.h:31
SLV_151
@ SLV_151
151 20918
Definition saveload.h:224
SLV_101
@ SLV_101
101 14233
Definition saveload.h:164
SLV_EXTEND_ENTITY_MAPPING
@ SLV_EXTEND_ENTITY_MAPPING
311 PR#10672 Extend entity mapping range.
Definition saveload.h:353
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition saveload.h:1017
ChunkHandler
Handlers and description of chunk.
Definition saveload.h:478
EntityIDMapping
Maps an entity id stored on the map to a GRF file.
Definition newgrf_commons.h:192
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition newgrf_config.h:158
GRFConfig::param
std::vector< uint32_t > param
GRF parameters.
Definition newgrf_config.h:185
LoadCheckData::grfconfig
GRFConfigList grfconfig
NewGrf configuration from save.
Definition fios.h:47
NGRFChunkHandler
Definition newgrf_sl.cpp:64
NGRFChunkHandler::LoadCheck
void LoadCheck(size_t) const override
Load the chunk for game preview.
Definition newgrf_sl.cpp:139
NGRFChunkHandler::Save
void Save() const override
Save the chunk.
Definition newgrf_sl.cpp:88
NGRFChunkHandler::Load
void Load() const override
Load the chunk.
Definition newgrf_sl.cpp:123
NewGRFMappingChunkHandler::Load
void Load() const override
Load a GRF ID + local id -> OpenTTD's id mapping.
Definition newgrf_sl.cpp:47
NewGRFMappingChunkHandler::Save
void Save() const override
Save a GRF ID + local id -> OpenTTD's id mapping.
Definition newgrf_sl.cpp:32
SaveLoad
SaveLoad type struct.
Definition saveload.h:732