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 | |
---|
39 | namespace 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 | lua->loadFile(level->getFile(), true); |
---|
117 | lua->run(); |
---|
118 | |
---|
119 | try |
---|
120 | { |
---|
121 | COUT(0) << "Start loading " << level->getFile() << "..." << std::endl; |
---|
122 | COUT(3) << "Mask: " << Loader::currentMask_s << std::endl; |
---|
123 | |
---|
124 | //ticpp::Document xmlfile(level->getFile()); |
---|
125 | //xmlfile.LoadFile(); |
---|
126 | //ticpp::Element myelement(*Script::getFileString()); |
---|
127 | ticpp::Document xmlfile; |
---|
128 | //xmlfile.ToDocument(); |
---|
129 | xmlfile.Parse(lua->getLuaOutput(), true); |
---|
130 | |
---|
131 | for ( ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++ ) |
---|
132 | { |
---|
133 | Identifier* identifier = ID(child->Value()); |
---|
134 | if (identifier) |
---|
135 | { |
---|
136 | if (Loader::currentMask_s.isIncluded(identifier)) |
---|
137 | { |
---|
138 | COUT(4) << " fabricating " << child->Value() << "..." << std::endl; |
---|
139 | BaseObject* newObject = identifier->fabricate(); |
---|
140 | newObject->setLoaderIndentation(" "); |
---|
141 | newObject->setLevel(level); |
---|
142 | newObject->XMLPort(*child, true); |
---|
143 | COUT(5) << " ...fabricated " << child->Value() << " (objectname " << newObject->getName() << ")." << std::endl; |
---|
144 | } |
---|
145 | } |
---|
146 | else |
---|
147 | { |
---|
148 | COUT(2) << " Warning: '" << child->Value() << "' is not a valid classname." << std::endl; |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | COUT(0) << "Finished loading " << level->getFile() << "." << 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 (Iterator<BaseObject> 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 | } |
---|