aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDLL/squirrelautobind.h
blob: 865479a145a1ec77ba3637e1e593d8f08837a956 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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);
	}
};