Changeset - r14667:a7ee51a0cfe3
[Not reviewed]
master
0 7 0
yexo - 15 years ago 2010-02-25 20:06:11
yexo@openttd.org
(svn r19256) -Codechange: use a constructor/destructor for GRFConfig to make sure all members are properly initialized
7 files changed with 32 insertions and 33 deletions:
0 comments (0 inline, 0 general)
src/gfxinit.cpp
Show inline comments
 
@@ -175,24 +175,23 @@ static void LoadSpriteTables()
 
	/*
 
	 * Load the base NewGRF with OTTD required graphics as first NewGRF.
 
	 * However, we do not want it to show up in the list of used NewGRFs,
 
	 * so we have to manually add it, and then remove it later.
 
	 */
 
	GRFConfig *top = _grfconfig;
 
	GRFConfig *master = CallocT<GRFConfig>(1);
 
	master->filename = strdup(used_set->files[GFT_EXTRA].filename);
 
	GRFConfig *master = new GRFConfig(used_set->files[GFT_EXTRA].filename);
 
	FillGRFDetails(master, false);
 
	master->windows_paletted = (used_set->palette == PAL_WINDOWS);
 
	ClrBit(master->flags, GCF_INIT_ONLY);
 
	master->next = top;
 
	_grfconfig = master;
 

	
 
	LoadNewGRF(SPR_NEWGRFS_BASE, i);
 

	
 
	/* Free and remove the top element. */
 
	ClearGRFConfig(&master);
 
	delete master;
 
	_grfconfig = top;
 
}
 

	
 

	
 
void GfxLoadSprites()
 
{
src/network/core/udp.cpp
Show inline comments
 
@@ -235,13 +235,13 @@ void NetworkUDPSocketHandler::Recv_Netwo
 
			uint num_grfs = p->Recv_uint8();
 

	
 
			/* Broken/bad data. It cannot have that many NewGRFs. */
 
			if (num_grfs > NETWORK_MAX_GRF_COUNT) return;
 

	
 
			for (i = 0; i < num_grfs; i++) {
 
				GRFConfig *c = CallocT<GRFConfig>(1);
 
				GRFConfig *c = new GRFConfig();
 
				this->Recv_GRFIdentifier(p, &c->ident);
 
				this->HandleIncomingNetworkGameInfoGRFConfig(c);
 

	
 
				/* Append GRFConfig to the list */
 
				*dst = c;
 
				dst = &c->next;
src/newgrf_config.cpp
Show inline comments
 
@@ -17,12 +17,27 @@
 
#include "network/network_func.h"
 
#include "gfx_func.h"
 

	
 
#include "fileio_func.h"
 
#include "fios.h"
 

	
 
GRFConfig::GRFConfig(const char *filename)
 
{
 
	if (filename != NULL) this->filename = strdup(filename);
 
}
 

	
 
GRFConfig::~GRFConfig()
 
{
 
	/* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
 
	if (!HasBit(this->flags, GCF_COPY)) {
 
		free(this->filename);
 
		free(this->name);
 
		free(this->info);
 
		delete this->error;
 
	}
 
}
 

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

	
 
@@ -102,46 +117,32 @@ bool FillGRFDetails(GRFConfig *config, b
 
	config->windows_paletted = (_use_palette == PAL_WINDOWS);
 

	
 
	return CalcGRFMD5Sum(config);
 
}
 

	
 

	
 
void ClearGRFConfig(GRFConfig **config)
 
{
 
	/* GCF_COPY as in NOT strdupped/alloced the filename, name and info */
 
	if (!HasBit((*config)->flags, GCF_COPY)) {
 
		free((*config)->filename);
 
		free((*config)->name);
 
		free((*config)->info);
 
		delete (*config)->error;
 
	}
 
	free(*config);
 
	*config = NULL;
 
}
 

	
 

	
 
/* Clear a GRF Config list */
 
void ClearGRFConfigList(GRFConfig **config)
 
{
 
	GRFConfig *c, *next;
 
	for (c = *config; c != NULL; c = next) {
 
		next = c->next;
 
		ClearGRFConfig(&c);
 
		delete c;
 
	}
 
	*config = NULL;
 
}
 

	
 

	
 
/**
 
 * Make a deep copy of a GRFConfig.
 
 * @param c the grfconfig to copy
 
 * @return A pointer to a new grfconfig that's a copy of the original
 
 */
 
GRFConfig *DuplicateGRFConfig(const GRFConfig *c)
 
{
 
	GRFConfig *config = MallocT<GRFConfig>(1);
 
	GRFConfig *config = new GRFConfig();
 
	*config = *c;
 

	
 
	if (c->filename != NULL) config->filename = strdup(c->filename);
 
	if (c->name     != NULL) config->name = strdup(c->name);
 
	if (c->info     != NULL) config->info = strdup(c->info);
 
	if (c->error    != NULL) {
 
@@ -200,13 +201,13 @@ static void RemoveDuplicatesFromGRFConfi
 
	if (list == NULL) return;
 

	
 
	for (prev = list, cur = list->next; cur != NULL; prev = cur, cur = cur->next) {
 
		if (cur->ident.grfid != list->ident.grfid) continue;
 

	
 
		prev->next = cur->next;
 
		ClearGRFConfig(&cur);
 
		delete cur;
 
		cur = prev; // Just go back one so it continues as normal later on
 
	}
 

	
 
	RemoveDuplicatesFromGRFConfigList(list->next);
 
}
 

	
 
@@ -317,14 +318,13 @@ public:
 
		return fs.Scan(".grf", DATA_DIR);
 
	}
 
};
 

	
 
bool GRFFileScanner::AddFile(const char *filename, size_t basepath_length)
 
{
 
	GRFConfig *c = CallocT<GRFConfig>(1);
 
	c->filename = strdup(filename + basepath_length);
 
	GRFConfig *c = new GRFConfig(filename + basepath_length);
 

	
 
	bool added = true;
 
	if (FillGRFDetails(c, false)) {
 
		if (_all_grfs == NULL) {
 
			_all_grfs = c;
 
		} else {
 
@@ -352,13 +352,13 @@ bool GRFFileScanner::AddFile(const char 
 
		added = false;
 
	}
 

	
 
	if (!added) {
 
		/* File couldn't be opened, or is either not a NewGRF or is a
 
		 * 'system' NewGRF or it's already known, so forget about it. */
 
		ClearGRFConfig(&c);
 
		delete c;
 
	}
 

	
 
	return added;
 
}
 

	
 
/**
src/newgrf_config.h
Show inline comments
 
@@ -66,13 +66,16 @@ struct GRFError : ZeroedMemoryAllocator 
 
	StringID severity;     ///< Info / Warning / Error / Fatal
 
	uint8 num_params;      ///< Number of additinal parameters for message and custom_message (0, 1 or 2)
 
	uint32 param_value[2]; ///< Values of GRF parameters to show for message and custom_message
 
};
 

	
 
/** Information about GRF, used in the game and (part of it) in savegames */
 
struct GRFConfig {
 
struct GRFConfig : ZeroedMemoryAllocator {
 
	GRFConfig(const char *filename = NULL);
 
	~GRFConfig();
 

	
 
	GRFIdentifier ident; ///< grfid and md5sum to uniquely identify newgrfs
 
	char *filename;     ///< Filename - either with or without full path
 
	char *name;         ///< NOSAVE: GRF name (Action 0x08)
 
	char *info;         ///< NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
 
	GRFError *error;    ///< NOSAVE: Error/Warning during GRF loading (Action 0x0B)
 

	
 
@@ -96,13 +99,12 @@ extern GRFConfig *_grfconfig_static;  //
 
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 ClearGRFConfig(GRFConfig **config);
 
void ClearGRFConfigList(GRFConfig **config);
 
void ResetGRFConfig(bool defaults);
 
GRFListCompatibility IsGoodGRFConfigList();
 
bool FillGRFDetails(GRFConfig *config, bool is_static);
 
char *GRFBuildParamList(char *dst, const GRFConfig *c, const char *last);
 
GRFConfig *DuplicateGRFConfig(const GRFConfig *c);
src/saveload/newgrf_sl.cpp
Show inline comments
 
@@ -41,13 +41,13 @@ static void Save_NGRF()
 

	
 

	
 
static void Load_NGRF()
 
{
 
	ClearGRFConfigList(&_grfconfig);
 
	while (SlIterateArray() != -1) {
 
		GRFConfig *c = CallocT<GRFConfig>(1);
 
		GRFConfig *c = new GRFConfig();
 
		SlObject(c, _grfconfig_desc);
 
		if (CheckSavegameVersion(101)) c->windows_paletted = (_use_palette == PAL_WINDOWS);
 
		AppendToGRFConfigList(&_grfconfig, c);
 
	}
 

	
 
	/* Append static NewGRF configuration */
src/saveload/oldloader_sl.cpp
Show inline comments
 
@@ -1516,15 +1516,14 @@ static bool LoadTTDPatchExtraChunks(Load
 

	
 
				ClearGRFConfigList(&_grfconfig);
 
				while (len != 0) {
 
					uint32 grfid = ReadUint32(ls);
 

	
 
					if (ReadByte(ls) == 1) {
 
						GRFConfig *c = CallocT<GRFConfig>(1);
 
						GRFConfig *c = new GRFConfig("TTDP game, no information");
 
						c->ident.grfid = grfid;
 
						c->filename = strdup("TTDP game, no information");
 

	
 
						AppendToGRFConfigList(&_grfconfig, c);
 
						DEBUG(oldloader, 3, "TTDPatch game using GRF file with GRFID %0X", BSWAP32(c->ident.grfid));
 
					}
 
					len -= 5;
 
				};
src/settings.cpp
Show inline comments
 
@@ -1187,14 +1187,13 @@ static GRFConfig *GRFLoadConfig(IniFile 
 
	GRFConfig *first = NULL;
 
	GRFConfig **curr = &first;
 

	
 
	if (group == NULL) return NULL;
 

	
 
	for (item = group->item; item != NULL; item = item->next) {
 
		GRFConfig *c = CallocT<GRFConfig>(1);
 
		c->filename = strdup(item->name);
 
		GRFConfig *c = new GRFConfig(item->name);
 

	
 
		/* Parse parameters */
 
		if (!StrEmpty(item->value)) {
 
			c->num_params = parse_intlist(item->value, (int*)c->param, lengthof(c->param));
 
			if (c->num_params == (byte)-1) {
 
				ShowInfoF("ini: error in array '%s'", item->name);
 
@@ -1214,13 +1213,13 @@ static GRFConfig *GRFLoadConfig(IniFile 
 
				msg = "system NewGRF";
 
			} else {
 
				msg = "unknown";
 
			}
 

	
 
			ShowInfoF("ini: ignoring invalid NewGRF '%s': %s", item->name, msg);
 
			ClearGRFConfig(&c);
 
			delete c;
 
			continue;
 
		}
 

	
 
		/* Check for duplicate GRFID (will also check for duplicate filenames) */
 
		bool duplicate = false;
 
		for (const GRFConfig *gc = first; gc != NULL; gc = gc->next) {
 
@@ -1228,13 +1227,13 @@ static GRFConfig *GRFLoadConfig(IniFile 
 
				ShowInfoF("ini: ignoring  NewGRF '%s': duplicate GRF ID with '%s'", item->name, gc->filename);
 
				duplicate = true;
 
				break;
 
			}
 
		}
 
		if (duplicate) {
 
			ClearGRFConfig(&c);
 
			delete c;
 
			continue;
 
		}
 

	
 
		/* Mark file as static to avoid saving in savegame. */
 
		if (is_static) SetBit(c->flags, GCF_STATIC);
 

	
0 comments (0 inline, 0 general)