Changeset 7654 in orxonox.OLD for branches/script_engine/src
- Timestamp:
- May 17, 2006, 8:23:13 PM (19 years ago)
- 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 10 10 11 11 ### File Specific: 12 main-programmer: ...12 main-programmer: Silvan Nellen 13 13 co-programmer: ... 14 14 */ … … 16 16 //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ 17 17 18 #include "proto_class.h" 18 #include "Script.h" 19 #include "LuaCallback.h" 20 #include "luaincl.h" 21 #include "scriptable.h" 19 22 20 using namespace std; 23 #include <cassert> 24 #include <string> 25 #include <iostream> 26 #include <stdio.h> 21 27 28 namespace OrxScript 29 { 22 30 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 { 30 43 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; 35 47 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 } 41 116 } 42 43 44 /**45 * standard deconstructor46 */47 ProtoClass::~ProtoClass ()48 {49 // delete what has to be deleted here50 } -
branches/script_engine/src/lib/script_engine/LuaCallback.h
r7653 r7654 2 2 #define __LUA_CALLBACK_H__ 3 3 4 #include <assert.h> 5 #include <string> 6 #include <iostream> 7 #include <stdio.h> 4 struct lua_State; 8 5 9 6 namespace OrxScript 10 7 { 11 8 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); 104 10 } 105 11 #endif /* __LUA_CALLBACK_H__*/ -
branches/script_engine/src/lib/script_engine/Makefile.am
r7648 r7654 5 5 6 6 libORXscript_a_SOURCES = \ 7 LuaCallback.cc \ 8 RestoreStack.cc \ 7 9 Script.cc \ 8 10 scriptable.cc\ 11 This.cc \ 9 12 VirtualMachine.cc 10 13 -
branches/script_engine/src/lib/script_engine/RestoreStack.cc
r7653 r7654 16 16 //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ 17 17 18 #include "proto_class.h" 18 #include "RestoreStack.h" 19 #include "luaincl.h" 19 20 20 using namespace std; 21 namespace OrxScript 22 { 21 23 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 40 28 */ 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 } 41 46 } 42 43 44 /**45 * standard deconstructor46 */47 ProtoClass::~ProtoClass ()48 {49 // delete what has to be deleted here50 } -
branches/script_engine/src/lib/script_engine/RestoreStack.h
r7653 r7654 2 2 #define __RESTORE_STACK_H__ 3 3 4 #include "luaincl.h" 4 5 #include "VirtualMachine.h" 6 /// Forward declaration 7 struct lua_State; 5 8 6 9 namespace OrxScript … … 16 19 * @param virtualMachine the virtual machine to save 17 20 */ 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); 35 23 36 24 protected: -
branches/script_engine/src/lib/script_engine/Script.cc
r7653 r7654 1 #include < assert.h>1 #include <cassert> 2 2 #include <string> 3 #include <iostream> 3 4 4 5 #include "Script.h" … … 224 225 */ 225 226 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()); 233 233 ++argumentCount; 234 END_LUA_CHECK 234 END_LUA_CHECK; 235 235 } 236 236 -
branches/script_engine/src/lib/script_engine/Script.h
r7653 r7654 5 5 6 6 #include "VirtualMachine.h" 7 #include "luaincl.h"8 7 #include "scriptable.h" 9 8 … … 40 39 void addParam (int iInt); 41 40 void addParam (float fFloat); 42 void addParam (c har *string);41 void addParam (const std::string& string); 43 42 44 43 // Runs the loaded script … … 62 61 /* ------------------- Handle external Objects ------------------ */ 63 62 64 void removeFromScript(int referenceToScriptable) {}63 void removeFromScript(int referenceToScriptable) {} 65 64 int addScriptableToScript(Scriptable* scriptbl,const std::string& name); // name= name the scriptable goes by in lua 66 65 int addFunctionToScriptable(const std::string& strFuncName, int toScriptable, int lastMethodIndex); -
branches/script_engine/src/lib/script_engine/This.cc
r7653 r7654 10 10 11 11 ### File Specific: 12 main-programmer: ...12 main-programmer: Silvan Nellen 13 13 co-programmer: ... 14 14 */ … … 16 16 //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ 17 17 18 #include "proto_class.h" 18 #include "This.h" 19 #include "luaincl.h" 19 20 20 using namespace std; 21 namespace 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); 21 31 32 // replace it with our new one 33 lua_rawgeti(state, LUA_REGISTRYINDEX, iRef); 34 lua_setglobal (state, "this"); 35 } 36 } 22 37 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 } 41 49 } 42 43 44 /**45 * standard deconstructor46 */47 ProtoClass::~ProtoClass ()48 {49 // delete what has to be deleted here50 } -
branches/script_engine/src/lib/script_engine/This.h
r7653 r7654 3 3 4 4 #include "VirtualMachine.h" 5 #include "luaincl.h"6 5 7 6 // Sets the "this" global table that scripts use … … 12 11 { 13 12 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); 41 15 42 16 protected: -
branches/script_engine/src/lib/script_engine/VirtualMachine.h
r7653 r7654 24 24 25 25 // 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; } 28 28 29 29 static void panic (lua_State *lua); -
branches/script_engine/src/lib/script_engine/scriptable.cc
r7653 r7654 1 #include < assert.h>1 #include <cassert> 2 2 #include <string> 3 #include <iostream> 4 3 5 #include <map> 4 6 #include <list> -
branches/script_engine/src/lib/script_engine/scriptable.h
r7653 r7654 26 26 class Scriptable 27 27 { 28 friend class LuaScript; 29 28 30 public: 29 31 Scriptable (void); … … 62 64 63 65 64 friend class LuaScript;65 66 66 67 std::list<std::string> functionList;
Note: See TracChangeset
for help on using the changeset viewer.