| 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" | 
|---|
| 30 |  | 
|---|
| 31 | #include <sstream> | 
|---|
| 32 | #include <tinyxml/ticpp.h> | 
|---|
| 33 | #include <boost/scoped_ptr.hpp> | 
|---|
| 34 |  | 
|---|
| 35 | #include "util/Debug.h" | 
|---|
| 36 | #include "util/Exception.h" | 
|---|
| 37 | #include "util/StringUtils.h" | 
|---|
| 38 | #include "BaseObject.h" | 
|---|
| 39 | #include "Iterator.h" | 
|---|
| 40 | #include "ObjectList.h" | 
|---|
| 41 | #include "LuaState.h" | 
|---|
| 42 | #include "Namespace.h" | 
|---|
| 43 | #include "Resource.h" | 
|---|
| 44 | #include "XMLFile.h" | 
|---|
| 45 |  | 
|---|
| 46 | namespace orxonox | 
|---|
| 47 | { | 
|---|
| 48 | std::vector<std::pair<const XMLFile*, ClassTreeMask> > Loader::files_s; | 
|---|
| 49 | ClassTreeMask Loader::currentMask_s; | 
|---|
| 50 |  | 
|---|
| 51 | bool Loader::open(const XMLFile* file, const ClassTreeMask& mask) | 
|---|
| 52 | { | 
|---|
| 53 | Loader::add(file, mask); | 
|---|
| 54 | return Loader::load(file, mask); | 
|---|
| 55 | } | 
|---|
| 56 |  | 
|---|
| 57 | void Loader::close() | 
|---|
| 58 | { | 
|---|
| 59 | Loader::unload(); | 
|---|
| 60 | Loader::files_s.clear(); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | void Loader::close(const XMLFile* file) | 
|---|
| 64 | { | 
|---|
| 65 | Loader::unload(file); | 
|---|
| 66 | Loader::remove(file); | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | void Loader::add(const XMLFile* file, const ClassTreeMask& mask) | 
|---|
| 70 | { | 
|---|
| 71 | if (!file) | 
|---|
| 72 | return; | 
|---|
| 73 | Loader::files_s.insert(Loader::files_s.end(), std::pair<const XMLFile*, ClassTreeMask>(file, mask)); | 
|---|
| 74 | } | 
|---|
| 75 |  | 
|---|
| 76 | void Loader::remove(const XMLFile* file) | 
|---|
| 77 | { | 
|---|
| 78 | if (!file) | 
|---|
| 79 | return; | 
|---|
| 80 | for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it) | 
|---|
| 81 | { | 
|---|
| 82 | if (it->first == file) | 
|---|
| 83 | { | 
|---|
| 84 | Loader::files_s.erase(it); | 
|---|
| 85 | break; | 
|---|
| 86 | } | 
|---|
| 87 | } | 
|---|
| 88 | } | 
|---|
| 89 |  | 
|---|
| 90 | /** | 
|---|
| 91 | @brief | 
|---|
| 92 | Loads all opened files, while conforming to the restrictions given by the input ClassTreeMask. | 
|---|
| 93 | @param mask | 
|---|
| 94 | A ClassTreeMask, which defines which types of classes are loaded and which aren't. | 
|---|
| 95 | @param verbose | 
|---|
| 96 | Whether the loader is verbose (prints its progress in a low output level) or not. | 
|---|
| 97 | @return | 
|---|
| 98 | Returns true if successful. | 
|---|
| 99 | */ | 
|---|
| 100 | bool Loader::load(const ClassTreeMask& mask, bool verbose) | 
|---|
| 101 | { | 
|---|
| 102 | bool success = true; | 
|---|
| 103 | for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it) | 
|---|
| 104 | if (!Loader::load(it->first, it->second * mask, verbose)) | 
|---|
| 105 | success = false; | 
|---|
| 106 |  | 
|---|
| 107 | return success; | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | void Loader::unload(const ClassTreeMask& mask) | 
|---|
| 111 | { | 
|---|
| 112 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); ) | 
|---|
| 113 | { | 
|---|
| 114 | if (mask.isIncluded(it->getIdentifier())) | 
|---|
| 115 | (it++)->destroy(); | 
|---|
| 116 | else | 
|---|
| 117 | ++it; | 
|---|
| 118 | } | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | /** | 
|---|
| 122 | @brief | 
|---|
| 123 | Reloads all opened files, while conforming to the restrictions given by the input ClassTreeMask. | 
|---|
| 124 | @param mask | 
|---|
| 125 | A ClassTreeMask, which defines which types of classes are reloaded and which aren't. | 
|---|
| 126 | @param verbose | 
|---|
| 127 | Whether the loader is verbose (prints its progress in a low output level) or not. | 
|---|
| 128 | @return | 
|---|
| 129 | Returns true if successful. | 
|---|
| 130 | */ | 
|---|
| 131 | bool Loader::reload(const ClassTreeMask& mask, bool verbose) | 
|---|
| 132 | { | 
|---|
| 133 | Loader::unload(mask); | 
|---|
| 134 | return Loader::load(mask, verbose); | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | /** | 
|---|
| 138 | @brief | 
|---|
| 139 | Loads the input file, while conforming to the restrictions given by the input ClassTreeMask. | 
|---|
| 140 | @param file | 
|---|
| 141 | The file to be loaded. | 
|---|
| 142 | @param mask | 
|---|
| 143 | A ClassTreeMask, which defines which types of classes are loaded and which aren't. | 
|---|
| 144 | @param verbose | 
|---|
| 145 | Whether the loader is verbose (prints its progress in a low output level) or not. | 
|---|
| 146 | @return | 
|---|
| 147 | Returns true if successful. | 
|---|
| 148 | */ | 
|---|
| 149 | bool Loader::load(const XMLFile* file, const ClassTreeMask& mask, bool verbose) | 
|---|
| 150 | { | 
|---|
| 151 | if (!file) | 
|---|
| 152 | return false; | 
|---|
| 153 |  | 
|---|
| 154 | Loader::currentMask_s = file->getMask() * mask; | 
|---|
| 155 |  | 
|---|
| 156 | std::string xmlInput; | 
|---|
| 157 | if (file->getLuaSupport()) | 
|---|
| 158 | { | 
|---|
| 159 | // Use the LuaState to replace the XML tags (calls our function) | 
|---|
| 160 | scoped_ptr<LuaState> luaState(new LuaState()); | 
|---|
| 161 | luaState->setIncludeParser(&Loader::replaceLuaTags); | 
|---|
| 162 | luaState->includeFile(file->getFilename()); | 
|---|
| 163 | xmlInput = luaState->getOutput().str(); | 
|---|
| 164 | } | 
|---|
| 165 | else | 
|---|
| 166 | { | 
|---|
| 167 | shared_ptr<ResourceInfo> info = Resource::getInfo(file->getFilename()); | 
|---|
| 168 | if (info == NULL) | 
|---|
| 169 | { | 
|---|
| 170 | COUT(1) << "Error: Could not find XML file '" << file->getFilename() << "'." << std::endl; | 
|---|
| 171 | return false; | 
|---|
| 172 | } | 
|---|
| 173 | xmlInput = Resource::open(file->getFilename())->getAsString(); | 
|---|
| 174 | } | 
|---|
| 175 |  | 
|---|
| 176 | try | 
|---|
| 177 | { | 
|---|
| 178 | if(verbose) | 
|---|
| 179 | { | 
|---|
| 180 | COUT(0) << "Start loading " << file->getFilename() << "..." << std::endl; | 
|---|
| 181 | COUT(3) << "Mask: " << Loader::currentMask_s << std::endl; | 
|---|
| 182 | } | 
|---|
| 183 | else | 
|---|
| 184 | { | 
|---|
| 185 | COUT(4) << "Start loading " << file->getFilename() << "..." << std::endl; | 
|---|
| 186 | COUT(4) << "Mask: " << Loader::currentMask_s << std::endl; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | ticpp::Document xmlfile(file->getFilename()); | 
|---|
| 190 | xmlfile.Parse(xmlInput, true); | 
|---|
| 191 |  | 
|---|
| 192 | ticpp::Element rootElement; | 
|---|
| 193 | rootElement.SetAttribute("name", "root"); | 
|---|
| 194 | rootElement.SetAttribute("bAutogenerated", true); | 
|---|
| 195 |  | 
|---|
| 196 | for (ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++) | 
|---|
| 197 | rootElement.InsertEndChild(*child); | 
|---|
| 198 |  | 
|---|
| 199 | COUT(4) << "  creating root-namespace..." << std::endl; | 
|---|
| 200 | Namespace* rootNamespace = new Namespace(0); | 
|---|
| 201 | rootNamespace->setLoaderIndentation("    "); | 
|---|
| 202 | rootNamespace->setFile(file); | 
|---|
| 203 | rootNamespace->setNamespace(rootNamespace); | 
|---|
| 204 | rootNamespace->setRoot(true); | 
|---|
| 205 | rootNamespace->XMLPort(rootElement, XMLPort::LoadObject); | 
|---|
| 206 |  | 
|---|
| 207 | if(verbose) | 
|---|
| 208 | COUT(0) << "Finished loading " << file->getFilename() << '.' << std::endl; | 
|---|
| 209 | else | 
|---|
| 210 | COUT(4) << "Finished loading " << file->getFilename() << '.' << std::endl; | 
|---|
| 211 |  | 
|---|
| 212 | COUT(4) << "Namespace-tree:" << std::endl << rootNamespace->toString("  ") << std::endl; | 
|---|
| 213 |  | 
|---|
| 214 | return true; | 
|---|
| 215 | } | 
|---|
| 216 | catch (ticpp::Exception& ex) | 
|---|
| 217 | { | 
|---|
| 218 | COUT(1) << std::endl; | 
|---|
| 219 | COUT(1) << "An XML-error occurred in Loader.cc while loading " << file->getFilename() << ':' << std::endl; | 
|---|
| 220 | COUT(1) << ex.what() << std::endl; | 
|---|
| 221 | COUT(1) << "Loading aborted." << std::endl; | 
|---|
| 222 | return false; | 
|---|
| 223 | } | 
|---|
| 224 | catch (Exception& ex) | 
|---|
| 225 | { | 
|---|
| 226 | COUT(1) << std::endl; | 
|---|
| 227 | COUT(1) << "A loading-error occurred in Loader.cc while loading " << file->getFilename() << ':' << std::endl; | 
|---|
| 228 | COUT(1) << ex.what() << std::endl; | 
|---|
| 229 | COUT(1) << "Loading aborted." << std::endl; | 
|---|
| 230 | return false; | 
|---|
| 231 | } | 
|---|
| 232 | catch (...) | 
|---|
| 233 | { | 
|---|
| 234 | COUT(1) << std::endl; | 
|---|
| 235 | COUT(1) << "An error occurred in Loader.cc while loading " << file->getFilename() << ':' << std::endl; | 
|---|
| 236 | COUT(1) << Exception::handleMessage() << std::endl; | 
|---|
| 237 | COUT(1) << "Loading aborted." << std::endl; | 
|---|
| 238 | return false; | 
|---|
| 239 | } | 
|---|
| 240 | } | 
|---|
| 241 |  | 
|---|
| 242 | void Loader::unload(const XMLFile* file, const ClassTreeMask& mask) | 
|---|
| 243 | { | 
|---|
| 244 | if (!file) | 
|---|
| 245 | return; | 
|---|
| 246 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; ) | 
|---|
| 247 | { | 
|---|
| 248 | if ((it->getFile() == file) && mask.isIncluded(it->getIdentifier())) | 
|---|
| 249 | (it++)->destroy(); | 
|---|
| 250 | else | 
|---|
| 251 | ++it; | 
|---|
| 252 | } | 
|---|
| 253 | } | 
|---|
| 254 |  | 
|---|
| 255 | /** | 
|---|
| 256 | @brief | 
|---|
| 257 | Reloads the input file, while conforming to the restrictions given by the input ClassTreeMask. | 
|---|
| 258 | @param file | 
|---|
| 259 | The file to be reloaded. | 
|---|
| 260 | @param mask | 
|---|
| 261 | A ClassTreeMask, which defines which types of classes are reloaded and which aren't. | 
|---|
| 262 | @param verbose | 
|---|
| 263 | Whether the loader is verbose (prints its progress in a low output level) or not. | 
|---|
| 264 | @return | 
|---|
| 265 | Returns true if successful. | 
|---|
| 266 | */ | 
|---|
| 267 | bool Loader::reload(const XMLFile* file, const ClassTreeMask& mask, bool verbose) | 
|---|
| 268 | { | 
|---|
| 269 | Loader::unload(file, mask); | 
|---|
| 270 | return Loader::load(file, mask, verbose); | 
|---|
| 271 | } | 
|---|
| 272 |  | 
|---|
| 273 | std::string Loader::replaceLuaTags(const std::string& text) | 
|---|
| 274 | { | 
|---|
| 275 | // create map with all Lua tags | 
|---|
| 276 | std::map<size_t, bool> luaTags; | 
|---|
| 277 | { | 
|---|
| 278 | size_t pos = 0; | 
|---|
| 279 | while ((pos = text.find("<?lua", pos)) != std::string::npos) | 
|---|
| 280 | luaTags[pos++] = true; | 
|---|
| 281 | } | 
|---|
| 282 | { | 
|---|
| 283 | size_t pos = 0; | 
|---|
| 284 | while ((pos = text.find("?>", pos)) != std::string::npos) | 
|---|
| 285 | luaTags[pos++] = false; | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 | // erase all tags from the map that are between two quotes | 
|---|
| 289 | { | 
|---|
| 290 | std::map<size_t, bool>::iterator it = luaTags.begin(); | 
|---|
| 291 | std::map<size_t, bool>::iterator it2 = it; | 
|---|
| 292 | bool bBetweenQuotes = false; | 
|---|
| 293 | size_t pos = 0; | 
|---|
| 294 | while ((pos = getNextQuote(text, pos)) != std::string::npos) | 
|---|
| 295 | { | 
|---|
| 296 | while ((it != luaTags.end()) && (it->first < pos)) | 
|---|
| 297 | { | 
|---|
| 298 | if (bBetweenQuotes) | 
|---|
| 299 | { | 
|---|
| 300 | it2++; | 
|---|
| 301 | if (it->second && !(it2->second) && it2->first < pos) | 
|---|
| 302 | it = ++it2; | 
|---|
| 303 | else | 
|---|
| 304 | luaTags.erase(it++); | 
|---|
| 305 | } | 
|---|
| 306 | else | 
|---|
| 307 | ++it; | 
|---|
| 308 | } | 
|---|
| 309 | bBetweenQuotes = !bBetweenQuotes; | 
|---|
| 310 | pos++; | 
|---|
| 311 | } | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | // check whether on every opening <?lua tag a closing ?> tag follows | 
|---|
| 315 | { | 
|---|
| 316 | bool expectedValue = true; | 
|---|
| 317 | for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it) | 
|---|
| 318 | { | 
|---|
| 319 | if (it->second == expectedValue) | 
|---|
| 320 | expectedValue = !expectedValue; | 
|---|
| 321 | else | 
|---|
| 322 | { | 
|---|
| 323 | expectedValue = false; | 
|---|
| 324 | break; | 
|---|
| 325 | } | 
|---|
| 326 | } | 
|---|
| 327 | if (!expectedValue) | 
|---|
| 328 | { | 
|---|
| 329 | COUT(2) << "Warning: Error in level file" << std::endl; | 
|---|
| 330 | // todo: errorhandling | 
|---|
| 331 | return ""; | 
|---|
| 332 | } | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | // Use a stringstream object to speed up the parsing | 
|---|
| 336 | std::ostringstream output; | 
|---|
| 337 |  | 
|---|
| 338 | // cut the original string into pieces and put them together with print() instead of lua tags | 
|---|
| 339 | { | 
|---|
| 340 | std::map<size_t, bool>::iterator it = luaTags.begin(); | 
|---|
| 341 | bool bInPrintFunction = true; | 
|---|
| 342 | size_t start = 0; | 
|---|
| 343 | size_t end = 0; | 
|---|
| 344 |  | 
|---|
| 345 | do | 
|---|
| 346 | { | 
|---|
| 347 | if (it != luaTags.end()) | 
|---|
| 348 | end = (it++)->first; | 
|---|
| 349 | else | 
|---|
| 350 | end = std::string::npos; | 
|---|
| 351 |  | 
|---|
| 352 | unsigned int equalSignCounter = 0; | 
|---|
| 353 |  | 
|---|
| 354 | if (bInPrintFunction) | 
|---|
| 355 | { | 
|---|
| 356 | // count ['='[ and ]'='] and replace tags with print([[ and ]]) | 
|---|
| 357 | const std::string& temp = text.substr(start, end - start); | 
|---|
| 358 | { | 
|---|
| 359 | size_t pos = 0; | 
|---|
| 360 | while ((pos = temp.find('[', pos)) != std::string::npos) | 
|---|
| 361 | { | 
|---|
| 362 | unsigned int tempCounter = 1; | 
|---|
| 363 | size_t tempPos = pos++; | 
|---|
| 364 | while (temp[++tempPos] == '=') | 
|---|
| 365 | { | 
|---|
| 366 | tempCounter++; | 
|---|
| 367 | } | 
|---|
| 368 | if (temp[tempPos] != '[') | 
|---|
| 369 | { | 
|---|
| 370 | tempCounter = 0; | 
|---|
| 371 | } | 
|---|
| 372 | else if (tempCounter == 0) | 
|---|
| 373 | { | 
|---|
| 374 | tempCounter = 1; | 
|---|
| 375 | } | 
|---|
| 376 | if (tempCounter > equalSignCounter) | 
|---|
| 377 | equalSignCounter = tempCounter; | 
|---|
| 378 | } | 
|---|
| 379 | } | 
|---|
| 380 | { | 
|---|
| 381 | size_t pos = 0; | 
|---|
| 382 | while ((pos = temp.find(']', pos)) != std::string::npos) | 
|---|
| 383 | { | 
|---|
| 384 | unsigned int tempCounter = 1; | 
|---|
| 385 | size_t tempPos = pos++; | 
|---|
| 386 | while (temp[++tempPos] == '=') | 
|---|
| 387 | { | 
|---|
| 388 | tempCounter++; | 
|---|
| 389 | } | 
|---|
| 390 | if (temp[tempPos] != ']') | 
|---|
| 391 | { | 
|---|
| 392 | tempCounter = 0; | 
|---|
| 393 | } | 
|---|
| 394 | else if (tempCounter == 0) | 
|---|
| 395 | { | 
|---|
| 396 | tempCounter = 1; | 
|---|
| 397 | } | 
|---|
| 398 | if (tempCounter > equalSignCounter) | 
|---|
| 399 | equalSignCounter = tempCounter; | 
|---|
| 400 | } | 
|---|
| 401 | } | 
|---|
| 402 | std::string equalSigns; | 
|---|
| 403 | for (unsigned int i = 0; i < equalSignCounter; i++) | 
|---|
| 404 | { | 
|---|
| 405 | equalSigns += '='; | 
|---|
| 406 | } | 
|---|
| 407 | output << "print([" + equalSigns + '[' + temp + ']' + equalSigns +"])"; | 
|---|
| 408 | start = end + 5; | 
|---|
| 409 | } | 
|---|
| 410 | else | 
|---|
| 411 | { | 
|---|
| 412 | output << text.substr(start, end - start); | 
|---|
| 413 | start = end + 2; | 
|---|
| 414 | } | 
|---|
| 415 |  | 
|---|
| 416 | bInPrintFunction = !bInPrintFunction; | 
|---|
| 417 | } | 
|---|
| 418 | while (end != std::string::npos); | 
|---|
| 419 | } | 
|---|
| 420 |  | 
|---|
| 421 | return output.str(); | 
|---|
| 422 | } | 
|---|
| 423 | } | 
|---|