From 422ff9dbd866ed33ba52ac44599a6f9d1c6f549d Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 23 Mar 2025 23:29:33 +0000 Subject: [PATCH] Codechange: Use std::unique_ptr for ai/game config. --- src/ai/ai_config.cpp | 10 +++++----- src/ai/ai_config.hpp | 2 +- src/ai/ai_core.cpp | 28 +++++++++++----------------- src/ai/ai_gui.cpp | 4 ++-- src/game/game_config.cpp | 10 +++++----- src/game/game_config.hpp | 2 +- src/game/game_core.cpp | 28 +++++++++++----------------- src/openttd.cpp | 19 ++----------------- src/script/script_config.cpp | 10 +++++----- src/script/script_config.hpp | 2 +- src/settings.cpp | 28 ++++++++++++++++++++++++++++ src/settings_type.h | 16 ++++++++++++++-- 12 files changed, 86 insertions(+), 73 deletions(-) diff --git a/src/ai/ai_config.cpp b/src/ai/ai_config.cpp index bdb986692e..1c33cd77c3 100644 --- a/src/ai/ai_config.cpp +++ b/src/ai/ai_config.cpp @@ -21,18 +21,18 @@ { assert(company < MAX_COMPANIES); - AIConfig **config; + std::unique_ptr *config; if (source == SSS_FORCE_NEWGAME || (source == SSS_DEFAULT && _game_mode == GM_MENU)) { - config = &_settings_newgame.ai_config[company]; + config = &_settings_newgame.script_config.ai[company]; } else { if (source != SSS_FORCE_GAME) { Company *c = Company::GetIfValid(company); if (c != nullptr && c->ai_config != nullptr) return c->ai_config.get(); } - config = &_settings_game.ai_config[company]; + config = &_settings_game.script_config.ai[company]; } - if (*config == nullptr) *config = new AIConfig(); - return *config; + if (*config == nullptr) *config = std::make_unique(); + return config->get(); } class AIInfo *AIConfig::GetInfo() const diff --git a/src/ai/ai_config.hpp b/src/ai/ai_config.hpp index b64020b4f0..6a3723fc85 100644 --- a/src/ai/ai_config.hpp +++ b/src/ai/ai_config.hpp @@ -24,7 +24,7 @@ public: ScriptConfig() {} - AIConfig(const AIConfig *config) : + AIConfig(const AIConfig &config) : ScriptConfig(config) {} diff --git a/src/ai/ai_core.cpp b/src/ai/ai_core.cpp index eebb906128..f6fc28c68b 100644 --- a/src/ai/ai_core.cpp +++ b/src/ai/ai_core.cpp @@ -45,7 +45,7 @@ AIConfig *config = c->ai_config.get(); if (config == nullptr) { - c->ai_config = std::make_unique(AIConfig::GetConfig(company, AIConfig::SSS_FORCE_GAME)); + c->ai_config = std::make_unique(*AIConfig::GetConfig(company, AIConfig::SSS_FORCE_GAME)); config = c->ai_config.get(); } @@ -189,14 +189,8 @@ AI::scanner_library.reset(); for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) { - if (_settings_game.ai_config[c] != nullptr) { - delete _settings_game.ai_config[c]; - _settings_game.ai_config[c] = nullptr; - } - if (_settings_newgame.ai_config[c] != nullptr) { - delete _settings_newgame.ai_config[c]; - _settings_newgame.ai_config[c] = nullptr; - } + _settings_game.script_config.ai[c].reset(); + _settings_newgame.script_config.ai[c].reset(); } } } @@ -207,17 +201,17 @@ * the AIConfig. If not, remove the AI from the list (which will assign * a random new AI on reload). */ for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) { - if (_settings_game.ai_config[c] != nullptr && _settings_game.ai_config[c]->HasScript()) { - if (!_settings_game.ai_config[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.ai_config[c]->GetName()); - _settings_game.ai_config[c]->Change(std::nullopt); + if (_settings_game.script_config.ai[c] != nullptr && _settings_game.script_config.ai[c]->HasScript()) { + 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()); + _settings_game.script_config.ai[c]->Change(std::nullopt); } } - if (_settings_newgame.ai_config[c] != nullptr && _settings_newgame.ai_config[c]->HasScript()) { - if (!_settings_newgame.ai_config[c]->ResetInfo(false)) { - Debug(script, 0, "After a reload, the AI by the name '{}' was no longer found, and removed from the list.", _settings_newgame.ai_config[c]->GetName()); - _settings_newgame.ai_config[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]->ResetInfo(false)) { + 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()); + _settings_newgame.script_config.ai[c]->Change(std::nullopt); } } diff --git a/src/ai/ai_gui.cpp b/src/ai/ai_gui.cpp index 126b489bae..711441c564 100644 --- a/src/ai/ai_gui.cpp +++ b/src/ai/ai_gui.cpp @@ -258,7 +258,7 @@ struct AIConfigWindow : public Window { case WID_AIC_MOVE_UP: if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot - 1))) { - std::swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot - 1]); + std::swap(GetGameSettings().script_config.ai[this->selected_slot], GetGameSettings().script_config.ai[this->selected_slot - 1]); this->selected_slot = CompanyID(this->selected_slot - 1); this->vscroll->ScrollTowards(this->selected_slot.base()); this->InvalidateData(); @@ -267,7 +267,7 @@ struct AIConfigWindow : public Window { case WID_AIC_MOVE_DOWN: if (IsEditable(this->selected_slot) && IsEditable((CompanyID)(this->selected_slot + 1))) { - std::swap(GetGameSettings().ai_config[this->selected_slot], GetGameSettings().ai_config[this->selected_slot + 1]); + std::swap(GetGameSettings().script_config.ai[this->selected_slot], GetGameSettings().script_config.ai[this->selected_slot + 1]); ++this->selected_slot; this->vscroll->ScrollTowards(this->selected_slot.base()); this->InvalidateData(); diff --git a/src/game/game_config.cpp b/src/game/game_config.cpp index 28b6d281b0..32beac1660 100644 --- a/src/game/game_config.cpp +++ b/src/game/game_config.cpp @@ -17,14 +17,14 @@ /* static */ GameConfig *GameConfig::GetConfig(ScriptSettingSource source) { - GameConfig **config; + std::unique_ptr *config; if (source == SSS_FORCE_NEWGAME || (source == SSS_DEFAULT && _game_mode == GM_MENU)) { - config = &_settings_newgame.game_config; + config = &_settings_newgame.script_config.game; } else { - config = &_settings_game.game_config; + config = &_settings_game.script_config.game; } - if (*config == nullptr) *config = new GameConfig(); - return *config; + if (*config == nullptr) *config = std::make_unique(); + return config->get(); } class GameInfo *GameConfig::GetInfo() const diff --git a/src/game/game_config.hpp b/src/game/game_config.hpp index 41385035d2..9aa6ea994c 100644 --- a/src/game/game_config.hpp +++ b/src/game/game_config.hpp @@ -23,7 +23,7 @@ public: ScriptConfig() {} - GameConfig(const GameConfig *config) : + GameConfig(const GameConfig &config) : ScriptConfig(config) {} diff --git a/src/game/game_core.cpp b/src/game/game_core.cpp index fa69af0ae6..0542a42fea 100644 --- a/src/game/game_core.cpp +++ b/src/game/game_core.cpp @@ -114,14 +114,8 @@ Game::scanner_info.reset(); Game::scanner_library.reset(); - if (_settings_game.game_config != nullptr) { - delete _settings_game.game_config; - _settings_game.game_config = nullptr; - } - if (_settings_newgame.game_config != nullptr) { - delete _settings_newgame.game_config; - _settings_newgame.game_config = nullptr; - } + _settings_game.script_config.game.reset(); + _settings_newgame.script_config.game.reset(); } } @@ -164,22 +158,22 @@ { /* Check for both newgame as current game if we can reload the GameInfo inside * the GameConfig. If not, remove the Game from the list. */ - if (_settings_game.game_config != nullptr && _settings_game.game_config->HasScript()) { - if (!_settings_game.game_config->ResetInfo(true)) { - Debug(script, 0, "After a reload, the GameScript by the name '{}' was no longer found, and removed from the list.", _settings_game.game_config->GetName()); - _settings_game.game_config->Change(std::nullopt); + if (_settings_game.script_config.game != nullptr && _settings_game.script_config.game->HasScript()) { + 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()); + _settings_game.script_config.game->Change(std::nullopt); if (Game::instance != nullptr) { Game::instance.reset(); Game::info = nullptr; } } else if (Game::instance != nullptr) { - Game::info = _settings_game.game_config->GetInfo(); + Game::info = _settings_game.script_config.game->GetInfo(); } } - if (_settings_newgame.game_config != nullptr && _settings_newgame.game_config->HasScript()) { - if (!_settings_newgame.game_config->ResetInfo(false)) { - Debug(script, 0, "After a reload, the GameScript by the name '{}' was no longer found, and removed from the list.", _settings_newgame.game_config->GetName()); - _settings_newgame.game_config->Change(std::nullopt); + if (_settings_newgame.script_config.game != nullptr && _settings_newgame.script_config.game->HasScript()) { + if (!_settings_newgame.script_config.game->ResetInfo(false)) { + 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()); + _settings_newgame.script_config.game->Change(std::nullopt); } } } diff --git a/src/openttd.cpp b/src/openttd.cpp index 208711a7ff..e6c2b63cb1 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -344,29 +344,14 @@ static void LoadIntroGame(bool load_newgrfs = true) void MakeNewgameSettingsLive() { for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) { - if (_settings_game.ai_config[c] != nullptr) { - delete _settings_game.ai_config[c]; - } - } - if (_settings_game.game_config != nullptr) { - delete _settings_game.game_config; + _settings_game.script_config.ai[c].reset(); } + _settings_game.script_config.game.reset(); /* Copy newgame settings to active settings. * Also initialise old settings needed for savegame conversion. */ _settings_game = _settings_newgame; _old_vds = _settings_client.company.vehicle; - - for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) { - _settings_game.ai_config[c] = nullptr; - if (_settings_newgame.ai_config[c] != nullptr) { - _settings_game.ai_config[c] = new AIConfig(_settings_newgame.ai_config[c]); - } - } - _settings_game.game_config = nullptr; - if (_settings_newgame.game_config != nullptr) { - _settings_game.game_config = new GameConfig(_settings_newgame.game_config); - } } void OpenBrowser(const std::string &url) diff --git a/src/script/script_config.cpp b/src/script/script_config.cpp index 1c84c493a0..c5d8d136c8 100644 --- a/src/script/script_config.cpp +++ b/src/script/script_config.cpp @@ -35,14 +35,14 @@ void ScriptConfig::Change(std::optional name, int version, bool for this->ClearConfigList(); } -ScriptConfig::ScriptConfig(const ScriptConfig *config) +ScriptConfig::ScriptConfig(const ScriptConfig &config) { - this->name = config->name; - this->info = config->info; - this->version = config->version; + this->name = config.name; + this->info = config.info; + this->version = config.version; this->to_load_data.reset(); - for (const auto &item : config->settings) { + for (const auto &item : config.settings) { this->settings[item.first] = item.second; } } diff --git a/src/script/script_config.hpp b/src/script/script_config.hpp index 4822288d84..cb019d90ab 100644 --- a/src/script/script_config.hpp +++ b/src/script/script_config.hpp @@ -66,7 +66,7 @@ public: * Create a new Script config that is a copy of an existing config. * @param config The object to copy. */ - ScriptConfig(const ScriptConfig *config); + ScriptConfig(const ScriptConfig &config); /** Delete an Script configuration. */ virtual ~ScriptConfig(); diff --git a/src/settings.cpp b/src/settings.cpp index e405fcb6a7..a39b82fdc9 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -2009,3 +2009,31 @@ void IConsoleListSettings(const char *prefilter) IConsolePrint(CC_HELP, "Use 'setting' command to change a value."); } + +ScriptConfigSettings::ScriptConfigSettings() +{ + /* Instantiate here, because unique_ptr needs a complete type. */ +} + +ScriptConfigSettings::~ScriptConfigSettings() +{ + /* Instantiate here, because unique_ptr needs a complete type. */ +} + +ScriptConfigSettings::ScriptConfigSettings(const ScriptConfigSettings &other) +{ + *this = other; +} + +ScriptConfigSettings &ScriptConfigSettings::operator=(const ScriptConfigSettings &other) +{ + for (CompanyID c = CompanyID::Begin(); c < MAX_COMPANIES; ++c) { + if (other.ai[c] != nullptr) { + this->ai[c] = std::make_unique(*other.ai[c]); + } + } + if (other.game != nullptr) { + this->game = std::make_unique(*other.game); + } + return *this; +} diff --git a/src/settings_type.h b/src/settings_type.h index e9b589586b..f6dd5bc98c 100644 --- a/src/settings_type.h +++ b/src/settings_type.h @@ -595,6 +595,19 @@ struct CompanySettings { VehicleDefaultSettings vehicle{}; ///< default settings for vehicles }; +/** Container for AI and Game script configuration. */ +struct ScriptConfigSettings +{ + ReferenceThroughBaseContainer, MAX_COMPANIES>> ai; ///< settings per company + std::unique_ptr game; ///< settings for gamescript + + ScriptConfigSettings(); + ~ScriptConfigSettings(); + + ScriptConfigSettings(const ScriptConfigSettings &other); + ScriptConfigSettings &operator=(const ScriptConfigSettings &other); +}; + /** All settings together for the game. */ struct GameSettings { DifficultySettings difficulty; ///< settings related to the difficulty @@ -602,8 +615,7 @@ struct GameSettings { ConstructionSettings construction; ///< construction of things in-game AISettings ai; ///< what may the AI do? ScriptSettings script; ///< settings for scripts - ReferenceThroughBaseContainer> ai_config; ///< settings per company - class GameConfig *game_config; ///< settings for gamescript + ScriptConfigSettings script_config; ///< AI and Gamescript configuration. PathfinderSettings pf; ///< settings for all pathfinders OrderSettings order; ///< settings related to orders VehicleSettings vehicle; ///< options for vehicles