Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

new loader-concept is taking shape, but not yet finished

added new classes: Functor, Executor and XMLPortParamContainer.
more to come (i.e. XMLPortObjectContainer)

Executor might be useless, depends on how we'll implement the ingame shell-commands and the keybindings

moved all non-identifier-related functions and variables from OrxonoxClass to BaseObject. this might result in some casts to BaseObject when working with all objects of an interface (like Synchronisable), but I think it improves the class hierarchy.

File size: 4.9 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
35//#define TIXML_USE_TICPP
36#include "util/tinyxml/ticpp.h"
37
38namespace orxonox
39{
40    std::vector<std::pair<const Level*, ClassTreeMask> > Loader::levels_s;
41
42    bool Loader::open(const Level* level, const ClassTreeMask& mask)
43    {
44        Loader::add(level, mask);
45        return Loader::load(level, mask);
46    }
47
48    void Loader::close()
49    {
50        Loader::unload();
51        Loader::levels_s.clear();
52    }
53
54    void Loader::close(const Level* level)
55    {
56        Loader::unload(level);
57        Loader::remove(level);
58    }
59
60    void Loader::add(const Level* level, const ClassTreeMask& mask)
61    {
62        Loader::levels_s.insert(Loader::levels_s.end(), std::pair<const Level*, ClassTreeMask>(level, mask));
63    }
64
65    void Loader::remove(const Level* level)
66    {
67        for (std::vector<std::pair<const Level*, ClassTreeMask> >::iterator it = Loader::levels_s.begin(); it != Loader::levels_s.end(); ++it)
68        {
69            if ((*it).first == level)
70            {
71                Loader::levels_s.erase(it);
72                break;
73            }
74        }
75    }
76
77    bool Loader::load(const ClassTreeMask& mask)
78    {
79        bool success = true;
80        for (std::vector<std::pair<const Level*, ClassTreeMask> >::iterator it = Loader::levels_s.begin(); it != Loader::levels_s.end(); ++it)
81            if (!Loader::load((*it).first, (*it).second * mask))
82                success = false;
83
84        return success;
85    }
86
87    void Loader::unload(const ClassTreeMask& mask)
88    {
89        for (Iterator<BaseObject> it = ObjectList<BaseObject>::begin(); it; )
90        {
91            if (mask.isIncluded(it->getIdentifier()))
92                delete (*(it++));
93            else
94                ++it;
95        }
96    }
97
98    bool Loader::reload(const ClassTreeMask& mask)
99    {
100        Loader::unload(mask);
101        return Loader::load(mask);
102    }
103
104    bool Loader::load(const Level* level, const ClassTreeMask& mask)
105    {
106        ClassTreeMask loadmask = level->getMask() * mask;
107
108        try
109        {
110            COUT(0) << "Start loading " << level->getFile() << "..." << std::endl;
111            COUT(3) << "Mask: " << loadmask << std::endl;
112
113            ticpp::Document xmlfile(level->getFile());
114            xmlfile.LoadFile();
115
116            for ( ticpp::Iterator< ticpp::Element > child = xmlfile.FirstChildElement(); child != child.end(); child++ )
117            {
118                Identifier* identifier = ID(child->Value());
119                if (identifier)
120                {
121                    COUT(4) << "  fabricating " << child->Value() << "..." << std::endl;
122                    BaseObject* newObject = identifier->fabricate();
123                    newObject->XMLPort(*child, true);
124                }
125                else
126                {
127                    COUT(2) << "  Warning: '"<< child->Value() <<"' is not a valid classname." << std::endl;
128                }
129            }
130
131            COUT(0) << "Finished loading " << level->getFile() << "." << std::endl;
132
133            return true;
134        }
135        catch(ticpp::Exception& ex)
136        {
137            COUT(1) << "An error occurred in Loader.cc while loading " << level->getFile() << ":" << std::endl;
138            COUT(1) << ex.what() << std::endl;
139            COUT(1) << "Loading aborted." << std::endl;
140            return false;
141        }
142    }
143
144    void Loader::unload(const Level* level, const ClassTreeMask& mask)
145    {
146        for (Iterator<BaseObject> it = ObjectList<BaseObject>::begin(); it; )
147        {
148            if ((it->getLevel() == level) && mask.isIncluded(it->getIdentifier()))
149                delete (*(it++));
150            else
151                ++it;
152        }
153    }
154
155    bool Loader::reload(const Level* level, const ClassTreeMask& mask)
156    {
157        Loader::unload(level, mask);
158        return Loader::load(level, mask);
159    }
160}
Note: See TracBrowser for help on using the repository browser.