Codechange: Remove manual memory management of AIScannerInfo::info_dummy (#14338)

Co-authored-by: Henry Wilson <henry@henryandlizzy.uk>
This commit is contained in:
Henry 2025-06-16 10:30:22 +01:00 committed by GitHub
parent ea410cfe85
commit 56bc391763
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 9 additions and 17 deletions

View File

@ -108,7 +108,7 @@ template <> SQInteger PushClassName<AIInfo, ScriptType::AI>(HSQUIRRELVM vm) { sq
/* Remove the link to the real instance, else it might get deleted by RegisterAI() */ /* Remove the link to the real instance, else it might get deleted by RegisterAI() */
sq_setinstanceup(vm, 2, nullptr); sq_setinstanceup(vm, 2, nullptr);
/* Register the AI to the base system */ /* Register the AI to the base system */
static_cast<AIScannerInfo *>(info->GetScanner())->SetDummyAI(info); static_cast<AIScannerInfo *>(info->GetScanner())->SetDummyAI(std::unique_ptr<AIInfo>(info));
return 0; return 0;
} }

View File

@ -22,11 +22,8 @@
#include "../safeguards.h" #include "../safeguards.h"
AIScannerInfo::AIScannerInfo() : AIScannerInfo::AIScannerInfo() = default;
ScriptScanner(), AIScannerInfo::~AIScannerInfo() = default;
info_dummy(nullptr)
{
}
void AIScannerInfo::Initialize() void AIScannerInfo::Initialize()
{ {
@ -39,14 +36,9 @@ void AIScannerInfo::Initialize()
Script_CreateDummyInfo(this->engine->GetVM(), "AI", "ai"); Script_CreateDummyInfo(this->engine->GetVM(), "AI", "ai");
} }
void AIScannerInfo::SetDummyAI(class AIInfo *info) void AIScannerInfo::SetDummyAI(std::unique_ptr<class AIInfo> &&info)
{ {
this->info_dummy = info; this->info_dummy = std::move(info);
}
AIScannerInfo::~AIScannerInfo()
{
delete this->info_dummy;
} }
std::string AIScannerInfo::GetScriptName(ScriptInfo &info) std::string AIScannerInfo::GetScriptName(ScriptInfo &info)
@ -63,7 +55,7 @@ AIInfo *AIScannerInfo::SelectRandomAI() const
{ {
if (_game_mode == GM_MENU) { if (_game_mode == GM_MENU) {
Debug(script, 0, "The intro game should not use AI, loading 'dummy' AI."); Debug(script, 0, "The intro game should not use AI, loading 'dummy' AI.");
return this->info_dummy; return this->info_dummy.get();
} }
/* Filter for AIs suitable as Random AI. */ /* Filter for AIs suitable as Random AI. */
@ -72,7 +64,7 @@ AIInfo *AIScannerInfo::SelectRandomAI() const
uint num_random_ais = std::ranges::distance(random_ais); uint num_random_ais = std::ranges::distance(random_ais);
if (num_random_ais == 0) { if (num_random_ais == 0) {
Debug(script, 0, "No suitable AI found, loading 'dummy' AI."); Debug(script, 0, "No suitable AI found, loading 'dummy' AI.");
return this->info_dummy; return this->info_dummy.get();
} }
/* Pick a random AI */ /* Pick a random AI */

View File

@ -37,7 +37,7 @@ public:
/** /**
* Set the Dummy AI. * Set the Dummy AI.
*/ */
void SetDummyAI(class AIInfo *info); void SetDummyAI(std::unique_ptr<class AIInfo> &&info);
protected: protected:
std::string GetScriptName(ScriptInfo &info) override; std::string GetScriptName(ScriptInfo &info) override;
@ -47,7 +47,7 @@ protected:
void RegisterAPI(class Squirrel &engine) override; void RegisterAPI(class Squirrel &engine) override;
private: private:
AIInfo *info_dummy; ///< The dummy AI. std::unique_ptr<AIInfo> info_dummy; ///< The dummy AI.
}; };
class AIScannerLibrary : public ScriptScanner { class AIScannerLibrary : public ScriptScanner {