[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 | |
---|
| 31 | #include <tinyxml/ticpp.h> |
---|
| 32 | |
---|
[3196] | 33 | #include "util/Debug.h" |
---|
| 34 | #include "util/Exception.h" |
---|
[1505] | 35 | #include "BaseObject.h" |
---|
| 36 | #include "Iterator.h" |
---|
[1747] | 37 | #include "ObjectList.h" |
---|
[2087] | 38 | #include "LuaBind.h" |
---|
[1505] | 39 | #include "Namespace.h" |
---|
[3196] | 40 | #include "XMLFile.h" |
---|
[1505] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
[2087] | 44 | std::vector<std::pair<const XMLFile*, ClassTreeMask> > Loader::files_s; |
---|
[1505] | 45 | ClassTreeMask Loader::currentMask_s; |
---|
| 46 | |
---|
[2087] | 47 | bool Loader::open(const XMLFile* file, const ClassTreeMask& mask) |
---|
[1505] | 48 | { |
---|
[2087] | 49 | Loader::add(file, mask); |
---|
| 50 | return Loader::load(file, mask); |
---|
[1505] | 51 | } |
---|
| 52 | |
---|
| 53 | void Loader::close() |
---|
| 54 | { |
---|
| 55 | Loader::unload(); |
---|
[2087] | 56 | Loader::files_s.clear(); |
---|
[1505] | 57 | } |
---|
| 58 | |
---|
[2087] | 59 | void Loader::close(const XMLFile* file) |
---|
[1505] | 60 | { |
---|
[2087] | 61 | Loader::unload(file); |
---|
| 62 | Loader::remove(file); |
---|
[1505] | 63 | } |
---|
| 64 | |
---|
[2087] | 65 | void Loader::add(const XMLFile* file, const ClassTreeMask& mask) |
---|
[1505] | 66 | { |
---|
[2087] | 67 | if (!file) |
---|
[1755] | 68 | return; |
---|
[2087] | 69 | Loader::files_s.insert(Loader::files_s.end(), std::pair<const XMLFile*, ClassTreeMask>(file, mask)); |
---|
[1505] | 70 | } |
---|
| 71 | |
---|
[2087] | 72 | void Loader::remove(const XMLFile* file) |
---|
[1505] | 73 | { |
---|
[2087] | 74 | if (!file) |
---|
[1755] | 75 | return; |
---|
[2087] | 76 | for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it) |
---|
[1505] | 77 | { |
---|
[2087] | 78 | if ((*it).first == file) |
---|
[1505] | 79 | { |
---|
[2087] | 80 | Loader::files_s.erase(it); |
---|
[1505] | 81 | break; |
---|
| 82 | } |
---|
| 83 | } |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | bool Loader::load(const ClassTreeMask& mask) |
---|
| 87 | { |
---|
| 88 | bool success = true; |
---|
[2087] | 89 | for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it) |
---|
[1505] | 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 | { |
---|
[1747] | 98 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); ) |
---|
[1505] | 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 | |
---|
[2087] | 113 | bool Loader::load(const XMLFile* file, const ClassTreeMask& mask) |
---|
[1505] | 114 | { |
---|
[2087] | 115 | if (!file) |
---|
[1755] | 116 | return false; |
---|
| 117 | |
---|
[2087] | 118 | Loader::currentMask_s = file->getMask() * mask; |
---|
[1505] | 119 | |
---|
| 120 | // let Lua work this out: |
---|
[2662] | 121 | LuaBind& lua = LuaBind::getInstance(); |
---|
| 122 | lua.clearLuaOutput(); |
---|
| 123 | lua.loadFile(file->getFilename(), true); |
---|
| 124 | lua.run(); |
---|
[1505] | 125 | |
---|
| 126 | try |
---|
| 127 | { |
---|
[2087] | 128 | COUT(0) << "Start loading " << file->getFilename() << "..." << std::endl; |
---|
[1505] | 129 | COUT(3) << "Mask: " << Loader::currentMask_s << std::endl; |
---|
| 130 | |
---|
[2087] | 131 | //ticpp::Document xmlfile(file->getFilename()); |
---|
[1505] | 132 | //xmlfile.LoadFile(); |
---|
| 133 | //ticpp::Element myelement(*Script::getFileString()); |
---|
| 134 | ticpp::Document xmlfile; |
---|
| 135 | //xmlfile.ToDocument(); |
---|
[2662] | 136 | xmlfile.Parse(lua.getLuaOutput(), true); |
---|
[1505] | 137 | |
---|
| 138 | ticpp::Element rootElement; |
---|
| 139 | rootElement.SetAttribute("name", "root"); |
---|
| 140 | rootElement.SetAttribute("bAutogenerated", true); |
---|
| 141 | |
---|
| 142 | for (ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++) |
---|
| 143 | rootElement.InsertEndChild(*child); |
---|
| 144 | |
---|
| 145 | COUT(4) << " creating root-namespace..." << std::endl; |
---|
[2087] | 146 | Namespace* rootNamespace = new Namespace(0); |
---|
[1505] | 147 | rootNamespace->setLoaderIndentation(" "); |
---|
[2087] | 148 | rootNamespace->setFile(file); |
---|
[1505] | 149 | rootNamespace->setNamespace(rootNamespace); |
---|
| 150 | rootNamespace->setRoot(true); |
---|
| 151 | rootNamespace->XMLPort(rootElement, XMLPort::LoadObject); |
---|
| 152 | |
---|
[2087] | 153 | COUT(0) << "Finished loading " << file->getFilename() << "." << std::endl; |
---|
[1505] | 154 | |
---|
| 155 | COUT(4) << "Namespace-tree:" << std::endl << rootNamespace->toString(" ") << std::endl; |
---|
| 156 | |
---|
| 157 | return true; |
---|
| 158 | } |
---|
[2171] | 159 | catch (ticpp::Exception& ex) |
---|
[1505] | 160 | { |
---|
| 161 | COUT(1) << std::endl; |
---|
[2171] | 162 | COUT(1) << "An XML-error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
---|
| 163 | COUT(1) << ex.what() << std::endl; |
---|
| 164 | COUT(1) << "Loading aborted." << std::endl; |
---|
| 165 | return false; |
---|
| 166 | } |
---|
| 167 | catch (Exception& ex) |
---|
| 168 | { |
---|
| 169 | COUT(1) << std::endl; |
---|
| 170 | COUT(1) << "A loading-error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
---|
| 171 | COUT(1) << ex.what() << std::endl; |
---|
| 172 | COUT(1) << "Loading aborted." << std::endl; |
---|
| 173 | return false; |
---|
| 174 | } |
---|
| 175 | catch (std::exception& ex) |
---|
| 176 | { |
---|
| 177 | COUT(1) << std::endl; |
---|
[2087] | 178 | COUT(1) << "An error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
---|
[1505] | 179 | COUT(1) << ex.what() << std::endl; |
---|
| 180 | COUT(1) << "Loading aborted." << std::endl; |
---|
| 181 | return false; |
---|
| 182 | } |
---|
[2171] | 183 | catch (...) |
---|
| 184 | { |
---|
| 185 | COUT(1) << std::endl; |
---|
| 186 | COUT(1) << "An unknown error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
---|
| 187 | COUT(1) << "Loading aborted." << std::endl; |
---|
| 188 | return false; |
---|
| 189 | } |
---|
[1505] | 190 | } |
---|
| 191 | |
---|
[2087] | 192 | void Loader::unload(const XMLFile* file, const ClassTreeMask& mask) |
---|
[1505] | 193 | { |
---|
[2087] | 194 | if (!file) |
---|
[1755] | 195 | return; |
---|
[1747] | 196 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; ) |
---|
[1505] | 197 | { |
---|
[2087] | 198 | if ((it->getFile() == file) && mask.isIncluded(it->getIdentifier())) |
---|
[1505] | 199 | delete (*(it++)); |
---|
| 200 | else |
---|
| 201 | ++it; |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
[2087] | 205 | bool Loader::reload(const XMLFile* file, const ClassTreeMask& mask) |
---|
[1505] | 206 | { |
---|
[2087] | 207 | Loader::unload(file, mask); |
---|
| 208 | return Loader::load(file, mask); |
---|
[1505] | 209 | } |
---|
| 210 | } |
---|