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
 
@@ -64,98 +64,108 @@ GRFConfig::~GRFConfig()
 
	}
 
	CleanUpGRFText(this->name);
 
}
 

	
 
/**
 
 * Get the name of this grf. In case the name isn't known
 
 * the filename is returned.
 
 * @return The name of filename of this grf.
 
 */
 
const char *GRFConfig::GetName() const
 
{
 
	const char *name = GetGRFStringFromGRFText(this->name);
 
	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),
 
	severity(severity)
 
{
 
}
 

	
 
/**
 
 * Create a new GRFError that is a deep copy of an existing error message.
 
 * @param error The GRFError object to make a copy of.
 
 */
 
GRFError::GRFError(const GRFError &error) :
 
	custom_message(error.custom_message),
 
	data(error.data),
 
	message(error.message),
 
	severity(error.severity),
 
	num_params(error.num_params)
 
{
 
	if (error.custom_message != NULL) this->custom_message = strdup(error.custom_message);
 
	if (error.data           != NULL) this->data           = strdup(error.data);
 
	memcpy(this->param_value, error.param_value, sizeof(this->param_value));
 
}
 

	
 
GRFError::~GRFError()
 
{
 
	free(this->custom_message);
 
	free(this->data);
 
}
 

	
 
/**
 
 * 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;
 

	
 
	/* open the file */
 
	f = FioFOpenFile(config->filename, "rb", DATA_DIR, &size);
 
	if (f == NULL) return false;
 

	
 
	/* calculate md5sum */
 
	while ((len = fread(buffer, 1, (size > sizeof(buffer)) ? sizeof(buffer) : size, f)) != 0 && size != 0) {
 
		size -= len;
 
		checksum.Append(buffer, len);
 
	}
 
	checksum.Finish(config->ident.md5sum);
 

	
 
@@ -170,49 +180,49 @@ static bool CalcGRFMD5Sum(GRFConfig *con
 
 * @param is_static grf is static.
 
 * @return Operation was successfully completed.
 
 */
 
bool FillGRFDetails(GRFConfig *config, bool is_static)
 
{
 
	if (!FioCheckFileExists(config->filename)) {
 
		config->status = GCS_NOT_FOUND;
 
		return false;
 
	}
 

	
 
	/* Find and load the Action 8 information */
 
	LoadNewGRFFile(config, CONFIG_SLOT, GLS_FILESCAN);
 

	
 
	/* 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;
 
	for (c = *config; c != NULL; c = next) {
 
		next = c->next;
 
		delete c;
 
	}
 
	*config = NULL;
 
}
 

	
 

	
 
/** Copy a GRF Config list
 
 * @param dst pointer to destination list
 
 * @param src pointer to source list values
 
 * @param init_only the copied GRF will be processed up to GLS_INIT
src/newgrf_config.h
Show inline comments
 
@@ -87,48 +87,50 @@ struct GRFConfig : ZeroedMemoryAllocator
 
	GRFConfig(const GRFConfig &config);
 
	~GRFConfig();
 

	
 
	GRFIdentifier ident;       ///< grfid and md5sum to uniquely identify newgrfs
 
	uint8 original_md5sum[16]; ///< MD5 checksum of original file if only a 'compatible' file was loaded
 
	char *filename;            ///< Filename - either with or without full path
 
	struct GRFText *name;      ///< NOSAVE: GRF name (Action 0x08)
 
	struct GRFText *info;      ///< NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
 
	GRFError *error;           ///< NOSAVE: Error/Warning during GRF loading (Action 0x0B)
 

	
 
	uint8 flags;        ///< NOSAVE: GCF_Flags, bitset
 
	GRFStatus status;   ///< NOSAVE: GRFStatus, enum
 
	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);
 
void AppendToGRFConfigList(GRFConfig **dst, GRFConfig *el);
 
void ClearGRFConfigList(GRFConfig **config);
 
void ResetGRFConfig(bool defaults);
 
GRFListCompatibility IsGoodGRFConfigList(GRFConfig *grfconfig);
 
bool FillGRFDetails(GRFConfig *config, bool is_static);
 
char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last);
 

	
 
/* In newgrf_gui.cpp */
 
void ShowNewGRFSettings(bool editable, bool show_params, bool exec_changes, GRFConfig **config);
 

	
 
#ifdef ENABLE_NETWORK
 
/* For communication about GRFs over the network */
src/saveload/newgrf_sl.cpp
Show inline comments
 
@@ -26,45 +26,45 @@ static const SaveLoad _grfconfig_desc[] 
 
	    SLE_VAR(GRFConfig, num_params,       SLE_UINT8),
 
	SLE_CONDVAR(GRFConfig, windows_paletted, SLE_BOOL,   101, SL_MAX_VERSION),
 
	SLE_END()
 
};
 

	
 

	
 
static void Save_NGRF()
 
{
 
	int index = 0;
 

	
 
	for (GRFConfig *c = _grfconfig; c != NULL; 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 (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);
 
}
 

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

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