diff --git a/src/newgrf.cpp b/src/newgrf.cpp index d17999e2ff..31efc7360e 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -447,12 +447,11 @@ static GRFError *DisableGrf(StringID message = STR_NULL, GRFConfig *config = nul if (file != nullptr) ClearTemporaryNewGRFData(file); if (config == _cur.grfconfig) _cur.skip_sprites = -1; - if (message != STR_NULL) { - config->error = std::make_unique(STR_NEWGRF_ERROR_MSG_FATAL, message); - if (config == _cur.grfconfig) config->error->param_value[0] = _cur.nfo_line; - } + if (message == STR_NULL) return nullptr; - return config->error.get(); + config->error = {STR_NEWGRF_ERROR_MSG_FATAL, message}; + if (config == _cur.grfconfig) config->error->param_value[0] = _cur.nfo_line; + return &config->error.value(); } /** @@ -7154,10 +7153,10 @@ static void GRFLoadError(ByteReader *buf) } /* For now we can only show one message per newgrf file. */ - if (_cur.grfconfig->error != nullptr) return; + if (_cur.grfconfig->error.has_value()) return; - _cur.grfconfig->error = std::make_unique(sevstr[severity]); - GRFError *error = _cur.grfconfig->error.get(); + _cur.grfconfig->error = {sevstr[severity]}; + GRFError *error = &_cur.grfconfig->error.value(); if (message_id == 0xFF) { /* This is a custom error message. */ @@ -10006,7 +10005,7 @@ void LoadNewGRF(uint load_index, uint num_baseset) 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 = std::make_unique(STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED); + c->error = {STR_NEWGRF_ERROR_MSG_FATAL, STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED}; continue; } num_non_static++; diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 8f93043dd7..012da78ba8 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -51,6 +51,7 @@ GRFConfig::GRFConfig(const GRFConfig &config) : name(config.name), info(config.info), url(config.url), + error(config.error), version(config.version), min_loadable_version(config.min_loadable_version), flags(config.flags & ~(1 << GCF_COPY)), @@ -63,7 +64,6 @@ GRFConfig::GRFConfig(const GRFConfig &config) : param_info(config.param_info), has_param_defaults(config.has_param_defaults) { - if (config.error != nullptr) this->error = std::make_unique(*config.error); } /** @@ -495,7 +495,7 @@ compatible_grf: c->ident.md5sum = f->ident.md5sum; c->name = f->name; c->info = f->name; - c->error = nullptr; + c->error.reset(); c->version = f->version; c->min_loadable_version = f->min_loadable_version; c->num_valid_params = f->num_valid_params; diff --git a/src/newgrf_config.h b/src/newgrf_config.h index 171f49a604..9706f79fbc 100644 --- a/src/newgrf_config.h +++ b/src/newgrf_config.h @@ -109,11 +109,11 @@ struct GRFIdentifier { struct GRFError { GRFError(StringID severity, StringID message = 0); - 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 - std::array param_value; ///< 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 + std::array param_value{}; ///< Values of GRF parameters to show for message and custom_message }; /** The possible types of a newgrf parameter. */ @@ -157,7 +157,7 @@ struct GRFConfig : ZeroedMemoryAllocator { GRFTextWrapper name; ///< NOSAVE: GRF name (Action 0x08) GRFTextWrapper info; ///< NOSAVE: GRF info (author, copyright, ...) (Action 0x08) GRFTextWrapper url; ///< NOSAVE: URL belonging to this GRF. - std::unique_ptr error; ///< NOSAVE: Error/Warning during GRF loading (Action 0x0B) + std::optional 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 diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 3bcd2bf30e..785adf046f 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -48,7 +48,7 @@ void ShowNewGRFError() for (const GRFConfig *c = _grfconfig; c != nullptr; c = c->next) { /* Only show Fatal and Error level messages */ - if (c->error == nullptr || (c->error->severity != STR_NEWGRF_ERROR_MSG_FATAL && c->error->severity != STR_NEWGRF_ERROR_MSG_ERROR)) continue; + if (!c->error.has_value() || (c->error->severity != STR_NEWGRF_ERROR_MSG_FATAL && c->error->severity != STR_NEWGRF_ERROR_MSG_ERROR)) continue; SetDParamStr(0, c->GetName()); SetDParam (1, c->error->message != STR_NULL ? c->error->message : STR_JUST_RAW_STRING); @@ -70,7 +70,7 @@ void ShowNewGRFError() static void ShowNewGRFInfo(const GRFConfig *c, const Rect &r, bool show_params) { Rect tr = r.Shrink(WidgetDimensions::scaled.frametext); - if (c->error != nullptr) { + if (c->error.has_value()) { SetDParamStr(0, c->error->custom_message); // is skipped by built-in messages SetDParamStr(1, c->filename); SetDParamStr(2, c->error->data); @@ -886,8 +886,8 @@ struct NewGRFWindow : public Window, NewGRFScanCallback { } } DrawSprite(SPR_SQUARE, pal, square_left, tr.top + square_offset_y); - if (c->error != nullptr) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, tr.top + warning_offset_y); - uint txtoffset = c->error == nullptr ? 0 : warning.width; + if (c->error.has_value()) DrawSprite(SPR_WARNING_SIGN, 0, warning_left, tr.top + warning_offset_y); + uint txtoffset = !c->error.has_value() ? 0 : warning.width; DrawString(text_left + (rtl ? 0 : txtoffset), text_right - (rtl ? txtoffset : 0), tr.top + offset_y, text, h ? TC_WHITE : TC_ORANGE); tr.top += step_height; }