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