/
EClassManager.cpp
461 lines (369 loc) · 11.9 KB
/
EClassManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#include "EClassManager.h"
#include "iarchive.h"
#include "i18n.h"
#include "iregistry.h"
#include "irender.h"
#include "ieventmanager.h"
#include "icommandsystem.h"
#include "imainframe.h"
#include "iradiant.h"
#include "iuimanager.h"
#include "ifilesystem.h"
#include "parser/DefTokeniser.h"
#include "Doom3EntityClass.h"
#include "Doom3ModelDef.h"
#include <boost/algorithm/string/case_conv.hpp>
#include <functional>
#include "debugging/ScopedDebugTimer.h"
namespace eclass {
// Constructor
EClassManager::EClassManager() :
_realised(false),
_defLoader(std::bind(&EClassManager::loadDefAndResolveInheritance, this)),
_curParseStamp(0)
{}
sigc::signal<void> EClassManager::defsReloadedSignal() const
{
return _defsReloadedSignal;
}
// Get a named entity class, creating if necessary
IEntityClassPtr EClassManager::findOrInsert(const std::string& name, bool has_brushes)
{
ensureDefsLoaded();
// Return an error if no name is given
if (name.empty())
{
return IEntityClassPtr();
}
// Convert string to lowercase, for case-insensitive lookup
std::string lName = boost::algorithm::to_lower_copy(name);
// Find and return if exists
Doom3EntityClassPtr eclass = findInternal(lName);
if (eclass)
{
return eclass;
}
// Otherwise insert the new EntityClass
//IEntityClassPtr eclass = eclass::Doom3EntityClass::create(lName, has_brushes);
// greebo: Changed fallback behaviour when unknown entites are encountered to TRUE
// so that brushes of unknown entites don't get lost (issue #240)
eclass = Doom3EntityClass::create(lName, true);
// Try to insert the class
return insertUnique(eclass);
}
Doom3EntityClassPtr EClassManager::findInternal(const std::string& name)
{
// Find the EntityClass in the map.
EntityClasses::const_iterator i = _entityClasses.find(name);
return i != _entityClasses.end() ? i->second : Doom3EntityClassPtr();
}
Doom3EntityClassPtr EClassManager::insertUnique(const Doom3EntityClassPtr& eclass)
{
// Try to insert the eclass
std::pair<EntityClasses::iterator, bool> i = _entityClasses.insert(
EntityClasses::value_type(eclass->getName(), eclass)
);
// Return the pointer to the inserted eclass
return i.first->second;
}
void EClassManager::resolveModelInheritance(const std::string& name, const Doom3ModelDefPtr& model)
{
if (model->resolved == true) {
return; // inheritance already resolved
}
model->resolved = true;
if (!model->parent.empty())
{
Models::iterator i = _models.find(model->parent);
if (i == _models.end()) {
rError() << "model " << name
<< " inherits unknown model " << model->parent << std::endl;
}
else {
resolveModelInheritance(i->first, i->second);
// greebo: Only inherit the "mesh" of the parent if the current declaration doesn't have one
if (model->mesh.empty())
{
model->mesh = i->second->mesh;
}
// Only inherit the "skin" of the parent if the current declaration doesn't have one
if (model->skin.empty())
{
model->skin = i->second->skin;
}
// Append all inherited animations, if missing on the child
model->anims.insert(i->second->anims.begin(), i->second->anims.end());
}
}
}
void EClassManager::parseDefFiles()
{
rMessage() << "searching vfs directory 'def' for *.def\n";
// Increase the parse stamp for this run
_curParseStamp++;
{
ScopedDebugTimer timer("EntityDefs parsed: ");
GlobalFileSystem().forEachFile("def/", "def", [&](const std::string& filename)
{
parseFile(filename);
});
}
}
void EClassManager::resolveInheritance()
{
// Resolve inheritance on the model classes
for (Models::value_type& pair : _models)
{
resolveModelInheritance(pair.first, pair.second);
}
// Resolve inheritance for the entities. At this stage the classes
// will have the name of their parent, but not an actual pointer to
// it
for (EntityClasses::value_type& pair : _entityClasses)
{
// Tell the class to resolve its own inheritance using the given
// map as a source for parent lookup
pair.second->resolveInheritance(_entityClasses);
// If the entity has a model path ("model" key), lookup the actual
// model and apply its mesh and skin to this entity.
if (!pair.second->getModelPath().empty())
{
Models::iterator j = _models.find(pair.second->getModelPath());
if (j != _models.end())
{
pair.second->setModelPath(j->second->mesh);
pair.second->setSkin(j->second->skin);
}
}
}
// greebo: Override the eclass colours of two special entityclasses
Vector3 worlspawnColour = ColourSchemes().getColour("default_brush");
Vector3 lightColour = ColourSchemes().getColour("light_volumes");
Doom3EntityClassPtr light = findInternal("light");
if (light)
{
light->setColour(lightColour);
}
Doom3EntityClassPtr worldspawn = findInternal("worldspawn");
if (worldspawn)
{
worldspawn->setColour(worlspawnColour);
}
}
void EClassManager::ensureDefsLoaded()
{
assert(_realised);
_defLoader.ensureFinished();
}
void EClassManager::loadDefAndResolveInheritance()
{
parseDefFiles();
resolveInheritance();
}
void EClassManager::realise()
{
if (_realised)
{
return; // nothing to do anymore
}
_realised = true;
_defLoader.start();
}
// Find an entity class
IEntityClassPtr EClassManager::findClass(const std::string& className)
{
ensureDefsLoaded();
// greebo: Convert the lookup className string to lowercase first
std::string classNameLower = boost::algorithm::to_lower_copy(className);
EntityClasses::const_iterator i = _entityClasses.find(classNameLower);
return i != _entityClasses.end() ? i->second : IEntityClassPtr();
}
// Visit each entity class
void EClassManager::forEachEntityClass(EntityClassVisitor& visitor)
{
ensureDefsLoaded();
for (EntityClasses::value_type& pair : _entityClasses)
{
visitor.visit(pair.second);
}
}
void EClassManager::unrealise()
{
if (_realised)
{
// This waits for any threaded work to finish
_defLoader.reset();
_entityClasses.clear();
_realised = false;
}
}
IModelDefPtr EClassManager::findModel(const std::string& name)
{
ensureDefsLoaded();
Models::const_iterator found = _models.find(name);
return (found != _models.end()) ? found->second : Doom3ModelDefPtr();
}
void EClassManager::forEachModelDef(ModelDefVisitor& visitor)
{
ensureDefsLoaded();
for (const Models::value_type& pair : _models)
{
visitor.visit(pair.second);
}
}
void EClassManager::reloadDefs()
{
// greebo: Leave all current entityclasses as they are, just invoke the
// FileLoader again. It will parse the files again, and look up
// the eclass names in the existing map. If found, the eclass
// will be asked to clear itself and re-parse from the tokens.
// This is to assure that any IEntityClassPtrs remain intact during
// the process, only the class contents change.
parseDefFiles();
// Resolve the eclass inheritance again
resolveInheritance();
_defsReloadedSignal.emit();
}
// RegisterableModule implementation
const std::string& EClassManager::getName() const {
static std::string _name(MODULE_ECLASSMANAGER);
return _name;
}
const StringSet& EClassManager::getDependencies() const {
static StringSet _dependencies;
if (_dependencies.empty()) {
_dependencies.insert(MODULE_VIRTUALFILESYSTEM);
_dependencies.insert(MODULE_XMLREGISTRY);
_dependencies.insert(MODULE_RENDERSYSTEM);
_dependencies.insert(MODULE_UIMANAGER);
_dependencies.insert(MODULE_EVENTMANAGER);
_dependencies.insert(MODULE_COMMANDSYSTEM);
}
return _dependencies;
}
void EClassManager::initialiseModule(const ApplicationContext& ctx)
{
rMessage() << "EntityClassDoom3::initialiseModule called." << std::endl;
GlobalFileSystem().addObserver(*this);
realise();
GlobalCommandSystem().addCommand("ReloadDefs", std::bind(&EClassManager::reloadDefsCmd, this, std::placeholders::_1));
GlobalEventManager().addCommand("ReloadDefs", "ReloadDefs");
}
void EClassManager::shutdownModule()
{
rMessage() << "EntityClassDoom3::shutdownModule called." << std::endl;
unrealise();
GlobalFileSystem().removeObserver(*this);
}
// This takes care of relading the entityDefs and refreshing the scenegraph
void EClassManager::reloadDefsCmd(const cmd::ArgumentList& args)
{
IScopedScreenUpdateBlockerPtr blocker = GlobalMainFrame().getScopedScreenUpdateBlocker(_("Reloading Defs"),
_("Reloading Defs"), true);
reloadDefs();
}
// Gets called on VFS initialise
void EClassManager::onFileSystemInitialise()
{
realise();
}
// Gets called on VFS shutdown
void EClassManager::onFileSystemShutdown()
{
unrealise();
}
// Parse the provided stream containing the contents of a single .def file.
// Extract all entitydefs and create objects accordingly.
void EClassManager::parse(TextInputStream& inStr, const std::string& modDir)
{
// Construct a tokeniser for the stream
std::istream is(&inStr);
parser::BasicDefTokeniser<std::istream> tokeniser(is);
while (tokeniser.hasMoreTokens())
{
std::string blockType = tokeniser.nextToken();
boost::algorithm::to_lower(blockType);
if (blockType == "entitydef")
{
// Get the (lowercase) entity name
const std::string sName =
boost::algorithm::to_lower_copy(tokeniser.nextToken());
// Ensure that an Entity class with this name already exists
// When reloading entityDef declarations, most names will already be registered
EntityClasses::iterator i = _entityClasses.find(sName);
if (i == _entityClasses.end())
{
// Not existing yet, allocate a new class
Doom3EntityClassPtr entityClass(new eclass::Doom3EntityClass(sName));
std::pair<EntityClasses::iterator, bool> result = _entityClasses.insert(
EntityClasses::value_type(sName, entityClass)
);
i = result.first;
}
else
{
// EntityDef already exists, compare the parse stamp
if (i->second->getParseStamp() == _curParseStamp)
{
rWarning() << "[eclassmgr]: EntityDef "
<< sName << " redefined" << std::endl;
}
}
// At this point, i is pointing to a valid entityclass
i->second->setParseStamp(_curParseStamp);
// Parse the contents of the eclass (excluding name)
i->second->parseFromTokens(tokeniser);
// Set the mod directory
i->second->setModName(modDir);
}
else if (blockType == "model")
{
// Read the name
std::string modelDefName = tokeniser.nextToken();
// Ensure that an Entity class with this name already exists
// When reloading entityDef declarations, most names will already be registered
Models::iterator i = _models.find(modelDefName);
if (i == _models.end())
{
// Does not exist yet, allocate a new one
// Allocate an empty ModelDef
Doom3ModelDefPtr model(new Doom3ModelDef(modelDefName));
std::pair<Models::iterator, bool> result = _models.insert(
Models::value_type(modelDefName, model)
);
i = result.first;
}
else
{
// Model already exists, compare the parse stamp
if (i->second->getParseStamp() == _curParseStamp)
{
rWarning() << "[eclassmgr]: Model "
<< modelDefName << " redefined" << std::endl;
}
}
// Model structure is allocated and in the map,
// invoke the parser routine
i->second->setParseStamp(_curParseStamp);
i->second->parseFromTokens(tokeniser);
i->second->setModName(modDir);
}
}
}
void EClassManager::parseFile(const std::string& filename)
{
const std::string fullname = "def/" + filename;
ArchiveTextFilePtr file = GlobalFileSystem().openTextFile(fullname);
if (!file) return;
try
{
// Parse entity defs from the file
parse(file->getInputStream(), file->getModName());
}
catch (parser::ParseException& e)
{
rError() << "[eclassmgr] failed to parse " << filename
<< " (" << e.what() << ")" << std::endl;
}
}
} // namespace eclass