Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/script_engine/OLD/scriptable.cc @ 8258

Last change on this file since 8258 was 8061, checked in by bensch, 18 years ago

merged the scriptengine back to the trunk

File size: 8.6 KB
RevLine 
[7654]1#include <cassert>
[7645]2#include <string>
[7654]3#include <iostream>
4
[7645]5#include <map>
6#include <list>
7
[7655]8#include "luaincl.h"
[7645]9#include "Script.h"
10#include "LuaCallback.h"
11#include "scriptable.h"
12
13// ---------------------------------------------------------------------------
[7653]14namespace OrxScript
[7645]15{
[7653]16  /**
17   * @brief Constructor
18   *
19   */
20  Scriptable::Scriptable ()
21  {
22  }
[7645]23
[7653]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    }
[7645]37
[7653]38  }
[7645]39
40
[7653]41  //Method indexing check
42  int Scriptable::methods (LuaVirtualMachine& virtualMachine)
43  {
44    LuaScript* script =(getScriptByVirtualMachine(virtualMachine))->script;
45    if(script)
[7645]46    {
[7653]47      int lastMethod = getLastMethodIndexByPointer(script);
48      if(lastMethod != -1)
49        return lastMethod;
[7645]50    }
51
[7653]52    return -1;
53  }
[7645]54
55
[7653]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   */
[7645]66
[7653]67  bool Scriptable::scriptableAdded(LuaScript* toScript, int toScriptRef, int reference)
68  {
[7645]69
[7653]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;
[7645]80
[7653]81        scriptList.push_back(newScript);
[7645]82
[7653]83        int methodIndex;
84        Script* tmpScript = getScriptByPointer(toScript);
[7645]85
[7653]86        //add all the functions to the script
87        std::list<std::string>::const_iterator it;
[7645]88
[7653]89        for(it = functionList.begin();it != functionList.end(); it++)
[7645]90        {
91
[7653]92          if(tmpScript)
[7645]93          {
[7653]94            methodIndex = toScript->addFunctionToScriptable(*it, tmpScript->thisReference, tmpScript->lastMethodIndex);
[7645]95
96
[7653]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            }
[7645]108
[7653]109            else if(methodIndex != -1)
110            {
[7645]111
[7653]112              tmpScript->lastMethodIndex = methodIndex;
113              tmpScript->functionMap[newScript.lastMethodIndex] = *it;
114            }
115            else{success= false;break;}
[7645]116
[7653]117          }
118        }
[7645]119
120
121
[7653]122      }
123      else
124      {
125        success = false;
126      }
[7645]127
[7653]128    return success;
[7645]129
[7653]130  }
[7645]131
132
133
[7653]134  /**
135    * @brief  Register a function with the scriptable
136    * @param  functionName function name
137    *
138    *
139   */
[7645]140
[7653]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      }
[7645]151
[7653]152    }
[7645]153
[7653]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)
[7645]168  {
[7653]169    lua_State * luaState = (lua_State* ) virtualMachine;
[7645]170
[7653]171    if(virtualMachine.isOk())
[7645]172    {
[7653]173      lua_getglobal (luaState, "this");
[7645]174
[7653]175      if (lua_istable (luaState, 1))
[7645]176      {
[7653]177        // Found the "this" table. The object pointer is at the index 0
178        lua_rawgeti (luaState, 1, 0);
[7645]179
[7653]180        if (lua_islightuserdata (luaState, -1))
[7645]181        {
[7653]182          // Found the pointer, need to cast it
183          LuaScript* thisPointer = (LuaScript *) lua_touserdata (luaState, -1);
184
185          if(thisPointer != NULL)
[7645]186          {
[7653]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
[7645]195          }
196        }
197      }
198    }
[7657]199    return "";
[7645]200  }
201
202
[7653]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   */
[7645]211
[7653]212  int Scriptable::getLastMethodIndexByPointer(LuaScript* luaScript)
[7645]213  {
214    if( luaScript )
215    {
216      Script* script = (getScriptByPointer(luaScript));
[7653]217      if(script)
218      {
219        return (script->lastMethodIndex);
220      }
221    }
[7645]222
[7653]223    return -1;
[7645]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
[7653]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();
[7645]242
243
[7653]244      while(notFound && it !=scriptList.end() )
245      {
246        if((*it).script == scriptPointer)
247        {
248          notFound = false;
249          return &(it->functionMap);
250        }
251        it++;
252      }
[7645]253    }
254
[7653]255    if(notFound)
256      return NULL;
[7645]257
[7653]258  }
[7645]259
260
[7653]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   */
[7645]269
[7653]270  Script* Scriptable::getScriptByPointer(LuaScript* script)
271  {
272    bool notFound = true;
[7645]273
[7653]274    if(script)
275    {
276      std::list<Script>::iterator it = scriptList.begin();
[7645]277
[7653]278      while(notFound && it != scriptList.end() )
279      {
280        if((*it).script == script)
281        {
282          notFound = false;
283          return &(*it);
284        }
285        it++;
286      }
287
[7645]288    }
[7653]289    if(notFound)
290      return NULL;
[7645]291
[7653]292  }
[7645]293
294
[7653]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  {
[7645]305
[7653]306    if(virtualMachine.isOk())
307    {
308      lua_State * luaState = (lua_State* ) virtualMachine;
309      lua_getglobal (luaState, "this");
[7645]310
[7653]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);
[7645]315
[7653]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);
[7645]321
[7653]322          if(script)
323            return script;
324          else
325            return NULL;
326        }
327      }
328    }
[7645]329
330
[7653]331  }
[7645]332
333
[7653]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;
[7645]345
[7653]346    if(script)
347    {
348      std::list<Script>::iterator it = scriptList.begin();
[7645]349
350
[7653]351      while( notFound && it !=scriptList.end() )
352      {
353        if((*it).script == script)
[7645]354        {
[7653]355          notFound = false;
356          break;
[7645]357        }
[7653]358        it++;
359      }
360
[7645]361    }
[7653]362    return !notFound;
[7645]363
[7653]364  }
[7645]365
366
[7653]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   */
[7645]375
[7653]376  void Scriptable::scriptDeleted(LuaScript* deleted)
[7645]377  {
[7653]378    if(deleted)
379    {
380      Script* script = getScriptByPointer(deleted);
381      if(script)
382      {
383        std::list<Script>::iterator it = scriptList.begin();
[7645]384
385
[7653]386        while((*it).script != deleted && it != scriptList.end() )
387        {
388          it++;
389        }
[7645]390
[7653]391        if(it != scriptList.end())
[7645]392        {
393          scriptList.erase(it);
394        }
395
396
[7653]397      }
398    }
399
[7645]400  }
401
402
403
[7653]404  char Scriptable::whatIsThis()
405  {
406    char result = 's';
407    return result;
408  }
409}
Note: See TracBrowser for help on using the repository browser.