aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDLL/squirrelautobind.h
diff options
context:
space:
mode:
authorMaya <malte.hoermeyer@web.de>2022-11-13 04:01:14 +0100
committerGitHub <noreply@github.com>2022-11-13 03:01:14 +0000
commit23fda0b842560d2f3cf64ecf9a57d5ad2861e488 (patch)
tree9527c91bd744fb0562963e43e212c1bc5536847d /NorthstarDLL/squirrelautobind.h
parentd237401bb97c1fa2d6ee87220d49e4b3343e7201 (diff)
downloadNorthstarLauncher-23fda0b842560d2f3cf64ecf9a57d5ad2861e488.tar.gz
NorthstarLauncher-23fda0b842560d2f3cf64ecf9a57d5ad2861e488.zip
Squirrel functions auto bind (#299)
* Add defines to auto add squirrel funcs it brokey * Make it Work changed all squirrel function definitions to this system * Add defines to auto add squirrel funcs it brokey Co-authored-by: Emma-Miler <27428383+emma-miler@users.noreply.github.com> * Make it Work changed all squirrel function definitions to this system Co-authored-by: Emma-Miler <27428383+emma-miler@users.noreply.github.com> * Formatting * Good old Formatting commit * HelloGecko * Formatting Finalv2ForRealThisTime * idk anymore * i hate formatting * Rename some * Rename macro * Change function names to more human-readable * Revert to using old ScriptContext definition * Formatting Co-authored-by: RoyalBlue1 <realEmail@veryRealURL.com> Co-authored-by: Emma-Miler <27428383+emma-miler@users.noreply.github.com> Co-authored-by: Emma Miler <emma.pi@protonmail.com> Co-authored-by: BobTheBob <32057864+BobTheBob9@users.noreply.github.com>
Diffstat (limited to 'NorthstarDLL/squirrelautobind.h')
-rw-r--r--NorthstarDLL/squirrelautobind.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/NorthstarDLL/squirrelautobind.h b/NorthstarDLL/squirrelautobind.h
new file mode 100644
index 00000000..865479a1
--- /dev/null
+++ b/NorthstarDLL/squirrelautobind.h
@@ -0,0 +1,76 @@
+#pragma once
+#include <vector>
+
+typedef void (*SqAutoBindFunc)();
+
+class SquirrelAutoBindContainer
+{
+ public:
+ std::vector<std::function<void()>> clientSqAutoBindFuncs;
+ std::vector<std::function<void()>> serverSqAutoBindFuncs;
+};
+
+extern SquirrelAutoBindContainer* g_pSqAutoBindContainer;
+
+class __squirrelautobind;
+
+#define ADD_SQFUNC(returnType, funcName, argTypes, helpText, runOnContext) \
+ template <ScriptContext context> SQRESULT CONCAT2(Script_, funcName)(HSquirrelVM * sqvm); \
+ namespace \
+ { \
+ __squirrelautobind CONCAT2(__squirrelautobind, __LINE__)( \
+ []() \
+ { \
+ if constexpr (runOnContext & ScriptContext::UI) \
+ g_pSquirrel<ScriptContext::UI>->AddFuncRegistration( \
+ returnType, __STR(funcName), argTypes, helpText, CONCAT2(Script_, funcName) < ScriptContext::UI >); \
+ if constexpr (runOnContext & ScriptContext::CLIENT) \
+ g_pSquirrel<ScriptContext::CLIENT>->AddFuncRegistration( \
+ returnType, __STR(funcName), argTypes, helpText, CONCAT2(Script_, funcName) < ScriptContext::CLIENT >); \
+ }, \
+ []() \
+ { \
+ if constexpr (runOnContext & ScriptContext::SERVER) \
+ g_pSquirrel<ScriptContext::SERVER>->AddFuncRegistration( \
+ returnType, __STR(funcName), argTypes, helpText, CONCAT2(Script_, funcName) < ScriptContext::SERVER >); \
+ }); \
+ } \
+ template <ScriptContext context> SQRESULT CONCAT2(Script_, funcName)(HSquirrelVM * sqvm)
+
+#define REPLACE_SQFUNC(funcName, runOnContext) \
+ template <ScriptContext context> SQRESULT CONCAT2(Script_, funcName)(HSquirrelVM * sqvm); \
+ namespace \
+ { \
+ __squirrelautobind CONCAT2(__squirrelautobind, __LINE__)( \
+ []() \
+ { \
+ if constexpr (runOnContext & ScriptContext::UI) \
+ g_pSquirrel<ScriptContext::UI>->AddFuncOverride(__STR(funcName), CONCAT2(Script_, funcName) < ScriptContext::UI >); \
+ if constexpr (runOnContext & ScriptContext::CLIENT) \
+ g_pSquirrel<ScriptContext::CLIENT>->AddFuncOverride( \
+ __STR(funcName), CONCAT2(Script_, funcName) < ScriptContext::CLIENT >); \
+ }, \
+ []() \
+ { \
+ if constexpr (runOnContext & ScriptContext::SERVER) \
+ g_pSquirrel<ScriptContext::SERVER>->AddFuncOverride( \
+ __STR(funcName), CONCAT2(Script_, funcName) < ScriptContext::SERVER >); \
+ }); \
+ } \
+ template <ScriptContext context> SQRESULT CONCAT2(Script_, funcName)(HSquirrelVM * sqvm)
+
+class __squirrelautobind
+{
+ public:
+ __squirrelautobind() = delete;
+
+ __squirrelautobind(std::function<void()> clientAutoBindFunc, std::function<void()> serverAutoBindFunc)
+ {
+ // Bit hacky but we can't initialise this normally since this gets run automatically on load
+ if (g_pSqAutoBindContainer == nullptr)
+ g_pSqAutoBindContainer = new SquirrelAutoBindContainer();
+
+ g_pSqAutoBindContainer->clientSqAutoBindFuncs.push_back(clientAutoBindFunc);
+ g_pSqAutoBindContainer->serverSqAutoBindFuncs.push_back(serverAutoBindFunc);
+ }
+};