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