X-Git-Url: https://de.git.xonotic.org/?a=blobdiff_plain;ds=sidebyside;f=radiant%2Feclass.cpp;h=90c8a11b2f2eda7d2d6f487398a536dd150779c5;hb=7b51c9cbca692b2da959845a246847cec35e3939;hp=091595a32f4705c2dfc8de4d416aa2228040bb1a;hpb=830125fad042fad35dc029b6eb57c8156ad7e176;p=xonotic%2Fnetradiant.git diff --git a/radiant/eclass.cpp b/radiant/eclass.cpp index 091595a3..90c8a11b 100644 --- a/radiant/eclass.cpp +++ b/radiant/eclass.cpp @@ -1,5 +1,5 @@ /* - Copyright (C) 1999-2007 id Software, Inc. and contributors. + Copyright (C) 1999-2006 Id Software, Inc. and contributors. For a list of contributors, see the accompanying CONTRIBUTORS file. This file is part of GtkRadiant. @@ -19,454 +19,335 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "stdafx.h" -#include -#if defined ( __linux__ ) || defined ( __APPLE__ ) -#include -#endif -#include "assert.h" +#include "eclass.h" -eclass_t *eclass = NULL; -eclass_t *eclass_bad = NULL; -const vec3_t smallbox[2] = {{-8,-8,-8},{8,8,8}}; -char eclass_directory[1024]; - -qboolean parsing_single = false; -eclass_t *eclass_e; - -/*! - implementation of the EClass manager API - */ -eclass_t** Get_EClass_E(){ - return &eclass_e; -} +#include "debugging/debugging.h" -void Set_Eclass_Found( qboolean b ){ - eclass_found = b; -} - -qboolean Get_Parsing_Single(){ - return parsing_single; -} +#include +#include "ifilesystem.h" -// md3 cache for misc_models -//eclass_t *g_md3Cache = NULL; +#include "string/string.h" +#include "eclasslib.h" +#include "os/path.h" +#include "os/dir.h" +#include "stream/stringstream.h" +#include "moduleobservers.h" -/* +#include "cmdlib.h" - the classname, color triple, and bounding box are parsed out of comments - A ? size means take the exact brush size. +#include "preferences.h" +#include "mainframe.h" - / *QUAKED (0 0 0) ? - / *QUAKED (0 0 0) (-8 -8 -8) (8 8 8) - Flag names can follow the size description: +namespace +{ +typedef std::map EntityClasses; +EntityClasses g_entityClasses; +EntityClass *eclass_bad = 0; +char eclass_directory[1024]; +typedef std::map ListAttributeTypes; +ListAttributeTypes g_listTypes; +} - / *QUAKED func_door (0 .5 .8) ? START_OPEN STONE_SOUND DOOR_DONT_LINK GOLD_KEY SILVER_KEY +EClassModules& EntityClassManager_getEClassModules(); +/*! + implementation of the EClass manager API */ -void CleanEntityList( eclass_t *&pList ){ - while ( pList ) +void CleanEntityList( EntityClasses& entityClasses ){ + for ( EntityClasses::iterator i = entityClasses.begin(); i != entityClasses.end(); ++i ) { - eclass_t* pTemp = pList->next; - - entitymodel *model = pList->model; - while ( model != NULL ) - { - delete []model->pTriList; - if ( model->strSkin ) { - g_string_free( (GString *)model->strSkin, TRUE ); - } - model->strSkin = NULL; - model = model->pNext; - } - - if ( pList->modelpath ) { - free( pList->modelpath ); - pList->modelpath = NULL; - } - if ( pList->skinpath ) { - free( pList->skinpath ); - pList->skinpath = NULL; - } - - free( pList->name ); - free( pList->comments ); - free( pList ); - pList = pTemp; + ( *i ).second->free( ( *i ).second ); } - - pList = NULL; - + entityClasses.clear(); } +void Eclass_Clear(){ + CleanEntityList( g_entityClasses ); + g_listTypes.clear(); +} -void CleanUpEntities(){ - // NOTE: maybe some leak checks needed .. older versions of Radiant looked like they were freezing more stuff - CleanEntityList( eclass ); - //CleanEntityList(g_md3Cache); - if ( eclass_bad ) { - free( eclass_bad->name ); - free( eclass_bad->comments ); - free( eclass_bad ); - eclass_bad = NULL; +EntityClass* EClass_InsertSortedList( EntityClasses& entityClasses, EntityClass *entityClass ){ + std::pair result = entityClasses.insert( EntityClasses::value_type( entityClass->name(), entityClass ) ); + if ( !result.second ) { + entityClass->free( entityClass ); } + return ( *result.first ).second; } -void EClass_InsertSortedList( eclass_t *&pList, eclass_t *e ){ - eclass_t *s; +EntityClass* Eclass_InsertAlphabetized( EntityClass *e ){ + return EClass_InsertSortedList( g_entityClasses, e ); +} - if ( !pList ) { - pList = e; - return; +void Eclass_forEach( EntityClassVisitor& visitor ){ + for ( EntityClasses::iterator i = g_entityClasses.begin(); i != g_entityClasses.end(); ++i ) + { + visitor.visit( ( *i ).second ); } +} - s = pList; - if ( stricmp( e->name, s->name ) < 0 ) { - e->next = s; - pList = e; - return; - } - - do - { - if ( !s->next || stricmp( e->name, s->next->name ) < 0 ) { - e->next = s->next; - s->next = e; - return; - } - s = s->next; - } while ( 1 ); +class RadiantEclassCollector : public EntityClassCollector +{ +public: +void insert( EntityClass* eclass ){ + Eclass_InsertAlphabetized( eclass ); } +void insert( const char* name, const ListAttributeType& list ){ + g_listTypes.insert( ListAttributeTypes::value_type( name, list ) ); +} +}; -/* - ================= - Eclass_InsertAlphabetized - ================= - */ -void Eclass_InsertAlphabetized( eclass_t *e ){ -#if 1 - EClass_InsertSortedList( eclass, e ); -#else - eclass_t *s; - - if ( !eclass ) { - eclass = e; - return; - } - +RadiantEclassCollector g_collector; - s = eclass; - if ( stricmp( e->name, s->name ) < 0 ) { - e->next = s; - eclass = e; - return; +const ListAttributeType* EntityClass_findListType( const char* name ){ + ListAttributeTypes::iterator i = g_listTypes.find( name ); + if ( i != g_listTypes.end() ) { + return &( *i ).second; } - - do - { - if ( !s->next || stricmp( e->name, s->next->name ) < 0 ) { - e->next = s->next; - s->next = e; - return; - } - s = s->next; - } while ( 1 ); -#endif + return 0; } -/*! - This looks at each eclass_t, if it has a "modelpath" set then it leaves it alone - if it's not set it checks to see if a file called "sprites/.*" exists, and - if it does exist then it sets the "modelpath" to "sprites/.spr" - */ -void Eclass_CreateSpriteModelPaths(){ - int Counts[4] = { 0, 0, 0, 0 }; - char filename[512]; // should be big enough, ExtractFileBase doesn't take a buffer size... - eclass_t *e; - - // get a list of all sprite/*>* files in all sprite/ directories - Sys_Printf( "Searching VFS for files in sprites/*.* that match entity names...\n" ); - GSList *pFiles = vfsGetFileList( "sprites", NULL ); - GSList *pFile; - if ( pFiles ) { +class EntityClassFilterMode +{ +public: +bool filter_mp_sp; +const char* mp_ignore_prefix; +const char* sp_ignore_prefix; - // find an eclass without a modelpath. - for ( e = eclass ; e ; e = e->next ) - { - Counts[0]++; - if ( e->modelpath ) { -#ifdef _DEBUG - Sys_Printf( "Ignoring sprite for entity %s (modelpath: \"%s\")\n",e->name,e->modelpath ); -#endif - Counts[1]++; - continue; // ignore this eclass, it's already got a model +EntityClassFilterMode() : + filter_mp_sp( !string_empty( g_pGameDescription->getKeyValue( "eclass_filter_gamemode" ) ) ), + mp_ignore_prefix( g_pGameDescription->getKeyValue( "eclass_sp_prefix" ) ), + sp_ignore_prefix( g_pGameDescription->getKeyValue( "eclass_mp_prefix" ) ){ + if ( string_empty( mp_ignore_prefix ) ) { + mp_ignore_prefix = "sp_"; + } + if ( string_empty( sp_ignore_prefix ) ) { + sp_ignore_prefix = "mp_"; + } +} +}; + +class EntityClassesLoadFile +{ +const EntityClassScanner& scanner; +const char* m_directory; +public: +EntityClassesLoadFile( const EntityClassScanner& scanner, const char* directory ) : scanner( scanner ), m_directory( directory ){ +} +void operator()( const char* name ) const { + EntityClassFilterMode filterMode; + + if ( filterMode.filter_mp_sp ) { + if ( string_empty( GlobalRadiant().getGameMode() ) || string_equal( GlobalRadiant().getGameMode(), "sp" ) ) { + if ( string_equal_n( name, filterMode.sp_ignore_prefix, strlen( filterMode.sp_ignore_prefix ) ) ) { + globalOutputStream() << "Ignoring '" << name << "'\n"; + return; } - - // TODO: remove this check when we can have sprites for non-fixed size entities. - if ( !e->fixedsize ) { -#ifdef _DEBUG - Sys_Printf( "Ignoring sprite for non-fixed-size entity %s\n",e->name ); -#endif - Counts[2]++; - continue; // can't have sprites for non-fixed size entities (yet!) + } + else + { + if ( string_equal_n( name, filterMode.mp_ignore_prefix, strlen( filterMode.mp_ignore_prefix ) ) ) { + globalOutputStream() << "Ignoring '" << name << "'\n"; + return; } + } + } + // for a given name, we grab the first .def in the vfs + // this allows to override baseq3/scripts/entities.def for instance + StringOutputStream relPath( 256 ); + relPath << m_directory << name; - Sys_Printf( "Searching for sprite for fixed-size entity %s...",e->name ); + scanner.scanFile( g_collector, relPath.c_str() ); +} +}; - pFile = pFiles; // point to start of list +struct PathLess +{ + bool operator()( const CopiedString& path, const CopiedString& other ) const { + return path_less( path.c_str(), other.c_str() ); + } +}; - // look for a file that has the same name, with any extension. - bool Found = FALSE; - while ( pFile ) - { +typedef std::map Paths; - // strip the path/ and the .extension. - ExtractFileBase( (char *)pFile->data,filename ); +void EntityClassQuake3_constructDirectory( const char* directory, const char* extension, Paths& paths ){ + globalOutputStream() << "EntityClass: searching " << makeQuoted( directory ) << " for *." << extension << '\n'; + Directory_forEach(directory, matchFileExtension(extension, [&](const char *name) { + paths.insert(Paths::value_type(name, directory)); + })); +} - // does the eclass name match the filename? - if ( stricmp( e->name,filename ) == 0 ) { - // yes, so generate a sprite filename using the all-encompasing .spr extension - // so that the model wrapper knows the sprite model plugin will be the model - // plugin used to render it. - CString strSpriteName; - strSpriteName.Format( "sprites/%s.spr",e->name ); - e->modelpath = strdup( strSpriteName.GetBuffer() ); - Sys_Printf( "Found! (\"%s\")\n",(char *)pFile->data ); - Counts[3]++; - Found = TRUE; - } - pFile = pFile->next; - } - if ( !Found ) { - Sys_Printf( "not found\n" ); - } +void EntityClassQuake3_Construct(){ + StringOutputStream baseDirectory( 256 ); + StringOutputStream gameDirectory( 256 ); + const char* basegame = GlobalRadiant().getRequiredGameDescriptionKeyValue( "basegame" ); + const char* gamename = GlobalRadiant().getGameName(); + baseDirectory << GlobalRadiant().getGameToolsPath() << basegame << '/'; + gameDirectory << GlobalRadiant().getGameToolsPath() << gamename << '/'; + class LoadEntityDefinitionsVisitor : public EClassModules::Visitor + { + const char* baseDirectory; + const char* gameDirectory; +public: + LoadEntityDefinitionsVisitor( const char* baseDirectory, const char* gameDirectory ) + : baseDirectory( baseDirectory ), gameDirectory( gameDirectory ){ + } + void visit( const char* name, const EntityClassScanner& table ) const { + Paths paths; + EntityClassQuake3_constructDirectory( baseDirectory, table.getExtension(), paths ); + if ( !string_equal( baseDirectory, gameDirectory ) ) { + EntityClassQuake3_constructDirectory( gameDirectory, table.getExtension(), paths ); } - vfsClearFileDirList( &pFiles ); + for ( Paths::iterator i = paths.begin(); i != paths.end(); ++i ) + { + EntityClassesLoadFile( table, ( *i ).second ) ( ( *i ).first.c_str() ); + } } - Sys_Printf( "%d entities were scanned\n" - "%d entities that already had models/sprites were ignored\n" - "%d non-fixed-size entities were ignored\n" - "%d entities did not have matching sprite files\n" - "%d entities had sprite files and have been attached\n", - Counts[0],Counts[1],Counts[2],Counts[0] - Counts[3],Counts[3] ); + }; + EntityClassManager_getEClassModules().foreachModule( LoadEntityDefinitionsVisitor( baseDirectory.c_str(), gameDirectory.c_str() ) ); } -void EClass_InitForFileList( GSList *pFiles, _EClassTable *pTable ){ - GSList *pFile = pFiles; - while ( pFile ) - { - // for a given name, we grab the first .def in the vfs - // this allows to override baseq3/scripts/entities.def for instance - char relPath[PATH_MAX]; - strcpy( relPath, "scripts/" ); - strcat( relPath, (char*)pFile->data ); - if ( !vfsGetFullPath( relPath, 0, 0 ) ) { - Sys_FPrintf( SYS_ERR, "Failed to find the full path for '%s' in the VFS\n", relPath ); - } - else{ - pTable->m_pfnScanFile( vfsGetFullPath( relPath, 0, 0 ) ); - } - pFile = pFile->next; - } -} +EntityClass *Eclass_ForName( const char *name, bool has_brushes ){ + ASSERT_NOTNULL( name ); -/*! - Manually create an eclass_t, for when no modules exist. - this replaces and centralizes the eclass_t allocation - */ -eclass_t * EClass_Create( const char *name, float col1, float col2, float col3, const vec3_t *mins, const vec3_t *maxs, const char *comments ){ - eclass_t *e; - char color[128]; - - e = (eclass_t*)malloc( sizeof( *e ) ); - memset( e, 0, sizeof( *e ) ); - - e->name = strdup( name ); - - // grab the color, reformat as texture name - e->color[0] = col1; - e->color[1] = col2; - e->color[2] = col3; - sprintf( color, "(%f %f %f)", e->color[0], e->color[1], e->color[2] ); - e->texdef.SetName( color ); - - // supplied size ? - if ( mins && maxs ) { - // Hydra: - // If we set worldspawn to be a fixed-size all the textures are - // displayed as flat-shaded. This is a KLUDGE now that we have - // multiple game support as the worldspawn entity is game specific. - // Note that this is only ever fixed for the user if a definition - // for the worldspawn entity was not loaded, this can happen for - // several reasons: - // a) no entity definition plugin exists - // b) no entity definition files were found - // c) no entity definition file contained an entry for worldspawn. - - if ( stricmp( name,"worldspawn" ) != 0 ) { - e->fixedsize = true; - } + if ( string_empty( name ) ) { + return eclass_bad; + } - // copy the sizes.. - memcpy( e->mins,mins,sizeof( vec3_t ) ); - memcpy( e->maxs,maxs,sizeof( vec3_t ) ); + EntityClasses::iterator i = g_entityClasses.find( name ); + if ( i != g_entityClasses.end() && string_equal( ( *i ).first, name ) ) { + return ( *i ).second; } - if ( comments ) { - e->comments = strdup( comments ); + EntityClass* e = EntityClass_Create_Default( name, has_brushes ); + return Eclass_InsertAlphabetized( e ); +} + +class EntityClassQuake3 : public ModuleObserver +{ +std::size_t m_unrealised; +ModuleObservers m_observers; +public: +EntityClassQuake3() : m_unrealised( 4 ){ +} +void realise(){ + if ( --m_unrealised == 0 ) { + //globalOutputStream() << "Entity Classes: realise\n"; + EntityClassQuake3_Construct(); + m_observers.realise(); } - else - { - e->comments = (char*)malloc( 1 ); - e->comments[0] = '\0'; +} +void unrealise(){ + if ( ++m_unrealised == 1 ) { + m_observers.unrealise(); + //globalOutputStream() << "Entity Classes: unrealise\n"; + Eclass_Clear(); } +} +void attach( ModuleObserver& observer ){ + m_observers.attach( observer ); +} +void detach( ModuleObserver& observer ){ + m_observers.detach( observer ); +} +}; + +EntityClassQuake3 g_EntityClassQuake3; - return e; +void EntityClass_attach( ModuleObserver& observer ){ + g_EntityClassQuake3.attach( observer ); +} +void EntityClass_detach( ModuleObserver& observer ){ + g_EntityClassQuake3.detach( observer ); } -void Eclass_Init(){ - GSList *pFiles; +void EntityClass_realise(){ + g_EntityClassQuake3.realise(); +} +void EntityClass_unrealise(){ + g_EntityClassQuake3.unrealise(); +} +void EntityClassQuake3_construct(){ // start by creating the default unknown eclass - eclass_bad = EClass_Create( "UNKNOWN_CLASS", 0, 0.5, 0,NULL,NULL,NULL ); - - // now scan the definitions - _EClassTable *pTable = &g_EClassDefTable; - while ( pTable ) - { - // read in all scripts/*. - pFiles = vfsGetFileList( "scripts", pTable->m_pfnGetExtension() ); - if ( pFiles ) { - GSList *pFile = pFiles; - while ( pFile ) - { - /*! - \todo the MP/SP filtering rules need to be CLEANED UP and SANITIZED - */ - // HACK - // JKII SP/MP mapping mode - if ( g_pGameDescription->mGameFile == "jk2.game" || g_pGameDescription->mGameFile == "ja.game" ) { - if ( !strcmp( ValueForKey( g_qeglobals.d_project_entity, "gamemode" ), "sp" ) ) { - // SP mapping, ignore mp_*.def - char *name = (char *)pFile->data; - if ( name[0] == 'm' && name[1] == 'p' && name[2] == '_' ) { - Sys_Printf( "Single Player mapping mode. Ignoring '%s'\n", name ); - pFile = pFile->next; - continue; - } - } - else - { - // MP mapping, ignore sp_*.def - char *name = (char *)pFile->data; - if ( name[0] == 's' && name[1] == 'p' && name[2] == '_' ) { - Sys_Printf( "Multiplayer mapping mode. Ignoring '%s'\n", name ); - pFile = pFile->next; - continue; - } - } - } - // RIANT - // STVEF SP/MP mapping mode - else if ( g_pGameDescription->mGameFile == "stvef.game" ) { - if ( !strcmp( ValueForKey( g_qeglobals.d_project_entity, "gamemode" ), "sp" ) ) { - // SP mapping, ignore mp_*.def - char *name = (char *)pFile->data; - if ( name[0] == 'm' && name[1] == 'p' && name[2] == '_' ) { - Sys_Printf( "Single Player mapping mode. Ignoring '%s'\n", name ); - pFile = pFile->next; - continue; - } - } - else - { - // HM mapping, ignore sp_*.def - char *name = (char *)pFile->data; - if ( name[0] == 'h' && name[1] == 'm' && name[2] == '_' ) { - Sys_Printf( "HoloMatch mapping mode. Ignoring '%s'\n", name ); - pFile = pFile->next; - continue; - } - } - } - // for a given name, we grab the first .def in the vfs - // this allows to override baseq3/scripts/entities.def for instance - char relPath[PATH_MAX]; - strcpy( relPath, "scripts/" ); - strcat( relPath, (char*)pFile->data ); - char *fullpath = vfsGetFullPath( relPath, 0, 0 ); - if ( !fullpath ) { - Sys_FPrintf( SYS_ERR, "Failed to find the full path for \"%s\" in the VFS\n", relPath ); - } - else{ - pTable->m_pfnScanFile( fullpath ); - } - if ( g_pGameDescription->mEClassSingleLoad ) { - break; - } - pFile = pFile->next; - } - vfsClearFileDirList( &pFiles ); - pFiles = NULL; - } - else{ - Sys_FPrintf( SYS_ERR, "Didn't find any scripts/*.%s files to load EClass information\n", pTable->m_pfnGetExtension() ); - } + eclass_bad = EClass_Create( "UNKNOWN_CLASS", Vector3( 0.0f, 0.5f, 0.0f ), "" ); - // we deal with two formats max, if the other table exists, loop again - if ( g_bHaveEClassExt && pTable == &g_EClassDefTable ) { - pTable = &g_EClassExtTable; - } - else{ - pTable = NULL; // done, exit - } - } - Eclass_CreateSpriteModelPaths(); + EntityClass_realise(); } -eclass_t *Eclass_ForName( const char *name, qboolean has_brushes ){ - eclass_t *e; - - if ( !name || *name == '\0' ) { - return eclass_bad; - } +void EntityClassQuake3_destroy(){ + EntityClass_unrealise(); -#ifdef _DEBUG - // grouping stuff, not an eclass - if ( strcmp( name, "group_info" ) == 0 ) { - Sys_Printf( "WARNING: unexpected group_info entity in Eclass_ForName\n" ); - } -#endif + eclass_bad->free( eclass_bad ); +} - if ( !name ) { - return eclass_bad; - } +#include "modulesystem/modulesmap.h" + +class EntityClassQuake3Dependencies : + public GlobalRadiantModuleRef, + public GlobalFileSystemModuleRef, + public GlobalShaderCacheModuleRef +{ +EClassModulesRef m_eclass_modules; +public: +EntityClassQuake3Dependencies() : + m_eclass_modules( GlobalRadiant().getRequiredGameDescriptionKeyValue( "entityclasstype" ) ){ +} +EClassModules& getEClassModules(){ + return m_eclass_modules.get(); +} +}; + +class EclassManagerAPI +{ +EntityClassManager m_eclassmanager; +public: +typedef EntityClassManager Type; +STRING_CONSTANT( Name, "quake3" ); + +EclassManagerAPI(){ + EntityClassQuake3_construct(); + + m_eclassmanager.findOrInsert = &Eclass_ForName; + m_eclassmanager.findListType = &EntityClass_findListType; + m_eclassmanager.forEach = &Eclass_forEach; + m_eclassmanager.attach = &EntityClass_attach; + m_eclassmanager.detach = &EntityClass_detach; + m_eclassmanager.realise = &EntityClass_realise; + m_eclassmanager.unrealise = &EntityClass_unrealise; + + Radiant_attachGameToolsPathObserver( g_EntityClassQuake3 ); + Radiant_attachGameModeObserver( g_EntityClassQuake3 ); + Radiant_attachGameNameObserver( g_EntityClassQuake3 ); +} +~EclassManagerAPI(){ + Radiant_detachGameNameObserver( g_EntityClassQuake3 ); + Radiant_detachGameModeObserver( g_EntityClassQuake3 ); + Radiant_detachGameToolsPathObserver( g_EntityClassQuake3 ); - for ( e = eclass ; e ; e = e->next ) - if ( !strcmp( name, e->name ) ) { - return e; - } + EntityClassQuake3_destroy(); +} +EntityClassManager* getTable(){ + return &m_eclassmanager; +} +}; - // create a new class for it - if ( has_brushes ) { - e = EClass_Create( name, 0, 0.5, 0,NULL,NULL,"Not found in source." ); - } - else - { - e = EClass_Create( name, 0, 0.5, 0,&smallbox[0],&smallbox[1],"Not found in source." ); - } +#include "modulesystem/singletonmodule.h" +#include "modulesystem/moduleregistry.h" - Eclass_InsertAlphabetized( e ); +typedef SingletonModule EclassManagerModule; +typedef Static StaticEclassManagerModule; +StaticRegisterModule staticRegisterEclassManager( StaticEclassManagerModule::instance() ); - return e; +EClassModules& EntityClassManager_getEClassModules(){ + return StaticEclassManagerModule::instance().getDependencies().getEClassModules(); }