From 341cdbc16bd5edf9c431b056ce6abb5a1cd97908 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Mon, 24 Mar 2025 16:54:55 +0000 Subject: [PATCH] Codechange: Pass Script engine by reference. --- cmake/scripts/SquirrelExport.cmake | 2 +- cmake/scripts/SquirrelIncludes.cmake | 4 +- src/ai/ai_info.cpp | 14 +++---- src/ai/ai_info.hpp | 4 +- src/ai/ai_instance.cpp | 4 +- src/ai/ai_scanner.cpp | 4 +- src/ai/ai_scanner.hpp | 4 +- src/game/game_info.cpp | 12 +++--- src/game/game_info.hpp | 4 +- src/game/game_instance.cpp | 6 +-- src/game/game_scanner.cpp | 4 +- src/game/game_scanner.hpp | 4 +- src/game/game_text.cpp | 4 +- src/game/game_text.hpp | 2 +- src/script/api/ai/ai_controller.hpp.sq | 2 +- src/script/api/game/game_controller.hpp.sq | 2 +- src/script/api/script_includes.hpp.in | 2 +- src/script/script_scanner.cpp | 2 +- src/script/script_scanner.hpp | 2 +- src/script/squirrel_class.hpp | 48 +++++++++++----------- 20 files changed, 65 insertions(+), 65 deletions(-) diff --git a/cmake/scripts/SquirrelExport.cmake b/cmake/scripts/SquirrelExport.cmake index c1e601e14b..65e50470ce 100644 --- a/cmake/scripts/SquirrelExport.cmake +++ b/cmake/scripts/SquirrelExport.cmake @@ -345,7 +345,7 @@ foreach(LINE IN LISTS SOURCE_LINES) string(APPEND SQUIRREL_EXPORT "\n") # Then do the registration functions of the class. - string(APPEND SQUIRREL_EXPORT "\nvoid SQ${API_CLS}_Register(Squirrel *engine)") + string(APPEND SQUIRREL_EXPORT "\nvoid SQ${API_CLS}_Register(Squirrel &engine)") string(APPEND SQUIRREL_EXPORT "\n{") string(APPEND SQUIRREL_EXPORT "\n DefSQClass<${CLS}, ScriptType::${APIUC}> SQ${API_CLS}(\"${API_CLS}\");") if("${SUPER_CLS}" STREQUAL "Text") diff --git a/cmake/scripts/SquirrelIncludes.cmake b/cmake/scripts/SquirrelIncludes.cmake index d6022f73dd..5e1cab355b 100644 --- a/cmake/scripts/SquirrelIncludes.cmake +++ b/cmake/scripts/SquirrelIncludes.cmake @@ -19,7 +19,7 @@ endif() file(READ "${API_FILES}" SCRIPT_API_BINARY_FILES) foreach(FILE IN LISTS SCRIPT_API_BINARY_FILES) - file(STRINGS "${FILE}" LINES REGEX "^void SQ${APIUC}.*_Register\\(Squirrel \\*engine\\)$") + file(STRINGS "${FILE}" LINES REGEX "^void SQ${APIUC}.*_Register\\(Squirrel &engine\\)$") if(LINES) string(REGEX REPLACE ".*api/${APILC}/(.*)" "#include \"\\1\"" FILE "${FILE}") list(APPEND SQUIRREL_INCLUDES "${FILE}") @@ -28,7 +28,7 @@ foreach(FILE IN LISTS SCRIPT_API_BINARY_FILES) continue() endif() string(REGEX REPLACE "^.*void " " " LINE "${LINE}") - string(REGEX REPLACE "Squirrel \\*" "" LINE "${LINE}") + string(REGEX REPLACE "Squirrel &" "" LINE "${LINE}") list(APPEND SQUIRREL_REGISTER "${LINE}") endforeach() endif() diff --git a/src/ai/ai_info.cpp b/src/ai/ai_info.cpp index d59d149142..d35ddd5679 100644 --- a/src/ai/ai_info.cpp +++ b/src/ai/ai_info.cpp @@ -29,7 +29,7 @@ static bool CheckAPIVersion(const std::string &api_version) template <> SQInteger PushClassName(HSQUIRRELVM vm) { sq_pushstring(vm, "AIInfo", -1); return 1; } -/* static */ void AIInfo::RegisterAPI(Squirrel *engine) +/* static */ void AIInfo::RegisterAPI(Squirrel &engine) { /* Create the AIInfo class, and add the RegisterAI function */ DefSQClass SQAIInfo("AIInfo"); @@ -50,8 +50,8 @@ template <> SQInteger PushClassName(HSQUIRRELVM vm) { sq SQAIInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::InGame}.base(), "AICONFIG_INGAME"); SQAIInfo.PostRegister(engine); - engine->AddMethod("RegisterAI", &AIInfo::Constructor, "tx"); - engine->AddMethod("RegisterDummyAI", &AIInfo::DummyConstructor, "tx"); + engine.AddMethod("RegisterAI", &AIInfo::Constructor, "tx"); + engine.AddMethod("RegisterDummyAI", &AIInfo::DummyConstructor, "tx"); } /* static */ SQInteger AIInfo::Constructor(HSQUIRRELVM vm) @@ -125,12 +125,12 @@ bool AIInfo::CanLoadFromVersion(int version) const } -/* static */ void AILibrary::RegisterAPI(Squirrel *engine) +/* static */ void AILibrary::RegisterAPI(Squirrel &engine) { /* Create the AILibrary class, and add the RegisterLibrary function */ - engine->AddClassBegin("AILibrary"); - engine->AddClassEnd(); - engine->AddMethod("RegisterLibrary", &AILibrary::Constructor, "tx"); + engine.AddClassBegin("AILibrary"); + engine.AddClassEnd(); + engine.AddMethod("RegisterLibrary", &AILibrary::Constructor, "tx"); } /* static */ SQInteger AILibrary::Constructor(HSQUIRRELVM vm) diff --git a/src/ai/ai_info.hpp b/src/ai/ai_info.hpp index dc2e0de500..3928588114 100644 --- a/src/ai/ai_info.hpp +++ b/src/ai/ai_info.hpp @@ -23,7 +23,7 @@ public: /** * Register the functions of this class. */ - static void RegisterAPI(Squirrel *engine); + static void RegisterAPI(Squirrel &engine); /** * Create an AI, using this AIInfo as start-template. @@ -64,7 +64,7 @@ public: /** * Register the functions of this class. */ - static void RegisterAPI(Squirrel *engine); + static void RegisterAPI(Squirrel &engine); /** * Create an AI, using this AIInfo as start-template. diff --git a/src/ai/ai_instance.cpp b/src/ai/ai_instance.cpp index 07dcd40023..dab386ff13 100644 --- a/src/ai/ai_instance.cpp +++ b/src/ai/ai_instance.cpp @@ -43,7 +43,7 @@ void AIInstance::Initialize(AIInfo *info) this->api_version = info->GetAPIVersion(); /* Register the AIController (including the "import" command) */ - SQAIController_Register(this->engine); + SQAIController_Register(*this->engine); ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), _current_company); } @@ -53,7 +53,7 @@ void AIInstance::RegisterAPI() ScriptInstance::RegisterAPI(); /* Register all classes */ - SQAI_RegisterAll(this->engine); + SQAI_RegisterAll(*this->engine); if (!this->LoadCompatibilityScripts(AI_DIR, AIInfo::ApiVersions)) this->Died(); } diff --git a/src/ai/ai_scanner.cpp b/src/ai/ai_scanner.cpp index 843229e699..3bc0f9e2a7 100644 --- a/src/ai/ai_scanner.cpp +++ b/src/ai/ai_scanner.cpp @@ -54,7 +54,7 @@ std::string AIScannerInfo::GetScriptName(ScriptInfo *info) return info->GetName(); } -void AIScannerInfo::RegisterAPI(class Squirrel *engine) +void AIScannerInfo::RegisterAPI(class Squirrel &engine) { AIInfo::RegisterAPI(engine); } @@ -131,7 +131,7 @@ std::string AIScannerLibrary::GetScriptName(ScriptInfo *info) return fmt::format("{}.{}", library->GetCategory(), library->GetInstanceName()); } -void AIScannerLibrary::RegisterAPI(class Squirrel *engine) +void AIScannerLibrary::RegisterAPI(class Squirrel &engine) { AILibrary::RegisterAPI(engine); } diff --git a/src/ai/ai_scanner.hpp b/src/ai/ai_scanner.hpp index fe90204bbf..406ccb9fc7 100644 --- a/src/ai/ai_scanner.hpp +++ b/src/ai/ai_scanner.hpp @@ -44,7 +44,7 @@ protected: std::string_view GetFileName() const override { return PATHSEP "info.nut"; } Subdirectory GetDirectory() const override { return AI_DIR; } std::string_view GetScannerName() const override { return "AIs"; } - void RegisterAPI(class Squirrel *engine) override; + void RegisterAPI(class Squirrel &engine) override; private: AIInfo *info_dummy; ///< The dummy AI. @@ -67,7 +67,7 @@ protected: std::string_view GetFileName() const override { return PATHSEP "library.nut"; } Subdirectory GetDirectory() const override { return AI_LIBRARY_DIR; } std::string_view GetScannerName() const override { return "AI Libraries"; } - void RegisterAPI(class Squirrel *engine) override; + void RegisterAPI(class Squirrel &engine) override; }; #endif /* AI_SCANNER_HPP */ diff --git a/src/game/game_info.cpp b/src/game/game_info.cpp index 900bea5a12..6439e35c4d 100644 --- a/src/game/game_info.cpp +++ b/src/game/game_info.cpp @@ -27,7 +27,7 @@ static bool CheckAPIVersion(const std::string &api_version) template <> SQInteger PushClassName(HSQUIRRELVM vm) { sq_pushstring(vm, "GSInfo", -1); return 1; } -/* static */ void GameInfo::RegisterAPI(Squirrel *engine) +/* static */ void GameInfo::RegisterAPI(Squirrel &engine) { /* Create the GSInfo class, and add the RegisterGS function */ DefSQClass SQGSInfo("GSInfo"); @@ -42,7 +42,7 @@ template <> SQInteger PushClassName(HSQUIRRELVM vm) { SQGSInfo.DefSQConst(engine, ScriptConfigFlags{ScriptConfigFlag::Developer}.base(), "CONFIG_DEVELOPER"); SQGSInfo.PostRegister(engine); - engine->AddMethod("RegisterGS", &GameInfo::Constructor, "tx"); + engine.AddMethod("RegisterGS", &GameInfo::Constructor, "tx"); } /* static */ SQInteger GameInfo::Constructor(HSQUIRRELVM vm) @@ -95,12 +95,12 @@ bool GameInfo::CanLoadFromVersion(int version) const } -/* static */ void GameLibrary::RegisterAPI(Squirrel *engine) +/* static */ void GameLibrary::RegisterAPI(Squirrel &engine) { /* Create the GameLibrary class, and add the RegisterLibrary function */ - engine->AddClassBegin("GSLibrary"); - engine->AddClassEnd(); - engine->AddMethod("RegisterLibrary", &GameLibrary::Constructor, "tx"); + engine.AddClassBegin("GSLibrary"); + engine.AddClassEnd(); + engine.AddMethod("RegisterLibrary", &GameLibrary::Constructor, "tx"); } /* static */ SQInteger GameLibrary::Constructor(HSQUIRRELVM vm) diff --git a/src/game/game_info.hpp b/src/game/game_info.hpp index 8f429a62db..e2c801e18e 100644 --- a/src/game/game_info.hpp +++ b/src/game/game_info.hpp @@ -23,7 +23,7 @@ public: /** * Register the functions of this class. */ - static void RegisterAPI(Squirrel *engine); + static void RegisterAPI(Squirrel &engine); /** * Create an Game, using this GameInfo as start-template. @@ -56,7 +56,7 @@ public: /** * Register the functions of this class. */ - static void RegisterAPI(Squirrel *engine); + static void RegisterAPI(Squirrel &engine); /** * Create an GSLibrary, using this GSInfo as start-template. diff --git a/src/game/game_instance.cpp b/src/game/game_instance.cpp index cb46650f57..412e2b9204 100644 --- a/src/game/game_instance.cpp +++ b/src/game/game_instance.cpp @@ -38,7 +38,7 @@ void GameInstance::Initialize(GameInfo *info) this->api_version = info->GetAPIVersion(); /* Register the GameController */ - SQGSController_Register(this->engine); + SQGSController_Register(*this->engine); ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), OWNER_DEITY); } @@ -48,9 +48,9 @@ void GameInstance::RegisterAPI() ScriptInstance::RegisterAPI(); /* Register all classes */ - SQGS_RegisterAll(this->engine); + SQGS_RegisterAll(*this->engine); - RegisterGameTranslation(this->engine); + RegisterGameTranslation(*this->engine); if (!this->LoadCompatibilityScripts(GAME_DIR, GameInfo::ApiVersions)) this->Died(); } diff --git a/src/game/game_scanner.cpp b/src/game/game_scanner.cpp index 2707e3eade..d189a815ee 100644 --- a/src/game/game_scanner.cpp +++ b/src/game/game_scanner.cpp @@ -28,7 +28,7 @@ std::string GameScannerInfo::GetScriptName(ScriptInfo *info) return info->GetName(); } -void GameScannerInfo::RegisterAPI(class Squirrel *engine) +void GameScannerInfo::RegisterAPI(class Squirrel &engine) { GameInfo::RegisterAPI(engine); } @@ -81,7 +81,7 @@ std::string GameScannerLibrary::GetScriptName(ScriptInfo *info) return fmt::format("{}.{}", library->GetCategory(), library->GetInstanceName()); } -void GameScannerLibrary::RegisterAPI(class Squirrel *engine) +void GameScannerLibrary::RegisterAPI(class Squirrel &engine) { GameLibrary::RegisterAPI(engine); } diff --git a/src/game/game_scanner.hpp b/src/game/game_scanner.hpp index 57fe2e5924..45958d10ab 100644 --- a/src/game/game_scanner.hpp +++ b/src/game/game_scanner.hpp @@ -30,7 +30,7 @@ protected: std::string_view GetFileName() const override { return PATHSEP "info.nut"; } Subdirectory GetDirectory() const override { return GAME_DIR; } std::string_view GetScannerName() const override { return "Game Scripts"; } - void RegisterAPI(class Squirrel *engine) override; + void RegisterAPI(class Squirrel &engine) override; }; @@ -51,7 +51,7 @@ protected: std::string_view GetFileName() const override { return PATHSEP "library.nut"; } Subdirectory GetDirectory() const override { return GAME_LIBRARY_DIR; } std::string_view GetScannerName() const override { return "GS Libraries"; } - void RegisterAPI(class Squirrel *engine) override; + void RegisterAPI(class Squirrel &engine) override; }; #endif /* GAME_SCANNER_HPP */ diff --git a/src/game/game_text.cpp b/src/game/game_text.cpp index 07182ad712..92e25d52a2 100644 --- a/src/game/game_text.cpp +++ b/src/game/game_text.cpp @@ -356,12 +356,12 @@ const std::string &GetGameStringName(StringIndexInTab id) * Register the current translation to the Squirrel engine. * @param engine The engine to update/ */ -void RegisterGameTranslation(Squirrel *engine) +void RegisterGameTranslation(Squirrel &engine) { _current_gamestrings_data = LoadTranslations(); if (_current_gamestrings_data == nullptr) return; - HSQUIRRELVM vm = engine->GetVM(); + HSQUIRRELVM vm = engine.GetVM(); sq_pushroottable(vm); sq_pushstring(vm, "GSText", -1); if (SQ_FAILED(sq_get(vm, -2))) return; diff --git a/src/game/game_text.hpp b/src/game/game_text.hpp index 66375ebde1..ada1a3824a 100644 --- a/src/game/game_text.hpp +++ b/src/game/game_text.hpp @@ -33,7 +33,7 @@ using StringParamsList = std::vector; std::string_view GetGameStringPtr(StringIndexInTab id); const StringParams &GetGameStringParams(StringIndexInTab id); const std::string &GetGameStringName(StringIndexInTab id); -void RegisterGameTranslation(class Squirrel *engine); +void RegisterGameTranslation(class Squirrel &engine); void ReconsiderGameScriptLanguage(); /** Container for the raw (unencoded) language strings of a language. */ diff --git a/src/script/api/ai/ai_controller.hpp.sq b/src/script/api/ai/ai_controller.hpp.sq index 06ef24f143..10c184e1fe 100644 --- a/src/script/api/ai/ai_controller.hpp.sq +++ b/src/script/api/ai/ai_controller.hpp.sq @@ -9,7 +9,7 @@ template <> SQInteger PushClassName(HSQUIRRELVM vm) { sq_pushstring(vm, "AIController", -1); return 1; } -void SQAIController_Register(Squirrel *engine) +void SQAIController_Register(Squirrel &engine) { DefSQClass SQAIController("AIController"); SQAIController.PreRegister(engine); diff --git a/src/script/api/game/game_controller.hpp.sq b/src/script/api/game/game_controller.hpp.sq index 65da2e9443..38073674d8 100644 --- a/src/script/api/game/game_controller.hpp.sq +++ b/src/script/api/game/game_controller.hpp.sq @@ -9,7 +9,7 @@ template <> SQInteger PushClassName(HSQUIRRELVM vm) { sq_pushstring(vm, "GSController", -1); return 1; } -void SQGSController_Register(Squirrel *engine) +void SQGSController_Register(Squirrel &engine) { DefSQClass SQGSController("GSController"); SQGSController.PreRegister(engine); diff --git a/src/script/api/script_includes.hpp.in b/src/script/api/script_includes.hpp.in index 6b2f27e5cb..bf9bcda04b 100644 --- a/src/script/api/script_includes.hpp.in +++ b/src/script/api/script_includes.hpp.in @@ -21,7 +21,7 @@ static SQInteger ${APIUC}ObjectCloned(HSQUIRRELVM) throw Script_FatalError("This instance is not cloneable"); } -void SQ${APIUC}_RegisterAll(Squirrel *engine) +void SQ${APIUC}_RegisterAll(Squirrel &engine) { DefSQClass SQ${APIUC}Object("${APIUC}Object"); SQ${APIUC}Object.PreRegister(engine); diff --git a/src/script/script_scanner.cpp b/src/script/script_scanner.cpp index c21f152084..10d2ba7285 100644 --- a/src/script/script_scanner.cpp +++ b/src/script/script_scanner.cpp @@ -53,7 +53,7 @@ void ScriptScanner::ResetEngine() { this->engine->Reset(); this->engine->SetGlobalPointer(this); - this->RegisterAPI(this->engine); + this->RegisterAPI(*this->engine); } void ScriptScanner::Initialize(std::string_view name) diff --git a/src/script/script_scanner.hpp b/src/script/script_scanner.hpp index e457815abf..04a5064dcc 100644 --- a/src/script/script_scanner.hpp +++ b/src/script/script_scanner.hpp @@ -115,7 +115,7 @@ protected: /** * Register the API for this ScriptInfo. */ - virtual void RegisterAPI(class Squirrel *engine) = 0; + virtual void RegisterAPI(class Squirrel &engine) = 0; /** * Get the type of the script, in plural. diff --git a/src/script/squirrel_class.hpp b/src/script/squirrel_class.hpp index eb1a3d8535..6a683a0631 100644 --- a/src/script/squirrel_class.hpp +++ b/src/script/squirrel_class.hpp @@ -30,20 +30,20 @@ public: * This defines a method inside a class for Squirrel. */ template - void DefSQMethod(Squirrel *engine, Func function_proc, std::string_view function_name) + void DefSQMethod(Squirrel &engine, Func function_proc, std::string_view function_name) { using namespace SQConvert; - engine->AddMethod(function_name, DefSQNonStaticCallback, {}, &function_proc, sizeof(function_proc)); + engine.AddMethod(function_name, DefSQNonStaticCallback, {}, &function_proc, sizeof(function_proc)); } /** * This defines a method inside a class for Squirrel, which has access to the 'engine' (experts only!). */ template - void DefSQAdvancedMethod(Squirrel *engine, Func function_proc, std::string_view function_name) + void DefSQAdvancedMethod(Squirrel &engine, Func function_proc, std::string_view function_name) { using namespace SQConvert; - engine->AddMethod(function_name, DefSQAdvancedNonStaticCallback, {}, &function_proc, sizeof(function_proc)); + engine.AddMethod(function_name, DefSQAdvancedNonStaticCallback, {}, &function_proc, sizeof(function_proc)); } /** @@ -53,30 +53,30 @@ public: * of the code, but without it calling your function will fail! */ template - void DefSQMethod(Squirrel *engine, Func function_proc, std::string_view function_name, std::string_view params) + void DefSQMethod(Squirrel &engine, Func function_proc, std::string_view function_name, std::string_view params) { using namespace SQConvert; - engine->AddMethod(function_name, DefSQNonStaticCallback, params, &function_proc, sizeof(function_proc)); + engine.AddMethod(function_name, DefSQNonStaticCallback, params, &function_proc, sizeof(function_proc)); } /** * This defines a static method inside a class for Squirrel. */ template - void DefSQStaticMethod(Squirrel *engine, Func function_proc, std::string_view function_name) + void DefSQStaticMethod(Squirrel &engine, Func function_proc, std::string_view function_name) { using namespace SQConvert; - engine->AddMethod(function_name, DefSQStaticCallback, {}, &function_proc, sizeof(function_proc)); + engine.AddMethod(function_name, DefSQStaticCallback, {}, &function_proc, sizeof(function_proc)); } /** * This defines a static method inside a class for Squirrel, which has access to the 'engine' (experts only!). */ template - void DefSQAdvancedStaticMethod(Squirrel *engine, Func function_proc, std::string_view function_name) + void DefSQAdvancedStaticMethod(Squirrel &engine, Func function_proc, std::string_view function_name) { using namespace SQConvert; - engine->AddMethod(function_name, DefSQAdvancedStaticCallback, {}, &function_proc, sizeof(function_proc)); + engine.AddMethod(function_name, DefSQAdvancedStaticCallback, {}, &function_proc, sizeof(function_proc)); } /** @@ -86,44 +86,44 @@ public: * of the code, but without it calling your function will fail! */ template - void DefSQStaticMethod(Squirrel *engine, Func function_proc, std::string_view function_name, std::string_view params) + void DefSQStaticMethod(Squirrel &engine, Func function_proc, std::string_view function_name, std::string_view params) { using namespace SQConvert; - engine->AddMethod(function_name, DefSQStaticCallback, params, &function_proc, sizeof(function_proc)); + engine.AddMethod(function_name, DefSQStaticCallback, params, &function_proc, sizeof(function_proc)); } template - void DefSQConst(Squirrel *engine, Var value, std::string_view var_name) + void DefSQConst(Squirrel &engine, Var value, std::string_view var_name) { - engine->AddConst(var_name, value); + engine.AddConst(var_name, value); } - void PreRegister(Squirrel *engine) + void PreRegister(Squirrel &engine) { - engine->AddClassBegin(this->classname); + engine.AddClassBegin(this->classname); } - void PreRegister(Squirrel *engine, std::string_view parent_class) + void PreRegister(Squirrel &engine, std::string_view parent_class) { - engine->AddClassBegin(this->classname, parent_class); + engine.AddClassBegin(this->classname, parent_class); } template - void AddConstructor(Squirrel *engine, std::string_view params) + void AddConstructor(Squirrel &engine, std::string_view params) { using namespace SQConvert; - engine->AddMethod("constructor", DefSQConstructorCallback, params); + engine.AddMethod("constructor", DefSQConstructorCallback, params); } - void AddSQAdvancedConstructor(Squirrel *engine) + void AddSQAdvancedConstructor(Squirrel &engine) { using namespace SQConvert; - engine->AddMethod("constructor", DefSQAdvancedConstructorCallback); + engine.AddMethod("constructor", DefSQAdvancedConstructorCallback); } - void PostRegister(Squirrel *engine) + void PostRegister(Squirrel &engine) { - engine->AddClassEnd(); + engine.AddClassEnd(); } };