| 1 | /* |
| 2 | Copyright (C) 1999-2006 Id Software, Inc. and contributors. |
| 3 | For a list of contributors, see the accompanying CONTRIBUTORS file. |
| 4 | |
| 5 | This file is part of GtkRadiant. |
| 6 | |
| 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. |
| 11 | |
| 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. |
| 16 | |
| 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 |
| 20 | */ |
| 21 | |
| 22 | #include "eclass.h" |
| 23 | |
| 24 | #include "debugging/debugging.h" |
| 25 | |
| 26 | #include <map> |
| 27 | |
| 28 | #include "ifilesystem.h" |
| 29 | |
| 30 | #include "string/string.h" |
| 31 | #include "eclasslib.h" |
| 32 | #include "os/path.h" |
| 33 | #include "os/dir.h" |
| 34 | #include "stream/stringstream.h" |
| 35 | #include "moduleobservers.h" |
| 36 | |
| 37 | #include "cmdlib.h" |
| 38 | |
| 39 | #include "preferences.h" |
| 40 | #include "mainframe.h" |
| 41 | |
| 42 | |
| 43 | namespace |
| 44 | { |
| 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; |
| 51 | } |
| 52 | |
| 53 | EClassModules& EntityClassManager_getEClassModules(); |
| 54 | |
| 55 | /*! |
| 56 | implementation of the EClass manager API |
| 57 | */ |
| 58 | |
| 59 | void CleanEntityList( EntityClasses& entityClasses ){ |
| 60 | for ( EntityClasses::iterator i = entityClasses.begin(); i != entityClasses.end(); ++i ) |
| 61 | { |
| 62 | ( *i ).second->free( ( *i ).second ); |
| 63 | } |
| 64 | entityClasses.clear(); |
| 65 | } |
| 66 | |
| 67 | void Eclass_Clear(){ |
| 68 | CleanEntityList( g_entityClasses ); |
| 69 | g_listTypes.clear(); |
| 70 | } |
| 71 | |
| 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 ); |
| 76 | } |
| 77 | return ( *result.first ).second; |
| 78 | } |
| 79 | |
| 80 | EntityClass* Eclass_InsertAlphabetized( EntityClass *e ){ |
| 81 | return EClass_InsertSortedList( g_entityClasses, e ); |
| 82 | } |
| 83 | |
| 84 | void Eclass_forEach( EntityClassVisitor& visitor ){ |
| 85 | for ( EntityClasses::iterator i = g_entityClasses.begin(); i != g_entityClasses.end(); ++i ) |
| 86 | { |
| 87 | visitor.visit( ( *i ).second ); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
| 92 | class RadiantEclassCollector : public EntityClassCollector |
| 93 | { |
| 94 | public: |
| 95 | void insert( EntityClass* eclass ){ |
| 96 | Eclass_InsertAlphabetized( eclass ); |
| 97 | } |
| 98 | void insert( const char* name, const ListAttributeType& list ){ |
| 99 | g_listTypes.insert( ListAttributeTypes::value_type( name, list ) ); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | RadiantEclassCollector g_collector; |
| 104 | |
| 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; |
| 109 | } |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | |
| 114 | class EntityClassFilterMode |
| 115 | { |
| 116 | public: |
| 117 | bool filter_mp_sp; |
| 118 | const char* mp_ignore_prefix; |
| 119 | const char* sp_ignore_prefix; |
| 120 | |
| 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_"; |
| 127 | } |
| 128 | if ( string_empty( sp_ignore_prefix ) ) { |
| 129 | sp_ignore_prefix = "mp_"; |
| 130 | } |
| 131 | } |
| 132 | }; |
| 133 | |
| 134 | class EntityClassesLoadFile |
| 135 | { |
| 136 | const EntityClassScanner& scanner; |
| 137 | const char* m_directory; |
| 138 | public: |
| 139 | EntityClassesLoadFile( const EntityClassScanner& scanner, const char* directory ) : scanner( scanner ), m_directory( directory ){ |
| 140 | } |
| 141 | void operator()( const char* name ) const { |
| 142 | EntityClassFilterMode filterMode; |
| 143 | |
| 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"; |
| 148 | return; |
| 149 | } |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | if ( string_equal_n( name, filterMode.mp_ignore_prefix, strlen( filterMode.mp_ignore_prefix ) ) ) { |
| 154 | globalOutputStream() << "Ignoring '" << name << "'\n"; |
| 155 | return; |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 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; |
| 164 | |
| 165 | scanner.scanFile( g_collector, relPath.c_str() ); |
| 166 | } |
| 167 | }; |
| 168 | |
| 169 | struct PathLess |
| 170 | { |
| 171 | bool operator()( const CopiedString& path, const CopiedString& other ) const { |
| 172 | return path_less( path.c_str(), other.c_str() ); |
| 173 | } |
| 174 | }; |
| 175 | |
| 176 | typedef std::map<CopiedString, const char*, PathLess> Paths; |
| 177 | |
| 178 | void EntityClassQuake3_constructDirectory( const char* directory, const char* extension, Paths& paths ){ |
| 179 | globalOutputStream() << "EntityClass: searching " << makeQuoted( directory ) << " for *." << extension << '\n'; |
| 180 | Directory_forEach(directory, matchFileExtension(extension, [&](const char *name) { |
| 181 | paths.insert(Paths::value_type(name, directory)); |
| 182 | })); |
| 183 | } |
| 184 | |
| 185 | |
| 186 | void EntityClassQuake3_Construct(){ |
| 187 | StringOutputStream baseDirectory( 256 ); |
| 188 | StringOutputStream gameDirectory( 256 ); |
| 189 | const char* basegame = GlobalRadiant().getRequiredGameDescriptionKeyValue( "basegame" ); |
| 190 | const char* gamename = GlobalRadiant().getGameName(); |
| 191 | baseDirectory << GlobalRadiant().getGameToolsPath() << basegame << '/'; |
| 192 | gameDirectory << GlobalRadiant().getGameToolsPath() << gamename << '/'; |
| 193 | |
| 194 | class LoadEntityDefinitionsVisitor : public EClassModules::Visitor |
| 195 | { |
| 196 | const char* baseDirectory; |
| 197 | const char* gameDirectory; |
| 198 | public: |
| 199 | LoadEntityDefinitionsVisitor( const char* baseDirectory, const char* gameDirectory ) |
| 200 | : baseDirectory( baseDirectory ), gameDirectory( gameDirectory ){ |
| 201 | } |
| 202 | void visit( const char* name, const EntityClassScanner& table ) const { |
| 203 | Paths paths; |
| 204 | EntityClassQuake3_constructDirectory( baseDirectory, table.getExtension(), paths ); |
| 205 | if ( !string_equal( baseDirectory, gameDirectory ) ) { |
| 206 | EntityClassQuake3_constructDirectory( gameDirectory, table.getExtension(), paths ); |
| 207 | } |
| 208 | |
| 209 | for ( Paths::iterator i = paths.begin(); i != paths.end(); ++i ) |
| 210 | { |
| 211 | EntityClassesLoadFile( table, ( *i ).second ) ( ( *i ).first.c_str() ); |
| 212 | } |
| 213 | } |
| 214 | }; |
| 215 | |
| 216 | EntityClassManager_getEClassModules().foreachModule( LoadEntityDefinitionsVisitor( baseDirectory.c_str(), gameDirectory.c_str() ) ); |
| 217 | } |
| 218 | |
| 219 | EntityClass *Eclass_ForName( const char *name, bool has_brushes ){ |
| 220 | ASSERT_NOTNULL( name ); |
| 221 | |
| 222 | if ( string_empty( name ) ) { |
| 223 | return eclass_bad; |
| 224 | } |
| 225 | |
| 226 | EntityClasses::iterator i = g_entityClasses.find( name ); |
| 227 | if ( i != g_entityClasses.end() && string_equal( ( *i ).first, name ) ) { |
| 228 | return ( *i ).second; |
| 229 | } |
| 230 | |
| 231 | EntityClass* e = EntityClass_Create_Default( name, has_brushes ); |
| 232 | return Eclass_InsertAlphabetized( e ); |
| 233 | } |
| 234 | |
| 235 | class EntityClassQuake3 : public ModuleObserver |
| 236 | { |
| 237 | std::size_t m_unrealised; |
| 238 | ModuleObservers m_observers; |
| 239 | public: |
| 240 | EntityClassQuake3() : m_unrealised( 4 ){ |
| 241 | } |
| 242 | void realise(){ |
| 243 | if ( --m_unrealised == 0 ) { |
| 244 | //globalOutputStream() << "Entity Classes: realise\n"; |
| 245 | EntityClassQuake3_Construct(); |
| 246 | m_observers.realise(); |
| 247 | } |
| 248 | } |
| 249 | void unrealise(){ |
| 250 | if ( ++m_unrealised == 1 ) { |
| 251 | m_observers.unrealise(); |
| 252 | //globalOutputStream() << "Entity Classes: unrealise\n"; |
| 253 | Eclass_Clear(); |
| 254 | } |
| 255 | } |
| 256 | void attach( ModuleObserver& observer ){ |
| 257 | m_observers.attach( observer ); |
| 258 | } |
| 259 | void detach( ModuleObserver& observer ){ |
| 260 | m_observers.detach( observer ); |
| 261 | } |
| 262 | }; |
| 263 | |
| 264 | EntityClassQuake3 g_EntityClassQuake3; |
| 265 | |
| 266 | void EntityClass_attach( ModuleObserver& observer ){ |
| 267 | g_EntityClassQuake3.attach( observer ); |
| 268 | } |
| 269 | void EntityClass_detach( ModuleObserver& observer ){ |
| 270 | g_EntityClassQuake3.detach( observer ); |
| 271 | } |
| 272 | |
| 273 | void EntityClass_realise(){ |
| 274 | g_EntityClassQuake3.realise(); |
| 275 | } |
| 276 | void EntityClass_unrealise(){ |
| 277 | g_EntityClassQuake3.unrealise(); |
| 278 | } |
| 279 | |
| 280 | void EntityClassQuake3_construct(){ |
| 281 | // start by creating the default unknown eclass |
| 282 | eclass_bad = EClass_Create( "UNKNOWN_CLASS", Vector3( 0.0f, 0.5f, 0.0f ), "" ); |
| 283 | |
| 284 | EntityClass_realise(); |
| 285 | } |
| 286 | |
| 287 | void EntityClassQuake3_destroy(){ |
| 288 | EntityClass_unrealise(); |
| 289 | |
| 290 | eclass_bad->free( eclass_bad ); |
| 291 | } |
| 292 | |
| 293 | #include "modulesystem/modulesmap.h" |
| 294 | |
| 295 | class EntityClassQuake3Dependencies : |
| 296 | public GlobalRadiantModuleRef, |
| 297 | public GlobalFileSystemModuleRef, |
| 298 | public GlobalShaderCacheModuleRef |
| 299 | { |
| 300 | EClassModulesRef m_eclass_modules; |
| 301 | public: |
| 302 | EntityClassQuake3Dependencies() : |
| 303 | m_eclass_modules( GlobalRadiant().getRequiredGameDescriptionKeyValue( "entityclasstype" ) ){ |
| 304 | } |
| 305 | EClassModules& getEClassModules(){ |
| 306 | return m_eclass_modules.get(); |
| 307 | } |
| 308 | }; |
| 309 | |
| 310 | class EclassManagerAPI |
| 311 | { |
| 312 | EntityClassManager m_eclassmanager; |
| 313 | public: |
| 314 | typedef EntityClassManager Type; |
| 315 | STRING_CONSTANT( Name, "quake3" ); |
| 316 | |
| 317 | EclassManagerAPI(){ |
| 318 | EntityClassQuake3_construct(); |
| 319 | |
| 320 | m_eclassmanager.findOrInsert = &Eclass_ForName; |
| 321 | m_eclassmanager.findListType = &EntityClass_findListType; |
| 322 | m_eclassmanager.forEach = &Eclass_forEach; |
| 323 | m_eclassmanager.attach = &EntityClass_attach; |
| 324 | m_eclassmanager.detach = &EntityClass_detach; |
| 325 | m_eclassmanager.realise = &EntityClass_realise; |
| 326 | m_eclassmanager.unrealise = &EntityClass_unrealise; |
| 327 | |
| 328 | Radiant_attachGameToolsPathObserver( g_EntityClassQuake3 ); |
| 329 | Radiant_attachGameModeObserver( g_EntityClassQuake3 ); |
| 330 | Radiant_attachGameNameObserver( g_EntityClassQuake3 ); |
| 331 | } |
| 332 | ~EclassManagerAPI(){ |
| 333 | Radiant_detachGameNameObserver( g_EntityClassQuake3 ); |
| 334 | Radiant_detachGameModeObserver( g_EntityClassQuake3 ); |
| 335 | Radiant_detachGameToolsPathObserver( g_EntityClassQuake3 ); |
| 336 | |
| 337 | EntityClassQuake3_destroy(); |
| 338 | } |
| 339 | EntityClassManager* getTable(){ |
| 340 | return &m_eclassmanager; |
| 341 | } |
| 342 | }; |
| 343 | |
| 344 | #include "modulesystem/singletonmodule.h" |
| 345 | #include "modulesystem/moduleregistry.h" |
| 346 | |
| 347 | typedef SingletonModule<EclassManagerAPI, EntityClassQuake3Dependencies> EclassManagerModule; |
| 348 | typedef Static<EclassManagerModule> StaticEclassManagerModule; |
| 349 | StaticRegisterModule staticRegisterEclassManager( StaticEclassManagerModule::instance() ); |
| 350 | |
| 351 | EClassModules& EntityClassManager_getEClassModules(){ |
| 352 | return StaticEclassManagerModule::instance().getDependencies().getEClassModules(); |
| 353 | } |