Codechange: Use a common sub-class for NewGRFMapping chunks

This commit is contained in:
glx22 2021-07-04 22:44:23 +02:00 committed by Loïc Guilloux
parent 6e627f35ac
commit 7bcc7e11ff
6 changed files with 28 additions and 85 deletions

View File

@ -14,32 +14,12 @@
#include "../safeguards.h"
struct APIDChunkHandler : ChunkHandler {
APIDChunkHandler() : ChunkHandler('APID', CH_TABLE) {}
void Save() const override
{
Save_NewGRFMapping(_airport_mngr);
}
void Load() const override
{
Load_NewGRFMapping(_airport_mngr);
}
struct APIDChunkHandler : NewGRFMappingChunkHandler {
APIDChunkHandler() : NewGRFMappingChunkHandler('APID', _airport_mngr) {}
};
struct ATIDChunkHandler : ChunkHandler {
ATIDChunkHandler() : ChunkHandler('ATID', CH_TABLE) {}
void Save() const override
{
Save_NewGRFMapping(_airporttile_mngr);
}
void Load() const override
{
Load_NewGRFMapping(_airporttile_mngr);
}
struct ATIDChunkHandler : NewGRFMappingChunkHandler {
ATIDChunkHandler() : NewGRFMappingChunkHandler('ATID', _airporttile_mngr) {}
};
static const ATIDChunkHandler ATID;

View File

@ -119,32 +119,12 @@ struct INDYChunkHandler : ChunkHandler {
}
};
struct IIDSChunkHandler : ChunkHandler {
IIDSChunkHandler() : ChunkHandler('IIDS', CH_TABLE) {}
void Save() const override
{
Save_NewGRFMapping(_industry_mngr);
}
void Load() const override
{
Load_NewGRFMapping(_industry_mngr);
}
struct IIDSChunkHandler : NewGRFMappingChunkHandler {
IIDSChunkHandler() : NewGRFMappingChunkHandler('IIDS', _industry_mngr) {}
};
struct TIDSChunkHandler : ChunkHandler {
TIDSChunkHandler() : ChunkHandler('TIDS', CH_TABLE) {}
void Save() const override
{
Save_NewGRFMapping(_industile_mngr);
}
void Load() const override
{
Load_NewGRFMapping(_industile_mngr);
}
struct TIDSChunkHandler : NewGRFMappingChunkHandler {
TIDSChunkHandler() : NewGRFMappingChunkHandler('TIDS', _industile_mngr) {}
};
/** Description of the data to save and load in #IndustryBuildData. */

View File

@ -26,38 +26,36 @@ static const SaveLoad _newgrf_mapping_desc[] = {
/**
* Save a GRF ID + local id -> OpenTTD's id mapping.
* @param mapping The mapping to save.
*/
void Save_NewGRFMapping(const OverrideManagerBase &mapping)
void NewGRFMappingChunkHandler::Save() const
{
SlTableHeader(_newgrf_mapping_desc);
for (uint i = 0; i < mapping.GetMaxMapping(); i++) {
if (mapping.mapping_ID[i].grfid == 0 &&
mapping.mapping_ID[i].entity_id == 0) continue;
for (uint i = 0; i < this->mapping.GetMaxMapping(); i++) {
if (this->mapping.mapping_ID[i].grfid == 0 &&
this->mapping.mapping_ID[i].entity_id == 0) continue;
SlSetArrayIndex(i);
SlObject(&mapping.mapping_ID[i], _newgrf_mapping_desc);
SlObject(&this->mapping.mapping_ID[i], _newgrf_mapping_desc);
}
}
/**
* Load a GRF ID + local id -> OpenTTD's id mapping.
* @param mapping The mapping to load.
*/
void Load_NewGRFMapping(OverrideManagerBase &mapping)
void NewGRFMappingChunkHandler::Load() const
{
const std::vector<SaveLoad> slt = SlCompatTableHeader(_newgrf_mapping_desc, _newgrf_mapping_sl_compat);
/* Clear the current mapping stored.
* This will create the manager if ever it is not yet done */
mapping.ResetMapping();
this->mapping.ResetMapping();
uint max_id = mapping.GetMaxMapping();
uint max_id = this->mapping.GetMaxMapping();
int index;
while ((index = SlIterateArray()) != -1) {
if ((uint)index >= max_id) SlErrorCorrupt("Too many NewGRF entity mappings");
SlObject(&mapping.mapping_ID[index], slt);
SlObject(&this->mapping.mapping_ID[index], slt);
}
}

View File

@ -12,7 +12,12 @@
#include "../newgrf_commons.h"
void Save_NewGRFMapping(const OverrideManagerBase &mapping);
void Load_NewGRFMapping(OverrideManagerBase &mapping);
struct NewGRFMappingChunkHandler : ChunkHandler {
OverrideManagerBase &mapping;
NewGRFMappingChunkHandler(uint32 id, OverrideManagerBase &mapping) : ChunkHandler(id, CH_TABLE), mapping(mapping) {}
void Save() const override;
void Load() const override;
};
#endif /* SAVELOAD_NEWGRF_SL_H */

View File

@ -66,18 +66,8 @@ struct OBJSChunkHandler : ChunkHandler {
}
};
struct OBIDChunkHandler : ChunkHandler {
OBIDChunkHandler() : ChunkHandler('OBID', CH_TABLE) {}
void Save() const override
{
Save_NewGRFMapping(_object_mngr);
}
void Load() const override
{
Load_NewGRFMapping(_object_mngr);
}
struct OBIDChunkHandler : NewGRFMappingChunkHandler {
OBIDChunkHandler() : NewGRFMappingChunkHandler('OBID', _object_mngr) {}
};
static const OBIDChunkHandler OBID;

View File

@ -272,18 +272,8 @@ static const SaveLoad _town_desc[] = {
SLEG_CONDSTRUCTLIST("acceptance_matrix", SlTownAcceptanceMatrix, SLV_166, SLV_REMOVE_TOWN_CARGO_CACHE),
};
struct HIDSChunkHandler : ChunkHandler {
HIDSChunkHandler() : ChunkHandler('HIDS', CH_TABLE) {}
void Save() const override
{
Save_NewGRFMapping(_house_mngr);
}
void Load() const override
{
Load_NewGRFMapping(_house_mngr);
}
struct HIDSChunkHandler : NewGRFMappingChunkHandler {
HIDSChunkHandler() : NewGRFMappingChunkHandler('HIDS', _house_mngr) {}
};
struct CITYChunkHandler : ChunkHandler {