Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

yet another debug output tweak

File size: 5.0 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            ticpp::Element rootElement;
118            rootElement.SetAttribute("name", "root");
119
120            for (ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++)
121                rootElement.InsertEndChild(*child);
122
123            COUT(4) << "  creating root-namespace..." << std::endl;
124            Namespace* rootNamespace = new Namespace();
125            rootNamespace->setLoaderIndentation("    ");
126            rootNamespace->setLevel(level);
127            rootNamespace->setNamespace(rootNamespace);
128            rootNamespace->deleteNamespaceNodesAfterDestruction(true);
129            rootNamespace->XMLPort(rootElement, true);
130
131            COUT(0) << "Finished loading " << level->getFile() << "." << std::endl;
132
133            return true;
134        }
135        catch(ticpp::Exception& ex)
136        {
137            COUT(1) << std::endl;
138            COUT(1) << "An error occurred in Loader.cc while loading " << level->getFile() << ":" << std::endl;
139            COUT(1) << ex.what() << std::endl;
140            COUT(1) << "Loading aborted." << std::endl;
141            return false;
142        }
143    }
144
145    void Loader::unload(const Level* level, const ClassTreeMask& mask)
146    {
147        for (Iterator<BaseObject> it = ObjectList<BaseObject>::begin(); it; )
148        {
149            if ((it->getLevel() == level) && mask.isIncluded(it->getIdentifier()))
150                delete (*(it++));
151            else
152                ++it;
153        }
154    }
155
156    bool Loader::reload(const Level* level, const ClassTreeMask& mask)
157    {
158        Loader::unload(level, mask);
159        return Loader::load(level, mask);
160    }
161}
Note: See TracBrowser for help on using the repository browser.