| 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 | |
|---|
| 31 | #include <tinyxml/ticpp.h> |
|---|
| 32 | #include <boost/filesystem.hpp> |
|---|
| 33 | |
|---|
| 34 | #include "XMLFile.h" |
|---|
| 35 | #include "BaseObject.h" |
|---|
| 36 | #include "Identifier.h" |
|---|
| 37 | #include "Iterator.h" |
|---|
| 38 | #include "ObjectList.h" |
|---|
| 39 | #include "CoreIncludes.h" |
|---|
| 40 | #include "LuaBind.h" |
|---|
| 41 | #include "Namespace.h" |
|---|
| 42 | #include "util/Debug.h" |
|---|
| 43 | #include "util/Exception.h" |
|---|
| 44 | #include "Core.h" |
|---|
| 45 | |
|---|
| 46 | namespace orxonox |
|---|
| 47 | { |
|---|
| 48 | std::vector<std::pair<const XMLFile*, ClassTreeMask> > Loader::files_s; |
|---|
| 49 | ClassTreeMask Loader::currentMask_s; |
|---|
| 50 | |
|---|
| 51 | bool Loader::open(const XMLFile* file, const ClassTreeMask& mask) |
|---|
| 52 | { |
|---|
| 53 | Loader::add(file, mask); |
|---|
| 54 | return Loader::load(file, mask); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | void Loader::close() |
|---|
| 58 | { |
|---|
| 59 | Loader::unload(); |
|---|
| 60 | Loader::files_s.clear(); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | void Loader::close(const XMLFile* file) |
|---|
| 64 | { |
|---|
| 65 | Loader::unload(file); |
|---|
| 66 | Loader::remove(file); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | void Loader::add(const XMLFile* file, const ClassTreeMask& mask) |
|---|
| 70 | { |
|---|
| 71 | if (!file) |
|---|
| 72 | return; |
|---|
| 73 | Loader::files_s.insert(Loader::files_s.end(), std::pair<const XMLFile*, ClassTreeMask>(file, mask)); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | void Loader::remove(const XMLFile* file) |
|---|
| 77 | { |
|---|
| 78 | if (!file) |
|---|
| 79 | return; |
|---|
| 80 | for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it) |
|---|
| 81 | { |
|---|
| 82 | if ((*it).first == file) |
|---|
| 83 | { |
|---|
| 84 | Loader::files_s.erase(it); |
|---|
| 85 | break; |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | bool Loader::load(const ClassTreeMask& mask) |
|---|
| 91 | { |
|---|
| 92 | bool success = true; |
|---|
| 93 | for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it) |
|---|
| 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 | { |
|---|
| 102 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); ) |
|---|
| 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 | |
|---|
| 117 | bool Loader::load(const XMLFile* file, const ClassTreeMask& mask) |
|---|
| 118 | { |
|---|
| 119 | if (!file) |
|---|
| 120 | return false; |
|---|
| 121 | |
|---|
| 122 | Loader::currentMask_s = file->getMask() * mask; |
|---|
| 123 | |
|---|
| 124 | // let Lua work this out: |
|---|
| 125 | LuaBind& lua = LuaBind::getInstance(); |
|---|
| 126 | lua.clearLuaOutput(); |
|---|
| 127 | lua.loadFile(file->getFilename(), true); |
|---|
| 128 | lua.run(); |
|---|
| 129 | |
|---|
| 130 | try |
|---|
| 131 | { |
|---|
| 132 | COUT(0) << "Start loading " << file->getFilename() << "..." << std::endl; |
|---|
| 133 | COUT(3) << "Mask: " << Loader::currentMask_s << std::endl; |
|---|
| 134 | |
|---|
| 135 | //ticpp::Document xmlfile(file->getFilename()); |
|---|
| 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(0); |
|---|
| 151 | rootNamespace->setLoaderIndentation(" "); |
|---|
| 152 | rootNamespace->setFile(file); |
|---|
| 153 | rootNamespace->setNamespace(rootNamespace); |
|---|
| 154 | rootNamespace->setRoot(true); |
|---|
| 155 | rootNamespace->XMLPort(rootElement, XMLPort::LoadObject); |
|---|
| 156 | |
|---|
| 157 | COUT(0) << "Finished loading " << file->getFilename() << "." << 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 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; |
|---|
| 182 | COUT(1) << "An error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
|---|
| 183 | COUT(1) << ex.what() << std::endl; |
|---|
| 184 | COUT(1) << "Loading aborted." << std::endl; |
|---|
| 185 | return false; |
|---|
| 186 | } |
|---|
| 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 | } |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | void Loader::unload(const XMLFile* file, const ClassTreeMask& mask) |
|---|
| 197 | { |
|---|
| 198 | if (!file) |
|---|
| 199 | return; |
|---|
| 200 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; ) |
|---|
| 201 | { |
|---|
| 202 | if ((it->getFile() == file) && mask.isIncluded(it->getIdentifier())) |
|---|
| 203 | delete (*(it++)); |
|---|
| 204 | else |
|---|
| 205 | ++it; |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | bool Loader::reload(const XMLFile* file, const ClassTreeMask& mask) |
|---|
| 210 | { |
|---|
| 211 | Loader::unload(file, mask); |
|---|
| 212 | return Loader::load(file, mask); |
|---|
| 213 | } |
|---|
| 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 | } |
|---|
| 234 | } |
|---|