Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

script_engine: swapping to integrate into the ORXONOX framework

File size: 5.6 KB
Line 
1#include "script.h"
2#include "loading/load_param.h"
3
4
5Script::Script(const TiXmlElement* root)
6{
7  this->setClassID(CL_SCRIPT, "Script");
8 
9  returnCount = argumentCount = 0;
10
11  luaState = lua_open();
12
13  luaopen_base(luaState);
14  luaopen_table(luaState);
15  luaopen_io(luaState);
16  luaopen_string(luaState);
17  luaopen_math(luaState);
18  luaopen_debug(luaState);
19 
20  if (root != NULL)
21    this->loadParams(root);
22
23}
24
25
26Script::~Script()
27{
28  lua_setgcthreshold(luaState, 0);  // collected garbage
29  lua_close(luaState);
30}
31
32
33void Script::loadParams(const TiXmlElement* root)
34{
35  LOAD_PARAM_START_CYCLE(root, objectElement);
36  {
37    LoadParam_CYCLE(objectElement, "object", this, Script, addObject)
38        .describe("The name of an object that is needed by a script");
39  }
40  LOAD_PARAM_END_CYCLE(objectElement);
41
42
43  LoadParam(root, "file", this, Script, loadFileNoRet)
44      .describe("the fileName of the script, that should be loaded into this world")
45      .defaultValues("");
46}
47
48
49
50bool Script::loadFile(const std::string& filename)
51 {
52
53   if(currentFile.length() != 0)
54   {
55     printf("Could not load %s because an other file is already loaded: %s\n",filename.c_str(), currentFile.c_str());
56     return false;
57    }
58
59   int error = luaL_loadfile (luaState, filename.c_str());
60
61   if(error == 0)
62   {
63     error = lua_pcall(luaState, 0, 0, 0);
64
65     if(error == 0)
66     {
67      currentFile = filename;
68      return true;
69     }
70     else
71     {
72      reportError(error);
73     }
74
75   }
76   else
77   {
78     reportError(error);
79   }
80
81   return false;
82 }
83
84 
85 void Script::addObject(const std::string& className, const std::string& objectName)
86 {
87   
88 }
89
90 
91 
92 
93 bool Script::executeFile()
94 {
95   if(currentFile.length() != 0)
96   {
97    int error = lua_pcall(luaState, 0, 0, 0);
98    if( error == 0)
99      return true;
100     else
101      {
102       reportError(error);
103       return false;
104      }
105   }
106   return false;
107 }
108
109 bool Script::selectFunction(std::string& functionName, int retCount)
110 {
111   if(returnCount == 0 && currentFunction.length() == 0) //no return values left on the stack and no other function selected
112   {
113   lua_pushlstring(luaState, functionName.c_str(), functionName.size() );
114   lua_gettable(luaState, LUA_GLOBALSINDEX);
115
116   if(lua_isfunction( luaState , -1))
117   {
118     returnCount = retCount;
119     argumentCount = 0;
120     currentFunction = functionName;
121     return true;
122   }
123   else
124    return false;
125   }
126   else
127     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());
128   return false;
129 }
130
131 //return number of returned values
132 bool Script::executeFunction()
133 {
134   if(currentFunction.length() != 0 )
135   {
136    int error = lua_pcall(luaState, argumentCount, returnCount,0);
137    if(error != 0)
138    {
139     reportError(error);
140     return false;
141    }
142    else
143    {
144      currentFunction.assign("");//a function gets unusable after beeing called for the first time
145      argumentCount = 0;
146      return true;
147    }
148   }
149   else
150     printf("Error: no function selected.\n");
151 }
152
153
154 //overload this function to add different types
155 bool Script::pushParam(int param, std::string& toFunction)
156 {
157   if(currentFunction.compare(toFunction) == 0)
158   {
159     lua_pushnumber(luaState, (lua_Number) param);
160     argumentCount++;
161    return true;
162   }
163   else
164   {
165    printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str());
166    return false;
167   }
168
169 }
170
171
172 bool Script::pushParam(float param, std::string& toFunction)
173 {
174   if(currentFunction.compare(toFunction) == 0)
175   {
176     lua_pushnumber(luaState,(lua_Number) param);
177     argumentCount++;
178     return true;
179   }
180   else
181   {
182     printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str());
183     return false;
184   }
185
186 }
187
188 bool Script::pushParam(double param, std::string& toFunction)
189 {
190   if(currentFunction.compare(toFunction) == 0)
191   {
192     lua_pushnumber(luaState,(lua_Number) param);
193     argumentCount++;
194     return true;
195   }
196   else
197   {
198     printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str());
199     return false;
200   }
201
202 }
203
204 int Script::getReturnedInt()
205 {
206   int returnValue;
207   if(returnCount > 0)
208   {
209     if(lua_isnumber(luaState, -1))
210     {
211       returnValue = (int)lua_tonumber(luaState, -1);
212       returnCount--;
213       lua_pop(luaState,1);
214     }
215   }
216   return returnValue;
217 }
218
219
220 bool Script::getReturnedBool()
221 {
222   bool returnValue;
223   if(returnCount > 0)
224   {
225     if(lua_isboolean(luaState, -1))
226     {
227       returnValue = (bool)lua_toboolean(luaState, -1);
228       returnCount--;
229       lua_pop(luaState,1);
230     }
231   }
232   return returnValue;
233 }
234
235float Script::getReturnedFloat()
236 {
237   float returnValue;
238   if(returnCount > 0)
239   {
240     if(lua_isnumber(luaState, -1))
241     {
242       returnValue = (float)lua_tonumber(luaState, -1);
243       returnCount--;
244       lua_pop(luaState,1);
245     }
246   }
247   return returnValue;
248 }
249
250 void Script::getReturnedString(std::string& string)
251 {
252   const char* returnValue;
253   if(returnCount > 0)
254   {
255     if(lua_isstring(luaState, -1))
256     {
257       returnValue = lua_tostring(luaState, -1);
258       returnCount--;
259       lua_pop(luaState,1);
260     }
261   }
262  string.assign(returnValue);
263 }
264
265 int Script::reportError(int error)
266 {
267 if(error != 0)
268 {
269  const char *msg = lua_tostring(luaState, -1);
270  if (msg == NULL) msg = "(error with no message)";
271  fprintf(stderr, "ERROR: %s\n", msg);
272  lua_pop(luaState, 1);
273  return error;
274 }
275 }
276
Note: See TracBrowser for help on using the repository browser.