Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10292 was 10292, checked in by snellen, 18 years ago

patch that allows to organize the scripts in folders

File size: 10.2 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
96   int seperation =  filename.find_first_of('/');
97   std::string directory = filename.substr( seperation, 0 );
98   std::string file = filename.substr( seperation );
99
100   printf("Directory is %s \n", directory.c_str());
101   printf("File is %s \n", file.c_str());
102
103   std::string filedest(Resources::ResourceManager::getInstance()->mainGlobalPath().name());
104   filedest += "/scripts/" + directory + filename;
105
106   this->addThisScript();
107   this->registerStandartClasses();
108
109   if(currentFile.length() != 0)
110   {
111     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());
112     return false;
113    }
114
115   int error = luaL_loadfile (luaState, filedest.c_str());
116
117   if(error == 0)
118   {
119     currentFile = file;
120     error = lua_pcall(luaState, 0, 0, 0);
121
122     if(error == 0)
123     {
124      return true;
125     }
126     else
127     {
128       printf("SCRIPT %s : ERROR: while loading file %s: \n",currentFile.c_str(),filename.c_str());
129       reportError(error);
130     }
131
132   }
133   else
134   {
135     printf("SCRIPT %s : ERROR: while loading file %s: \n",currentFile.c_str(),filename.c_str());
136     reportError(error);
137   }
138
139   return false;
140 }
141
142
143 void Script::addObject(const std::string& className, const std::string& objectName)
144 {
145   //printf(("Script %s: I am about to add %s of class %s\n",this->getName(),objectName.c_str(),className.c_str());
146
147   ScriptClass* scriptClass = ScriptClass::objectList().getObject(className);
148  // printf(("The script class for %s is at %p \n",className.c_str(),scriptClass);
149   WorldObject tmpObj;
150   if (scriptClass != NULL)
151   {
152     tmpObj.type = className;
153     if( !classIsRegistered(className) )
154     {
155       scriptClass->registerClass(this);
156     }
157
158     BaseObject* object = ObjectListBase::getBaseObject(className, objectName);
159    // printf(("%s is at %p \n",objectName.c_str(),object);
160     if (object != NULL && !objectIsAdded(objectName))
161     {
162        scriptClass->insertObject(this, object, false);
163        tmpObj.name = objectName;
164        registeredObjects.push_back(tmpObj);
165     }
166   }
167 }
168
169
170
171
172 bool Script::executeFile()
173 {
174   PRINTF(2)("script.executeFile is not implemented yet\n");
175   /*if(currentFile.length() != 0)
176   {
177    int error = lua_pcall(luaState, 0, 0, 0);
178    if( error == 0)
179      return true;
180     else
181      {
182       reportError(error);
183       return false;
184      }
185 }*/
186   return false;
187 }
188
189 bool Script::selectFunction(const std::string& functionName, int retCount)
190 {
191   if(returnCount == 0 && currentFunction.length() == 0) //no return values left on the stack and no other function selected
192   {
193   lua_pushlstring(luaState, functionName.c_str(), functionName.size() );
194   lua_gettable(luaState, LUA_GLOBALSINDEX);
195
196   if(lua_isfunction( luaState , -1))
197   {
198     returnCount = retCount;
199     argumentCount = 0;
200     currentFunction = functionName;
201     return true;
202   }
203   else
204    return false;
205   }
206   else
207     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());
208   return false;
209 }
210
211 //return number of returned values
212 bool Script::executeFunction()
213 {
214   if(currentFunction.length() != 0 )
215   {
216    int error = lua_pcall(luaState, argumentCount, returnCount,0);
217    if(error != 0)
218    {
219      PRINTF(1)("Script '%s' : Failed to execute function '%s': \n",currentFile.c_str(),currentFunction.c_str());
220     reportError(error);
221     //clean up
222     currentFunction.assign("");
223     argumentCount = returnCount = 0;
224     return false;
225    }
226    else
227    {
228      currentFunction.assign("");//a function gets unusable after beeing called for the first time
229      argumentCount = 0;
230      return true;
231    }
232   }
233   else
234     PRINTF(1)("SCRIPT '%s' : no function selected.\n",currentFile.c_str());
235
236   return false;
237 }
238
239
240 //overload this function to add different types
241 bool Script::pushParam(int param, std::string& toFunction)
242 {
243   if(currentFunction == toFunction)
244   {
245     lua_pushnumber(luaState, (lua_Number) param);
246     argumentCount++;
247     return true;
248   }
249   else
250   {
251     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());
252    return false;
253   }
254
255 }
256
257
258 bool Script::pushParam(float param, std::string& toFunction)
259 {
260   if(currentFunction.compare(toFunction) == 0)
261   {
262     lua_pushnumber(luaState,(lua_Number) param);
263     argumentCount++;
264     return true;
265   }
266   else
267   {
268     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());
269     return false;
270   }
271
272 }
273
274 bool Script::pushParam(double param, std::string& toFunction)
275 {
276   if(currentFunction.compare(toFunction) == 0)
277   {
278     lua_pushnumber(luaState,(lua_Number) param);
279     argumentCount++;
280     return true;
281   }
282   else
283   {
284     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());
285     return false;
286   }
287
288 }
289
290 int Script::getReturnedInt()
291 {
292   int returnValue = 0;
293   if(returnCount > 0)
294   {
295     if(lua_isnumber(luaState, -1*returnCount))
296     {
297       returnValue = (int)lua_tonumber(luaState, -1*returnCount);
298       lua_remove(luaState,-1*returnCount);
299       returnCount--;
300
301     }
302   }
303   return returnValue;
304 }
305
306
307 bool Script::getReturnedBool()
308 {
309   bool returnValue = false;
310   if(returnCount > 0)
311   {
312     if(lua_isboolean(luaState, -1*returnCount))
313     {
314       returnValue = (bool)lua_toboolean(luaState, -1*returnCount);
315       lua_remove(luaState,-1*returnCount);
316       returnCount--;
317     }
318     else
319       printf("SCRIPT %s : ERROR: Trying to retreive non bolean value\n",this->currentFile.c_str());
320   }
321   return returnValue;
322 }
323
324float Script::getReturnedFloat()
325 {
326   float returnValue = 0.0f;
327   if(returnCount > 0)
328   {
329     if(lua_isnumber(luaState, -1*returnCount))
330     {
331       returnValue = (float)lua_tonumber(luaState, -1*returnCount);
332       lua_remove(luaState,-1*returnCount);
333       returnCount--;
334     }
335   }
336   return returnValue;
337 }
338
339 void Script::getReturnedString(std::string& string)
340 {
341   const char* returnValue = "";
342   if(returnCount > 0)
343   {
344     if(lua_isstring(luaState, -1*returnCount))
345     {
346       returnValue = lua_tostring(luaState, -1*returnCount);
347       lua_remove(luaState,-1*returnCount);
348       returnCount--;
349     }
350   }
351  string.assign(returnValue);
352 }
353
354
355void Script::addThisScript()
356{
357  ScriptClass* scriptClass = ScriptClass::objectList().getObject("Script");
358
359  if (scriptClass != NULL)
360   {
361     scriptClass->registerClass(this);
362     scriptClass->insertObject(this, this,"thisscript", false);
363   }
364}
365
366 int Script::reportError(int error)
367 {
368 if(error != 0)
369 {
370  const char *msg = lua_tostring(luaState, -1);
371  if (msg == NULL) msg = "(error with no message)\n";
372  printf("ERROR: %s\n", msg);
373  lua_pop(luaState, 1);
374 }
375  return error;
376 }
377
378 bool Script::registerStandartClasses()
379 {
380   bool success = false;
381
382   //this->registerClass(std::string("Vector"));
383    this->registerClass("ScriptTrigger");
384  //  this->registerClass("AttractorMine");
385
386   return success;
387 }
388
389
390 void Script::registerClass( const std::string& className)
391 {
392   ScriptClass* scriptClass = ScriptClass::objectList().getObject(className);
393   //printf(("The script class for %s is at %p \n",className.c_str(),scriptClass);
394
395   WorldObject tmpObj;
396   if (scriptClass != NULL)
397   {
398     tmpObj.type = className;
399     if( !classIsRegistered(className) )
400     {
401       static_cast<ScriptClass*>(scriptClass)->registerClass(this);
402       tmpObj.name = "";
403       registeredObjects.push_back(tmpObj);
404       return;
405     }
406   }
407
408 }
409
410 bool Script::classIsRegistered(const std::string& type)
411 {
412   for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ )
413   {
414     if( (*it).type == type)
415     {
416       return true;
417     }
418   }
419   return false;
420 }
421
422
423
424 bool Script::objectIsAdded(const std::string& name)
425 {
426   for(std::list<WorldObject>::const_iterator it = registeredObjects.begin(); it != registeredObjects.end(); it++ )
427   {
428     if( (*it).name == name)
429     {
430       return true;
431     }
432   }
433   return false;
434
435
436 }
Note: See TracBrowser for help on using the repository browser.