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;
53 EClassModules& EntityClassManager_getEClassModules();
56 implementation of the EClass manager API
59 void CleanEntityList( EntityClasses& entityClasses ){
60 for ( EntityClasses::iterator i = entityClasses.begin(); i != entityClasses.end(); ++i )
62 ( *i ).second->free( ( *i ).second );
64 entityClasses.clear();
68 CleanEntityList( g_entityClasses );
72 EntityClass* EClass_InsertSortedList( EntityClasses& entityClasses, EntityClass *entityClass ){
73 std::pair<EntityClasses::iterator, bool> result = entityClasses.insert( EntityClasses::value_type( entityClass->name(), entityClass ) );
74 if ( !result.second ) {
75 entityClass->free( entityClass );
77 return ( *result.first ).second;
80 EntityClass* Eclass_InsertAlphabetized( EntityClass *e ){
81 return EClass_InsertSortedList( g_entityClasses, e );
84 void Eclass_forEach( EntityClassVisitor& visitor ){
85 for ( EntityClasses::iterator i = g_entityClasses.begin(); i != g_entityClasses.end(); ++i )
87 visitor.visit( ( *i ).second );
92 class RadiantEclassCollector : public EntityClassCollector
95 void insert( EntityClass* eclass ){
96 Eclass_InsertAlphabetized( eclass );
98 void insert( const char* name, const ListAttributeType& list ){
99 g_listTypes.insert( ListAttributeTypes::value_type( name, list ) );
103 RadiantEclassCollector g_collector;
105 const ListAttributeType* EntityClass_findListType( const char* name ){
106 ListAttributeTypes::iterator i = g_listTypes.find( name );
107 if ( i != g_listTypes.end() ) {
108 return &( *i ).second;
114 class EntityClassFilterMode
118 const char* mp_ignore_prefix;
119 const char* sp_ignore_prefix;
121 EntityClassFilterMode() :
122 filter_mp_sp( !string_empty( g_pGameDescription->getKeyValue( "eclass_filter_gamemode" ) ) ),
123 mp_ignore_prefix( g_pGameDescription->getKeyValue( "eclass_sp_prefix" ) ),
124 sp_ignore_prefix( g_pGameDescription->getKeyValue( "eclass_mp_prefix" ) ){
125 if ( string_empty( mp_ignore_prefix ) ) {
126 mp_ignore_prefix = "sp_";
128 if ( string_empty( sp_ignore_prefix ) ) {
129 sp_ignore_prefix = "mp_";
134 class EntityClassesLoadFile
136 const EntityClassScanner& scanner;
137 const char* m_directory;
139 EntityClassesLoadFile( const EntityClassScanner& scanner, const char* directory ) : scanner( scanner ), m_directory( directory ){
141 void operator()( const char* name ) const {
142 EntityClassFilterMode filterMode;
144 if ( filterMode.filter_mp_sp ) {
145 if ( string_empty( GlobalRadiant().getGameMode() ) || string_equal( GlobalRadiant().getGameMode(), "sp" ) ) {
146 if ( string_equal_n( name, filterMode.sp_ignore_prefix, strlen( filterMode.sp_ignore_prefix ) ) ) {
147 globalOutputStream() << "Ignoring '" << name << "'\n";
153 if ( string_equal_n( name, filterMode.mp_ignore_prefix, strlen( filterMode.mp_ignore_prefix ) ) ) {
154 globalOutputStream() << "Ignoring '" << name << "'\n";
160 // for a given name, we grab the first .def in the vfs
161 // this allows to override baseq3/scripts/entities.def for instance
162 StringOutputStream relPath( 256 );
163 relPath << m_directory << name;
165 scanner.scanFile( g_collector, relPath.c_str() );
171 bool operator()( const CopiedString& path, const CopiedString& other ) const {
172 return path_less( path.c_str(), other.c_str() );
176 typedef std::map<CopiedString, const char*, PathLess> Paths;
181 const char* m_directory;
183 PathsInsert( Paths& paths, const char* directory ) : m_paths( paths ), m_directory( directory ){
185 void operator()( const char* name ) const {
186 m_paths.insert( Paths::value_type( name, m_directory ) );
191 void EntityClassQuake3_constructDirectory( const char* directory, const char* extension, Paths& paths ){
192 globalOutputStream() << "EntityClass: searching " << makeQuoted( directory ) << " for *." << extension << '\n';
193 Directory_forEach( directory, matchFileExtension( extension, PathsInsert( paths, directory ) ) );
197 void EntityClassQuake3_Construct(){
198 StringOutputStream baseDirectory( 256 );
199 StringOutputStream gameDirectory( 256 );
200 const char* basegame = GlobalRadiant().getRequiredGameDescriptionKeyValue( "basegame" );
201 const char* gamename = GlobalRadiant().getGameName();
202 baseDirectory << GlobalRadiant().getGameToolsPath() << basegame << '/';
203 gameDirectory << GlobalRadiant().getGameToolsPath() << gamename << '/';
205 class LoadEntityDefinitionsVisitor : public EClassModules::Visitor
207 const char* baseDirectory;
208 const char* gameDirectory;
210 LoadEntityDefinitionsVisitor( const char* baseDirectory, const char* gameDirectory )
211 : baseDirectory( baseDirectory ), gameDirectory( gameDirectory ){
213 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 )
222 EntityClassesLoadFile( table, ( *i ).second ) ( ( *i ).first.c_str() );
227 EntityClassManager_getEClassModules().foreachModule( LoadEntityDefinitionsVisitor( baseDirectory.c_str(), gameDirectory.c_str() ) );
230 EntityClass *Eclass_ForName( const char *name, bool has_brushes ){
231 ASSERT_NOTNULL( name );
233 if ( string_empty( name ) ) {
237 EntityClasses::iterator i = g_entityClasses.find( name );
238 if ( i != g_entityClasses.end() && string_equal( ( *i ).first, name ) ) {
239 return ( *i ).second;
242 EntityClass* e = EntityClass_Create_Default( name, has_brushes );
243 return Eclass_InsertAlphabetized( e );
246 class EntityClassQuake3 : public ModuleObserver
248 std::size_t m_unrealised;
249 ModuleObservers m_observers;
251 EntityClassQuake3() : m_unrealised( 4 ){
254 if ( --m_unrealised == 0 ) {
255 //globalOutputStream() << "Entity Classes: realise\n";
256 EntityClassQuake3_Construct();
257 m_observers.realise();
261 if ( ++m_unrealised == 1 ) {
262 m_observers.unrealise();
263 //globalOutputStream() << "Entity Classes: unrealise\n";
267 void attach( ModuleObserver& observer ){
268 m_observers.attach( observer );
270 void detach( ModuleObserver& observer ){
271 m_observers.detach( observer );
275 EntityClassQuake3 g_EntityClassQuake3;
277 void EntityClass_attach( ModuleObserver& observer ){
278 g_EntityClassQuake3.attach( observer );
280 void EntityClass_detach( ModuleObserver& observer ){
281 g_EntityClassQuake3.detach( observer );
284 void EntityClass_realise(){
285 g_EntityClassQuake3.realise();
287 void EntityClass_unrealise(){
288 g_EntityClassQuake3.unrealise();
291 void EntityClassQuake3_construct(){
292 // start by creating the default unknown eclass
293 eclass_bad = EClass_Create( "UNKNOWN_CLASS", Vector3( 0.0f, 0.5f, 0.0f ), "" );
295 EntityClass_realise();
298 void EntityClassQuake3_destroy(){
299 EntityClass_unrealise();
301 eclass_bad->free( eclass_bad );
304 #include "modulesystem/modulesmap.h"
306 class EntityClassQuake3Dependencies :
307 public GlobalRadiantModuleRef,
308 public GlobalFileSystemModuleRef,
309 public GlobalShaderCacheModuleRef
311 EClassModulesRef m_eclass_modules;
313 EntityClassQuake3Dependencies() :
314 m_eclass_modules( GlobalRadiant().getRequiredGameDescriptionKeyValue( "entityclasstype" ) ){
316 EClassModules& getEClassModules(){
317 return m_eclass_modules.get();
321 class EclassManagerAPI
323 EntityClassManager m_eclassmanager;
325 typedef EntityClassManager Type;
326 STRING_CONSTANT( Name, "quake3" );
329 EntityClassQuake3_construct();
331 m_eclassmanager.findOrInsert = &Eclass_ForName;
332 m_eclassmanager.findListType = &EntityClass_findListType;
333 m_eclassmanager.forEach = &Eclass_forEach;
334 m_eclassmanager.attach = &EntityClass_attach;
335 m_eclassmanager.detach = &EntityClass_detach;
336 m_eclassmanager.realise = &EntityClass_realise;
337 m_eclassmanager.unrealise = &EntityClass_unrealise;
339 GlobalRadiant().attachGameToolsPathObserver( g_EntityClassQuake3 );
340 GlobalRadiant().attachGameModeObserver( g_EntityClassQuake3 );
341 GlobalRadiant().attachGameNameObserver( g_EntityClassQuake3 );
344 GlobalRadiant().detachGameNameObserver( g_EntityClassQuake3 );
345 GlobalRadiant().detachGameModeObserver( g_EntityClassQuake3 );
346 GlobalRadiant().detachGameToolsPathObserver( g_EntityClassQuake3 );
348 EntityClassQuake3_destroy();
350 EntityClassManager* getTable(){
351 return &m_eclassmanager;
355 #include "modulesystem/singletonmodule.h"
356 #include "modulesystem/moduleregistry.h"
358 typedef SingletonModule<EclassManagerAPI, EntityClassQuake3Dependencies> EclassManagerModule;
359 typedef Static<EclassManagerModule> StaticEclassManagerModule;
360 StaticRegisterModule staticRegisterEclassManager( StaticEclassManagerModule::instance() );
362 EClassModules& EntityClassManager_getEClassModules(){
363 return StaticEclassManagerModule::instance().getDependencies().getEClassModules();