Changeset - r15582:6daf5f9adce7
[Not reviewed]
master
0 3 0
rubidium - 14 years ago 2010-07-31 09:36:09
rubidium@openttd.org
(svn r20252) -Codechange: deduplicate logic for setting a suitable (initial) palette for NewGRFs
3 files changed with 16 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/newgrf_config.cpp
Show inline comments
 
@@ -76,24 +76,34 @@ const char *GRFConfig::GetName() const
 
	return StrEmpty(name) ? this->filename : name;
 
}
 

	
 
/**
 
 * Get the grf info.
 
 * @return A string with a description of this grf.
 
 */
 
const char *GRFConfig::GetDescription() const
 
{
 
	return GetGRFStringFromGRFText(this->info);
 
}
 

	
 
/**
 
 * Set the palette of this GRFConfig to something suitable.
 
 * That is either the setting coming from the NewGRF or
 
 * the globally used palette.
 
 */
 
void GRFConfig::SetSuitablePalette()
 
{
 
 this->windows_paletted = (_use_palette == PAL_WINDOWS);
 
}
 

	
 
GRFConfig *_all_grfs;
 
GRFConfig *_grfconfig;
 
GRFConfig *_grfconfig_newgame;
 
GRFConfig *_grfconfig_static;
 

	
 
/**
 
 * Construct a new GRFError.
 
 * @param severity The severity of this error.
 
 * @param message The actual error-string.
 
 */
 
GRFError::GRFError(StringID severity, StringID message) :
 
	message(message),
 
@@ -124,26 +134,26 @@ GRFError::~GRFError()
 
}
 

	
 
/**
 
 * Update the palettes of the graphics from the config file.
 
 * This is needed because the config file gets read and parsed
 
 * before the palette is chosen (one can configure the base
 
 * graphics set governing the palette in the config after all).
 
 * As a result of this we update the settings from the config
 
 * once we have determined the palette.
 
 */
 
void UpdateNewGRFConfigPalette()
 
{
 
	for (GRFConfig *c = _grfconfig_newgame; c != NULL; c = c->next) c->windows_paletted = (_use_palette == PAL_WINDOWS);
 
	for (GRFConfig *c = _grfconfig_static;  c != NULL; c = c->next) c->windows_paletted = (_use_palette == PAL_WINDOWS);
 
	for (GRFConfig *c = _grfconfig_newgame; c != NULL; c = c->next) c->SetSuitablePalette();
 
	for (GRFConfig *c = _grfconfig_static;  c != NULL; c = c->next) c->SetSuitablePalette();
 
}
 

	
 
/** Calculate the MD5 sum for a GRF, and store it in the config.
 
 * @param config GRF to compute.
 
 * @return MD5 sum was successfully computed
 
 */
 
static bool CalcGRFMD5Sum(GRFConfig *config)
 
{
 
	FILE *f;
 
	Md5 checksum;
 
	uint8 buffer[1024];
 
	size_t len, size;
 
@@ -182,25 +192,25 @@ bool FillGRFDetails(GRFConfig *config, b
 

	
 
	/* Skip if the grfid is 0 (not read) or 0xFFFFFFFF (ttdp system grf) */
 
	if (config->ident.grfid == 0 || config->ident.grfid == 0xFFFFFFFF || config->IsOpenTTDBaseGRF()) return false;
 

	
 
	if (is_static) {
 
		/* Perform a 'safety scan' for static GRFs */
 
		LoadNewGRFFile(config, 62, GLS_SAFETYSCAN);
 

	
 
		/* GCF_UNSAFE is set if GLS_SAFETYSCAN finds unsafe actions */
 
		if (HasBit(config->flags, GCF_UNSAFE)) return false;
 
	}
 

	
 
	config->windows_paletted = (_use_palette == PAL_WINDOWS);
 
	config->SetSuitablePalette();
 

	
 
	return CalcGRFMD5Sum(config);
 
}
 

	
 

	
 
/** Clear a GRF Config list, freeing all nodes.
 
 * @param config Start of the list.
 
 * @post \a config is set to \c NULL.
 
 */
 
void ClearGRFConfigList(GRFConfig **config)
 
{
 
	GRFConfig *c, *next;
src/newgrf_config.h
Show inline comments
 
@@ -99,24 +99,26 @@ struct GRFConfig : ZeroedMemoryAllocator
 
	uint32 grf_bugs;    ///< NOSAVE: bugs in this GRF in this run, @see enum GRFBugs
 
	uint32 param[0x80]; ///< GRF parameters
 
	uint8 num_params;   ///< Number of used parameters
 
	uint8 num_valid_params; ///< Number of valid parameters (action 0x14)
 
	bool windows_paletted;  ///< Whether the NewGRF is Windows paletted or not
 

	
 
	struct GRFConfig *next; ///< NOSAVE: Next item in the linked list
 

	
 
	bool IsOpenTTDBaseGRF() const;
 

	
 
	const char *GetName() const;
 
	const char *GetDescription() const;
 

	
 
	void SetSuitablePalette();
 
};
 

	
 
extern GRFConfig *_all_grfs;          ///< First item in list of all scanned NewGRFs
 
extern GRFConfig *_grfconfig;         ///< First item in list of current GRF set up
 
extern GRFConfig *_grfconfig_newgame; ///< First item in list of default GRF set up
 
extern GRFConfig *_grfconfig_static;  ///< First item in list of static GRF set up
 

	
 
void ScanNewGRFFiles();
 
const GRFConfig *FindGRFConfig(uint32 grfid, const uint8 *md5sum = NULL);
 
GRFConfig *GetGRFConfig(uint32 grfid, uint32 mask = 0xFFFFFFFF);
 
GRFConfig **CopyGRFConfigList(GRFConfig **dst, const GRFConfig *src, bool init_only);
 
void AppendStaticGRFConfigs(GRFConfig **dst);
src/saveload/newgrf_sl.cpp
Show inline comments
 
@@ -38,25 +38,25 @@ static void Save_NGRF()
 
		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 (CheckSavegameVersion(101)) c->windows_paletted = (_use_palette == PAL_WINDOWS);
 
		if (CheckSavegameVersion(101)) c->SetSuitablePalette();
 
		AppendToGRFConfigList(&grfconfig, c);
 
	}
 
}
 

	
 
static void Load_NGRF()
 
{
 
	Load_NGRF_common(_grfconfig);
 

	
 
	/* Append static NewGRF configuration */
 
	AppendStaticGRFConfigs(&_grfconfig);
 
}
 

	
0 comments (0 inline, 0 general)