[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> |
---|
[2710] | 34 | |
---|
[3196] | 35 | #include "util/Debug.h" |
---|
| 36 | #include "util/Exception.h" |
---|
[5695] | 37 | #include "util/StringUtils.h" |
---|
[1505] | 38 | #include "BaseObject.h" |
---|
| 39 | #include "Iterator.h" |
---|
[1747] | 40 | #include "ObjectList.h" |
---|
[5695] | 41 | #include "LuaState.h" |
---|
[1505] | 42 | #include "Namespace.h" |
---|
[5695] | 43 | #include "Resource.h" |
---|
[3196] | 44 | #include "XMLFile.h" |
---|
[1505] | 45 | |
---|
| 46 | namespace orxonox |
---|
| 47 | { |
---|
[2087] | 48 | std::vector<std::pair<const XMLFile*, ClassTreeMask> > Loader::files_s; |
---|
[1505] | 49 | ClassTreeMask Loader::currentMask_s; |
---|
| 50 | |
---|
[2087] | 51 | bool Loader::open(const XMLFile* file, const ClassTreeMask& mask) |
---|
[1505] | 52 | { |
---|
[2087] | 53 | Loader::add(file, mask); |
---|
| 54 | return Loader::load(file, mask); |
---|
[1505] | 55 | } |
---|
| 56 | |
---|
| 57 | void Loader::close() |
---|
| 58 | { |
---|
| 59 | Loader::unload(); |
---|
[2087] | 60 | Loader::files_s.clear(); |
---|
[1505] | 61 | } |
---|
| 62 | |
---|
[2087] | 63 | void Loader::close(const XMLFile* file) |
---|
[1505] | 64 | { |
---|
[2087] | 65 | Loader::unload(file); |
---|
| 66 | Loader::remove(file); |
---|
[1505] | 67 | } |
---|
| 68 | |
---|
[2087] | 69 | void Loader::add(const XMLFile* file, const ClassTreeMask& mask) |
---|
[1505] | 70 | { |
---|
[2087] | 71 | if (!file) |
---|
[1755] | 72 | return; |
---|
[2087] | 73 | Loader::files_s.insert(Loader::files_s.end(), std::pair<const XMLFile*, ClassTreeMask>(file, mask)); |
---|
[1505] | 74 | } |
---|
| 75 | |
---|
[2087] | 76 | void Loader::remove(const XMLFile* file) |
---|
[1505] | 77 | { |
---|
[2087] | 78 | if (!file) |
---|
[1755] | 79 | return; |
---|
[2087] | 80 | for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it) |
---|
[1505] | 81 | { |
---|
[2087] | 82 | if ((*it).first == file) |
---|
[1505] | 83 | { |
---|
[2087] | 84 | Loader::files_s.erase(it); |
---|
[1505] | 85 | break; |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | bool Loader::load(const ClassTreeMask& mask) |
---|
| 91 | { |
---|
| 92 | bool success = true; |
---|
[2087] | 93 | for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it) |
---|
[1505] | 94 | if (!Loader::load((*it).first, (*it).second * mask)) |
---|
| 95 | success = false; |
---|
| 96 | |
---|
| 97 | return success; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void Loader::unload(const ClassTreeMask& mask) |
---|
| 101 | { |
---|
[1747] | 102 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); ) |
---|
[1505] | 103 | { |
---|
| 104 | if (mask.isIncluded(it->getIdentifier())) |
---|
[5929] | 105 | (it++)->destroy(); |
---|
[1505] | 106 | else |
---|
| 107 | ++it; |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | bool Loader::reload(const ClassTreeMask& mask) |
---|
| 112 | { |
---|
| 113 | Loader::unload(mask); |
---|
| 114 | return Loader::load(mask); |
---|
| 115 | } |
---|
| 116 | |
---|
[2087] | 117 | bool Loader::load(const XMLFile* file, const ClassTreeMask& mask) |
---|
[1505] | 118 | { |
---|
[2087] | 119 | if (!file) |
---|
[1755] | 120 | return false; |
---|
| 121 | |
---|
[2087] | 122 | Loader::currentMask_s = file->getMask() * mask; |
---|
[1505] | 123 | |
---|
[5695] | 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 | } |
---|
[1505] | 143 | |
---|
| 144 | try |
---|
| 145 | { |
---|
[2087] | 146 | COUT(0) << "Start loading " << file->getFilename() << "..." << std::endl; |
---|
[1505] | 147 | COUT(3) << "Mask: " << Loader::currentMask_s << std::endl; |
---|
| 148 | |
---|
[5695] | 149 | ticpp::Document xmlfile(file->getFilename()); |
---|
| 150 | xmlfile.Parse(xmlInput, true); |
---|
[1505] | 151 | |
---|
| 152 | ticpp::Element rootElement; |
---|
| 153 | rootElement.SetAttribute("name", "root"); |
---|
| 154 | rootElement.SetAttribute("bAutogenerated", true); |
---|
| 155 | |
---|
| 156 | for (ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++) |
---|
| 157 | rootElement.InsertEndChild(*child); |
---|
| 158 | |
---|
| 159 | COUT(4) << " creating root-namespace..." << std::endl; |
---|
[2087] | 160 | Namespace* rootNamespace = new Namespace(0); |
---|
[1505] | 161 | rootNamespace->setLoaderIndentation(" "); |
---|
[2087] | 162 | rootNamespace->setFile(file); |
---|
[1505] | 163 | rootNamespace->setNamespace(rootNamespace); |
---|
| 164 | rootNamespace->setRoot(true); |
---|
| 165 | rootNamespace->XMLPort(rootElement, XMLPort::LoadObject); |
---|
| 166 | |
---|
[2087] | 167 | COUT(0) << "Finished loading " << file->getFilename() << "." << std::endl; |
---|
[1505] | 168 | |
---|
| 169 | COUT(4) << "Namespace-tree:" << std::endl << rootNamespace->toString(" ") << std::endl; |
---|
| 170 | |
---|
| 171 | return true; |
---|
| 172 | } |
---|
[2171] | 173 | catch (ticpp::Exception& ex) |
---|
[1505] | 174 | { |
---|
| 175 | COUT(1) << std::endl; |
---|
[2171] | 176 | COUT(1) << "An XML-error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
---|
| 177 | COUT(1) << ex.what() << std::endl; |
---|
| 178 | COUT(1) << "Loading aborted." << std::endl; |
---|
| 179 | return false; |
---|
| 180 | } |
---|
| 181 | catch (Exception& ex) |
---|
| 182 | { |
---|
| 183 | COUT(1) << std::endl; |
---|
| 184 | COUT(1) << "A loading-error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
---|
| 185 | COUT(1) << ex.what() << std::endl; |
---|
| 186 | COUT(1) << "Loading aborted." << std::endl; |
---|
| 187 | return false; |
---|
| 188 | } |
---|
[5747] | 189 | catch (...) |
---|
[2171] | 190 | { |
---|
| 191 | COUT(1) << std::endl; |
---|
[2087] | 192 | COUT(1) << "An error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
---|
[5747] | 193 | COUT(1) << Exception::handleMessage() << std::endl; |
---|
[1505] | 194 | COUT(1) << "Loading aborted." << std::endl; |
---|
| 195 | return false; |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
[2087] | 199 | void Loader::unload(const XMLFile* file, const ClassTreeMask& mask) |
---|
[1505] | 200 | { |
---|
[2087] | 201 | if (!file) |
---|
[1755] | 202 | return; |
---|
[1747] | 203 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; ) |
---|
[1505] | 204 | { |
---|
[2087] | 205 | if ((it->getFile() == file) && mask.isIncluded(it->getIdentifier())) |
---|
[5929] | 206 | (it++)->destroy(); |
---|
[1505] | 207 | else |
---|
| 208 | ++it; |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
[2087] | 212 | bool Loader::reload(const XMLFile* file, const ClassTreeMask& mask) |
---|
[1505] | 213 | { |
---|
[2087] | 214 | Loader::unload(file, mask); |
---|
| 215 | return Loader::load(file, mask); |
---|
[1505] | 216 | } |
---|
[5695] | 217 | |
---|
| 218 | std::string Loader::replaceLuaTags(const std::string& text) |
---|
| 219 | { |
---|
| 220 | // chreate map with all Lua tags |
---|
| 221 | std::map<size_t, bool> luaTags; |
---|
| 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 |
---|
| 234 | { |
---|
| 235 | std::map<size_t, bool>::iterator it = luaTags.begin(); |
---|
| 236 | std::map<size_t, bool>::iterator it2 = it; |
---|
| 237 | bool bBetweenQuotes = false; |
---|
| 238 | size_t pos = 0; |
---|
| 239 | while ((pos = getNextQuote(text, pos)) != std::string::npos) |
---|
| 240 | { |
---|
| 241 | while ((it != luaTags.end()) && (it->first < pos)) |
---|
| 242 | { |
---|
| 243 | if (bBetweenQuotes) |
---|
| 244 | { |
---|
| 245 | it2++; |
---|
| 246 | if(it->second && !(it2->second) && it2->first < pos) |
---|
| 247 | it = ++it2; |
---|
| 248 | else |
---|
| 249 | luaTags.erase(it++); |
---|
| 250 | } |
---|
| 251 | else |
---|
| 252 | ++it; |
---|
| 253 | } |
---|
| 254 | bBetweenQuotes = !bBetweenQuotes; |
---|
| 255 | pos++; |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | // check whether on every opening <?lua tag a closing ?> tag follows |
---|
| 260 | { |
---|
| 261 | bool expectedValue = true; |
---|
| 262 | for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it) |
---|
| 263 | { |
---|
| 264 | if (it->second == expectedValue) |
---|
| 265 | expectedValue = !expectedValue; |
---|
| 266 | else |
---|
| 267 | { |
---|
| 268 | expectedValue = false; |
---|
| 269 | break; |
---|
| 270 | } |
---|
| 271 | } |
---|
| 272 | if (!expectedValue) |
---|
| 273 | { |
---|
| 274 | COUT(2) << "Warning: Error in level file" << std::endl; |
---|
| 275 | // todo: errorhandling |
---|
| 276 | return ""; |
---|
| 277 | } |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | // Use a stringstream object to speed up the parsing |
---|
| 281 | std::ostringstream output; |
---|
| 282 | |
---|
| 283 | // cut the original string into pieces and put them together with print() instead of lua tags |
---|
| 284 | { |
---|
| 285 | std::map<size_t, bool>::iterator it = luaTags.begin(); |
---|
| 286 | bool bInPrintFunction = true; |
---|
| 287 | size_t start = 0; |
---|
| 288 | size_t end = 0; |
---|
| 289 | |
---|
| 290 | do |
---|
| 291 | { |
---|
| 292 | if (it != luaTags.end()) |
---|
| 293 | end = (*(it++)).first; |
---|
| 294 | else |
---|
| 295 | end = std::string::npos; |
---|
| 296 | |
---|
| 297 | unsigned int equalSignCounter = 0; |
---|
| 298 | |
---|
| 299 | if (bInPrintFunction) |
---|
| 300 | { |
---|
| 301 | // count ['='[ and ]'='] and replace tags with print([[ and ]]) |
---|
| 302 | std::string temp = text.substr(start, end - start); |
---|
| 303 | { |
---|
| 304 | size_t pos = 0; |
---|
| 305 | while ((pos = temp.find('[', pos)) != std::string::npos) |
---|
| 306 | { |
---|
| 307 | unsigned int tempCounter = 1; |
---|
| 308 | size_t tempPos = pos++; |
---|
| 309 | while(temp[++tempPos] == '=') |
---|
| 310 | { |
---|
| 311 | tempCounter++; |
---|
| 312 | } |
---|
| 313 | if(temp[tempPos] != '[') |
---|
| 314 | { |
---|
| 315 | tempCounter = 0; |
---|
| 316 | } |
---|
| 317 | else if(tempCounter == 0) |
---|
| 318 | { |
---|
| 319 | tempCounter = 1; |
---|
| 320 | } |
---|
| 321 | if (tempCounter > equalSignCounter) |
---|
| 322 | equalSignCounter = tempCounter; |
---|
| 323 | } |
---|
| 324 | } |
---|
| 325 | { |
---|
| 326 | size_t pos = 0; |
---|
| 327 | while ((pos = temp.find(']', pos)) != std::string::npos) |
---|
| 328 | { |
---|
| 329 | unsigned int tempCounter = 1; |
---|
| 330 | size_t tempPos = pos++; |
---|
| 331 | while(temp[++tempPos] == '=') |
---|
| 332 | { |
---|
| 333 | tempCounter++; |
---|
| 334 | } |
---|
| 335 | if(temp[tempPos] != ']') |
---|
| 336 | { |
---|
| 337 | tempCounter = 0; |
---|
| 338 | } |
---|
| 339 | else if(tempCounter == 0) |
---|
| 340 | { |
---|
| 341 | tempCounter = 1; |
---|
| 342 | } |
---|
| 343 | if (tempCounter > equalSignCounter) |
---|
| 344 | equalSignCounter = tempCounter; |
---|
| 345 | } |
---|
| 346 | } |
---|
| 347 | std::string equalSigns = ""; |
---|
| 348 | for(unsigned int i = 0; i < equalSignCounter; i++) |
---|
| 349 | { |
---|
| 350 | equalSigns += "="; |
---|
| 351 | } |
---|
| 352 | output << "print([" + equalSigns + "[" + temp + "]" + equalSigns +"])"; |
---|
| 353 | start = end + 5; |
---|
| 354 | } |
---|
| 355 | else |
---|
| 356 | { |
---|
| 357 | output << text.substr(start, end - start); |
---|
| 358 | start = end + 2; |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | bInPrintFunction = !bInPrintFunction; |
---|
| 362 | } |
---|
| 363 | while (end != std::string::npos); |
---|
| 364 | } |
---|
| 365 | |
---|
| 366 | return output.str(); |
---|
| 367 | } |
---|
[1505] | 368 | } |
---|