1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2 
3 #include "unitsync.h"
4 
5 #include "Lua/LuaParser.h"
6 #include "System/FileSystem/ArchiveScanner.h"
7 #include "System/FileSystem/VFSHandler.h"
8 #include "System/exportdefines.h"
9 
10 #include <string>
11 #include <vector>
12 
13 
14 /******************************************************************************/
15 
16 static LuaParser* luaParser = NULL;
17 
18 static LuaTable rootTable;
19 static LuaTable currTable;
20 static std::vector<LuaTable> luaTables;
21 
22 static std::vector<int>         intKeys;
23 static std::vector<std::string> strKeys;
24 
25 
26 /******************************************************************************/
27 //
28 //  Primary calls
29 //
30 
lpClose()31 EXPORT(void) lpClose()
32 {
33 	rootTable = LuaTable();
34 	currTable = LuaTable();
35 
36 	luaTables.clear();
37 
38 	intKeys.clear();
39 	strKeys.clear();
40 
41 	delete luaParser;
42 	luaParser = NULL;
43 
44 	return;
45 }
46 
47 
lpOpenFile(const char * filename,const char * fileModes,const char * accessModes)48 EXPORT(int) lpOpenFile(const char* filename, const char* fileModes,
49 		const char* accessModes)
50 {
51 	lpClose();
52 	luaParser = new LuaParser(filename, fileModes, accessModes);
53 	return 1;
54 }
55 
56 
lpOpenSource(const char * source,const char * accessModes)57 EXPORT(int) lpOpenSource(const char* source, const char* accessModes)
58 {
59 	lpClose();
60 	luaParser = new LuaParser(source, accessModes);
61 	return 1;
62 }
63 
64 
lpExecute()65 EXPORT(int) lpExecute()
66 {
67 	if (!luaParser) {
68 		return 0;
69 	}
70 	const bool success = luaParser->Execute();
71 	rootTable = luaParser->GetRoot();
72 	currTable = rootTable;
73 	return success ? 1 : 0;
74 }
75 
76 
lpErrorLog()77 EXPORT(const char*) lpErrorLog()
78 {
79 	if (luaParser) {
80 		return GetStr(luaParser->GetErrorLog());
81 	}
82 	return GetStr("no LuaParser is loaded");
83 }
84 
85 
86 /******************************************************************************/
87 //
88 //  Environment additions
89 //
90 
lpAddTableInt(int key,int override)91 EXPORT(void) lpAddTableInt(int key, int override)
92 {
93 	if (luaParser) { luaParser->GetTable(key, override); }
94 }
95 
96 
lpAddTableStr(const char * key,int override)97 EXPORT(void) lpAddTableStr(const char* key, int override)
98 {
99 	if (luaParser) { luaParser->GetTable(key, override); }
100 }
101 
102 
lpEndTable()103 EXPORT(void) lpEndTable()
104 {
105 	if (luaParser) { luaParser->EndTable(); }
106 }
107 
108 
lpAddIntKeyIntVal(int key,int val)109 EXPORT(void) lpAddIntKeyIntVal(int key, int val)
110 {
111 	if (luaParser) { luaParser->AddInt(key, val); }
112 }
113 
114 
lpAddStrKeyIntVal(const char * key,int val)115 EXPORT(void) lpAddStrKeyIntVal(const char* key, int val)
116 {
117 	if (luaParser) { luaParser->AddInt(key, val); }
118 }
119 
120 
lpAddIntKeyBoolVal(int key,int val)121 EXPORT(void) lpAddIntKeyBoolVal(int key, int val)
122 {
123 	if (luaParser) { luaParser->AddBool(key, val); }
124 }
125 
126 
lpAddStrKeyBoolVal(const char * key,int val)127 EXPORT(void) lpAddStrKeyBoolVal(const char* key, int val)
128 {
129 	if (luaParser) { luaParser->AddBool(key, val); }
130 }
131 
132 
lpAddIntKeyFloatVal(int key,float val)133 EXPORT(void) lpAddIntKeyFloatVal(int key, float val)
134 {
135 	if (luaParser) { luaParser->AddFloat(key, val); }
136 }
137 
138 
lpAddStrKeyFloatVal(const char * key,float val)139 EXPORT(void) lpAddStrKeyFloatVal(const char* key, float val)
140 {
141 	if (luaParser) { luaParser->AddFloat(key, val); }
142 }
143 
144 
lpAddIntKeyStrVal(int key,const char * val)145 EXPORT(void) lpAddIntKeyStrVal(int key, const char* val)
146 {
147 	if (luaParser) { luaParser->AddString(key, val); }
148 }
149 
150 
lpAddStrKeyStrVal(const char * key,const char * val)151 EXPORT(void) lpAddStrKeyStrVal(const char* key, const char* val)
152 {
153 	if (luaParser) { luaParser->AddString(key, val); }
154 }
155 
156 
157 /******************************************************************************/
158 //
159 //  Table manipulation
160 //
161 
lpRootTable()162 EXPORT(int) lpRootTable()
163 {
164 	currTable = rootTable;
165 	luaTables.clear();
166 	return currTable.IsValid() ? 1 : 0;
167 }
168 
169 
lpRootTableExpr(const char * expr)170 EXPORT(int) lpRootTableExpr(const char* expr)
171 {
172 	currTable = rootTable.SubTableExpr(expr);
173 	luaTables.clear();
174 	return currTable.IsValid() ? 1 : 0;
175 }
176 
177 
lpSubTableInt(int key)178 EXPORT(int) lpSubTableInt(int key)
179 {
180 	luaTables.push_back(currTable);
181 	currTable = currTable.SubTable(key);
182 	return currTable.IsValid() ? 1 : 0;
183 }
184 
185 
lpSubTableStr(const char * key)186 EXPORT(int) lpSubTableStr(const char* key)
187 {
188 	luaTables.push_back(currTable);
189 	currTable = currTable.SubTable(key);
190 	return currTable.IsValid() ? 1 : 0;
191 }
192 
193 
lpSubTableExpr(const char * expr)194 EXPORT(int) lpSubTableExpr(const char* expr)
195 {
196 	luaTables.push_back(currTable);
197 	currTable = currTable.SubTableExpr(expr);
198 	return currTable.IsValid() ? 1 : 0;
199 }
200 
201 
lpPopTable()202 EXPORT(void) lpPopTable()
203 {
204 	if (luaTables.empty()) {
205 		currTable = rootTable;
206 		return;
207 	}
208 	const unsigned popSize = luaTables.size() - 1;
209 	currTable = luaTables[popSize];
210 	luaTables.resize(popSize);
211 }
212 
213 
214 /******************************************************************************/
215 //
216 //  Key existance
217 //
218 
lpGetKeyExistsInt(int key)219 EXPORT(int) lpGetKeyExistsInt(int key)
220 {
221 	return currTable.KeyExists(key) ? 1 : 0;
222 }
223 
224 
lpGetKeyExistsStr(const char * key)225 EXPORT(int) lpGetKeyExistsStr(const char* key)
226 {
227 	return currTable.KeyExists(key) ? 1 : 0;
228 }
229 
230 
231 /******************************************************************************/
232 //
233 //  Type
234 //
235 
lpGetIntKeyType(int key)236 EXPORT(int) lpGetIntKeyType(int key)
237 {
238 	return currTable.GetType(key);
239 }
240 
241 
lpGetStrKeyType(const char * key)242 EXPORT(int) lpGetStrKeyType(const char* key)
243 {
244 	return currTable.GetType(key);
245 }
246 
247 
248 /******************************************************************************/
249 //
250 // Key lists
251 //
252 
lpGetIntKeyListCount()253 EXPORT(int) lpGetIntKeyListCount()
254 {
255 	if (!currTable.IsValid()) {
256 		intKeys.clear();
257 		return 0;
258 	}
259 	intKeys.clear();
260 	currTable.GetKeys(intKeys);
261 	return (int)intKeys.size();
262 }
263 
264 
lpGetIntKeyListEntry(int index)265 EXPORT(int) lpGetIntKeyListEntry(int index)
266 {
267 	if ((index < 0) || (index >= (int)intKeys.size())) {
268 		return 0;
269 	}
270 	return intKeys[index];
271 }
272 
273 
lpGetStrKeyListCount()274 EXPORT(int) lpGetStrKeyListCount()
275 {
276 	if (!currTable.IsValid()) {
277 		strKeys.clear();
278 		return 0;
279 	}
280 	strKeys.clear();
281 	currTable.GetKeys(strKeys);
282 	return (int)strKeys.size();
283 }
284 
285 
lpGetStrKeyListEntry(int index)286 EXPORT(const char*) lpGetStrKeyListEntry(int index)
287 {
288 	if ((index < 0) || (index >= (int)strKeys.size())) {
289 		return GetStr("");
290 	}
291 	return GetStr(strKeys[index]);
292 }
293 
294 
295 /******************************************************************************/
296 //
297 //  Value queries
298 //
299 
lpGetIntKeyIntVal(int key,int defVal)300 EXPORT(int) lpGetIntKeyIntVal(int key, int defVal)
301 {
302 	return currTable.GetInt(key, defVal);
303 }
304 
305 
lpGetStrKeyIntVal(const char * key,int defVal)306 EXPORT(int) lpGetStrKeyIntVal(const char* key, int defVal)
307 {
308 	return currTable.GetInt(key, defVal);
309 }
310 
311 
lpGetIntKeyBoolVal(int key,int defVal)312 EXPORT(int) lpGetIntKeyBoolVal(int key, int defVal)
313 {
314 	return currTable.GetBool(key, defVal) ? 1 : 0;
315 }
316 
317 
lpGetStrKeyBoolVal(const char * key,int defVal)318 EXPORT(int) lpGetStrKeyBoolVal(const char* key, int defVal)
319 {
320 	return currTable.GetBool(key, defVal) ? 1 : 0;
321 }
322 
323 
lpGetIntKeyFloatVal(int key,float defVal)324 EXPORT(float) lpGetIntKeyFloatVal(int key, float defVal)
325 {
326 	return currTable.GetFloat(key, defVal);
327 }
328 
329 
lpGetStrKeyFloatVal(const char * key,float defVal)330 EXPORT(float) lpGetStrKeyFloatVal(const char* key, float defVal)
331 {
332 	return currTable.GetFloat(key, defVal);
333 }
334 
335 
lpGetIntKeyStrVal(int key,const char * defVal)336 EXPORT(const char*) lpGetIntKeyStrVal(int key, const char* defVal)
337 {
338 	return GetStr(currTable.GetString(key, defVal));
339 }
340 
341 
lpGetStrKeyStrVal(const char * key,const char * defVal)342 EXPORT(const char*) lpGetStrKeyStrVal(const char* key, const char* defVal)
343 {
344 	return GetStr(currTable.GetString(key, defVal));
345 }
346 
347 
348 /******************************************************************************/
349 /******************************************************************************/
350