Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1009 was 999, checked in by bknecht, 17 years ago

some changes with the script. Made it singleton plus the files were moved into the core.

File size: 5.8 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 "Script.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        // let Lua work this out:
111        //Script* lua;
112        /*Script::loadFile(level->getFile(), true);
113        Script::init(Script::getLuaState());
114        Script::run();*/
115        Script* lua = Script::getInstance();
116
117        try
118        {
119            COUT(0) << "Start loading " << level->getFile() << "..." << std::endl;
120            COUT(3) << "Mask: " << Loader::currentMask_s << std::endl;
121
122            //ticpp::Document xmlfile(level->getFile());
123            //xmlfile.LoadFile();
124            //ticpp::Element myelement(*Script::getFileString());
125            ticpp::Document xmlfile;
126            //xmlfile.ToDocument();
127            xmlfile.Parse(lua->getLuaOutput(), true);
128
129            for ( ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++ )
130            {
131                Identifier* identifier = ID(child->Value());
132                if (identifier)
133                {
134                    if (Loader::currentMask_s.isIncluded(identifier))
135                    {
136                        COUT(4) << "  fabricating " << child->Value() << "..." << std::endl;
137                        BaseObject* newObject = identifier->fabricate();
138                        newObject->setLoaderIndentation("    ");
139                        newObject->setLevel(level);
140                        newObject->XMLPort(*child, true);
141                        COUT(5) << "  ...fabricated " << child->Value() << " (objectname " << newObject->getName() << ")." << std::endl;
142                    }
143                }
144                else
145                {
146                    COUT(2) << "  Warning: '" << child->Value() << "' is not a valid classname." << std::endl;
147                }
148            }
149
150            COUT(0) << "Finished loading " << level->getFile() << "." << std::endl;
151
152            return true;
153        }
154        catch(ticpp::Exception& ex)
155        {
156            COUT(1) << std::endl;
157            COUT(1) << "An error occurred in Loader.cc while loading " << level->getFile() << ":" << std::endl;
158            COUT(1) << ex.what() << std::endl;
159            COUT(1) << "Loading aborted." << std::endl;
160            return false;
161        }
162    }
163
164    void Loader::unload(const Level* level, const ClassTreeMask& mask)
165    {
166        for (Iterator<BaseObject> it = ObjectList<BaseObject>::begin(); it; )
167        {
168            if ((it->getLevel() == level) && mask.isIncluded(it->getIdentifier()))
169                delete (*(it++));
170            else
171                ++it;
172        }
173    }
174
175    bool Loader::reload(const Level* level, const ClassTreeMask& mask)
176    {
177        Loader::unload(level, mask);
178        return Loader::load(level, mask);
179    }
180}
Note: See TracBrowser for help on using the repository browser.