2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant 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.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "debugging/debugging.h"
28 #include "ifilesystem.h"
30 #include "string/string.h"
31 #include "eclasslib.h"
34 #include "stream/stringstream.h"
35 #include "moduleobservers.h"
39 #include "preferences.h"
40 #include "mainframe.h"
44 typedef std::map<const char *, EntityClass *, RawStringLessNoCase> EntityClasses;
45 EntityClasses g_entityClasses;
46 EntityClass *eclass_bad = 0;
47 char eclass_directory[1024];
48 typedef std::map<CopiedString, ListAttributeType> ListAttributeTypes;
49 ListAttributeTypes g_listTypes;
52 EClassModules &EntityClassManager_getEClassModules();
55 implementation of the EClass manager API
58 void CleanEntityList(EntityClasses &entityClasses)
60 for (EntityClasses::iterator i = entityClasses.begin(); i != entityClasses.end(); ++i) {
61 (*i).second->free((*i).second);
63 entityClasses.clear();
68 CleanEntityList(g_entityClasses);
72 EntityClass *EClass_InsertSortedList(EntityClasses &entityClasses, EntityClass *entityClass)
74 std::pair<EntityClasses::iterator, bool> result = entityClasses.insert(
75 EntityClasses::value_type(entityClass->name(), entityClass));
77 entityClass->free(entityClass);
79 return (*result.first).second;
82 EntityClass *Eclass_InsertAlphabetized(EntityClass *e)
84 return EClass_InsertSortedList(g_entityClasses, e);
87 void Eclass_forEach(EntityClassVisitor &visitor)
89 for (EntityClasses::iterator i = g_entityClasses.begin(); i != g_entityClasses.end(); ++i) {
90 visitor.visit((*i).second);
95 class RadiantEclassCollector : public EntityClassCollector {
97 void insert(EntityClass *eclass)
99 Eclass_InsertAlphabetized(eclass);
102 void insert(const char *name, const ListAttributeType &list)
104 g_listTypes.insert(ListAttributeTypes::value_type(name, list));
108 RadiantEclassCollector g_collector;
110 const ListAttributeType *EntityClass_findListType(const char *name)
112 ListAttributeTypes::iterator i = g_listTypes.find(name);
113 if (i != g_listTypes.end()) {
120 class EntityClassFilterMode {
123 const char *mp_ignore_prefix;
124 const char *sp_ignore_prefix;
126 EntityClassFilterMode() :
127 filter_mp_sp(!string_empty(g_pGameDescription->getKeyValue("eclass_filter_gamemode"))),
128 mp_ignore_prefix(g_pGameDescription->getKeyValue("eclass_sp_prefix")),
129 sp_ignore_prefix(g_pGameDescription->getKeyValue("eclass_mp_prefix"))
131 if (string_empty(mp_ignore_prefix)) {
132 mp_ignore_prefix = "sp_";
134 if (string_empty(sp_ignore_prefix)) {
135 sp_ignore_prefix = "mp_";
140 class EntityClassesLoadFile {
141 const EntityClassScanner &scanner;
142 const char *m_directory;
144 EntityClassesLoadFile(const EntityClassScanner &scanner, const char *directory) : scanner(scanner),
145 m_directory(directory)
149 void operator()(const char *name) const
151 EntityClassFilterMode filterMode;
153 if (filterMode.filter_mp_sp) {
154 if (string_empty(GlobalRadiant().getGameMode()) || string_equal(GlobalRadiant().getGameMode(), "sp")) {
155 if (string_equal_n(name, filterMode.sp_ignore_prefix, strlen(filterMode.sp_ignore_prefix))) {
156 globalOutputStream() << "Ignoring '" << name << "'\n";
160 if (string_equal_n(name, filterMode.mp_ignore_prefix, strlen(filterMode.mp_ignore_prefix))) {
161 globalOutputStream() << "Ignoring '" << name << "'\n";
167 // for a given name, we grab the first .def in the vfs
168 // this allows to override baseq3/scripts/entities.def for instance
169 StringOutputStream relPath(256);
170 relPath << m_directory << name;
172 scanner.scanFile(g_collector, relPath.c_str());
177 bool operator()(const CopiedString &path, const CopiedString &other) const
179 return path_less(path.c_str(), other.c_str());
183 typedef std::map<CopiedString, const char *, PathLess> Paths;
185 void EntityClassQuake3_constructDirectory(const char *directory, const char *extension, Paths &paths)
187 globalOutputStream() << "EntityClass: searching " << makeQuoted(directory) << " for *." << extension << '\n';
188 Directory_forEach(directory, matchFileExtension(extension, [&](const char *name) {
189 paths.insert(Paths::value_type(name, directory));
194 void EntityClassQuake3_Construct()
196 StringOutputStream baseDirectory(256);
197 StringOutputStream gameDirectory(256);
198 const char *basegame = GlobalRadiant().getRequiredGameDescriptionKeyValue("basegame");
199 const char *gamename = GlobalRadiant().getGameName();
200 baseDirectory << GlobalRadiant().getGameToolsPath() << basegame << '/';
201 gameDirectory << GlobalRadiant().getGameToolsPath() << gamename << '/';
203 class LoadEntityDefinitionsVisitor : public EClassModules::Visitor {
204 const char *baseDirectory;
205 const char *gameDirectory;
207 LoadEntityDefinitionsVisitor(const char *baseDirectory, const char *gameDirectory)
208 : baseDirectory(baseDirectory), gameDirectory(gameDirectory)
212 void visit(const char *name, const EntityClassScanner &table) const
215 EntityClassQuake3_constructDirectory(baseDirectory, table.getExtension(), paths);
216 if (!string_equal(baseDirectory, gameDirectory)) {
217 EntityClassQuake3_constructDirectory(gameDirectory, table.getExtension(), paths);
220 for (Paths::iterator i = paths.begin(); i != paths.end(); ++i) {
221 EntityClassesLoadFile(table, (*i).second)((*i).first.c_str());
226 EntityClassManager_getEClassModules().foreachModule(
227 LoadEntityDefinitionsVisitor(baseDirectory.c_str(), gameDirectory.c_str()));
230 EntityClass *Eclass_ForName(const char *name, bool has_brushes)
232 ASSERT_NOTNULL(name);
234 if (string_empty(name)) {
238 EntityClasses::iterator i = g_entityClasses.find(name);
239 if (i != g_entityClasses.end() && string_equal((*i).first, name)) {
243 EntityClass *e = EntityClass_Create_Default(name, has_brushes);
244 return Eclass_InsertAlphabetized(e);
247 class EntityClassQuake3 : public ModuleObserver {
248 std::size_t m_unrealised;
249 ModuleObservers m_observers;
251 EntityClassQuake3() : m_unrealised(4)
257 if (--m_unrealised == 0) {
258 //globalOutputStream() << "Entity Classes: realise\n";
259 EntityClassQuake3_Construct();
260 m_observers.realise();
266 if (++m_unrealised == 1) {
267 m_observers.unrealise();
268 //globalOutputStream() << "Entity Classes: unrealise\n";
273 void attach(ModuleObserver &observer)
275 m_observers.attach(observer);
278 void detach(ModuleObserver &observer)
280 m_observers.detach(observer);
284 EntityClassQuake3 g_EntityClassQuake3;
286 void EntityClass_attach(ModuleObserver &observer)
288 g_EntityClassQuake3.attach(observer);
291 void EntityClass_detach(ModuleObserver &observer)
293 g_EntityClassQuake3.detach(observer);
296 void EntityClass_realise()
298 g_EntityClassQuake3.realise();
301 void EntityClass_unrealise()
303 g_EntityClassQuake3.unrealise();
306 void EntityClassQuake3_construct()
308 // start by creating the default unknown eclass
309 eclass_bad = EClass_Create("UNKNOWN_CLASS", Vector3(0.0f, 0.5f, 0.0f), "");
311 EntityClass_realise();
314 void EntityClassQuake3_destroy()
316 EntityClass_unrealise();
318 eclass_bad->free(eclass_bad);
321 #include "modulesystem/modulesmap.h"
323 class EntityClassQuake3Dependencies :
324 public GlobalRadiantModuleRef,
325 public GlobalFileSystemModuleRef,
326 public GlobalShaderCacheModuleRef {
327 EClassModulesRef m_eclass_modules;
329 EntityClassQuake3Dependencies() :
330 m_eclass_modules(GlobalRadiant().getRequiredGameDescriptionKeyValue("entityclasstype"))
334 EClassModules &getEClassModules()
336 return m_eclass_modules.get();
340 class EclassManagerAPI {
341 EntityClassManager m_eclassmanager;
343 typedef EntityClassManager Type;
345 STRING_CONSTANT(Name, "quake3");
349 EntityClassQuake3_construct();
351 m_eclassmanager.findOrInsert = &Eclass_ForName;
352 m_eclassmanager.findListType = &EntityClass_findListType;
353 m_eclassmanager.forEach = &Eclass_forEach;
354 m_eclassmanager.attach = &EntityClass_attach;
355 m_eclassmanager.detach = &EntityClass_detach;
356 m_eclassmanager.realise = &EntityClass_realise;
357 m_eclassmanager.unrealise = &EntityClass_unrealise;
359 Radiant_attachGameToolsPathObserver(g_EntityClassQuake3);
360 Radiant_attachGameModeObserver(g_EntityClassQuake3);
361 Radiant_attachGameNameObserver(g_EntityClassQuake3);
366 Radiant_detachGameNameObserver(g_EntityClassQuake3);
367 Radiant_detachGameModeObserver(g_EntityClassQuake3);
368 Radiant_detachGameToolsPathObserver(g_EntityClassQuake3);
370 EntityClassQuake3_destroy();
373 EntityClassManager *getTable()
375 return &m_eclassmanager;
379 #include "modulesystem/singletonmodule.h"
380 #include "modulesystem/moduleregistry.h"
382 typedef SingletonModule<EclassManagerAPI, EntityClassQuake3Dependencies> EclassManagerModule;
383 typedef Static<EclassManagerModule> StaticEclassManagerModule;
384 StaticRegisterModule staticRegisterEclassManager(StaticEclassManagerModule::instance());
386 EClassModules &EntityClassManager_getEClassModules()
388 return StaticEclassManagerModule::instance().getDependencies().getEClassModules();