Changeset 5654 for code/branches/resource2/src/core/Loader.cc
- Timestamp:
- Aug 17, 2009, 4:37:10 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/resource2/src/core/Loader.cc
r3370 r5654 29 29 #include "Loader.h" 30 30 31 #include <sstream> 31 32 #include <tinyxml/ticpp.h> 33 #include <boost/scoped_ptr.hpp> 32 34 33 35 #include "util/Debug.h" 34 36 #include "util/Exception.h" 37 #include "util/StringUtils.h" 35 38 #include "BaseObject.h" 36 39 #include "Iterator.h" 37 40 #include "ObjectList.h" 38 #include "Lua Bind.h"41 #include "LuaState.h" 39 42 #include "Namespace.h" 43 #include "Resource.h" 40 44 #include "XMLFile.h" 41 45 … … 118 122 Loader::currentMask_s = file->getMask() * mask; 119 123 120 // let Lua work this out:121 LuaBind& lua = LuaBind::getInstance();122 lua .clearLuaOutput();123 lua .loadFile(file->getFilename(), true);124 lua.run();124 // Use the LuaState to replace the XML tags (calls our function) 125 scoped_ptr<LuaState> luaState(new LuaState()); 126 luaState->setIncludeParser(&Loader::replaceLuaTags); 127 luaState->includeFile(file->getFilename(), file->getResourceGroup(), false); 128 //luaState->doString(luaInput); 125 129 126 130 try … … 129 133 COUT(3) << "Mask: " << Loader::currentMask_s << std::endl; 130 134 131 //ticpp::Document xmlfile(file->getFilename()); 132 //xmlfile.LoadFile(); 133 //ticpp::Element myelement(*Script::getFileString()); 134 ticpp::Document xmlfile; 135 //xmlfile.ToDocument(); 136 xmlfile.Parse(lua.getLuaOutput(), true); 135 ticpp::Document xmlfile(file->getFilename()); 136 xmlfile.Parse(luaState->getOutput().str(), true); 137 137 138 138 ticpp::Element rootElement; … … 208 208 return Loader::load(file, mask); 209 209 } 210 211 std::string Loader::replaceLuaTags(const std::string& text) 212 { 213 // chreate map with all Lua tags 214 std::map<size_t, bool> luaTags; 215 { 216 size_t pos = 0; 217 while ((pos = text.find("<?lua", pos)) != std::string::npos) 218 luaTags[pos++] = true; 219 } 220 { 221 size_t pos = 0; 222 while ((pos = text.find("?>", pos)) != std::string::npos) 223 luaTags[pos++] = false; 224 } 225 226 // erase all tags from the map that are between two quotes 227 { 228 std::map<size_t, bool>::iterator it = luaTags.begin(); 229 std::map<size_t, bool>::iterator it2 = it; 230 bool bBetweenQuotes = false; 231 size_t pos = 0; 232 while ((pos = getNextQuote(text, pos)) != std::string::npos) 233 { 234 while ((it != luaTags.end()) && (it->first < pos)) 235 { 236 if (bBetweenQuotes) 237 { 238 it2++; 239 if(it->second && !(it2->second) && it2->first < pos) 240 it = ++it2; 241 else 242 luaTags.erase(it++); 243 } 244 else 245 ++it; 246 } 247 bBetweenQuotes = !bBetweenQuotes; 248 pos++; 249 } 250 } 251 252 // check whether on every opening <?lua tag a closing ?> tag follows 253 { 254 bool expectedValue = true; 255 for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it) 256 { 257 if (it->second == expectedValue) 258 expectedValue = !expectedValue; 259 else 260 { 261 expectedValue = false; 262 break; 263 } 264 } 265 if (!expectedValue) 266 { 267 COUT(2) << "Warning: Error in level file" << std::endl; 268 // todo: errorhandling 269 return ""; 270 } 271 } 272 273 // Use a stringstream object to speed up the parsing 274 std::ostringstream output; 275 276 // cut the original string into pieces and put them together with print() instead of lua tags 277 { 278 std::map<size_t, bool>::iterator it = luaTags.begin(); 279 bool bInPrintFunction = true; 280 size_t start = 0; 281 size_t end = 0; 282 283 do 284 { 285 if (it != luaTags.end()) 286 end = (*(it++)).first; 287 else 288 end = std::string::npos; 289 290 unsigned int equalSignCounter = 0; 291 292 if (bInPrintFunction) 293 { 294 // count ['='[ and ]'='] and replace tags with print([[ and ]]) 295 std::string temp = text.substr(start, end - start); 296 { 297 size_t pos = 0; 298 while ((pos = temp.find('[', pos)) != std::string::npos) 299 { 300 unsigned int tempCounter = 1; 301 size_t tempPos = pos++; 302 while(temp[++tempPos] == '=') 303 { 304 tempCounter++; 305 } 306 if(temp[tempPos] != '[') 307 { 308 tempCounter = 0; 309 } 310 else if(tempCounter == 0) 311 { 312 tempCounter = 1; 313 } 314 if (tempCounter > equalSignCounter) 315 equalSignCounter = tempCounter; 316 } 317 } 318 { 319 size_t pos = 0; 320 while ((pos = temp.find(']', pos)) != std::string::npos) 321 { 322 unsigned int tempCounter = 1; 323 size_t tempPos = pos++; 324 while(temp[++tempPos] == '=') 325 { 326 tempCounter++; 327 } 328 if(temp[tempPos] != ']') 329 { 330 tempCounter = 0; 331 } 332 else if(tempCounter == 0) 333 { 334 tempCounter = 1; 335 } 336 if (tempCounter > equalSignCounter) 337 equalSignCounter = tempCounter; 338 } 339 } 340 std::string equalSigns = ""; 341 for(unsigned int i = 0; i < equalSignCounter; i++) 342 { 343 equalSigns += "="; 344 } 345 output << "print([" + equalSigns + "[" + temp + "]" + equalSigns +"])"; 346 start = end + 5; 347 } 348 else 349 { 350 output << text.substr(start, end - start); 351 start = end + 2; 352 } 353 354 bInPrintFunction = !bInPrintFunction; 355 } 356 while (end != std::string::npos); 357 } 358 359 return output.str(); 360 } 210 361 }
Note: See TracChangeset
for help on using the changeset viewer.