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 | if (scriptClass != NULL) |
---|
96 | { |
---|
97 | static_cast<ScriptClass*>(scriptClass)->registerClass(this); |
---|
98 | |
---|
99 | BaseObject* object = ClassList::getObject(objectName, className); |
---|
100 | if (object != NULL) |
---|
101 | { |
---|
102 | static_cast<ScriptClass*>(scriptClass)->insertObject(this, object, false); |
---|
103 | } |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | |
---|
109 | |
---|
110 | bool Script::executeFile() |
---|
111 | { |
---|
112 | if(currentFile.length() != 0) |
---|
113 | { |
---|
114 | int error = lua_pcall(luaState, 0, 0, 0); |
---|
115 | if( error == 0) |
---|
116 | return true; |
---|
117 | else |
---|
118 | { |
---|
119 | reportError(error); |
---|
120 | return false; |
---|
121 | } |
---|
122 | } |
---|
123 | return false; |
---|
124 | } |
---|
125 | |
---|
126 | bool Script::selectFunction(std::string& functionName, int retCount) |
---|
127 | { |
---|
128 | if(returnCount == 0 && currentFunction.length() == 0) //no return values left on the stack and no other function selected |
---|
129 | { |
---|
130 | lua_pushlstring(luaState, functionName.c_str(), functionName.size() ); |
---|
131 | lua_gettable(luaState, LUA_GLOBALSINDEX); |
---|
132 | |
---|
133 | if(lua_isfunction( luaState , -1)) |
---|
134 | { |
---|
135 | returnCount = retCount; |
---|
136 | argumentCount = 0; |
---|
137 | currentFunction = functionName; |
---|
138 | return true; |
---|
139 | } |
---|
140 | else |
---|
141 | return false; |
---|
142 | } |
---|
143 | else |
---|
144 | 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()); |
---|
145 | return false; |
---|
146 | } |
---|
147 | |
---|
148 | //return number of returned values |
---|
149 | bool Script::executeFunction() |
---|
150 | { |
---|
151 | if(currentFunction.length() != 0 ) |
---|
152 | { |
---|
153 | int error = lua_pcall(luaState, argumentCount, returnCount,0); |
---|
154 | if(error != 0) |
---|
155 | { |
---|
156 | reportError(error); |
---|
157 | return false; |
---|
158 | } |
---|
159 | else |
---|
160 | { |
---|
161 | currentFunction.assign("");//a function gets unusable after beeing called for the first time |
---|
162 | argumentCount = 0; |
---|
163 | return true; |
---|
164 | } |
---|
165 | } |
---|
166 | else |
---|
167 | printf("Error: no function selected.\n"); |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | //overload this function to add different types |
---|
172 | bool Script::pushParam(int 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 | |
---|
189 | bool Script::pushParam(float param, std::string& toFunction) |
---|
190 | { |
---|
191 | if(currentFunction.compare(toFunction) == 0) |
---|
192 | { |
---|
193 | lua_pushnumber(luaState,(lua_Number) param); |
---|
194 | argumentCount++; |
---|
195 | return true; |
---|
196 | } |
---|
197 | else |
---|
198 | { |
---|
199 | printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); |
---|
200 | return false; |
---|
201 | } |
---|
202 | |
---|
203 | } |
---|
204 | |
---|
205 | bool Script::pushParam(double param, std::string& toFunction) |
---|
206 | { |
---|
207 | if(currentFunction.compare(toFunction) == 0) |
---|
208 | { |
---|
209 | lua_pushnumber(luaState,(lua_Number) param); |
---|
210 | argumentCount++; |
---|
211 | return true; |
---|
212 | } |
---|
213 | else |
---|
214 | { |
---|
215 | printf("Couldn't add parameter because the wrong function is selected: %s instead of %s\n", currentFunction.c_str(), toFunction.c_str()); |
---|
216 | return false; |
---|
217 | } |
---|
218 | |
---|
219 | } |
---|
220 | |
---|
221 | int Script::getReturnedInt() |
---|
222 | { |
---|
223 | int returnValue; |
---|
224 | if(returnCount > 0) |
---|
225 | { |
---|
226 | if(lua_isnumber(luaState, -1)) |
---|
227 | { |
---|
228 | returnValue = (int)lua_tonumber(luaState, -1); |
---|
229 | returnCount--; |
---|
230 | lua_pop(luaState,1); |
---|
231 | } |
---|
232 | } |
---|
233 | return returnValue; |
---|
234 | } |
---|
235 | |
---|
236 | |
---|
237 | bool Script::getReturnedBool() |
---|
238 | { |
---|
239 | bool returnValue; |
---|
240 | if(returnCount > 0) |
---|
241 | { |
---|
242 | if(lua_isboolean(luaState, -1)) |
---|
243 | { |
---|
244 | returnValue = (bool)lua_toboolean(luaState, -1); |
---|
245 | returnCount--; |
---|
246 | lua_pop(luaState,1); |
---|
247 | } |
---|
248 | } |
---|
249 | return returnValue; |
---|
250 | } |
---|
251 | |
---|
252 | float Script::getReturnedFloat() |
---|
253 | { |
---|
254 | float returnValue; |
---|
255 | if(returnCount > 0) |
---|
256 | { |
---|
257 | if(lua_isnumber(luaState, -1)) |
---|
258 | { |
---|
259 | returnValue = (float)lua_tonumber(luaState, -1); |
---|
260 | returnCount--; |
---|
261 | lua_pop(luaState,1); |
---|
262 | } |
---|
263 | } |
---|
264 | return returnValue; |
---|
265 | } |
---|
266 | |
---|
267 | void Script::getReturnedString(std::string& string) |
---|
268 | { |
---|
269 | const char* returnValue; |
---|
270 | if(returnCount > 0) |
---|
271 | { |
---|
272 | if(lua_isstring(luaState, -1)) |
---|
273 | { |
---|
274 | returnValue = lua_tostring(luaState, -1); |
---|
275 | returnCount--; |
---|
276 | lua_pop(luaState,1); |
---|
277 | } |
---|
278 | } |
---|
279 | string.assign(returnValue); |
---|
280 | } |
---|
281 | |
---|
282 | int Script::reportError(int error) |
---|
283 | { |
---|
284 | if(error != 0) |
---|
285 | { |
---|
286 | const char *msg = lua_tostring(luaState, -1); |
---|
287 | if (msg == NULL) msg = "(error with no message)"; |
---|
288 | fprintf(stderr, "ERROR: %s\n", msg); |
---|
289 | lua_pop(luaState, 1); |
---|
290 | return error; |
---|
291 | } |
---|
292 | } |
---|
293 | |
---|