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

OpenTTD Source 12.2

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"
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_VAR(EntityIDMapping, entity_id, SLE_UINT8),
24 SLE_VAR(EntityIDMapping, substitute_id, SLE_UINT8),
25 };
26
30 void NewGRFMappingChunkHandler::Save() const
31 {
32 SlTableHeader(_newgrf_mapping_desc);
33
34 for (uint i = 0; i < this->mapping.GetMaxMapping(); i++) {
35 if (this->mapping.mapping_ID[i].grfid == 0 &&
36 this->mapping.mapping_ID[i].entity_id == 0) continue;
37 SlSetArrayIndex(i);
38 SlObject(&this->mapping.mapping_ID[i], _newgrf_mapping_desc);
39 }
40 }
41
45 void NewGRFMappingChunkHandler::Load() const
46 {
47 const std::vector<SaveLoad> slt = SlCompatTableHeader(_newgrf_mapping_desc, _newgrf_mapping_sl_compat);
48
49 /* Clear the current mapping stored.
50 * This will create the manager if ever it is not yet done */
51 this->mapping.ResetMapping();
52
53 uint max_id = this->mapping.GetMaxMapping();
54
55 int index;
56 while ((index = SlIterateArray()) != -1) {
57 if ((uint)index >= max_id) SlErrorCorrupt("Too many NewGRF entity mappings");
58 SlObject(&this->mapping.mapping_ID[index], slt);
59 }
60 }
61
62
63 static const SaveLoad _grfconfig_desc[] = {
64 SLE_STR(GRFConfig, filename, SLE_STR, 0x40),
65 SLE_VAR(GRFConfig, ident.grfid, SLE_UINT32),
66 SLE_ARR(GRFConfig, ident.md5sum, SLE_UINT8, 16),
67 SLE_CONDVAR(GRFConfig, version, SLE_UINT32, SLV_151, SL_MAX_VERSION),
68 SLE_ARR(GRFConfig, param, SLE_UINT32, 0x80),
69 SLE_VAR(GRFConfig, num_params, SLE_UINT8),
70 SLE_CONDVAR(GRFConfig, palette, SLE_UINT8, SLV_101, SL_MAX_VERSION),
71 };
72
73
74 struct NGRFChunkHandler : ChunkHandler {
75 NGRFChunkHandler() : ChunkHandler('NGRF', CH_TABLE) {}
76
77 void Save() const override
78 {
79 SlTableHeader(_grfconfig_desc);
80
81 int index = 0;
82
83 for (GRFConfig *c = _grfconfig; c != nullptr; c = c->next) {
84 if (HasBit(c->flags, GCF_STATIC) || HasBit(c->flags, GCF_INIT_ONLY)) continue;
85 SlSetArrayIndex(index++);
86 SlObject(c, _grfconfig_desc);
87 }
88 }
89
90
91 void LoadCommon(GRFConfig *&grfconfig) const
92 {
93 const std::vector<SaveLoad> slt = SlCompatTableHeader(_grfconfig_desc, _grfconfig_sl_compat);
94
95 ClearGRFConfigList(&grfconfig);
96 while (SlIterateArray() != -1) {
97 GRFConfig *c = new GRFConfig();
98 SlObject(c, slt);
99 if (IsSavegameVersionBefore(SLV_101)) c->SetSuitablePalette();
100 AppendToGRFConfigList(&grfconfig, c);
101 }
102 }
103
104 void Load() const override
105 {
106 this->LoadCommon(_grfconfig);
107
108 if (_game_mode == GM_MENU) {
109 /* Intro game must not have NewGRF. */
110 if (_grfconfig != nullptr) SlErrorCorrupt("The intro game must not use NewGRF");
111
112 /* Activate intro NewGRFs (townnames) */
113 ResetGRFConfig(false);
114 } else {
115 /* Append static NewGRF configuration */
116 AppendStaticGRFConfigs(&_grfconfig);
117 }
118 }
119
120 void LoadCheck(size_t) const override
121 {
122 this->LoadCommon(_load_check_data.grfconfig);
123 }
124 };
125
126 static const NGRFChunkHandler NGRF;
127 static const ChunkHandlerRef newgrf_chunk_handlers[] = {
128 NGRF,
129 };
130
131 extern const ChunkHandlerTable _newgrf_chunk_handlers(newgrf_chunk_handlers);
GRFConfig::SetSuitablePalette
void SetSuitablePalette()
Set the palette of this GRFConfig to something suitable.
Definition: newgrf_config.cpp:148
ClearGRFConfigList
void ClearGRFConfigList(GRFConfig **config)
Clear a GRF Config list, freeing all nodes.
Definition: newgrf_config.cpp:400
ChunkHandlerRef
std::reference_wrapper< const ChunkHandler > ChunkHandlerRef
A reference to ChunkHandler.
Definition: saveload.h:445
NGRFChunkHandler
Definition: newgrf_sl.cpp:74
NGRFChunkHandler::Load
void Load() const override
Load the chunk.
Definition: newgrf_sl.cpp:104
_load_check_data
LoadCheckData _load_check_data
Data loaded from save during SL_LOAD_CHECK.
Definition: fios_gui.cpp:38
SLE_CONDVAR
#define SLE_CONDVAR(base, variable, type, from, to)
Storage of a variable in some savegame versions.
Definition: saveload.h:704
SLE_STR
#define SLE_STR(base, variable, type, length)
Storage of a string in every savegame version.
Definition: saveload.h:800
HasBit
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Definition: bitmath_func.hpp:103
LoadCheckData::grfconfig
GRFConfig * grfconfig
NewGrf configuration from save.
Definition: fios.h:43
saveload.h
_newgrf_mapping_sl_compat
const SaveLoadCompat _newgrf_mapping_sl_compat[]
Original field order for _newgrf_mapping_desc.
Definition: newgrf_sl_compat.h:16
NGRFChunkHandler::Save
void Save() const override
Save the chunk.
Definition: newgrf_sl.cpp:77
ChunkHandler
Handlers and description of chunk.
Definition: saveload.h:408
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:791
GCF_INIT_ONLY
@ GCF_INIT_ONLY
GRF file is processed up to GLS_INIT.
Definition: newgrf_config.h:28
EntityIDMapping::grfid
uint32 grfid
The GRF ID of the file the entity belongs to.
Definition: newgrf_commons.h:187
GRFConfig
Information about GRF, used in the game and (part of it) in savegames.
Definition: newgrf_config.h:155
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
OverrideManagerBase::ResetMapping
void ResetMapping()
Resets the mapping, which is used while initializing game.
Definition: newgrf_commons.cpp:82
NewGRFMappingChunkHandler::Load
void Load() const override
Load a GRF ID + local id -> OpenTTD's id mapping.
Definition: newgrf_sl.cpp:45
IsSavegameVersionBefore
static bool IsSavegameVersionBefore(SaveLoadVersion major, byte minor=0)
Checks whether the savegame is below major.
Definition: saveload.h:1026
EntityIDMapping
Maps an entity id stored on the map to a GRF file.
Definition: newgrf_commons.h:186
NewGRFMappingChunkHandler::Save
void Save() const override
Save a GRF ID + local id -> OpenTTD's id mapping.
Definition: newgrf_sl.cpp:30
_grfconfig_sl_compat
const SaveLoadCompat _grfconfig_sl_compat[]
Original field order for _newgrf_desc.
Definition: newgrf_sl_compat.h:23
SL_MAX_VERSION
@ SL_MAX_VERSION
Highest possible saveload version.
Definition: saveload.h:344
SLE_VAR
#define SLE_VAR(base, variable, type)
Storage of a variable in every version of a savegame.
Definition: saveload.h:774
AppendStaticGRFConfigs
void AppendStaticGRFConfigs(GRFConfig **dst)
Appends the static GRFs to a list of GRFs.
Definition: newgrf_config.cpp:470
SlErrorCorrupt
void NORETURN SlErrorCorrupt(const char *msg)
Error handler for corrupt savegames.
Definition: saveload.cpp:364
NGRFChunkHandler::LoadCheck
void LoadCheck(size_t) const override
Load the chunk for game preview.
Definition: newgrf_sl.cpp:120
GRFConfig::next
struct GRFConfig * next
NOSAVE: Next item in the linked list.
Definition: newgrf_config.h:183
_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
OverrideManagerBase::mapping_ID
EntityIDMapping * mapping_ID
mapping of ids from grf files. Public out of convenience
Definition: newgrf_commons.h:204
_grfconfig
GRFConfig * _grfconfig
First item in list of current GRF set up.
Definition: newgrf_config.cpp:171
SlCompatTableHeader
std::vector< SaveLoad > SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct)
Load a table header in a savegame compatible way.
Definition: saveload.cpp:2029
SLV_101
@ SLV_101
101 14233
Definition: saveload.h:168
ResetGRFConfig
void ResetGRFConfig(bool defaults)
Reset the current GRF Config to either blank or newgame settings.
Definition: newgrf_config.cpp:495
SLV_151
@ SLV_151
151 20918
Definition: saveload.h:228
SlObject
void SlObject(void *object, const SaveLoadTable &slt)
Main SaveLoad function.
Definition: saveload.cpp:1838
SlTableHeader
std::vector< SaveLoad > SlTableHeader(const SaveLoadTable &slt)
Save or Load a table header.
Definition: saveload.cpp:1891
SaveLoad
SaveLoad type struct.
Definition: saveload.h:655
newgrf_sl_compat.h
newgrf_sl.h
AppendToGRFConfigList
void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el)
Appends an element to a list of GRFs.
Definition: newgrf_config.cpp:484
SlIterateArray
int SlIterateArray()
Iterate through the elements of an array and read the whole thing.
Definition: saveload.cpp:670
GCF_STATIC
@ GCF_STATIC
GRF file is used statically (can be used in any MP game)
Definition: newgrf_config.h:25