Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #8846: When upgrading NewGRF presets, copy NewGRF parameters only if the NewGRF are compatible. #11348

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/newgrf_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,21 @@ 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.
*/
void GRFConfig::CopyParams(const GRFConfig &src)
{
this->num_params = src.num_params;
this->num_valid_params = src.num_valid_params;
this->param = src.param;
}

Expand Down Expand Up @@ -686,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;
}
Expand Down
1 change: 1 addition & 0 deletions src/newgrf_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> GetTextfile(TextfileType type) const;
Expand Down
6 changes: 5 additions & 1 deletion src/newgrf_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down