Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/Loader.cc @ 10270

Last change on this file since 10270 was 10269, checked in by muemart, 11 years ago

Tweak the xml error message a bit

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