Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 17, 2006, 7:55:59 PM (18 years ago)
Author:
bensch
Message:

orxonox/script_engine: namespace OrxScript introduced

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/script_engine/src/lib/script_engine/scriptable.cc

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