Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7873 was 7648, checked in by dafrick, 14 years ago

Merged releasetodo, containing a new way to describe and tag levels, back to trunk.

  • Property svn:eol-style set to native
File size: 14.2 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>
[2710]34
[3196]35#include "util/Debug.h"
36#include "util/Exception.h"
[5695]37#include "util/StringUtils.h"
[1505]38#include "BaseObject.h"
39#include "Iterator.h"
[1747]40#include "ObjectList.h"
[5695]41#include "LuaState.h"
[1505]42#include "Namespace.h"
[5695]43#include "Resource.h"
[3196]44#include "XMLFile.h"
[1505]45
46namespace orxonox
47{
[2087]48    std::vector<std::pair<const XMLFile*, ClassTreeMask> > Loader::files_s;
[1505]49    ClassTreeMask Loader::currentMask_s;
50
[2087]51    bool Loader::open(const XMLFile* file, const ClassTreeMask& mask)
[1505]52    {
[2087]53        Loader::add(file, mask);
54        return Loader::load(file, mask);
[1505]55    }
56
57    void Loader::close()
58    {
59        Loader::unload();
[2087]60        Loader::files_s.clear();
[1505]61    }
62
[2087]63    void Loader::close(const XMLFile* file)
[1505]64    {
[2087]65        Loader::unload(file);
66        Loader::remove(file);
[1505]67    }
68
[2087]69    void Loader::add(const XMLFile* file, const ClassTreeMask& mask)
[1505]70    {
[2087]71        if (!file)
[1755]72            return;
[2087]73        Loader::files_s.insert(Loader::files_s.end(), std::pair<const XMLFile*, ClassTreeMask>(file, mask));
[1505]74    }
75
[2087]76    void Loader::remove(const XMLFile* file)
[1505]77    {
[2087]78        if (!file)
[1755]79            return;
[2087]80        for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it)
[1505]81        {
[6417]82            if (it->first == file)
[1505]83            {
[2087]84                Loader::files_s.erase(it);
[1505]85                break;
86            }
87        }
88    }
89
[7648]90    /**
91    @brief
92        Loads all opened files, while conforming to the restrictions given by the input ClassTreeMask.
93    @param mask
94        A ClassTreeMask, which defines which types of classes are loaded and which aren't.
95    @param verbose
96        Whether the loader is verbose (prints its progress in a low output level) or not.
97    @return
98        Returns true if successful.
99    */
100    bool Loader::load(const ClassTreeMask& mask, bool verbose)
[1505]101    {
102        bool success = true;
[2087]103        for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it)
[7648]104            if (!Loader::load(it->first, it->second * mask, verbose))
[1505]105                success = false;
106
107        return success;
108    }
109
110    void Loader::unload(const ClassTreeMask& mask)
111    {
[1747]112        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); )
[1505]113        {
114            if (mask.isIncluded(it->getIdentifier()))
[5929]115                (it++)->destroy();
[1505]116            else
117                ++it;
118        }
119    }
120
[7648]121    /**
122    @brief
123        Reloads all opened files, while conforming to the restrictions given by the input ClassTreeMask.
124    @param mask
125        A ClassTreeMask, which defines which types of classes are reloaded and which aren't.
126    @param verbose
127        Whether the loader is verbose (prints its progress in a low output level) or not.
128    @return
129        Returns true if successful.
130    */
131    bool Loader::reload(const ClassTreeMask& mask, bool verbose)
[1505]132    {
133        Loader::unload(mask);
[7648]134        return Loader::load(mask, verbose);
[1505]135    }
136
[7648]137    /**
138    @brief
139        Loads the input file, while conforming to the restrictions given by the input ClassTreeMask.
140    @param file
141        The file to be loaded.
142    @param mask
143        A ClassTreeMask, which defines which types of classes are loaded and which aren't.
144    @param verbose
145        Whether the loader is verbose (prints its progress in a low output level) or not.
146    @return
147        Returns true if successful.
148    */
149    bool Loader::load(const XMLFile* file, const ClassTreeMask& mask, bool verbose)
[1505]150    {
[2087]151        if (!file)
[1755]152            return false;
153
[2087]154        Loader::currentMask_s = file->getMask() * mask;
[1505]155
[5695]156        std::string xmlInput;
157        if (file->getLuaSupport())
158        {
159            // Use the LuaState to replace the XML tags (calls our function)
160            scoped_ptr<LuaState> luaState(new LuaState());
161            luaState->setIncludeParser(&Loader::replaceLuaTags);
[6417]162            luaState->includeFile(file->getFilename());
[5695]163            xmlInput = luaState->getOutput().str();
164        }
165        else
166        {
[6417]167            shared_ptr<ResourceInfo> info = Resource::getInfo(file->getFilename());
[5695]168            if (info == NULL)
169            {
170                COUT(1) << "Error: Could not find XML file '" << file->getFilename() << "'." << std::endl;
171                return false;
172            }
[6417]173            xmlInput = Resource::open(file->getFilename())->getAsString();
[5695]174        }
[1505]175
176        try
177        {
[7648]178            if(verbose)
179            {
180                COUT(0) << "Start loading " << file->getFilename() << "..." << std::endl;
181                COUT(3) << "Mask: " << Loader::currentMask_s << std::endl;
182            }
183            else
184            {
185                COUT(4) << "Start loading " << file->getFilename() << "..." << std::endl;
186                COUT(4) << "Mask: " << Loader::currentMask_s << std::endl;
187            }
[1505]188
[5695]189            ticpp::Document xmlfile(file->getFilename());
190            xmlfile.Parse(xmlInput, true);
[1505]191
192            ticpp::Element rootElement;
193            rootElement.SetAttribute("name", "root");
194            rootElement.SetAttribute("bAutogenerated", true);
195
196            for (ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++)
197                rootElement.InsertEndChild(*child);
198
199            COUT(4) << "  creating root-namespace..." << std::endl;
[2087]200            Namespace* rootNamespace = new Namespace(0);
[1505]201            rootNamespace->setLoaderIndentation("    ");
[2087]202            rootNamespace->setFile(file);
[1505]203            rootNamespace->setNamespace(rootNamespace);
204            rootNamespace->setRoot(true);
205            rootNamespace->XMLPort(rootElement, XMLPort::LoadObject);
206
[7648]207            if(verbose)
208                COUT(0) << "Finished loading " << file->getFilename() << '.' << std::endl;
209            else
210                COUT(4) << "Finished loading " << file->getFilename() << '.' << std::endl;
[1505]211
212            COUT(4) << "Namespace-tree:" << std::endl << rootNamespace->toString("  ") << std::endl;
213
214            return true;
215        }
[2171]216        catch (ticpp::Exception& ex)
[1505]217        {
218            COUT(1) << std::endl;
[6417]219            COUT(1) << "An XML-error occurred in Loader.cc while loading " << file->getFilename() << ':' << std::endl;
[2171]220            COUT(1) << ex.what() << std::endl;
221            COUT(1) << "Loading aborted." << std::endl;
222            return false;
223        }
224        catch (Exception& ex)
225        {
226            COUT(1) << std::endl;
[6417]227            COUT(1) << "A loading-error occurred in Loader.cc while loading " << file->getFilename() << ':' << std::endl;
[2171]228            COUT(1) << ex.what() << std::endl;
229            COUT(1) << "Loading aborted." << std::endl;
230            return false;
231        }
[5747]232        catch (...)
[2171]233        {
234            COUT(1) << std::endl;
[6417]235            COUT(1) << "An error occurred in Loader.cc while loading " << file->getFilename() << ':' << std::endl;
[5747]236            COUT(1) << Exception::handleMessage() << std::endl;
[1505]237            COUT(1) << "Loading aborted." << std::endl;
238            return false;
239        }
240    }
241
[2087]242    void Loader::unload(const XMLFile* file, const ClassTreeMask& mask)
[1505]243    {
[2087]244        if (!file)
[1755]245            return;
[1747]246        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; )
[1505]247        {
[2087]248            if ((it->getFile() == file) && mask.isIncluded(it->getIdentifier()))
[5929]249                (it++)->destroy();
[1505]250            else
251                ++it;
252        }
253    }
254
[7648]255    /**
256    @brief
257        Reloads the input file, while conforming to the restrictions given by the input ClassTreeMask.
258    @param file
259        The file to be reloaded.
260    @param mask
261        A ClassTreeMask, which defines which types of classes are reloaded and which aren't.
262    @param verbose
263        Whether the loader is verbose (prints its progress in a low output level) or not.
264    @return
265        Returns true if successful.
266    */
267    bool Loader::reload(const XMLFile* file, const ClassTreeMask& mask, bool verbose)
[1505]268    {
[2087]269        Loader::unload(file, mask);
[7648]270        return Loader::load(file, mask, verbose);
[1505]271    }
[5695]272
273    std::string Loader::replaceLuaTags(const std::string& text)
274    {
[6417]275        // create map with all Lua tags
[5695]276        std::map<size_t, bool> luaTags;
277        {
278            size_t pos = 0;
279            while ((pos = text.find("<?lua", pos)) != std::string::npos)
280                luaTags[pos++] = true;
281        }
282        {
283            size_t pos = 0;
284            while ((pos = text.find("?>", pos)) != std::string::npos)
285                luaTags[pos++] = false;
286        }
287
288        // erase all tags from the map that are between two quotes
289        {
290            std::map<size_t, bool>::iterator it = luaTags.begin();
291            std::map<size_t, bool>::iterator it2 = it;
292            bool bBetweenQuotes = false;
293            size_t pos = 0;
294            while ((pos = getNextQuote(text, pos)) != std::string::npos)
295            {
296                while ((it != luaTags.end()) && (it->first < pos))
297                {
298                    if (bBetweenQuotes)
299                    {
300                        it2++;
[6422]301                        if (it->second && !(it2->second) && it2->first < pos)
[5695]302                            it = ++it2;
303                        else
304                            luaTags.erase(it++);
305                    }
306                    else
307                        ++it;
308                }
309                bBetweenQuotes = !bBetweenQuotes;
310                pos++;
311            }
312        }
313
314        // check whether on every opening <?lua tag a closing ?> tag follows
315        {
316            bool expectedValue = true;
317            for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it)
318            {
319                if (it->second == expectedValue)
320                    expectedValue = !expectedValue;
321                else
322                {
323                    expectedValue = false;
324                    break;
325                }
326            }
327            if (!expectedValue)
328            {
329                COUT(2) << "Warning: Error in level file" << std::endl;
330                // todo: errorhandling
331                return "";
332            }
333        }
334
335        // Use a stringstream object to speed up the parsing
336        std::ostringstream output;
337
338        // cut the original string into pieces and put them together with print() instead of lua tags
339        {
340            std::map<size_t, bool>::iterator it = luaTags.begin();
341            bool bInPrintFunction = true;
342            size_t start = 0;
343            size_t end = 0;
344
345            do
346            {
347                if (it != luaTags.end())
[6417]348                    end = (it++)->first;
[5695]349                else
350                    end = std::string::npos;
351
352                unsigned int equalSignCounter = 0;
353
354                if (bInPrintFunction)
355                {
356                    // count ['='[ and ]'='] and replace tags with print([[ and ]])
[6417]357                    const std::string& temp = text.substr(start, end - start);
[5695]358                    {
359                    size_t pos = 0;
360                    while ((pos = temp.find('[', pos)) != std::string::npos)
361                    {
362                        unsigned int tempCounter = 1;
363                        size_t tempPos = pos++;
[6422]364                        while (temp[++tempPos] == '=')
[5695]365                        {
366                            tempCounter++;
367                        }
[6422]368                        if (temp[tempPos] != '[')
[5695]369                        {
370                            tempCounter = 0;
371                        }
[6422]372                        else if (tempCounter == 0)
[5695]373                        {
374                            tempCounter = 1;
375                        }
376                        if (tempCounter > equalSignCounter)
377                            equalSignCounter = tempCounter;
378                        }
379                    }
380                    {
381                        size_t pos = 0;
382                        while ((pos = temp.find(']', pos)) != std::string::npos)
383                        {
384                            unsigned int tempCounter = 1;
385                            size_t tempPos = pos++;
[6422]386                            while (temp[++tempPos] == '=')
[5695]387                            {
388                                tempCounter++;
389                            }
[6422]390                            if (temp[tempPos] != ']')
[5695]391                            {
392                                tempCounter = 0;
393                            }
[6422]394                            else if (tempCounter == 0)
[5695]395                            {
396                                tempCounter = 1;
397                            }
398                            if (tempCounter > equalSignCounter)
399                                equalSignCounter = tempCounter;
400                        }
401                    }
[6417]402                    std::string equalSigns;
[6422]403                    for (unsigned int i = 0; i < equalSignCounter; i++)
[5695]404                    {
[6417]405                        equalSigns += '=';
[5695]406                    }
[6417]407                    output << "print([" + equalSigns + '[' + temp + ']' + equalSigns +"])";
[5695]408                    start = end + 5;
409                }
410                else
411                {
412                    output << text.substr(start, end - start);
413                    start = end + 2;
414                }
415
416                bInPrintFunction = !bInPrintFunction;
417            }
418            while (end != std::string::npos);
419        }
420
421        return output.str();
422    }
[1505]423}
Note: See TracBrowser for help on using the repository browser.