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 | |
---|
36 | #include "util/tinyxml/ticpp.h" |
---|
37 | |
---|
38 | namespace orxonox |
---|
39 | { |
---|
40 | std::vector<std::pair<const Level*, ClassTreeMask> > Loader::levels_s; |
---|
41 | ClassTreeMask Loader::currentMask_s; |
---|
42 | |
---|
43 | bool Loader::open(const Level* level, const ClassTreeMask& mask) |
---|
44 | { |
---|
45 | Loader::add(level, mask); |
---|
46 | return Loader::load(level, mask); |
---|
47 | } |
---|
48 | |
---|
49 | void Loader::close() |
---|
50 | { |
---|
51 | Loader::unload(); |
---|
52 | Loader::levels_s.clear(); |
---|
53 | } |
---|
54 | |
---|
55 | void Loader::close(const Level* level) |
---|
56 | { |
---|
57 | Loader::unload(level); |
---|
58 | Loader::remove(level); |
---|
59 | } |
---|
60 | |
---|
61 | void Loader::add(const Level* level, const ClassTreeMask& mask) |
---|
62 | { |
---|
63 | Loader::levels_s.insert(Loader::levels_s.end(), std::pair<const Level*, ClassTreeMask>(level, mask)); |
---|
64 | } |
---|
65 | |
---|
66 | void Loader::remove(const Level* level) |
---|
67 | { |
---|
68 | for (std::vector<std::pair<const Level*, ClassTreeMask> >::iterator it = Loader::levels_s.begin(); it != Loader::levels_s.end(); ++it) |
---|
69 | { |
---|
70 | if ((*it).first == level) |
---|
71 | { |
---|
72 | Loader::levels_s.erase(it); |
---|
73 | break; |
---|
74 | } |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | bool Loader::load(const ClassTreeMask& mask) |
---|
79 | { |
---|
80 | bool success = true; |
---|
81 | for (std::vector<std::pair<const Level*, ClassTreeMask> >::iterator it = Loader::levels_s.begin(); it != Loader::levels_s.end(); ++it) |
---|
82 | if (!Loader::load((*it).first, (*it).second * mask)) |
---|
83 | success = false; |
---|
84 | |
---|
85 | return success; |
---|
86 | } |
---|
87 | |
---|
88 | void Loader::unload(const ClassTreeMask& mask) |
---|
89 | { |
---|
90 | for (Iterator<BaseObject> it = ObjectList<BaseObject>::begin(); it; ) |
---|
91 | { |
---|
92 | if (mask.isIncluded(it->getIdentifier())) |
---|
93 | delete (*(it++)); |
---|
94 | else |
---|
95 | ++it; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | bool Loader::reload(const ClassTreeMask& mask) |
---|
100 | { |
---|
101 | Loader::unload(mask); |
---|
102 | return Loader::load(mask); |
---|
103 | } |
---|
104 | |
---|
105 | bool Loader::load(const Level* level, const ClassTreeMask& mask) |
---|
106 | { |
---|
107 | Loader::currentMask_s = level->getMask() * mask; |
---|
108 | |
---|
109 | try |
---|
110 | { |
---|
111 | COUT(0) << "Start loading " << level->getFile() << "..." << std::endl; |
---|
112 | COUT(3) << "Mask: " << Loader::currentMask_s << std::endl; |
---|
113 | |
---|
114 | ticpp::Document xmlfile(level->getFile()); |
---|
115 | xmlfile.LoadFile(); |
---|
116 | |
---|
117 | for ( ticpp::Iterator<ticpp::Element> child = xmlfile.FirstChildElement(false); child != child.end(); child++ ) |
---|
118 | { |
---|
119 | Identifier* identifier = ID(child->Value()); |
---|
120 | if (identifier) |
---|
121 | { |
---|
122 | if (Loader::currentMask_s.isIncluded(identifier)) |
---|
123 | { |
---|
124 | COUT(4) << " fabricating " << child->Value() << "..." << std::endl; |
---|
125 | BaseObject* newObject = identifier->fabricate(); |
---|
126 | newObject->setLoaderIndentation(" "); |
---|
127 | newObject->setLevel(level); |
---|
128 | newObject->XMLPort(*child, true); |
---|
129 | COUT(5) << " ...fabricated " << child->Value() << " (objectname " << newObject->getName() << ")." << std::endl; |
---|
130 | } |
---|
131 | } |
---|
132 | else |
---|
133 | { |
---|
134 | COUT(2) << " Warning: '" << child->Value() << "' is not a valid classname." << std::endl; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | COUT(0) << "Finished loading " << level->getFile() << "." << std::endl; |
---|
139 | |
---|
140 | return true; |
---|
141 | } |
---|
142 | catch(ticpp::Exception& ex) |
---|
143 | { |
---|
144 | COUT(1) << std::endl; |
---|
145 | COUT(1) << "An error occurred in Loader.cc while loading " << level->getFile() << ":" << std::endl; |
---|
146 | COUT(1) << ex.what() << std::endl; |
---|
147 | COUT(1) << "Loading aborted." << std::endl; |
---|
148 | return false; |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | void Loader::unload(const Level* level, const ClassTreeMask& mask) |
---|
153 | { |
---|
154 | for (Iterator<BaseObject> it = ObjectList<BaseObject>::begin(); it; ) |
---|
155 | { |
---|
156 | if ((it->getLevel() == level) && mask.isIncluded(it->getIdentifier())) |
---|
157 | delete (*(it++)); |
---|
158 | else |
---|
159 | ++it; |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | bool Loader::reload(const Level* level, const ClassTreeMask& mask) |
---|
164 | { |
---|
165 | Loader::unload(level, mask); |
---|
166 | return Loader::load(level, mask); |
---|
167 | } |
---|
168 | } |
---|