Changeset 9869 in orxonox.OLD for trunk/src/lib/script_engine
- Timestamp:
- Oct 3, 2006, 12:19:30 AM (18 years ago)
- Location:
- trunk/src/lib/script_engine
- Files:
-
- 10 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/lib/script_engine/Makefile.am
r8711 r9869 16 16 script_manager.cc \ 17 17 script_class.cc \ 18 script_method.cc 19 18 script_method.cc \ 19 executor_lua_state.cc 20 20 21 AM_CPPFLAGS= @LUA_INCLUDES@ 21 22 … … 25 26 script_manager.h \ 26 27 script_class.h \ 27 script_method.h 28 script_method.h \ 29 executor_lua_state.h 28 30 29 31 -
trunk/src/lib/script_engine/lunar.h
r8408 r9869 16 16 userdataType; 17 17 public: 18 typedef Executor * mfp;18 typedef Executor<lua_State*>* mfp; 19 19 typedef struct { const char *name; mfp mfunc; } 20 20 RegType; … … 184 184 lua_remove(L, 1); // remove self so member function args start at index 1 185 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; 186 mfp l = static_cast<mfp>(lua_touserdata(L, lua_upvalueindex(1))); 187 (*l)(obj, L); // call member function 188 return l->hasRetVal()? 1 : 0; 190 189 } 191 190 -
trunk/src/lib/script_engine/script.cc
r9298 r9869 18 18 #include "luaincl.h" 19 19 #include "debug.h" 20 #include "util/loading/resource_manager.h" 21 20 21 #include "loading/resource_manager.h" 22 22 #include "loading/load_param.h" 23 23 #include "parser/tinyxml/tinyxml.h" 24 24 25 #include "class_list.h" 26 CREATE_SCRIPTABLE_CLASS(Script, CL_SCRIPT, 27 addMethod("addObject", ExecutorLua2<Script,const std::string&, const std::string& >(&Script::addObject)) 28 ->addMethod("registerClass", ExecutorLua1<Script,const std::string&>(&Script::registerClass)) 29 ->addMethod("selectFunction", ExecutorLua2ret<Script, bool, const std::string&, int >(&Script::selectFunction)) 30 ->addMethod("executeFunction", ExecutorLua0ret<Script,bool >(&Script::executeFunction)) 25 ObjectListDefinition(Script); 26 27 CREATE_SCRIPTABLE_CLASS(Script, 28 addMethod("addObject", Executor2<Script, lua_State*,const std::string&, const std::string& >(&Script::addObject)) 29 ->addMethod("registerClass", Executor1<Script, lua_State*,const std::string&>(&Script::registerClass)) 30 ->addMethod("selectFunction", Executor2ret<Script, lua_State*, bool, const std::string&, int >(&Script::selectFunction)) 31 ->addMethod("executeFunction", Executor0ret<Script, lua_State*,bool >(&Script::executeFunction)) 31 32 ); 33 32 34 33 35 Script::Script(const TiXmlElement* root) 34 36 { 35 this-> setClassID(CL_SCRIPT, "Script");37 this->registerObject(this, Script::_objectList); 36 38 37 39 returnCount = argumentCount = 0; … … 52 54 Script::Script(const std::string& filename) 53 55 { 54 this-> setClassID(CL_SCRIPT, "Script");56 this->registerObject(this, Script::_objectList); 55 57 56 58 returnCount = argumentCount = 0; … … 64 66 luaopen_math(luaState); 65 67 luaopen_debug(luaState); 66 68 67 69 this->loadFile(filename); 68 70 … … 98 100 bool Script::loadFile(const std::string& filename) 99 101 { 100 std::string filedest(Resource Manager::getInstance()->getDataDir());101 filedest += " scripts/" + filename;102 102 std::string filedest(Resources::ResourceManager::getInstance()->mainGlobalPath().name()); 103 filedest += "/scripts/" + filename; 104 103 105 this->addThisScript(); 104 106 this->registerStandartClasses(); 105 107 106 108 if(currentFile.length() != 0) 107 109 { … … 142 144 //printf(("Script %s: I am about to add %s of class %s\n",this->getName(),objectName.c_str(),className.c_str()); 143 145 144 BaseObject* scriptClass = ClassList::getObject(className, CL_SCRIPT_CLASS);146 ScriptClass* scriptClass = ScriptClass::objectList().getObject(className); 145 147 // printf(("The script class for %s is at %p \n",className.c_str(),scriptClass); 146 148 WorldObject tmpObj; … … 150 152 if( !classIsRegistered(className) ) 151 153 { 152 static_cast<ScriptClass*>(scriptClass)->registerClass(this);153 } 154 155 BaseObject* object = ClassList::getObject(objectName, className);154 scriptClass->registerClass(this); 155 } 156 157 BaseObject* object = ObjectListBase::getBaseObject(className, objectName); 156 158 // printf(("%s is at %p \n",objectName.c_str(),object); 157 159 if (object != NULL && !objectIsAdded(objectName)) 158 160 { 159 s tatic_cast<ScriptClass*>(scriptClass)->insertObject(this, object, false);161 scriptClass->insertObject(this, object, false); 160 162 tmpObj.name = objectName; 161 163 registeredObjects.push_back(tmpObj); … … 169 171 bool Script::executeFile() 170 172 { 171 PRINT (2)("WARNING:script.executeFile is not implemented yet\n");173 PRINTF(2)("script.executeFile is not implemented yet\n"); 172 174 /*if(currentFile.length() != 0) 173 175 { … … 214 216 if(error != 0) 215 217 { 216 printf("SCRIPT %s : ERROR: Failed to execute function %s: \n",currentFile.c_str(),currentFunction.c_str());218 PRINTF(1)("Script '%s' : Failed to execute function '%s': \n",currentFile.c_str(),currentFunction.c_str()); 217 219 reportError(error); 218 220 //clean up … … 229 231 } 230 232 else 231 printf("SCRIPT %s : ERROR: no function selected.\n",currentFile.c_str());233 PRINTF(1)("SCRIPT '%s' : no function selected.\n",currentFile.c_str()); 232 234 233 235 return false; … … 295 297 lua_remove(luaState,-1*returnCount); 296 298 returnCount--; 297 299 298 300 } 299 301 } … … 352 354 void Script::addThisScript() 353 355 { 354 BaseObject* scriptClass = ClassList::getObject("Script", CL_SCRIPT_CLASS); 355 if (scriptClass != NULL) 356 { 357 static_cast<ScriptClass*>(scriptClass)->registerClass(this); 358 static_cast<ScriptClass*>(scriptClass)->insertObject(this, this,"thisscript", false); 356 ScriptClass* scriptClass = ScriptClass::objectList().getObject("Script"); 357 358 if (scriptClass != NULL) 359 { 360 scriptClass->registerClass(this); 361 scriptClass->insertObject(this, this,"thisscript", false); 359 362 } 360 363 } … … 375 378 { 376 379 bool success = false; 377 380 378 381 //this->registerClass(std::string("Vector")); 379 382 this->registerClass("ScriptTrigger"); … … 382 385 return success; 383 386 } 384 385 387 388 386 389 void Script::registerClass( const std::string& className) 387 390 { 388 BaseObject* scriptClass = ClassList::getObject(className, CL_SCRIPT_CLASS);391 ScriptClass* scriptClass = ScriptClass::objectList().getObject(className); 389 392 //printf(("The script class for %s is at %p \n",className.c_str(),scriptClass); 393 390 394 WorldObject tmpObj; 391 395 if (scriptClass != NULL) … … 400 404 } 401 405 } 402 406 403 407 } 404 408 -
trunk/src/lib/script_engine/script.h
r9298 r9869 22 22 class Script : public BaseObject 23 23 { 24 ObjectListDeclaration(Script); 24 25 public: 25 26 Script(const TiXmlElement* root = NULL); … … 33 34 bool loadFile(const std::string& filename); 34 35 void addObject( const std::string& className,const std::string& objectName); 35 void registerClass(const std::string& className); //!< Register a class but dont add any instances 36 void registerClass(const std::string& className); //!< Register a class but dont add any instances 36 37 37 38 /// QUERRYING -
trunk/src/lib/script_engine/script_class.cc
r8711 r9869 19 19 #include <cassert> 20 20 21 ObjectListDefinition(ScriptClass); 21 22 /** 22 23 * @brief standard constructor 23 24 * @todo this constructor is not jet implemented - do it 24 25 */ 25 ScriptClass::ScriptClass(const std::string& name, ClassIDclassID, ScriptMethod* scriptMethods)26 : BaseObject(name) 26 ScriptClass::ScriptClass(const std::string& name, const ClassID& classID, ScriptMethod* scriptMethods) 27 : BaseObject(name), _classID(classID) 27 28 { 28 29 assert(scriptMethods != NULL); 29 this-> setClassID(CL_SCRIPT_CLASS, "ScriptClass");30 this->registerObject(this, ScriptClass::_objectList); 30 31 31 32 this->_classID = classID; -
trunk/src/lib/script_engine/script_class.h
r9003 r9869 18 18 * this should be used at the beginning of all the Classes that should be loadable (in the cc-file) 19 19 */ 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)20 #define CREATE_SCRIPTABLE_CLASS(CLASS_NAME, SCRIPT_METHODS) \ 21 tScriptClass<CLASS_NAME> global_##CLASS_NAME##_ScriptableClass(#CLASS_NAME, CLASS_NAME::staticClassID(), (new ScriptMethod)->SCRIPT_METHODS) 22 22 23 23 24 24 //! A class for ... 25 class ScriptClass : p rotectedBaseObject25 class ScriptClass : public BaseObject 26 26 { 27 ObjectListDeclaration(ScriptClass); 27 28 28 29 public: 29 30 virtual ~ScriptClass(); 30 31 31 bool operator==(const std::string& name) { return (this->getName() == name); }32 bool operator==(ClassID classID) { return (this->_classID == classID); }33 34 32 virtual void registerClass(Script* script) = 0; 35 33 virtual int insertObject(Script* L, BaseObject* obj, bool gc=false) = 0; 36 virtual int insertObject(Script* L, BaseObject* obj, const std::string& name, bool gc=false) = 0; 34 virtual int insertObject(Script* L, BaseObject* obj, const std::string& name, bool gc=false) = 0; 37 35 38 36 const ScriptMethod* scriptMethods() const { return this->_scriptMethods; } 39 37 40 38 protected: 41 ScriptClass(const std::string& name, ClassIDclassID, ScriptMethod* scriptMethods);39 ScriptClass(const std::string& name, const ClassID& classID, ScriptMethod* scriptMethods); 42 40 43 41 private: -
trunk/src/lib/script_engine/script_manager.cc
r9003 r9869 24 24 #include "lunar.h" 25 25 26 #include "class_list.h"27 28 26 #include "script.h" 29 27 #include "script_trigger.h" 30 28 #include "luaincl.h" 31 #include "loading/load_param.h" 32 #include "parser/tinyxml/tinyxml.h" 29 #include "loading/load_param_xml.h" 33 30 34 31 … … 37 34 { 38 35 this->setName("ScriptManager"); 39 this->scripts = NULL;40 this->triggers = NULL;41 36 42 37 if (root != NULL) … … 62 57 63 58 // fill the scripts and triggers (doing that on runtime is very slow!) 64 getTriggers();65 getScripts();66 59 } 67 60 … … 71 64 { 72 65 //Delete all scripts as they aren't deleted automatically 73 if(this->getScripts()) 74 while(!scripts->empty()) 75 delete scripts->front(); 66 while(!Script::objectList().empty()) 67 delete Script::objectList().front(); 76 68 //Delete all triggers 77 if(this->getTriggers()) 78 while(!triggers->empty()) 79 delete triggers->front(); 80 69 while(!ScriptTrigger::objectList().empty()) 70 delete ScriptTrigger::objectList().front(); 71 81 72 } 82 73 … … 105 96 Script* ScriptManager::getScriptByFile(const std::string& file) 106 97 { 107 if(getScripts()) 108 for(std::list<BaseObject*>::const_iterator it = scripts->begin(); it != scripts->end(); it++ ) 109 { 110 if( (dynamic_cast<Script*>(*it))->getFileName().compare(file) == 0) 111 { 112 return dynamic_cast<Script*>(*it); 113 } 114 } 98 for (ObjectList<Script>::const_iterator it = Script::objectList().begin(); 99 it != Script::objectList().end(); 100 ++it) 101 if( ((*it))->getFileName().compare(file) == 0) 102 return (*it); 115 103 116 104 return NULL; 117 105 118 106 } 119 120 121 bool ScriptManager::getScripts()122 {123 return (this->scripts != NULL || (this->scripts = ClassList::getList(CL_SCRIPT)) != NULL);124 }125 126 bool ScriptManager::getTriggers()127 {128 return (this->triggers != NULL || (this->triggers = ClassList::getList(CL_SCRIPT_TRIGGER)) != NULL);129 }130 131 132 -
trunk/src/lib/script_engine/script_manager.h
r8894 r9869 34 34 void createTriggers(const TiXmlElement* triggers); 35 35 36 bool getTriggers();37 bool getScripts();38 39 36 static ScriptManager* singletonRef; //!< Reference to this class 40 41 const std::list<BaseObject*>* triggers; //!< A list of all the triggers in the world42 43 const std::list<BaseObject*>* scripts; //!< A list of all the scripts in the world44 45 46 37 }; 47 38 #endif -
trunk/src/lib/script_engine/script_method.cc
r8711 r9869 25 25 { } 26 26 27 ScriptMethod* ScriptMethod::addMethod(const std::string& methodName, const Executor & executor)27 ScriptMethod* ScriptMethod::addMethod(const std::string& methodName, const Executor<lua_State*>& executor) 28 28 { 29 29 this->methods.push_back(ScriptMethod::Method(methodName, executor)); … … 32 32 } 33 33 34 ScriptMethod::Method::Method(const std::string& name, const Executor & executor)34 ScriptMethod::Method::Method(const std::string& name, const Executor<lua_State*>& executor) 35 35 { 36 36 this->name = name; -
trunk/src/lib/script_engine/script_method.h
r8711 r9869 9 9 #include <vector> 10 10 11 #include "executor /executor_lua.h"12 11 #include "executor_lua_state.h" 12 #include "executor/executor_member.h" 13 13 14 14 class ScriptMethod … … 18 18 ~ScriptMethod(); 19 19 20 ScriptMethod* addMethod(const std::string& methodName, const Executor & executor);20 ScriptMethod* addMethod(const std::string& methodName, const Executor<lua_State*>& executor); 21 21 22 22 unsigned int size() const { return methods.size(); }; 23 23 24 24 const std::string& name(unsigned int methodNumber) const { return methods[methodNumber].name; }; 25 const Executor * executor(unsigned int methodNumber) const { return methods[methodNumber].executor; };25 const Executor<lua_State*>* executor(unsigned int methodNumber) const { return methods[methodNumber].executor; }; 26 26 27 27 … … 29 29 struct Method 30 30 { 31 Method(const std::string& name, const Executor & executor);32 std::string name;33 Executor *executor;31 Method(const std::string& name, const Executor<lua_State*>& executor); 32 std::string name; 33 Executor<lua_State*>* executor; 34 34 }; 35 35
Note: See TracChangeset
for help on using the changeset viewer.