File: ade_api.h

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (485 lines) | stat: -rw-r--r-- 20,190 bytes parent folder | download
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//
//

#ifndef FS2_OPEN_ADE_API_H_H
#define FS2_OPEN_ADE_API_H_H

#include "globalincs/version.h"

#include "scripting/ade.h"
#include "scripting/ade_args.h"
#include "scripting/ade_external_serializer.h"
#include "scripting/ade_doc.h"

class lua_net_exception : public std::runtime_error {
public:
	lua_net_exception(const char* msg);
};

namespace scripting {

const size_t INVALID_ID = (size_t) -1; // Use -1 to get highest possible unsigned number

/**
 * @ingroup ade_api
 */
class ade_lib_handle {
  protected:
	size_t LibIdx;

  public:
	ade_lib_handle() = default;

	size_t GetIdx() const { return LibIdx; }
};

namespace internal {
	template<typename T> void ade_multi_serialize_fundamental(lua_State* L, const scripting::ade_table_entry& tableEntry, const luacpp::LuaValue& value, ubyte* data, int& packet_size);

	template<typename T> void ade_multi_deserialize_fundamental(lua_State* L, const scripting::ade_table_entry& tableEntry, char* data_ptr, ubyte* data, int& offset);

	inline void ade_multi_serialize_unsupported(lua_State* L, const scripting::ade_table_entry& tableEntry, const luacpp::LuaValue& /*value*/, ubyte* /*data*/, int& /*packet_size*/) {
		LuaError(L, "Cannot serialize data of type %s for sending over network!", tableEntry.GetName());
		throw lua_net_exception("Cannot serialize data of given userdata type to network");
	}

	inline void ade_multi_deserialize_unsupported(lua_State* L, const scripting::ade_table_entry& tableEntry, char* /*data_ptr*/, ubyte* /*data*/, int& /*offset*/) {
		LuaError(L, "Cannot deserialize data of type %s from network! Make sure all players are running the same version!", tableEntry.GetName());
		throw lua_net_exception("Cannot deserialize data of received userdata type from network");
	}

	enum class ade_multi_serialize_mode : size_t { NATIVE, EXTERNAL, FUNDAMENTAL, UNSUPPORTED };

	template <typename T, typename = int>
	struct ade_serializable : std::false_type { };

	template <typename T>
	struct ade_serializable <T, decltype((void)T::serialize, 0)> : std::true_type { };

	template<typename T, ade_multi_serialize_mode mode>
	struct ade_multi_serialize_dispatcher {};

	template<typename T>
	struct ade_multi_serialize_dispatcher<T, ade_multi_serialize_mode::NATIVE> {
		static constexpr ade_serialize_func serialize = &T::serialize;
		static constexpr ade_deserialize_func deserialize = &T::deserialize;
	};

	template<typename T>
	struct ade_multi_serialize_dispatcher<T, ade_multi_serialize_mode::EXTERNAL> {
		static constexpr ade_serialize_func serialize = &ade_serializable_external<T>::serialize;
		static constexpr ade_deserialize_func deserialize = &ade_serializable_external<T>::deserialize;
	};

	template<typename T>
	struct ade_multi_serialize_dispatcher<T, ade_multi_serialize_mode::FUNDAMENTAL> {
		static constexpr ade_serialize_func serialize = &ade_multi_serialize_fundamental<T>;
		static constexpr ade_deserialize_func deserialize = &ade_multi_deserialize_fundamental<T>;
	};

	template<typename T>
	struct ade_multi_serialize_dispatcher<T, ade_multi_serialize_mode::UNSUPPORTED> {
		static constexpr ade_serialize_func serialize = &ade_multi_serialize_unsupported;
		static constexpr ade_deserialize_func deserialize = &ade_multi_deserialize_unsupported;
	};
}

template<typename T>
struct ade_multi_serializer : public internal::ade_multi_serialize_dispatcher<T,
	internal::ade_serializable<T>::value ? internal::ade_multi_serialize_mode::NATIVE :
	(internal::ade_serializable_external<T>::value ? internal::ade_multi_serialize_mode::EXTERNAL : (
	std::is_fundamental<T>::value ? internal::ade_multi_serialize_mode::FUNDAMENTAL : internal::ade_multi_serialize_mode::UNSUPPORTED))> {};

/**
 * @ingroup ade_api
 */
template <class StoreType>
class ade_obj : public ade_lib_handle {
	static int lua_destructor(lua_State* L)
	{
		auto* obj =
		    static_cast<ade_obj<StoreType>*>(lua_touserdata(L, lua_upvalueindex(ADE_DESTRUCTOR_OBJ_UPVALUE_INDEX)));
		StoreType* value = nullptr;
		if (!ade_get_args(L, "o", obj->GetPtr(&value))) {
			return 0;
		}

		if (value == nullptr) {
			return 0;
		}

		value->~StoreType();
		return 0;
	}

  public:
	ade_obj(const char* in_name, const char* in_desc, const ade_lib_handle* in_deriv = nullptr, size_t size = 0, ade_serialize_func serializer = nullptr, ade_deserialize_func deserializer = nullptr)
	{
		ade_table_entry ate;

		// WMC - object metadata are uninstanced library types
		ate.Name = in_name;
		if (in_deriv != nullptr) {
			ate.DerivatorIdx = in_deriv->GetIdx();
		}
		ate.Type        = 'o';
		ate.Description = in_desc;
		ate.Size = size;
		ate.Serializer = serializer;
		ate.Deserializer = deserializer;

		if (!std::is_trivially_destructible<StoreType>::value) {
			// If this type is not trivial then we need to have a destructor
			// This is mildly dangerous since "this" will only remain valid if it was constructed statically. Since this
			// value is only used once at program startup it should be relatively safe
			ate.Destructor_upvalue = static_cast<void*>(this);
			ate.Destructor         = lua_destructor;
		}

		LibIdx = ade_manager::getInstance()->addTableEntry(ate);
	}

	// WMC - Use this to store object data for return, or for setting as a global
	ade_odata_setter<StoreType> Set(StoreType&& obj) const
	{
		return ade_odata_setter<StoreType>(LibIdx, std::move(obj));
	}
	ade_odata_setter<StoreType> Set(const StoreType& obj) const
	{
		return ade_odata_setter<StoreType>(LibIdx, obj);
	}

	// WMC - Use this to copy object data, for modification or whatever
	ade_odata_getter<StoreType> Get(StoreType* ptr) const { return ade_odata_getter<StoreType>(LibIdx, ptr); }

	// WMC - Use this to get a pointer to Lua object data.
	// Use >ONLY< when:
	// 1 - You are setting the data of an object (ie 'x' component of vector)
	// 2 - To speed up read-only calcs (ie computing dot product of vectors)
	// 3 - To get a reference to a move-only type stored in Lua memory
	ade_odata_ptr_getter<StoreType> GetPtr(StoreType** ptr) const
	{
		return ade_odata_ptr_getter<StoreType>(LibIdx, ptr);
	}
};

/**
 * @warning Utility macro. DO NOT USE!
 */
#define ADE_OBJ_DERIV_IMPL(field, type, name, desc, deriv, ...)                                                 \
	const ::scripting::ade_obj<type>& SCP_TOKEN_CONCAT(get_, field)()                                                  \
	{                                                                                                                  \
		static ::scripting::ade_obj<type> obj(name, desc, deriv, sizeof(type), __VA_ARGS__::serialize, __VA_ARGS__::deserialize);\
		return obj;                                                                                                    \
	}                                                                                                                  \
	const ::scripting::ade_obj<type>& field = SCP_TOKEN_CONCAT(get_, field)()

/**
 * @brief Define an API object
 *
 * An object is similar to a C++ class. Use this if you want to return a special type from a function that should be
 * able to do more on its own.
 *
 * @param field The name of the field by which the class should be accessible
 * @param type The type of the data the class contains
 * @param name The name the class should have in the documentation
 * @param desc Documentation about what this class is
 *
 * @ingroup ade_api
 */
#define ADE_OBJ(field, type, name, desc) ADE_OBJ_DERIV_IMPL(field, type, name, desc, nullptr, ade_multi_serializer<type>)

 /**
  * @brief Define an API object that cannot be serialized for multi, despite a serialization handler existing
  *
  * An object is similar to a C++ class. Use this if you want to return a special type from a function that should be
  * able to do more on its own.
  *
  * @param field The name of the field by which the class should be accessible
  * @param type The type of the data the class contains
  * @param name The name the class should have in the documentation
  * @param desc Documentation about what this class is
  *
  * @ingroup ade_api
  */
#define ADE_OBJ_NO_MULTI(field, type, name, desc) ADE_OBJ_DERIV_IMPL(field, type, name, desc, nullptr, internal::ade_multi_serialize_dispatcher<type, internal::ade_multi_serialize_mode::UNSUPPORTED>)


/**
 * @brief Define an API object that derives from another
 *
 * This is the same as ADE_OBJ but this allows to derive this class from another
 *
 * @param field The name of the field by which the class should be accessible
 * @param type The type of the data the class contains
 * @param name The name the class should have in the documentation
 * @param desc Documentation about what this class is
 * @param deriv The class to derive from. This should be the name of the class field (e.g. l_Object)
 *
 * @ingroup ade_api
 */
#define ADE_OBJ_DERIV(field, type, name, desc, deriv)                                                                  \
	ADE_OBJ_DERIV_IMPL(field, type, name, desc, &SCP_TOKEN_CONCAT(get_, deriv)(), ade_multi_serializer<type>)

/**
 * @brief Declare an API object but don't define it
 *
 * You should use this in headers if the class should be able to be used by other files
 *
 * @param field The name of the field
 * @param type The type of the contained value
 *
 * @ingroup ade_api
 */
#define DECLARE_ADE_OBJ(field, type)                                                                                   \
	extern const ::scripting::ade_obj<type>& SCP_TOKEN_CONCAT(get_, field)();                                          \
	extern const ::scripting::ade_obj<type>& field

/**
 * Library class
 * This is what you define a variable of to make new libraries
 *
 * @ingroup ade_api
 */
class ade_lib : public ade_lib_handle {
  public:
	explicit ade_lib(const char* in_name, const ade_lib_handle* parent = nullptr, const char* in_shortname = nullptr,
	                 const char* in_desc = nullptr);

	const char* GetName() const;
};
/**
 * @warning Utility macro. DO NOT USE!
 */
#define ADE_LIB_IMPL(field, name, short_name, desc, parent)                                                            \
	const ::scripting::ade_lib& SCP_TOKEN_CONCAT(get_, field)()                                                        \
	{                                                                                                                  \
		static ::scripting::ade_lib lib(name, parent, short_name, desc);                                               \
		return lib;                                                                                                    \
	}                                                                                                                  \
	const ::scripting::ade_lib& field = SCP_TOKEN_CONCAT(get_, field)()

/**
 * @brief Define an API library
 *
 * A library is similar to a C++ namespace or a class with static functions. It can be used to group multiple functions
 * together that serve a similar pupose
 *
 * @param field The name of the field by which the class should be ac
 * @param name The name the class should have in the documentationcessible
 * @param short_name The short name of the library, makes writing scripts easier
 * @param desc Documentation about what this class is
 *
 * @ingroup ade_api
 */
#define ADE_LIB(field, name, short_name, desc) ADE_LIB_IMPL(field, name, short_name, desc, nullptr)

/**
 * @brief Define an API library which is the child of another library
 *
 * A library is similar to a C++ namespace or a class with static functions. It can be used to group multiple functions
 * together that serve a similar pupose.
 *
 * A sublibrary is basically a nested namespace
 *
 * @param field The name of the field by which the class should be ac
 * @param name The name the class should have in the documentationcessible
 * @param short_name The short name of the library, makes writing scripts easier
 * @param desc Documentation about what this class is
 * @param parent The parent library, this should be the field name, e.g. l_Base
 *
 * @ingroup ade_api
 */
#define ADE_LIB_DERIV(field, name, short_name, desc, parent)                                                           \
	ADE_LIB_IMPL(field, name, short_name, desc, &SCP_TOKEN_CONCAT(get_, parent)())

/**
 * @brief Declare an API library but don't define it
 *
 * You should use this in headers if the library should be able to be used by other files
 *
 * @param field The name of the field, must match the name in the source file
 *
 * @ingroup ade_api
 */
#define DECLARE_ADE_LIB(field)                                                                                         \
	extern const ::scripting::ade_lib& SCP_TOKEN_CONCAT(get_, field)();                                                \
	extern const ::scripting::ade_lib& field;                                                                          \
	static const ::scripting::ade_lib* SCP_TOKEN_CONCAT(field, reference_dummy) USED_VARIABLE = &(field)

/**
 * @ingroup ade_api
 */
class ade_func : public ade_lib_handle {
  public:
	ade_func(const char* name,
		lua_CFunction func,
		const ade_lib_handle& parent,
		ade_overload_list args,
		const char* desc,
		const char* ret_type,
		const char* ret_desc,
		const gameversion::version& deprecation_version,
		const char* deprecation_message);
};

/**
 * @brief Declare an API function
 *
 * Immediately after this macro the function body should follow.
 *
 * @param name The name of the function, this may not be a string
 * @param parent The library or object containing this function
 * @param args Documentation for parameters of the function
 * @param desc Description of what the function does
 * @param ret_type The type of the returned value
 * @param ret_desc Documentation for the returned value
 *
 * @ingroup ade_api
 */
#define ADE_FUNC(name, parent, args, desc, ret_type, ret_desc)                                                         \
	static int parent##_##name##_f(lua_State* L);                                                                      \
	::scripting::ade_func parent##_##name(#name, parent##_##name##_f, parent, args, desc, ret_type, ret_desc,          \
	                                      ::gameversion::version(), nullptr);                                          \
	static int parent##_##name##_f(lua_State* L)

/**
 * @brief Declare a deprecated API function
 *
 * Immediately after this macro the function body should follow. This function is marked as deprecated and will be
 * handled specially if the targeted engine version is higher than the specified version.
 *
 * @param name The name of the function, this may not be a string
 * @param parent The library or object containing this function
 * @param args Documentation for parameters of the function
 * @param desc Description of what the function does
 * @param ret_type The type of the returned value
 * @param ret_desc Documentation for the returned value
 * @param deprecate_version Version starting from which this function is deprecated.
 * @param deprecated_msg Message for the deprecation notice. May be nullptr.
 *
 * @ingroup ade_api
 */
#define ADE_FUNC_DEPRECATED(name, parent, args, desc, ret_type, ret_desc, deprecate_version, deprecated_msg)           \
	static int parent##_##name##_f(lua_State* L);                                                                      \
	::scripting::ade_func parent##_##name(#name, parent##_##name##_f, parent, args, desc, ret_type, ret_desc,          \
	                                      deprecate_version, deprecated_msg);                                          \
	static int parent##_##name##_f(lua_State* L)

/**
 * @ingroup ade_api
 */
class ade_virtvar : public ade_lib_handle {
  public:
	ade_virtvar(const char* name,
		lua_CFunction func,
		const ade_lib_handle& parent,
		const char* args,
		const char* desc,
		const char* ret_type,
		const char* ret_desc,
		const gameversion::version& deprecation_version,
		const char* deprecation_message);
};

/**
 * @brief Declare an API variable
 *
 * Use this to handle forms of type vec.x and vec['x']. Basically an indexer for a specific variable. Format string
 * should be "o*%", where * is indexing value, and % is the value to set to when LUA_SETTTING_VAR is set
 *
 * @param name The name of the variable, this may not be a string
 * @param parent The library or object containing this field
 * @param args Documentation for the type of the value that may be assigned
 * @param desc Description of what the variable does
 * @param ret_type The type of the returned value
 * @param ret_desc Documentation for the returned value
 *
 * @ingroup ade_api
 */
#define ADE_VIRTVAR(name, parent, args, desc, ret_type, ret_desc)                                                      \
	static int parent##_##name##_f(lua_State* L);                                                                      \
	::scripting::ade_virtvar parent##_##name(#name, parent##_##name##_f, parent, args, desc, ret_type, ret_desc,       \
	                                         ::gameversion::version(), nullptr);                                       \
	static int parent##_##name##_f(lua_State* L)

/**
 * @brief Declare a deprecated API variable
 *
 * Use this to handle forms of type vec.x and vec['x']. Basically an indexer for a specific variable. Format string
 * should be "o*%", where * is indexing value, and % is the value to set to when LUA_SETTTING_VAR is set
 *
 * @param name The name of the variable, this may not be a string
 * @param parent The library or object containing this field
 * @param args Documentation for the type of the value that may be assigned
 * @param desc Description of what the variable does
 * @param ret_type The type of the returned value
 * @param ret_desc Documentation for the returned value
 * @param deprecate_version Version starting from which this function is deprecated.
 * @param deprecated_msg Message for the deprecation notice. May be nullptr.
 *
 * @ingroup ade_api
 */
#define ADE_VIRTVAR_DEPRECATED(name, parent, args, desc, ret_type, ret_desc, deprecate_version, deprecated_msg)        \
	static int parent##_##name##_f(lua_State* L);                                                                      \
	::scripting::ade_virtvar parent##_##name(#name, parent##_##name##_f, parent, args, desc, ret_type, ret_desc,       \
	                                         deprecate_version, deprecated_msg);                                       \
	static int parent##_##name##_f(lua_State* L)

/**
 * @ingroup ade_api
 */
class ade_indexer : public ade_lib_handle {
  public:
	ade_indexer(lua_CFunction func,
		const ade_lib_handle& parent,
		ade_overload_list overloads,
		const char* desc,
		const char* ret_type,
		const char* ret_desc);
};

/**
 * @brief Declare an indexer of an object
 *
 * Use this with objects to deal with forms such as vec.x, vec['x'], vec[0]. Format string should be "o*%", where * is
 * indexing value, and % is the value to set to when LUA_SETTTING_VAR is set
 *
 * @param parent The library or object containing the indexer
 * @param args Documentation for the type of the value that may be assigned
 * @param desc Description of what the variable does
 * @param ret_type The type of the returned value
 * @param ret_desc Documentation for the returned value
 *
 * @ingroup ade_api
 */
#define ADE_INDEXER(parent, args, desc, ret_type, ret_desc)                                                            \
	static int parent##___indexer_f(lua_State* L);                                                                     \
	::scripting::ade_indexer parent##___indexer(parent##___indexer_f, parent, args, desc, ret_type, ret_desc);         \
	static int parent##___indexer_f(lua_State* L)

//*************************Lua return values*************************
/**
 * @brief Return the Lua @c nil value
 *
 * @ingroup ade_api
 */
#define ADE_RETURN_NIL				0

/**
 * @brief Return @c true from an API function
 *
 * @ingroup ade_api
 */
#define ADE_RETURN_TRUE				ade_set_args(L, "b", true)

/**
 * @brief Return @c false from an API function
 *
 * @ingroup ade_api
 */
#define ADE_RETURN_FALSE			ade_set_args(L, "b", false)

}

#endif //FS2_OPEN_ADE_API_H_H