1 | #include <cassert> |
---|
2 | #include <string> |
---|
3 | #include <iostream> |
---|
4 | |
---|
5 | #include <map> |
---|
6 | #include <list> |
---|
7 | |
---|
8 | #include "luaincl.h" |
---|
9 | #include "Script.h" |
---|
10 | #include "LuaCallback.h" |
---|
11 | #include "scriptable.h" |
---|
12 | |
---|
13 | // --------------------------------------------------------------------------- |
---|
14 | namespace OrxScript |
---|
15 | { |
---|
16 | /** |
---|
17 | * @brief Constructor |
---|
18 | * |
---|
19 | */ |
---|
20 | Scriptable::Scriptable () |
---|
21 | { |
---|
22 | } |
---|
23 | |
---|
24 | /** |
---|
25 | * @brief Deconstructor |
---|
26 | * |
---|
27 | * The Deconstructor tells all the scripts, that it is part of, that it is deleted. |
---|
28 | * |
---|
29 | */ |
---|
30 | Scriptable::~Scriptable (void) |
---|
31 | { |
---|
32 | std::list<Script>::iterator it; |
---|
33 | for(it = scriptList.begin();it != scriptList.end(); it++) |
---|
34 | { |
---|
35 | (*it).script->removeFromScript((*it).thisReference); |
---|
36 | } |
---|
37 | |
---|
38 | } |
---|
39 | |
---|
40 | |
---|
41 | //Method indexing check |
---|
42 | int Scriptable::methods (LuaVirtualMachine& virtualMachine) |
---|
43 | { |
---|
44 | LuaScript* script =(getScriptByVirtualMachine(virtualMachine))->script; |
---|
45 | if(script) |
---|
46 | { |
---|
47 | int lastMethod = getLastMethodIndexByPointer(script); |
---|
48 | if(lastMethod != -1) |
---|
49 | return lastMethod; |
---|
50 | } |
---|
51 | |
---|
52 | return -1; |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | /** |
---|
57 | * @brief Tells the scriptable that it got added to a script |
---|
58 | * @param toScript a pointer to the script the object got added |
---|
59 | * @param |
---|
60 | * @param reference a reference to the scriptable in that partilular script |
---|
61 | * |
---|
62 | * The scriptable will register all its functions to the table at "reference" |
---|
63 | * with the script. |
---|
64 | * |
---|
65 | */ |
---|
66 | |
---|
67 | bool Scriptable::scriptableAdded(LuaScript* toScript, int toScriptRef, int reference) |
---|
68 | { |
---|
69 | |
---|
70 | bool success = true ; |
---|
71 | if(!scriptIsInScriptList(toScript))// look if the scriptable isn't already added. |
---|
72 | if(toScript) |
---|
73 | { |
---|
74 | Script newScript; |
---|
75 | newScript.script = toScript; |
---|
76 | newScript.scriptReference = toScriptRef; |
---|
77 | newScript.thisReference = reference; |
---|
78 | newScript.lastMethodIndex = -1; |
---|
79 | newScript.methodBase = -1; |
---|
80 | |
---|
81 | scriptList.push_back(newScript); |
---|
82 | |
---|
83 | int methodIndex; |
---|
84 | Script* tmpScript = getScriptByPointer(toScript); |
---|
85 | |
---|
86 | //add all the functions to the script |
---|
87 | std::list<std::string>::const_iterator it; |
---|
88 | |
---|
89 | for(it = functionList.begin();it != functionList.end(); it++) |
---|
90 | { |
---|
91 | |
---|
92 | if(tmpScript) |
---|
93 | { |
---|
94 | methodIndex = toScript->addFunctionToScriptable(*it, tmpScript->thisReference, tmpScript->lastMethodIndex); |
---|
95 | |
---|
96 | |
---|
97 | if(newScript.methodBase = -1) |
---|
98 | { |
---|
99 | if(methodIndex != -1) |
---|
100 | { |
---|
101 | std::cout<<"methodIndex is "<<methodIndex<<std::endl; |
---|
102 | tmpScript->methodBase= methodIndex; |
---|
103 | tmpScript->lastMethodIndex= methodIndex; |
---|
104 | tmpScript->functionMap[newScript.methodBase] = *it; |
---|
105 | } |
---|
106 | else{success = false; break;} |
---|
107 | } |
---|
108 | |
---|
109 | else if(methodIndex != -1) |
---|
110 | { |
---|
111 | |
---|
112 | tmpScript->lastMethodIndex = methodIndex; |
---|
113 | tmpScript->functionMap[newScript.lastMethodIndex] = *it; |
---|
114 | } |
---|
115 | else{success= false;break;} |
---|
116 | |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | } |
---|
123 | else |
---|
124 | { |
---|
125 | success = false; |
---|
126 | } |
---|
127 | |
---|
128 | return success; |
---|
129 | |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | |
---|
134 | /** |
---|
135 | * @brief Register a function with the scriptable |
---|
136 | * @param functionName function name |
---|
137 | * |
---|
138 | * |
---|
139 | */ |
---|
140 | |
---|
141 | void Scriptable::registerFunction(std::string functionName) |
---|
142 | { |
---|
143 | //check whether the function is already registered |
---|
144 | std::list<std::string>::iterator it; |
---|
145 | for(it = functionList.begin();it != functionList.end(); it++) |
---|
146 | { |
---|
147 | if((*it).compare(functionName) == 0) |
---|
148 | { |
---|
149 | break; |
---|
150 | } |
---|
151 | |
---|
152 | } |
---|
153 | |
---|
154 | if(it == functionList.end()) // if the functoin wasn't found |
---|
155 | functionList.push_back(functionName); |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | /** |
---|
160 | * @brief Get the function name of the function at index |
---|
161 | * @param index |
---|
162 | * |
---|
163 | * @return the function name on success. |
---|
164 | * |
---|
165 | * This function is used in the ScriptCalling function |
---|
166 | */ |
---|
167 | std::string Scriptable::getFunctionAtIndex(int index, LuaVirtualMachine& virtualMachine) |
---|
168 | { |
---|
169 | lua_State * luaState = (lua_State* ) virtualMachine; |
---|
170 | |
---|
171 | if(virtualMachine.isOk()) |
---|
172 | { |
---|
173 | lua_getglobal (luaState, "this"); |
---|
174 | |
---|
175 | if (lua_istable (luaState, 1)) |
---|
176 | { |
---|
177 | // Found the "this" table. The object pointer is at the index 0 |
---|
178 | lua_rawgeti (luaState, 1, 0); |
---|
179 | |
---|
180 | if (lua_islightuserdata (luaState, -1)) |
---|
181 | { |
---|
182 | // Found the pointer, need to cast it |
---|
183 | LuaScript* thisPointer = (LuaScript *) lua_touserdata (luaState, -1); |
---|
184 | |
---|
185 | if(thisPointer != NULL) |
---|
186 | { |
---|
187 | Script* script = getScriptByPointer(thisPointer); |
---|
188 | if( script != NULL) |
---|
189 | { |
---|
190 | std::map<int, std::string>::const_iterator it = script->functionMap.find(index); |
---|
191 | if(it != script->functionMap.end())//if found |
---|
192 | return script->functionMap[index]; |
---|
193 | } |
---|
194 | |
---|
195 | } |
---|
196 | } |
---|
197 | } |
---|
198 | } |
---|
199 | return ""; |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | /** |
---|
204 | * @brief Gets the lastMethod index associated with the LuaScript |
---|
205 | * @param the luaScript |
---|
206 | * |
---|
207 | * @return A lua reference to the last function of the Scriptable |
---|
208 | * |
---|
209 | * |
---|
210 | */ |
---|
211 | |
---|
212 | int Scriptable::getLastMethodIndexByPointer(LuaScript* luaScript) |
---|
213 | { |
---|
214 | if( luaScript ) |
---|
215 | { |
---|
216 | Script* script = (getScriptByPointer(luaScript)); |
---|
217 | if(script) |
---|
218 | { |
---|
219 | return (script->lastMethodIndex); |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | return -1; |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | /** |
---|
228 | * @brief Gets the functionMap Associated with a LuaScript |
---|
229 | * @param the LuaScript |
---|
230 | * |
---|
231 | * @return A pointer to the functionMap, NULL on failure |
---|
232 | * |
---|
233 | * |
---|
234 | */ |
---|
235 | |
---|
236 | std::map<int, std::string>* Scriptable::getFunctionMapByPointer(LuaScript* scriptPointer) |
---|
237 | { |
---|
238 | bool notFound = true; |
---|
239 | if(scriptPointer) |
---|
240 | { |
---|
241 | std::list<Script>::iterator it = scriptList.begin(); |
---|
242 | |
---|
243 | |
---|
244 | while(notFound && it !=scriptList.end() ) |
---|
245 | { |
---|
246 | if((*it).script == scriptPointer) |
---|
247 | { |
---|
248 | notFound = false; |
---|
249 | return &(it->functionMap); |
---|
250 | } |
---|
251 | it++; |
---|
252 | } |
---|
253 | } |
---|
254 | |
---|
255 | if(notFound) |
---|
256 | return NULL; |
---|
257 | |
---|
258 | } |
---|
259 | |
---|
260 | |
---|
261 | /** |
---|
262 | * @brief Gets the internal representation of a LuaScript by its pointer. |
---|
263 | * @param script the LuaScript |
---|
264 | * |
---|
265 | * @return returns the script if it was found, NULL else. |
---|
266 | * |
---|
267 | * |
---|
268 | */ |
---|
269 | |
---|
270 | Script* Scriptable::getScriptByPointer(LuaScript* script) |
---|
271 | { |
---|
272 | bool notFound = true; |
---|
273 | |
---|
274 | if(script) |
---|
275 | { |
---|
276 | std::list<Script>::iterator it = scriptList.begin(); |
---|
277 | |
---|
278 | while(notFound && it != scriptList.end() ) |
---|
279 | { |
---|
280 | if((*it).script == script) |
---|
281 | { |
---|
282 | notFound = false; |
---|
283 | return &(*it); |
---|
284 | } |
---|
285 | it++; |
---|
286 | } |
---|
287 | |
---|
288 | } |
---|
289 | if(notFound) |
---|
290 | return NULL; |
---|
291 | |
---|
292 | } |
---|
293 | |
---|
294 | |
---|
295 | /** |
---|
296 | * @brief Extracts the Script out of the virtual machine |
---|
297 | * @param virtualMachine the virtualMachine to search for the script |
---|
298 | * |
---|
299 | * @return The Script. If there was an error it returns NULL |
---|
300 | * |
---|
301 | * |
---|
302 | */ |
---|
303 | Script* Scriptable::getScriptByVirtualMachine(LuaVirtualMachine& virtualMachine) |
---|
304 | { |
---|
305 | |
---|
306 | if(virtualMachine.isOk()) |
---|
307 | { |
---|
308 | lua_State * luaState = (lua_State* ) virtualMachine; |
---|
309 | lua_getglobal (luaState, "this"); |
---|
310 | |
---|
311 | if (lua_istable (luaState, 1)) |
---|
312 | { |
---|
313 | // Found the "this" table. The object pointer is at the index 0 |
---|
314 | lua_rawgeti (luaState, 1, 0); |
---|
315 | |
---|
316 | if (lua_islightuserdata (luaState, -1)) |
---|
317 | { |
---|
318 | // Found the pointer, need to cast it |
---|
319 | LuaScript* thisPointer = (LuaScript *) lua_touserdata (luaState, -1); |
---|
320 | Script* script = getScriptByPointer(thisPointer); |
---|
321 | |
---|
322 | if(script) |
---|
323 | return script; |
---|
324 | else |
---|
325 | return NULL; |
---|
326 | } |
---|
327 | } |
---|
328 | } |
---|
329 | |
---|
330 | |
---|
331 | } |
---|
332 | |
---|
333 | |
---|
334 | /** |
---|
335 | * @brief Checks if "this" Scriptable is already registred with a LuaScript |
---|
336 | * @param script The LuaScript |
---|
337 | * |
---|
338 | * @return true when the Scriptable is alreads registered with that script, flase else. |
---|
339 | * |
---|
340 | * |
---|
341 | */ |
---|
342 | bool Scriptable::scriptIsInScriptList(LuaScript* script) |
---|
343 | { |
---|
344 | bool notFound = true; |
---|
345 | |
---|
346 | if(script) |
---|
347 | { |
---|
348 | std::list<Script>::iterator it = scriptList.begin(); |
---|
349 | |
---|
350 | |
---|
351 | while( notFound && it !=scriptList.end() ) |
---|
352 | { |
---|
353 | if((*it).script == script) |
---|
354 | { |
---|
355 | notFound = false; |
---|
356 | break; |
---|
357 | } |
---|
358 | it++; |
---|
359 | } |
---|
360 | |
---|
361 | } |
---|
362 | return !notFound; |
---|
363 | |
---|
364 | } |
---|
365 | |
---|
366 | |
---|
367 | /** |
---|
368 | * @brief Removes a LuaScript from the scriptList |
---|
369 | * @param deleted LuaScript that is about to be deleted |
---|
370 | * |
---|
371 | * This function has to be called int the destructor of the LuaScript. |
---|
372 | * |
---|
373 | * |
---|
374 | */ |
---|
375 | |
---|
376 | void Scriptable::scriptDeleted(LuaScript* deleted) |
---|
377 | { |
---|
378 | if(deleted) |
---|
379 | { |
---|
380 | Script* script = getScriptByPointer(deleted); |
---|
381 | if(script) |
---|
382 | { |
---|
383 | std::list<Script>::iterator it = scriptList.begin(); |
---|
384 | |
---|
385 | |
---|
386 | while((*it).script != deleted && it != scriptList.end() ) |
---|
387 | { |
---|
388 | it++; |
---|
389 | } |
---|
390 | |
---|
391 | if(it != scriptList.end()) |
---|
392 | { |
---|
393 | scriptList.erase(it); |
---|
394 | } |
---|
395 | |
---|
396 | |
---|
397 | } |
---|
398 | } |
---|
399 | |
---|
400 | } |
---|
401 | |
---|
402 | |
---|
403 | |
---|
404 | char Scriptable::whatIsThis() |
---|
405 | { |
---|
406 | char result = 's'; |
---|
407 | return result; |
---|
408 | } |
---|
409 | } |
---|