Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/objecthierarchy2/src/core/Loader.cc @ 2662

Last change on this file since 2662 was 2344, checked in by rgrieder, 16 years ago

Completed destruction of static elements like XMLPort, Identifier, etc.
Of initially about 250 memory leaks (not in the actual meaning but the memory was never freed anyway) only 1 remains in TinyCpp.

  • Core class is now a normal Singleton that gets created and destroyed in main.
  • The same goes for Language, LuaBind, SignalHandler and PlayerManager.
  • Added a new std::set to the CommandExecutor so that the external ConsoleCommands can get destroyed too.
  • Code for destroying CommandLineArguments
  • Added destruction code for ConstructionCallbacks in Identifier
  • Moved internal identifier map (the one with the typeid(.) names) in a static function in Identifier. This was necessary in order to destroy ALL Identifiers with the static destruction function. Before it was possible to create an Identifier with having a class instance (that would call RegisterObject) for instance by simply accessing it via getIdentifier.
  • Removed a big memory leak in Button (forgot to destroy the ConfigValueContainers)
  • Added destruction code for InputBufferListenerTuples in InputBuffer destructor.
  • Added destruction code for load and save executors in both XMLPortParam and XMLPortObject
  • Added destruction code for ConsoleCommands in GSRoot, GSGraphics and GSLevel (temporary solution anyway)
  • Deleting the CEGUILua script module seems to work properly now, one memory leak less (GUIManager.cc)
  • Added global destruction calls in Main.cc
  • Property svn:eol-style set to native
File size: 6.7 KB
RevLine 
[1505]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"
[2087]30#include "XMLFile.h"
[1505]31#include "BaseObject.h"
32#include "Identifier.h"
33#include "Iterator.h"
[1747]34#include "ObjectList.h"
[1505]35#include "CoreIncludes.h"
[2087]36#include "LuaBind.h"
[1505]37#include "Namespace.h"
[1747]38#include "util/Debug.h"
[2171]39#include "util/Exception.h"
[1505]40
41#include "tinyxml/ticpp.h"
42
43namespace orxonox
44{
[2087]45    std::vector<std::pair<const XMLFile*, ClassTreeMask> > Loader::files_s;
[1505]46    ClassTreeMask Loader::currentMask_s;
47
[2087]48    bool Loader::open(const XMLFile* file, const ClassTreeMask& mask)
[1505]49    {
[2087]50        Loader::add(file, mask);
51        return Loader::load(file, mask);
[1505]52    }
53
54    void Loader::close()
55    {
56        Loader::unload();
[2087]57        Loader::files_s.clear();
[1505]58    }
59
[2087]60    void Loader::close(const XMLFile* file)
[1505]61    {
[2087]62        Loader::unload(file);
63        Loader::remove(file);
[1505]64    }
65
[2087]66    void Loader::add(const XMLFile* file, const ClassTreeMask& mask)
[1505]67    {
[2087]68        if (!file)
[1755]69            return;
[2087]70        Loader::files_s.insert(Loader::files_s.end(), std::pair<const XMLFile*, ClassTreeMask>(file, mask));
[1505]71    }
72
[2087]73    void Loader::remove(const XMLFile* file)
[1505]74    {
[2087]75        if (!file)
[1755]76            return;
[2087]77        for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it)
[1505]78        {
[2087]79            if ((*it).first == file)
[1505]80            {
[2087]81                Loader::files_s.erase(it);
[1505]82                break;
83            }
84        }
85    }
86
87    bool Loader::load(const ClassTreeMask& mask)
88    {
89        bool success = true;
[2087]90        for (std::vector<std::pair<const XMLFile*, ClassTreeMask> >::iterator it = Loader::files_s.begin(); it != Loader::files_s.end(); ++it)
[1505]91            if (!Loader::load((*it).first, (*it).second * mask))
92                success = false;
93
94        return success;
95    }
96
97    void Loader::unload(const ClassTreeMask& mask)
98    {
[1747]99        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it != ObjectList<BaseObject>::end(); )
[1505]100        {
101            if (mask.isIncluded(it->getIdentifier()))
102                delete (*(it++));
103            else
104                ++it;
105        }
106    }
107
108    bool Loader::reload(const ClassTreeMask& mask)
109    {
110        Loader::unload(mask);
111        return Loader::load(mask);
112    }
113
[2087]114    bool Loader::load(const XMLFile* file, const ClassTreeMask& mask)
[1505]115    {
[2087]116        if (!file)
[1755]117            return false;
118
[2087]119        Loader::currentMask_s = file->getMask() * mask;
[1505]120
121        // let Lua work this out:
[2344]122        LuaBind& lua = LuaBind::getInstance();
123        lua.clearLuaOutput();
124        lua.loadFile(file->getFilename(), true);
125        lua.run();
[1505]126
127        try
128        {
[2087]129            COUT(0) << "Start loading " << file->getFilename() << "..." << std::endl;
[1505]130            COUT(3) << "Mask: " << Loader::currentMask_s << std::endl;
131
[2087]132            //ticpp::Document xmlfile(file->getFilename());
[1505]133            //xmlfile.LoadFile();
134            //ticpp::Element myelement(*Script::getFileString());
135            ticpp::Document xmlfile;
136            //xmlfile.ToDocument();
[2344]137            xmlfile.Parse(lua.getLuaOutput(), true);
[1505]138
139            ticpp::Element rootElement;
140            rootElement.SetAttribute("name", "root");
141            rootElement.SetAttribute("bAutogenerated", true);
142
143            for (ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++)
144                rootElement.InsertEndChild(*child);
145
146            COUT(4) << "  creating root-namespace..." << std::endl;
[2087]147            Namespace* rootNamespace = new Namespace(0);
[1505]148            rootNamespace->setLoaderIndentation("    ");
[2087]149            rootNamespace->setFile(file);
[1505]150            rootNamespace->setNamespace(rootNamespace);
151            rootNamespace->setRoot(true);
152            rootNamespace->XMLPort(rootElement, XMLPort::LoadObject);
153
[2087]154            COUT(0) << "Finished loading " << file->getFilename() << "." << std::endl;
[1505]155
156            COUT(4) << "Namespace-tree:" << std::endl << rootNamespace->toString("  ") << std::endl;
157
158            return true;
159        }
[2171]160        catch (ticpp::Exception& ex)
[1505]161        {
162            COUT(1) << std::endl;
[2171]163            COUT(1) << "An XML-error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl;
164            COUT(1) << ex.what() << std::endl;
165            COUT(1) << "Loading aborted." << std::endl;
166            return false;
167        }
168        catch (Exception& ex)
169        {
170            COUT(1) << std::endl;
171            COUT(1) << "A loading-error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl;
172            COUT(1) << ex.what() << std::endl;
173            COUT(1) << "Loading aborted." << std::endl;
174            return false;
175        }
176        catch (std::exception& ex)
177        {
178            COUT(1) << std::endl;
[2087]179            COUT(1) << "An error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl;
[1505]180            COUT(1) << ex.what() << std::endl;
181            COUT(1) << "Loading aborted." << std::endl;
182            return false;
183        }
[2171]184        catch (...)
185        {
186            COUT(1) << std::endl;
187            COUT(1) << "An unknown error occurred in Loader.cc while loading " << file->getFilename() << ":" << std::endl;
188            COUT(1) << "Loading aborted." << std::endl;
189            return false;
190        }
[1505]191    }
192
[2087]193    void Loader::unload(const XMLFile* file, const ClassTreeMask& mask)
[1505]194    {
[2087]195        if (!file)
[1755]196            return;
[1747]197        for (ObjectList<BaseObject>::iterator it = ObjectList<BaseObject>::begin(); it; )
[1505]198        {
[2087]199            if ((it->getFile() == file) && mask.isIncluded(it->getIdentifier()))
[1505]200                delete (*(it++));
201            else
202                ++it;
203        }
204    }
205
[2087]206    bool Loader::reload(const XMLFile* file, const ClassTreeMask& mask)
[1505]207    {
[2087]208        Loader::unload(file, mask);
209        return Loader::load(file, mask);
[1505]210    }
211}
Note: See TracBrowser for help on using the repository browser.