Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/resource2/src/core/Loader.cc @ 5654

Last change on this file since 5654 was 5654, checked in by rgrieder, 15 years ago
  • Implemented file management via resource manager and loading of resource locations via XML. Changes made:
    • SoundManager loads via memory stream rather than via file
    • Loader uses LuaState::includeFile() to load an XML file and passes the lua tag remover function to its LuaState.
    • ConfigFileManager still loads with hard paths because the files are required before Ogre gets created
  • Renamed LuaBind to LuaState, deSingletonised it and added new features:
    • doFile(), doString(), includeFile(), includeString() where include will preparse the string with a function provided with LuaState::setIncludeParser
    • Moved lua tags replace function to Loader (since it's actually an XML related task)
    • Using data_path/lua/LuaInitScript.lua to provide the following functions
      • logMessage(level, message)
      • doFile, dofile, include (all working with relative paths but within the same resource group)
  • Modified Script class to work with LuaState and fixed its XML Loader
  • Adjusted all level and include files (both "include" and "dofile" lua commands)
  • Property svn:eol-style set to native
File size: 11.9 KB
Line 
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
46namespace 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        // Use the LuaState to replace the XML tags (calls our function)
125        scoped_ptr<LuaState> luaState(new LuaState());
126        luaState->setIncludeParser(&Loader::replaceLuaTags);
127        luaState->includeFile(file->getFilename(), file->getResourceGroup(), false);
128        //luaState->doString(luaInput);
129
130        try
131        {
132            COUT(0) << "Start loading " << file->getFilename() << "..." << std::endl;
133            COUT(3) << "Mask: " << Loader::currentMask_s << std::endl;
134
135            ticpp::Document xmlfile(file->getFilename());
136            xmlfile.Parse(luaState->getOutput().str(), true);
137
138            ticpp::Element rootElement;
139            rootElement.SetAttribute("name", "root");
140            rootElement.SetAttribute("bAutogenerated", true);
141
142            for (ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++)
143                rootElement.InsertEndChild(*child);
144
145            COUT(4) << "  creating root-namespace..." << std::endl;
146            Namespace* rootNamespace = new Namespace(0);
147            rootNamespace->setLoaderIndentation("    ");
148            rootNamespace->setFile(file);
149            rootNamespace->setNamespace(rootNamespace);
150            rootNamespace->setRoot(true);
151            rootNamespace->XMLPort(rootElement, XMLPort::LoadObject);
152
153            COUT(0) << "Finished loading " << file->getFilename() << "." << std::endl;
154
155            COUT(4) << "Namespace-tree:" << std::endl << rootNamespace->toString("  ") << std::endl;
156
157            return true;
158        }
159        catch (ticpp::Exception& ex)
160        {
161            COUT(1) << std::endl;
162            COUT(1) << "An XML-error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl;
163            COUT(1) << ex.what() << std::endl;
164            COUT(1) << "Loading aborted." << std::endl;
165            return false;
166        }
167        catch (Exception& ex)
168        {
169            COUT(1) << std::endl;
170            COUT(1) << "A loading-error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl;
171            COUT(1) << ex.what() << std::endl;
172            COUT(1) << "Loading aborted." << std::endl;
173            return false;
174        }
175        catch (std::exception& ex)
176        {
177            COUT(1) << std::endl;
178            COUT(1) << "An error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl;
179            COUT(1) << ex.what() << std::endl;
180            COUT(1) << "Loading aborted." << std::endl;
181            return false;
182        }
183        catch (...)
184        {
185            COUT(1) << std::endl;
186            COUT(1) << "An unknown error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl;
187            COUT(1) << "Loading aborted." << std::endl;
188            return false;
189        }
190    }
191
192    void Loader::unload(const XMLFile* file, const ClassTreeMask& mask)
193    {
194        if (!file)
195            return;
196        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; )
197        {
198            if ((it->getFile() == file) && mask.isIncluded(it->getIdentifier()))
199                delete (*(it++));
200            else
201                ++it;
202        }
203    }
204
205    bool Loader::reload(const XMLFile* file, const ClassTreeMask& mask)
206    {
207        Loader::unload(file, mask);
208        return Loader::load(file, mask);
209    }
210
211    std::string Loader::replaceLuaTags(const std::string& text)
212    {
213        // chreate map with all Lua tags
214        std::map<size_t, bool> luaTags;
215        {
216            size_t pos = 0;
217            while ((pos = text.find("<?lua", pos)) != std::string::npos)
218                luaTags[pos++] = true;
219        }
220        {
221            size_t pos = 0;
222            while ((pos = text.find("?>", pos)) != std::string::npos)
223                luaTags[pos++] = false;
224        }
225
226        // erase all tags from the map that are between two quotes
227        {
228            std::map<size_t, bool>::iterator it = luaTags.begin();
229            std::map<size_t, bool>::iterator it2 = it;
230            bool bBetweenQuotes = false;
231            size_t pos = 0;
232            while ((pos = getNextQuote(text, pos)) != std::string::npos)
233            {
234                while ((it != luaTags.end()) && (it->first < pos))
235                {
236                    if (bBetweenQuotes)
237                    {
238                        it2++;
239                        if(it->second && !(it2->second) && it2->first < pos)
240                            it = ++it2;
241                        else
242                            luaTags.erase(it++);
243                    }
244                    else
245                        ++it;
246                }
247                bBetweenQuotes = !bBetweenQuotes;
248                pos++;
249            }
250        }
251
252        // check whether on every opening <?lua tag a closing ?> tag follows
253        {
254            bool expectedValue = true;
255            for (std::map<size_t, bool>::iterator it = luaTags.begin(); it != luaTags.end(); ++it)
256            {
257                if (it->second == expectedValue)
258                    expectedValue = !expectedValue;
259                else
260                {
261                    expectedValue = false;
262                    break;
263                }
264            }
265            if (!expectedValue)
266            {
267                COUT(2) << "Warning: Error in level file" << std::endl;
268                // todo: errorhandling
269                return "";
270            }
271        }
272
273        // Use a stringstream object to speed up the parsing
274        std::ostringstream output;
275
276        // cut the original string into pieces and put them together with print() instead of lua tags
277        {
278            std::map<size_t, bool>::iterator it = luaTags.begin();
279            bool bInPrintFunction = true;
280            size_t start = 0;
281            size_t end = 0;
282
283            do
284            {
285                if (it != luaTags.end())
286                    end = (*(it++)).first;
287                else
288                    end = std::string::npos;
289
290                unsigned int equalSignCounter = 0;
291
292                if (bInPrintFunction)
293                {
294                    // count ['='[ and ]'='] and replace tags with print([[ and ]])
295                    std::string temp = text.substr(start, end - start);
296                    {
297                    size_t pos = 0;
298                    while ((pos = temp.find('[', pos)) != std::string::npos)
299                    {
300                        unsigned int tempCounter = 1;
301                        size_t tempPos = pos++;
302                        while(temp[++tempPos] == '=')
303                        {
304                            tempCounter++;
305                        }
306                        if(temp[tempPos] != '[')
307                        {
308                            tempCounter = 0;
309                        }
310                        else if(tempCounter == 0)
311                        {
312                            tempCounter = 1;
313                        }
314                        if (tempCounter > equalSignCounter)
315                            equalSignCounter = tempCounter;
316                        }
317                    }
318                    {
319                        size_t pos = 0;
320                        while ((pos = temp.find(']', pos)) != std::string::npos)
321                        {
322                            unsigned int tempCounter = 1;
323                            size_t tempPos = pos++;
324                            while(temp[++tempPos] == '=')
325                            {
326                                tempCounter++;
327                            }
328                            if(temp[tempPos] != ']')
329                            {
330                                tempCounter = 0;
331                            }
332                            else if(tempCounter == 0)
333                            {
334                                tempCounter = 1;
335                            }
336                            if (tempCounter > equalSignCounter)
337                                equalSignCounter = tempCounter;
338                        }
339                    }
340                    std::string equalSigns = "";
341                    for(unsigned int i = 0; i < equalSignCounter; i++)
342                    {
343                        equalSigns += "=";
344                    }
345                    output << "print([" + equalSigns + "[" + temp + "]" + equalSigns +"])";
346                    start = end + 5;
347                }
348                else
349                {
350                    output << text.substr(start, end - start);
351                    start = end + 2;
352                }
353
354                bInPrintFunction = !bInPrintFunction;
355            }
356            while (end != std::string::npos);
357        }
358
359        return output.str();
360    }
361}
Note: See TracBrowser for help on using the repository browser.