Skip to content

Commit

Permalink
Fix OpenTTD#6468: Load correct version of AI as specified in the save.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuXarick committed Feb 6, 2020
1 parent 896ea6a commit 6afd89b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
25 changes: 20 additions & 5 deletions src/saveload/ai_sl.cpp
Expand Up @@ -81,21 +81,36 @@ static void Load_AIPL()
/* A random AI. */
config->Change(nullptr, -1, false, true);
} else {
config->Change(_ai_saveload_name, _ai_saveload_version, false, _ai_saveload_is_random);
char script_name[1024];
strecpy(script_name, _ai_saveload_name, lastof(script_name));
strtolower(script_name);
int versionParam = _ai_saveload_version;
bool force_match_version = false;
char *e = strrchr(script_name, '.');
if (e != nullptr) {
*e = '\0';
e++;
versionParam = atoi(e);
if (versionParam == _ai_saveload_version) {
versionParam = -1;
force_match_version = true;
}
}
config->Change(_ai_saveload_name, versionParam, force_match_version, _ai_saveload_is_random);
if (!config->HasScript()) {
/* No version of the AI available that can load the data. Try to load the
* latest version of the AI instead. */
config->Change(_ai_saveload_name, -1, false, _ai_saveload_is_random);
config->Change(script_name, -1, false, _ai_saveload_is_random);
if (!config->HasScript()) {
if (strcmp(_ai_saveload_name, "%_dummy") != 0) {
DEBUG(script, 0, "The savegame has an AI by the name '%s', version %d which is no longer available.", _ai_saveload_name, _ai_saveload_version);
if (strcmp(script_name, "%_dummy") != 0) {
DEBUG(script, 0, "The savegame has an AI by the name '%s', version %d which is no longer available.", script_name, _ai_saveload_version);
DEBUG(script, 0, "A random other AI will be loaded in its place.");
} else {
DEBUG(script, 0, "The savegame had no AIs available at the time of saving.");
DEBUG(script, 0, "A random available AI will be loaded now.");
}
} else {
DEBUG(script, 0, "The savegame has an AI by the name '%s', version %d which is no longer available.", _ai_saveload_name, _ai_saveload_version);
DEBUG(script, 0, "The savegame has an AI by the name '%s', version %d which is no longer available.", script_name, _ai_saveload_version);
DEBUG(script, 0, "The latest version of that AI has been loaded instead, but it'll not get the savegame data as it's incompatible.");
}
/* Make sure the AI doesn't get the saveload data, as he was not the
Expand Down
25 changes: 20 additions & 5 deletions src/saveload/game_sl.cpp
Expand Up @@ -73,21 +73,36 @@ static void Load_GSDT()
GameConfig *config = GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME);
if (StrEmpty(_game_saveload_name)) {
} else {
config->Change(_game_saveload_name, _game_saveload_version, false, _game_saveload_is_random);
char script_name[1024];
strecpy(script_name, _game_saveload_name, lastof(script_name));
strtolower(script_name);
int versionParam = _game_saveload_version;
bool force_match_version = false;
char *e = strrchr(script_name, '.');
if (e != nullptr) {
*e = '\0';
e++;
versionParam = atoi(e);
if (versionParam == _game_saveload_version) {
versionParam = -1;
force_match_version = true;
}
}
config->Change(_game_saveload_name, versionParam, force_match_version, _game_saveload_is_random);
if (!config->HasScript()) {
/* No version of the GameScript available that can load the data. Try to load the
* latest version of the GameScript instead. */
config->Change(_game_saveload_name, -1, false, _game_saveload_is_random);
config->Change(script_name, -1, false, _game_saveload_is_random);
if (!config->HasScript()) {
if (strcmp(_game_saveload_name, "%_dummy") != 0) {
DEBUG(script, 0, "The savegame has an GameScript by the name '%s', version %d which is no longer available.", _game_saveload_name, _game_saveload_version);
if (strcmp(script_name, "%_dummy") != 0) {
DEBUG(script, 0, "The savegame has a GameScript by the name '%s', version %d which is no longer available.", script_name, _game_saveload_version);
DEBUG(script, 0, "This game will continue to run without GameScript.");
} else {
DEBUG(script, 0, "The savegame had no GameScript available at the time of saving.");
DEBUG(script, 0, "This game will continue to run without GameScript.");
}
} else {
DEBUG(script, 0, "The savegame has an GameScript by the name '%s', version %d which is no longer available.", _game_saveload_name, _game_saveload_version);
DEBUG(script, 0, "The savegame has a GameScript by the name '%s', version %d which is no longer available.", script_name, _game_saveload_version);
DEBUG(script, 0, "The latest version of that GameScript has been loaded instead, but it'll not get the savegame data as it's incompatible.");
}
/* Make sure the GameScript doesn't get the saveload data, as he was not the
Expand Down
12 changes: 12 additions & 0 deletions src/script/script_config.cpp
Expand Up @@ -22,6 +22,18 @@ void ScriptConfig::Change(const char *name, int version, bool force_exact_match,
this->name = (name == nullptr) ? nullptr : stredup(name);
this->info = (name == nullptr) ? nullptr : this->FindInfo(this->name, version, force_exact_match);
this->version = (info == nullptr) ? -1 : info->GetVersion();
if (this->name != nullptr && info != nullptr && strcasecmp(this->name, this->info->GetName()) != 0 && this->version > -1) {
char script_name[1024];
strecpy(script_name, this->name, lastof(script_name));
strtolower(script_name);
char *e = strrchr(script_name, '.');
if (e != nullptr) {
*e = '\0';
e++;
int versionParam = atoi(e);
if (this->version != versionParam) this->name = stredup(GetInfo()->GetName());
}
}
this->is_random = is_random;
if (this->config_list != nullptr) delete this->config_list;
this->config_list = (info == nullptr) ? nullptr : new ScriptConfigItemList();
Expand Down

0 comments on commit 6afd89b

Please sign in to comment.