File diff r17402:05a8887a1cde → r17403:bbaee031fd60
src/newgrf_config.cpp
Show inline comments
 
@@ -18,30 +18,48 @@
 
#include "newgrf_text.h"
 
#include "window_func.h"
 

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

	
 
/** Create a new GRFTextWrapper. */
 
GRFTextWrapper::GRFTextWrapper() :
 
	text(NULL)
 
{
 
}
 

	
 
/** Cleanup a GRFTextWrapper object. */
 
GRFTextWrapper::~GRFTextWrapper()
 
{
 
	CleanUpGRFText(this->text);
 
}
 

	
 
/**
 
 * Create a new GRFConfig.
 
 * @param filename Set the filename of this GRFConfig to filename. The argument
 
 *   is copied so the original string isn't needed after the constructor.
 
 */
 
GRFConfig::GRFConfig(const char *filename) :
 
	name(new GRFTextWrapper()),
 
	info(new GRFTextWrapper()),
 
	num_valid_params(lengthof(param))
 
{
 
	if (filename != NULL) this->filename = strdup(filename);
 
	this->name->AddRef();
 
	this->info->AddRef();
 
}
 

	
 
/**
 
 * Create a new GRFConfig that is a deep copy of an existing config.
 
 * @param config The GRFConfig object to make a copy of.
 
 */
 
GRFConfig::GRFConfig(const GRFConfig &config) :
 
	ZeroedMemoryAllocator(),
 
	ident(config.ident),
 
	name(config.name),
 
	info(config.info),
 
	version(config.version),
 
	min_loadable_version(config.min_loadable_version),
 
	flags(config.flags & ~(1 << GCF_COPY)),
 
	status(config.status),
 
	grf_bugs(config.grf_bugs),
 
	num_params(config.num_params),
 
@@ -49,14 +67,14 @@ GRFConfig::GRFConfig(const GRFConfig &co
 
	palette(config.palette),
 
	has_param_defaults(config.has_param_defaults)
 
{
 
	MemCpyT<uint8>(this->original_md5sum, config.original_md5sum, lengthof(this->original_md5sum));
 
	MemCpyT<uint32>(this->param, config.param, lengthof(this->param));
 
	if (config.filename != NULL) this->filename = strdup(config.filename);
 
	this->name = DuplicateGRFText(config.name);
 
	this->info = DuplicateGRFText(config.info);
 
	this->name->AddRef();
 
	this->info->AddRef();
 
	if (config.error    != NULL) this->error    = new GRFError(*config.error);
 
	for (uint i = 0; i < config.param_info.Length(); i++) {
 
		if (config.param_info[i] == NULL) {
 
			*this->param_info.Append() = NULL;
 
		} else {
 
			*this->param_info.Append() = new GRFParameterInfo(*config.param_info[i]);
 
@@ -64,41 +82,41 @@ GRFConfig::GRFConfig(const GRFConfig &co
 
	}
 
}
 

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

	
 
	for (uint i = 0; i < this->param_info.Length(); i++) delete this->param_info[i];
 
}
 

	
 
/**
 
 * 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);
 
	const char *name = GetGRFStringFromGRFText(this->name->text);
 
	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);
 
	return GetGRFStringFromGRFText(this->info->text);
 
}
 

	
 
/** Set the default value for all parameters as specified by action14. */
 
void GRFConfig::SetParameterDefaults()
 
{
 
	this->num_params = 0;
 
@@ -479,14 +497,18 @@ compatible_grf:
 
			 * When the GCF_COPY flag is set, it is certain that the filename is
 
			 * already a local one, so there is no need to replace it. */
 
			if (!HasBit(c->flags, GCF_COPY)) {
 
				free(c->filename);
 
				c->filename = strdup(f->filename);
 
				memcpy(c->ident.md5sum, f->ident.md5sum, sizeof(c->ident.md5sum));
 
				if (c->name == NULL) c->name = DuplicateGRFText(f->name);
 
				if (c->info == NULL) c->info = DuplicateGRFText(f->info);
 
				c->name->Release();
 
				c->name = f->name;
 
				c->name->AddRef();
 
				c->info->Release();
 
				c->info = f->name;
 
				c->info->AddRef();
 
				c->error = NULL;
 
				c->version = f->version;
 
				c->min_loadable_version = f->min_loadable_version;
 
				c->num_valid_params = f->num_valid_params;
 
				c->has_param_defaults = f->has_param_defaults;
 
				for (uint i = 0; i < f->param_info.Length(); i++) {
 
@@ -643,13 +665,13 @@ const GRFConfig *FindGRFConfig(uint32 gr
 

	
 
#ifdef ENABLE_NETWORK
 

	
 
/** Structure for UnknownGRFs; this is a lightweight variant of GRFConfig */
 
struct UnknownGRF : public GRFIdentifier {
 
	UnknownGRF *next;
 
	char   name[NETWORK_GRF_NAME_LENGTH];
 
	GRFTextWrapper *name;
 
};
 

	
 
/**
 
 * Finds the name of a NewGRF in the list of names for unknown GRFs. An
 
 * unknown GRF is a GRF where the .grf is not found during scanning.
 
 *
 
@@ -659,17 +681,17 @@ struct UnknownGRF : public GRFIdentifier
 
 * up the GRF anyway and that works better with the GRF ID.
 
 *
 
 * @param grfid  the GRF ID part of the 'unique' GRF identifier
 
 * @param md5sum the MD5 checksum part of the 'unique' GRF identifier
 
 * @param create whether to create a new GRFConfig if the GRFConfig did not
 
 *               exist in the fake list of GRFConfigs.
 
 * @return the GRFConfig with the given GRF ID and MD5 checksum or NULL when
 
 *         it does not exist and create is false. This value must NEVER be
 
 *         freed by the caller.
 
 * @return The GRFTextWrapper of the name of the GRFConfig with the given GRF ID
 
 *         and MD5 checksum or NULL when it does not exist and create is false.
 
 *         This value must NEVER be freed by the caller.
 
 */
 
char *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
 
GRFTextWrapper *FindUnknownGRFName(uint32 grfid, uint8 *md5sum, bool create)
 
{
 
	UnknownGRF *grf;
 
	static UnknownGRF *unknown_grfs = NULL;
 

	
 
	for (grf = unknown_grfs; grf != NULL; grf = grf->next) {
 
		if (grf->grfid == grfid) {
 
@@ -679,13 +701,16 @@ char *FindUnknownGRFName(uint32 grfid, u
 

	
 
	if (!create) return NULL;
 

	
 
	grf = CallocT<UnknownGRF>(1);
 
	grf->grfid = grfid;
 
	grf->next  = unknown_grfs;
 
	strecpy(grf->name, UNKNOWN_GRF_NAME_PLACEHOLDER, lastof(grf->name));
 
	grf->name = new GRFTextWrapper();
 
	grf->name->AddRef();
 

	
 
	AddGRFTextToList(&grf->name->text, UNKNOWN_GRF_NAME_PLACEHOLDER);
 
	memcpy(grf->md5sum, md5sum, sizeof(grf->md5sum));
 

	
 
	unknown_grfs = grf;
 
	return grf->name;
 
}