Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core2/src/orxonox/core/Loader.cc @ 901

Last change on this file since 901 was 901, checked in by landauf, 16 years ago

Namespaces are working now. I love this feature, can't stop playing with it :D

File size: 5.2 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *
4 *
5 *   License notice:
6 *
7 *   This program is free software; you can redistribute it and/or
8 *   modify it under the terms of the GNU General Public License
9 *   as published by the Free Software Foundation; either version 2
10 *   of the License, or (at your option) any later version.
11 *
12 *   This program is distributed in the hope that it will be useful,
13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *   GNU General Public License for more details.
16 *
17 *   You should have received a copy of the GNU General Public License
18 *   along with this program; if not, write to the Free Software
19 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 *
21 *   Author:
22 *      Fabian 'x3n' Landau
23 *   Co-authors:
24 *      ...
25 *
26 */
27
28#include "Loader.h"
29#include "Level.h"
30#include "BaseObject.h"
31#include "Identifier.h"
32#include "Iterator.h"
33#include "Debug.h"
34#include "CoreIncludes.h"
35#include "Namespace.h"
36
37#include "util/tinyxml/ticpp.h"
38
39namespace orxonox
40{
41    std::vector<std::pair<const Level*, ClassTreeMask> > Loader::levels_s;
42    ClassTreeMask Loader::currentMask_s;
43
44    bool Loader::open(const Level* level, const ClassTreeMask& mask)
45    {
46        Loader::add(level, mask);
47        return Loader::load(level, mask);
48    }
49
50    void Loader::close()
51    {
52        Loader::unload();
53        Loader::levels_s.clear();
54    }
55
56    void Loader::close(const Level* level)
57    {
58        Loader::unload(level);
59        Loader::remove(level);
60    }
61
62    void Loader::add(const Level* level, const ClassTreeMask& mask)
63    {
64        Loader::levels_s.insert(Loader::levels_s.end(), std::pair<const Level*, ClassTreeMask>(level, mask));
65    }
66
67    void Loader::remove(const Level* level)
68    {
69        for (std::vector<std::pair<const Level*, ClassTreeMask> >::iterator it = Loader::levels_s.begin(); it != Loader::levels_s.end(); ++it)
70        {
71            if ((*it).first == level)
72            {
73                Loader::levels_s.erase(it);
74                break;
75            }
76        }
77    }
78
79    bool Loader::load(const ClassTreeMask& mask)
80    {
81        bool success = true;
82        for (std::vector<std::pair<const Level*, ClassTreeMask> >::iterator it = Loader::levels_s.begin(); it != Loader::levels_s.end(); ++it)
83            if (!Loader::load((*it).first, (*it).second * mask))
84                success = false;
85
86        return success;
87    }
88
89    void Loader::unload(const ClassTreeMask& mask)
90    {
91        for (Iterator<BaseObject> it = ObjectList<BaseObject>::begin(); it; )
92        {
93            if (mask.isIncluded(it->getIdentifier()))
94                delete (*(it++));
95            else
96                ++it;
97        }
98    }
99
100    bool Loader::reload(const ClassTreeMask& mask)
101    {
102        Loader::unload(mask);
103        return Loader::load(mask);
104    }
105
106    bool Loader::load(const Level* level, const ClassTreeMask& mask)
107    {
108        Loader::currentMask_s = level->getMask() * mask;
109
110        try
111        {
112            COUT(0) << "Start loading " << level->getFile() << "..." << std::endl;
113            COUT(3) << "Mask: " << Loader::currentMask_s << std::endl;
114
115            ticpp::Document xmlfile(level->getFile());
116            xmlfile.LoadFile();
117
118            ticpp::Element rootElement;
119            rootElement.SetAttribute("name", "root");
120            rootElement.SetAttribute("bAutogenerated", true);
121
122            for (ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++)
123                rootElement.InsertEndChild(*child);
124
125            COUT(4) << "  creating root-namespace..." << std::endl;
126            Namespace* rootNamespace = new Namespace();
127            rootNamespace->setLoaderIndentation("    ");
128            rootNamespace->setLevel(level);
129            rootNamespace->setNamespace(rootNamespace);
130            rootNamespace->setRoot(true);
131            rootNamespace->XMLPort(rootElement, true);
132
133            COUT(0) << "Finished loading " << level->getFile() << "." << std::endl;
134
135            COUT(4) << "Namespace-tree:" << std::endl << rootNamespace->toString("  ") << std::endl;
136
137            return true;
138        }
139        catch(ticpp::Exception& ex)
140        {
141            COUT(1) << std::endl;
142            COUT(1) << "An error occurred in Loader.cc while loading " << level->getFile() << ":" << std::endl;
143            COUT(1) << ex.what() << std::endl;
144            COUT(1) << "Loading aborted." << std::endl;
145            return false;
146        }
147    }
148
149    void Loader::unload(const Level* level, const ClassTreeMask& mask)
150    {
151        for (Iterator<BaseObject> it = ObjectList<BaseObject>::begin(); it; )
152        {
153            if ((it->getLevel() == level) && mask.isIncluded(it->getIdentifier()))
154                delete (*(it++));
155            else
156                ++it;
157        }
158    }
159
160    bool Loader::reload(const Level* level, const ClassTreeMask& mask)
161    {
162        Loader::unload(level, mask);
163        return Loader::load(level, mask);
164    }
165}
Note: See TracBrowser for help on using the repository browser.