| 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 | #include "Level.h" | 
|---|
| 31 | #include "BaseObject.h" | 
|---|
| 32 | #include "Identifier.h" | 
|---|
| 33 | #include "Iterator.h" | 
|---|
| 34 | #include "ObjectList.h" | 
|---|
| 35 | #include "CoreIncludes.h" | 
|---|
| 36 | #include "Script.h" | 
|---|
| 37 | #include "Namespace.h" | 
|---|
| 38 | #include "util/Debug.h" | 
|---|
| 39 |  | 
|---|
| 40 | #include "tinyxml/ticpp.h" | 
|---|
| 41 |  | 
|---|
| 42 | namespace orxonox | 
|---|
| 43 | { | 
|---|
| 44 | std::vector<std::pair<const Level*, ClassTreeMask> > Loader::levels_s; | 
|---|
| 45 | ClassTreeMask Loader::currentMask_s; | 
|---|
| 46 |  | 
|---|
| 47 | bool Loader::open(const Level* level, const ClassTreeMask& mask) | 
|---|
| 48 | { | 
|---|
| 49 | Loader::add(level, mask); | 
|---|
| 50 | return Loader::load(level, mask); | 
|---|
| 51 | } | 
|---|
| 52 |  | 
|---|
| 53 | void Loader::close() | 
|---|
| 54 | { | 
|---|
| 55 | Loader::unload(); | 
|---|
| 56 | Loader::levels_s.clear(); | 
|---|
| 57 | } | 
|---|
| 58 |  | 
|---|
| 59 | void Loader::close(const Level* level) | 
|---|
| 60 | { | 
|---|
| 61 | Loader::unload(level); | 
|---|
| 62 | Loader::remove(level); | 
|---|
| 63 | } | 
|---|
| 64 |  | 
|---|
| 65 | void Loader::add(const Level* level, const ClassTreeMask& mask) | 
|---|
| 66 | { | 
|---|
| 67 | if (!level) | 
|---|
| 68 | return; | 
|---|
| 69 | Loader::levels_s.insert(Loader::levels_s.end(), std::pair<const Level*, ClassTreeMask>(level, mask)); | 
|---|
| 70 | } | 
|---|
| 71 |  | 
|---|
| 72 | void Loader::remove(const Level* level) | 
|---|
| 73 | { | 
|---|
| 74 | if (!level) | 
|---|
| 75 | return; | 
|---|
| 76 | for (std::vector<std::pair<const Level*, ClassTreeMask> >::iterator it = Loader::levels_s.begin(); it != Loader::levels_s.end(); ++it) | 
|---|
| 77 | { | 
|---|
| 78 | if ((*it).first == level) | 
|---|
| 79 | { | 
|---|
| 80 | Loader::levels_s.erase(it); | 
|---|
| 81 | break; | 
|---|
| 82 | } | 
|---|
| 83 | } | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 | bool Loader::load(const ClassTreeMask& mask) | 
|---|
| 87 | { | 
|---|
| 88 | bool success = true; | 
|---|
| 89 | for (std::vector<std::pair<const Level*, ClassTreeMask> >::iterator it = Loader::levels_s.begin(); it != Loader::levels_s.end(); ++it) | 
|---|
| 90 | if (!Loader::load((*it).first, (*it).second * mask)) | 
|---|
| 91 | success = false; | 
|---|
| 92 |  | 
|---|
| 93 | return success; | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | void Loader::unload(const ClassTreeMask& mask) | 
|---|
| 97 | { | 
|---|
| 98 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); ) | 
|---|
| 99 | { | 
|---|
| 100 | if (mask.isIncluded(it->getIdentifier())) | 
|---|
| 101 | delete (*(it++)); | 
|---|
| 102 | else | 
|---|
| 103 | ++it; | 
|---|
| 104 | } | 
|---|
| 105 | } | 
|---|
| 106 |  | 
|---|
| 107 | bool Loader::reload(const ClassTreeMask& mask) | 
|---|
| 108 | { | 
|---|
| 109 | Loader::unload(mask); | 
|---|
| 110 | return Loader::load(mask); | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | bool Loader::load(const Level* level, const ClassTreeMask& mask) | 
|---|
| 114 | { | 
|---|
| 115 | if (!level) | 
|---|
| 116 | return false; | 
|---|
| 117 |  | 
|---|
| 118 | Loader::currentMask_s = level->getMask() * mask; | 
|---|
| 119 |  | 
|---|
| 120 | // let Lua work this out: | 
|---|
| 121 | //Script* lua; | 
|---|
| 122 | /*Script::loadFile(level->getFile(), true); | 
|---|
| 123 | Script::init(Script::getLuaState()); | 
|---|
| 124 | Script::run();*/ | 
|---|
| 125 | Script* lua = Script::getInstance(); | 
|---|
| 126 | lua->clearLuaOutput(); | 
|---|
| 127 | lua->loadFile(level->getFile(), true); | 
|---|
| 128 | lua->run(); | 
|---|
| 129 |  | 
|---|
| 130 | try | 
|---|
| 131 | { | 
|---|
| 132 | COUT(0) << "Start loading " << level->getFile() << "..." << std::endl; | 
|---|
| 133 | COUT(3) << "Mask: " << Loader::currentMask_s << std::endl; | 
|---|
| 134 |  | 
|---|
| 135 | //ticpp::Document xmlfile(level->getFile()); | 
|---|
| 136 | //xmlfile.LoadFile(); | 
|---|
| 137 | //ticpp::Element myelement(*Script::getFileString()); | 
|---|
| 138 | ticpp::Document xmlfile; | 
|---|
| 139 | //xmlfile.ToDocument(); | 
|---|
| 140 | xmlfile.Parse(lua->getLuaOutput(), true); | 
|---|
| 141 |  | 
|---|
| 142 | ticpp::Element rootElement; | 
|---|
| 143 | rootElement.SetAttribute("name", "root"); | 
|---|
| 144 | rootElement.SetAttribute("bAutogenerated", true); | 
|---|
| 145 |  | 
|---|
| 146 | for (ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++) | 
|---|
| 147 | rootElement.InsertEndChild(*child); | 
|---|
| 148 |  | 
|---|
| 149 | COUT(4) << "  creating root-namespace..." << std::endl; | 
|---|
| 150 | Namespace* rootNamespace = new Namespace(); | 
|---|
| 151 | rootNamespace->setLoaderIndentation("    "); | 
|---|
| 152 | rootNamespace->setLevel(level); | 
|---|
| 153 | rootNamespace->setNamespace(rootNamespace); | 
|---|
| 154 | rootNamespace->setRoot(true); | 
|---|
| 155 | rootNamespace->XMLPort(rootElement, XMLPort::LoadObject); | 
|---|
| 156 |  | 
|---|
| 157 | COUT(0) << "Finished loading " << level->getFile() << "." << std::endl; | 
|---|
| 158 |  | 
|---|
| 159 | COUT(4) << "Namespace-tree:" << std::endl << rootNamespace->toString("  ") << std::endl; | 
|---|
| 160 |  | 
|---|
| 161 | return true; | 
|---|
| 162 | } | 
|---|
| 163 | catch(ticpp::Exception& ex) | 
|---|
| 164 | { | 
|---|
| 165 | COUT(1) << std::endl; | 
|---|
| 166 | COUT(1) << "An error occurred in Loader.cc while loading " << level->getFile() << ":" << std::endl; | 
|---|
| 167 | COUT(1) << ex.what() << std::endl; | 
|---|
| 168 | COUT(1) << "Loading aborted." << std::endl; | 
|---|
| 169 | return false; | 
|---|
| 170 | } | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | void Loader::unload(const Level* level, const ClassTreeMask& mask) | 
|---|
| 174 | { | 
|---|
| 175 | if (!level) | 
|---|
| 176 | return; | 
|---|
| 177 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; ) | 
|---|
| 178 | { | 
|---|
| 179 | if ((it->getLevel() == level) && mask.isIncluded(it->getIdentifier())) | 
|---|
| 180 | delete (*(it++)); | 
|---|
| 181 | else | 
|---|
| 182 | ++it; | 
|---|
| 183 | } | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 | bool Loader::reload(const Level* level, const ClassTreeMask& mask) | 
|---|
| 187 | { | 
|---|
| 188 | Loader::unload(level, mask); | 
|---|
| 189 | return Loader::load(level, mask); | 
|---|
| 190 | } | 
|---|
| 191 | } | 
|---|