1
0
Fork 0

Codechange: Use std::string for storing GRF error messages.

pull/8170/head
Michael Lutz 2020-05-17 23:31:54 +02:00
parent c082f570ce
commit 9c2e47d03c
4 changed files with 24 additions and 33 deletions

View File

@ -5248,7 +5248,7 @@ static void NewSpriteGroup(ByteReader *buf)
group->num_input = buf->ReadByte(); group->num_input = buf->ReadByte();
if (group->num_input > lengthof(group->subtract_input)) { if (group->num_input > lengthof(group->subtract_input)) {
GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK); GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK);
error->data = stredup("too many inputs (max 16)"); error->data = "too many inputs (max 16)";
return; return;
} }
for (uint i = 0; i < group->num_input; i++) { for (uint i = 0; i < group->num_input; i++) {
@ -5261,7 +5261,7 @@ static void NewSpriteGroup(ByteReader *buf)
group->version = 0xFF; group->version = 0xFF;
} else if (std::find(group->cargo_input, group->cargo_input + i, cargo) != group->cargo_input + i) { } else if (std::find(group->cargo_input, group->cargo_input + i, cargo) != group->cargo_input + i) {
GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK); GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK);
error->data = stredup("duplicate input cargo"); error->data = "duplicate input cargo";
return; return;
} }
group->cargo_input[i] = cargo; group->cargo_input[i] = cargo;
@ -5270,7 +5270,7 @@ static void NewSpriteGroup(ByteReader *buf)
group->num_output = buf->ReadByte(); group->num_output = buf->ReadByte();
if (group->num_output > lengthof(group->add_output)) { if (group->num_output > lengthof(group->add_output)) {
GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK); GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK);
error->data = stredup("too many outputs (max 16)"); error->data = "too many outputs (max 16)";
return; return;
} }
for (uint i = 0; i < group->num_output; i++) { for (uint i = 0; i < group->num_output; i++) {
@ -5281,7 +5281,7 @@ static void NewSpriteGroup(ByteReader *buf)
group->version = 0xFF; group->version = 0xFF;
} else if (std::find(group->cargo_output, group->cargo_output + i, cargo) != group->cargo_output + i) { } else if (std::find(group->cargo_output, group->cargo_output + i, cargo) != group->cargo_output + i) {
GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK); GRFError *error = DisableGrf(STR_NEWGRF_ERROR_INDPROD_CALLBACK);
error->data = stredup("duplicate output cargo"); error->data = "duplicate output cargo";
return; return;
} }
group->cargo_output[i] = cargo; group->cargo_output[i] = cargo;
@ -6517,7 +6517,7 @@ static void CfgApply(ByteReader *buf)
static void DisableStaticNewGRFInfluencingNonStaticNewGRFs(GRFConfig *c) static void DisableStaticNewGRFInfluencingNonStaticNewGRFs(GRFConfig *c)
{ {
GRFError *error = DisableGrf(STR_NEWGRF_ERROR_STATIC_GRF_CAUSES_DESYNC, 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 /* Action 0x07
@ -6895,10 +6895,10 @@ static void GRFLoadError(ByteReader *buf)
if (buf->HasData()) { if (buf->HasData()) {
const char *message = buf->ReadString(); 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 { } else {
grfmsg(7, "GRFLoadError: No custom message supplied."); grfmsg(7, "GRFLoadError: No custom message supplied.");
error->custom_message = stredup(""); error->custom_message.clear();
} }
} else { } else {
error->message = msgstr[message_id]; error->message = msgstr[message_id];
@ -6907,10 +6907,10 @@ static void GRFLoadError(ByteReader *buf)
if (buf->HasData()) { if (buf->HasData()) {
const char *data = buf->ReadString(); 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 { } else {
grfmsg(7, "GRFLoadError: No message data supplied."); grfmsg(7, "GRFLoadError: No message data supplied.");
error->data = stredup(""); error->data.clear();
} }
/* Only two parameter numbers can be used in the string. */ /* Only two parameter numbers can be used in the string. */
@ -7771,7 +7771,7 @@ static void TranslateGRFStrings(ByteReader *buf)
char tmp[256]; char tmp[256];
GetString(tmp, STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE, lastof(tmp)); GetString(tmp, STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE, lastof(tmp));
error->data = stredup(tmp); error->data = tmp;
return; return;
} }

View File

@ -179,7 +179,8 @@ uint _missing_extra_graphics = 0;
*/ */
GRFError::GRFError(StringID severity, StringID message) : GRFError::GRFError(StringID severity, StringID message) :
message(message), message(message),
severity(severity) severity(severity),
param_value()
{ {
} }
@ -188,23 +189,14 @@ GRFError::GRFError(StringID severity, StringID message) :
* @param error The GRFError object to make a copy of. * @param error The GRFError object to make a copy of.
*/ */
GRFError::GRFError(const GRFError &error) : GRFError::GRFError(const GRFError &error) :
ZeroedMemoryAllocator(),
custom_message(error.custom_message), custom_message(error.custom_message),
data(error.data), data(error.data),
message(error.message), message(error.message),
severity(error.severity) 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)); 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. * Create a new empty GRFParameterInfo object.
* @param nr The newgrf parameter that is changed. * @param nr The newgrf parameter that is changed.

View File

@ -109,16 +109,15 @@ struct GRFIdentifier {
}; };
/** Information about why GRF had problems during initialisation */ /** Information about why GRF had problems during initialisation */
struct GRFError : ZeroedMemoryAllocator { struct GRFError {
GRFError(StringID severity, StringID message = 0); GRFError(StringID severity, StringID message = 0);
GRFError(const GRFError &error); GRFError(const GRFError &error);
~GRFError();
char *custom_message; ///< Custom message (if present) std::string custom_message; ///< Custom message (if present)
char *data; ///< Additional data for message and custom_message std::string data; ///< Additional data for message and custom_message
StringID message; ///< Default message StringID message; ///< Default message
StringID severity; ///< Info / Warning / Error / Fatal StringID severity; ///< Info / Warning / Error / Fatal
uint32 param_value[2]; ///< Values of GRF parameters to show for message and custom_message uint32 param_value[2]; ///< Values of GRF parameters to show for message and custom_message
}; };
/** The possible types of a newgrf parameter. */ /** The possible types of a newgrf parameter. */

View File

@ -50,10 +50,10 @@ void ShowNewGRFError()
/* We only want to show fatal errors */ /* We only want to show fatal errors */
if (c->error == nullptr || c->error->severity != STR_NEWGRF_ERROR_MSG_FATAL) continue; 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); SetDParam (0, c->error->message != STR_NULL ? c->error->message : STR_JUST_RAW_STRING);
SetDParamStr(1, c->error->custom_message); SetDParamStr(1, c->error->custom_message.c_str());
SetDParamStr(2, c->filename); 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++) { for (uint i = 0; i < lengthof(c->error->param_value); i++) {
SetDParam(4 + i, c->error->param_value[i]); SetDParam(4 + i, c->error->param_value[i]);
} }
@ -66,13 +66,13 @@ static void ShowNewGRFInfo(const GRFConfig *c, uint x, uint y, uint right, uint
{ {
if (c->error != nullptr) { if (c->error != nullptr) {
char message[512]; 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(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++) { for (uint i = 0; i < lengthof(c->error->param_value); i++) {
SetDParam(3 + i, 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); SetDParamStr(0, message);
y = DrawStringMultiLine(x, right, y, bottom, c->error->severity); y = DrawStringMultiLine(x, right, y, bottom, c->error->severity);