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