[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> |
---|
[3008] | 32 | #include <boost/filesystem.hpp> |
---|
[2710] | 33 | |
---|
[2087] | 34 | #include "XMLFile.h" |
---|
[1505] | 35 | #include "BaseObject.h" |
---|
| 36 | #include "Identifier.h" |
---|
| 37 | #include "Iterator.h" |
---|
[1747] | 38 | #include "ObjectList.h" |
---|
[1505] | 39 | #include "CoreIncludes.h" |
---|
[2087] | 40 | #include "LuaBind.h" |
---|
[1505] | 41 | #include "Namespace.h" |
---|
[1747] | 42 | #include "util/Debug.h" |
---|
[2171] | 43 | #include "util/Exception.h" |
---|
[3008] | 44 | #include "Core.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())) |
---|
| 105 | delete (*(it++)); |
---|
| 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 | |
---|
| 124 | // let Lua work this out: |
---|
[2662] | 125 | LuaBind& lua = LuaBind::getInstance(); |
---|
| 126 | lua.clearLuaOutput(); |
---|
| 127 | lua.loadFile(file->getFilename(), true); |
---|
| 128 | lua.run(); |
---|
[1505] | 129 | |
---|
| 130 | try |
---|
| 131 | { |
---|
[2087] | 132 | COUT(0) << "Start loading " << file->getFilename() << "..." << std::endl; |
---|
[1505] | 133 | COUT(3) << "Mask: " << Loader::currentMask_s << std::endl; |
---|
| 134 | |
---|
[2087] | 135 | //ticpp::Document xmlfile(file->getFilename()); |
---|
[1505] | 136 | //xmlfile.LoadFile(); |
---|
| 137 | //ticpp::Element myelement(*Script::getFileString()); |
---|
| 138 | ticpp::Document xmlfile; |
---|
| 139 | //xmlfile.ToDocument(); |
---|
[2662] | 140 | xmlfile.Parse(lua.getLuaOutput(), true); |
---|
[1505] | 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; |
---|
[2087] | 150 | Namespace* rootNamespace = new Namespace(0); |
---|
[1505] | 151 | rootNamespace->setLoaderIndentation(" "); |
---|
[2087] | 152 | rootNamespace->setFile(file); |
---|
[1505] | 153 | rootNamespace->setNamespace(rootNamespace); |
---|
| 154 | rootNamespace->setRoot(true); |
---|
| 155 | rootNamespace->XMLPort(rootElement, XMLPort::LoadObject); |
---|
| 156 | |
---|
[2087] | 157 | COUT(0) << "Finished loading " << file->getFilename() << "." << std::endl; |
---|
[1505] | 158 | |
---|
| 159 | COUT(4) << "Namespace-tree:" << std::endl << rootNamespace->toString(" ") << std::endl; |
---|
| 160 | |
---|
| 161 | return true; |
---|
| 162 | } |
---|
[2171] | 163 | catch (ticpp::Exception& ex) |
---|
[1505] | 164 | { |
---|
| 165 | COUT(1) << std::endl; |
---|
[2171] | 166 | COUT(1) << "An XML-error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
---|
| 167 | COUT(1) << ex.what() << std::endl; |
---|
| 168 | COUT(1) << "Loading aborted." << std::endl; |
---|
| 169 | return false; |
---|
| 170 | } |
---|
| 171 | catch (Exception& ex) |
---|
| 172 | { |
---|
| 173 | COUT(1) << std::endl; |
---|
| 174 | COUT(1) << "A loading-error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
---|
| 175 | COUT(1) << ex.what() << std::endl; |
---|
| 176 | COUT(1) << "Loading aborted." << std::endl; |
---|
| 177 | return false; |
---|
| 178 | } |
---|
| 179 | catch (std::exception& ex) |
---|
| 180 | { |
---|
| 181 | COUT(1) << std::endl; |
---|
[2087] | 182 | COUT(1) << "An error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
---|
[1505] | 183 | COUT(1) << ex.what() << std::endl; |
---|
| 184 | COUT(1) << "Loading aborted." << std::endl; |
---|
| 185 | return false; |
---|
| 186 | } |
---|
[2171] | 187 | catch (...) |
---|
| 188 | { |
---|
| 189 | COUT(1) << std::endl; |
---|
| 190 | COUT(1) << "An unknown error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
---|
| 191 | COUT(1) << "Loading aborted." << std::endl; |
---|
| 192 | return false; |
---|
| 193 | } |
---|
[1505] | 194 | } |
---|
| 195 | |
---|
[2087] | 196 | void Loader::unload(const XMLFile* file, const ClassTreeMask& mask) |
---|
[1505] | 197 | { |
---|
[2087] | 198 | if (!file) |
---|
[1755] | 199 | return; |
---|
[1747] | 200 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; ) |
---|
[1505] | 201 | { |
---|
[2087] | 202 | if ((it->getFile() == file) && mask.isIncluded(it->getIdentifier())) |
---|
[1505] | 203 | delete (*(it++)); |
---|
| 204 | else |
---|
| 205 | ++it; |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|
[2087] | 209 | bool Loader::reload(const XMLFile* file, const ClassTreeMask& mask) |
---|
[1505] | 210 | { |
---|
[2087] | 211 | Loader::unload(file, mask); |
---|
| 212 | return Loader::load(file, mask); |
---|
[1505] | 213 | } |
---|
[3008] | 214 | |
---|
| 215 | std::vector<std::string> Loader::getLevelList() |
---|
| 216 | { |
---|
| 217 | std::vector<std::string> levelList; |
---|
| 218 | |
---|
| 219 | boost::filesystem::directory_iterator file(Core::getMediaPathString() + "levels"); |
---|
| 220 | boost::filesystem::directory_iterator end; |
---|
| 221 | |
---|
| 222 | while (file != end) |
---|
| 223 | { |
---|
| 224 | if (!boost::filesystem::is_directory(*file) && file->string()[file->string().length()-1] != '~') |
---|
| 225 | { |
---|
| 226 | std::string filename = file->path().leaf(); |
---|
| 227 | if (filename.length() > 4) |
---|
| 228 | levelList.push_back(filename.substr(0,filename.length()-4)); |
---|
| 229 | } |
---|
| 230 | ++file; |
---|
| 231 | } |
---|
| 232 | return levelList; |
---|
| 233 | } |
---|
[1505] | 234 | } |
---|