Changeset - r25438:91ac2a71fe27
[Not reviewed]
master
0 2 0
PeterN - 3 years ago 2021-05-12 07:11:14
peter1138@openttd.org
Fix #9202: Invalid test for unset NewGRF override mapping. (#9226)
2 files changed with 2 insertions and 2 deletions:
0 comments (0 inline, 0 general)
src/newgrf_commons.h
Show inline comments
 
@@ -125,193 +125,192 @@ struct NewGRFSpriteLayout : ZeroedMemory
 
	void Clone(const NewGRFSpriteLayout *source);
 

	
 
	/**
 
	 * Clone a spritelayout.
 
	 * @param source The spritelayout to copy.
 
	 */
 
	void Clone(const DrawTileSprites *source)
 
	{
 
		assert(source != nullptr && this != source);
 
		this->ground = source->ground;
 
		this->Clone(source->seq);
 
	}
 

	
 
	virtual ~NewGRFSpriteLayout()
 
	{
 
		free(this->seq);
 
		free(this->registers);
 
	}
 

	
 
	/**
 
	 * Tests whether this spritelayout needs preprocessing by
 
	 * #PrepareLayout() and #ProcessRegisters(), or whether it can be
 
	 * used directly.
 
	 * @return true if preprocessing is needed
 
	 */
 
	bool NeedsPreprocessing() const
 
	{
 
		return this->registers != nullptr;
 
	}
 

	
 
	uint32 PrepareLayout(uint32 orig_offset, uint32 newgrf_ground_offset, uint32 newgrf_offset, uint constr_stage, bool separate_ground) const;
 
	void ProcessRegisters(uint8 resolved_var10, uint32 resolved_sprite, bool separate_ground) const;
 

	
 
	/**
 
	 * Returns the result spritelayout after preprocessing.
 
	 * @pre #PrepareLayout() and #ProcessRegisters() need calling first.
 
	 * @return result spritelayout
 
	 */
 
	const DrawTileSeqStruct *GetLayout(PalSpriteID *ground) const
 
	{
 
		DrawTileSeqStruct *front = result_seq.data();
 
		*ground = front->image;
 
		return front + 1;
 
	}
 

	
 
private:
 
	static std::vector<DrawTileSeqStruct> result_seq; ///< Temporary storage when preprocessing spritelayouts.
 
};
 

	
 
/**
 
 * Maps an entity id stored on the map to a GRF file.
 
 * Entities are objects used ingame (houses, industries, industry tiles) for
 
 * which we need to correlate the ids from the grf files with the ones in the
 
 * the savegames themselves.
 
 * An array of EntityIDMapping structs is saved with the savegame so
 
 * that those GRFs can be loaded in a different order, or removed safely. The
 
 * index in the array is the entity's ID stored on the map.
 
 *
 
 * The substitute ID is the ID of an original entity that should be used instead
 
 * if the GRF containing the new entity is not available.
 
 */
 
struct EntityIDMapping {
 
	uint32 grfid;          ///< The GRF ID of the file the entity belongs to
 
	uint8  entity_id;      ///< The entity ID within the GRF file
 
	uint8  substitute_id;  ///< The (original) entity ID to use if this GRF is not available
 
};
 

	
 
class OverrideManagerBase {
 
protected:
 
	uint16 *entity_overrides;
 
	uint32 *grfid_overrides;
 

	
 
	uint16 max_offset;       ///< what is the length of the original entity's array of specs
 
	uint16 max_new_entities; ///< what is the amount of entities, old and new summed
 

	
 
	uint16 invalid_ID;       ///< ID used to detected invalid entities;
 
	virtual bool CheckValidNewID(uint16 testid) { return true; }
 

	
 
public:
 
	EntityIDMapping *mapping_ID; ///< mapping of ids from grf files.  Public out of convenience
 

	
 
	OverrideManagerBase(uint16 offset, uint16 maximum, uint16 invalid);
 
	virtual ~OverrideManagerBase();
 

	
 
	void ResetOverride();
 
	void ResetMapping();
 

	
 
	void Add(uint8 local_id, uint32 grfid, uint entity_type);
 
	virtual uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id);
 

	
 
	uint32 GetGRFID(uint16 entity_id) const;
 
	uint16 GetSubstituteID(uint16 entity_id) const;
 
	virtual uint16 GetID(uint8 grf_local_id, uint32 grfid) const;
 

	
 
	inline uint16 GetMaxMapping() const { return max_new_entities; }
 
	inline uint16 GetMaxOffset() const { return max_offset; }
 
	inline bool IsValidID(uint16 entity_id) const { return entity_overrides[entity_id] != invalid_ID; }
 
};
 

	
 

	
 
struct HouseSpec;
 
class HouseOverrideManager : public OverrideManagerBase {
 
public:
 
	HouseOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
 
			OverrideManagerBase(offset, maximum, invalid) {}
 

	
 
	void SetEntitySpec(const HouseSpec *hs);
 
};
 

	
 

	
 
struct IndustrySpec;
 
class IndustryOverrideManager : public OverrideManagerBase {
 
public:
 
	IndustryOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
 
			OverrideManagerBase(offset, maximum, invalid) {}
 

	
 
	uint16 AddEntityID(byte grf_local_id, uint32 grfid, byte substitute_id) override;
 
	uint16 GetID(uint8 grf_local_id, uint32 grfid) const override;
 

	
 
	void SetEntitySpec(IndustrySpec *inds);
 
};
 

	
 

	
 
struct IndustryTileSpec;
 
class IndustryTileOverrideManager : public OverrideManagerBase {
 
protected:
 
	virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
 
public:
 
	IndustryTileOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
 
			OverrideManagerBase(offset, maximum, invalid) {}
 

	
 
	void SetEntitySpec(const IndustryTileSpec *indts);
 
};
 

	
 
struct AirportSpec;
 
class AirportOverrideManager : public OverrideManagerBase {
 
public:
 
	AirportOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
 
			OverrideManagerBase(offset, maximum, invalid) {}
 

	
 
	void SetEntitySpec(AirportSpec *inds);
 
};
 

	
 
struct AirportTileSpec;
 
class AirportTileOverrideManager : public OverrideManagerBase {
 
protected:
 
	virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
 
public:
 
	AirportTileOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
 
			OverrideManagerBase(offset, maximum, invalid) {}
 

	
 
	void SetEntitySpec(const AirportTileSpec *ats);
 
};
 

	
 
struct ObjectSpec;
 
class ObjectOverrideManager : public OverrideManagerBase {
 
protected:
 
	virtual bool CheckValidNewID(uint16 testid) { return testid != 0xFF; }
 
public:
 
	ObjectOverrideManager(uint16 offset, uint16 maximum, uint16 invalid) :
 
			OverrideManagerBase(offset, maximum, invalid) {}
 

	
 
	void SetEntitySpec(ObjectSpec *spec);
 
};
 

	
 
extern HouseOverrideManager _house_mngr;
 
extern IndustryOverrideManager _industry_mngr;
 
extern IndustryTileOverrideManager _industile_mngr;
 
extern AirportOverrideManager _airport_mngr;
 
extern AirportTileOverrideManager _airporttile_mngr;
 
extern ObjectOverrideManager _object_mngr;
 

	
 
uint32 GetTerrainType(TileIndex tile, TileContext context = TCX_NORMAL);
 
TileIndex GetNearbyTile(byte parameter, TileIndex tile, bool signed_offsets = true, Axis axis = INVALID_AXIS);
 
uint32 GetNearbyTileInformation(TileIndex tile, bool grf_version8);
 
uint32 GetCompanyInfo(CompanyID owner, const struct Livery *l = nullptr);
 
CommandCost GetErrorMessageFromLocationCallbackResult(uint16 cb_res, const GRFFile *grffile, StringID default_error);
 

	
 
void ErrorUnknownCallbackResult(uint32 grfid, uint16 cbid, uint16 cb_res);
 
bool ConvertBooleanCallback(const struct GRFFile *grffile, uint16 cbid, uint16 cb_res);
 
bool Convert8bitBooleanCallback(const struct GRFFile *grffile, uint16 cbid, uint16 cb_res);
 

	
 
/**
 
 * Data related to the handling of grf files.
 
 * @tparam Tcnt Number of spritegroups
 
 */
 
template <size_t Tcnt>
 
struct GRFFilePropsBase {
 
	GRFFilePropsBase() : local_id(0), grffile(0)
 
	{
 
		/* The lack of some compilers to provide default constructors complying to the specs
 
		 * requires us to zero the stuff ourself. */
 
		memset(spritegroup, 0, sizeof(spritegroup));
src/saveload/newgrf_sl.cpp
Show inline comments
 
/*
 
 * 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/>.
 
 */
 

	
 
/** @file newgrf_sl.cpp Code handling saving and loading of newgrf config */
 

	
 
#include "../stdafx.h"
 
#include "../fios.h"
 

	
 
#include "saveload.h"
 
#include "newgrf_sl.h"
 

	
 
#include "../safeguards.h"
 

	
 
/** 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),
 
	SLE_END()
 
};
 

	
 
/**
 
 * Save a GRF ID + local id -> OpenTTD's id mapping.
 
 * @param mapping The mapping to save.
 
 */
 
void Save_NewGRFMapping(const OverrideManagerBase &mapping)
 
{
 
	for (uint i = 0; i < mapping.GetMaxMapping(); i++) {
 
		if (!mapping.IsValidID(i)) continue;
 
		if (mapping.mapping_ID[i].grfid == 0 &&
 
		    mapping.mapping_ID[i].entity_id == 0) continue;
 
		SlSetArrayIndex(i);
 
		SlObject(&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)
 
{
 
	/* 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) {
 
		if ((uint)index >= max_id) SlErrorCorrupt("Too many NewGRF entity mappings");
 
		SlObject(&mapping.mapping_ID[index], _newgrf_mapping_desc);
 
	}
 
}
 

	
 

	
 
static const SaveLoad _grfconfig_desc[] = {
 
	    SLE_STR(GRFConfig, filename,         SLE_STR,    0x40),
 
	    SLE_VAR(GRFConfig, ident.grfid,      SLE_UINT32),
 
	    SLE_ARR(GRFConfig, ident.md5sum,     SLE_UINT8,  16),
 
	SLE_CONDVAR(GRFConfig, version,          SLE_UINT32, SLV_151, SL_MAX_VERSION),
 
	    SLE_ARR(GRFConfig, param,            SLE_UINT32, 0x80),
 
	    SLE_VAR(GRFConfig, num_params,       SLE_UINT8),
 
	SLE_CONDVAR(GRFConfig, palette,          SLE_UINT8,  SLV_101, SL_MAX_VERSION),
 
	SLE_END()
 
};
 

	
 

	
 
static void Save_NGRF()
 
{
 
	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);
 
	}
 
}
 

	
 

	
 
static void Load_NGRF_common(GRFConfig *&grfconfig)
 
{
 
	ClearGRFConfigList(&grfconfig);
 
	while (SlIterateArray() != -1) {
 
		GRFConfig *c = new GRFConfig();
 
		SlObject(c, _grfconfig_desc);
 
		if (IsSavegameVersionBefore(SLV_101)) c->SetSuitablePalette();
 
		AppendToGRFConfigList(&grfconfig, c);
 
	}
 
}
 

	
 
static void Load_NGRF()
 
{
 
	Load_NGRF_common(_grfconfig);
 

	
 
	if (_game_mode == GM_MENU) {
 
		/* Intro game must not have NewGRF. */
 
		if (_grfconfig != nullptr) SlErrorCorrupt("The intro game must not use NewGRF");
 

	
 
		/* Activate intro NewGRFs (townnames) */
 
		ResetGRFConfig(false);
 
	} else {
 
		/* Append static NewGRF configuration */
 
		AppendStaticGRFConfigs(&_grfconfig);
 
	}
 
}
 

	
 
static void Check_NGRF()
 
{
 
	Load_NGRF_common(_load_check_data.grfconfig);
 
}
 

	
 
extern const ChunkHandler _newgrf_chunk_handlers[] = {
 
	{ 'NGRF', Save_NGRF, Load_NGRF, nullptr, Check_NGRF, CH_ARRAY | CH_LAST }
 
};
0 comments (0 inline, 0 general)