(svn r22443) -Codechange: Deduplicate code for deactivating GRFs.

This commit is contained in:
frosch 2011-05-11 20:22:04 +00:00
parent b6c4832b1e
commit 67b9eda8c6

View File

@ -282,6 +282,34 @@ static void ClearTemporaryNewGRFData(GRFFile *gf)
gf->spritegroups_count = 0; gf->spritegroups_count = 0;
} }
/**
* Disable a GRF
* @param message Error message or STR_NULL
* @param config GRFConfig to disable, NULL for current
* @return Error message of the GRF for further customisation
*/
static GRFError *DisableGrf(StringID message = STR_NULL, GRFConfig *config = NULL)
{
GRFFile *file;
if (config != NULL) {
file = GetFileByGRFID(config->ident.grfid);
} else {
config = _cur_grfconfig;
file = _cur_grffile;
}
config->status = GCS_DISABLED;
if (file != NULL) ClearTemporaryNewGRFData(file);
if (config == _cur_grfconfig) _skip_sprites = -1;
if (message != STR_NULL) {
delete config->error;
config->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, message);
}
return config->error;
}
typedef std::map<StringID *, uint32> StringIDToGRFIDMapping; typedef std::map<StringID *, uint32> StringIDToGRFIDMapping;
static StringIDToGRFIDMapping _string_to_grf_mapping; static StringIDToGRFIDMapping _string_to_grf_mapping;
@ -3488,11 +3516,7 @@ static bool HandleChangeInfoResult(const char *caller, ChangeInfoResult cir, uin
case CIR_INVALID_ID: case CIR_INVALID_ID:
/* No debug message for an invalid ID, as it has already been output */ /* No debug message for an invalid ID, as it has already been output */
_skip_sprites = -1; DisableGrf(cir == CIR_INVALID_ID ? STR_NEWGRF_ERROR_INVALID_ID : STR_NEWGRF_ERROR_UNKNOWN_PROPERTY);
_cur_grfconfig->status = GCS_DISABLED;
delete _cur_grfconfig->error;
_cur_grfconfig->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL);
_cur_grfconfig->error->message = (cir == CIR_INVALID_ID) ? STR_NEWGRF_ERROR_INVALID_ID : STR_NEWGRF_ERROR_UNKNOWN_PROPERTY;
return true; return true;
} }
} }
@ -5215,12 +5239,8 @@ static void CfgApply(ByteReader *buf)
*/ */
static void DisableStaticNewGRFInfluencingNonStaticNewGRFs(GRFConfig *c) static void DisableStaticNewGRFInfluencingNonStaticNewGRFs(GRFConfig *c)
{ {
delete c->error; GRFError *error = DisableGrf(STR_NEWGRF_ERROR_STATIC_GRF_CAUSES_DESYNC, c);
c->status = GCS_DISABLED; error->data = strdup(_cur_grfconfig->GetName());
c->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_STATIC_GRF_CAUSES_DESYNC);
c->error->data = strdup(_cur_grfconfig->GetName());
ClearTemporaryNewGRFData(GetFileByGRFID(c->ident.grfid));
} }
/* Action 0x07 /* Action 0x07
@ -5382,8 +5402,7 @@ static void SkipIf(ByteReader *buf)
/* If an action 8 hasn't been encountered yet, disable the grf. */ /* If an action 8 hasn't been encountered yet, disable the grf. */
if (_cur_grfconfig->status != (_cur_stage < GLS_RESERVE ? GCS_INITIALISED : GCS_ACTIVATED)) { if (_cur_grfconfig->status != (_cur_stage < GLS_RESERVE ? GCS_INITIALISED : GCS_ACTIVATED)) {
_cur_grfconfig->status = GCS_DISABLED; DisableGrf();
ClearTemporaryNewGRFData(_cur_grffile);
} }
} }
} }
@ -5432,11 +5451,7 @@ static void GRFInfo(ByteReader *buf)
const char *name = buf->ReadString(); const char *name = buf->ReadString();
if (_cur_stage < GLS_RESERVE && _cur_grfconfig->status != GCS_UNKNOWN) { if (_cur_stage < GLS_RESERVE && _cur_grfconfig->status != GCS_UNKNOWN) {
_cur_grfconfig->status = GCS_DISABLED; DisableGrf(STR_NEWGRF_ERROR_MULTIPLE_ACTION_8);
delete _cur_grfconfig->error;
_cur_grfconfig->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_MULTIPLE_ACTION_8);
_skip_sprites = -1;
return; return;
} }
@ -5561,9 +5576,7 @@ static void GRFLoadError(ByteReader *buf)
} else if (severity == 3) { } else if (severity == 3) {
/* This is a fatal error, so make sure the GRF is deactivated and no /* This is a fatal error, so make sure the GRF is deactivated and no
* more of it gets loaded. */ * more of it gets loaded. */
_cur_grfconfig->status = GCS_DISABLED; DisableGrf();
ClearTemporaryNewGRFData(_cur_grffile);
_skip_sprites = -1;
} }
if (message_id >= lengthof(msgstr) && message_id != 0xFF) { if (message_id >= lengthof(msgstr) && message_id != 0xFF) {
@ -5745,9 +5758,7 @@ static uint32 PerformGRM(uint32 *grm, uint16 num_ids, uint16 count, uint8 op, ui
if (op != 4 && op != 5) { if (op != 4 && op != 5) {
/* Deactivate GRF */ /* Deactivate GRF */
grfmsg(0, "ParamSet: GRM: Unable to allocate %d %s, deactivating", count, type); grfmsg(0, "ParamSet: GRM: Unable to allocate %d %s, deactivating", count, type);
_cur_grfconfig->status = GCS_DISABLED; DisableGrf();
ClearTemporaryNewGRFData(_cur_grffile);
_skip_sprites = -1;
return UINT_MAX; return UINT_MAX;
} }
@ -5822,9 +5833,7 @@ static void ParamSet(ByteReader *buf)
/* Check if the allocated sprites will fit below the original sprite limit */ /* Check if the allocated sprites will fit below the original sprite limit */
if (_cur_spriteid + count >= 16384) { if (_cur_spriteid + count >= 16384) {
grfmsg(0, "ParamSet: GRM: Unable to allocate %d sprites; try changing NewGRF order", count); grfmsg(0, "ParamSet: GRM: Unable to allocate %d sprites; try changing NewGRF order", count);
_cur_grfconfig->status = GCS_DISABLED; DisableGrf();
ClearTemporaryNewGRFData(_cur_grffile);
_skip_sprites = -1;
return; return;
} }
@ -6101,7 +6110,8 @@ static void GRFInhibit(ByteReader *buf)
/* Unset activation flag */ /* Unset activation flag */
if (file != NULL && file != _cur_grfconfig) { if (file != NULL && file != _cur_grfconfig) {
grfmsg(2, "GRFInhibit: Deactivating file '%s'", file->filename); grfmsg(2, "GRFInhibit: Deactivating file '%s'", file->filename);
file->status = GCS_DISABLED; GRFError *error = DisableGrf(STR_NULL, file);
error->data = strdup(_cur_grfconfig->GetName());
} }
} }
} }
@ -6172,9 +6182,7 @@ static void FeatureTownName(ByteReader *buf)
if (townname->nbparts[ref_id] == 0) { if (townname->nbparts[ref_id] == 0) {
grfmsg(0, "FeatureTownName: definition 0x%02X doesn't exist, deactivating", ref_id); grfmsg(0, "FeatureTownName: definition 0x%02X doesn't exist, deactivating", ref_id);
DelGRFTownName(grfid); DelGRFTownName(grfid);
_cur_grfconfig->status = GCS_DISABLED; DisableGrf(STR_NEWGRF_ERROR_INVALID_ID);
ClearTemporaryNewGRFData(_cur_grffile);
_skip_sprites = -1;
return; return;
} }
@ -6449,16 +6457,12 @@ static void TranslateGRFStrings(ByteReader *buf)
if (c->status == GCS_INITIALISED) { if (c->status == GCS_INITIALISED) {
/* If the file is not active but will be activated later, give an error /* If the file is not active but will be activated later, give an error
* and disable this file. */ * and disable this file. */
delete _cur_grfconfig->error; GRFError *error = DisableGrf(STR_NEWGRF_ERROR_LOAD_AFTER);
_cur_grfconfig->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_LOAD_AFTER);
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));
_cur_grfconfig->error->data = strdup(tmp); error->data = strdup(tmp);
_cur_grfconfig->status = GCS_DISABLED;
ClearTemporaryNewGRFData(_cur_grffile);
_skip_sprites = -1;
return; return;
} }
@ -7902,11 +7906,7 @@ static void DecodeSpecialSprite(byte *buf, uint num, GrfLoadingStage stage)
} }
} catch (...) { } catch (...) {
grfmsg(1, "DecodeSpecialSprite: Tried to read past end of pseudo-sprite data"); grfmsg(1, "DecodeSpecialSprite: Tried to read past end of pseudo-sprite data");
DisableGrf(STR_NEWGRF_ERROR_READ_BOUNDS);
_skip_sprites = -1;
_cur_grfconfig->status = GCS_DISABLED;
delete _cur_grfconfig->error;
_cur_grfconfig->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_READ_BOUNDS);
} }
} }
@ -7981,9 +7981,7 @@ void LoadNewGRFFile(GRFConfig *config, uint file_index, GrfLoadingStage stage)
} else { } else {
if (_skip_sprites == 0) { if (_skip_sprites == 0) {
grfmsg(0, "LoadNewGRFFile: Unexpected sprite, disabling"); grfmsg(0, "LoadNewGRFFile: Unexpected sprite, disabling");
config->status = GCS_DISABLED; DisableGrf(STR_NEWGRF_ERROR_UNEXPECTED_SPRITE);
delete config->error;
config->error = new GRFError(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_UNEXPECTED_SPRITE);
break; break;
} }