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/>.
8 /** @file newgrf_sl.cpp Code handling saving and loading of newgrf config */
10 #include "../stdafx.h"
13 #include "compat/newgrf_sl_compat.h"
15 #include "newgrf_sl.h"
18 #include "../safeguards.h"
20 /** Save and load the mapping between a spec and the NewGRF it came from. */
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
),
30 * Save a GRF ID + local id -> OpenTTD's id mapping.
32 void NewGRFMappingChunkHandler::Save() const
34 SlTableHeader(_newgrf_mapping_desc
);
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;
40 SlObject(&this->mapping
.mappings
[i
], _newgrf_mapping_desc
);
45 * Load a GRF ID + local id -> OpenTTD's id mapping.
47 void NewGRFMappingChunkHandler::Load() const
49 const std::vector
<SaveLoad
> slt
= SlCompatTableHeader(_newgrf_mapping_desc
, _newgrf_mapping_sl_compat
);
51 /* Clear the current mapping stored.
52 * This will create the manager if ever it is not yet done */
53 this->mapping
.ResetMapping();
55 uint max_id
= this->mapping
.GetMaxMapping();
58 while ((index
= SlIterateArray()) != -1) {
59 if ((uint
)index
>= max_id
) SlErrorCorrupt("Too many NewGRF entity mappings");
60 SlObject(&this->mapping
.mappings
[index
], slt
);
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
),
76 struct NGRFChunkHandler
: ChunkHandler
{
77 NGRFChunkHandler() : ChunkHandler('NGRF', CH_TABLE
) {}
79 void Save() const override
81 SlTableHeader(_grfconfig_desc
);
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
);
93 void LoadCommon(GRFConfig
*&grfconfig
) const
95 const std::vector
<SaveLoad
> slt
= SlCompatTableHeader(_grfconfig_desc
, _grfconfig_sl_compat
);
97 ClearGRFConfigList(&grfconfig
);
98 while (SlIterateArray() != -1) {
99 GRFConfig
*c
= new GRFConfig();
101 if (IsSavegameVersionBefore(SLV_101
)) c
->SetSuitablePalette();
102 AppendToGRFConfigList(&grfconfig
, c
);
106 void Load() const override
108 this->LoadCommon(_grfconfig
);
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");
114 /* Activate intro NewGRFs (townnames) */
115 ResetGRFConfig(false);
117 /* Append static NewGRF configuration */
118 AppendStaticGRFConfigs(&_grfconfig
);
122 void LoadCheck(size_t) const override
124 this->LoadCommon(_load_check_data
.grfconfig
);
128 static const NGRFChunkHandler NGRF
;
129 static const ChunkHandlerRef newgrf_chunk_handlers
[] = {
133 extern const ChunkHandlerTable
_newgrf_chunk_handlers(newgrf_chunk_handlers
);