Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/core/Loader.cc @ 3196

Last change on this file since 3196 was 3196, checked in by rgrieder, 15 years ago

Merged pch branch back to trunk.

  • Property svn:eol-style set to native
File size: 7.3 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 <tinyxml/ticpp.h>
32#include <boost/filesystem.hpp>
33
34#include "util/Debug.h"
35#include "util/Exception.h"
36#include "BaseObject.h"
37#include "Core.h"
38#include "Iterator.h"
39#include "ObjectList.h"
40#include "LuaBind.h"
41#include "Namespace.h"
42#include "XMLFile.h"
43
44namespace orxonox
45{
46    std::vector<std::pair<const XMLFile*, ClassTreeMask> > Loader::files_s;
47    ClassTreeMask Loader::currentMask_s;
48
49    bool Loader::open(const XMLFile* file, const ClassTreeMask& mask)
50    {
51        Loader::add(file, mask);
52        return Loader::load(file, mask);
53    }
54
55    void Loader::close()
56    {
57        Loader::unload();
58        Loader::files_s.clear();
59    }
60
61    void Loader::close(const XMLFile* file)
62    {
63        Loader::unload(file);
64        Loader::remove(file);
65    }
66
67    void Loader::add(const XMLFile* file, const ClassTreeMask& mask)
68    {
69        if (!file)
70            return;
71        Loader::files_s.insert(Loader::files_s.end(), std::pair<const XMLFile*, ClassTreeMask>(file, mask));
72    }
73
74    void Loader::remove(const XMLFile* file)
75    {
76        if (!file)
77            return;
78        for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it)
79        {
80            if ((*it).first == file)
81            {
82                Loader::files_s.erase(it);
83                break;
84            }
85        }
86    }
87
88    bool Loader::load(const ClassTreeMask& mask)
89    {
90        bool success = true;
91        for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it)
92            if (!Loader::load((*it).first, (*it).second * mask))
93                success = false;
94
95        return success;
96    }
97
98    void Loader::unload(const ClassTreeMask& mask)
99    {
100        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); )
101        {
102            if (mask.isIncluded(it->getIdentifier()))
103                delete (*(it++));
104            else
105                ++it;
106        }
107    }
108
109    bool Loader::reload(const ClassTreeMask& mask)
110    {
111        Loader::unload(mask);
112        return Loader::load(mask);
113    }
114
115    bool Loader::load(const XMLFile* file, const ClassTreeMask& mask)
116    {
117        if (!file)
118            return false;
119
120        Loader::currentMask_s = file->getMask() * mask;
121
122        // let Lua work this out:
123        LuaBind& lua = LuaBind::getInstance();
124        lua.clearLuaOutput();
125        lua.loadFile(file->getFilename(), true);
126        lua.run();
127
128        try
129        {
130            COUT(0) << "Start loading " << file->getFilename() << "..." << std::endl;
131            COUT(3) << "Mask: " << Loader::currentMask_s << std::endl;
132
133            //ticpp::Document xmlfile(file->getFilename());
134            //xmlfile.LoadFile();
135            //ticpp::Element myelement(*Script::getFileString());
136            ticpp::Document xmlfile;
137            //xmlfile.ToDocument();
138            xmlfile.Parse(lua.getLuaOutput(), true);
139
140            ticpp::Element rootElement;
141            rootElement.SetAttribute("name", "root");
142            rootElement.SetAttribute("bAutogenerated", true);
143
144            for (ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++)
145                rootElement.InsertEndChild(*child);
146
147            COUT(4) << "  creating root-namespace..." << std::endl;
148            Namespace* rootNamespace = new Namespace(0);
149            rootNamespace->setLoaderIndentation("    ");
150            rootNamespace->setFile(file);
151            rootNamespace->setNamespace(rootNamespace);
152            rootNamespace->setRoot(true);
153            rootNamespace->XMLPort(rootElement, XMLPort::LoadObject);
154
155            COUT(0) << "Finished loading " << file->getFilename() << "." << std::endl;
156
157            COUT(4) << "Namespace-tree:" << std::endl << rootNamespace->toString("  ") << std::endl;
158
159            return true;
160        }
161        catch (ticpp::Exception& ex)
162        {
163            COUT(1) << std::endl;
164            COUT(1) << "An XML-error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl;
165            COUT(1) << ex.what() << std::endl;
166            COUT(1) << "Loading aborted." << std::endl;
167            return false;
168        }
169        catch (Exception& ex)
170        {
171            COUT(1) << std::endl;
172            COUT(1) << "A loading-error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl;
173            COUT(1) << ex.what() << std::endl;
174            COUT(1) << "Loading aborted." << std::endl;
175            return false;
176        }
177        catch (std::exception& ex)
178        {
179            COUT(1) << std::endl;
180            COUT(1) << "An error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl;
181            COUT(1) << ex.what() << std::endl;
182            COUT(1) << "Loading aborted." << std::endl;
183            return false;
184        }
185        catch (...)
186        {
187            COUT(1) << std::endl;
188            COUT(1) << "An unknown error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl;
189            COUT(1) << "Loading aborted." << std::endl;
190            return false;
191        }
192    }
193
194    void Loader::unload(const XMLFile* file, const ClassTreeMask& mask)
195    {
196        if (!file)
197            return;
198        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; )
199        {
200            if ((it->getFile() == file) && mask.isIncluded(it->getIdentifier()))
201                delete (*(it++));
202            else
203                ++it;
204        }
205    }
206
207    bool Loader::reload(const XMLFile* file, const ClassTreeMask& mask)
208    {
209        Loader::unload(file, mask);
210        return Loader::load(file, mask);
211    }
212
213    std::vector<std::string> Loader::getLevelList()
214    {
215        std::vector<std::string> levelList;
216
217        boost::filesystem::directory_iterator file(Core::getMediaPathString() + "levels");
218        boost::filesystem::directory_iterator end;
219
220        while (file != end)
221        {
222            if (!boost::filesystem::is_directory(*file) && file->string()[file->string().length()-1] != '~')
223            {
224                std::string filename = file->path().leaf();
225                if (filename.length() > 4)
226                    levelList.push_back(filename.substr(0,filename.length()-4));
227            }
228            ++file;
229        }
230        return levelList;
231    }
232}
Note: See TracBrowser for help on using the repository browser.