Changeset 996
- Timestamp:
- Apr 5, 2008, 9:47:12 PM (17 years ago)
- Location:
- code/branches/script
- Files:
-
- 4 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/script/CMakeLists.txt
r946 r996 72 72 FIND_PACKAGE(ZLIB) 73 73 FIND_PACKAGE(Lua) 74 FIND_PACKAGE(ToLua) 74 75 75 76 #Sets the search paths for the linking -
code/branches/script/bin/levels/sample.oxw
r888 r996 8 8 <ogg src="tha_silent_partner_-_void" /> 9 9 </ambient> 10 </audio> 10 </audio> 11 11 12 --> 12 13 -
code/branches/script/bin/plugins.cfg-init
r790 r996 2 2 3 3 # Define plugin folder 4 PluginFolder=/usr/l ib/OGRE4 PluginFolder=/usr/local/lib/OGRE 5 5 6 6 # Define plugins -
code/branches/script/src/orxonox/CMakeLists.txt
r946 r996 33 33 objects/weapon/WeaponStation.cc 34 34 script/Script.cc 35 script/tolua_script.cc 35 36 ) 36 37 … … 43 44 ${OIS_LIBRARIES} 44 45 ${Lua_LIBRARIES} 46 ${ToLua_LIBRARIES} 45 47 util 46 48 core -
code/branches/script/src/orxonox/core/Loader.cc
r871 r996 33 33 #include "Debug.h" 34 34 #include "CoreIncludes.h" 35 #include "../script/Script.h" 35 36 36 37 #include "util/tinyxml/ticpp.h" … … 107 108 Loader::currentMask_s = level->getMask() * mask; 108 109 110 // let Lua work this out: 111 //Script* lua; 112 Script::loadFile(level->getFile(), true); 113 Script::init(Script::getLuaState()); 114 Script::run(); 115 109 116 try 110 117 { … … 112 119 COUT(3) << "Mask: " << Loader::currentMask_s << std::endl; 113 120 114 ticpp::Document xmlfile(level->getFile()); 115 xmlfile.LoadFile(); 121 //ticpp::Document xmlfile(level->getFile()); 122 //xmlfile.LoadFile(); 123 //ticpp::Element myelement(*Script::getFileString()); 124 ticpp::Document xmlfile; 125 //xmlfile.ToDocument(); 126 xmlfile.Parse(*Script::getFileString(), true); 116 127 117 128 for ( ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++ ) -
code/branches/script/src/orxonox/script/Script.cc
r956 r996 29 29 30 30 #include <fstream> 31 #include <map> 32 33 #include "../core/CoreIncludes.h" 31 34 32 35 extern "C" { … … 35 38 } 36 39 40 #include "tolua++.h" 41 #include "tolua_script.h" 37 42 38 43 namespace orxonox 39 44 { 45 46 lua_State* Script::luaState_ = lua_open(); 47 //lua_State* Script::luaState_ = NULL; 48 std::string Script::fileString_ = ""; 49 40 50 Script::Script() 41 51 { 42 fileLines_ = std::list<std::string>(); 43 44 state_ = lua_open(); 52 } 53 54 Script::~Script() 55 { 56 57 } 58 59 /** 60 @brief Initializes a lua state 61 @param state_ the pointer of the lua_state to initialise 62 */ 63 void Script::init(lua_State *state_) 64 { 65 tolua_script_open(state_); 66 67 /* 45 68 #if LUA_VERSION_NUM == 501 46 69 luaL_openlibs(state_); … … 53 76 luaopen_debug(state_); 54 77 #endif 55 } 56 57 Script::~Script() 58 { 59 78 */ 79 } 80 81 void Script::luaPrint(std::string str) 82 { 83 fileString_ = str; 60 84 } 61 85 … … 63 87 @brief Loads the specified file line by line 64 88 @param filename The filename of the file 89 @param luaTags if true, the loaded file gets stripped off luaTags 65 90 */ 66 void Script::loadFile(std::string filename )91 void Script::loadFile(std::string filename, bool luaTags) 67 92 { 68 93 std::ifstream file; … … 79 104 { 80 105 file.getline(line, 1024); 81 this->fileLines_.push_back(line); 82 } 83 84 // The last line is useless 85 this->fileLines_.pop_back(); 106 fileString_ += line; 107 } 86 108 87 109 file.close(); 88 } 89 90 /** 91 @brief Parses the level file to correct Lua code 92 */ 93 void Script::xmlToLua() 94 { 95 // We will iterate through all the lines and replace things. 96 std::list<std::string>::iterator it; 97 for(it = this->fileLines_.begin(); it != this->fileLines_.end(); ++it) 98 { 99 int pos = (*it).find("<?lua"); 100 while (pos < (int)(*it).length()) 110 //std::string output; 111 112 if (luaTags) fileString_ = replaceLuaTags(Script::fileString_); 113 } 114 115 void Script::run() 116 { 117 int error = 0; 118 std::string init = "local scr = orxonox.Script:new()\n"; 119 init += fileString_; 120 error = luaL_loadstring(luaState_, init.c_str()); 121 if (error == 0) 122 error = lua_pcall(luaState_, 0, 0, 0); 123 if (error != 0) COUT(0) << "Error in Lua-script: " << lua_tostring(luaState_, -1) << std::endl; 124 } 125 126 127 unsigned int Script::getNextQuote(const std::string& text, unsigned int start) 128 { 129 unsigned int quote = start - 1; 130 131 while ((quote = text.find('\"', quote + 1)) != std::string::npos) 132 { 133 unsigned int backslash = quote; 134 unsigned int numbackslashes = 0; 135 for (; backslash > 0; backslash--, numbackslashes++) 136 if (text[backslash - 1] != '\\') 137 break; 138 139 if (numbackslashes % 2 == 0) 140 break; 141 } 142 143 return quote; 144 } 145 146 std::string Script::replaceLuaTags(const std::string& text) 147 { 148 // chreate map with all Lua tags 149 std::map<unsigned int, bool> luaTags; 150 { 151 unsigned int pos = 0; 152 while ((pos = text.find("<?lua", pos)) != std::string::npos) 153 luaTags[pos++] = true; 154 } 155 { 156 unsigned int pos = 0; 157 while ((pos = text.find("?>", pos)) != std::string::npos) 158 luaTags[pos++] = false; 159 } 160 161 // erase all tags from the map that are between two quotes 162 { 163 std::map<unsigned int, bool>::iterator it = luaTags.begin(); 164 bool bBetweenQuotes = false; 165 unsigned int pos = 0; 166 while ((pos = getNextQuote(text, pos)) != std::string::npos) 101 167 { 102 // We found a lua tag 103 std::string front = (*it).substr(0,pos); 104 std::string back = (*it).substr(pos + 5); 105 (*it) = front + "]])" + back; 106 pos = (*it).find("<?lua"); 107 } 108 109 pos = (*it).find("?>"); 110 while (pos < (int)(*it).length()) 168 while ((it != luaTags.end()) && ((*it).first < pos)) 169 { 170 if (bBetweenQuotes) 171 luaTags.erase(it++); 172 else 173 ++it; 174 } 175 bBetweenQuotes = !bBetweenQuotes; 176 pos++; 177 } 178 } 179 180 // check whether on every opening <?lua tag a closing ?> tag follows 181 { 182 bool expectedValue = true; 183 for (std::map<unsigned int, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it) 111 184 { 112 // We found a lua tag 113 std::string front = (*it).substr(0,pos); 114 std::string back = (*it).substr(pos + 2); 115 (*it) = front + "print([[" + back; 116 pos = (*it).find("?>"); 117 } 118 } 119 this->fileLines_.push_front("print([["); 120 this->fileLines_.push_back("]])"); 185 if ((*it).second == expectedValue) 186 expectedValue = !expectedValue; 187 else 188 { 189 expectedValue = false; 190 break; 191 } 192 } 193 if (!expectedValue) { 194 // todo: errorhandling 195 return ""; 196 } 197 } 198 199 // cut the original string into pieces and put them together with print() instead of lua tags 200 std::string output; 201 { 202 std::map<unsigned int, bool>::iterator it = luaTags.begin(); 203 bool bInPrintFunction = true; 204 unsigned int start = 0; 205 unsigned int end = 0; 206 207 do 208 { 209 if (it != luaTags.end()) 210 end = (*(it++)).first; 211 else 212 end = std::string::npos; 213 214 unsigned int equalSignCounter = 0; 215 216 if (bInPrintFunction) 217 { 218 // count ['='[ and ]'='] and replace tags with print([[ and ]]) 219 std::string temp = text.substr(start, end - start); 220 { 221 unsigned int pos = 0; 222 while ((pos = temp.find('[', pos)) != std::string::npos) 223 { 224 unsigned int tempCounter = 1; 225 unsigned int tempPos = pos++; 226 while(temp[++tempPos] == '=') { 227 tempCounter++; 228 } 229 if(temp[tempPos] != '[') { 230 tempCounter = 0; 231 } 232 else if(tempCounter == 0) { 233 tempCounter = 1; 234 } 235 if (tempCounter > equalSignCounter) 236 equalSignCounter = tempCounter; 237 } 238 } 239 { 240 unsigned int pos = 0; 241 while ((pos = temp.find(']', pos)) != std::string::npos) 242 { 243 unsigned int tempCounter = 1; 244 unsigned int tempPos = pos++; 245 while(temp[++tempPos] == '=') { 246 tempCounter++; 247 } 248 if(temp[tempPos] != ']') { 249 tempCounter = 0; 250 } 251 else if(tempCounter == 0) { 252 tempCounter = 1; 253 } 254 if (tempCounter > equalSignCounter) 255 equalSignCounter = tempCounter; 256 } 257 } 258 std::string equalSigns = ""; 259 for(unsigned int i = 0; i < equalSignCounter; i++) { 260 equalSigns += "="; 261 } 262 output += "scr:luaPrint([" + equalSigns + "[" + temp + "]" + equalSigns +"])\n"; 263 start = end + 5; 264 } 265 else 266 { 267 output += text.substr(start, end - start); 268 start = end + 2; 269 } 270 271 bInPrintFunction = !bInPrintFunction; 272 } 273 while (end != std::string::npos); 274 } 275 276 return output; 121 277 } 122 278 -
code/branches/script/src/orxonox/script/Script.h
r956 r996 1 1 /** 2 2 @file script.h 3 @brief Representation of a lua script3 @brief Representation of an interface to lua 4 4 @author Benjamin Knecht <beni_at_orxonox.net> 5 5 */ … … 15 15 #include <string> 16 16 17 namespace orxonox 18 { 17 namespace orxonox // tolua_export 18 { // tolua_export 19 19 20 class Script 21 { 22 Script(); 20 class Script // tolua_export 21 { // tolua_export 22 public: 23 Script(); // tolua_export 23 24 ~Script(); 24 25 25 void loadFile(std::string filename); 26 void xmlToLua(); 26 static void loadFile(std::string filename, bool luaTags); 27 static void init(lua_State *state_); 28 //void xmlToLua(); 29 static void run(); 30 void luaPrint(std::string str); // tolua_export 27 31 28 inline lua_State* getLuaState() { return state_; }; 32 inline static lua_State* getLuaState() { return luaState_; }; 33 inline static std::string* getFileString() { return &fileString_; }; 34 35 static unsigned int getNextQuote(const std::string& text, unsigned int start); 36 static std::string replaceLuaTags(const std::string& text); 29 37 30 38 private: … … 32 40 //std::list<std::string>& getLevelFileLines(); 33 41 34 st d::list<std::string> fileLines_;35 lua_State* state_;42 static std::string fileString_; 43 static lua_State* luaState_; 36 44 37 }; 38 } 45 }; // tolua_export 46 } // tolua_export 39 47 #endif /* _Script_H__ */
Note: See TracChangeset
for help on using the changeset viewer.