summaryrefslogtreecommitdiffstats
path: root/src/Bindings/ManualBindings.h
blob: 233c438c3c9215739724436098ab664ea3d1c947 (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293

// ManualBindings.h

// Declares the cManualBindings class used as a namespace for functions exported to the Lua API manually





#pragma once

#include "LuaState.h"




// fwd:
struct tolua_Error;
class cPluginLua;





/** Provides namespace for the bindings. */
class cManualBindings
{
  public:
	/** Binds all the manually implemented functions to tolua_S. */
	static void Bind(lua_State * tolua_S);

  protected:
	/** Binds the manually implemented cNetwork-related API to tolua_S.
	Implemented in ManualBindings_Network.cpp. */
	static void BindNetwork(lua_State * tolua_S);

	/** Binds the manually implemented cRankManager glue code to tolua_S.
	Implemented in ManualBindings_RankManager.cpp. */
	static void BindRankManager(lua_State * tolua_S);

	/** Binds the manually implemented cWorld API functions to tolua_S.
	Implemented in ManualBindings_World.cpp. */
	static void BindWorld(lua_State * tolua_S);

	/** Binds the manually implemented cBlockArea API functions to tlua_S.
	Implemented in ManualBindings_BlockArea.cpp. */
	static void BindBlockArea(lua_State * tolua_S);


  public:
	// Helper functions:
	static cPluginLua * GetLuaPlugin(lua_State * L);
	static int tolua_do_error(lua_State * L, const char * a_pMsg, tolua_Error * a_pToLuaError);
	static int vlua_do_error(lua_State * L, const char * a_pFormat, fmt::printf_args a_ArgList);
	template <typename... Args> static int lua_do_error(lua_State * L, const char * a_Format, const Args &... a_Args)
	{
		return vlua_do_error(L, a_Format, fmt::make_printf_args(a_Args...));
	}


	/** Binds the DoWith(ItemName) functions of regular classes. */
	template <class Ty1, class Ty2, bool (Ty1::*DoWithFn)(const AString &, cFunctionRef<bool(Ty2 &)>)>
	static int DoWith(lua_State * tolua_S)
	{
		// Check params:
		cLuaState L(tolua_S);
		if (!L.CheckParamString(2) || !L.CheckParamFunction(3))
		{
			return 0;
		}

		// Get parameters:
		Ty1 * Self;
		AString ItemName;
		cLuaState::cRef FnRef;
		L.GetStackValues(1, Self, ItemName, FnRef);
		if (Self == nullptr)
		{
			return lua_do_error(tolua_S, "Error in function call '#funcname#': Invalid 'self'");
		}
		if (ItemName.empty() || (ItemName[0] == 0))
		{
			return lua_do_error(
				tolua_S,
				"Error in function call '#funcname#': Expected a non-empty string for parameter #1"
			);
		}
		if (!FnRef.IsValid())
		{
			return lua_do_error(
				tolua_S,
				"Error in function call '#funcname#': Expected a valid callback function for parameter #2"
			);
		}

		// Call the DoWith function:
		bool res = (Self->*DoWithFn)(
			ItemName,
			[&](Ty2 & a_Item)
			{
				bool ret = false;
				L.Call(FnRef, &a_Item, cLuaState::Return, ret);
				return ret;
			}
		);

		// Push the result as the return value:
		L.Push(res);
		return 1;
	}





	/** Template for static functions DoWith(ItemName), on a type that has a static ::Get() function. */
	template <class Ty1, class Ty2, bool (Ty1::*DoWithFn)(const AString &, cFunctionRef<bool(Ty2 &)>)>
	static int StaticDoWith(lua_State * tolua_S)
	{
		// Check params:
		cLuaState L(tolua_S);
		if (!L.CheckParamString(2) || !L.CheckParamFunction(3))
		{
			return 0;
		}

		// Get parameters:
		AString ItemName;
		cLuaState::cRef FnRef;
		L.GetStackValues(2, ItemName, FnRef);
		if (ItemName.empty() || (ItemName[0] == 0))
		{
			return lua_do_error(
				tolua_S,
				"Error in function call '#funcname#': Expected a non-empty string for parameter #1"
			);
		}
		if (!FnRef.IsValid())
		{
			return lua_do_error(
				tolua_S,
				"Error in function call '#funcname#': Expected a valid callback function for parameter #2"
			);
		}

		// Call the DoWith function:
		bool res = (Ty1::Get()->*DoWithFn)(
			ItemName,
			[&](Ty2 & a_Item)
			{
				bool ret = false;
				L.Call(FnRef, &a_Item, cLuaState::Return, ret);
				return ret;
			}
		);

		// Push the result as the return value:
		L.Push(res);
		return 1;
	}





	template <class Ty1, class Ty2, bool (Ty1::*DoWithFn)(UInt32, cFunctionRef<bool(Ty2 &)>)>
	static int DoWithID(lua_State * tolua_S)
	{
		// Check params:
		cLuaState L(tolua_S);
		if (!L.CheckParamNumber(2) || !L.CheckParamFunction(3))
		{
			return 0;
		}

		// Get parameters:
		Ty1 * Self = nullptr;
		UInt32 ItemID = 0;
		cLuaState::cRef FnRef;
		L.GetStackValues(1, Self, ItemID, FnRef);
		if (Self == nullptr)
		{
			return lua_do_error(tolua_S, "Error in function call '#funcname#': Invalid 'self'");
		}
		if (!FnRef.IsValid())
		{
			return lua_do_error(
				tolua_S,
				"Error in function call '#funcname#': Expected a valid callback function for parameter #2"
			);
		}

		// Call the DoWith function:
		bool res = (Self->*DoWithFn)(
			ItemID,
			[&](Ty2 & a_Item)
			{
				bool ret = false;
				L.Call(FnRef, &a_Item, cLuaState::Return, ret);
				return ret;
			}
		);

		// Push the result as the return value:
		L.Push(res);
		return 1;
	}





	template <class Ty1, class Ty2, bool (Ty1::*ForEachFn)(cFunctionRef<bool(Ty2 &)>)>
	static int ForEach(lua_State * tolua_S)
	{
		// Check params:
		cLuaState L(tolua_S);
		if (!L.CheckParamFunction(2) || !L.CheckParamEnd(3))
		{
			return 0;
		}

		// Get the params:
		Ty1 * Self = nullptr;
		cLuaState::cRef FnRef;
		L.GetStackValues(1, Self, FnRef);
		if (Self == nullptr)
		{
			return lua_do_error(tolua_S, "Error in function call '#funcname#': Invalid 'self'.");
		}
		if (!FnRef.IsValid())
		{
			return lua_do_error(
				tolua_S,
				"Error in function call '#funcname#': Expected a valid callback function for parameter #1"
			);
		}

		// Call the enumeration:
		bool res = (Self->*ForEachFn)(
			[&](Ty2 & a_Item)
			{
				bool ret = false;  // By default continue the enumeration
				L.Call(FnRef, &a_Item, cLuaState::Return, ret);
				return ret;
			}
		);

		// Push the return value:
		L.Push(res);
		return 1;
	}





	/** Implements bindings for ForEach() functions in a class that is static (has a ::Get() static function). */
	template <class Ty1, class Ty2, bool (Ty1::*ForEachFn)(cFunctionRef<bool(Ty2 &)>)>
	static int StaticForEach(lua_State * tolua_S)
	{
		// Check params:
		cLuaState L(tolua_S);
		if (!L.CheckParamFunction(2) || !L.CheckParamEnd(3))
		{
			return 0;
		}

		// Get the params:
		cLuaState::cRef FnRef(L, 2);
		if (!FnRef.IsValid())
		{
			return lua_do_error(
				tolua_S,
				"Error in function call '#funcname#': Expected a valid callback function for parameter #1"
			);
		}

		// Call the enumeration:
		bool res = (Ty1::Get()->*ForEachFn)(
			[&](Ty2 & a_Item)
			{
				bool ret = false;  // By default continue the enumeration
				L.Call(FnRef, &a_Item, cLuaState::Return, ret);
				return ret;
			}
		);

		// Push the return value:
		L.Push(res);
		return 1;
	}
};