1
0
Fork 0

Codechange: Use unique_ptr for ScriptInfo instances.

Replaces raw pointers, slightly.
pull/14337/head
Peter Nelson 2025-04-28 22:26:47 +01:00
parent f182d7ab65
commit 7098320126
No known key found for this signature in database
GPG Key ID: 8EF8F0A467DF75ED
5 changed files with 23 additions and 28 deletions

View File

@ -90,7 +90,7 @@ template <> SQInteger PushClassName<AIInfo, ScriptType::AI>(HSQUIRRELVM vm) { sq
/* Remove the link to the real instance, else it might get deleted by RegisterAI() */
sq_setinstanceup(vm, 2, nullptr);
/* Register the AI to the base system */
info->GetScanner()->RegisterScript(info);
info->GetScanner()->RegisterScript(std::unique_ptr<AIInfo>{info});
return 0;
}
@ -136,22 +136,21 @@ bool AIInfo::CanLoadFromVersion(int version) const
/* static */ SQInteger AILibrary::Constructor(HSQUIRRELVM vm)
{
/* Create a new library */
AILibrary *library = new AILibrary();
auto library = std::make_unique<AILibrary>();
SQInteger res = ScriptInfo::Constructor(vm, *library);
if (res != 0) {
delete library;
return res;
}
/* Cache the category */
if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethod(library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
delete library;
return SQ_ERROR;
}
/* Register the Library to the base system */
library->GetScanner()->RegisterScript(library);
ScriptScanner *scanner = library->GetScanner();
scanner->RegisterScript(std::move(library));
return 0;
}

View File

@ -78,7 +78,7 @@ template <> SQInteger PushClassName<GameInfo, ScriptType::GS>(HSQUIRRELVM vm) {
/* Remove the link to the real instance, else it might get deleted by RegisterGame() */
sq_setinstanceup(vm, 2, nullptr);
/* Register the Game to the base system */
info->GetScanner()->RegisterScript(info);
info->GetScanner()->RegisterScript(std::unique_ptr<GameInfo>{info});
return 0;
}
@ -106,22 +106,21 @@ bool GameInfo::CanLoadFromVersion(int version) const
/* static */ SQInteger GameLibrary::Constructor(HSQUIRRELVM vm)
{
/* Create a new library */
GameLibrary *library = new GameLibrary();
auto library = std::make_unique<GameLibrary>();
SQInteger res = ScriptInfo::Constructor(vm, *library);
if (res != 0) {
delete library;
return res;
}
/* Cache the category */
if (!library->CheckMethod("GetCategory") || !library->engine->CallStringMethod(library->SQ_instance, "GetCategory", &library->category, MAX_GET_OPS)) {
delete library;
return SQ_ERROR;
}
/* Register the Library to the base system */
library->GetScanner()->RegisterScript(library);
ScriptScanner *scanner = library->GetScanner();
scanner->RegisterScript(std::move(library));
return 0;
}

View File

@ -92,8 +92,8 @@ GameLibrary *GameScannerLibrary::FindLibrary(const std::string &library, int ver
std::string library_name = fmt::format("{}.{}", library, version);
/* Check if the library + version exists */
ScriptInfoList::iterator it = this->info_list.find(library_name);
auto it = this->info_list.find(library_name);
if (it == this->info_list.end()) return nullptr;
return static_cast<GameLibrary *>((*it).second);
return static_cast<GameLibrary *>(it->second);
}

View File

@ -78,15 +78,12 @@ void ScriptScanner::RescanDir()
void ScriptScanner::Reset()
{
for (const auto &item : this->info_list) {
delete item.second;
}
this->info_list.clear();
this->info_single_list.clear();
this->info_vector.clear();
}
void ScriptScanner::RegisterScript(ScriptInfo *info)
void ScriptScanner::RegisterScript(std::unique_ptr<ScriptInfo> &&info)
{
std::string script_original_name = this->GetScriptName(*info);
std::string script_name = fmt::format("{}.{}", script_original_name, info->GetVersion());
@ -94,7 +91,6 @@ void ScriptScanner::RegisterScript(ScriptInfo *info)
/* Check if GetShortName follows the rules */
if (info->GetShortName().size() != 4) {
Debug(script, 0, "The script '{}' returned a string from GetShortName() which is not four characters. Unable to load the script.", info->GetName());
delete info;
return;
}
@ -106,7 +102,6 @@ void ScriptScanner::RegisterScript(ScriptInfo *info)
#else
if (it->second->GetMainScript() == info->GetMainScript()) {
#endif
delete info;
return;
}
@ -115,20 +110,20 @@ void ScriptScanner::RegisterScript(ScriptInfo *info)
Debug(script, 1, " 2: {}", info->GetMainScript());
Debug(script, 1, "The first is taking precedence.");
delete info;
return;
}
this->info_list[script_name] = info;
ScriptInfo *script_info = this->info_vector.emplace_back(std::move(info)).get();
this->info_list[script_name] = script_info;
if (!info->IsDeveloperOnly() || _settings_client.gui.ai_developer_tools) {
if (!script_info->IsDeveloperOnly() || _settings_client.gui.ai_developer_tools) {
/* Add the script to the 'unique' script list, where only the highest version
* of the script is registered. */
auto it = this->info_single_list.find(script_original_name);
if (it == this->info_single_list.end()) {
this->info_single_list[script_original_name] = info;
} else if (it->second->GetVersion() < info->GetVersion()) {
it->second = info;
this->info_single_list[script_original_name] = script_info;
} else if (it->second->GetVersion() < script_info->GetVersion()) {
it->second = script_info;
}
}
}

View File

@ -13,7 +13,7 @@
#include "../fileio_func.h"
#include "../string_func.h"
typedef std::map<std::string, class ScriptInfo *, CaseInsensitiveComparator> ScriptInfoList; ///< Type for the list of scripts.
using ScriptInfoList = std::map<std::string, class ScriptInfo *, CaseInsensitiveComparator>; ///< Type for the list of scripts.
/** Scanner to help finding scripts. */
class ScriptScanner : public FileScanner {
@ -51,7 +51,7 @@ public:
/**
* Register a ScriptInfo to the scanner.
*/
void RegisterScript(class ScriptInfo *info);
void RegisterScript(std::unique_ptr<class ScriptInfo> &&info);
/**
* Get the list of registered scripts to print on the console.
@ -88,7 +88,9 @@ protected:
std::string main_script; ///< The full path of the script.
std::string tar_file; ///< If, which tar file the script was in.
ScriptInfoList info_list; ///< The list of all script.
std::vector<std::unique_ptr<ScriptInfo>> info_vector;
ScriptInfoList info_list; ///< The list of all script.
ScriptInfoList info_single_list; ///< The list of all unique script. The best script (highest version) is shown.
/**