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