1
0
Fork 0

Codechange: use std::optional<std::string> for changing the script over char *

pull/10801/head
Rubidium 2023-05-05 21:59:50 +02:00 committed by rubidium42
parent 0fd9eb0faa
commit 9f2fc860ad
9 changed files with 25 additions and 21 deletions

View File

@ -208,7 +208,7 @@
if (_settings_game.ai_config[c] != nullptr && _settings_game.ai_config[c]->HasScript()) { if (_settings_game.ai_config[c] != nullptr && _settings_game.ai_config[c]->HasScript()) {
if (!_settings_game.ai_config[c]->ResetInfo(true)) { 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()); 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(nullptr); _settings_game.ai_config[c]->Change(std::nullopt);
if (Company::IsValidAiID(c)) { if (Company::IsValidAiID(c)) {
/* 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
@ -225,7 +225,7 @@
if (_settings_newgame.ai_config[c] != nullptr && _settings_newgame.ai_config[c]->HasScript()) { if (_settings_newgame.ai_config[c] != nullptr && _settings_newgame.ai_config[c]->HasScript()) {
if (!_settings_newgame.ai_config[c]->ResetInfo(false)) { 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()); 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(nullptr); _settings_newgame.ai_config[c]->Change(std::nullopt);
} }
} }
} }

View File

@ -175,7 +175,7 @@
if (_settings_game.game_config != nullptr && _settings_game.game_config->HasScript()) { if (_settings_game.game_config != nullptr && _settings_game.game_config->HasScript()) {
if (!_settings_game.game_config->ResetInfo(true)) { 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()); 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(nullptr); _settings_game.game_config->Change(std::nullopt);
if (Game::instance != nullptr) { if (Game::instance != nullptr) {
delete Game::instance; delete Game::instance;
Game::instance = nullptr; Game::instance = nullptr;
@ -188,7 +188,7 @@
if (_settings_newgame.game_config != nullptr && _settings_newgame.game_config->HasScript()) { if (_settings_newgame.game_config != nullptr && _settings_newgame.game_config->HasScript()) {
if (!_settings_newgame.game_config->ResetInfo(false)) { 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()); 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(nullptr); _settings_newgame.game_config->Change(std::nullopt);
} }
} }
} }

View File

@ -373,7 +373,7 @@ void MakeNewgameSettingsLive()
if (_settings_newgame.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.ai_config[c] = new AIConfig(_settings_newgame.ai_config[c]);
if (!AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->HasScript()) { if (!AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->HasScript()) {
AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(nullptr); AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(std::nullopt);
} }
} }
} }

View File

@ -66,7 +66,7 @@ struct AIPLChunkHandler : ChunkHandler {
/* Free all current data */ /* Free all current data */
for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) { for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(nullptr); AIConfig::GetConfig(c, AIConfig::SSS_FORCE_GAME)->Change(std::nullopt);
} }
CompanyID index; CompanyID index;
@ -85,13 +85,13 @@ struct AIPLChunkHandler : ChunkHandler {
AIConfig *config = AIConfig::GetConfig(index, AIConfig::SSS_FORCE_GAME); AIConfig *config = AIConfig::GetConfig(index, AIConfig::SSS_FORCE_GAME);
if (_ai_saveload_name.empty()) { if (_ai_saveload_name.empty()) {
/* A random AI. */ /* A random AI. */
config->Change(nullptr, -1, false, true); config->Change(std::nullopt, -1, false, true);
} else { } else {
config->Change(_ai_saveload_name.c_str(), _ai_saveload_version, false, _ai_saveload_is_random); config->Change(_ai_saveload_name, _ai_saveload_version, false, _ai_saveload_is_random);
if (!config->HasScript()) { if (!config->HasScript()) {
/* No version of the AI available that can load the data. Try to load the /* No version of the AI available that can load the data. Try to load the
* latest version of the AI instead. */ * latest version of the AI instead. */
config->Change(_ai_saveload_name.c_str(), -1, false, _ai_saveload_is_random); config->Change(_ai_saveload_name, -1, false, _ai_saveload_is_random);
if (!config->HasScript()) { if (!config->HasScript()) {
if (_ai_saveload_name.compare("%_dummy") != 0) { if (_ai_saveload_name.compare("%_dummy") != 0) {
Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version); Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version);

View File

@ -62,7 +62,7 @@ struct GSDTChunkHandler : ChunkHandler {
const std::vector<SaveLoad> slt = SlCompatTableHeader(_game_script_desc, _game_script_sl_compat); const std::vector<SaveLoad> slt = SlCompatTableHeader(_game_script_desc, _game_script_sl_compat);
/* Free all current data */ /* Free all current data */
GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME)->Change(nullptr); GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME)->Change(std::nullopt);
if (SlIterateArray() == -1) return; if (SlIterateArray() == -1) return;
@ -77,11 +77,11 @@ struct GSDTChunkHandler : ChunkHandler {
GameConfig *config = GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME); GameConfig *config = GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME);
if (!_game_saveload_name.empty()) { if (!_game_saveload_name.empty()) {
config->Change(_game_saveload_name.c_str(), _game_saveload_version, false, _game_saveload_is_random); config->Change(_game_saveload_name, _game_saveload_version, false, _game_saveload_is_random);
if (!config->HasScript()) { if (!config->HasScript()) {
/* No version of the GameScript available that can load the data. Try to load the /* No version of the GameScript available that can load the data. Try to load the
* latest version of the GameScript instead. */ * latest version of the GameScript instead. */
config->Change(_game_saveload_name.c_str(), -1, false, _game_saveload_is_random); config->Change(_game_saveload_name, -1, false, _game_saveload_is_random);
if (!config->HasScript()) { if (!config->HasScript()) {
if (_game_saveload_name.compare("%_dummy") != 0) { if (_game_saveload_name.compare("%_dummy") != 0) {
Debug(script, 0, "The savegame has an GameScript by the name '{}', version {} which is no longer available.", _game_saveload_name, _game_saveload_version); Debug(script, 0, "The savegame has an GameScript by the name '{}', version {} which is no longer available.", _game_saveload_name, _game_saveload_version);

View File

@ -17,10 +17,14 @@
#include "../safeguards.h" #include "../safeguards.h"
void ScriptConfig::Change(const char *name, int version, bool force_exact_match, bool is_random) void ScriptConfig::Change(std::optional<const std::string> name, int version, bool force_exact_match, bool is_random)
{ {
this->name = (name == nullptr) ? "" : name; if (name.has_value()) {
this->info = (name == nullptr) ? nullptr : this->FindInfo(this->name, version, force_exact_match); this->name = std::move(name.value());
this->info = this->FindInfo(this->name, version, force_exact_match);
} else {
this->info = nullptr;
}
this->version = (info == nullptr) ? -1 : info->GetVersion(); this->version = (info == nullptr) ? -1 : info->GetVersion();
this->is_random = is_random; this->is_random = is_random;
this->config_list.reset(); this->config_list.reset();

View File

@ -83,7 +83,7 @@ public:
* as specified. If false any compatible version is ok. * as specified. If false any compatible version is ok.
* @param is_random Is the Script chosen randomly? * @param is_random Is the Script chosen randomly?
*/ */
void Change(const char *name, int version = -1, bool force_exact_match = false, bool is_random = false); void Change(std::optional<const std::string> name, int version = -1, bool force_exact_match = false, bool is_random = false);
/** /**
* Get the ScriptInfo linked to this ScriptConfig. * Get the ScriptInfo linked to this ScriptConfig.

View File

@ -172,7 +172,7 @@ struct ScriptListWindow : public Window {
void ChangeScript() void ChangeScript()
{ {
if (this->selected == -1) { if (this->selected == -1) {
GetConfig(slot)->Change(nullptr); GetConfig(slot)->Change(std::nullopt);
} else { } else {
ScriptInfoList::const_iterator it = this->info_list->cbegin(); ScriptInfoList::const_iterator it = this->info_list->cbegin();
std::advance(it, this->selected); std::advance(it, this->selected);

View File

@ -869,7 +869,7 @@ static void AILoadConfig(IniFile &ini, const char *grpname)
/* Clean any configured AI */ /* Clean any configured AI */
for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) { for (CompanyID c = COMPANY_FIRST; c < MAX_COMPANIES; c++) {
AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME)->Change(nullptr); AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME)->Change(std::nullopt);
} }
/* If no group exists, return */ /* If no group exists, return */
@ -879,7 +879,7 @@ static void AILoadConfig(IniFile &ini, const char *grpname)
for (item = group->item; c < MAX_COMPANIES && item != nullptr; c++, item = item->next) { for (item = group->item; c < MAX_COMPANIES && item != nullptr; c++, item = item->next) {
AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME); AIConfig *config = AIConfig::GetConfig(c, AIConfig::SSS_FORCE_NEWGAME);
config->Change(item->name.c_str()); config->Change(item->name);
if (!config->HasScript()) { if (!config->HasScript()) {
if (item->name != "none") { if (item->name != "none") {
Debug(script, 0, "The AI by the name '{}' was no longer found, and removed from the list.", item->name); Debug(script, 0, "The AI by the name '{}' was no longer found, and removed from the list.", item->name);
@ -896,7 +896,7 @@ static void GameLoadConfig(IniFile &ini, const char *grpname)
IniItem *item; IniItem *item;
/* Clean any configured GameScript */ /* Clean any configured GameScript */
GameConfig::GetConfig(GameConfig::SSS_FORCE_NEWGAME)->Change(nullptr); GameConfig::GetConfig(GameConfig::SSS_FORCE_NEWGAME)->Change(std::nullopt);
/* If no group exists, return */ /* If no group exists, return */
if (group == nullptr) return; if (group == nullptr) return;
@ -906,7 +906,7 @@ static void GameLoadConfig(IniFile &ini, const char *grpname)
GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME); GameConfig *config = GameConfig::GetConfig(AIConfig::SSS_FORCE_NEWGAME);
config->Change(item->name.c_str()); config->Change(item->name);
if (!config->HasScript()) { if (!config->HasScript()) {
if (item->name != "none") { if (item->name != "none") {
Debug(script, 0, "The GameScript by the name '{}' was no longer found, and removed from the list.", item->name); Debug(script, 0, "The GameScript by the name '{}' was no longer found, and removed from the list.", item->name);