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 | #include <boost/filesystem.hpp> |
---|
35 | #include <boost/filesystem/fstream.hpp> |
---|
36 | |
---|
37 | #include "util/Output.h" |
---|
38 | #include "util/Exception.h" |
---|
39 | #include "util/StringUtils.h" |
---|
40 | #include "BaseObject.h" |
---|
41 | #include "LuaState.h" |
---|
42 | #include "Namespace.h" |
---|
43 | #include "Resource.h" |
---|
44 | #include "XMLFile.h" |
---|
45 | #include "object/Iterator.h" |
---|
46 | #include "object/ObjectList.h" |
---|
47 | |
---|
48 | namespace orxonox |
---|
49 | { |
---|
50 | Loader* Loader::singletonPtr_s = 0; |
---|
51 | |
---|
52 | /** |
---|
53 | @brief |
---|
54 | Loads the input file, while conforming to the restrictions given by the input ClassTreeMask. |
---|
55 | @param file |
---|
56 | The file to be loaded. |
---|
57 | @param mask |
---|
58 | A ClassTreeMask, which defines which types of classes are loaded and which aren't. |
---|
59 | @param bVerbose |
---|
60 | Whether the loader is verbose (prints its progress in a low output level) or not. |
---|
61 | @param bRemoveLuaTags |
---|
62 | If true lua tags are just ignored and removed. The default is false. |
---|
63 | @return |
---|
64 | Returns true if successful. |
---|
65 | */ |
---|
66 | bool Loader::load(const XMLFile* file, const ClassTreeMask& mask, bool bVerbose, bool bRemoveLuaTags) |
---|
67 | { |
---|
68 | if (!file) |
---|
69 | return false; |
---|
70 | |
---|
71 | this->currentMask_ = file->getMask() * mask; |
---|
72 | |
---|
73 | std::string xmlInput; |
---|
74 | |
---|
75 | shared_ptr<std::vector<std::vector<std::pair<std::string, size_t> > > > lineTrace(new std::vector<std::vector<std::pair<std::string, size_t> > >()); |
---|
76 | lineTrace->reserve(1000); //arbitrary number |
---|
77 | |
---|
78 | |
---|
79 | if (file->getLuaSupport() && !bRemoveLuaTags) |
---|
80 | { |
---|
81 | // Use the LuaState to replace the XML tags (calls our function) |
---|
82 | scoped_ptr<LuaState> luaState(new LuaState()); |
---|
83 | luaState->setTraceMap(lineTrace); |
---|
84 | luaState->setIncludeParser(&Loader::replaceLuaTags); |
---|
85 | luaState->includeFile(file->getFilename()); |
---|
86 | xmlInput = luaState->getOutput().str(); |
---|
87 | } |
---|
88 | else |
---|
89 | { |
---|
90 | shared_ptr<ResourceInfo> info = Resource::getInfo(file->getFilename()); |
---|
91 | if (info == NULL) |
---|
92 | { |
---|
93 | orxout(user_error, context::loader) << "Could not find XML file '" << file->getFilename() << "'." << endl; |
---|
94 | return false; |
---|
95 | } |
---|
96 | xmlInput = Resource::open(file->getFilename())->getAsString(); |
---|
97 | |
---|
98 | if (bRemoveLuaTags) |
---|
99 | { |
---|
100 | // Remove all Lua code. |
---|
101 | // Note: we only need this to speed up parsing of level files at the |
---|
102 | // start of the program. |
---|
103 | // Assumption: the LevelInfo tag does not use Lua scripting |
---|
104 | xmlInput = Loader::removeLuaTags(xmlInput); |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | try |
---|
109 | { |
---|
110 | if(bVerbose) |
---|
111 | { |
---|
112 | orxout(user_info) << "Start loading " << file->getFilename() << "..." << endl; |
---|
113 | orxout(internal_info, context::loader) << "Mask: " << this->currentMask_ << endl; |
---|
114 | } |
---|
115 | else |
---|
116 | { |
---|
117 | orxout(verbose, context::loader) << "Start loading " << file->getFilename() << "..." << endl; |
---|
118 | orxout(verbose_more, context::loader) << "Mask: " << this->currentMask_ << endl; |
---|
119 | } |
---|
120 | |
---|
121 | ticpp::Document xmlfile(file->getFilename()); |
---|
122 | xmlfile.Parse(xmlInput, true); |
---|
123 | |
---|
124 | ticpp::Element rootElement; |
---|
125 | rootElement.SetAttribute("name", "root"); |
---|
126 | rootElement.SetAttribute("bAutogenerated", true); |
---|
127 | |
---|
128 | for (ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++) |
---|
129 | rootElement.InsertEndChild(*child); |
---|
130 | |
---|
131 | orxout(verbose, context::loader) << " creating root-namespace..." << endl; |
---|
132 | Namespace* rootNamespace = new Namespace(Context::getRootContext()); |
---|
133 | rootNamespace->setLoaderIndentation(" "); |
---|
134 | rootNamespace->setFile(file); |
---|
135 | rootNamespace->setNamespace(rootNamespace); |
---|
136 | rootNamespace->setRoot(true); |
---|
137 | rootNamespace->XMLPort(rootElement, XMLPort::LoadObject); |
---|
138 | |
---|
139 | if(bVerbose) |
---|
140 | orxout(user_info) << "Finished loading " << file->getFilename() << '.' << endl; |
---|
141 | else |
---|
142 | orxout(verbose, context::loader) << "Finished loading " << file->getFilename() << '.' << endl; |
---|
143 | |
---|
144 | orxout(verbose, context::loader) << "Namespace-tree:" << '\n' << rootNamespace->toString(" ") << endl; |
---|
145 | |
---|
146 | return true; |
---|
147 | } |
---|
148 | catch (ticpp::Exception& ex) |
---|
149 | { |
---|
150 | orxout(user_error, context::loader) << endl; |
---|
151 | orxout(user_error, context::loader) << "An XML-error occurred in Loader.cc while loading " << file->getFilename() << ':' << endl; |
---|
152 | OutputLevel ticpplevel = user_error; |
---|
153 | if (lineTrace->size() > 0) |
---|
154 | { |
---|
155 | ticpplevel = internal_error; |
---|
156 | //Extract the line number from the exception |
---|
157 | std::string tempstring(ex.what()); |
---|
158 | std::string::size_type pos = tempstring.find("\nLine: "); |
---|
159 | if (pos != std::string::npos) |
---|
160 | { |
---|
161 | std::istringstream istr(tempstring.substr(pos + 7)); |
---|
162 | size_t line; |
---|
163 | istr >> line; |
---|
164 | if (line <= lineTrace->size()) |
---|
165 | { |
---|
166 | std::vector<std::pair<std::string, size_t> > linesources = lineTrace->at(line - 1); |
---|
167 | std::ostringstream message; |
---|
168 | message << "Possible sources of error:" << endl; |
---|
169 | for (std::vector<std::pair<std::string, size_t> >::iterator it = linesources.begin(); it != linesources.end(); ++it) |
---|
170 | { |
---|
171 | message << it->first << ", Line " << it->second << endl; |
---|
172 | } |
---|
173 | orxout(user_error, context::loader) << message.str() << endl; |
---|
174 | } |
---|
175 | } |
---|
176 | } |
---|
177 | orxout(ticpplevel, context::loader) << ex.what() << endl; |
---|
178 | orxout(user_error, context::loader) << "Loading aborted." << endl; |
---|
179 | } |
---|
180 | catch (Exception& ex) |
---|
181 | { |
---|
182 | orxout(user_error, context::loader) << endl; |
---|
183 | orxout(user_error, context::loader) << "A loading-error occurred in Loader.cc while loading " << file->getFilename() << ':' << endl; |
---|
184 | orxout(user_error, context::loader) << ex.what() << endl; |
---|
185 | orxout(user_error, context::loader) << "Loading aborted." << endl; |
---|
186 | } |
---|
187 | catch (...) |
---|
188 | { |
---|
189 | orxout(user_error, context::loader) << endl; |
---|
190 | orxout(user_error, context::loader) << "An error occurred in Loader.cc while loading " << file->getFilename() << ':' << endl; |
---|
191 | orxout(user_error, context::loader) << Exception::handleMessage() << endl; |
---|
192 | orxout(user_error, context::loader) << "Loading aborted." << endl; |
---|
193 | } |
---|
194 | //The Tardis' version of boost is too old... |
---|
195 | #if BOOST_VERSION >= 104600 |
---|
196 | boost::filesystem::path temppath = boost::filesystem::temp_directory_path() / "orxonoxml.xml"; |
---|
197 | //Need binary mode, because xmlInput already has \r\n for windows |
---|
198 | boost::filesystem::ofstream outfile(temppath, std::ios_base::binary | std::ios_base::out); |
---|
199 | outfile << xmlInput; |
---|
200 | outfile.flush(); |
---|
201 | outfile.close(); |
---|
202 | orxout(internal_error, context::loader) << "The complete xml file has been saved to " << temppath << endl; |
---|
203 | #endif |
---|
204 | return false; |
---|
205 | } |
---|
206 | |
---|
207 | void Loader::unload(const XMLFile* file, const ClassTreeMask& mask) |
---|
208 | { |
---|
209 | if (!file) |
---|
210 | return; |
---|
211 | for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; ) |
---|
212 | { |
---|
213 | if ((it->getFile() == file) && mask.isIncluded(it->getIdentifier())) |
---|
214 | (it++)->destroy(); |
---|
215 | else |
---|
216 | ++it; |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | bool Loader::getLuaTags(const std::string& text, std::map<size_t, bool>& luaTags) |
---|
221 | { |
---|
222 | // fill map with all Lua tags |
---|
223 | { |
---|
224 | size_t pos = 0; |
---|
225 | while ((pos = text.find("<?lua", pos)) != std::string::npos) |
---|
226 | luaTags[pos++] = true; |
---|
227 | } |
---|
228 | { |
---|
229 | size_t pos = 0; |
---|
230 | while ((pos = text.find("?>", pos)) != std::string::npos) |
---|
231 | luaTags[pos++] = false; |
---|
232 | } |
---|
233 | |
---|
234 | // erase all tags from the map that are between two quotes |
---|
235 | // that means occurrences like "..<?lua.." and "..?>.." would be deleted |
---|
236 | // however occurrences of lua tags within quotas are retained: ".. <?lua ... ?> .. " |
---|
237 | { |
---|
238 | std::map<size_t, bool>::iterator it = luaTags.begin(); |
---|
239 | bool bBetweenQuotes = false; |
---|
240 | size_t pos = 0; |
---|
241 | while ((pos = getNextQuote(text, pos)) != std::string::npos) |
---|
242 | { |
---|
243 | while ((it != luaTags.end()) && (it->first < pos)) |
---|
244 | { |
---|
245 | if (bBetweenQuotes) |
---|
246 | { |
---|
247 | std::map<size_t, bool>::iterator it2 = it; |
---|
248 | it2++; |
---|
249 | if (it->second && !(it2->second) && it2->first < pos) |
---|
250 | std::advance(it, 2); |
---|
251 | else |
---|
252 | luaTags.erase(it++); |
---|
253 | } |
---|
254 | else |
---|
255 | ++it; |
---|
256 | } |
---|
257 | bBetweenQuotes = !bBetweenQuotes; |
---|
258 | pos++; |
---|
259 | } |
---|
260 | } |
---|
261 | |
---|
262 | // check whether on every opening <?lua tag a closing ?> tag follows |
---|
263 | { |
---|
264 | bool expectedValue = true; |
---|
265 | for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it) |
---|
266 | { |
---|
267 | if (it->second == expectedValue) |
---|
268 | expectedValue = !expectedValue; |
---|
269 | else |
---|
270 | { |
---|
271 | expectedValue = false; |
---|
272 | break; |
---|
273 | } |
---|
274 | } |
---|
275 | if (!expectedValue) |
---|
276 | { |
---|
277 | orxout(internal_error, context::loader) << "Error parsing file: lua tags not matching" << endl; |
---|
278 | // TODO: error handling |
---|
279 | return false; |
---|
280 | } |
---|
281 | } |
---|
282 | |
---|
283 | return true; |
---|
284 | } |
---|
285 | |
---|
286 | std::string Loader::replaceLuaTags(const std::string& text) |
---|
287 | { |
---|
288 | // create a map with all lua tags |
---|
289 | std::map<size_t, bool> luaTags; |
---|
290 | if (!getLuaTags(text, luaTags)) |
---|
291 | return ""; |
---|
292 | |
---|
293 | // Use a stringstream object to speed up the parsing |
---|
294 | std::ostringstream output; |
---|
295 | |
---|
296 | // cut the original string into pieces and put them together with print() instead of lua tags |
---|
297 | { |
---|
298 | std::map<size_t, bool>::iterator it = luaTags.begin(); |
---|
299 | bool bInPrintFunction = true; |
---|
300 | size_t start = 0; |
---|
301 | size_t end = 0; |
---|
302 | |
---|
303 | do |
---|
304 | { |
---|
305 | if (it != luaTags.end()) |
---|
306 | end = (it++)->first; |
---|
307 | else |
---|
308 | end = std::string::npos; |
---|
309 | |
---|
310 | unsigned int equalSignCounter = 0; |
---|
311 | |
---|
312 | if (bInPrintFunction) |
---|
313 | { |
---|
314 | // count ['='[ and ]'='] and replace tags with print([[ and ]]) |
---|
315 | const std::string& temp = text.substr(start, end - start); |
---|
316 | { |
---|
317 | size_t pos = 0; |
---|
318 | while ((pos = temp.find('[', pos)) != std::string::npos) |
---|
319 | { |
---|
320 | unsigned int tempCounter = 1; |
---|
321 | size_t tempPos = pos++; |
---|
322 | while (temp[++tempPos] == '=') |
---|
323 | { |
---|
324 | tempCounter++; |
---|
325 | } |
---|
326 | if (temp[tempPos] != '[') |
---|
327 | { |
---|
328 | tempCounter = 0; |
---|
329 | } |
---|
330 | else if (tempCounter == 0) |
---|
331 | { |
---|
332 | tempCounter = 1; |
---|
333 | } |
---|
334 | if (tempCounter > equalSignCounter) |
---|
335 | equalSignCounter = tempCounter; |
---|
336 | } |
---|
337 | } |
---|
338 | { |
---|
339 | size_t pos = 0; |
---|
340 | while ((pos = temp.find(']', pos)) != std::string::npos) |
---|
341 | { |
---|
342 | unsigned int tempCounter = 1; |
---|
343 | size_t tempPos = pos++; |
---|
344 | while (temp[++tempPos] == '=') |
---|
345 | { |
---|
346 | tempCounter++; |
---|
347 | } |
---|
348 | if (temp[tempPos] != ']') |
---|
349 | { |
---|
350 | tempCounter = 0; |
---|
351 | } |
---|
352 | else if (tempCounter == 0) |
---|
353 | { |
---|
354 | tempCounter = 1; |
---|
355 | } |
---|
356 | if (tempCounter > equalSignCounter) |
---|
357 | equalSignCounter = tempCounter; |
---|
358 | } |
---|
359 | } |
---|
360 | std::string equalSigns; |
---|
361 | for (unsigned int i = 0; i < equalSignCounter; i++) |
---|
362 | { |
---|
363 | equalSigns += '='; |
---|
364 | } |
---|
365 | //A newline directly after square brackets is ignored. To make sure that the string is printed |
---|
366 | //exactly as it is, including newlines at the beginning, insert a space after the brackets. |
---|
367 | bool needsExtraSpace = false; |
---|
368 | if (temp.size() > 0 && (temp[0] == '\n' || temp[0] == '\r')) // begins with \n or \r (a line break) |
---|
369 | needsExtraSpace = true; |
---|
370 | output << "print([" + equalSigns + (needsExtraSpace ? "[ " : "[") + temp + ']' + equalSigns +"])"; |
---|
371 | start = end + 5; |
---|
372 | } |
---|
373 | else |
---|
374 | { |
---|
375 | output << text.substr(start, end - start); |
---|
376 | start = end + 2; |
---|
377 | } |
---|
378 | |
---|
379 | bInPrintFunction = !bInPrintFunction; |
---|
380 | } |
---|
381 | while (end != std::string::npos); |
---|
382 | } |
---|
383 | |
---|
384 | return output.str(); |
---|
385 | } |
---|
386 | |
---|
387 | std::string Loader::removeLuaTags(const std::string& text) |
---|
388 | { |
---|
389 | // create a map with all lua tags |
---|
390 | std::map<size_t, bool> luaTags; |
---|
391 | if (!getLuaTags(text, luaTags)) |
---|
392 | return ""; |
---|
393 | |
---|
394 | // Use a stringstream object to speed up the concatenation |
---|
395 | std::ostringstream output; |
---|
396 | |
---|
397 | // cut the original string into pieces and only write the non Lua parts |
---|
398 | std::map<size_t, bool>::iterator it = luaTags.begin(); |
---|
399 | bool bLuaCode = false; |
---|
400 | size_t start = 0; |
---|
401 | size_t end = 0; |
---|
402 | |
---|
403 | do |
---|
404 | { |
---|
405 | if (it != luaTags.end()) |
---|
406 | end = (it++)->first; |
---|
407 | else |
---|
408 | end = std::string::npos; |
---|
409 | |
---|
410 | if (!bLuaCode) |
---|
411 | { |
---|
412 | output << text.substr(start, end - start); |
---|
413 | start = end + 5; |
---|
414 | } |
---|
415 | else |
---|
416 | { |
---|
417 | //Preserve the amount of lines, otherwise the linenumber from the xml parse error is useless |
---|
418 | std::string tempstring = text.substr(start, end - start); |
---|
419 | output << std::string(std::count(tempstring.begin(), tempstring.end(), '\n'), '\n'); |
---|
420 | start = end + 2; |
---|
421 | } |
---|
422 | |
---|
423 | bLuaCode = !bLuaCode; |
---|
424 | } |
---|
425 | while (end != std::string::npos); |
---|
426 | |
---|
427 | return output.str(); |
---|
428 | } |
---|
429 | } |
---|