OpenTTD Source  20241108-master-g80f628063a
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 <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../stdafx.h"
11 
12 #include "saveload.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),
25  SLE_CONDVAR(EntityIDMapping, substitute_id, SLE_FILE_U8 | SLE_VAR_U16, SL_MIN_VERSION, SLV_EXTEND_ENTITY_MAPPING),
27 };
28 
33 {
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 
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 
65 static const SaveLoad _grfconfig_desc[] = {
66  SLE_SSTR(GRFConfig, filename, SLE_STR),
67  SLE_VAR(GRFConfig, ident.grfid, SLE_UINT32),
68  SLE_ARR(GRFConfig, ident.md5sum, SLE_UINT8, 16),
69  SLE_CONDVAR(GRFConfig, version, SLE_UINT32, SLV_151, SL_MAX_VERSION),
70  SLE_ARR(GRFConfig, param, SLE_UINT32, 0x80),
71  SLE_VAR(GRFConfig, num_params, SLE_UINT8),
72  SLE_CONDVAR(GRFConfig, palette, SLE_UINT8, SLV_101, SL_MAX_VERSION),
73 };
74 
75 
77  NGRFChunkHandler() : ChunkHandler('NGRF', CH_TABLE) {}
78 
79  void Save() const override
80  {
81  SlTableHeader(_grfconfig_desc);
82 
83  int index = 0;
84 
85  for (GRFConfig *c = _grfconfig; c != nullptr; c = c->next) {
86  if (HasBit(c->flags, GCF_STATIC) || HasBit(c->flags, GCF_INIT_ONLY)) continue;
87  SlSetArrayIndex(index++);
88  SlObject(c, _grfconfig_desc);
89  }
90  }
91 
92 
93  void LoadCommon(GRFConfig *&grfconfig) const
94  {
95  const std::vector<SaveLoad> slt = SlCompatTableHeader(_grfconfig_desc, _grfconfig_sl_compat);
96 
97  ClearGRFConfigList(&grfconfig);
98  while (SlIterateArray() != -1) {
99  GRFConfig *c = new GRFConfig();
100  SlObject(c, slt);
102  AppendToGRFConfigList(&grfconfig, c);
103  }
104  }
105 
106  void Load() const override
107  {
108  this->LoadCommon(_grfconfig);
109 
110  if (_game_mode == GM_MENU) {
111  /* Intro game must not have NewGRF. */
112  if (_grfconfig != nullptr) SlErrorCorrupt("The intro game must not use NewGRF");
113 
114  /* Activate intro NewGRFs (townnames) */
115  ResetGRFConfig(false);
116  } else {
117  /* Append static NewGRF configuration */
119  }
120  }
121 
122  void LoadCheck(size_t) const override
123  {
124  this->LoadCommon(_load_check_data.grfconfig);
125  }
126 };
127 
128 static const NGRFChunkHandler NGRF;
129 static const ChunkHandlerRef newgrf_chunk_handlers[] = {
130  NGRF,
131 };
132 
133 extern const ChunkHandlerTable _newgrf_chunk_handlers(newgrf_chunk_handlers);
constexpr debug_inline bool HasBit(const T x, const uint8_t y)
Checks if a bit in a value is set.
void ResetMapping()
Resets the mapping, which is used while initializing game.
std::vector< EntityIDMapping > mappings
mapping of ids from grf files. Public out of convenience
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:41
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
Appends an element to a list of GRFs.
GRFConfig * _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(GRFConfig **dst)
Appends the static GRFs to a list of GRFs.
@ GCF_INIT_ONLY
GRF file is processed up to GLS_INIT.
Definition: newgrf_config.h:27
@ GCF_STATIC
GRF file is used statically (can be used in any MP game)
Definition: newgrf_config.h:24
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
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.
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:658
void SlErrorCorrupt(const std::string &msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:351
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:1888
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1750
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1697
Functions/types related to saving and loading games.
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:505
std::span< const ChunkHandlerRef > ChunkHandlerTable
A table of ChunkHandler entries.
Definition: saveload.h:508
#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:1001
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:861
bool IsSavegameVersionBefore(SaveLoadVersion major, uint8_t minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1227
#define SLE_SSTR(base, variable, type)
Storage of a std::string in every savegame version.
Definition: saveload.h:1019
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:395
@ SL_MIN_VERSION
First savegame version.
Definition: saveload.h:31
@ SLV_151
151 20918
Definition: saveload.h:224
@ SLV_101
101 14233
Definition: saveload.h:164
@ SLV_EXTEND_ENTITY_MAPPING
311 PR#10672 Extend entity mapping range.
Definition: saveload.h:353
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:975
Handlers and description of chunk.
Definition: saveload.h:459
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.
struct GRFConfig * next
NOSAVE: Next item in the linked list.
void SetSuitablePalette()
Set the palette of this GRFConfig to something suitable.
GRFConfig * grfconfig
NewGrf configuration from save.
Definition: fios.h:45
void LoadCheck(size_t) const override
Load the chunk for game preview.
Definition: newgrf_sl.cpp:122
void Save() const override
Save the chunk.
Definition: newgrf_sl.cpp:79
void Load() const override
Load the chunk.
Definition: newgrf_sl.cpp:106
void Load() const override
Load a GRF ID + local id -> OpenTTD's id mapping.
Definition: newgrf_sl.cpp:47
void Save() const override
Save a GRF ID + local id -> OpenTTD's id mapping.
Definition: newgrf_sl.cpp:32
SaveLoad type struct.
Definition: saveload.h:711