File: lua.h

package info (click to toggle)
freespace2 3.7.0%2Brepack-2
  • links: PTS, VCS
  • area: non-free
  • in suites: jessie, jessie-kfreebsd
  • size: 22,848 kB
  • ctags: 41,897
  • sloc: cpp: 369,931; makefile: 1,060; xml: 129; sh: 112
file content (241 lines) | stat: -rw-r--r-- 5,775 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
#ifndef _LUA_H
#define _LUA_H

extern "C" {
	#include <lauxlib.h>
	#include <lualib.h>
}

#include "object/object.h"
#include "globalincs/pstypes.h"
#include "menuui/mainhallmenu.h"

//*************************Lua funcs*************************
//Used to parse arguments on the stack to C values
int ade_get_args(lua_State *L, char *fmt, ...);
int ade_set_args(lua_State *L, char* fmt, ...);
void ade_stackdump(lua_State *L, char *stackdump);
int ade_friendly_error(lua_State *L);

//*************************Lua hacks*************************
//WMC - Hack to allow for quick&easy return value parsing
extern int Ade_get_args_skip;
//WMC - Tell ade_get_args it is parsing lua functions,
//which have no upvalues
extern bool Ade_get_args_lfunction;

//*************************Lua types*************************

//WMC - Define to say that this is to store just a pointer.
#define ODATA_PTR_SIZE		-1
#define ODATA_SIG_TYPE		uint						//WMC - Please don't touch.
#define ODATA_SIG_DEFAULT	0
/** Used for internal object->lua_set and lua_get->object communication.

Must remain a struct and only contain POD datatypes because this is passed via
variable args.
*/
struct ade_odata
{
	//ade_id aid;
	uint idx;
	ODATA_SIG_TYPE *sig;
	void *buf;
	int size;
	//ade_odata(){idx=UINT_MAX;sig=NULL;buf=NULL;size=0;}
/*
	ade_odata &operator =(const ade_odata &slo) {
		aid = slo.aid;
		buf = slo.buf;
		size = slo.size;

		return (*this);
	}*/
};

//WMC - 'Type' is the same as ade_set_args,
//plus some extra
//b - boolean
//d - double
//f - float
//i - integer
//s - string
//x - fix
//o - object
//EXTRA:
//l - library	//WMC - no longer exists
//u - function
//v - virtual variable
//
//u - oh wait...

#define ADE_INDEX(ate) (ate - &Ade_table_entries[0])

extern SCP_vector<class ade_table_entry> Ade_table_entries;

class ade_table_entry
{
public:
	char *Name;
	char *ShortName;

	//Important stuff
	uint ParentIdx;
	uint DerivatorIdx;
	//ade_id AdeID;
	//ade_id DerivatorID;			//Who do we derive from

	//Type-specific
	bool Instanced;				//Is this a single instance?
	char Type;
	union {
		//Variables
		bool varBool;
		double varDouble;
		float varFloat;
		int varInt;
		char *varString;

		//Functions/virtfuncs
		lua_CFunction Function;

		//Objects
		ade_odata Object;
	} Value;
	size_t Size;

	//Metadata
	char *Arguments;
	char *Description;
	char *ReturnType;
	char *ReturnDescription;

	//Subentries, of course
	//WMC - I have HAD it with these motherfriendly vectors
	//on this motherfriendly class.
	uint Num_subentries;
	uint Subentries[256];
	
private:
	//*****Internal functions
	//int IndexHandler(lua_State *L);

public:
	//*****Constructors
	ade_table_entry() : Name(NULL), ShortName(NULL), ParentIdx(UINT_MAX), DerivatorIdx(UINT_MAX), Instanced(false), 
        Size(0), Arguments(NULL), Description(NULL),  ReturnType(NULL), ReturnDescription(NULL), Num_subentries(0)
    {
        Type = '\0';
		memset(Subentries, 0, sizeof(Subentries));
	}

	//*****Operators
	//ade_table_entry &operator = (const ade_table_entry &ate);

	//*****Functions
	uint AddSubentry(ade_table_entry &n_ate)
	{
		ade_table_entry ate = n_ate;
		ate.ParentIdx = ADE_INDEX(this);
		Ade_table_entries.push_back(ate);
		uint new_idx = Ade_table_entries.size()-1;

		//WMC - Oi. Moving the Ade_table_entries vector
		//invalidates the "this" pointer. Workaround time.
		ade_table_entry *new_this = &Ade_table_entries[ate.ParentIdx];
		uint idx = new_this->Num_subentries++;
		new_this->Subentries[idx] = new_idx;

		return new_idx;
	}
	int SetTable(lua_State *L, int p_amt_ldx, int p_mtb_ldx);
	void OutputMeta(FILE *fp);

	//*****Get
	char *GetName(){if(Name!=NULL)return Name;else return ShortName;}
};

class ade_lib_handle
{
protected:
	//ade_id LibAID;
	uint LibIdx;
public:
	ade_lib_handle(){}
/*
	void AddEntry(ade_table_entry &in_ate) {
		Ade_table_entries[LibIdx].AddSubentry(in_ate);
	}
*/
	uint GetIdx() {
		return LibIdx;
	}
};

//Object class
//This is what you define a variable of to make new objects
template <class StoreType> class ade_obj : public ade_lib_handle
{
public:
	ade_obj(char*in_name, char*in_desc, ade_lib_handle* in_deriv=NULL)
	{
		ade_table_entry ate;

		//WMC - object metadata are uninstanced library types
		ate.Name = in_name;
		if(in_deriv != NULL)
			ate.DerivatorIdx = in_deriv->GetIdx();
		ate.Type = 'o';
		ate.Description = in_desc;
		ate.Value.Object.idx = Ade_table_entries.size();
		ate.Value.Object.sig = NULL;
		ate.Value.Object.size = sizeof(StoreType);

		Ade_table_entries.push_back(ate);
		LibIdx = Ade_table_entries.size() - 1;
	}

	//WMC - Use this to store object data for return, or for setting as a global
	ade_odata Set(const StoreType &obj, ODATA_SIG_TYPE n_sig=ODATA_SIG_DEFAULT) {
		ade_odata od;
		od.idx = LibIdx;
		od.sig = (uint*)&n_sig;
		od.buf = (void*)&obj;
		od.size = sizeof(StoreType);
		return od;
	}

	//WMC - Use this to copy object data, for modification or whatever
	ade_odata Get(StoreType *ptr, uint *n_sig=NULL){
		ade_odata od;
		od.idx = LibIdx;
		od.sig = n_sig;
		od.buf = ptr;
		od.size = sizeof(StoreType);
		return od;
	}

	//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)
	ade_odata GetPtr(StoreType **ptr){
		ade_odata od;
		od.idx = LibIdx;
		od.buf = (void**)ptr;
		od.size = -1;
		return od;
	}
};

//*************************Lua global structs*************************


//*************************Lua globals*************************
extern ade_obj<object_h> l_Object;
extern ade_obj<object_h> l_Weapon;
extern ade_obj<object_h> l_Ship;
extern ade_obj<object_h> l_Debris;
extern ade_obj<object_h> l_Asteroid;

#endif //_LUA_H