- Timestamp:
- Jun 14, 2006, 5:50:18 PM (18 years ago)
- Location:
- trunk/src
- Files:
-
- 22 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/script_engine/Makefile.am
r8271 r8408 13 13 14 14 libORXscript_a_SOURCES = \ 15 script.cc\ 16 script_manager.cc\ 17 script_class.cc\ 15 script.cc \ 16 script_manager.cc \ 17 script_class.cc \ 18 script_method.cc \ 19 \ 18 20 account.cc \ 19 21 object.cc … … 25 27 lunar.h\ 26 28 script.h\ 27 script_manager.h\ 28 script_class.h 29 script_manager.h \ 30 script_class.h \ 31 script_method.h 29 32 30 33 … … 32 35 33 36 check_PROGRAMS = example 34 35 36 37 38 39 40 41 37 example_DEPENDENCIES = \ 42 38 $(MAINSRCDIR)/world_entities/libORXwe.a \ 43 39 $(libORXlibs_a_LIBRARIES_) \ 44 40 $(MAINSRCDIR)/util/libORXutils.a 45 46 41 example_LDADD = \ 47 42 $(MAINSRCDIR)/util/libORXutils.a \ -
trunk/src/lib/script_engine/account.cc
r8271 r8408 11 11 12 12 13 class Account : public BaseObject { 14 lua_Number m_balance; 15 public: 16 static const char className[]; 17 static Lunar<Account>::RegType methods[]; 13 class Account : public BaseObject 14 { 15 lua_Number m_balance; 16 public: 17 Account(lua_State *L) { m_balance = luaL_checknumber(L, 1); } 18 Account(double balance=0) : m_balance(balance) { this->setClassID(CL_ACCOUNT, "Account"); } 18 19 19 Account(lua_State *L) { m_balance = luaL_checknumber(L, 1); } 20 Account(double balance=0) : m_balance(balance) { this->setClassID(CL_ACCOUNT, "Account"); } 20 void deposit (float value) { m_balance += value; }; 21 void withdraw(float value) { m_balance -= value; }; 22 float balance() { return m_balance; }; 21 23 22 void deposit (float value) { m_balance += value; }; 23 void withdraw(float value) { m_balance -= value; }; 24 float balance() { return m_balance; }; 24 int deposit (lua_State *L) { m_balance += luaL_checknumber(L, 1); return 0; } 25 int withdraw(lua_State *L) { m_balance -= luaL_checknumber(L, 1); return 0; } 26 int balance (lua_State *L) { lua_pushnumber(L, m_balance); return 1; } 27 ~Account() { printf("deleted Account (%p)\n", this); } 28 }; 25 29 26 int deposit (lua_State *L) { m_balance += luaL_checknumber(L, 1); return 0; }27 int withdraw(lua_State *L) { m_balance -= luaL_checknumber(L, 1); return 0; }28 int balance (lua_State *L) { lua_pushnumber(L, m_balance); return 1; }29 ~Account() { printf("deleted Account (%p)\n", this); }30 };31 30 32 const char Account::className[] = "Account";33 31 34 Lunar<Account>::RegType Account::methods[] = { 35 {"deposit", new ExecutorLua1<Account, float>(&Account::deposit)}, 36 {"withdraw", new ExecutorLua1<Account, float>(&Account::withdraw)}, 37 {"balance", new ExecutorLua0ret<Account, float>(&Account::balance)}, 38 {0,NULL} 39 }; 40 41 CREATE_SCRIPTABLE_CLASS(Account, CL_ACCOUNT); 42 32 CREATE_SCRIPTABLE_CLASS(Account, CL_ACCOUNT, 33 addMethod("deposit", ExecutorLua1<Account, float>(&Account::deposit)) 34 ->addMethod("withdraw", ExecutorLua1<Account, float>(&Account::withdraw)) 35 ->addMethod("balance", ExecutorLua0ret<Account, float>(&Account::balance))); -
trunk/src/lib/script_engine/lunar.h
r8271 r8408 7 7 8 8 #include "luaincl.h" 9 #include "executor/executor_lua.h" 10 11 12 13 template <typename T> class Lunar { 14 typedef struct { T *pT; } userdataType; 15 public: 16 typedef Executor* mfp; 17 typedef struct { const char *name; mfp mfunc; } RegType; 18 19 20 static void Register(Script* script) 9 #include "script_method.h" 10 11 12 13 template <typename T> class Lunar 14 { 15 typedef struct { T *pT; } 16 userdataType; 17 public: 18 typedef Executor* mfp; 19 typedef struct { const char *name; mfp mfunc; } 20 RegType; 21 22 23 static void Register(Script* script, const std::string& className, const ScriptMethod* scriptMethods) 24 { 25 if(script != NULL) 26 Register(script->getLuaState(), className, scriptMethods); 27 } 28 29 static void Register(lua_State *L, const std::string& className, const ScriptMethod* scriptMethods) 30 { 31 Lunar<T>::className = className; 32 Lunar<T>::scriptMethods = scriptMethods; 33 lua_newtable(L); 34 int methods = lua_gettop(L); 35 36 luaL_newmetatable(L, Lunar<T>::className.c_str()); 37 int metatable = lua_gettop(L); 38 39 // store method table in globals so that 40 // scripts can add functions written in Lua. 41 lua_pushvalue(L, methods); 42 set(L, LUA_GLOBALSINDEX, Lunar<T>::className.c_str()); 43 44 // hide metatable from Lua getmetatable() 45 lua_pushvalue(L, methods); 46 set(L, metatable, "__metatable"); 47 48 lua_pushvalue(L, methods); 49 set(L, metatable, "__index"); 50 51 lua_pushcfunction(L, tostring_T); 52 set(L, metatable, "__tostring"); 53 54 lua_pushcfunction(L, gc_T); 55 set(L, metatable, "__gc"); 56 57 lua_newtable(L); // mt for method table 58 lua_pushcfunction(L, new_T); 59 lua_pushvalue(L, -1); // dup new_T function 60 set(L, methods, "new"); // add new_T to method table 61 set(L, -3, "__call"); // mt.__call = new_T 62 lua_setmetatable(L, methods); 63 64 // fill method table with methods from class T 65 /* as it was originally 66 for (RegType *l = T::methods; l->name; l++) 67 { 68 lua_pushstring(L, l->name); 69 lua_pushlightuserdata(L, (void*)l); 70 lua_pushcclosure(L, thunk, 1); 71 lua_settable(L, methods); 72 }*/ 73 for (unsigned int i = 0; i < Lunar<T>::scriptMethods->size(); i++) 74 { 75 lua_pushstring(L, Lunar<T>::scriptMethods->name(i).c_str()); 76 lua_pushlightuserdata(L, (void*)Lunar<T>::scriptMethods->executor(i)); 77 lua_pushcclosure(L, thunk, 1); 78 lua_settable(L, methods); 79 } 80 lua_pop(L, 2); // drop metatable and method table 81 } 82 83 // call named lua method from userdata method table 84 static int call(lua_State *L, const char *method, 85 int nargs=0, int nresults=LUA_MULTRET, int errfunc=0) 86 { 87 int base = lua_gettop(L) - nargs; // userdata index 88 if (!luaL_checkudata(L, base, Lunar<T>::className.c_str())) 89 { 90 lua_settop(L, base-1); // drop userdata and args 91 lua_pushfstring(L, "not a valid %s userdata", Lunar<T>::className.c_str()); 92 return -1; 93 } 94 95 lua_pushstring(L, method); // method name 96 lua_gettable(L, base); // get method from userdata 97 if (lua_isnil(L, -1)) 98 { // no method? 99 lua_settop(L, base-1); // drop userdata and args 100 lua_pushfstring(L, "%s missing method '%s'", Lunar<T>::className.c_str(), method); 101 return -1; 102 } 103 lua_insert(L, base); // put method under userdata, args 104 105 int status = lua_pcall(L, 1+nargs, nresults, errfunc); // call method 106 if (status) 107 { 108 const char *msg = lua_tostring(L, -1); 109 if (msg == NULL) msg = "(error with no message)"; 110 lua_pushfstring(L, "%s:%s status = %d\n%s", 111 Lunar<T>::className.c_str(), method, status, msg); 112 lua_remove(L, base); // remove old message 113 return -1; 114 } 115 return lua_gettop(L) - base + 1; // number of results 116 } 117 118 // push onto the Lua stack a userdata containing a pointer to T object 119 static int push(lua_State *L, T *obj, bool gc=false) 120 { 121 if (!obj) { lua_pushnil(L); return 0; } 122 luaL_getmetatable(L, Lunar<T>::className.c_str()); // lookup metatable in Lua registry 123 if (lua_isnil(L, -1)) luaL_error(L, "%s missing metatable", Lunar<T>::className.c_str()); 124 int mt = lua_gettop(L); 125 subtable(L, mt, "userdata", "v"); 126 userdataType *ud = 127 static_cast<userdataType*>(pushuserdata(L, obj, sizeof(userdataType))); 128 if (ud) 129 { 130 ud->pT = obj; // store pointer to object in userdata 131 lua_pushvalue(L, mt); 132 lua_setmetatable(L, -2); 133 if (gc == false) 21 134 { 22 if(script != NULL) 23 Register(script->getLuaState()); 135 lua_checkstack(L, 3); 136 subtable(L, mt, "do not trash", "k"); 137 lua_pushvalue(L, -2); 138 lua_pushboolean(L, 1); 139 lua_settable(L, -3); 140 lua_pop(L, 1); 24 141 } 25 26 static void Register(lua_State *L) { 27 lua_newtable(L); 28 int methods = lua_gettop(L); 29 30 luaL_newmetatable(L, T::className); 31 int metatable = lua_gettop(L); 32 33 // store method table in globals so that 34 // scripts can add functions written in Lua. 35 lua_pushvalue(L, methods); 36 set(L, LUA_GLOBALSINDEX, T::className); 37 38 // hide metatable from Lua getmetatable() 39 lua_pushvalue(L, methods); 40 set(L, metatable, "__metatable"); 41 42 lua_pushvalue(L, methods); 43 set(L, metatable, "__index"); 44 45 lua_pushcfunction(L, tostring_T); 46 set(L, metatable, "__tostring"); 47 48 lua_pushcfunction(L, gc_T); 49 set(L, metatable, "__gc"); 50 51 lua_newtable(L); // mt for method table 52 lua_pushcfunction(L, new_T); 53 lua_pushvalue(L, -1); // dup new_T function 54 set(L, methods, "new"); // add new_T to method table 55 set(L, -3, "__call"); // mt.__call = new_T 56 lua_setmetatable(L, methods); 57 58 // fill method table with methods from class T 59 for (RegType *l = T::methods; l->name; l++) { 60 lua_pushstring(L, l->name); 61 lua_pushlightuserdata(L, (void*)l); 62 lua_pushcclosure(L, thunk, 1); 63 lua_settable(L, methods); 64 } 65 66 lua_pop(L, 2); // drop metatable and method table 67 } 68 69 // call named lua method from userdata method table 70 static int call(lua_State *L, const char *method, 71 int nargs=0, int nresults=LUA_MULTRET, int errfunc=0) 72 { 73 int base = lua_gettop(L) - nargs; // userdata index 74 if (!luaL_checkudata(L, base, T::className)) { 75 lua_settop(L, base-1); // drop userdata and args 76 lua_pushfstring(L, "not a valid %s userdata", T::className); 77 return -1; 78 } 79 80 lua_pushstring(L, method); // method name 81 lua_gettable(L, base); // get method from userdata 82 if (lua_isnil(L, -1)) { // no method? 83 lua_settop(L, base-1); // drop userdata and args 84 lua_pushfstring(L, "%s missing method '%s'", T::className, method); 85 return -1; 86 } 87 lua_insert(L, base); // put method under userdata, args 88 89 int status = lua_pcall(L, 1+nargs, nresults, errfunc); // call method 90 if (status) { 91 const char *msg = lua_tostring(L, -1); 92 if (msg == NULL) msg = "(error with no message)"; 93 lua_pushfstring(L, "%s:%s status = %d\n%s", 94 T::className, method, status, msg); 95 lua_remove(L, base); // remove old message 96 return -1; 97 } 98 return lua_gettop(L) - base + 1; // number of results 99 } 100 101 // push onto the Lua stack a userdata containing a pointer to T object 102 static int push(lua_State *L, T *obj, bool gc=false) { 103 if (!obj) { lua_pushnil(L); return 0; } 104 luaL_getmetatable(L, T::className); // lookup metatable in Lua registry 105 if (lua_isnil(L, -1)) luaL_error(L, "%s missing metatable", T::className); 106 int mt = lua_gettop(L); 107 subtable(L, mt, "userdata", "v"); 108 userdataType *ud = 109 static_cast<userdataType*>(pushuserdata(L, obj, sizeof(userdataType))); 110 if (ud) { 111 ud->pT = obj; // store pointer to object in userdata 112 lua_pushvalue(L, mt); 113 lua_setmetatable(L, -2); 114 if (gc == false) { 115 lua_checkstack(L, 3); 116 subtable(L, mt, "do not trash", "k"); 117 lua_pushvalue(L, -2); 118 lua_pushboolean(L, 1); 119 lua_settable(L, -3); 120 lua_pop(L, 1); 121 } 122 } 123 lua_replace(L, mt); 124 lua_settop(L, mt); 125 return mt; // index of userdata containing pointer to T object 126 } 127 128 129 static int insertObject(lua_State* L, T* obj, const std::string& name, bool gc=false) 130 { 131 int objectRef = push(L, obj, gc); 132 133 lua_pushlstring(L, name.c_str(), name.size()); 134 lua_pushvalue(L, objectRef ); 135 lua_settable(L, LUA_GLOBALSINDEX ); 136 137 return objectRef; 138 } 139 140 141 static int insertObject(Script* script, T* obj, const std::string& name, bool gc=false) 142 { 143 assert (script != NULL); 144 return insertObject(script->getLuaState(), obj, name, gc); 145 } 146 147 // get userdata from Lua stack and return pointer to T object 148 static T *check(lua_State *L, int narg) { 149 userdataType *ud = 150 static_cast<userdataType*>(luaL_checkudata(L, narg, T::className)); 151 if(!ud) luaL_typerror(L, narg, T::className); 152 return ud->pT; // pointer to T object 153 } 154 155 private: 156 Lunar(); // hide default constructor 157 158 // member function dispatcher 159 static int thunk(lua_State *L) { 160 // stack has userdata, followed by method args 161 T *obj = check(L, 1); // get 'self', or if you prefer, 'this' 162 lua_remove(L, 1); // remove self so member function args start at index 1 163 // get member function from upvalue 164 RegType *l = static_cast<RegType*>(lua_touserdata(L, lua_upvalueindex(1))); 165 int value; 166 (*l->mfunc)(obj, value, L); // call member function 167 return value; 168 } 169 170 // create a new T object and 171 // push onto the Lua stack a userdata containing a pointer to T object 172 static int new_T(lua_State *L) { 173 lua_remove(L, 1); // use classname:new(), instead of classname.new() 174 T *obj = new T(L); // call constructor for T objects 175 push(L, obj, true); // gc_T will delete this object 176 return 1; // userdata containing pointer to T object 177 } 178 179 // garbage collection metamethod 180 static int gc_T(lua_State *L) { 181 if (luaL_getmetafield(L, 1, "do not trash")) { 182 lua_pushvalue(L, 1); // dup userdata 183 lua_gettable(L, -2); 184 if (!lua_isnil(L, -1)) return 0; // do not delete object 185 } 186 userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, 1)); 187 T *obj = ud->pT; 188 if (obj) delete obj; // call destructor for T objects 189 return 0; 190 } 191 192 static int tostring_T (lua_State *L) { 193 char buff[32]; 194 userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, 1)); 195 T *obj = ud->pT; 196 sprintf(buff, "%p", obj); 197 lua_pushfstring(L, "%s (%s)", T::className, buff); 198 return 1; 199 } 200 201 static void set(lua_State *L, int table_index, const char *key) { 202 lua_pushstring(L, key); 203 lua_insert(L, -2); // swap value and key 204 lua_settable(L, table_index); 205 } 206 207 static void weaktable(lua_State *L, const char *mode) { 208 lua_newtable(L); 209 lua_pushvalue(L, -1); // table is its own metatable 210 lua_setmetatable(L, -2); 211 lua_pushliteral(L, "__mode"); 212 lua_pushstring(L, mode); 213 lua_settable(L, -3); // metatable.__mode = mode 214 } 215 216 static void subtable(lua_State *L, int tindex, const char *name, const char *mode) { 217 lua_pushstring(L, name); 218 lua_gettable(L, tindex); 219 if (lua_isnil(L, -1)) { 220 lua_pop(L, 1); 221 lua_checkstack(L, 3); 222 weaktable(L, mode); 223 lua_pushstring(L, name); 224 lua_pushvalue(L, -2); 225 lua_settable(L, tindex); 226 } 227 } 228 229 static void *pushuserdata(lua_State *L, void *key, size_t sz) { 230 void *ud = 0; 231 lua_pushlightuserdata(L, key); 232 lua_gettable(L, -2); // lookup[key] 233 if (lua_isnil(L, -1)) { 234 lua_pop(L, 1); // drop nil 235 lua_checkstack(L, 3); 236 ud = lua_newuserdata(L, sz); // create new userdata 237 lua_pushlightuserdata(L, key); 238 lua_pushvalue(L, -2); // dup userdata 239 lua_settable(L, -4); // lookup[key] = userdata 240 } 241 return ud; 242 } 243 }; 142 } 143 lua_replace(L, mt); 144 lua_settop(L, mt); 145 return mt; // index of userdata containing pointer to T object 146 } 147 148 149 static int insertObject(lua_State* L, T* obj, const std::string& name, bool gc=false) 150 { 151 int objectRef = push(L, obj, gc); 152 153 lua_pushlstring(L, name.c_str(), name.size()); 154 lua_pushvalue(L, objectRef ); 155 lua_settable(L, LUA_GLOBALSINDEX ); 156 157 return objectRef; 158 } 159 160 161 static int insertObject(Script* script, T* obj, const std::string& name, bool gc=false) 162 { 163 assert (script != NULL); 164 return insertObject(script->getLuaState(), obj, name, gc); 165 } 166 167 // get userdata from Lua stack and return pointer to T object 168 static T *check(lua_State *L, int narg) 169 { 170 userdataType *ud = 171 static_cast<userdataType*>(luaL_checkudata(L, narg, Lunar<T>::className.c_str())); 172 if(!ud) luaL_typerror(L, narg, Lunar<T>::className.c_str()); 173 return ud->pT; // pointer to T object 174 } 175 176 private: 177 Lunar(); // hide default constructor 178 179 // member function dispatcher 180 static int thunk(lua_State *L) 181 { 182 // stack has userdata, followed by method args 183 T *obj = check(L, 1); // get 'self', or if you prefer, 'this' 184 lua_remove(L, 1); // remove self so member function args start at index 1 185 // get member function from upvalue 186 Executor *l = static_cast<Executor*>(lua_touserdata(L, lua_upvalueindex(1))); 187 int value; 188 (*l)(obj, value, L); // call member function 189 return value; 190 } 191 192 // create a new T object and 193 // push onto the Lua stack a userdata containing a pointer to T object 194 static int new_T(lua_State *L) 195 { 196 lua_remove(L, 1); // use classname:new(), instead of classname.new() 197 T *obj = new T(); // call constructor for T objects 198 push(L, obj, true); // gc_T will delete this object 199 return 1; // userdata containing pointer to T object 200 } 201 202 // garbage collection metamethod 203 static int gc_T(lua_State *L) 204 { 205 if (luaL_getmetafield(L, 1, "do not trash")) 206 { 207 lua_pushvalue(L, 1); // dup userdata 208 lua_gettable(L, -2); 209 if (!lua_isnil(L, -1)) return 0; // do not delete object 210 } 211 userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, 1)); 212 T *obj = ud->pT; 213 if (obj) delete obj; // call destructor for T objects 214 return 0; 215 } 216 217 static int tostring_T (lua_State *L) 218 { 219 char buff[32]; 220 userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, 1)); 221 T *obj = ud->pT; 222 sprintf(buff, "%p", obj); 223 lua_pushfstring(L, "%s (%s)", Lunar<T>::className.c_str(), buff); 224 return 1; 225 } 226 227 static void set(lua_State *L, int table_index, const char *key) 228 { 229 lua_pushstring(L, key); 230 lua_insert(L, -2); // swap value and key 231 lua_settable(L, table_index); 232 } 233 234 static void weaktable(lua_State *L, const char *mode) 235 { 236 lua_newtable(L); 237 lua_pushvalue(L, -1); // table is its own metatable 238 lua_setmetatable(L, -2); 239 lua_pushliteral(L, "__mode"); 240 lua_pushstring(L, mode); 241 lua_settable(L, -3); // metatable.__mode = mode 242 } 243 244 static void subtable(lua_State *L, int tindex, const char *name, const char *mode) 245 { 246 lua_pushstring(L, name); 247 lua_gettable(L, tindex); 248 if (lua_isnil(L, -1)) 249 { 250 lua_pop(L, 1); 251 lua_checkstack(L, 3); 252 weaktable(L, mode); 253 lua_pushstring(L, name); 254 lua_pushvalue(L, -2); 255 lua_settable(L, tindex); 256 } 257 } 258 259 static void *pushuserdata(lua_State *L, void *key, size_t sz) 260 { 261 void *ud = 0; 262 lua_pushlightuserdata(L, key); 263 lua_gettable(L, -2); // lookup[key] 264 if (lua_isnil(L, -1)) 265 { 266 lua_pop(L, 1); // drop nil 267 lua_checkstack(L, 3); 268 ud = lua_newuserdata(L, sz); // create new userdata 269 lua_pushlightuserdata(L, key); 270 lua_pushvalue(L, -2); // dup userdata 271 lua_settable(L, -4); // lookup[key] = userdata 272 } 273 return ud; 274 } 275 276 private: 277 static std::string className; 278 static const ScriptMethod* scriptMethods; 279 }; 280 281 template <typename T> 282 std::string Lunar<T>::className = ""; 283 template <typename T> 284 const ScriptMethod* Lunar<T>::scriptMethods = NULL; 244 285 245 286 #endif /* _LUNAR_H */ -
trunk/src/lib/script_engine/lunartest2.lua
r8271 r8408 22 22 23 23 function main(arg) 24 io.write("hello i am main!") 24 25 -- use parameter 25 26 io.write("main received ", arg) 26 27 io.write(" as parameter\n") 27 28 29 o = Object() 30 o:printName() 28 31 --call member of an inserted object 29 32 Obj:printName() 30 33 31 34 --create object of a registered type 32 o = Object()33 o:printName()35 --o = Object() 36 --o:printName() 34 37 35 38 --take returnvalue from c -
trunk/src/lib/script_engine/object.cc
r8271 r8408 8 8 9 9 10 class Object : public BaseObject { 11 public: 12 static const char className[]; 13 static Lunar<Object>::RegType methods[]; 10 class Object : public BaseObject 11 { 12 public: 13 Object(lua_State* L) {callCount = 0; } 14 Object(){callCount = 0;this->setClassID(CL_TEST_OBJECT, "Object");}; 15 ~Object() { printf("deleted Object (%p)\n", this); } 14 16 15 Object(lua_State* L) {callCount = 0; } 16 Object(){callCount = 0;}; 17 ~Object() { printf("deleted Object (%p)\n", this); } 17 //meber functions 18 void takeParam(int i) 19 { 20 printf("Lua passed %i to c++\n",i); 21 } 18 22 19 //meber functions20 void takeParam(int i)21 {22 printf("Lua passed %i to c++\n",i);23 23 int printName(lua_State* L) 24 { 25 this->printName(); 26 return 0; 27 } 24 28 25 int printName(lua_State* L)26 27 this->printName();28 return 0;29 29 void printName() 30 { 31 callCount ++; 32 printf("Hi i'm object %p ! This is the %i. call.\n",this,callCount); 33 } 30 34 31 void printName() 32 { 33 callCount ++; 34 printf("Hi i'm object %p ! This is the %i. call.\n",this,callCount); 35 } 35 int getCallCount(){ return callCount; } 36 36 37 int getCallCount(){ return callCount; } 37 private: 38 int callCount; 38 39 39 private: 40 int callCount; 40 }; 41 41 42 };43 42 44 const char Object::className[] = "Object"; 45 46 Lunar<Object>::RegType Object::methods[] = { 47 {"printName", new ExecutorLua0<Object>(&Object::printName)}, 48 {"getCallCount", new ExecutorLua0ret<Object, int>(&Object::getCallCount)}, 49 {"takeParam", new ExecutorLua1<Object, int>(&Object::takeParam)}, 50 {0,0} 51 }; 52 53 CREATE_SCRIPTABLE_CLASS(Object, CL_TEST_OBJECT); 43 CREATE_SCRIPTABLE_CLASS(Object, CL_TEST_OBJECT, 44 addMethod("printName", ExecutorLua0<Object>(&Object::printName)) 45 ->addMethod("getCallCount", ExecutorLua0ret<Object, int>(&Object::getCallCount)) 46 ->addMethod("takeParam", ExecutorLua1<Object, int>(&Object::takeParam))); -
trunk/src/lib/script_engine/script.cc
r8271 r8408 23 23 luaopen_math(luaState); 24 24 luaopen_debug(luaState); 25 26 25 if (root != NULL) 27 26 this->loadParams(root); … … 38 37 void Script::loadParams(const TiXmlElement* root) 39 38 { 39 //printf("Loading params for %p \n",this); 40 40 BaseObject::loadParams(root); 41 41 … … 77 77 else 78 78 { 79 reportError(error); 80 } 81 82 } 83 else 84 { 79 printf("ERROR while loading file %s: \n",filename.c_str()); 80 reportError(error); 81 } 82 83 } 84 else 85 { 86 printf("ERROR while loading file %s: \n",filename.c_str()); 85 87 reportError(error); 86 88 } … … 92 94 void Script::addObject(const std::string& className, const std::string& objectName) 93 95 { 96 //printf("Script %p: I am about to add %s of class %s\n",this,objectName.c_str(),className.c_str()); 97 94 98 BaseObject* scriptClass = ClassList::getObject(className, CL_SCRIPT_CLASS); 99 //printf("The script class for %s is at %p \n",className.c_str(),scriptClass); 95 100 WorldObject tmpObj; 96 101 if (scriptClass != NULL) … … 103 108 104 109 BaseObject* object = ClassList::getObject(objectName, className); 110 //printf("%s is at %p \n",objectName.c_str(),object); 105 111 if (object != NULL && !objectIsAdded(objectName)) 106 112 { … … 162 168 if(error != 0) 163 169 { 170 printf("ERROR while executing function %s: \n",currentFunction.c_str()); 164 171 reportError(error); 172 //clean up 173 currentFunction.assign(""); 174 argumentCount = returnCount = 0; 165 175 return false; 166 176 } … … 174 184 else 175 185 printf("Error: no function selected.\n"); 186 187 return false; 176 188 } 177 189 … … 229 241 int Script::getReturnedInt() 230 242 { 231 int returnValue ;243 int returnValue = 0; 232 244 if(returnCount > 0) 233 245 { 234 if(lua_isnumber(luaState, -1)) 235 { 236 returnValue = (int)lua_tonumber(luaState, -1); 246 if(lua_isnumber(luaState, -1*returnCount)) 247 { 248 returnValue = (int)lua_tonumber(luaState, -1*returnCount); 249 lua_remove(luaState,-1*returnCount); 237 250 returnCount--; 238 lua_pop(luaState,1);251 239 252 } 240 253 } … … 245 258 bool Script::getReturnedBool() 246 259 { 247 bool returnValue ;260 bool returnValue = false; 248 261 if(returnCount > 0) 249 262 { 250 if(lua_isboolean(luaState, -1)) 251 { 252 returnValue = (bool)lua_toboolean(luaState, -1); 263 if(lua_isboolean(luaState, -1*returnCount)) 264 { 265 returnValue = (bool)lua_toboolean(luaState, -1*returnCount); 266 lua_remove(luaState,-1*returnCount); 253 267 returnCount--; 254 lua_pop(luaState,1);255 268 } 256 269 } … … 260 273 float Script::getReturnedFloat() 261 274 { 262 float returnValue ;275 float returnValue = 0.0f; 263 276 if(returnCount > 0) 264 277 { 265 if(lua_isnumber(luaState, -1)) 266 { 267 returnValue = (float)lua_tonumber(luaState, -1); 278 if(lua_isnumber(luaState, -1*returnCount)) 279 { 280 returnValue = (float)lua_tonumber(luaState, -1*returnCount); 281 lua_remove(luaState,-1*returnCount); 268 282 returnCount--; 269 lua_pop(luaState,1);270 283 } 271 284 } … … 275 288 void Script::getReturnedString(std::string& string) 276 289 { 277 const char* returnValue ;290 const char* returnValue = ""; 278 291 if(returnCount > 0) 279 292 { 280 if(lua_isstring(luaState, -1)) 281 { 282 returnValue = lua_tostring(luaState, -1); 293 if(lua_isstring(luaState, -1*returnCount)) 294 { 295 returnValue = lua_tostring(luaState, -1*returnCount); 296 lua_remove(luaState,-1*returnCount); 283 297 returnCount--; 284 lua_pop(luaState,1);285 298 } 286 299 } … … 296 309 fprintf(stderr, "ERROR: %s\n", msg); 297 310 lua_pop(luaState, 1); 311 } 298 312 return error; 299 313 } 300 }301 314 302 315 -
trunk/src/lib/script_engine/script.h
r8362 r8408 47 47 bool executeFunction(); 48 48 49 // get returned values the last return value in lua is the first in c. TODO: change order of return49 // get returned values in the order the lua function returns it 50 50 int getReturnedInt(); 51 51 bool getReturnedBool(); -
trunk/src/lib/script_engine/script_class.cc
r8271 r8408 17 17 18 18 #include "script_class.h" 19 20 using namespace std; 21 19 #include <cassert> 22 20 23 21 /** 24 * standard constructor22 * @brief standard constructor 25 23 * @todo this constructor is not jet implemented - do it 26 24 */ 27 ScriptClass::ScriptClass(const std::string& name, ClassID classID )28 : BaseObject(name)25 ScriptClass::ScriptClass(const std::string& name, ClassID classID, ScriptMethod* scriptMethods) 26 : BaseObject(name) 29 27 { 30 this->setClassID(CL_SCRIPT_CLASS, "ScriptClass"); 31 this->classID = classID; 28 assert(scriptMethods != NULL); 29 this->setClassID(CL_SCRIPT_CLASS, "ScriptClass"); 30 31 this->_classID = classID; 32 33 this->_scriptMethods = scriptMethods; 32 34 } 33 35 … … 38 40 ScriptClass::~ScriptClass () 39 41 { 40 // delete what has to be deleted here42 delete this->_scriptMethods; 41 43 } -
trunk/src/lib/script_engine/script_class.h
r8271 r8408 11 11 #include "script.h" 12 12 #include "lunar.h" 13 14 // FORWARD DECLARATION 13 #include "script_method.h" 15 14 16 15 … … 19 18 * this should be used at the beginning of all the Classes that should be loadable (in the cc-file) 20 19 */ 21 #define CREATE_SCRIPTABLE_CLASS(CLASS_NAME, CLASS_ID) \ 22 tScriptable<CLASS_NAME> global_##CLASS_NAME##_ScriptableClass(#CLASS_NAME, CLASS_ID) 23 20 #define CREATE_SCRIPTABLE_CLASS(CLASS_NAME, CLASS_ID, SCRIPT_METHODS) \ 21 tScriptClass<CLASS_NAME> global_##CLASS_NAME##_ScriptableClass(#CLASS_NAME, CLASS_ID, (new ScriptMethod)->SCRIPT_METHODS) 24 22 25 23 … … 28 26 { 29 27 30 31 28 public: 29 virtual ~ScriptClass(); 32 30 33 34 bool operator==(ClassID classID) { return (this->classID == classID); }31 bool operator==(const std::string& name) { return (this->getName() == name); } 32 bool operator==(ClassID classID) { return (this->_classID == classID); } 35 33 36 37 34 virtual void registerClass(Script* script) = 0; 35 virtual int insertObject(Script* L, BaseObject* obj, bool gc=false) = 0; 38 36 39 protected: 40 ScriptClass(const std::string& name, ClassID classID); 37 const ScriptMethod* scriptMethods() const { return this->_scriptMethods; } 41 38 42 private: 43 ClassID classID; 39 protected: 40 ScriptClass(const std::string& name, ClassID classID, ScriptMethod* scriptMethods); 41 42 private: 43 ClassID _classID; 44 ScriptMethod* _scriptMethods; 44 45 }; 45 46 … … 48 49 49 50 template <class T> 50 class tScript able: public ScriptClass51 class tScriptClass : public ScriptClass 51 52 { 52 53 tScriptable(const std::string& name, ClassID classID)54 : ScriptClass(name, classID)55 53 public: 54 tScriptClass(const std::string& name, ClassID classID, ScriptMethod* scriptMethods) 55 : ScriptClass(name, classID, scriptMethods) 56 { } 56 57 57 virtual void registerClass(Script* script) 58 { 59 Lunar<T>::Register(script); 60 } 61 virtual int insertObject(Script* L, BaseObject* obj, bool gc=false) 62 { 63 Lunar<T>::insertObject(L, dynamic_cast<T*>(obj), obj->getName(), gc); 64 } 65 66 67 68 } 69 ; 58 virtual void registerClass(Script* script) 59 { 60 Lunar<T>::Register(script, this->getName(), this->scriptMethods()); 61 } 62 virtual int insertObject(Script* L, BaseObject* obj, bool gc=false) 63 { 64 return Lunar<T>::insertObject(L, dynamic_cast<T*>(obj), obj->getName(), gc); 65 } 66 }; 70 67 71 68 -
trunk/src/lib/script_engine/script_manager.cc
r8278 r8408 1 1 #include <string> 2 2 #include <list> 3 4 #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD 3 5 4 6 … … 19 21 { 20 22 this->setName("ScriptManager"); 21 printf("ScriptManager created\n");22 23 this->scripts = NULL; 23 24 this->triggers = NULL; … … 37 38 void ScriptManager::loadParams(const TiXmlElement* root) 38 39 { 39 //BaseObject::loadParams(root);40 BaseObject::loadParams(root); 40 41 { 41 42 LoadParamXML(root, "Scripts", this, ScriptManager, createScripts); -
trunk/src/lib/util/executor/executor.h
r8271 r8408 33 33 * Objects of any Class (Templated) 34 34 * Default Values 35 * Functions with up to 5 parameters (more seems useless)35 * Functions with up to 5 parameters (more seems overhead, split up the function) 36 36 * Functions with many types (@see functor_list.h) 37 37 */ -
trunk/src/lib/util/executor/executor_functional.cc
r8035 r8408 15 15 16 16 #include "executor.h" 17 18 std::string ExecutorFunctional_returningString_from; 19 std::string ExecutorFunctional_returningString_default; 17 std::string ExecutorFunctional_returningString_from[5]; 20 18 21 19 … … 32 30 template<> float fromString<float>(const std::string& input, float defaultValue) { return isFloat(input, defaultValue); }; 33 31 template<> char fromString<char>(const std::string& input, char defaultValue) { return isInt(input, defaultValue); }; 34 template<> const std::string& fromString<const std::string&>(const std::string& input, const std::string& defaultValue) { return ExecutorFunctional_returningString_from = isString(input, defaultValue); };32 template<> const std::string& fromString<const std::string&>(const std::string& input, const std::string& defaultValue) { return (input.size() > 0) ? input : defaultValue; }; 35 33 36 34 … … 48 46 template<> float getDefault<float>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getFloat(); }; 49 47 template<> char getDefault<char>(const MultiType* const defaultValues, unsigned int i) { return defaultValues[i].getChar(); }; 50 template<> const std::string& getDefault<const std::string&>(const MultiType* const defaultValues, unsigned int i) { return ExecutorFunctional_returningString_ default= defaultValues[i].getString(); };48 template<> const std::string& getDefault<const std::string&>(const MultiType* const defaultValues, unsigned int i) { return ExecutorFunctional_returningString_from[i] = defaultValues[i].getString(); }; 51 49 52 50 -
trunk/src/lib/util/executor/executor_lua.cc
r8271 r8408 18 18 #include "executor_lua.h" 19 19 20 std::string temp; 20 #include "lunar.h" 21 21 22 std::string temp[5]; 23 24 template<typename type> type* fromLua(lua_State* state, int index) { type *obj = Lunar<type>::check(state, 1); lua_remove(state, 1); return obj;}; 22 25 template<> bool fromLua<bool>(lua_State* state, int index) { return lua_toboolean(state, index); }; 23 26 template<> int fromLua<int>(lua_State* state, int index) { return (int)lua_tonumber(state, index); }; … … 25 28 template<> float fromLua<float>(lua_State* state, int index) { return (float)lua_tonumber(state, index); }; 26 29 template<> char fromLua<char>(lua_State* state, int index) { return (char)lua_tonumber(state, index); }; 27 template<> const std::string& fromLua<const std::string&>(lua_State* state, int index) { temp = lua_tostring(state, index); return temp; };30 template<> const std::string& fromLua<const std::string&>(lua_State* state, int index) { temp[index] = lua_tostring(state, index); return temp[index]; }; 28 31 29 32 30 33 template<typename type> void toLua(lua_State* state, type value) { Lunar<type>::push(state, value, false); }; 31 34 template<> void toLua<bool>(lua_State* state, bool value) { lua_pushboolean(state, (int) value); }; 32 35 template<> void toLua<int>(lua_State* state, int value) { lua_pushnumber(state, (lua_Number) value); }; -
trunk/src/lib/util/executor/executor_lua.h
r8271 r8408 14 14 15 15 16 template<typename type> type fromLua(lua_State* state, int index) { PRINTF(1)("NOT IMPLEMENTED\n"); };16 template<typename type> type fromLua(lua_State* state, int index); 17 17 template<> bool fromLua<bool>(lua_State* state, int index); 18 18 template<> int fromLua<int>(lua_State* state, int index); … … 23 23 24 24 25 template<typename type> void toLua(lua_State* state, type value) { PRINTF(1)("NOT IMPLEMENTED\n"); };25 template<typename type> void toLua(lua_State* state, type value); 26 26 template<> void toLua<bool>(lua_State* state, bool value); 27 27 template<> void toLua<int>(lua_State* state, int value); … … 339 339 }; 340 340 341 342 343 344 345 346 347 341 #endif /* _EXECUTOR_LUA_H */ -
trunk/src/lib/util/loading/load_param.cc
r8316 r8408 31 31 : object(object), paramName(paramName) 32 32 { 33 this->object = object;34 33 this->inLoadCycle = inLoadCycle; 35 34 -
trunk/src/lib/util/substring.h
r7477 r8408 70 70 inline unsigned int size() const { return this->strings.size(); }; 71 71 /** @param i the i'th String @returns the i'th string from the subset of Strings */ 72 const std::string& operator[](unsigned int i) const { return (i < this->strings.size()) ? this->strings[i] : emptyString;return this->getString(i); };72 inline const std::string& operator[](unsigned int i) const { return (i < this->strings.size()) ? this->strings[i] : emptyString; }; 73 73 /** @param i the i'th String @returns the i'th string from the subset of Strings */ 74 const std::string& getString(unsigned int i) const { return (*this)[i]; };74 inline const std::string& getString(unsigned int i) const { return (*this)[i]; }; 75 75 76 76 // the almighty algorithm. -
trunk/src/story_entities/game_world.cc
r8312 r8408 137 137 PhysicsEngine::getInstance(); 138 138 CREngine::getInstance(); 139 140 State::setScriptManager(&this->scriptManager); 141 139 142 } 140 143 … … 146 149 ErrorMessage GameWorld::loadData() 147 150 { 148 this->displayLoadScreen(); 151 this->displayLoadScreen(); State::setScriptManager(&this->scriptManager); 152 149 153 150 154 PRINTF(0)("Loading the GameWorld\n"); … … 182 186 this->dataXML = (TiXmlElement*)root->Clone(); 183 187 184 188 //remove this after finished testing !!!! 185 189 Object* obj= new Object(); 186 190 obj->setName("Obj"); … … 190 194 b->setName("b"); 191 195 196 192 197 LoadParamXML(root, "ScriptManager", &this->scriptManager, ScriptManager, loadParams); 193 198 … … 228 233 this->bPaused = false; 229 234 this->bRunning = true; 230 231 State::setScripManager(&this->scriptManager); 232 235 State::setScriptManager(&this->scriptManager); //make sure we have the right script manager 233 236 this->run(); 234 237 } … … 241 244 { 242 245 PRINTF(3)("GameWorld::stop() - got stop signal\n"); 243 State::setScrip Manager(NULL);246 State::setScriptManager(NULL); 244 247 this->bRunning = false; 245 248 } … … 603 606 604 607 AtmosphericEngine::getInstance()->draw(); 605 608 606 609 if( unlikely( this->showBV)) 607 610 { -
trunk/src/util/state.h
r8271 r8408 96 96 97 97 //////////////////// 98 /// SCRIP _ENGINE ///98 /// SCRIPT_ENGINE /// 99 99 //////////////////// 100 static void setScrip Manager(ScriptManager* scriptManager) { State::scriptManager = scriptManager; };100 static void setScriptManager(ScriptManager* scriptManager) { State::scriptManager = scriptManager; }; 101 101 static ScriptManager* getScriptManager() { return State::scriptManager; }; 102 102 -
trunk/src/world_entities/script_trigger.cc
r8271 r8408 1 1 //for testing 2 2 #include "luaincl.h" 3 3 4 #include "script_trigger.h" 4 5 #include "class_list.h" … … 6 7 7 8 #include "state.h" 8 9 10 9 10 11 12 /** 13 * Constructs a new ScriptTrigger. 14 * @param root the xml element to load the parameters from. 15 * 16 */ 11 17 ScriptTrigger::ScriptTrigger(const TiXmlElement* root) 12 18 { 19 this->setClassID(CL_SCRIPT_TRIGGER, "ScriptTrigger"); 20 this->toList(OM_COMMON); 21 22 doDebugDraw = false; 13 23 scriptCalled = false; 14 24 scriptIsOk = false; 15 25 loadParams(root); 16 26 17 18 } 19 27 } 28 29 /** 30 * Deletes the ScriptTrigger. 31 * 32 */ 20 33 ScriptTrigger::~ScriptTrigger() 21 34 { … … 23 36 } 24 37 38 /** 39 * Reads the values from the tml element and sets them. 40 * @param root the xml element to load the parameters from. 41 * 42 */ 25 43 void ScriptTrigger::loadParams(const TiXmlElement* root) 26 44 { … … 53 71 .describe("True if the script shoul only be called once") 54 72 .defaultValues(""); 55 } 56 57 } 58 59 73 LoadParam(root, "debugdraw", this, ScriptTrigger, setDebugDraw) 74 .describe("True if the script should only be called once") 75 .defaultValues(""); 76 } 77 78 } 79 80 81 /** 82 * Sets the target(a world entity) of the ScriptTrigger. If the distance between the target and this trigger is smaller than the radius, the script gets triggered. 83 * @param target The worldentity that the script supervises. 84 */ 60 85 void ScriptTrigger::setTarget(const std::string& target) 61 86 { … … 72 97 } 73 98 99 /** 100 * Sets the parent of the trigger. 101 * @param parent The parrent. 102 */ 74 103 void ScriptTrigger::setTriggerParent(const std::string& parent) 75 104 { … … 89 118 void ScriptTrigger::tick(float timestep) 90 119 { 91 printf("SCRIPTTRIGGER: tick called\n"); 92 if((this->getAbsDirV()-target->getAbsDirV()).len() < radius)120 121 if( this->distance(target) < radius) 93 122 { 94 123 if(!callOnce) … … 102 131 } 103 132 } 133 //else 134 //printf("SCRIPTTRIGGER: target out of range\n"); 104 135 105 136 } … … 111 142 { 112 143 testScriptingFramework(); 113 if(!(script->selectFunction(this->functionName,0)) )144 /*if(!(script->selectFunction(this->functionName,0)) ) 114 145 printf("Error ScriptTrigger: Selection of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); 115 146 if( !(script->executeFunction()) ) 116 printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str()); 147 printf("Error ScriptTrigger: Execution of %s in %s failed.\n",functionName.c_str(), script->getFileName().c_str());*/ 117 148 } 118 149 } … … 121 152 void ScriptTrigger::setScript(const std::string& file) 122 153 { 154 123 155 ScriptManager* scriptManager = State::getScriptManager(); 124 156 if (scriptManager != NULL) 125 157 { 158 126 159 script = scriptManager->getScriptByFile(file); 127 160 if(script != NULL) 161 { 128 162 scriptIsOk = true; 163 } 129 164 } 130 165 } … … 134 169 { 135 170 std::string file("lunartest2.lua"); 136 // Script script;171 //get script 137 172 Script* script = State::getScriptManager()->getScriptByFile(file); 138 173 printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); 139 140 // Register classes with the script141 // Lunar<Account>::Register(&script);142 //Lunar<Object>::Register(&script);143 144 //Object* obj= new Object();145 //Account a;146 //Account *b = new Account(30);147 148 // Add instances to the script149 //Lunar<Object>::insertObject(&script,obj,"Obj",true);150 //Lunar<Account>::insertObject(&script,&a,"a",false);151 //Lunar<Account>::insertObject(&script,b,"b",true);152 //printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState()));153 154 //Load a file155 //std::string file("lunartest2.lua");156 157 //if(script.loadFile(file))158 //printf("File %s succefully loaded\n", file.c_str());159 //printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState()));160 174 161 175 //execute a function … … 166 180 167 181 script->pushParam(3.14159,main); 182 printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); 168 183 script->executeFunction(); 169 184 170 float retf = script->getReturnedFloat(); 171 printf("main returned %f\n",retf); 172 185 int ret = script->getReturnedInt(); 186 printf("main returned %i\n",ret); 173 187 174 188 if(script->getReturnedBool()) … … 177 191 printf("main returned false\n"); 178 192 179 int ret = script->getReturnedInt(); 180 printf("main returned %i\n",ret); 181 182 //printf("-------------------------- top of the stack:%i\n",lua_gettop(script.getLuaState())); 193 float retf = script->getReturnedFloat(); 194 printf("main returned %f\n",retf); 195 196 197 printf("-------------------------- top of the stack:%i\n",lua_gettop(script->getLuaState())); 183 198 //execute a 2nd function 184 199 printf("----------- test -----------\n"); -
trunk/src/world_entities/script_trigger.h
r8271 r8408 34 34 void setScript(const std::string& file); 35 35 void setFunction(const std::string& function){ this->functionName = function; } 36 36 void setDebugDraw(const bool draw) { this->doDebugDraw = draw; } 37 37 38 ///DRAWING 38 void draw()const{ this->debugDraw();};39 void draw()const{if(doDebugDraw)this->debugDraw();}; 39 40 40 41 private: … … 46 47 Script* script; 47 48 std::string functionName; 49 bool doDebugDraw; 48 50 49 51 //for internal use -
trunk/src/world_entities/space_ships/helicopter.cc
r8362 r8408 35 35 36 36 CREATE_FACTORY(Helicopter, CL_HELICOPTER); 37 #include "script_class.h" 38 //CREATE_SCRIPTABLE_CLASS(Helicopter, CL_HELICOPTER, NULL); 39 37 40 38 41 /** -
trunk/src/world_entities/space_ships/helicopter.h
r7810 r8408 37 37 38 38 virtual void process(const Event &event); 39 40 virtual void moveUp(bool move){bUp = move;}; 39 41 40 42 … … 78 80 }; 79 81 82 //CREATE_SCRIPTABLE_CLASS(Helicopter, CL_HELICOPTER, 83 // addMethod("moveUp", ExecutorLua1<Object,bool>(&Helicopter::moveUp)) 84 // ); 85 86 80 87 #endif /* _HELICOPTERS_H */
Note: See TracChangeset
for help on using the changeset viewer.