Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 7654 in orxonox.OLD for branches/script_engine


Ignore:
Timestamp:
May 17, 2006, 8:23:13 PM (19 years ago)
Author:
bensch
Message:

orxonox/script_engine: moved everything into cc-files

Location:
branches/script_engine/src/lib/script_engine
Files:
9 edited
3 copied

Legend:

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

    r7653 r7654  
    1010
    1111   ### File Specific:
    12    main-programmer: ...
     12   main-programmer: Silvan Nellen
    1313   co-programmer: ...
    1414*/
     
    1616//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
    1717
    18 #include "proto_class.h"
     18#include "Script.h"
     19#include "LuaCallback.h"
     20#include "luaincl.h"
     21#include "scriptable.h"
    1922
    20 using namespace std;
     23#include <cassert>
     24#include <string>
     25#include <iostream>
     26#include <stdio.h>
    2127
     28namespace OrxScript
     29{
    2230
    23 /**
    24  * standard constructor
    25  * @todo this constructor is not jet implemented - do it
    26 */
    27 ProtoClass::ProtoClass ()
    28 {
    29    this->setClassID(CL_PROTO_ID, "ProtoClass");
     31  /**
     32   * @brief This function accually executes a method called by Lua.
     33   * @param lua Since this function gets called by lua it has to have a lua_State as parameter
     34   * @return the number of return values. (which can be found on the stack)
     35   *
     36   * For Lua doesn't know how to handle C++ objects it pushes the Objecttable (including the objectpointer) on top of the stack and
     37   * calls this function.
     38   *
     39   * @todo handle an acces to a nonexistent (deleted) object
     40   */
     41  int luaCallback (lua_State *lua)
     42  {
    3043
    31    /* If you make a new class, what is most probably the case when you write this file
    32       don't forget to:
    33        1. Add the new file new_class.cc to the ./src/Makefile.am
    34        2. Add the class identifier to ./src/class_id.h eg. CL_NEW_CLASS
     44    // Locate the psudo-index for the function number
     45    int methodNumber = lua_upvalueindex (1);
     46    int returnCount = 0;
    3547
    36       Advanced Topics:
    37       - if you want to let your object be managed via the ObjectManager make sure to read
    38         the object_manager.h header comments. You will use this most certanly only if you
    39         make many objects of your class, like a weapon bullet.
    40    */
     48    bool functionSuccess = false;
     49
     50    // Check for the "this" table
     51    if (lua_istable (lua, 1))
     52    {
     53      // Found the "this" table. The object pointer is at the index 0
     54      lua_rawgeti (lua, 1, 0);
     55
     56      if (lua_islightuserdata (lua, -1))
     57      {
     58        // Found the pointer, need to cast it
     59        Scriptable* thisPointer = (Scriptable*) lua_touserdata (lua, -1);
     60
     61        // Get the method index
     62        int methodIndex = (int) lua_tonumber (lua, methodNumber);
     63
     64        // Reformat the stack so our parameters are correct
     65        // Clean up the "this" table
     66        lua_remove (lua, 1);
     67        // Clean up the thisPointer pointer
     68        lua_remove (lua, -1);
     69
     70        //debug
     71        //std::cout<<thisPointer->whatIsThis()<<std::endl;
     72
     73        LuaScript* originScript = NULL;
     74
     75        lua_getglobal (lua, "this");
     76
     77        if (lua_istable (lua, 1))
     78        {
     79          // Found the "this" table. The object pointer is at the index 0
     80          lua_rawgeti (lua, 1, 0);
     81
     82          if (lua_islightuserdata (lua, -1))
     83          {
     84            // Found the pointer, need to cast it
     85            originScript = (LuaScript *) lua_touserdata (lua, -1);
     86          }
     87        }
     88
     89        if(originScript == NULL)
     90        {//do something usefull
     91        }
     92        //debug
     93        // std::cout<<originScript->whatIsThis()<<std::endl;
     94
     95        LuaVirtualMachine virtualMachine = originScript->getVirtualMachine();
     96        //debug
     97        //std::cout<<"test "<< thisPointer->methods(virtualMachine)<<std::endl;
     98        // Check that the method is correct index
     99        assert ((methodIndex <= thisPointer->methods(virtualMachine) ));
     100        // Call the class
     101        returnCount = thisPointer->scriptCalling ( virtualMachine, thisPointer->getFunctionAtIndex(methodIndex,virtualMachine));
     102
     103        functionSuccess = true;
     104      }
     105    }
     106
     107    if (functionSuccess == false)
     108    {
     109      lua_pushstring (lua, "luaCallback -> Failed to call the class function");
     110      lua_error (lua);
     111    }
     112
     113    // Number of return variables
     114    return returnCount;
     115  }
    41116}
    42 
    43 
    44 /**
    45  * standard deconstructor
    46 */
    47 ProtoClass::~ProtoClass ()
    48 {
    49   // delete what has to be deleted here
    50 }
  • branches/script_engine/src/lib/script_engine/LuaCallback.h

    r7653 r7654  
    22#define __LUA_CALLBACK_H__
    33
    4 #include <assert.h>
    5 #include <string>
    6 #include <iostream>
    7 #include <stdio.h>
     4struct lua_State;
    85
    96namespace OrxScript
    107{
    118  class LuaVirtualMachine;
    12 
    13 
    14   /**
    15    * @brief This function accually executes a method called by Lua.
    16    *
    17    * @param lua Since this function gets called by lua it has to have a lua_State as parameter
    18    *
    19    * @return the number of return values. (which can be found on the stack)
    20    *
    21    * For Lua doesn't know how to handle C++ objects it pushes the Objecttable (including the objectpointer) on top of the stack and
    22    * calls this function.
    23    *
    24    *
    25    * @todo handle an acces to a nonexistent (deleted) object
    26    */
    27 
    28 
    29   static int luaCallback (lua_State *lua)
    30   {
    31 
    32     // Locate the psudo-index for the function number
    33     int methodNumber = lua_upvalueindex (1);
    34     int returnCount = 0;
    35 
    36     bool functionSuccess = false;
    37 
    38     // Check for the "this" table
    39     if (lua_istable (lua, 1))
    40     {
    41       // Found the "this" table. The object pointer is at the index 0
    42       lua_rawgeti (lua, 1, 0);
    43 
    44       if (lua_islightuserdata (lua, -1))
    45       {
    46         // Found the pointer, need to cast it
    47         Scriptable* thisPointer = (Scriptable*) lua_touserdata (lua, -1);
    48 
    49         // Get the method index
    50         int methodIndex = (int) lua_tonumber (lua, methodNumber);
    51 
    52         // Reformat the stack so our parameters are correct
    53         // Clean up the "this" table
    54         lua_remove (lua, 1);
    55         // Clean up the thisPointer pointer
    56         lua_remove (lua, -1);
    57 
    58         //debug
    59         //std::cout<<thisPointer->whatIsThis()<<std::endl;
    60 
    61         LuaScript* originScript = NULL;
    62 
    63         lua_getglobal (lua, "this");
    64 
    65         if (lua_istable (lua, 1))
    66         {
    67           // Found the "this" table. The object pointer is at the index 0
    68           lua_rawgeti (lua, 1, 0);
    69 
    70           if (lua_islightuserdata (lua, -1))
    71           {
    72             // Found the pointer, need to cast it
    73             originScript = (LuaScript *) lua_touserdata (lua, -1);
    74           }
    75         }
    76 
    77         if(originScript == NULL)
    78         {//do something usefull
    79         }
    80         //debug
    81         // std::cout<<originScript->whatIsThis()<<std::endl;
    82 
    83         LuaVirtualMachine virtualMachine = originScript->getVirtualMachine();
    84         //debug
    85         //std::cout<<"test "<< thisPointer->methods(virtualMachine)<<std::endl;
    86         // Check that the method is correct index
    87         assert ((methodIndex <= thisPointer->methods(virtualMachine) ));
    88         // Call the class
    89         returnCount = thisPointer->scriptCalling ( virtualMachine, thisPointer->getFunctionAtIndex(methodIndex,virtualMachine));
    90 
    91         functionSuccess = true;
    92       }
    93     }
    94 
    95     if (functionSuccess == false)
    96     {
    97       lua_pushstring (lua, "luaCallback -> Failed to call the class function");
    98       lua_error (lua);
    99     }
    100 
    101     // Number of return variables
    102     return returnCount;
    103   }
     9  int luaCallback (lua_State *lua);
    10410}
    10511#endif /* __LUA_CALLBACK_H__*/
  • branches/script_engine/src/lib/script_engine/Makefile.am

    r7648 r7654  
    55
    66libORXscript_a_SOURCES = \
     7                LuaCallback.cc \
     8                RestoreStack.cc \
    79                Script.cc \
    810                scriptable.cc\
     11                This.cc \
    912                VirtualMachine.cc
    1013
  • branches/script_engine/src/lib/script_engine/RestoreStack.cc

    r7653 r7654  
    1616//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
    1717
    18 #include "proto_class.h"
     18#include "RestoreStack.h"
     19#include "luaincl.h"
    1920
    20 using namespace std;
     21namespace OrxScript
     22{
    2123
    22 
    23 /**
    24  * standard constructor
    25  * @todo this constructor is not jet implemented - do it
    26 */
    27 ProtoClass::ProtoClass ()
    28 {
    29    this->setClassID(CL_PROTO_ID, "ProtoClass");
    30 
    31    /* If you make a new class, what is most probably the case when you write this file
    32       don't forget to:
    33        1. Add the new file new_class.cc to the ./src/Makefile.am
    34        2. Add the class identifier to ./src/class_id.h eg. CL_NEW_CLASS
    35 
    36       Advanced Topics:
    37       - if you want to let your object be managed via the ObjectManager make sure to read
    38         the object_manager.h header comments. You will use this most certanly only if you
    39         make many objects of your class, like a weapon bullet.
     24  /**
     25   * @brief Stores the stack and the index to the top element
     26   *
     27   * @param virtualMachine the virtual machine to save
    4028   */
     29  LuaRestoreStack::LuaRestoreStack (LuaVirtualMachine& virtualMachine) : savedState (NULL)
     30  {
     31    savedState = (lua_State *) virtualMachine;
     32    if (virtualMachine.isOk ())
     33    {
     34      savedTopIndex = lua_gettop (savedState);
     35    }
     36  }
     37  /**
     38  * @brief Restores the virtual machine
     39  *
     40  *
     41  */
     42  LuaRestoreStack::~LuaRestoreStack ()
     43  {
     44    lua_settop (savedState, savedTopIndex);
     45  }
    4146}
    42 
    43 
    44 /**
    45  * standard deconstructor
    46 */
    47 ProtoClass::~ProtoClass ()
    48 {
    49   // delete what has to be deleted here
    50 }
  • branches/script_engine/src/lib/script_engine/RestoreStack.h

    r7653 r7654  
    22#define __RESTORE_STACK_H__
    33
    4 #include "luaincl.h"
     4
     5#include "VirtualMachine.h"
     6/// Forward declaration
     7struct lua_State;
    58
    69namespace OrxScript
     
    1619     * @param virtualMachine the virtual machine to save
    1720     */
    18     LuaRestoreStack (LuaVirtualMachine& virtualMachine) : savedState (NULL)
    19     {
    20       savedState = (lua_State *) virtualMachine;
    21       if (virtualMachine.isOk ())
    22       {
    23         savedTopIndex = lua_gettop (savedState);
    24       }
    25     }
    26     /**
    27      * @brief Restores the virtual machine
    28      *
    29      *
    30      */
    31     virtual ~LuaRestoreStack (void)
    32     {
    33       lua_settop (savedState, savedTopIndex);
    34     }
     21    LuaRestoreStack (LuaVirtualMachine& virtualMachine);
     22    virtual ~LuaRestoreStack (void);
    3523
    3624  protected:
  • branches/script_engine/src/lib/script_engine/Script.cc

    r7653 r7654  
    1 #include <assert.h>
     1#include <cassert>
    22#include <string>
     3#include <iostream>
    34
    45#include "Script.h"
     
    224225   */
    225226
    226   void LuaScript::addParam (char *string)
    227   {
    228     assert (string != NULL && "LuaScript::addParam -> string == NULL");
    229     assert (virtualMachine.isOk () && "VM Not OK");
    230 
    231     BEGIN_LUA_CHECK (virtualMachine)
    232     lua_pushstring (state, string);
     227  void LuaScript::addParam (const std::string& string)
     228  {
     229    assert (virtualMachine.isOk () && "VM Not OK");
     230
     231    BEGIN_LUA_CHECK (virtualMachine);
     232    lua_pushstring (state, string.c_str());
    233233    ++argumentCount;
    234     END_LUA_CHECK
     234    END_LUA_CHECK;
    235235  }
    236236
  • branches/script_engine/src/lib/script_engine/Script.h

    r7653 r7654  
    55
    66#include "VirtualMachine.h"
    7 #include "luaincl.h"
    87#include "scriptable.h"
    98
     
    4039    void addParam (int iInt);
    4140    void addParam (float fFloat);
    42     void addParam (char *string);
     41    void addParam (const std::string& string);
    4342
    4443    // Runs the loaded script
     
    6261    /* ------------------- Handle external Objects ------------------ */
    6362
    64     void removeFromScript(int referenceToScriptable){}
     63    void removeFromScript(int referenceToScriptable) {}
    6564    int addScriptableToScript(Scriptable* scriptbl,const std::string& name);  // name= name the scriptable goes by in lua
    6665    int addFunctionToScriptable(const std::string& strFuncName, int toScriptable, int lastMethodIndex);
  • branches/script_engine/src/lib/script_engine/This.cc

    r7653 r7654  
    1010
    1111   ### File Specific:
    12    main-programmer: ...
     12   main-programmer: Silvan Nellen
    1313   co-programmer: ...
    1414*/
     
    1616//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
    1717
    18 #include "proto_class.h"
     18#include "This.h"
     19#include "luaincl.h"
    1920
    20 using namespace std;
     21namespace OrxScript
     22{
     23  LuaThis::LuaThis (LuaVirtualMachine& vm, int iRef) : oldReference (0), virtualMachine (vm)
     24  {
     25    lua_State *state = (lua_State *) vm;
     26    if (vm.isOk ())
     27    {
     28      // Save the old "this" table
     29      lua_getglobal (state, "this");
     30      oldReference = luaL_ref (state, LUA_REGISTRYINDEX);
    2131
     32      // replace it with our new one
     33      lua_rawgeti(state, LUA_REGISTRYINDEX, iRef);
     34      lua_setglobal (state, "this");
     35    }
     36  }
    2237
    23 /**
    24  * standard constructor
    25  * @todo this constructor is not jet implemented - do it
    26 */
    27 ProtoClass::ProtoClass ()
    28 {
    29    this->setClassID(CL_PROTO_ID, "ProtoClass");
    30 
    31    /* If you make a new class, what is most probably the case when you write this file
    32       don't forget to:
    33        1. Add the new file new_class.cc to the ./src/Makefile.am
    34        2. Add the class identifier to ./src/class_id.h eg. CL_NEW_CLASS
    35 
    36       Advanced Topics:
    37       - if you want to let your object be managed via the ObjectManager make sure to read
    38         the object_manager.h header comments. You will use this most certanly only if you
    39         make many objects of your class, like a weapon bullet.
    40    */
     38  LuaThis::~LuaThis (void)
     39  {
     40    lua_State *state = (lua_State *) virtualMachine;
     41    if (oldReference > 0 && virtualMachine.isOk ())
     42    {
     43      // Replace the old "this" table
     44      lua_rawgeti(state, LUA_REGISTRYINDEX, oldReference);
     45      lua_setglobal (state, "this");
     46      luaL_unref (state, LUA_REGISTRYINDEX, oldReference);
     47    }
     48  }
    4149}
    42 
    43 
    44 /**
    45  * standard deconstructor
    46 */
    47 ProtoClass::~ProtoClass ()
    48 {
    49   // delete what has to be deleted here
    50 }
  • branches/script_engine/src/lib/script_engine/This.h

    r7653 r7654  
    33
    44#include "VirtualMachine.h"
    5 #include "luaincl.h"
    65
    76// Sets the "this" global table that scripts use
     
    1211  {
    1312  public:
    14     LuaThis (LuaVirtualMachine& vm, int iRef) : oldReference (0), virtualMachine (vm)
    15     {
    16       lua_State *state = (lua_State *) vm;
    17       if (vm.isOk ())
    18       {
    19         // Save the old "this" table
    20         lua_getglobal (state, "this");
    21         oldReference = luaL_ref (state, LUA_REGISTRYINDEX);
    22 
    23         // replace it with our new one
    24         lua_rawgeti(state, LUA_REGISTRYINDEX, iRef);
    25         lua_setglobal (state, "this");
    26       }
    27     }
    28 
    29     virtual ~LuaThis (void)
    30     {
    31       lua_State *state = (lua_State *) virtualMachine;
    32       if (oldReference > 0 && virtualMachine.isOk ())
    33       {
    34         // Replace the old "this" table
    35         lua_rawgeti(state, LUA_REGISTRYINDEX, oldReference);
    36         lua_setglobal (state, "this");
    37         luaL_unref (state, LUA_REGISTRYINDEX, oldReference);
    38       }
    39     }
    40 
     13    LuaThis (LuaVirtualMachine& vm, int iRef);
     14    virtual ~LuaThis (void);
    4115
    4216  protected:
  • branches/script_engine/src/lib/script_engine/VirtualMachine.h

    r7653 r7654  
    2424
    2525    // Get the state of the lua stack (use the cast operator)
    26     //lua_State *GetState (void) { return luaState; }
    27     operator lua_State *(void) { return luaState; }
     26    lua_State* getState (void) const{ return luaState; }
     27    operator lua_State* (void) const { return luaState; }
    2828
    2929    static void panic (lua_State *lua);
  • branches/script_engine/src/lib/script_engine/scriptable.cc

    r7653 r7654  
    1 #include <assert.h>
     1#include <cassert>
    22#include <string>
     3#include <iostream>
     4
    35#include <map>
    46#include <list>
  • branches/script_engine/src/lib/script_engine/scriptable.h

    r7653 r7654  
    2626  class Scriptable
    2727  {
     28    friend class LuaScript;
     29
    2830  public:
    2931    Scriptable (void);
     
    6264
    6365
    64     friend class LuaScript;
    6566
    6667    std::list<std::string> functionList;
Note: See TracChangeset for help on using the changeset viewer.