mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Pass Script Info by reference.
parent
341cdbc16b
commit
a5578166bb
|
@ -61,7 +61,7 @@ template <> SQInteger PushClassName<AIInfo, ScriptType::AI>(HSQUIRRELVM vm) { sq
|
||||||
if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, nullptr)) || instance == nullptr) return sq_throwerror(vm, "Pass an instance of a child class of AIInfo to RegisterAI");
|
if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, nullptr)) || instance == nullptr) return sq_throwerror(vm, "Pass an instance of a child class of AIInfo to RegisterAI");
|
||||||
AIInfo *info = (AIInfo *)instance;
|
AIInfo *info = (AIInfo *)instance;
|
||||||
|
|
||||||
SQInteger res = ScriptInfo::Constructor(vm, info);
|
SQInteger res = ScriptInfo::Constructor(vm, *info);
|
||||||
if (res != 0) return res;
|
if (res != 0) return res;
|
||||||
|
|
||||||
if (info->engine->MethodExists(info->SQ_instance, "MinVersionToLoad")) {
|
if (info->engine->MethodExists(info->SQ_instance, "MinVersionToLoad")) {
|
||||||
|
@ -102,7 +102,7 @@ template <> SQInteger PushClassName<AIInfo, ScriptType::AI>(HSQUIRRELVM vm) { sq
|
||||||
AIInfo *info = (AIInfo *)instance;
|
AIInfo *info = (AIInfo *)instance;
|
||||||
info->api_version = *std::rbegin(AIInfo::ApiVersions);
|
info->api_version = *std::rbegin(AIInfo::ApiVersions);
|
||||||
|
|
||||||
SQInteger res = ScriptInfo::Constructor(vm, info);
|
SQInteger res = ScriptInfo::Constructor(vm, *info);
|
||||||
if (res != 0) return res;
|
if (res != 0) return res;
|
||||||
|
|
||||||
/* 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() */
|
||||||
|
@ -138,7 +138,7 @@ bool AIInfo::CanLoadFromVersion(int version) const
|
||||||
/* Create a new library */
|
/* Create a new library */
|
||||||
AILibrary *library = new AILibrary();
|
AILibrary *library = new AILibrary();
|
||||||
|
|
||||||
SQInteger res = ScriptInfo::Constructor(vm, library);
|
SQInteger res = ScriptInfo::Constructor(vm, *library);
|
||||||
if (res != 0) {
|
if (res != 0) {
|
||||||
delete library;
|
delete library;
|
||||||
return res;
|
return res;
|
||||||
|
|
|
@ -49,9 +49,9 @@ AIScannerInfo::~AIScannerInfo()
|
||||||
delete this->info_dummy;
|
delete this->info_dummy;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string AIScannerInfo::GetScriptName(ScriptInfo *info)
|
std::string AIScannerInfo::GetScriptName(ScriptInfo &info)
|
||||||
{
|
{
|
||||||
return info->GetName();
|
return info.GetName();
|
||||||
}
|
}
|
||||||
|
|
||||||
void AIScannerInfo::RegisterAPI(class Squirrel &engine)
|
void AIScannerInfo::RegisterAPI(class Squirrel &engine)
|
||||||
|
@ -125,10 +125,10 @@ void AIScannerLibrary::Initialize()
|
||||||
ScriptScanner::Initialize("AIScanner");
|
ScriptScanner::Initialize("AIScanner");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string AIScannerLibrary::GetScriptName(ScriptInfo *info)
|
std::string AIScannerLibrary::GetScriptName(ScriptInfo &info)
|
||||||
{
|
{
|
||||||
AILibrary *library = static_cast<AILibrary *>(info);
|
AILibrary &library = static_cast<AILibrary &>(info);
|
||||||
return fmt::format("{}.{}", library->GetCategory(), library->GetInstanceName());
|
return fmt::format("{}.{}", library.GetCategory(), library.GetInstanceName());
|
||||||
}
|
}
|
||||||
|
|
||||||
void AIScannerLibrary::RegisterAPI(class Squirrel &engine)
|
void AIScannerLibrary::RegisterAPI(class Squirrel &engine)
|
||||||
|
|
|
@ -40,7 +40,7 @@ public:
|
||||||
void SetDummyAI(class AIInfo *info);
|
void SetDummyAI(class AIInfo *info);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string GetScriptName(ScriptInfo *info) override;
|
std::string GetScriptName(ScriptInfo &info) override;
|
||||||
std::string_view GetFileName() const override { return PATHSEP "info.nut"; }
|
std::string_view GetFileName() const override { return PATHSEP "info.nut"; }
|
||||||
Subdirectory GetDirectory() const override { return AI_DIR; }
|
Subdirectory GetDirectory() const override { return AI_DIR; }
|
||||||
std::string_view GetScannerName() const override { return "AIs"; }
|
std::string_view GetScannerName() const override { return "AIs"; }
|
||||||
|
@ -63,7 +63,7 @@ public:
|
||||||
class AILibrary *FindLibrary(const std::string &library, int version);
|
class AILibrary *FindLibrary(const std::string &library, int version);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string GetScriptName(ScriptInfo *info) override;
|
std::string GetScriptName(ScriptInfo &info) override;
|
||||||
std::string_view GetFileName() const override { return PATHSEP "library.nut"; }
|
std::string_view GetFileName() const override { return PATHSEP "library.nut"; }
|
||||||
Subdirectory GetDirectory() const override { return AI_LIBRARY_DIR; }
|
Subdirectory GetDirectory() const override { return AI_LIBRARY_DIR; }
|
||||||
std::string_view GetScannerName() const override { return "AI Libraries"; }
|
std::string_view GetScannerName() const override { return "AI Libraries"; }
|
||||||
|
|
|
@ -52,7 +52,7 @@ template <> SQInteger PushClassName<GameInfo, ScriptType::GS>(HSQUIRRELVM vm) {
|
||||||
if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, nullptr)) || instance == nullptr) return sq_throwerror(vm, "Pass an instance of a child class of GameInfo to RegisterGame");
|
if (SQ_FAILED(sq_getinstanceup(vm, 2, &instance, nullptr)) || instance == nullptr) return sq_throwerror(vm, "Pass an instance of a child class of GameInfo to RegisterGame");
|
||||||
GameInfo *info = (GameInfo *)instance;
|
GameInfo *info = (GameInfo *)instance;
|
||||||
|
|
||||||
SQInteger res = ScriptInfo::Constructor(vm, info);
|
SQInteger res = ScriptInfo::Constructor(vm, *info);
|
||||||
if (res != 0) return res;
|
if (res != 0) return res;
|
||||||
|
|
||||||
if (info->engine->MethodExists(info->SQ_instance, "MinVersionToLoad")) {
|
if (info->engine->MethodExists(info->SQ_instance, "MinVersionToLoad")) {
|
||||||
|
@ -108,7 +108,7 @@ bool GameInfo::CanLoadFromVersion(int version) const
|
||||||
/* Create a new library */
|
/* Create a new library */
|
||||||
GameLibrary *library = new GameLibrary();
|
GameLibrary *library = new GameLibrary();
|
||||||
|
|
||||||
SQInteger res = ScriptInfo::Constructor(vm, library);
|
SQInteger res = ScriptInfo::Constructor(vm, *library);
|
||||||
if (res != 0) {
|
if (res != 0) {
|
||||||
delete library;
|
delete library;
|
||||||
return res;
|
return res;
|
||||||
|
|
|
@ -23,9 +23,9 @@ void GameScannerInfo::Initialize()
|
||||||
ScriptScanner::Initialize("GSScanner");
|
ScriptScanner::Initialize("GSScanner");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string GameScannerInfo::GetScriptName(ScriptInfo *info)
|
std::string GameScannerInfo::GetScriptName(ScriptInfo &info)
|
||||||
{
|
{
|
||||||
return info->GetName();
|
return info.GetName();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameScannerInfo::RegisterAPI(class Squirrel &engine)
|
void GameScannerInfo::RegisterAPI(class Squirrel &engine)
|
||||||
|
@ -75,10 +75,10 @@ void GameScannerLibrary::Initialize()
|
||||||
ScriptScanner::Initialize("GSScanner");
|
ScriptScanner::Initialize("GSScanner");
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string GameScannerLibrary::GetScriptName(ScriptInfo *info)
|
std::string GameScannerLibrary::GetScriptName(ScriptInfo &info)
|
||||||
{
|
{
|
||||||
GameLibrary *library = static_cast<GameLibrary *>(info);
|
GameLibrary &library = static_cast<GameLibrary &>(info);
|
||||||
return fmt::format("{}.{}", library->GetCategory(), library->GetInstanceName());
|
return fmt::format("{}.{}", library.GetCategory(), library.GetInstanceName());
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameScannerLibrary::RegisterAPI(class Squirrel &engine)
|
void GameScannerLibrary::RegisterAPI(class Squirrel &engine)
|
||||||
|
|
|
@ -26,7 +26,7 @@ public:
|
||||||
class GameInfo *FindInfo(const std::string &name, int version, bool force_exact_match);
|
class GameInfo *FindInfo(const std::string &name, int version, bool force_exact_match);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string GetScriptName(ScriptInfo *info) override;
|
std::string GetScriptName(ScriptInfo &info) override;
|
||||||
std::string_view GetFileName() const override { return PATHSEP "info.nut"; }
|
std::string_view GetFileName() const override { return PATHSEP "info.nut"; }
|
||||||
Subdirectory GetDirectory() const override { return GAME_DIR; }
|
Subdirectory GetDirectory() const override { return GAME_DIR; }
|
||||||
std::string_view GetScannerName() const override { return "Game Scripts"; }
|
std::string_view GetScannerName() const override { return "Game Scripts"; }
|
||||||
|
@ -47,7 +47,7 @@ public:
|
||||||
class GameLibrary *FindLibrary(const std::string &library, int version);
|
class GameLibrary *FindLibrary(const std::string &library, int version);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
std::string GetScriptName(ScriptInfo *info) override;
|
std::string GetScriptName(ScriptInfo &info) override;
|
||||||
std::string_view GetFileName() const override { return PATHSEP "library.nut"; }
|
std::string_view GetFileName() const override { return PATHSEP "library.nut"; }
|
||||||
Subdirectory GetDirectory() const override { return GAME_LIBRARY_DIR; }
|
Subdirectory GetDirectory() const override { return GAME_LIBRARY_DIR; }
|
||||||
std::string_view GetScannerName() const override { return "GS Libraries"; }
|
std::string_view GetScannerName() const override { return "GS Libraries"; }
|
||||||
|
|
|
@ -27,15 +27,15 @@ bool ScriptInfo::CheckMethod(std::string_view name) const
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* static */ SQInteger ScriptInfo::Constructor(HSQUIRRELVM vm, ScriptInfo *info)
|
/* static */ SQInteger ScriptInfo::Constructor(HSQUIRRELVM vm, ScriptInfo &info)
|
||||||
{
|
{
|
||||||
/* Set some basic info from the parent */
|
/* Set some basic info from the parent */
|
||||||
Squirrel::GetInstance(vm, &info->SQ_instance, 2);
|
Squirrel::GetInstance(vm, &info.SQ_instance, 2);
|
||||||
/* Make sure the instance stays alive over time */
|
/* Make sure the instance stays alive over time */
|
||||||
sq_addref(vm, &info->SQ_instance);
|
sq_addref(vm, &info.SQ_instance);
|
||||||
|
|
||||||
info->scanner = (ScriptScanner *)Squirrel::GetGlobalPointer(vm);
|
info.scanner = (ScriptScanner *)Squirrel::GetGlobalPointer(vm);
|
||||||
info->engine = info->scanner->GetEngine();
|
info.engine = info.scanner->GetEngine();
|
||||||
|
|
||||||
/* Ensure the mandatory functions exist */
|
/* Ensure the mandatory functions exist */
|
||||||
static std::string_view const required_functions[] = {
|
static std::string_view const required_functions[] = {
|
||||||
|
@ -48,31 +48,31 @@ bool ScriptInfo::CheckMethod(std::string_view name) const
|
||||||
"CreateInstance",
|
"CreateInstance",
|
||||||
};
|
};
|
||||||
for (const auto &required_function : required_functions) {
|
for (const auto &required_function : required_functions) {
|
||||||
if (!info->CheckMethod(required_function)) return SQ_ERROR;
|
if (!info.CheckMethod(required_function)) return SQ_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Get location information of the scanner */
|
/* Get location information of the scanner */
|
||||||
info->main_script = info->scanner->GetMainScript();
|
info.main_script = info.scanner->GetMainScript();
|
||||||
info->tar_file = info->scanner->GetTarFile();
|
info.tar_file = info.scanner->GetTarFile();
|
||||||
|
|
||||||
/* Cache the data the info file gives us. */
|
/* Cache the data the info file gives us. */
|
||||||
if (!info->engine->CallStringMethod(info->SQ_instance, "GetAuthor", &info->author, MAX_GET_OPS)) return SQ_ERROR;
|
if (!info.engine->CallStringMethod(info.SQ_instance, "GetAuthor", &info.author, MAX_GET_OPS)) return SQ_ERROR;
|
||||||
if (!info->engine->CallStringMethod(info->SQ_instance, "GetName", &info->name, MAX_GET_OPS)) return SQ_ERROR;
|
if (!info.engine->CallStringMethod(info.SQ_instance, "GetName", &info.name, MAX_GET_OPS)) return SQ_ERROR;
|
||||||
if (!info->engine->CallStringMethod(info->SQ_instance, "GetShortName", &info->short_name, MAX_GET_OPS)) return SQ_ERROR;
|
if (!info.engine->CallStringMethod(info.SQ_instance, "GetShortName", &info.short_name, MAX_GET_OPS)) return SQ_ERROR;
|
||||||
if (!info->engine->CallStringMethod(info->SQ_instance, "GetDescription", &info->description, MAX_GET_OPS)) return SQ_ERROR;
|
if (!info.engine->CallStringMethod(info.SQ_instance, "GetDescription", &info.description, MAX_GET_OPS)) return SQ_ERROR;
|
||||||
if (!info->engine->CallStringMethod(info->SQ_instance, "GetDate", &info->date, MAX_GET_OPS)) return SQ_ERROR;
|
if (!info.engine->CallStringMethod(info.SQ_instance, "GetDate", &info.date, MAX_GET_OPS)) return SQ_ERROR;
|
||||||
if (!info->engine->CallIntegerMethod(info->SQ_instance, "GetVersion", &info->version, MAX_GET_OPS)) return SQ_ERROR;
|
if (!info.engine->CallIntegerMethod(info.SQ_instance, "GetVersion", &info.version, MAX_GET_OPS)) return SQ_ERROR;
|
||||||
if (info->version < 0) return SQ_ERROR;
|
if (info.version < 0) return SQ_ERROR;
|
||||||
if (!info->engine->CallStringMethod(info->SQ_instance, "CreateInstance", &info->instance_name, MAX_CREATEINSTANCE_OPS)) return SQ_ERROR;
|
if (!info.engine->CallStringMethod(info.SQ_instance, "CreateInstance", &info.instance_name, MAX_CREATEINSTANCE_OPS)) return SQ_ERROR;
|
||||||
|
|
||||||
/* The GetURL function is optional. */
|
/* The GetURL function is optional. */
|
||||||
if (info->engine->MethodExists(info->SQ_instance, "GetURL")) {
|
if (info.engine->MethodExists(info.SQ_instance, "GetURL")) {
|
||||||
if (!info->engine->CallStringMethod(info->SQ_instance, "GetURL", &info->url, MAX_GET_OPS)) return SQ_ERROR;
|
if (!info.engine->CallStringMethod(info.SQ_instance, "GetURL", &info.url, MAX_GET_OPS)) return SQ_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check if we have settings */
|
/* Check if we have settings */
|
||||||
if (info->engine->MethodExists(info->SQ_instance, "GetSettings")) {
|
if (info.engine->MethodExists(info.SQ_instance, "GetSettings")) {
|
||||||
if (!info->GetSettings()) return SQ_ERROR;
|
if (!info.GetSettings()) return SQ_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -87,7 +87,7 @@ public:
|
||||||
/**
|
/**
|
||||||
* Process the creation of a FileInfo object.
|
* Process the creation of a FileInfo object.
|
||||||
*/
|
*/
|
||||||
static SQInteger Constructor(HSQUIRRELVM vm, ScriptInfo *info);
|
static SQInteger Constructor(HSQUIRRELVM vm, ScriptInfo &info);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the scanner which has found this ScriptInfo.
|
* Get the scanner which has found this ScriptInfo.
|
||||||
|
|
|
@ -93,7 +93,7 @@ void ScriptScanner::Reset()
|
||||||
|
|
||||||
void ScriptScanner::RegisterScript(ScriptInfo *info)
|
void ScriptScanner::RegisterScript(ScriptInfo *info)
|
||||||
{
|
{
|
||||||
std::string script_original_name = this->GetScriptName(info);
|
std::string script_original_name = this->GetScriptName(*info);
|
||||||
std::string script_name = fmt::format("{}.{}", script_original_name, info->GetVersion());
|
std::string script_name = fmt::format("{}.{}", script_original_name, info->GetVersion());
|
||||||
|
|
||||||
/* Check if GetShortName follows the rules */
|
/* Check if GetShortName follows the rules */
|
||||||
|
|
|
@ -100,7 +100,7 @@ protected:
|
||||||
/**
|
/**
|
||||||
* Get the script name how to store the script in memory.
|
* Get the script name how to store the script in memory.
|
||||||
*/
|
*/
|
||||||
virtual std::string GetScriptName(ScriptInfo *info) = 0;
|
virtual std::string GetScriptName(ScriptInfo &info) = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the filename to scan for this type of script.
|
* Get the filename to scan for this type of script.
|
||||||
|
|
Loading…
Reference in New Issue