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"
45 typedef std::map<const char*, EntityClass*, RawStringLessNoCase> EntityClasses;
46 EntityClasses g_entityClasses;
47 EntityClass *eclass_bad = 0;
48 char eclass_directory[1024];
49 typedef std::map<CopiedString, ListAttributeType> ListAttributeTypes;
50 ListAttributeTypes g_listTypes;
54 implementation of the EClass manager API
57 void CleanEntityList(EntityClasses& entityClasses)
59 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(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)
91 visitor.visit((*i).second);
96 class RadiantEclassCollector : public EntityClassCollector
99 void insert(EntityClass* eclass)
101 Eclass_InsertAlphabetized(eclass);
103 void insert(const char* name, const ListAttributeType& list)
105 g_listTypes.insert(ListAttributeTypes::value_type(name, list));
109 RadiantEclassCollector g_collector;
111 const ListAttributeType* EntityClass_findListType(const char* name)
113 ListAttributeTypes::iterator i = g_listTypes.find(name);
114 if(i != g_listTypes.end())
122 class EntityClassFilterMode
126 const char* mp_ignore_prefix;
127 const char* sp_ignore_prefix;
129 EntityClassFilterMode() :
130 filter_mp_sp(!string_empty(g_pGameDescription->getKeyValue("eclass_filter_gamemode"))),
131 mp_ignore_prefix(g_pGameDescription->getKeyValue("eclass_sp_prefix")),
132 sp_ignore_prefix(g_pGameDescription->getKeyValue("eclass_mp_prefix"))
134 if(string_empty(mp_ignore_prefix))
136 mp_ignore_prefix = "sp_";
138 if(string_empty(sp_ignore_prefix))
140 sp_ignore_prefix = "mp_";
145 class EntityClassesLoadFile
147 const char* m_directory;
149 EntityClassesLoadFile(const char* directory) : m_directory(directory)
152 void operator()(const char* name) const
154 EntityClassFilterMode filterMode;
156 if(filterMode.filter_mp_sp)
158 if(string_empty(GlobalRadiant().getGameMode()) || string_equal(GlobalRadiant().getGameMode(), "sp"))
160 if(string_equal_n(name, filterMode.sp_ignore_prefix, strlen(filterMode.sp_ignore_prefix)))
162 globalOutputStream() << "Ignoring '" << name << "'\n";
168 if(string_equal_n(name, filterMode.mp_ignore_prefix, strlen(filterMode.mp_ignore_prefix)))
170 globalOutputStream() << "Ignoring '" << name << "'\n";
176 // for a given name, we grab the first .def in the vfs
177 // this allows to override baseq3/scripts/entities.def for instance
178 StringOutputStream relPath(256);
179 relPath << m_directory << name;
181 GlobalEClassLoader().scanFile(g_collector, relPath.c_str());
187 bool operator()(const CopiedString& path, const CopiedString& other) const
189 return path_less(path.c_str(), other.c_str());
193 typedef std::map<CopiedString, const char*, PathLess> Paths;
198 const char* m_directory;
200 PathsInsert(Paths& paths, const char* directory) : m_paths(paths), m_directory(directory)
203 void operator()(const char* name) const
205 m_paths.insert(Paths::value_type(name, m_directory));
210 void EntityClassQuake3_constructDirectory(const char* directory, const char* extension)
212 globalOutputStream() << "EntityClass: searching " << makeQuoted(directory) << " for *." << extension << '\n';
213 Directory_forEach(directory, matchFileExtension(extension, EntityClassesLoadFile(directory)));
216 void EntityClassQuake3_constructDirectory(const char* directory, const char* extension, Paths& paths)
218 globalOutputStream() << "EntityClass: searching " << makeQuoted(directory) << " for *." << extension << '\n';
219 Directory_forEach(directory, matchFileExtension(extension, PathsInsert(paths, directory)));
223 void EntityClassQuake3_Construct()
226 StringOutputStream baseDirectory(256);
227 StringOutputStream gameDirectory(256);
228 const char* basegame = GlobalRadiant().getRequiredGameDescriptionKeyValue("basegame");
229 const char* gamename = GlobalRadiant().getGameName();
230 baseDirectory << GlobalRadiant().getGameToolsPath() << basegame << '/';
231 gameDirectory << GlobalRadiant().getGameToolsPath() << gamename << '/';
234 EntityClassQuake3_constructDirectory(baseDirectory.c_str(), GlobalEClassLoader().getExtension(), paths);
235 if(!string_equal(basegame, gamename))
237 EntityClassQuake3_constructDirectory(gameDirectory.c_str(), GlobalEClassLoader().getExtension(), paths);
240 for(Paths::iterator i = paths.begin(); i != paths.end(); ++i)
242 EntityClassesLoadFile((*i).second)((*i).first.c_str());
245 StringOutputStream directory(256);
246 directory << GlobalRadiant().getGameToolsPath() << GlobalRadiant().getGameName() << '/';
247 EntityClassQuake3_constructDirectory(directory.c_str(), GlobalEClassLoader().getExtension());
251 EntityClass *Eclass_ForName(const char *name, bool has_brushes)
253 ASSERT_NOTNULL(name);
255 if(string_empty(name))
260 EntityClasses::iterator i = g_entityClasses.find(name);
261 if(i != g_entityClasses.end() && string_equal((*i).first, name))
266 EntityClass* e = EntityClass_Create_Default(name, has_brushes);
267 return Eclass_InsertAlphabetized(e);
270 class EntityClassQuake3 : public ModuleObserver
272 std::size_t m_unrealised;
273 ModuleObservers m_observers;
275 EntityClassQuake3() : m_unrealised(4)
280 if(--m_unrealised == 0)
282 //globalOutputStream() << "Entity Classes: realise\n";
283 EntityClassQuake3_Construct();
284 m_observers.realise();
289 if(++m_unrealised == 1)
291 m_observers.unrealise();
292 //globalOutputStream() << "Entity Classes: unrealise\n";
296 void attach(ModuleObserver& observer)
298 m_observers.attach(observer);
300 void detach(ModuleObserver& observer)
302 m_observers.detach(observer);
306 EntityClassQuake3 g_EntityClassQuake3;
308 void EntityClass_attach(ModuleObserver& observer)
310 g_EntityClassQuake3.attach(observer);
312 void EntityClass_detach(ModuleObserver& observer)
314 g_EntityClassQuake3.detach(observer);
317 void EntityClass_realise()
319 g_EntityClassQuake3.realise();
321 void EntityClass_unrealise()
323 g_EntityClassQuake3.unrealise();
326 void EntityClassQuake3_construct()
328 // start by creating the default unknown eclass
329 eclass_bad = EClass_Create("UNKNOWN_CLASS", Vector3(0.0f, 0.5f, 0.0f), "");
331 EntityClass_realise();
334 void EntityClassQuake3_destroy()
336 EntityClass_unrealise();
338 eclass_bad->free(eclass_bad);
341 class EntityClassQuake3Dependencies :
342 public GlobalRadiantModuleRef,
343 public GlobalFileSystemModuleRef,
344 public GlobalShaderCacheModuleRef,
345 public GlobalEClassModuleRef
348 EntityClassQuake3Dependencies() :
349 GlobalEClassModuleRef(GlobalRadiant().getRequiredGameDescriptionKeyValue("entityclasstype"))
354 class EclassManagerAPI
356 EntityClassManager m_eclassmanager;
358 typedef EntityClassManager Type;
359 STRING_CONSTANT(Name, "quake3");
363 EntityClassQuake3_construct();
365 m_eclassmanager.findOrInsert = &Eclass_ForName;
366 m_eclassmanager.findListType = &EntityClass_findListType;
367 m_eclassmanager.forEach = &Eclass_forEach;
368 m_eclassmanager.attach = &EntityClass_attach;
369 m_eclassmanager.detach = &EntityClass_detach;
370 m_eclassmanager.realise = &EntityClass_realise;
371 m_eclassmanager.unrealise = &EntityClass_unrealise;
373 GlobalRadiant().attachGameToolsPathObserver(g_EntityClassQuake3);
374 GlobalRadiant().attachGameModeObserver(g_EntityClassQuake3);
375 GlobalRadiant().attachGameNameObserver(g_EntityClassQuake3);
379 GlobalRadiant().detachGameNameObserver(g_EntityClassQuake3);
380 GlobalRadiant().detachGameModeObserver(g_EntityClassQuake3);
381 GlobalRadiant().detachGameToolsPathObserver(g_EntityClassQuake3);
383 EntityClassQuake3_destroy();
385 EntityClassManager* getTable()
387 return &m_eclassmanager;
391 #include "modulesystem/singletonmodule.h"
392 #include "modulesystem/moduleregistry.h"
394 typedef SingletonModule<EclassManagerAPI, EntityClassQuake3Dependencies> EclassManagerModule;
395 typedef Static<EclassManagerModule> StaticEclassManagerModule;
396 StaticRegisterModule staticRegisterEclassManager(StaticEclassManagerModule::instance());