Changeset - r27237:9cb09b9cf61b
[Not reviewed]
master
0 3 0
Rubidium - 14 months ago 2023-04-27 18:28:09
rubidium@openttd.org
Codechange: use std::unique_ptr to manager GRFErrors in GRFConfig
3 files changed with 9 insertions and 16 deletions:
0 comments (0 inline, 0 general)
src/newgrf.cpp
Show inline comments
 
@@ -446,18 +446,17 @@ static GRFError *DisableGrf(StringID mes
 

	
 
	config->status = GCS_DISABLED;
 
	if (file != nullptr) ClearTemporaryNewGRFData(file);
 
	if (config == _cur.grfconfig) _cur.skip_sprites = -1;
 

	
 
	if (message != STR_NULL) {
 
		delete config->error;
 
		config->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, message);
 
		config->error = std::make_unique<GRFError>(STR_NEWGRF_ERROR_MSG_FATAL, message);
 
		if (config == _cur.grfconfig) config->error->param_value[0] = _cur.nfo_line;
 
	}
 

	
 
	return config->error;
 
	return config->error.get();
 
}
 

	
 
/**
 
 * Information for mapping static StringIDs.
 
 */
 
struct StringIDMapping {
 
@@ -7131,14 +7130,13 @@ static void GRFLoadError(ByteReader *buf
 
	} else if (severity == 3) {
 
		/* This is a fatal error, so make sure the GRF is deactivated and no
 
		 * more of it gets loaded. */
 
		DisableGrf();
 

	
 
		/* Make sure we show fatal errors, instead of silly infos from before */
 
		delete _cur.grfconfig->error;
 
		_cur.grfconfig->error = nullptr;
 
		_cur.grfconfig->error.reset();
 
	}
 

	
 
	if (message_id >= lengthof(msgstr) && message_id != 0xFF) {
 
		GrfMsg(7, "GRFLoadError: Invalid message id.");
 
		return;
 
	}
 
@@ -7148,13 +7146,14 @@ static void GRFLoadError(ByteReader *buf
 
		return;
 
	}
 

	
 
	/* For now we can only show one message per newgrf file. */
 
	if (_cur.grfconfig->error != nullptr) return;
 

	
 
	GRFError *error = new GRFError(sevstr[severity]);
 
	_cur.grfconfig->error = std::make_unique<GRFError>(sevstr[severity]);
 
	GRFError *error = _cur.grfconfig->error.get();
 

	
 
	if (message_id == 0xFF) {
 
		/* This is a custom error message. */
 
		if (buf->HasData()) {
 
			const char *message = buf->ReadString();
 

	
 
@@ -7178,14 +7177,12 @@ static void GRFLoadError(ByteReader *buf
 

	
 
	/* Only two parameter numbers can be used in the string. */
 
	for (uint i = 0; i < lengthof(error->param_value) && buf->HasData(); i++) {
 
		uint param_number = buf->ReadByte();
 
		error->param_value[i] = _cur.grffile->GetParam(param_number);
 
	}
 

	
 
	_cur.grfconfig->error = error;
 
}
 

	
 
/* Action 0x0C */
 
static void GRFComment(ByteReader *buf)
 
{
 
	/* <0C> [<ignored...>]
 
@@ -8720,16 +8717,13 @@ static void ResetNewGRF()
 
}
 

	
 
/** Clear all NewGRF errors */
 
static void ResetNewGRFErrors()
 
{
 
	for (GRFConfig *c = _grfconfig; c != nullptr; c = c->next) {
 
		if (!HasBit(c->flags, GCF_COPY) && c->error != nullptr) {
 
			delete c->error;
 
			c->error = nullptr;
 
		}
 
		c->error.reset();
 
	}
 
}
 

	
 
/**
 
 * Reset all NewGRF loaded data
 
 */
 
@@ -10008,13 +10002,13 @@ void LoadNewGRF(uint load_index, uint nu
 
			if (stage == GLS_LABELSCAN) InitNewGRFFile(c);
 

	
 
			if (!HasBit(c->flags, GCF_STATIC) && !HasBit(c->flags, GCF_SYSTEM)) {
 
				if (num_non_static == NETWORK_MAX_GRF_COUNT) {
 
					Debug(grf, 0, "'{}' is not loaded as the maximum number of non-static GRFs has been reached", c->filename);
 
					c->status = GCS_DISABLED;
 
					c->error  = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED);
 
					c->error  = std::make_unique<GRFError>(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED);
 
					continue;
 
				}
 
				num_non_static++;
 
			}
 

	
 
			num_grfs++;
src/newgrf_config.cpp
Show inline comments
 
@@ -61,13 +61,13 @@ 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 != nullptr) this->filename = stredup(config.filename);
 
	if (config.error != nullptr) this->error = new GRFError(*config.error);
 
	if (config.error != nullptr) this->error = std::make_unique<GRFError>(*config.error);
 
	for (uint i = 0; i < config.param_info.size(); i++) {
 
		if (config.param_info[i] == nullptr) {
 
			this->param_info.push_back(nullptr);
 
		} else {
 
			this->param_info.push_back(new GRFParameterInfo(*config.param_info[i]));
 
		}
 
@@ -77,13 +77,12 @@ GRFConfig::GRFConfig(const GRFConfig &co
 
/** Cleanup a GRFConfig object. */
 
GRFConfig::~GRFConfig()
 
{
 
	/* GCF_COPY as in NOT stredupped/alloced the filename */
 
	if (!HasBit(this->flags, GCF_COPY)) {
 
		free(this->filename);
 
		delete this->error;
 
	}
 

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

	
 
/**
src/newgrf_config.h
Show inline comments
 
@@ -163,13 +163,13 @@ struct GRFConfig : ZeroedMemoryAllocator
 
	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
 
	GRFTextWrapper name;                        ///< NOSAVE: GRF name (Action 0x08)
 
	GRFTextWrapper info;                        ///< NOSAVE: GRF info (author, copyright, ...) (Action 0x08)
 
	GRFTextWrapper url;                         ///< NOSAVE: URL belonging to this GRF.
 
	GRFError *error;                            ///< NOSAVE: Error/Warning during GRF loading (Action 0x0B)
 
	std::unique_ptr<GRFError> error;            ///< NOSAVE: Error/Warning during GRF loading (Action 0x0B)
 

	
 
	uint32 version;                             ///< NOSAVE: Version a NewGRF can set so only the newest NewGRF is shown
 
	uint32 min_loadable_version;                ///< NOSAVE: Minimum compatible version a NewGRF can define
 
	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
0 comments (0 inline, 0 general)