Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 1102 was 1102, checked in by bknecht, 16 years ago

lua50 compatiple level code and loading, should work on tardis as well now

File size: 5.7 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#include "Level.h"
31#include "BaseObject.h"
32#include "Identifier.h"
33#include "Iterator.h"
34#include "Debug.h"
35#include "CoreIncludes.h"
36#include "Script.h"
37#include "Namespace.h"
38
39#include "util/tinyxml/ticpp.h"
40
41namespace orxonox
42{
43    std::vector<std::pair<const Level*, ClassTreeMask> > Loader::levels_s;
44    ClassTreeMask Loader::currentMask_s;
45
46    bool Loader::open(const Level* level, const ClassTreeMask& mask)
47    {
48        Loader::add(level, mask);
49        return Loader::load(level, mask);
50    }
51
52    void Loader::close()
53    {
54        Loader::unload();
55        Loader::levels_s.clear();
56    }
57
58    void Loader::close(const Level* level)
59    {
60        Loader::unload(level);
61        Loader::remove(level);
62    }
63
64    void Loader::add(const Level* level, const ClassTreeMask& mask)
65    {
66        Loader::levels_s.insert(Loader::levels_s.end(), std::pair<const Level*, ClassTreeMask>(level, mask));
67    }
68
69    void Loader::remove(const Level* level)
70    {
71        for (std::vector<std::pair<const Level*, ClassTreeMask> >::iterator it = Loader::levels_s.begin(); it != Loader::levels_s.end(); ++it)
72        {
73            if ((*it).first == level)
74            {
75                Loader::levels_s.erase(it);
76                break;
77            }
78        }
79    }
80
81    bool Loader::load(const ClassTreeMask& mask)
82    {
83        bool success = true;
84        for (std::vector<std::pair<const Level*, ClassTreeMask> >::iterator it = Loader::levels_s.begin(); it != Loader::levels_s.end(); ++it)
85            if (!Loader::load((*it).first, (*it).second * mask))
86                success = false;
87
88        return success;
89    }
90
91    void Loader::unload(const ClassTreeMask& mask)
92    {
93        for (Iterator<BaseObject> it = ObjectList<BaseObject>::begin(); it; )
94        {
95            if (mask.isIncluded(it->getIdentifier()))
96                delete (*(it++));
97            else
98                ++it;
99        }
100    }
101
102    bool Loader::reload(const ClassTreeMask& mask)
103    {
104        Loader::unload(mask);
105        return Loader::load(mask);
106    }
107
108    bool Loader::load(const Level* level, const ClassTreeMask& mask)
109    {
110        Loader::currentMask_s = level->getMask() * mask;
111
112        // let Lua work this out:
113        //Script* lua;
114        /*Script::loadFile(level->getFile(), true);
115        Script::init(Script::getLuaState());
116        Script::run();*/
117        Script* lua = Script::getInstance();
118        lua->loadFile(level->getFile(), true);
119        lua->run();
120
121        try
122        {
123            COUT(0) << "Start loading " << level->getFile() << "..." << std::endl;
124            COUT(3) << "Mask: " << Loader::currentMask_s << std::endl;
125
126            //ticpp::Document xmlfile(level->getFile());
127            //xmlfile.LoadFile();
128            //ticpp::Element myelement(*Script::getFileString());
129            ticpp::Document xmlfile;
130            //xmlfile.ToDocument();
131            xmlfile.Parse(lua->getLuaOutput(), true);
132
133            ticpp::Element rootElement;
134            rootElement.SetAttribute("name", "root");
135            rootElement.SetAttribute("bAutogenerated", true);
136
137            for (ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++)
138                rootElement.InsertEndChild(*child);
139
140            COUT(4) << "  creating root-namespace..." << std::endl;
141            Namespace* rootNamespace = new Namespace();
142            rootNamespace->setLoaderIndentation("    ");
143            rootNamespace->setLevel(level);
144            rootNamespace->setNamespace(rootNamespace);
145            rootNamespace->setRoot(true);
146            rootNamespace->XMLPort(rootElement, XMLPort::LoadObject);
147
148            COUT(0) << "Finished loading " << level->getFile() << "." << std::endl;
149
150            COUT(4) << "Namespace-tree:" << std::endl << rootNamespace->toString("  ") << 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.