2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2009-01-04 15:32:25 +00:00
|
|
|
/** @file newgrf_sl.cpp Code handling saving and loading of newgrf config */
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
|
|
|
|
#include "saveload.h"
|
2021-05-30 13:59:40 +00:00
|
|
|
#include "compat/newgrf_sl_compat.h"
|
|
|
|
|
2010-08-11 18:57:48 +00:00
|
|
|
#include "newgrf_sl.h"
|
2021-05-30 13:59:40 +00:00
|
|
|
#include "../fios.h"
|
2010-08-11 18:57:48 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2010-08-11 18:57:48 +00:00
|
|
|
/** Save and load the mapping between a spec and the NewGRF it came from. */
|
|
|
|
static const SaveLoad _newgrf_mapping_desc[] = {
|
|
|
|
SLE_VAR(EntityIDMapping, grfid, SLE_UINT32),
|
|
|
|
SLE_VAR(EntityIDMapping, entity_id, SLE_UINT8),
|
|
|
|
SLE_VAR(EntityIDMapping, substitute_id, SLE_UINT8),
|
|
|
|
};
|
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/**
|
|
|
|
* Save a GRF ID + local id -> OpenTTD's id mapping.
|
|
|
|
* @param mapping The mapping to save.
|
|
|
|
*/
|
2010-08-11 18:57:48 +00:00
|
|
|
void Save_NewGRFMapping(const OverrideManagerBase &mapping)
|
|
|
|
{
|
2021-05-30 13:59:40 +00:00
|
|
|
SlTableHeader(_newgrf_mapping_desc);
|
|
|
|
|
2010-08-11 18:57:48 +00:00
|
|
|
for (uint i = 0; i < mapping.GetMaxMapping(); i++) {
|
2021-05-12 07:11:14 +00:00
|
|
|
if (mapping.mapping_ID[i].grfid == 0 &&
|
|
|
|
mapping.mapping_ID[i].entity_id == 0) continue;
|
2010-08-11 18:57:48 +00:00
|
|
|
SlSetArrayIndex(i);
|
|
|
|
SlObject(&mapping.mapping_ID[i], _newgrf_mapping_desc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/**
|
|
|
|
* Load a GRF ID + local id -> OpenTTD's id mapping.
|
|
|
|
* @param mapping The mapping to load.
|
|
|
|
*/
|
2010-08-11 18:57:48 +00:00
|
|
|
void Load_NewGRFMapping(OverrideManagerBase &mapping)
|
|
|
|
{
|
2021-05-30 13:59:40 +00:00
|
|
|
const std::vector<SaveLoad> slt = SlCompatTableHeader(_newgrf_mapping_desc, _newgrf_mapping_sl_compat);
|
|
|
|
|
2010-08-11 18:57:48 +00:00
|
|
|
/* Clear the current mapping stored.
|
|
|
|
* This will create the manager if ever it is not yet done */
|
|
|
|
mapping.ResetMapping();
|
|
|
|
|
|
|
|
uint max_id = mapping.GetMaxMapping();
|
|
|
|
|
|
|
|
int index;
|
|
|
|
while ((index = SlIterateArray()) != -1) {
|
2015-05-09 10:04:50 +00:00
|
|
|
if ((uint)index >= max_id) SlErrorCorrupt("Too many NewGRF entity mappings");
|
2021-05-30 13:59:40 +00:00
|
|
|
SlObject(&mapping.mapping_ID[index], slt);
|
2010-08-11 18:57:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-01-04 15:32:25 +00:00
|
|
|
|
|
|
|
static const SaveLoad _grfconfig_desc[] = {
|
|
|
|
SLE_STR(GRFConfig, filename, SLE_STR, 0x40),
|
2010-02-25 20:05:31 +00:00
|
|
|
SLE_VAR(GRFConfig, ident.grfid, SLE_UINT32),
|
|
|
|
SLE_ARR(GRFConfig, ident.md5sum, SLE_UINT8, 16),
|
2019-01-26 01:48:40 +00:00
|
|
|
SLE_CONDVAR(GRFConfig, version, SLE_UINT32, SLV_151, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
SLE_ARR(GRFConfig, param, SLE_UINT32, 0x80),
|
|
|
|
SLE_VAR(GRFConfig, num_params, SLE_UINT8),
|
2019-01-26 01:48:40 +00:00
|
|
|
SLE_CONDVAR(GRFConfig, palette, SLE_UINT8, SLV_101, SL_MAX_VERSION),
|
2009-01-04 15:32:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-06-07 21:24:37 +00:00
|
|
|
struct NGRFChunkHandler : ChunkHandler {
|
2021-06-10 01:21:07 +00:00
|
|
|
NGRFChunkHandler() : ChunkHandler('NGRF', CH_TABLE) {}
|
2021-05-30 13:59:40 +00:00
|
|
|
|
2021-06-07 21:24:37 +00:00
|
|
|
void Save() const override
|
|
|
|
{
|
|
|
|
SlTableHeader(_grfconfig_desc);
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2021-06-07 21:24:37 +00:00
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
for (GRFConfig *c = _grfconfig; c != nullptr; c = c->next) {
|
|
|
|
if (HasBit(c->flags, GCF_STATIC)) continue;
|
|
|
|
SlSetArrayIndex(index++);
|
|
|
|
SlObject(c, _grfconfig_desc);
|
|
|
|
}
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-07 21:24:37 +00:00
|
|
|
void LoadCommon(GRFConfig *&grfconfig) const
|
|
|
|
{
|
|
|
|
const std::vector<SaveLoad> slt = SlCompatTableHeader(_grfconfig_desc, _grfconfig_sl_compat);
|
|
|
|
|
|
|
|
ClearGRFConfigList(&grfconfig);
|
|
|
|
while (SlIterateArray() != -1) {
|
|
|
|
GRFConfig *c = new GRFConfig();
|
|
|
|
SlObject(c, slt);
|
|
|
|
if (IsSavegameVersionBefore(SLV_101)) c->SetSuitablePalette();
|
|
|
|
AppendToGRFConfigList(&grfconfig, c);
|
|
|
|
}
|
2009-01-04 15:32:25 +00:00
|
|
|
}
|
2010-06-13 14:15:58 +00:00
|
|
|
|
2021-06-07 21:24:37 +00:00
|
|
|
void Load() const override
|
|
|
|
{
|
|
|
|
this->LoadCommon(_grfconfig);
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2021-06-07 21:24:37 +00:00
|
|
|
if (_game_mode == GM_MENU) {
|
|
|
|
/* Intro game must not have NewGRF. */
|
|
|
|
if (_grfconfig != nullptr) SlErrorCorrupt("The intro game must not use NewGRF");
|
2017-03-07 20:18:54 +00:00
|
|
|
|
2021-06-07 21:24:37 +00:00
|
|
|
/* Activate intro NewGRFs (townnames) */
|
|
|
|
ResetGRFConfig(false);
|
|
|
|
} else {
|
|
|
|
/* Append static NewGRF configuration */
|
|
|
|
AppendStaticGRFConfigs(&_grfconfig);
|
|
|
|
}
|
2017-03-07 20:18:54 +00:00
|
|
|
}
|
2009-01-04 15:32:25 +00:00
|
|
|
|
2021-06-07 21:24:37 +00:00
|
|
|
void LoadCheck(size_t) const override
|
|
|
|
{
|
|
|
|
this->LoadCommon(_load_check_data.grfconfig);
|
|
|
|
}
|
|
|
|
};
|
2010-06-13 14:15:58 +00:00
|
|
|
|
2021-06-07 21:24:37 +00:00
|
|
|
static const NGRFChunkHandler NGRF;
|
2021-06-09 14:23:35 +00:00
|
|
|
static const ChunkHandlerRef newgrf_chunk_handlers[] = {
|
|
|
|
NGRF,
|
2009-01-04 15:32:25 +00:00
|
|
|
};
|
2021-05-02 20:41:34 +00:00
|
|
|
|
|
|
|
extern const ChunkHandlerTable _newgrf_chunk_handlers(newgrf_chunk_handlers);
|