Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Continued working on script manager

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