[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "Loader.h" |
---|
[2710] | 30 | |
---|
[5695] | 31 | #include <sstream> |
---|
[2710] | 32 | #include <tinyxml/ticpp.h> |
---|
[5695] | 33 | #include <boost/scoped_ptr.hpp> |
---|
[10264] | 34 | #include <boost/filesystem.hpp> |
---|
| 35 | #include <boost/filesystem/fstream.hpp> |
---|
[2710] | 36 | |
---|
[8858] | 37 | #include "util/Output.h" |
---|
[3196] | 38 | #include "util/Exception.h" |
---|
[5695] | 39 | #include "util/StringUtils.h" |
---|
[1505] | 40 | #include "BaseObject.h" |
---|
[5695] | 41 | #include "LuaState.h" |
---|
[1505] | 42 | #include "Namespace.h" |
---|
[5695] | 43 | #include "Resource.h" |
---|
[3196] | 44 | #include "XMLFile.h" |
---|
[9667] | 45 | #include "object/Iterator.h" |
---|
| 46 | #include "object/ObjectList.h" |
---|
[1505] | 47 | |
---|
| 48 | namespace orxonox |
---|
| 49 | { |
---|
[10624] | 50 | Loader* Loader::singletonPtr_s = 0; |
---|
[1505] | 51 | |
---|
[7648] | 52 | /** |
---|
| 53 | @brief |
---|
| 54 | Loads the input file, while conforming to the restrictions given by the input ClassTreeMask. |
---|
| 55 | @param file |
---|
| 56 | The file to be loaded. |
---|
| 57 | @param mask |
---|
| 58 | A ClassTreeMask, which defines which types of classes are loaded and which aren't. |
---|
[8858] | 59 | @param bVerbose |
---|
[7648] | 60 | Whether the loader is verbose (prints its progress in a low output level) or not. |
---|
[8108] | 61 | @param bRemoveLuaTags |
---|
| 62 | If true lua tags are just ignored and removed. The default is false. |
---|
[7648] | 63 | @return |
---|
| 64 | Returns true if successful. |
---|
| 65 | */ |
---|
[8858] | 66 | bool Loader::load(const XMLFile* file, const ClassTreeMask& mask, bool bVerbose, bool bRemoveLuaTags) |
---|
[1505] | 67 | { |
---|
[2087] | 68 | if (!file) |
---|
[1755] | 69 | return false; |
---|
| 70 | |
---|
[10624] | 71 | this->currentMask_ = file->getMask() * mask; |
---|
[1505] | 72 | |
---|
[5695] | 73 | std::string xmlInput; |
---|
[10264] | 74 | |
---|
[10265] | 75 | shared_ptr<std::vector<std::vector<std::pair<std::string, size_t> > > > lineTrace(new std::vector<std::vector<std::pair<std::string, size_t> > >()); |
---|
[10264] | 76 | lineTrace->reserve(1000); //arbitrary number |
---|
| 77 | |
---|
| 78 | |
---|
[8079] | 79 | if (file->getLuaSupport() && !bRemoveLuaTags) |
---|
[5695] | 80 | { |
---|
| 81 | // Use the LuaState to replace the XML tags (calls our function) |
---|
| 82 | scoped_ptr<LuaState> luaState(new LuaState()); |
---|
[10264] | 83 | luaState->setTraceMap(lineTrace); |
---|
[5695] | 84 | luaState->setIncludeParser(&Loader::replaceLuaTags); |
---|
[6417] | 85 | luaState->includeFile(file->getFilename()); |
---|
[5695] | 86 | xmlInput = luaState->getOutput().str(); |
---|
| 87 | } |
---|
| 88 | else |
---|
| 89 | { |
---|
[6417] | 90 | shared_ptr<ResourceInfo> info = Resource::getInfo(file->getFilename()); |
---|
[5695] | 91 | if (info == NULL) |
---|
| 92 | { |
---|
[8858] | 93 | orxout(user_error, context::loader) << "Could not find XML file '" << file->getFilename() << "'." << endl; |
---|
[5695] | 94 | return false; |
---|
| 95 | } |
---|
[6417] | 96 | xmlInput = Resource::open(file->getFilename())->getAsString(); |
---|
[8079] | 97 | |
---|
| 98 | if (bRemoveLuaTags) |
---|
| 99 | { |
---|
| 100 | // Remove all Lua code. |
---|
| 101 | // Note: we only need this to speed up parsing of level files at the |
---|
| 102 | // start of the program. |
---|
| 103 | // Assumption: the LevelInfo tag does not use Lua scripting |
---|
[10624] | 104 | xmlInput = Loader::removeLuaTags(xmlInput); |
---|
[8079] | 105 | } |
---|
[5695] | 106 | } |
---|
[1505] | 107 | |
---|
| 108 | try |
---|
| 109 | { |
---|
[8858] | 110 | if(bVerbose) |
---|
[7648] | 111 | { |
---|
[8858] | 112 | orxout(user_info) << "Start loading " << file->getFilename() << "..." << endl; |
---|
[10624] | 113 | orxout(internal_info, context::loader) << "Mask: " << this->currentMask_ << endl; |
---|
[7648] | 114 | } |
---|
| 115 | else |
---|
| 116 | { |
---|
[8858] | 117 | orxout(verbose, context::loader) << "Start loading " << file->getFilename() << "..." << endl; |
---|
[10624] | 118 | orxout(verbose_more, context::loader) << "Mask: " << this->currentMask_ << endl; |
---|
[7648] | 119 | } |
---|
[1505] | 120 | |
---|
[5695] | 121 | ticpp::Document xmlfile(file->getFilename()); |
---|
| 122 | xmlfile.Parse(xmlInput, true); |
---|
[1505] | 123 | |
---|
| 124 | ticpp::Element rootElement; |
---|
| 125 | rootElement.SetAttribute("name", "root"); |
---|
| 126 | rootElement.SetAttribute("bAutogenerated", true); |
---|
| 127 | |
---|
| 128 | for (ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++) |
---|
| 129 | rootElement.InsertEndChild(*child); |
---|
| 130 | |
---|
[8858] | 131 | orxout(verbose, context::loader) << " creating root-namespace..." << endl; |
---|
[9667] | 132 | Namespace* rootNamespace = new Namespace(Context::getRootContext()); |
---|
[1505] | 133 | rootNamespace->setLoaderIndentation(" "); |
---|
[2087] | 134 | rootNamespace->setFile(file); |
---|
[1505] | 135 | rootNamespace->setRoot(true); |
---|
| 136 | rootNamespace->XMLPort(rootElement, XMLPort::LoadObject); |
---|
| 137 | |
---|
[8858] | 138 | if(bVerbose) |
---|
| 139 | orxout(user_info) << "Finished loading " << file->getFilename() << '.' << endl; |
---|
[7648] | 140 | else |
---|
[8858] | 141 | orxout(verbose, context::loader) << "Finished loading " << file->getFilename() << '.' << endl; |
---|
[1505] | 142 | |
---|
[8858] | 143 | orxout(verbose, context::loader) << "Namespace-tree:" << '\n' << rootNamespace->toString(" ") << endl; |
---|
[1505] | 144 | |
---|
| 145 | return true; |
---|
| 146 | } |
---|
[2171] | 147 | catch (ticpp::Exception& ex) |
---|
[1505] | 148 | { |
---|
[8858] | 149 | orxout(user_error, context::loader) << endl; |
---|
| 150 | orxout(user_error, context::loader) << "An XML-error occurred in Loader.cc while loading " << file->getFilename() << ':' << endl; |
---|
[10269] | 151 | OutputLevel ticpplevel = user_error; |
---|
[10264] | 152 | if (lineTrace->size() > 0) |
---|
| 153 | { |
---|
[10269] | 154 | ticpplevel = internal_error; |
---|
[10264] | 155 | //Extract the line number from the exception |
---|
| 156 | std::string tempstring(ex.what()); |
---|
| 157 | std::string::size_type pos = tempstring.find("\nLine: "); |
---|
| 158 | if (pos != std::string::npos) |
---|
| 159 | { |
---|
| 160 | std::istringstream istr(tempstring.substr(pos + 7)); |
---|
| 161 | size_t line; |
---|
| 162 | istr >> line; |
---|
| 163 | if (line <= lineTrace->size()) |
---|
| 164 | { |
---|
[10265] | 165 | std::vector<std::pair<std::string, size_t> > linesources = lineTrace->at(line - 1); |
---|
[10269] | 166 | std::ostringstream message; |
---|
| 167 | message << "Possible sources of error:" << endl; |
---|
[10265] | 168 | for (std::vector<std::pair<std::string, size_t> >::iterator it = linesources.begin(); it != linesources.end(); ++it) |
---|
[10264] | 169 | { |
---|
[10269] | 170 | message << it->first << ", Line " << it->second << endl; |
---|
| 171 | } |
---|
| 172 | orxout(user_error, context::loader) << message.str() << endl; |
---|
[10264] | 173 | } |
---|
| 174 | } |
---|
| 175 | } |
---|
[10269] | 176 | orxout(ticpplevel, context::loader) << ex.what() << endl; |
---|
| 177 | orxout(user_error, context::loader) << "Loading aborted." << endl; |
---|
[2171] | 178 | } |
---|
| 179 | catch (Exception& ex) |
---|
| 180 | { |
---|
[8858] | 181 | orxout(user_error, context::loader) << endl; |
---|
| 182 | orxout(user_error, context::loader) << "A loading-error occurred in Loader.cc while loading " << file->getFilename() << ':' << endl; |
---|
| 183 | orxout(user_error, context::loader) << ex.what() << endl; |
---|
| 184 | orxout(user_error, context::loader) << "Loading aborted." << endl; |
---|
[2171] | 185 | } |
---|
[5747] | 186 | catch (...) |
---|
[2171] | 187 | { |
---|
[8858] | 188 | orxout(user_error, context::loader) << endl; |
---|
| 189 | orxout(user_error, context::loader) << "An error occurred in Loader.cc while loading " << file->getFilename() << ':' << endl; |
---|
| 190 | orxout(user_error, context::loader) << Exception::handleMessage() << endl; |
---|
| 191 | orxout(user_error, context::loader) << "Loading aborted." << endl; |
---|
[1505] | 192 | } |
---|
[10264] | 193 | //The Tardis' version of boost is too old... |
---|
| 194 | #if BOOST_VERSION >= 104600 |
---|
| 195 | boost::filesystem::path temppath = boost::filesystem::temp_directory_path() / "orxonoxml.xml"; |
---|
| 196 | //Need binary mode, because xmlInput already has \r\n for windows |
---|
| 197 | boost::filesystem::ofstream outfile(temppath, std::ios_base::binary | std::ios_base::out); |
---|
| 198 | outfile << xmlInput; |
---|
| 199 | outfile.flush(); |
---|
| 200 | outfile.close(); |
---|
[10269] | 201 | orxout(internal_error, context::loader) << "The complete xml file has been saved to " << temppath << endl; |
---|
[10264] | 202 | #endif |
---|
| 203 | return false; |
---|
[1505] | 204 | } |
---|
| 205 | |
---|
[2087] | 206 | void Loader::unload(const XMLFile* file, const ClassTreeMask& mask) |
---|
[1505] | 207 | { |
---|
[2087] | 208 | if (!file) |
---|
[1755] | 209 | return; |
---|
[1747] | 210 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; ) |
---|
[1505] | 211 | { |
---|
[2087] | 212 | if ((it->getFile() == file) && mask.isIncluded(it->getIdentifier())) |
---|
[5929] | 213 | (it++)->destroy(); |
---|
[1505] | 214 | else |
---|
| 215 | ++it; |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
[8079] | 219 | bool Loader::getLuaTags(const std::string& text, std::map<size_t, bool>& luaTags) |
---|
[5695] | 220 | { |
---|
[8079] | 221 | // fill map with all Lua tags |
---|
[5695] | 222 | { |
---|
| 223 | size_t pos = 0; |
---|
| 224 | while ((pos = text.find("<?lua", pos)) != std::string::npos) |
---|
| 225 | luaTags[pos++] = true; |
---|
| 226 | } |
---|
| 227 | { |
---|
| 228 | size_t pos = 0; |
---|
| 229 | while ((pos = text.find("?>", pos)) != std::string::npos) |
---|
| 230 | luaTags[pos++] = false; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | // erase all tags from the map that are between two quotes |
---|
[10273] | 234 | // that means occurrences like "..<?lua.." and "..?>.." would be deleted |
---|
| 235 | // however occurrences of lua tags within quotas are retained: ".. <?lua ... ?> .. " |
---|
[5695] | 236 | { |
---|
| 237 | std::map<size_t, bool>::iterator it = luaTags.begin(); |
---|
[10272] | 238 | bool bBetweenQuotes = false; |
---|
| 239 | size_t pos = 0; |
---|
| 240 | while ((pos = getNextQuote(text, pos)) != std::string::npos) |
---|
[5695] | 241 | { |
---|
[10272] | 242 | while ((it != luaTags.end()) && (it->first < pos)) |
---|
[5695] | 243 | { |
---|
[10272] | 244 | if (bBetweenQuotes) |
---|
| 245 | { |
---|
[10273] | 246 | std::map<size_t, bool>::iterator it2 = it; |
---|
[10272] | 247 | it2++; |
---|
| 248 | if (it->second && !(it2->second) && it2->first < pos) |
---|
[10273] | 249 | std::advance(it, 2); |
---|
[10272] | 250 | else |
---|
| 251 | luaTags.erase(it++); |
---|
| 252 | } |
---|
| 253 | else |
---|
| 254 | ++it; |
---|
[5695] | 255 | } |
---|
[10272] | 256 | bBetweenQuotes = !bBetweenQuotes; |
---|
| 257 | pos++; |
---|
[5695] | 258 | } |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | // check whether on every opening <?lua tag a closing ?> tag follows |
---|
| 262 | { |
---|
| 263 | bool expectedValue = true; |
---|
| 264 | for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it) |
---|
| 265 | { |
---|
| 266 | if (it->second == expectedValue) |
---|
| 267 | expectedValue = !expectedValue; |
---|
| 268 | else |
---|
| 269 | { |
---|
| 270 | expectedValue = false; |
---|
| 271 | break; |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | if (!expectedValue) |
---|
| 275 | { |
---|
[10264] | 276 | orxout(internal_error, context::loader) << "Error parsing file: lua tags not matching" << endl; |
---|
[8079] | 277 | // TODO: error handling |
---|
[8858] | 278 | return false; |
---|
[5695] | 279 | } |
---|
| 280 | } |
---|
| 281 | |
---|
[8079] | 282 | return true; |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | std::string Loader::replaceLuaTags(const std::string& text) |
---|
| 286 | { |
---|
| 287 | // create a map with all lua tags |
---|
| 288 | std::map<size_t, bool> luaTags; |
---|
| 289 | if (!getLuaTags(text, luaTags)) |
---|
| 290 | return ""; |
---|
| 291 | |
---|
[5695] | 292 | // Use a stringstream object to speed up the parsing |
---|
| 293 | std::ostringstream output; |
---|
| 294 | |
---|
| 295 | // cut the original string into pieces and put them together with print() instead of lua tags |
---|
| 296 | { |
---|
| 297 | std::map<size_t, bool>::iterator it = luaTags.begin(); |
---|
| 298 | bool bInPrintFunction = true; |
---|
| 299 | size_t start = 0; |
---|
| 300 | size_t end = 0; |
---|
| 301 | |
---|
| 302 | do |
---|
| 303 | { |
---|
| 304 | if (it != luaTags.end()) |
---|
[6417] | 305 | end = (it++)->first; |
---|
[5695] | 306 | else |
---|
| 307 | end = std::string::npos; |
---|
| 308 | |
---|
| 309 | unsigned int equalSignCounter = 0; |
---|
| 310 | |
---|
| 311 | if (bInPrintFunction) |
---|
| 312 | { |
---|
| 313 | // count ['='[ and ]'='] and replace tags with print([[ and ]]) |
---|
[6417] | 314 | const std::string& temp = text.substr(start, end - start); |
---|
[5695] | 315 | { |
---|
| 316 | size_t pos = 0; |
---|
| 317 | while ((pos = temp.find('[', pos)) != std::string::npos) |
---|
| 318 | { |
---|
| 319 | unsigned int tempCounter = 1; |
---|
| 320 | size_t tempPos = pos++; |
---|
[6422] | 321 | while (temp[++tempPos] == '=') |
---|
[5695] | 322 | { |
---|
| 323 | tempCounter++; |
---|
| 324 | } |
---|
[6422] | 325 | if (temp[tempPos] != '[') |
---|
[5695] | 326 | { |
---|
| 327 | tempCounter = 0; |
---|
| 328 | } |
---|
[6422] | 329 | else if (tempCounter == 0) |
---|
[5695] | 330 | { |
---|
| 331 | tempCounter = 1; |
---|
| 332 | } |
---|
| 333 | if (tempCounter > equalSignCounter) |
---|
| 334 | equalSignCounter = tempCounter; |
---|
| 335 | } |
---|
| 336 | } |
---|
| 337 | { |
---|
| 338 | size_t pos = 0; |
---|
| 339 | while ((pos = temp.find(']', pos)) != std::string::npos) |
---|
| 340 | { |
---|
| 341 | unsigned int tempCounter = 1; |
---|
| 342 | size_t tempPos = pos++; |
---|
[6422] | 343 | while (temp[++tempPos] == '=') |
---|
[5695] | 344 | { |
---|
| 345 | tempCounter++; |
---|
| 346 | } |
---|
[6422] | 347 | if (temp[tempPos] != ']') |
---|
[5695] | 348 | { |
---|
| 349 | tempCounter = 0; |
---|
| 350 | } |
---|
[6422] | 351 | else if (tempCounter == 0) |
---|
[5695] | 352 | { |
---|
| 353 | tempCounter = 1; |
---|
| 354 | } |
---|
| 355 | if (tempCounter > equalSignCounter) |
---|
| 356 | equalSignCounter = tempCounter; |
---|
| 357 | } |
---|
| 358 | } |
---|
[6417] | 359 | std::string equalSigns; |
---|
[6422] | 360 | for (unsigned int i = 0; i < equalSignCounter; i++) |
---|
[5695] | 361 | { |
---|
[6417] | 362 | equalSigns += '='; |
---|
[5695] | 363 | } |
---|
[10264] | 364 | //A newline directly after square brackets is ignored. To make sure that the string is printed |
---|
| 365 | //exactly as it is, including newlines at the beginning, insert a space after the brackets. |
---|
[10278] | 366 | bool needsExtraSpace = false; |
---|
| 367 | if (temp.size() > 0 && (temp[0] == '\n' || temp[0] == '\r')) // begins with \n or \r (a line break) |
---|
| 368 | needsExtraSpace = true; |
---|
| 369 | output << "print([" + equalSigns + (needsExtraSpace ? "[ " : "[") + temp + ']' + equalSigns +"])"; |
---|
[5695] | 370 | start = end + 5; |
---|
| 371 | } |
---|
| 372 | else |
---|
| 373 | { |
---|
| 374 | output << text.substr(start, end - start); |
---|
| 375 | start = end + 2; |
---|
| 376 | } |
---|
| 377 | |
---|
| 378 | bInPrintFunction = !bInPrintFunction; |
---|
| 379 | } |
---|
| 380 | while (end != std::string::npos); |
---|
| 381 | } |
---|
| 382 | |
---|
| 383 | return output.str(); |
---|
| 384 | } |
---|
[8079] | 385 | |
---|
| 386 | std::string Loader::removeLuaTags(const std::string& text) |
---|
| 387 | { |
---|
| 388 | // create a map with all lua tags |
---|
| 389 | std::map<size_t, bool> luaTags; |
---|
| 390 | if (!getLuaTags(text, luaTags)) |
---|
| 391 | return ""; |
---|
| 392 | |
---|
| 393 | // Use a stringstream object to speed up the concatenation |
---|
| 394 | std::ostringstream output; |
---|
| 395 | |
---|
| 396 | // cut the original string into pieces and only write the non Lua parts |
---|
| 397 | std::map<size_t, bool>::iterator it = luaTags.begin(); |
---|
| 398 | bool bLuaCode = false; |
---|
| 399 | size_t start = 0; |
---|
| 400 | size_t end = 0; |
---|
| 401 | |
---|
| 402 | do |
---|
| 403 | { |
---|
| 404 | if (it != luaTags.end()) |
---|
| 405 | end = (it++)->first; |
---|
| 406 | else |
---|
| 407 | end = std::string::npos; |
---|
| 408 | |
---|
| 409 | if (!bLuaCode) |
---|
| 410 | { |
---|
| 411 | output << text.substr(start, end - start); |
---|
| 412 | start = end + 5; |
---|
| 413 | } |
---|
| 414 | else |
---|
[10264] | 415 | { |
---|
| 416 | //Preserve the amount of lines, otherwise the linenumber from the xml parse error is useless |
---|
| 417 | std::string tempstring = text.substr(start, end - start); |
---|
| 418 | output << std::string(std::count(tempstring.begin(), tempstring.end(), '\n'), '\n'); |
---|
[8079] | 419 | start = end + 2; |
---|
[10264] | 420 | } |
---|
[8079] | 421 | |
---|
| 422 | bLuaCode = !bLuaCode; |
---|
| 423 | } |
---|
| 424 | while (end != std::string::npos); |
---|
| 425 | |
---|
| 426 | return output.str(); |
---|
| 427 | } |
---|
[1505] | 428 | } |
---|