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 <sstream> |
---|
32 | #include <tinyxml/ticpp.h> |
---|
33 | #include <boost/scoped_ptr.hpp> |
---|
34 | |
---|
35 | #include "util/Debug.h" |
---|
36 | #include "util/Exception.h" |
---|
37 | #include "util/StringUtils.h" |
---|
38 | #include "BaseObject.h" |
---|
39 | #include "Iterator.h" |
---|
40 | #include "ObjectList.h" |
---|
41 | #include "LuaState.h" |
---|
42 | #include "Namespace.h" |
---|
43 | #include "Resource.h" |
---|
44 | #include "XMLFile.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 | std::string xmlInput; |
---|
125 | if (file->getLuaSupport()) |
---|
126 | { |
---|
127 | // Use the LuaState to replace the XML tags (calls our function) |
---|
128 | scoped_ptr<LuaState> luaState(new LuaState()); |
---|
129 | luaState->setIncludeParser(&Loader::replaceLuaTags); |
---|
130 | luaState->includeFile(file->getFilename(), file->getResourceGroup(), false); |
---|
131 | xmlInput = luaState->getOutput().str(); |
---|
132 | } |
---|
133 | else |
---|
134 | { |
---|
135 | shared_ptr<ResourceInfo> info = Resource::getInfo(file->getFilename(), file->getResourceGroup()); |
---|
136 | if (info == NULL) |
---|
137 | { |
---|
138 | COUT(1) << "Error: Could not find XML file '" << file->getFilename() << "'." << std::endl; |
---|
139 | return false; |
---|
140 | } |
---|
141 | xmlInput = Resource::open(file->getFilename(), file->getResourceGroup())->getAsString(); |
---|
142 | } |
---|
143 | |
---|
144 | try |
---|
145 | { |
---|
146 | COUT(0) << "Start loading " << file->getFilename() << "..." << std::endl; |
---|
147 | COUT(3) << "Mask: " << Loader::currentMask_s << std::endl; |
---|
148 | |
---|
149 | ticpp::Document xmlfile(file->getFilename()); |
---|
150 | xmlfile.Parse(xmlInput, true); |
---|
151 | |
---|
152 | ticpp::Element rootElement; |
---|
153 | rootElement.SetAttribute("name", "root"); |
---|
154 | rootElement.SetAttribute("bAutogenerated", true); |
---|
155 | |
---|
156 | for (ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++) |
---|
157 | rootElement.InsertEndChild(*child); |
---|
158 | |
---|
159 | COUT(4) << " creating root-namespace..." << std::endl; |
---|
160 | Namespace* rootNamespace = new Namespace(0); |
---|
161 | rootNamespace->setLoaderIndentation(" "); |
---|
162 | rootNamespace->setFile(file); |
---|
163 | rootNamespace->setNamespace(rootNamespace); |
---|
164 | rootNamespace->setRoot(true); |
---|
165 | rootNamespace->XMLPort(rootElement, XMLPort::LoadObject); |
---|
166 | |
---|
167 | COUT(0) << "Finished loading " << file->getFilename() << "." << std::endl; |
---|
168 | |
---|
169 | COUT(4) << "Namespace-tree:" << std::endl << rootNamespace->toString(" ") << std::endl; |
---|
170 | |
---|
171 | return true; |
---|
172 | } |
---|
173 | catch (ticpp::Exception& ex) |
---|
174 | { |
---|
175 | COUT(1) << std::endl; |
---|
176 | COUT(1) << "An XML-error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
---|
177 | COUT(1) << ex.what() << std::endl; |
---|
178 | COUT(1) << "Loading aborted." << std::endl; |
---|
179 | return false; |
---|
180 | } |
---|
181 | catch (Exception& ex) |
---|
182 | { |
---|
183 | COUT(1) << std::endl; |
---|
184 | COUT(1) << "A loading-error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
---|
185 | COUT(1) << ex.what() << std::endl; |
---|
186 | COUT(1) << "Loading aborted." << std::endl; |
---|
187 | return false; |
---|
188 | } |
---|
189 | catch (std::exception& ex) |
---|
190 | { |
---|
191 | COUT(1) << std::endl; |
---|
192 | COUT(1) << "An error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
---|
193 | COUT(1) << ex.what() << std::endl; |
---|
194 | COUT(1) << "Loading aborted." << std::endl; |
---|
195 | return false; |
---|
196 | } |
---|
197 | catch (...) |
---|
198 | { |
---|
199 | COUT(1) << std::endl; |
---|
200 | COUT(1) << "An unknown error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl; |
---|
201 | COUT(1) << "Loading aborted." << std::endl; |
---|
202 | return false; |
---|
203 | } |
---|
204 | } |
---|
205 | |
---|
206 | void Loader::unload(const XMLFile* file, const ClassTreeMask& mask) |
---|
207 | { |
---|
208 | if (!file) |
---|
209 | return; |
---|
210 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; ) |
---|
211 | { |
---|
212 | if ((it->getFile() == file) && mask.isIncluded(it->getIdentifier())) |
---|
213 | delete (*(it++)); |
---|
214 | else |
---|
215 | ++it; |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | bool Loader::reload(const XMLFile* file, const ClassTreeMask& mask) |
---|
220 | { |
---|
221 | Loader::unload(file, mask); |
---|
222 | return Loader::load(file, mask); |
---|
223 | } |
---|
224 | |
---|
225 | std::string Loader::replaceLuaTags(const std::string& text) |
---|
226 | { |
---|
227 | // chreate map with all Lua tags |
---|
228 | std::map<size_t, bool> luaTags; |
---|
229 | { |
---|
230 | size_t pos = 0; |
---|
231 | while ((pos = text.find("<?lua", pos)) != std::string::npos) |
---|
232 | luaTags[pos++] = true; |
---|
233 | } |
---|
234 | { |
---|
235 | size_t pos = 0; |
---|
236 | while ((pos = text.find("?>", pos)) != std::string::npos) |
---|
237 | luaTags[pos++] = false; |
---|
238 | } |
---|
239 | |
---|
240 | // erase all tags from the map that are between two quotes |
---|
241 | { |
---|
242 | std::map<size_t, bool>::iterator it = luaTags.begin(); |
---|
243 | std::map<size_t, bool>::iterator it2 = it; |
---|
244 | bool bBetweenQuotes = false; |
---|
245 | size_t pos = 0; |
---|
246 | while ((pos = getNextQuote(text, pos)) != std::string::npos) |
---|
247 | { |
---|
248 | while ((it != luaTags.end()) && (it->first < pos)) |
---|
249 | { |
---|
250 | if (bBetweenQuotes) |
---|
251 | { |
---|
252 | it2++; |
---|
253 | if(it->second && !(it2->second) && it2->first < pos) |
---|
254 | it = ++it2; |
---|
255 | else |
---|
256 | luaTags.erase(it++); |
---|
257 | } |
---|
258 | else |
---|
259 | ++it; |
---|
260 | } |
---|
261 | bBetweenQuotes = !bBetweenQuotes; |
---|
262 | pos++; |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | // check whether on every opening <?lua tag a closing ?> tag follows |
---|
267 | { |
---|
268 | bool expectedValue = true; |
---|
269 | for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it) |
---|
270 | { |
---|
271 | if (it->second == expectedValue) |
---|
272 | expectedValue = !expectedValue; |
---|
273 | else |
---|
274 | { |
---|
275 | expectedValue = false; |
---|
276 | break; |
---|
277 | } |
---|
278 | } |
---|
279 | if (!expectedValue) |
---|
280 | { |
---|
281 | COUT(2) << "Warning: Error in level file" << std::endl; |
---|
282 | // todo: errorhandling |
---|
283 | return ""; |
---|
284 | } |
---|
285 | } |
---|
286 | |
---|
287 | // Use a stringstream object to speed up the parsing |
---|
288 | std::ostringstream output; |
---|
289 | |
---|
290 | // cut the original string into pieces and put them together with print() instead of lua tags |
---|
291 | { |
---|
292 | std::map<size_t, bool>::iterator it = luaTags.begin(); |
---|
293 | bool bInPrintFunction = true; |
---|
294 | size_t start = 0; |
---|
295 | size_t end = 0; |
---|
296 | |
---|
297 | do |
---|
298 | { |
---|
299 | if (it != luaTags.end()) |
---|
300 | end = (*(it++)).first; |
---|
301 | else |
---|
302 | end = std::string::npos; |
---|
303 | |
---|
304 | unsigned int equalSignCounter = 0; |
---|
305 | |
---|
306 | if (bInPrintFunction) |
---|
307 | { |
---|
308 | // count ['='[ and ]'='] and replace tags with print([[ and ]]) |
---|
309 | std::string temp = text.substr(start, end - start); |
---|
310 | { |
---|
311 | size_t pos = 0; |
---|
312 | while ((pos = temp.find('[', pos)) != std::string::npos) |
---|
313 | { |
---|
314 | unsigned int tempCounter = 1; |
---|
315 | size_t tempPos = pos++; |
---|
316 | while(temp[++tempPos] == '=') |
---|
317 | { |
---|
318 | tempCounter++; |
---|
319 | } |
---|
320 | if(temp[tempPos] != '[') |
---|
321 | { |
---|
322 | tempCounter = 0; |
---|
323 | } |
---|
324 | else if(tempCounter == 0) |
---|
325 | { |
---|
326 | tempCounter = 1; |
---|
327 | } |
---|
328 | if (tempCounter > equalSignCounter) |
---|
329 | equalSignCounter = tempCounter; |
---|
330 | } |
---|
331 | } |
---|
332 | { |
---|
333 | size_t pos = 0; |
---|
334 | while ((pos = temp.find(']', pos)) != std::string::npos) |
---|
335 | { |
---|
336 | unsigned int tempCounter = 1; |
---|
337 | size_t tempPos = pos++; |
---|
338 | while(temp[++tempPos] == '=') |
---|
339 | { |
---|
340 | tempCounter++; |
---|
341 | } |
---|
342 | if(temp[tempPos] != ']') |
---|
343 | { |
---|
344 | tempCounter = 0; |
---|
345 | } |
---|
346 | else if(tempCounter == 0) |
---|
347 | { |
---|
348 | tempCounter = 1; |
---|
349 | } |
---|
350 | if (tempCounter > equalSignCounter) |
---|
351 | equalSignCounter = tempCounter; |
---|
352 | } |
---|
353 | } |
---|
354 | std::string equalSigns = ""; |
---|
355 | for(unsigned int i = 0; i < equalSignCounter; i++) |
---|
356 | { |
---|
357 | equalSigns += "="; |
---|
358 | } |
---|
359 | output << "print([" + equalSigns + "[" + temp + "]" + equalSigns +"])"; |
---|
360 | start = end + 5; |
---|
361 | } |
---|
362 | else |
---|
363 | { |
---|
364 | output << text.substr(start, end - start); |
---|
365 | start = end + 2; |
---|
366 | } |
---|
367 | |
---|
368 | bInPrintFunction = !bInPrintFunction; |
---|
369 | } |
---|
370 | while (end != std::string::npos); |
---|
371 | } |
---|
372 | |
---|
373 | return output.str(); |
---|
374 | } |
---|
375 | } |
---|