| 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 | #include <boost/filesystem.hpp> |
|---|
| 35 | #include <boost/filesystem/fstream.hpp> |
|---|
| 36 | |
|---|
| 37 | #include "util/Output.h" |
|---|
| 38 | #include "util/Exception.h" |
|---|
| 39 | #include "util/StringUtils.h" |
|---|
| 40 | #include "BaseObject.h" |
|---|
| 41 | #include "LuaState.h" |
|---|
| 42 | #include "Namespace.h" |
|---|
| 43 | #include "Resource.h" |
|---|
| 44 | #include "XMLFile.h" |
|---|
| 45 | #include "object/Iterator.h" |
|---|
| 46 | #include "object/ObjectList.h" |
|---|
| 47 | |
|---|
| 48 | namespace orxonox |
|---|
| 49 | { |
|---|
| 50 | std::vector<std::pair<const XMLFile*, ClassTreeMask> > Loader::files_s; |
|---|
| 51 | ClassTreeMask Loader::currentMask_s; |
|---|
| 52 | |
|---|
| 53 | bool Loader::open(const XMLFile* file, const ClassTreeMask& mask, bool bVerbose) |
|---|
| 54 | { |
|---|
| 55 | Loader::add(file, mask); |
|---|
| 56 | return Loader::load(file, mask, bVerbose); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | void Loader::close() |
|---|
| 60 | { |
|---|
| 61 | Loader::unload(); |
|---|
| 62 | Loader::files_s.clear(); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | void Loader::close(const XMLFile* file) |
|---|
| 66 | { |
|---|
| 67 | Loader::unload(file); |
|---|
| 68 | Loader::remove(file); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | void Loader::add(const XMLFile* file, const ClassTreeMask& mask) |
|---|
| 72 | { |
|---|
| 73 | if (!file) |
|---|
| 74 | return; |
|---|
| 75 | Loader::files_s.insert(Loader::files_s.end(), std::pair<const XMLFile*, ClassTreeMask>(file, mask)); |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | void Loader::remove(const XMLFile* file) |
|---|
| 79 | { |
|---|
| 80 | if (!file) |
|---|
| 81 | return; |
|---|
| 82 | for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it) |
|---|
| 83 | { |
|---|
| 84 | if (it->first == file) |
|---|
| 85 | { |
|---|
| 86 | Loader::files_s.erase(it); |
|---|
| 87 | break; |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | @brief |
|---|
| 94 | Loads all opened files, while conforming to the restrictions given by the input ClassTreeMask. |
|---|
| 95 | @param mask |
|---|
| 96 | A ClassTreeMask, which defines which types of classes are loaded and which aren't. |
|---|
| 97 | @param bVerbose |
|---|
| 98 | Whether the loader is verbose (prints its progress in a low output level) or not. |
|---|
| 99 | @return |
|---|
| 100 | Returns true if successful. |
|---|
| 101 | */ |
|---|
| 102 | bool Loader::load(const ClassTreeMask& mask, bool bVerbose) |
|---|
| 103 | { |
|---|
| 104 | bool success = true; |
|---|
| 105 | for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it) |
|---|
| 106 | if (!Loader::load(it->first, it->second * mask, bVerbose)) |
|---|
| 107 | success = false; |
|---|
| 108 | |
|---|
| 109 | return success; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void Loader::unload(const ClassTreeMask& mask) |
|---|
| 113 | { |
|---|
| 114 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); ) |
|---|
| 115 | { |
|---|
| 116 | if (mask.isIncluded(it->getIdentifier())) |
|---|
| 117 | (it++)->destroy(); |
|---|
| 118 | else |
|---|
| 119 | ++it; |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | @brief |
|---|
| 125 | Reloads all opened files, while conforming to the restrictions given by the input ClassTreeMask. |
|---|
| 126 | @param mask |
|---|
| 127 | A ClassTreeMask, which defines which types of classes are reloaded and which aren't. |
|---|
| 128 | @param bVerbose |
|---|
| 129 | Whether the loader is verbose (prints its progress in a low output level) or not. |
|---|
| 130 | @return |
|---|
| 131 | Returns true if successful. |
|---|
| 132 | */ |
|---|
| 133 | bool Loader::reload(const ClassTreeMask& mask, bool bVerbose) |
|---|
| 134 | { |
|---|
| 135 | Loader::unload(mask); |
|---|
| 136 | return Loader::load(mask, bVerbose); |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | /** |
|---|
| 140 | @brief |
|---|
| 141 | Loads the input file, while conforming to the restrictions given by the input ClassTreeMask. |
|---|
| 142 | @param file |
|---|
| 143 | The file to be loaded. |
|---|
| 144 | @param mask |
|---|
| 145 | A ClassTreeMask, which defines which types of classes are loaded and which aren't. |
|---|
| 146 | @param bVerbose |
|---|
| 147 | Whether the loader is verbose (prints its progress in a low output level) or not. |
|---|
| 148 | @param bRemoveLuaTags |
|---|
| 149 | If true lua tags are just ignored and removed. The default is false. |
|---|
| 150 | @return |
|---|
| 151 | Returns true if successful. |
|---|
| 152 | */ |
|---|
| 153 | bool Loader::load(const XMLFile* file, const ClassTreeMask& mask, bool bVerbose, bool bRemoveLuaTags) |
|---|
| 154 | { |
|---|
| 155 | if (!file) |
|---|
| 156 | return false; |
|---|
| 157 | |
|---|
| 158 | Loader::currentMask_s = file->getMask() * mask; |
|---|
| 159 | |
|---|
| 160 | std::string xmlInput; |
|---|
| 161 | |
|---|
| 162 | shared_ptr<std::vector<std::vector<std::pair<std::string, size_t> > > > lineTrace(new std::vector<std::vector<std::pair<std::string, size_t> > >()); |
|---|
| 163 | lineTrace->reserve(1000); //arbitrary number |
|---|
| 164 | |
|---|
| 165 | |
|---|
| 166 | if (file->getLuaSupport() && !bRemoveLuaTags) |
|---|
| 167 | { |
|---|
| 168 | // Use the LuaState to replace the XML tags (calls our function) |
|---|
| 169 | scoped_ptr<LuaState> luaState(new LuaState()); |
|---|
| 170 | luaState->setTraceMap(lineTrace); |
|---|
| 171 | luaState->setIncludeParser(&Loader::replaceLuaTags); |
|---|
| 172 | luaState->includeFile(file->getFilename()); |
|---|
| 173 | xmlInput = luaState->getOutput().str(); |
|---|
| 174 | } |
|---|
| 175 | else |
|---|
| 176 | { |
|---|
| 177 | shared_ptr<ResourceInfo> info = Resource::getInfo(file->getFilename()); |
|---|
| 178 | if (info == NULL) |
|---|
| 179 | { |
|---|
| 180 | orxout(user_error, context::loader) << "Could not find XML file '" << file->getFilename() << "'." << endl; |
|---|
| 181 | return false; |
|---|
| 182 | } |
|---|
| 183 | xmlInput = Resource::open(file->getFilename())->getAsString(); |
|---|
| 184 | |
|---|
| 185 | if (bRemoveLuaTags) |
|---|
| 186 | { |
|---|
| 187 | // Remove all Lua code. |
|---|
| 188 | // Note: we only need this to speed up parsing of level files at the |
|---|
| 189 | // start of the program. |
|---|
| 190 | // Assumption: the LevelInfo tag does not use Lua scripting |
|---|
| 191 | xmlInput = removeLuaTags(xmlInput); |
|---|
| 192 | } |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | try |
|---|
| 196 | { |
|---|
| 197 | if(bVerbose) |
|---|
| 198 | { |
|---|
| 199 | orxout(user_info) << "Start loading " << file->getFilename() << "..." << endl; |
|---|
| 200 | orxout(internal_info, context::loader) << "Mask: " << Loader::currentMask_s << endl; |
|---|
| 201 | } |
|---|
| 202 | else |
|---|
| 203 | { |
|---|
| 204 | orxout(verbose, context::loader) << "Start loading " << file->getFilename() << "..." << endl; |
|---|
| 205 | orxout(verbose_more, context::loader) << "Mask: " << Loader::currentMask_s << endl; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | ticpp::Document xmlfile(file->getFilename()); |
|---|
| 209 | xmlfile.Parse(xmlInput, true); |
|---|
| 210 | |
|---|
| 211 | ticpp::Element rootElement; |
|---|
| 212 | rootElement.SetAttribute("name", "root"); |
|---|
| 213 | rootElement.SetAttribute("bAutogenerated", true); |
|---|
| 214 | |
|---|
| 215 | for (ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++) |
|---|
| 216 | rootElement.InsertEndChild(*child); |
|---|
| 217 | |
|---|
| 218 | orxout(verbose, context::loader) << " creating root-namespace..." << endl; |
|---|
| 219 | Namespace* rootNamespace = new Namespace(Context::getRootContext()); |
|---|
| 220 | rootNamespace->setLoaderIndentation(" "); |
|---|
| 221 | rootNamespace->setFile(file); |
|---|
| 222 | rootNamespace->setNamespace(rootNamespace); |
|---|
| 223 | rootNamespace->setRoot(true); |
|---|
| 224 | rootNamespace->XMLPort(rootElement, XMLPort::LoadObject); |
|---|
| 225 | |
|---|
| 226 | if(bVerbose) |
|---|
| 227 | orxout(user_info) << "Finished loading " << file->getFilename() << '.' << endl; |
|---|
| 228 | else |
|---|
| 229 | orxout(verbose, context::loader) << "Finished loading " << file->getFilename() << '.' << endl; |
|---|
| 230 | |
|---|
| 231 | orxout(verbose, context::loader) << "Namespace-tree:" << '\n' << rootNamespace->toString(" ") << endl; |
|---|
| 232 | |
|---|
| 233 | return true; |
|---|
| 234 | } |
|---|
| 235 | catch (ticpp::Exception& ex) |
|---|
| 236 | { |
|---|
| 237 | orxout(user_error, context::loader) << endl; |
|---|
| 238 | orxout(user_error, context::loader) << "An XML-error occurred in Loader.cc while loading " << file->getFilename() << ':' << endl; |
|---|
| 239 | OutputLevel ticpplevel = user_error; |
|---|
| 240 | if (lineTrace->size() > 0) |
|---|
| 241 | { |
|---|
| 242 | ticpplevel = internal_error; |
|---|
| 243 | //Extract the line number from the exception |
|---|
| 244 | std::string tempstring(ex.what()); |
|---|
| 245 | std::string::size_type pos = tempstring.find("\nLine: "); |
|---|
| 246 | if (pos != std::string::npos) |
|---|
| 247 | { |
|---|
| 248 | std::istringstream istr(tempstring.substr(pos + 7)); |
|---|
| 249 | size_t line; |
|---|
| 250 | istr >> line; |
|---|
| 251 | if (line <= lineTrace->size()) |
|---|
| 252 | { |
|---|
| 253 | std::vector<std::pair<std::string, size_t> > linesources = lineTrace->at(line - 1); |
|---|
| 254 | std::ostringstream message; |
|---|
| 255 | message << "Possible sources of error:" << endl; |
|---|
| 256 | for (std::vector<std::pair<std::string, size_t> >::iterator it = linesources.begin(); it != linesources.end(); ++it) |
|---|
| 257 | { |
|---|
| 258 | message << it->first << ", Line " << it->second << endl; |
|---|
| 259 | } |
|---|
| 260 | orxout(user_error, context::loader) << message.str() << endl; |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | } |
|---|
| 264 | orxout(ticpplevel, context::loader) << ex.what() << endl; |
|---|
| 265 | orxout(user_error, context::loader) << "Loading aborted." << endl; |
|---|
| 266 | } |
|---|
| 267 | catch (Exception& ex) |
|---|
| 268 | { |
|---|
| 269 | orxout(user_error, context::loader) << endl; |
|---|
| 270 | orxout(user_error, context::loader) << "A loading-error occurred in Loader.cc while loading " << file->getFilename() << ':' << endl; |
|---|
| 271 | orxout(user_error, context::loader) << ex.what() << endl; |
|---|
| 272 | orxout(user_error, context::loader) << "Loading aborted." << endl; |
|---|
| 273 | } |
|---|
| 274 | catch (...) |
|---|
| 275 | { |
|---|
| 276 | orxout(user_error, context::loader) << endl; |
|---|
| 277 | orxout(user_error, context::loader) << "An error occurred in Loader.cc while loading " << file->getFilename() << ':' << endl; |
|---|
| 278 | orxout(user_error, context::loader) << Exception::handleMessage() << endl; |
|---|
| 279 | orxout(user_error, context::loader) << "Loading aborted." << endl; |
|---|
| 280 | } |
|---|
| 281 | //The Tardis' version of boost is too old... |
|---|
| 282 | #if BOOST_VERSION >= 104600 |
|---|
| 283 | boost::filesystem::path temppath = boost::filesystem::temp_directory_path() / "orxonoxml.xml"; |
|---|
| 284 | //Need binary mode, because xmlInput already has \r\n for windows |
|---|
| 285 | boost::filesystem::ofstream outfile(temppath, std::ios_base::binary | std::ios_base::out); |
|---|
| 286 | outfile << xmlInput; |
|---|
| 287 | outfile.flush(); |
|---|
| 288 | outfile.close(); |
|---|
| 289 | orxout(internal_error, context::loader) << "The complete xml file has been saved to " << temppath << endl; |
|---|
| 290 | #endif |
|---|
| 291 | return false; |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | void Loader::unload(const XMLFile* file, const ClassTreeMask& mask) |
|---|
| 295 | { |
|---|
| 296 | if (!file) |
|---|
| 297 | return; |
|---|
| 298 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; ) |
|---|
| 299 | { |
|---|
| 300 | if ((it->getFile() == file) && mask.isIncluded(it->getIdentifier())) |
|---|
| 301 | (it++)->destroy(); |
|---|
| 302 | else |
|---|
| 303 | ++it; |
|---|
| 304 | } |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | /** |
|---|
| 308 | @brief |
|---|
| 309 | Reloads the input file, while conforming to the restrictions given by the input ClassTreeMask. |
|---|
| 310 | @param file |
|---|
| 311 | The file to be reloaded. |
|---|
| 312 | @param mask |
|---|
| 313 | A ClassTreeMask, which defines which types of classes are reloaded and which aren't. |
|---|
| 314 | @param bVerbose |
|---|
| 315 | Whether the loader is verbose (prints its progress in a low output level) or not. |
|---|
| 316 | @return |
|---|
| 317 | Returns true if successful. |
|---|
| 318 | */ |
|---|
| 319 | bool Loader::reload(const XMLFile* file, const ClassTreeMask& mask, bool bVerbose) |
|---|
| 320 | { |
|---|
| 321 | Loader::unload(file, mask); |
|---|
| 322 | return Loader::load(file, mask, bVerbose); |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | bool Loader::getLuaTags(const std::string& text, std::map<size_t, bool>& luaTags) |
|---|
| 326 | { |
|---|
| 327 | // fill map with all Lua tags |
|---|
| 328 | { |
|---|
| 329 | size_t pos = 0; |
|---|
| 330 | while ((pos = text.find("<?lua", pos)) != std::string::npos) |
|---|
| 331 | luaTags[pos++] = true; |
|---|
| 332 | } |
|---|
| 333 | { |
|---|
| 334 | size_t pos = 0; |
|---|
| 335 | while ((pos = text.find("?>", pos)) != std::string::npos) |
|---|
| 336 | luaTags[pos++] = false; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | // erase all tags from the map that are between two quotes |
|---|
| 340 | // that means occurrences like "..<?lua.." and "..?>.." would be deleted |
|---|
| 341 | // however occurrences of lua tags within quotas are retained: ".. <?lua ... ?> .. " |
|---|
| 342 | { |
|---|
| 343 | std::map<size_t, bool>::iterator it = luaTags.begin(); |
|---|
| 344 | bool bBetweenQuotes = false; |
|---|
| 345 | size_t pos = 0; |
|---|
| 346 | while ((pos = getNextQuote(text, pos)) != std::string::npos) |
|---|
| 347 | { |
|---|
| 348 | while ((it != luaTags.end()) && (it->first < pos)) |
|---|
| 349 | { |
|---|
| 350 | if (bBetweenQuotes) |
|---|
| 351 | { |
|---|
| 352 | std::map<size_t, bool>::iterator it2 = it; |
|---|
| 353 | it2++; |
|---|
| 354 | if (it->second && !(it2->second) && it2->first < pos) |
|---|
| 355 | std::advance(it, 2); |
|---|
| 356 | else |
|---|
| 357 | luaTags.erase(it++); |
|---|
| 358 | } |
|---|
| 359 | else |
|---|
| 360 | ++it; |
|---|
| 361 | } |
|---|
| 362 | bBetweenQuotes = !bBetweenQuotes; |
|---|
| 363 | pos++; |
|---|
| 364 | } |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | // check whether on every opening <?lua tag a closing ?> tag follows |
|---|
| 368 | { |
|---|
| 369 | bool expectedValue = true; |
|---|
| 370 | for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it) |
|---|
| 371 | { |
|---|
| 372 | if (it->second == expectedValue) |
|---|
| 373 | expectedValue = !expectedValue; |
|---|
| 374 | else |
|---|
| 375 | { |
|---|
| 376 | expectedValue = false; |
|---|
| 377 | break; |
|---|
| 378 | } |
|---|
| 379 | } |
|---|
| 380 | if (!expectedValue) |
|---|
| 381 | { |
|---|
| 382 | orxout(internal_error, context::loader) << "Error parsing file: lua tags not matching" << endl; |
|---|
| 383 | // TODO: error handling |
|---|
| 384 | return false; |
|---|
| 385 | } |
|---|
| 386 | } |
|---|
| 387 | |
|---|
| 388 | return true; |
|---|
| 389 | } |
|---|
| 390 | |
|---|
| 391 | std::string Loader::replaceLuaTags(const std::string& text) |
|---|
| 392 | { |
|---|
| 393 | // create a map with all lua tags |
|---|
| 394 | std::map<size_t, bool> luaTags; |
|---|
| 395 | if (!getLuaTags(text, luaTags)) |
|---|
| 396 | return ""; |
|---|
| 397 | |
|---|
| 398 | // Use a stringstream object to speed up the parsing |
|---|
| 399 | std::ostringstream output; |
|---|
| 400 | |
|---|
| 401 | // cut the original string into pieces and put them together with print() instead of lua tags |
|---|
| 402 | { |
|---|
| 403 | std::map<size_t, bool>::iterator it = luaTags.begin(); |
|---|
| 404 | bool bInPrintFunction = true; |
|---|
| 405 | size_t start = 0; |
|---|
| 406 | size_t end = 0; |
|---|
| 407 | |
|---|
| 408 | do |
|---|
| 409 | { |
|---|
| 410 | if (it != luaTags.end()) |
|---|
| 411 | end = (it++)->first; |
|---|
| 412 | else |
|---|
| 413 | end = std::string::npos; |
|---|
| 414 | |
|---|
| 415 | unsigned int equalSignCounter = 0; |
|---|
| 416 | |
|---|
| 417 | if (bInPrintFunction) |
|---|
| 418 | { |
|---|
| 419 | // count ['='[ and ]'='] and replace tags with print([[ and ]]) |
|---|
| 420 | const std::string& temp = text.substr(start, end - start); |
|---|
| 421 | { |
|---|
| 422 | size_t pos = 0; |
|---|
| 423 | while ((pos = temp.find('[', pos)) != std::string::npos) |
|---|
| 424 | { |
|---|
| 425 | unsigned int tempCounter = 1; |
|---|
| 426 | size_t tempPos = pos++; |
|---|
| 427 | while (temp[++tempPos] == '=') |
|---|
| 428 | { |
|---|
| 429 | tempCounter++; |
|---|
| 430 | } |
|---|
| 431 | if (temp[tempPos] != '[') |
|---|
| 432 | { |
|---|
| 433 | tempCounter = 0; |
|---|
| 434 | } |
|---|
| 435 | else if (tempCounter == 0) |
|---|
| 436 | { |
|---|
| 437 | tempCounter = 1; |
|---|
| 438 | } |
|---|
| 439 | if (tempCounter > equalSignCounter) |
|---|
| 440 | equalSignCounter = tempCounter; |
|---|
| 441 | } |
|---|
| 442 | } |
|---|
| 443 | { |
|---|
| 444 | size_t pos = 0; |
|---|
| 445 | while ((pos = temp.find(']', pos)) != std::string::npos) |
|---|
| 446 | { |
|---|
| 447 | unsigned int tempCounter = 1; |
|---|
| 448 | size_t tempPos = pos++; |
|---|
| 449 | while (temp[++tempPos] == '=') |
|---|
| 450 | { |
|---|
| 451 | tempCounter++; |
|---|
| 452 | } |
|---|
| 453 | if (temp[tempPos] != ']') |
|---|
| 454 | { |
|---|
| 455 | tempCounter = 0; |
|---|
| 456 | } |
|---|
| 457 | else if (tempCounter == 0) |
|---|
| 458 | { |
|---|
| 459 | tempCounter = 1; |
|---|
| 460 | } |
|---|
| 461 | if (tempCounter > equalSignCounter) |
|---|
| 462 | equalSignCounter = tempCounter; |
|---|
| 463 | } |
|---|
| 464 | } |
|---|
| 465 | std::string equalSigns; |
|---|
| 466 | for (unsigned int i = 0; i < equalSignCounter; i++) |
|---|
| 467 | { |
|---|
| 468 | equalSigns += '='; |
|---|
| 469 | } |
|---|
| 470 | //A newline directly after square brackets is ignored. To make sure that the string is printed |
|---|
| 471 | //exactly as it is, including newlines at the beginning, insert a space after the brackets. |
|---|
| 472 | bool needsExtraSpace = false; |
|---|
| 473 | if (temp.size() > 0 && (temp[0] == '\n' || temp[0] == '\r')) // begins with \n or \r (a line break) |
|---|
| 474 | needsExtraSpace = true; |
|---|
| 475 | output << "print([" + equalSigns + (needsExtraSpace ? "[ " : "[") + temp + ']' + equalSigns +"])"; |
|---|
| 476 | start = end + 5; |
|---|
| 477 | } |
|---|
| 478 | else |
|---|
| 479 | { |
|---|
| 480 | output << text.substr(start, end - start); |
|---|
| 481 | start = end + 2; |
|---|
| 482 | } |
|---|
| 483 | |
|---|
| 484 | bInPrintFunction = !bInPrintFunction; |
|---|
| 485 | } |
|---|
| 486 | while (end != std::string::npos); |
|---|
| 487 | } |
|---|
| 488 | |
|---|
| 489 | return output.str(); |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | std::string Loader::removeLuaTags(const std::string& text) |
|---|
| 493 | { |
|---|
| 494 | // create a map with all lua tags |
|---|
| 495 | std::map<size_t, bool> luaTags; |
|---|
| 496 | if (!getLuaTags(text, luaTags)) |
|---|
| 497 | return ""; |
|---|
| 498 | |
|---|
| 499 | // Use a stringstream object to speed up the concatenation |
|---|
| 500 | std::ostringstream output; |
|---|
| 501 | |
|---|
| 502 | // cut the original string into pieces and only write the non Lua parts |
|---|
| 503 | std::map<size_t, bool>::iterator it = luaTags.begin(); |
|---|
| 504 | bool bLuaCode = false; |
|---|
| 505 | size_t start = 0; |
|---|
| 506 | size_t end = 0; |
|---|
| 507 | |
|---|
| 508 | do |
|---|
| 509 | { |
|---|
| 510 | if (it != luaTags.end()) |
|---|
| 511 | end = (it++)->first; |
|---|
| 512 | else |
|---|
| 513 | end = std::string::npos; |
|---|
| 514 | |
|---|
| 515 | if (!bLuaCode) |
|---|
| 516 | { |
|---|
| 517 | output << text.substr(start, end - start); |
|---|
| 518 | start = end + 5; |
|---|
| 519 | } |
|---|
| 520 | else |
|---|
| 521 | { |
|---|
| 522 | //Preserve the amount of lines, otherwise the linenumber from the xml parse error is useless |
|---|
| 523 | std::string tempstring = text.substr(start, end - start); |
|---|
| 524 | output << std::string(std::count(tempstring.begin(), tempstring.end(), '\n'), '\n'); |
|---|
| 525 | start = end + 2; |
|---|
| 526 | } |
|---|
| 527 | |
|---|
| 528 | bLuaCode = !bLuaCode; |
|---|
| 529 | } |
|---|
| 530 | while (end != std::string::npos); |
|---|
| 531 | |
|---|
| 532 | return output.str(); |
|---|
| 533 | } |
|---|
| 534 | } |
|---|