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