Changeset - r24212:4cf87ac30a4f
[Not reviewed]
master
0 4 0
Michael Lutz - 4 years ago 2020-05-17 21:31:54
michi@icosahedron.de
Codechange: Use std::string for storing GRF error messages.
4 files changed with 24 insertions and 33 deletions:
0 comments (0 inline, 0 general)
src/newgrf.cpp
Show inline comments
 
@@ -5248,7 +5248,7 @@ static void NewSpriteGroup(ByteReader *b
 
						group->num_input = buf->ReadByte();
 
						if (group->num_input > lengthof(group->subtract_input)) {
 
							GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK);
 
							error->data = stredup("too many inputs (max 16)");
 
							error->data = "too many inputs (max 16)";
 
							return;
 
						}
 
						for (uint i = 0; i < group->num_input; i++) {
 
@@ -5261,7 +5261,7 @@ static void NewSpriteGroup(ByteReader *b
 
								group->version = 0xFF;
 
							} else if (std::find(group->cargo_input, group->cargo_input + i, cargo) != group->cargo_input + i) {
 
								GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK);
 
								error->data = stredup("duplicate input cargo");
 
								error->data = "duplicate input cargo";
 
								return;
 
							}
 
							group->cargo_input[i] = cargo;
 
@@ -5270,7 +5270,7 @@ static void NewSpriteGroup(ByteReader *b
 
						group->num_output = buf->ReadByte();
 
						if (group->num_output > lengthof(group->add_output)) {
 
							GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK);
 
							error->data = stredup("too many outputs (max 16)");
 
							error->data = "too many outputs (max 16)";
 
							return;
 
						}
 
						for (uint i = 0; i < group->num_output; i++) {
 
@@ -5281,7 +5281,7 @@ static void NewSpriteGroup(ByteReader *b
 
								group->version = 0xFF;
 
							} else if (std::find(group->cargo_output, group->cargo_output + i, cargo) != group->cargo_output + i) {
 
								GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK);
 
								error->data = stredup("duplicate output cargo");
 
								error->data = "duplicate output cargo";
 
								return;
 
							}
 
							group->cargo_output[i] = cargo;
 
@@ -6517,7 +6517,7 @@ static void CfgApply(ByteReader *buf)
 
static void DisableStaticNewGRFInfluencingNonStaticNewGRFs(GRFConfig *c)
 
{
 
	GRFError *error = DisableGrf(STR_NEWGRF_ERROR_STATIC_GRF_CAUSES_DESYNC, c);
 
	error->data = stredup(_cur.grfconfig->GetName());
 
	error->data = _cur.grfconfig->GetName();
 
}
 

	
 
/* Action 0x07
 
@@ -6895,10 +6895,10 @@ static void GRFLoadError(ByteReader *buf
 
		if (buf->HasData()) {
 
			const char *message = buf->ReadString();
 

	
 
			error->custom_message = stredup(TranslateTTDPatchCodes(_cur.grffile->grfid, lang, true, message, SCC_RAW_STRING_POINTER).c_str());
 
			error->custom_message = TranslateTTDPatchCodes(_cur.grffile->grfid, lang, true, message, SCC_RAW_STRING_POINTER);
 
		} else {
 
			grfmsg(7, "GRFLoadError: No custom message supplied.");
 
			error->custom_message = stredup("");
 
			error->custom_message.clear();
 
		}
 
	} else {
 
		error->message = msgstr[message_id];
 
@@ -6907,10 +6907,10 @@ static void GRFLoadError(ByteReader *buf
 
	if (buf->HasData()) {
 
		const char *data = buf->ReadString();
 

	
 
		error->data = stredup(TranslateTTDPatchCodes(_cur.grffile->grfid, lang, true, data).c_str());
 
		error->data = TranslateTTDPatchCodes(_cur.grffile->grfid, lang, true, data);
 
	} else {
 
		grfmsg(7, "GRFLoadError: No message data supplied.");
 
		error->data = stredup("");
 
		error->data.clear();
 
	}
 

	
 
	/* Only two parameter numbers can be used in the string. */
 
@@ -7771,7 +7771,7 @@ static void TranslateGRFStrings(ByteRead
 

	
 
		char tmp[256];
 
		GetString(tmp, STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE, lastof(tmp));
 
		error->data = stredup(tmp);
 
		error->data = tmp;
 

	
 
		return;
 
	}
src/newgrf_config.cpp
Show inline comments
 
@@ -179,7 +179,8 @@ uint _missing_extra_graphics = 0;
 
 */
 
GRFError::GRFError(StringID severity, StringID message) :
 
	message(message),
 
	severity(severity)
 
	severity(severity),
 
	param_value()
 
{
 
}
 

	
 
@@ -188,23 +189,14 @@ GRFError::GRFError(StringID severity, St
 
 * @param error The GRFError object to make a copy of.
 
 */
 
GRFError::GRFError(const GRFError &error) :
 
	ZeroedMemoryAllocator(),
 
	custom_message(error.custom_message),
 
	data(error.data),
 
	message(error.message),
 
	severity(error.severity)
 
{
 
	if (error.custom_message != nullptr) this->custom_message = stredup(error.custom_message);
 
	if (error.data           != nullptr) this->data           = stredup(error.data);
 
	memcpy(this->param_value, error.param_value, sizeof(this->param_value));
 
}
 

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

	
 
/**
 
 * Create a new empty GRFParameterInfo object.
 
 * @param nr The newgrf parameter that is changed.
src/newgrf_config.h
Show inline comments
 
@@ -109,16 +109,15 @@ struct GRFIdentifier {
 
};
 

	
 
/** Information about why GRF had problems during initialisation */
 
struct GRFError : ZeroedMemoryAllocator {
 
struct GRFError {
 
	GRFError(StringID severity, StringID message = 0);
 
	GRFError(const GRFError &error);
 
	~GRFError();
 

	
 
	char *custom_message;  ///< Custom message (if present)
 
	char *data;            ///< Additional data for message and custom_message
 
	StringID message;      ///< Default message
 
	StringID severity;     ///< Info / Warning / Error / Fatal
 
	uint32 param_value[2]; ///< Values of GRF parameters to show for message and custom_message
 
	std::string custom_message; ///< Custom message (if present)
 
	std::string data;           ///< Additional data for message and custom_message
 
	StringID message;           ///< Default message
 
	StringID severity;          ///< Info / Warning / Error / Fatal
 
	uint32 param_value[2];      ///< Values of GRF parameters to show for message and custom_message
 
};
 

	
 
/** The possible types of a newgrf parameter. */
src/newgrf_gui.cpp
Show inline comments
 
@@ -50,10 +50,10 @@ void ShowNewGRFError()
 
		/* We only want to show fatal errors */
 
		if (c->error == nullptr || c->error->severity != STR_NEWGRF_ERROR_MSG_FATAL) continue;
 

	
 
		SetDParam   (0, c->error->custom_message == nullptr ? c->error->message : STR_JUST_RAW_STRING);
 
		SetDParamStr(1, c->error->custom_message);
 
		SetDParam   (0, c->error->message != STR_NULL ? c->error->message : STR_JUST_RAW_STRING);
 
		SetDParamStr(1, c->error->custom_message.c_str());
 
		SetDParamStr(2, c->filename);
 
		SetDParamStr(3, c->error->data);
 
		SetDParamStr(3, c->error->data.c_str());
 
		for (uint i = 0; i < lengthof(c->error->param_value); i++) {
 
			SetDParam(4 + i, c->error->param_value[i]);
 
		}
 
@@ -66,13 +66,13 @@ static void ShowNewGRFInfo(const GRFConf
 
{
 
	if (c->error != nullptr) {
 
		char message[512];
 
		SetDParamStr(0, c->error->custom_message); // is skipped by built-in messages
 
		SetDParamStr(0, c->error->custom_message.c_str()); // is skipped by built-in messages
 
		SetDParamStr(1, c->filename);
 
		SetDParamStr(2, c->error->data);
 
		SetDParamStr(2, c->error->data.c_str());
 
		for (uint i = 0; i < lengthof(c->error->param_value); i++) {
 
			SetDParam(3 + i, c->error->param_value[i]);
 
		}
 
		GetString(message, c->error->custom_message == nullptr ? c->error->message : STR_JUST_RAW_STRING, lastof(message));
 
		GetString(message, c->error->message != STR_NULL ? c->error->message : STR_JUST_RAW_STRING, lastof(message));
 

	
 
		SetDParamStr(0, message);
 
		y = DrawStringMultiLine(x, right, y, bottom, c->error->severity);
0 comments (0 inline, 0 general)