1
0
Fork 0

Fix: [Script] Reset instance when changing running scripts in scenario editor (#13906)

pull/13960/head
Loïc Guilloux 2025-04-03 21:14:45 +02:00 committed by GitHub
parent 20805ba84b
commit b25daba561
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 6 deletions

View File

@ -95,6 +95,11 @@ public:
*/ */
static class GameInstance *GetInstance() { return Game::instance.get(); } static class GameInstance *GetInstance() { return Game::instance.get(); }
/**
* Reset the current active instance.
*/
static void ResetInstance();
/** Wrapper function for GameScanner::HasGame */ /** Wrapper function for GameScanner::HasGame */
static bool HasGame(const struct ContentInfo *ci, bool md5sum); static bool HasGame(const struct ContentInfo *ci, bool md5sum);
static bool HasGameLibrary(const ContentInfo *ci, bool md5sum); static bool HasGameLibrary(const ContentInfo *ci, bool md5sum);

View File

@ -103,8 +103,7 @@
{ {
Backup<CompanyID> cur_company(_current_company); Backup<CompanyID> cur_company(_current_company);
Game::instance.reset(); Game::ResetInstance();
Game::info = nullptr;
cur_company.Restore(); cur_company.Restore();
@ -162,10 +161,7 @@
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 '{}' was no longer found, and removed from the list.", _settings_game.script_config.game->GetName());
_settings_game.script_config.game->Change(std::nullopt); _settings_game.script_config.game->Change(std::nullopt);
if (Game::instance != nullptr) { if (Game::instance != nullptr) Game::ResetInstance();
Game::instance.reset();
Game::info = nullptr;
}
} else if (Game::instance != nullptr) { } else if (Game::instance != nullptr) {
Game::info = _settings_game.script_config.game->GetInfo(); Game::info = _settings_game.script_config.game->GetInfo();
} }
@ -234,6 +230,12 @@
return Game::scanner_library->FindLibrary(library, version); return Game::scanner_library->FindLibrary(library, version);
} }
/* static */ void Game::ResetInstance()
{
Game::instance.reset();
Game::info = nullptr;
}
/** /**
* Check whether we have an Game (library) with the exact characteristics as ci. * Check whether we have an Game (library) with the exact characteristics as ci.
* @param ci the characteristics to search on (shortname and md5sum) * @param ci the characteristics to search on (shortname and md5sum)

View File

@ -173,8 +173,21 @@ struct ScriptListWindow : public Window {
std::advance(it, this->selected); std::advance(it, this->selected);
GetConfig(slot)->Change(it->second->GetName(), it->second->GetVersion()); GetConfig(slot)->Change(it->second->GetName(), it->second->GetVersion());
} }
if (_game_mode == GM_EDITOR) {
if (slot == OWNER_DEITY) {
if (Game::GetInstance() != nullptr) Game::ResetInstance();
Game::StartNew();
} else {
Company *c = Company::GetIfValid(slot);
if (c != nullptr && c->ai_instance != nullptr) {
c->ai_instance.reset();
AI::StartNew(slot);
}
}
}
InvalidateWindowData(WC_GAME_OPTIONS, slot == OWNER_DEITY ? WN_GAME_OPTIONS_GS : WN_GAME_OPTIONS_AI); InvalidateWindowData(WC_GAME_OPTIONS, slot == OWNER_DEITY ? WN_GAME_OPTIONS_GS : WN_GAME_OPTIONS_AI);
InvalidateWindowClassesData(WC_SCRIPT_SETTINGS); InvalidateWindowClassesData(WC_SCRIPT_SETTINGS);
InvalidateWindowClassesData(WC_SCRIPT_DEBUG, -1);
CloseWindowByClass(WC_QUERY_STRING); CloseWindowByClass(WC_QUERY_STRING);
InvalidateWindowClassesData(WC_TEXTFILE); InvalidateWindowClassesData(WC_TEXTFILE);
} }