Changeset 5695 for code/trunk/src/core/Loader.cc
- Timestamp:
- Aug 30, 2009, 2:22:00 AM (16 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
/code/branches/resource2 (added) merged: 3373-3374,5594,5597,5610-5611,5614,5624,5641,5644-5646,5650-5664,5667-5672,5682-5684,5688-5691,5694
- Property svn:mergeinfo changed
-
code/trunk/src/core/Loader.cc
r3370 r5695 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 std::string xmlInput; 125 if (file->getLuaSupport()) 126 { 127 // Use the LuaState to replace the XML tags (calls our function) 128 scoped_ptr<LuaState> luaState(new LuaState()); 129 luaState->setIncludeParser(&Loader::replaceLuaTags); 130 luaState->includeFile(file->getFilename(), file->getResourceGroup(), false); 131 xmlInput = luaState->getOutput().str(); 132 } 133 else 134 { 135 shared_ptr<ResourceInfo> info = Resource::getInfo(file->getFilename(), file->getResourceGroup()); 136 if (info == NULL) 137 { 138 COUT(1) << "Error: Could not find XML file '" << file->getFilename() << "'." << std::endl; 139 return false; 140 } 141 xmlInput = Resource::open(file->getFilename(), file->getResourceGroup())->getAsString(); 142 } 125 143 126 144 try … … 129 147 COUT(3) << "Mask: " << Loader::currentMask_s << std::endl; 130 148 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); 149 ticpp::Document xmlfile(file->getFilename()); 150 xmlfile.Parse(xmlInput, true); 137 151 138 152 ticpp::Element rootElement; … … 208 222 return Loader::load(file, mask); 209 223 } 224 225 std::string Loader::replaceLuaTags(const std::string& text) 226 { 227 // chreate map with all Lua tags 228 std::map<size_t, bool> luaTags; 229 { 230 size_t pos = 0; 231 while ((pos = text.find("<?lua", pos)) != std::string::npos) 232 luaTags[pos++] = true; 233 } 234 { 235 size_t pos = 0; 236 while ((pos = text.find("?>", pos)) != std::string::npos) 237 luaTags[pos++] = false; 238 } 239 240 // erase all tags from the map that are between two quotes 241 { 242 std::map<size_t, bool>::iterator it = luaTags.begin(); 243 std::map<size_t, bool>::iterator it2 = it; 244 bool bBetweenQuotes = false; 245 size_t pos = 0; 246 while ((pos = getNextQuote(text, pos)) != std::string::npos) 247 { 248 while ((it != luaTags.end()) && (it->first < pos)) 249 { 250 if (bBetweenQuotes) 251 { 252 it2++; 253 if(it->second && !(it2->second) && it2->first < pos) 254 it = ++it2; 255 else 256 luaTags.erase(it++); 257 } 258 else 259 ++it; 260 } 261 bBetweenQuotes = !bBetweenQuotes; 262 pos++; 263 } 264 } 265 266 // check whether on every opening <?lua tag a closing ?> tag follows 267 { 268 bool expectedValue = true; 269 for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it) 270 { 271 if (it->second == expectedValue) 272 expectedValue = !expectedValue; 273 else 274 { 275 expectedValue = false; 276 break; 277 } 278 } 279 if (!expectedValue) 280 { 281 COUT(2) << "Warning: Error in level file" << std::endl; 282 // todo: errorhandling 283 return ""; 284 } 285 } 286 287 // Use a stringstream object to speed up the parsing 288 std::ostringstream output; 289 290 // cut the original string into pieces and put them together with print() instead of lua tags 291 { 292 std::map<size_t, bool>::iterator it = luaTags.begin(); 293 bool bInPrintFunction = true; 294 size_t start = 0; 295 size_t end = 0; 296 297 do 298 { 299 if (it != luaTags.end()) 300 end = (*(it++)).first; 301 else 302 end = std::string::npos; 303 304 unsigned int equalSignCounter = 0; 305 306 if (bInPrintFunction) 307 { 308 // count ['='[ and ]'='] and replace tags with print([[ and ]]) 309 std::string temp = text.substr(start, end - start); 310 { 311 size_t pos = 0; 312 while ((pos = temp.find('[', pos)) != std::string::npos) 313 { 314 unsigned int tempCounter = 1; 315 size_t tempPos = pos++; 316 while(temp[++tempPos] == '=') 317 { 318 tempCounter++; 319 } 320 if(temp[tempPos] != '[') 321 { 322 tempCounter = 0; 323 } 324 else if(tempCounter == 0) 325 { 326 tempCounter = 1; 327 } 328 if (tempCounter > equalSignCounter) 329 equalSignCounter = tempCounter; 330 } 331 } 332 { 333 size_t pos = 0; 334 while ((pos = temp.find(']', pos)) != std::string::npos) 335 { 336 unsigned int tempCounter = 1; 337 size_t tempPos = pos++; 338 while(temp[++tempPos] == '=') 339 { 340 tempCounter++; 341 } 342 if(temp[tempPos] != ']') 343 { 344 tempCounter = 0; 345 } 346 else if(tempCounter == 0) 347 { 348 tempCounter = 1; 349 } 350 if (tempCounter > equalSignCounter) 351 equalSignCounter = tempCounter; 352 } 353 } 354 std::string equalSigns = ""; 355 for(unsigned int i = 0; i < equalSignCounter; i++) 356 { 357 equalSigns += "="; 358 } 359 output << "print([" + equalSigns + "[" + temp + "]" + equalSigns +"])"; 360 start = end + 5; 361 } 362 else 363 { 364 output << text.substr(start, end - start); 365 start = end + 2; 366 } 367 368 bInPrintFunction = !bInPrintFunction; 369 } 370 while (end != std::string::npos); 371 } 372 373 return output.str(); 374 } 210 375 }
Note: See TracChangeset
for help on using the changeset viewer.