Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/script_engine/src/lib/script_engine/script.cc @ 8680

Last change on this file since 8680 was 8680, checked in by snellen, 19 years ago

removed all testing traces

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