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