From 82c87208146e11f26c2dbc1aa647bb38c7da7458 Mon Sep 17 00:00:00 2001 From: frosch Date: Sun, 1 Oct 2023 15:21:57 +0200 Subject: [PATCH] Fix #8846: When upgrading NewGRF presets, copy NewGRF parameters only if the NewGRF are compatible. Otherwise reset to defaults. --- src/newgrf_config.cpp | 10 +++++++++- src/newgrf_config.h | 1 + src/newgrf_gui.cpp | 6 +++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/newgrf_config.cpp b/src/newgrf_config.cpp index 8985f4c181..c45498ee2b 100644 --- a/src/newgrf_config.cpp +++ b/src/newgrf_config.cpp @@ -66,6 +66,14 @@ GRFConfig::GRFConfig(const GRFConfig &config) : { } +/** + * Return whether this NewGRF can replace an older version of the same NewGRF. + */ +bool GRFConfig::IsCompatible(uint32_t old_version) const +{ + return this->min_loadable_version <= old_version && old_version <= this->version; +} + /** * Copy the parameter information from the \a src config. * @param src Source config. @@ -685,7 +693,7 @@ const GRFConfig *FindGRFConfig(uint32_t grfid, FindGRFConfigMode mode, const MD5 /* Skip incompatible stuff, unless explicitly allowed */ if (mode != FGCM_NEWEST && HasBit(c->flags, GCF_INVALID)) continue; /* check version compatibility */ - if (mode == FGCM_COMPATIBLE && (c->version < desired_version || c->min_loadable_version > desired_version)) continue; + if (mode == FGCM_COMPATIBLE && !c->IsCompatible(desired_version)) continue; /* remember the newest one as "the best" */ if (best == nullptr || c->version > best->version) best = c; } diff --git a/src/newgrf_config.h b/src/newgrf_config.h index 22706ce025..3620501b19 100644 --- a/src/newgrf_config.h +++ b/src/newgrf_config.h @@ -173,6 +173,7 @@ struct GRFConfig : ZeroedMemoryAllocator { struct GRFConfig *next; ///< NOSAVE: Next item in the linked list + bool IsCompatible(uint32_t old_version) const; void CopyParams(const GRFConfig &src); std::optional GetTextfile(TextfileType type) const; diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 9aab5e9b6f..f1ec5e3f7f 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -723,7 +723,11 @@ struct NewGRFWindow : public Window, NewGRFScanCallback { while (*c != iter->second) c = &(*c)->next; GRFConfig *d = new GRFConfig(*a); d->next = (*c)->next; - d->CopyParams(**c); + if (d->IsCompatible((*c)->version)) { + d->CopyParams(**c); + } else { + d->SetParameterDefaults(); + } if (this->active_sel == *c) { CloseWindowByClass(WC_GRF_PARAMETERS); CloseWindowByClass(WC_TEXTFILE);