aboutsummaryrefslogtreecommitdiff
path: root/primedev/squirrel/squirrelautobind.h
diff options
context:
space:
mode:
authorJack <66967891+ASpoonPlaysGames@users.noreply.github.com>2023-12-27 00:32:01 +0000
committerGitHub <noreply@github.com>2023-12-27 01:32:01 +0100
commitf5ab6fb5e8be7b73e6003d4145081d5e0c0ce287 (patch)
tree90f2c6a4885dbd181799e2325cf33588697674e1 /primedev/squirrel/squirrelautobind.h
parentbb8ed59f6891b1196c5f5bbe7346cd171c8215fa (diff)
downloadNorthstarLauncher-f5ab6fb5e8be7b73e6003d4145081d5e0c0ce287.tar.gz
NorthstarLauncher-f5ab6fb5e8be7b73e6003d4145081d5e0c0ce287.zip
Folder restructuring from primedev (#624)v1.21.2-rc3v1.21.2
Copies of over the primedev folder structure for easier cherry-picking of further changes Co-authored-by: F1F7Y <filip.bartos07@proton.me>
Diffstat (limited to 'primedev/squirrel/squirrelautobind.h')
-rw-r--r--primedev/squirrel/squirrelautobind.h76
1 files changed, 76 insertions, 0 deletions
diff --git a/primedev/squirrel/squirrelautobind.h b/primedev/squirrel/squirrelautobind.h
new file mode 100644
index 00000000..0fc599f3
--- /dev/null
+++ b/primedev/squirrel/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);
+ }
+};