Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/script_engine/script.cc @ 10209

Last change on this file since 10209 was 9916, checked in by bensch, 18 years ago

orxonox/trunk: Windows runs again without any segfaults
it seems, that windows links object files differently… and extremely strange

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