1
0
Fork 0

Change: [Script] Append version in config file when forcing exact version

pull/13565/head
SamuXarick 2025-02-17 22:04:47 +00:00
parent ae7903e71f
commit e3db611fde
3 changed files with 42 additions and 19 deletions

View File

@ -203,21 +203,21 @@
for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) { for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
if (_settings_game.script_config.ai[c] != nullptr && _settings_game.script_config.ai[c]->HasScript()) { if (_settings_game.script_config.ai[c] != nullptr && _settings_game.script_config.ai[c]->HasScript()) {
if (!_settings_game.script_config.ai[c]->ResetInfo(true)) { if (!_settings_game.script_config.ai[c]->ResetInfo(true)) {
Debug(script, 0, "After a reload, the AI by the name '{}' was no longer found, and removed from the list.", _settings_game.script_config.ai[c]->GetName()); Debug(script, 0, "After a reload, the AI by the name '{}' with version {} was no longer found, and removed from the list.", _settings_game.script_config.ai[c]->GetName(), _settings_game.script_config.ai[c]->GetVersion());
_settings_game.script_config.ai[c]->Change(std::nullopt); _settings_game.script_config.ai[c]->Change(std::nullopt);
} }
} }
if (_settings_newgame.script_config.ai[c] != nullptr && _settings_newgame.script_config.ai[c]->HasScript()) { if (_settings_newgame.script_config.ai[c] != nullptr && _settings_newgame.script_config.ai[c]->HasScript()) {
if (!_settings_newgame.script_config.ai[c]->ResetInfo(false)) { if (!_settings_newgame.script_config.ai[c]->ResetInfo(_settings_newgame.script_config.ai[c]->GetForceExactMatch())) {
Debug(script, 0, "After a reload, the AI by the name '{}' was no longer found, and removed from the list.", _settings_newgame.script_config.ai[c]->GetName()); Debug(script, 0, "After a reload, the AI by the name '{}' with version {} was no longer found, and removed from the list.", _settings_newgame.script_config.ai[c]->GetName(), _settings_game.script_config.ai[c]->GetVersion());
_settings_newgame.script_config.ai[c]->Change(std::nullopt); _settings_newgame.script_config.ai[c]->Change(std::nullopt);
} }
} }
if (Company::IsValidAiID(c) && Company::Get(c)->ai_config != nullptr) { if (Company::IsValidAiID(c) && Company::Get(c)->ai_config != nullptr) {
AIConfig *config = Company::Get(c)->ai_config.get(); AIConfig *config = Company::Get(c)->ai_config.get();
if (!config->ResetInfo(true)) { if (!config->ResetInfo(_settings_newgame.script_config.ai[c]->GetForceExactMatch())) {
/* The code belonging to an already running AI was deleted. We can only do /* The code belonging to an already running AI was deleted. We can only do
* one thing here to keep everything sane and that is kill the AI. After * one thing here to keep everything sane and that is kill the AI. After
* killing the offending AI we start a random other one in it's place, just * killing the offending AI we start a random other one in it's place, just

View File

@ -159,7 +159,7 @@
* the GameConfig. If not, remove the Game from the list. */ * the GameConfig. If not, remove the Game from the list. */
if (_settings_game.script_config.game != nullptr && _settings_game.script_config.game->HasScript()) { if (_settings_game.script_config.game != nullptr && _settings_game.script_config.game->HasScript()) {
if (!_settings_game.script_config.game->ResetInfo(true)) { if (!_settings_game.script_config.game->ResetInfo(true)) {
Debug(script, 0, "After a reload, the GameScript by the name '{}' was no longer found, and removed from the list.", _settings_game.script_config.game->GetName()); Debug(script, 0, "After a reload, the GameScript by the name '{}' with version {} was no longer found, and removed from the list.", _settings_game.script_config.game->GetName(), _settings_newgame.script_config.game->GetVersion());
_settings_game.script_config.game->Change(std::nullopt); _settings_game.script_config.game->Change(std::nullopt);
if (Game::instance != nullptr) Game::ResetInstance(); if (Game::instance != nullptr) Game::ResetInstance();
} else if (Game::instance != nullptr) { } else if (Game::instance != nullptr) {
@ -167,8 +167,8 @@
} }
} }
if (_settings_newgame.script_config.game != nullptr && _settings_newgame.script_config.game->HasScript()) { if (_settings_newgame.script_config.game != nullptr && _settings_newgame.script_config.game->HasScript()) {
if (!_settings_newgame.script_config.game->ResetInfo(false)) { if (!_settings_newgame.script_config.game->ResetInfo(_settings_newgame.script_config.game->GetForceExactMatch())) {
Debug(script, 0, "After a reload, the GameScript by the name '{}' was no longer found, and removed from the list.", _settings_newgame.script_config.game->GetName()); Debug(script, 0, "After a reload, the GameScript by the name '{}' with version {} was no longer found, and removed from the list.", _settings_newgame.script_config.game->GetName(), _settings_newgame.script_config.game->GetVersion());
_settings_newgame.script_config.game->Change(std::nullopt); _settings_newgame.script_config.game->Change(std::nullopt);
} }
} }

View File

@ -948,6 +948,21 @@ static void ValidateSettings()
} }
} }
static std::pair<std::string_view, int> TryReadScriptNameWithVersion(std::string_view item_name)
{
StringConsumer consumer{item_name};
auto name = consumer.ReadUntilChar('.', StringConsumer::SKIP_ONE_SEPARATOR);
if (consumer.AnyBytesLeft()) {
auto version = consumer.TryReadIntegerBase<uint32_t>(10);
if (version.has_value()) {
return { name, *version };
}
}
return { name, -1 };
}
static void AILoadConfig(const IniFile &ini, std::string_view grpname) static void AILoadConfig(const IniFile &ini, std::string_view grpname)
{ {
const IniGroup *group = ini.GetGroup(grpname); const IniGroup *group = ini.GetGroup(grpname);
@ -966,11 +981,16 @@ static void AILoadConfig(const IniFile &ini, std::string_view grpname)
config->Change(item.name); config->Change(item.name);
if (!config->HasScript()) { if (!config->HasScript()) {
if (item.name != "none") { auto [name, version] = TryReadScriptNameWithVersion(item.name);
Debug(script, 0, "The AI by the name '{}' was no longer found, and removed from the list.", item.name); config->Change(name, version, version != -1);
if (!config->HasScript()) {
if (name != "none") {
Debug(script, 0, "The AI by the name '{}' was no longer found, and removed from the list.", name);
continue; continue;
} }
} }
}
if (item.value.has_value()) config->StringToSettings(*item.value); if (item.value.has_value()) config->StringToSettings(*item.value);
++c; ++c;
if (c >= MAX_COMPANIES) break; if (c >= MAX_COMPANIES) break;
@ -993,11 +1013,16 @@ static void GameLoadConfig(const IniFile &ini, std::string_view grpname)
config->Change(item.name); config->Change(item.name);
if (!config->HasScript()) { if (!config->HasScript()) {
if (item.name != "none") { auto [name, version] = TryReadScriptNameWithVersion(item.name);
Debug(script, 0, "The GameScript by the name '{}' was no longer found, and removed from the list.", item.name); config->Change(name, version, version != -1);
if (!config->HasScript()) {
if (name != "none") {
Debug(script, 0, "The GameScript by the name '{}' was no longer found, and removed from the list.", name);
return; return;
} }
} }
}
if (item.value.has_value()) config->StringToSettings(*item.value); if (item.value.has_value()) config->StringToSettings(*item.value);
} }
@ -1184,13 +1209,12 @@ static void AISaveConfig(IniFile &ini, std::string_view grpname)
for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) { for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) {
AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME); AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
std::string name; std::string name = "none";
std::string value = config->SettingsToString(); std::string value = config->SettingsToString();
if (config->HasScript()) { if (config->HasScript()) {
name = config->GetName(); name = config->GetName();
} else { if (config->GetForceExactMatch()) name = fmt::format("{}.{}", name, config->GetVersion());
name = "none";
} }
group.CreateItem(name).SetValue(value); group.CreateItem(name).SetValue(value);
@ -1203,13 +1227,12 @@ static void GameSaveConfig(IniFile &ini, std::string_view grpname)
group.Clear(); group.Clear();
GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME); GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME);
std::string name; std::string name = "none";
std::string value = config->SettingsToString(); std::string value = config->SettingsToString();
if (config->HasScript()) { if (config->HasScript()) {
name = config->GetName(); name = config->GetName();
} else { if (config->GetForceExactMatch()) name = fmt::format("{}.{}", name, config->GetVersion());
name = "none";
} }
group.CreateItem(name).SetValue(value); group.CreateItem(name).SetValue(value);