1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Silvan Nellen |
---|
13 | co-programmer: Benjamin Grauer |
---|
14 | */ |
---|
15 | |
---|
16 | #include "script.h" |
---|
17 | #include "script_class.h" |
---|
18 | #include "luaincl.h" |
---|
19 | #include "debug.h" |
---|
20 | #include "util/loading/resource_manager.h" |
---|
21 | |
---|
22 | #include "loading/load_param.h" |
---|
23 | #include "parser/tinyxml/tinyxml.h" |
---|
24 | |
---|
25 | CREATE_SCRIPTABLE_CLASS(Script, Script::classID(), |
---|
26 | addMethod("addObject", ExecutorLua2<Script,const std::string&, const std::string& >(&Script::addObject)) |
---|
27 | ->addMethod("registerClass", ExecutorLua1<Script,const std::string&>(&Script::registerClass)) |
---|
28 | ->addMethod("selectFunction", ExecutorLua2ret<Script, bool, const std::string&, int >(&Script::selectFunction)) |
---|
29 | ->addMethod("executeFunction", ExecutorLua0ret<Script,bool >(&Script::executeFunction)) |
---|
30 | ); |
---|
31 | |
---|
32 | NewObjectListDefinition(Script); |
---|
33 | |
---|
34 | Script::Script(const TiXmlElement* root) |
---|
35 | { |
---|
36 | this->registerObject(this, Script::_objectList); |
---|
37 | |
---|
38 | returnCount = argumentCount = 0; |
---|
39 | |
---|
40 | luaState = lua_open(); |
---|
41 | |
---|
42 | luaopen_base(luaState); |
---|
43 | luaopen_table(luaState); |
---|
44 | luaopen_io(luaState); |
---|
45 | luaopen_string(luaState); |
---|
46 | luaopen_math(luaState); |
---|
47 | luaopen_debug(luaState); |
---|
48 | if (root != NULL) |
---|
49 | this->loadParams(root); |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | Script::Script(const std::string& filename) |
---|
54 | { |
---|
55 | this->registerObject(this, Script::_objectList); |
---|
56 | |
---|
57 | returnCount = argumentCount = 0; |
---|
58 | |
---|
59 | luaState = lua_open(); |
---|
60 | |
---|
61 | luaopen_base(luaState); |
---|
62 | luaopen_table(luaState); |
---|
63 | luaopen_io(luaState); |
---|
64 | luaopen_string(luaState); |
---|
65 | luaopen_math(luaState); |
---|
66 | luaopen_debug(luaState); |
---|
67 | |
---|
68 | this->loadFile(filename); |
---|
69 | |
---|
70 | } |
---|
71 | |
---|
72 | Script::~Script() |
---|
73 | { |
---|
74 | lua_setgcthreshold(luaState, 0); // collected garbage |
---|
75 | lua_close(luaState); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | void Script::loadParams(const TiXmlElement* root) |
---|
80 | { |
---|
81 | //printf(("Loading params for %p \n",this); |
---|
82 | BaseObject::loadParams(root); |
---|
83 | |
---|
84 | LOAD_PARAM_START_CYCLE(root, object); |
---|
85 | { |
---|
86 | LoadParam_CYCLE(object, "object", this, Script, addObject) |
---|
87 | .describe("The name of an object that is needed by a script"); |
---|
88 | } |
---|
89 | LOAD_PARAM_END_CYCLE(object); |
---|
90 | |
---|
91 | |
---|
92 | LoadParam(root, "file", this, Script, loadFileNoRet) |
---|
93 | .describe("the fileName of the script, that should be loaded into this world") |
---|
94 | .defaultValues(""); |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | bool Script::loadFile(const std::string& filename) |
---|
100 | { |
---|
101 | std::string filedest(ResourceManager::getInstance()->getDataDir()); |
---|
102 | filedest += "scripts/" + filename; |
---|
103 | |
---|
104 | this->addThisScript(); |
---|
105 | this->registerStandartClasses(); |
---|
106 | |
---|
107 | if(currentFile.length() != 0) |
---|
108 | { |
---|
109 | printf("SCRIPT %s : ERROR: Could not load %s because an other file is already loaded: %s\n",currentFile.c_str(),filename.c_str(), currentFile.c_str()); |
---|
110 | return false; |
---|
111 | } |
---|
112 | |
---|
113 | int error = luaL_loadfile (luaState, filedest.c_str()); |
---|
114 | |
---|
115 | if(error == 0) |
---|
116 | { |
---|
117 | currentFile = filename; |
---|
118 | error = lua_pcall(luaState, 0, 0, 0); |
---|
119 | |
---|
120 | if(error == 0) |
---|
121 | { |
---|
122 | return true; |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | printf("SCRIPT %s : ERROR: while loading file %s: \n",currentFile.c_str(),filename.c_str()); |
---|
127 | reportError(error); |
---|
128 | } |
---|
129 | |
---|
130 | } |
---|
131 | else |
---|
132 | { |
---|
133 | printf("SCRIPT %s : ERROR: while loading file %s: \n",currentFile.c_str(),filename.c_str()); |
---|
134 | reportError(error); |
---|
135 | } |
---|
136 | |
---|
137 | return false; |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | void Script::addObject(const std::string& className, const std::string& objectName) |
---|
142 | { |
---|
143 | //printf(("Script %s: I am about to add %s of class %s\n",this->getName(),objectName.c_str(),className.c_str()); |
---|
144 | |
---|
145 | ScriptClass* scriptClass = ScriptClass::objectList().getObject(className); |
---|
146 | // printf(("The script class for %s is at %p \n",className.c_str(),scriptClass); |
---|
147 | WorldObject tmpObj; |
---|
148 | if (scriptClass != NULL) |
---|
149 | { |
---|
150 | tmpObj.type = className; |
---|
151 | if( !classIsRegistered(className) ) |
---|
152 | { |
---|
153 | scriptClass->registerClass(this); |
---|
154 | } |
---|
155 | |
---|
156 | BaseObject* object = NewObjectListBase::getBaseObject(objectName, className); |
---|
157 | // printf(("%s is at %p \n",objectName.c_str(),object); |
---|
158 | if (object != NULL && !objectIsAdded(objectName)) |
---|
159 | { |
---|
160 | scriptClass->insertObject(this, object, false); |
---|
161 | tmpObj.name = objectName; |
---|
162 | registeredObjects.push_back(tmpObj); |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | bool Script::executeFile() |
---|
171 | { |
---|
172 | PRINT(2)("WARNING: script.executeFile is not implemented yet\n"); |
---|
173 | /*if(currentFile.length() != 0) |
---|
174 | { |
---|
175 | int error = lua_pcall(luaState, 0, 0, 0); |
---|
176 | if( error == 0) |
---|
177 | return true; |
---|
178 | else |
---|
179 | { |
---|
180 | reportError(error); |
---|
181 | return false; |
---|
182 | } |
---|
183 | }*/ |
---|
184 | return false; |
---|
185 | } |
---|
186 | |
---|
187 | bool Script::selectFunction(const std::string& functionName, int retCount) |
---|
188 | { |
---|
189 | if(returnCount == 0 && currentFunction.length() == 0) //no return values left on the stack and no other function selected |
---|
190 | { |
---|
191 | lua_pushlstring(luaState, functionName.c_str(), functionName.size() ); |
---|
192 | lua_gettable(luaState, LUA_GLOBALSINDEX); |
---|
193 | |
---|
194 | if(lua_isfunction( luaState , -1)) |
---|
195 | { |
---|
196 | returnCount = retCount; |
---|
197 | argumentCount = 0; |
---|
198 | currentFunction = functionName; |
---|
199 | return true; |
---|
200 | } |
---|
201 | else |
---|
202 | return false; |
---|
203 | } |
---|
204 | else |
---|
205 | printf("SCRIPT %s : ERROR: There is an other function active ( %s ) or there are unremoved return values on the stack. Please remove them first.\n",currentFile.c_str(),currentFunction.c_str()); |
---|
206 | return false; |
---|
207 | } |
---|
208 | |
---|
209 | //return number of returned values |
---|
210 | bool Script::executeFunction() |
---|
211 | { |
---|
212 | if(currentFunction.length() != 0 ) |
---|
213 | { |
---|
214 | int error = lua_pcall(luaState, argumentCount, returnCount,0); |
---|
215 | if(error != 0) |
---|
216 | { |
---|
217 | printf("SCRIPT %s : ERROR: Failed to execute function %s: \n",currentFile.c_str(),currentFunction.c_str()); |
---|
218 | reportError(error); |
---|
219 | //clean up |
---|
220 | currentFunction.assign(""); |
---|
221 | argumentCount = returnCount = 0; |
---|
222 | return false; |
---|
223 | } |
---|
224 | else |
---|
225 | { |
---|
226 | currentFunction.assign("");//a function gets unusable after beeing called for the first time |
---|
227 | argumentCount = 0; |
---|
228 | return true; |
---|
229 | } |
---|
230 | } |
---|
231 | else |
---|
232 | printf("SCRIPT %s : ERROR: no function selected.\n",currentFile.c_str()); |
---|
233 | |
---|
234 | return false; |
---|
235 | } |
---|
236 | |
---|
237 | |
---|
238 | //overload this function to add different types |
---|
239 | bool Script::pushParam(int param, std::string& toFunction) |
---|
240 | { |
---|
241 | if(currentFunction == toFunction) |
---|
242 | { |
---|
243 | lua_pushnumber(luaState, (lua_Number) param); |
---|
244 | argumentCount++; |
---|
245 | return true; |
---|
246 | } |
---|
247 | else |
---|
248 | { |
---|
249 | printf("SCRIPT %s : ERROR: Couldn't add parameter because the wrong function is selected: %s instead of %s\n",currentFile.c_str(), currentFunction.c_str(), toFunction.c_str()); |
---|
250 | return false; |
---|
251 | } |
---|
252 | |
---|
253 | } |
---|
254 | |
---|
255 | |
---|
256 | bool Script::pushParam(float param, std::string& toFunction) |
---|
257 | { |
---|
258 | if(currentFunction.compare(toFunction) == 0) |
---|
259 | { |
---|
260 | lua_pushnumber(luaState,(lua_Number) param); |
---|
261 | argumentCount++; |
---|
262 | return true; |
---|
263 | } |
---|
264 | else |
---|
265 | { |
---|
266 | printf("SCRIPT %s : ERROR: Couldn't add parameter because the wrong function is selected: %s instead of %s\n",currentFile.c_str(), currentFunction.c_str(), toFunction.c_str()); |
---|
267 | return false; |
---|
268 | } |
---|
269 | |
---|
270 | } |
---|
271 | |
---|
272 | bool Script::pushParam(double param, std::string& toFunction) |
---|
273 | { |
---|
274 | if(currentFunction.compare(toFunction) == 0) |
---|
275 | { |
---|
276 | lua_pushnumber(luaState,(lua_Number) param); |
---|
277 | argumentCount++; |
---|
278 | return true; |
---|
279 | } |
---|
280 | else |
---|
281 | { |
---|
282 | printf("SCRIPT %s : ERROR: Couldn't add parameter because the wrong function is selected: %s instead of %s\n",currentFile.c_str(), currentFunction.c_str(), toFunction.c_str()); |
---|
283 | return false; |
---|
284 | } |
---|
285 | |
---|
286 | } |
---|
287 | |
---|
288 | int Script::getReturnedInt() |
---|
289 | { |
---|
290 | int returnValue = 0; |
---|
291 | if(returnCount > 0) |
---|
292 | { |
---|
293 | if(lua_isnumber(luaState, -1*returnCount)) |
---|
294 | { |
---|
295 | returnValue = (int)lua_tonumber(luaState, -1*returnCount); |
---|
296 | lua_remove(luaState,-1*returnCount); |
---|
297 | returnCount--; |
---|
298 | |
---|
299 | } |
---|
300 | } |
---|
301 | return returnValue; |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | bool Script::getReturnedBool() |
---|
306 | { |
---|
307 | bool returnValue = false; |
---|
308 | if(returnCount > 0) |
---|
309 | { |
---|
310 | if(lua_isboolean(luaState, -1*returnCount)) |
---|
311 | { |
---|
312 | returnValue = (bool)lua_toboolean(luaState, -1*returnCount); |
---|
313 | lua_remove(luaState,-1*returnCount); |
---|
314 | returnCount--; |
---|
315 | } |
---|
316 | else |
---|
317 | printf("SCRIPT %s : ERROR: Trying to retreive non bolean value\n",this->currentFile.c_str()); |
---|
318 | } |
---|
319 | return returnValue; |
---|
320 | } |
---|
321 | |
---|
322 | float Script::getReturnedFloat() |
---|
323 | { |
---|
324 | float returnValue = 0.0f; |
---|
325 | if(returnCount > 0) |
---|
326 | { |
---|
327 | if(lua_isnumber(luaState, -1*returnCount)) |
---|
328 | { |
---|
329 | returnValue = (float)lua_tonumber(luaState, -1*returnCount); |
---|
330 | lua_remove(luaState,-1*returnCount); |
---|
331 | returnCount--; |
---|
332 | } |
---|
333 | } |
---|
334 | return returnValue; |
---|
335 | } |
---|
336 | |
---|
337 | void Script::getReturnedString(std::string& string) |
---|
338 | { |
---|
339 | const char* returnValue = ""; |
---|
340 | if(returnCount > 0) |
---|
341 | { |
---|
342 | if(lua_isstring(luaState, -1*returnCount)) |
---|
343 | { |
---|
344 | returnValue = lua_tostring(luaState, -1*returnCount); |
---|
345 | lua_remove(luaState,-1*returnCount); |
---|
346 | returnCount--; |
---|
347 | } |
---|
348 | } |
---|
349 | string.assign(returnValue); |
---|
350 | } |
---|
351 | |
---|
352 | |
---|
353 | void Script::addThisScript() |
---|
354 | { |
---|
355 | ScriptClass* scriptClass = ScriptClass::objectList().getObject("Script"); |
---|
356 | |
---|
357 | if (scriptClass != NULL) |
---|
358 | { |
---|
359 | scriptClass->registerClass(this); |
---|
360 | scriptClass->insertObject(this, this,"thisscript", false); |
---|
361 | } |
---|
362 | } |
---|
363 | |
---|
364 | int Script::reportError(int error) |
---|
365 | { |
---|
366 | if(error != 0) |
---|
367 | { |
---|
368 | const char *msg = lua_tostring(luaState, -1); |
---|
369 | if (msg == NULL) msg = "(error with no message)\n"; |
---|
370 | printf("ERROR: %s\n", msg); |
---|
371 | lua_pop(luaState, 1); |
---|
372 | } |
---|
373 | return error; |
---|
374 | } |
---|
375 | |
---|
376 | bool Script::registerStandartClasses() |
---|
377 | { |
---|
378 | bool success = false; |
---|
379 | |
---|
380 | //this->registerClass(std::string("Vector")); |
---|
381 | this->registerClass("ScriptTrigger"); |
---|
382 | // this->registerClass("AttractorMine"); |
---|
383 | |
---|
384 | return success; |
---|
385 | } |
---|
386 | |
---|
387 | |
---|
388 | void Script::registerClass( const std::string& className) |
---|
389 | { |
---|
390 | ScriptClass* scriptClass = ScriptClass::objectList().getObject(className); |
---|
391 | //printf(("The script class for %s is at %p \n",className.c_str(),scriptClass); |
---|
392 | |
---|
393 | WorldObject tmpObj; |
---|
394 | if (scriptClass != NULL) |
---|
395 | { |
---|
396 | tmpObj.type = className; |
---|
397 | if( !classIsRegistered(className) ) |
---|
398 | { |
---|
399 | static_cast<ScriptClass*>(scriptClass)->registerClass(this); |
---|
400 | tmpObj.name = ""; |
---|
401 | registeredObjects.push_back(tmpObj); |
---|
402 | return; |
---|
403 | } |
---|
404 | } |
---|
405 | |
---|
406 | } |
---|
407 | |
---|
408 | bool Script::classIsRegistered(const std::string& type) |
---|
409 | { |
---|
410 | for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ ) |
---|
411 | { |
---|
412 | if( (*it).type == type) |
---|
413 | { |
---|
414 | return true; |
---|
415 | } |
---|
416 | } |
---|
417 | return false; |
---|
418 | } |
---|
419 | |
---|
420 | |
---|
421 | |
---|
422 | bool Script::objectIsAdded(const std::string& name) |
---|
423 | { |
---|
424 | for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ ) |
---|
425 | { |
---|
426 | if( (*it).name == name) |
---|
427 | { |
---|
428 | return true; |
---|
429 | } |
---|
430 | } |
---|
431 | return false; |
---|
432 | |
---|
433 | |
---|
434 | } |
---|