C_BaseEntity *CClassMap::CreateEntity( const char *mapname )
{
	int c = m_ClassDict.Count();
	int i;

	for ( i = 0; i < c; i++ )
	{
		classentry_t *lookup = &m_ClassDict[ i ];
		if ( !lookup )
			continue;

		if ( Q_stricmp( lookup->GetMapName(), mapname ) )
			continue;

		if ( !lookup->factory )
		{
#if defined( _DEBUG )
			Msg( "No factory for %s/%s\n", lookup->GetMapName(), m_ClassDict.GetElementName( i ) );
#endif
			continue;
		}

		return ( *lookup->factory )();
	}

	return NULL;
}
void CTextureManager::DebugPrintUsedTextures( void )
{
	for ( int i = m_TextureList.First(); i != m_TextureList.InvalidIndex(); i = m_TextureList.Next( i ) )
	{
		ITextureInternal *pTexture = m_TextureList[i];
		Msg( "Texture: '%s' RefCount: %d\n", pTexture->GetName(), pTexture->GetReferenceCount() );
	}

	if ( m_TextureExcludes.Count() )
	{
		Msg( "\nExcluded Textures: (%d)\n", m_TextureExcludes.Count() );
		for ( int i = m_TextureExcludes.First(); i != m_TextureExcludes.InvalidIndex(); i = m_TextureExcludes.Next( i ) )
		{
			char buff[256];
			const char *pName = m_TextureExcludes.GetElementName( i );
			V_snprintf( buff, sizeof( buff ), "Excluded: %d '%s' \n", m_TextureExcludes[i], pName );
	
			// an excluded texture is valid, but forced tiny
			if ( IsTextureLoaded( pName ) )
			{
				Msg( buff );
			}
			else
			{
				// warn as unknown, could be a spelling error
				Warning( buff );
			}
		}
	}
}
	virtual void Save( const SaveRestoreFieldInfo_t &fieldInfo, ISave *pSave )
	{
		CUtlDict< ConceptHistory_t, int > *ch = ((CUtlDict< ConceptHistory_t, int > *)fieldInfo.pField);
		int count = ch->Count();
		pSave->WriteInt( &count );
		for ( int i = 0 ; i < count; i++ )
		{
			ConceptHistory_t *pHistory = &(*ch)[ i ];

			pSave->StartBlock();
			{

				// Write element name
				pSave->WriteString( ch->GetElementName( i ) );

				// Write data
				pSave->WriteAll( pHistory );
				// Write response blob
				bool hasresponse = pHistory->response != NULL ? true : false;
				pSave->WriteBool( &hasresponse );
				if ( hasresponse )
				{
					pSave->WriteAll( pHistory->response );
				}
				// TODO: Could blat out pHistory->criteria pointer here, if it's needed
			}
			pSave->EndBlock();
		}
	}
char const *CMapEntities::GetName( int number )
{
	if ( number < 0 || number >= (int)m_Entities.Count() )
		return NULL;

	return m_Entities.GetElementName( number );
}
//-----------------------------------------------------------------------------
// Purpose: 
// Input  : gamematerial - 
// Output : char const
//-----------------------------------------------------------------------------
char const *CDecalEmitterSystem::ImpactDecalForGameMaterial( int gamematerial )
{
	char gm[ 2 ];
	gm[0] = (char)gamematerial;
	gm[1] = 0;

	int idx = m_GameMaterialTranslation.Find( gm );
	if ( idx == m_GameMaterialTranslation.InvalidIndex() )
		return NULL;

	return m_Decals.GetElementName( m_GameMaterialTranslation.Element(idx) );
}
void CGECreateServer::PopulateControls( void )
{
	// Only populate on first load
	if ( !m_bFirstLoad )
		return;

	// Populate the map list
	ComboBox *maplist = dynamic_cast<ComboBox*>( FindChildByName("MapList") );
	if ( maplist )
	{
		// Clear the list first
		maplist->DeleteAllItems();
		
		FileFindHandle_t findHandle; // note: FileFINDHandle
		char file[32];

		maplist->AddItem( "#SERVER_RANDOM_MAP", new KeyValues(RANDOM_VALUE) );
		const char *pFilename = filesystem->FindFirstEx( "maps\\*.bsp", "MOD", &findHandle );
		while ( pFilename )
		{
			if ( stricmp(pFilename, "ge_transition.bsp") ) //They don't need to pick our dinky crash avoidance map.
			{
				// Add the map to the list
				Q_FileBase(pFilename, file, 32);
				maplist->AddItem(file, new KeyValues(file));
			}

			pFilename = filesystem->FindNext( findHandle );
		}

		filesystem->FindClose( findHandle );
		maplist->SetNumberOfEditLines( 10 );
		maplist->SetEditable( false );
		maplist->GetMenu()->ForceCalculateWidth();
		maplist->ActivateItemByRow( 0 );
	}

	// Populate the weapon list
	ComboBox *weaponlist = dynamic_cast<ComboBox*>( FindChildByName("WeaponList") );
	if ( weaponlist )
	{
		weaponlist->DeleteAllItems();

		// TAKEN DIRECTLY FROM ge_loadoutmanager.cpp
		// Parsing individually allows us to overwrite the default sets with custom ones
		// Multiple custom sets can be defined as needed (can even make sets per gameplay)
		if ( !GELoadoutParser.HasBeenParsed() )
		{
			GELoadoutParser.InitParser("scripts/loadouts/weapon_sets_default.X");
			GELoadoutParser.SetHasBeenParsed( false );
			GELoadoutParser.InitParser("scripts/loadouts/weapon_sets_custom*.X");
		}

		// Random loadout
		weaponlist->AddItem( "#SERVER_RANDOM_SET", new KeyValues("random_loadout") );
	
		FOR_EACH_DICT( m_WeaponSets, idx )
		{
			if (Q_strstr(m_WeaponSets.GetElementName(idx), "_mhide"))
				continue;

			int id = weaponlist->AddItem( m_WeaponSets.GetElementName(idx), NULL );
			weaponlist->GetMenu()->SetItemEnabled( id, false );

			for ( int k=m_WeaponSets[idx]->First(); k != m_WeaponSets[idx]->InvalidIndex(); k = m_WeaponSets[idx]->Next(k) )
				weaponlist->AddItem( m_WeaponSets[idx]->Element(k), new KeyValues(m_WeaponSets[idx]->GetElementName(k)) );
		}

		weaponlist->SetEditable( false );
		weaponlist->SetNumberOfEditLines( 15 );
		weaponlist->GetMenu()->ForceCalculateWidth();
		weaponlist->ActivateItemByRow( 0 );
	}

	// Populate the scenario list
	ComboBox *scenariolist = dynamic_cast<ComboBox*>( FindChildByName("ScenarioList") );
	if ( scenariolist )
	{
		// Clear the list first
		scenariolist->DeleteAllItems();

		FileFindHandle_t findHandle; // note: FileFINDHandle
		char file[32];

		scenariolist->AddItem( "#SERVER_RANDOM_SCENARIO", new KeyValues(RANDOM_VALUE) );
		const char *pFilename = filesystem->FindFirstEx( PYDIR, "MOD", &findHandle );
		while ( pFilename )
		{
			// Add the scenario to the list if not __init__
			if ( !Q_stristr( pFilename, "__init__") )
			{
				Q_FileBase( pFilename, file, 32 );
				scenariolist->AddItem( file, new KeyValues(file) );
			}

			pFilename = filesystem->FindNext( findHandle );
		}

		filesystem->FindClose( findHandle );
		scenariolist->SetEditable( false );
		scenariolist->SetNumberOfEditLines( 10 );
		scenariolist->GetMenu()->ForceCalculateWidth();
		scenariolist->ActivateItemByRow( 0 );
	}

	// Populate the bot difficulty list
	ComboBox *botdifflist = dynamic_cast<ComboBox*>( FindChildByName("BotLevel") );
	if ( botdifflist && botdifflist->GetItemCount() == 0 )
	{
		// Hard coded items (sorry!)
		botdifflist->AddItem( "#BOT_LEVEL_EASY", new KeyValues("1") );
		botdifflist->AddItem( "#BOT_LEVEL_MED", new KeyValues("3") );
		botdifflist->AddItem( "#BOT_LEVEL_HARD", new KeyValues("6") );
		botdifflist->AddItem( "#BOT_LEVEL_UBER", new KeyValues("9") );

		// Admin
		botdifflist->SetEditable( false );
		botdifflist->SetNumberOfEditLines( 4 );
		botdifflist->GetMenu()->ForceCalculateWidth();
		botdifflist->ActivateItemByRow( 0 );
	}

	// Populate the turbo mode list
	ComboBox *turbolist = dynamic_cast<ComboBox*>( FindChildByName("TurboMode") );
	if ( turbolist && turbolist->GetItemCount() == 0 )
	{
		// Hard coded items (sorry!)
		turbolist->AddItem( "#TURBO_MODE_NORM", new KeyValues("1.000000") );
		turbolist->AddItem( "#TURBO_MODE_FAST", new KeyValues("1.500000") );
		turbolist->AddItem( "#TURBO_MODE_LIGHT", new KeyValues("1.850000") );
		
		// Admin
		turbolist->SetEditable( false );
		turbolist->SetNumberOfEditLines( 3 );
		turbolist->GetMenu()->ForceCalculateWidth();
		turbolist->ActivateItemByRow( 0 );
	}

	// Load the default/saved values from our command map
	for ( KeyValues *kv = m_kvCmdMap->GetFirstTrueSubKey(); kv; kv = kv->GetNextTrueSubKey() )
	{
		// Get our value (or default)
		const char *value = (m_kvCmdValues->FindKey( kv->GetName() )) ? m_kvCmdValues->GetString( kv->GetName() ) : NULL;
		if ( !value )
			value = kv->GetString( "default", "" );

		if ( !Q_stricmp(kv->GetString("type"), "CHOICE") )
		{
			ComboBox *panel = dynamic_cast<ComboBox*>( FindChildByName(kv->GetName()) );
			if ( !panel )
				continue;

			// Search through all our items to find a matching value
			for ( int i=0; i < panel->GetItemCount(); i++ )
			{
				int id = panel->GetItemIDFromRow( i );
				KeyValues* userdata = panel->GetItemUserData( id );
				
				if ( userdata && !Q_stricmp(value, userdata->GetName()) )
				{
					panel->ActivateItem( id );
					break;
				}
			}
		}
		else if ( !Q_stricmp(kv->GetString("type"), "TEXT") )
		{
			TextEntry *panel = dynamic_cast<TextEntry*>( FindChildByName(kv->GetName()) );
			if ( !panel )
				continue;

			panel->SetText( value );
		}
		else if ( !Q_stricmp(kv->GetString("type"), "BOOL") )
		{
			CheckButton *panel = dynamic_cast<CheckButton*>( FindChildByName(kv->GetName()) );
			if ( !panel )
				continue;

			if ( !Q_stricmp(value, "1") )
				panel->SetSelected( true );
			else
				panel->SetSelected( false );
		}
	}

	m_bFirstLoad = false;
}
void Correlate( CUtlRBTree< ReferencedFile, int >& referencedfiles, CUtlVector< FileEntry >& contentfiles, const char *modname )
{
	int i;
	int c = contentfiles.Count();
	
	double totalDiskSize = 0;
	double totalReferencedDiskSize = 0;
	double totalWhiteListDiskSize = 0;

	for ( i = 0; i < c; ++i )
	{
		totalDiskSize += contentfiles [ i ].size;
	}

	vprint( 0, "Content tree size on disk %s\n", Q_pretifymem( totalDiskSize, 3 ) );
	
	// Analysis is to walk tree and see which files on disk are referenced in the .lst files
	// Need a fast lookup from file symbol to referenced list
	CUtlRBTree< ReferencedFile, int >	tree( 0, 0, ReferencedFileLessFunc );
	c = referencedfiles.Count();
	for ( i = 0 ; i < c; ++i )
	{
		tree.Insert( referencedfiles[ i ] );
	}

	// Now walk the on disk file and see check off resources which are in referenced
	c = contentfiles.Count();
	int invalidindex = tree.InvalidIndex();
	unsigned int refcounted = 0;
	unsigned int whitelisted = 0;

	filesystem->RemoveFile( CFmtStr( "%swhitelist.lst", g_szReslistDir ), "GAME" );

	for ( i = 0; i < c; ++i )
	{
		FileEntry & entry = contentfiles[ i ];

		ReferencedFile foo;
		foo.sym = entry.sym;

		bool gameref = tree.Find( foo ) != invalidindex;
		char const *fn = g_Analysis.symbols.String( entry.sym );

		bool whitelist = g_WhiteList.Find( entry.sym ) != g_WhiteList.InvalidIndex();

		if ( gameref || whitelist )
		{
			entry.referenced = gameref ? REFERENCED_GAME : REFERENCED_WHITELIST;
			totalReferencedDiskSize += entry.size;
			if ( entry.referenced == REFERENCED_WHITELIST )
			{
				logprint( CFmtStr( "%swhitelist.lst", g_szReslistDir ), "\"%s\\%s\"\n", modname, fn );

				totalWhiteListDiskSize += entry.size;
				++whitelisted;
			}
			++refcounted;
		}
	}

	vprint( 0, "Found %i referenced (%i whitelist) files in tree, %s\n", refcounted, whitelisted, Q_pretifymem( totalReferencedDiskSize, 2 ) );
	vprint( 0, "%s appear unused\n", Q_pretifymem( totalDiskSize - totalReferencedDiskSize, 2 ) );

	// Now sort and dump the unreferenced ones..
	vprint( 0, "Sorting unreferenced files list...\n" );

	CUtlRBTree< FileEntry, int >	unreftree( 0, 0, FileEntryLessFunc );
	for ( i = 0; i < c; ++i )
	{
		FileEntry & entry = contentfiles[ i ];
		if ( entry.referenced != REFERENCED_NO )
			continue;

		unreftree.Insert( entry );
	}

	// Now walk the unref tree in order
	i = unreftree.FirstInorder();
	invalidindex = unreftree.InvalidIndex();
	int index = 0;
	while ( i != invalidindex )
	{
		FileEntry & entry = unreftree[ i ];

		if ( showreferencedfiles )
		{
			vprint( 1, "%6i %12s: %s\n", ++index, Q_pretifymem( entry.size, 2 ), g_Analysis.symbols.String( entry.sym ) );
		}
		
		i = unreftree.NextInorder( i );
	}

	if ( showmapfileusage )
	{
		vprint( 0, "Writing referenced.csv...\n" );

		// Now walk the list of referenced files and print out how many and which maps reference them
		i = tree.FirstInorder();
		invalidindex = tree.InvalidIndex();
		index = 0;
		while ( i != invalidindex )
		{
			ReferencedFile & entry = tree[ i ];

			char ext[ 32 ];
			Q_ExtractFileExtension( g_Analysis.symbols.String( entry.sym ), ext, sizeof( ext ) );

			logprint( "referenced.csv", "\"%s\",\"%s\",%d", g_Analysis.symbols.String( entry.sym ), ext, entry.maplist.Count() );

			int mapcount = entry.maplist.Count();
			for ( int j = 0 ; j < mapcount; ++j )
			{
				char basemap[ 128 ];
				Q_FileBase( g_Analysis.symbols.String( entry.maplist[ j ] ), basemap, sizeof( basemap ) );
				logprint( "referenced.csv", ",\"%s\"", basemap );
			}

			logprint( "referenced.csv", "\n" );
			
			i = tree.NextInorder( i );
		}
	}


	vprint( 0, "\nBuilding directory summary list...\n" );

	// Now build summaries by root branch off of gamedir (e.g., for sound, materials, models, etc.)
	CUtlDict< DirEntry, int > directories;
	invalidindex = directories.InvalidIndex();
	for ( i = 0; i < c; ++i )
	{
		FileEntry & entry = contentfiles[ i ];

		// Get the dir name
		char const *dirname = g_Analysis.symbols.String( entry.sym );

		const char *backslash = strstr( dirname, "\\" );

		char dir[ 256 ];
		if ( !backslash )
		{
			dir[0] = 0;
		}
		else
		{
			Q_strncpy( dir, dirname, backslash - dirname + 1);
		}

		
		int idx = directories.Find( dir );
		if ( idx == invalidindex )
		{
			DirEntry foo;
			idx = directories.Insert( dir, foo );
		}

		DirEntry & de = directories[ idx ];
		de.total += entry.size;
		if ( entry.referenced == REFERENCED_NO )
		{
			de.unreferenced += entry.size;
		}
		if ( entry.referenced == REFERENCED_WHITELIST )
		{
			de.whitelist += entry.size;
		}
	}

	if ( spewdeletions )
	{
		// Spew deletion commands to console
		if ( immediatedelete )
		{
			vprint( 0, "\n\nDeleting files...\n" );
		}
		else
		{
			vprint( 0, "\n\nGenerating deletions.bat\n" );
		}

		i = unreftree.FirstInorder();
		invalidindex = unreftree.InvalidIndex();
		float deletionSize = 0.0f;
		int deletionCount = 0;

		while ( i != invalidindex )
		{
			FileEntry & entry = unreftree[ i ];
			i = unreftree.NextInorder( i );

			// Don't delete stuff that's in the white list
			if ( g_WhiteList.Find( entry.sym ) != g_WhiteList.InvalidIndex() )
			{
				if ( verbose )
				{
					vprint( 0, "whitelist blocked deletion of %s\n", g_Analysis.symbols.String( entry.sym ) );
				}
				continue;
			}

			++deletionCount;
			deletionSize += entry.size;

			if ( immediatedelete ) 
			{
				if ( _chmod( g_Analysis.symbols.String( entry.sym ), _S_IWRITE ) == -1 )
				{
					vprint( 0, "Could not find file %s\n", g_Analysis.symbols.String( entry.sym ) );
				}
				if ( _unlink( g_Analysis.symbols.String( entry.sym ) ) == -1 )
				{
					vprint( 0, "Could not delete file %s\n", g_Analysis.symbols.String( entry.sym ) );
				}

				if ( deletionCount % 1000 == 0 )
				{
					vprint( 0, "...deleted %i files\n", deletionCount );
				}
			}
			else
			{
				logprint( "deletions.bat", "del \"%s\" /f\n",  g_Analysis.symbols.String( entry.sym ) );
			}
		}

		vprint( 0, "\nFile deletion (%d files, %s)\n\n", deletionCount, Q_pretifymem(deletionSize, 2) );
	}

	double grand_total = 0;
	double grand_total_unref = 0;
	double grand_total_white = 0;

	char totalstring[ 20 ];
	char unrefstring[ 20 ];
	char refstring[ 20 ];
	char whiteliststring[ 20 ];

	vprint( 0, "---------------------------------------- Summary ----------------------------------------\n" );

	vprint( 0, "% 15s               % 15s               % 15s               % 15s %12s\n",
		"Referenced",
		"WhiteListed",
		"Unreferenced",
		"Total",
		"Directory" );

	// Now walk the dictionary in order
	i = directories.First();
	while ( i != invalidindex )
	{
		DirEntry & de = directories[ i ];

		double remainder = de.total - de.unreferenced;

		float percent_unref = 0.0f;
		float percent_white = 0.0f;
		if ( de.total > 0 )
		{
			percent_unref = 100.0f * (float)de.unreferenced / (float)de.total;
			percent_white = 100.0f * (float)de.whitelist / (float)de.total;
		}

		Q_strncpy( totalstring, Q_pretifymem( de.total, 2 ), sizeof( totalstring ) );
		Q_strncpy( unrefstring, Q_pretifymem( de.unreferenced, 2 ), sizeof( unrefstring ) );
		Q_strncpy( refstring, Q_pretifymem( remainder, 2 ), sizeof( refstring ) );
		Q_strncpy( whiteliststring, Q_pretifymem( de.whitelist, 2 ), sizeof( whiteliststring ) );

		vprint( 0, "%15s (%8.3f%%)   %15s (%8.3f%%)   %15s (%8.3f%%)   %15s => dir: %s\n",
			refstring, 100.0f - percent_unref, whiteliststring, percent_white, unrefstring, percent_unref, totalstring, directories.GetElementName( i ) );

		grand_total += de.total;
		grand_total_unref += de.unreferenced;
		grand_total_white += de.whitelist;

		i = directories.Next( i );
	}

	Q_strncpy( totalstring, Q_pretifymem( grand_total, 2 ), sizeof( totalstring ) );
	Q_strncpy( unrefstring, Q_pretifymem( grand_total_unref, 2 ), sizeof( unrefstring ) );
	Q_strncpy( refstring, Q_pretifymem( grand_total - grand_total_unref, 2 ), sizeof( refstring ) );
	Q_strncpy( whiteliststring, Q_pretifymem( grand_total_white, 2 ), sizeof( whiteliststring ) );

	double percent_unref = 100.0 * grand_total_unref / grand_total;
	double percent_white = 100.0 * grand_total_white / grand_total;

	vprint( 0, "-----------------------------------------------------------------------------------------\n" );
	vprint( 0, "%15s (%8.3f%%)   %15s (%8.3f%%)   %15s (%8.3f%%)   %15s\n",
		refstring, 100.0f - percent_unref, whiteliststring, percent_white, unrefstring, percent_unref, totalstring );
}
void CTextureManager::SetExcludedTextures( const char *pScriptName )
{
	// clear all exisiting texture's exclusion
	for ( int i = m_TextureExcludes.First(); i != m_TextureExcludes.InvalidIndex(); i = m_TextureExcludes.Next( i ) )
	{
		ITextureInternal *pTexture = FindTexture( m_TextureExcludes.GetElementName( i ) );
		if ( pTexture )
		{
			pTexture->MarkAsExcluded( false, 0 );
		}
	}
	m_TextureExcludes.RemoveAll();

	MEM_ALLOC_CREDIT();

	// get optional script
	CUtlBuffer excludeBuffer( 0, 0, CUtlBuffer::TEXT_BUFFER );
	if ( g_pFullFileSystem->ReadFile( pScriptName, NULL, excludeBuffer ) )
	{
		char szToken[MAX_PATH];
		while ( 1 )
		{
			// must support spaces in names without quotes
			// have to brute force parse up to a valid line
			while ( 1 )
			{
				excludeBuffer.EatWhiteSpace();
				if ( !excludeBuffer.EatCPPComment() )
				{
					// not a comment
					break;
				}
			}
			excludeBuffer.GetLine( szToken, sizeof( szToken ) );
			int tokenLength = strlen( szToken );
			if ( !tokenLength )
			{
				// end of list
				break;
			}

			// remove all trailing whitespace
			while ( tokenLength > 0 )
			{
				tokenLength--;
				if ( isgraph( szToken[tokenLength] ) )
				{
					break;
				}
				szToken[tokenLength] = '\0';
			}

			// first optional token may be a dimension limit hint
			int nDimensionsLimit = 0;
			char *pTextureName = szToken;
			if ( pTextureName[0] != 0 && isdigit( pTextureName[0] ) )
			{
				nDimensionsLimit = atoi( pTextureName );
				
				// skip forward to name
				for ( ;; )
				{
					char ch = *pTextureName;
					if ( !ch || ( !isdigit( ch ) && !isspace( ch ) ) )
					{
						break;
					}
					pTextureName++;
				}
			}

			char szCleanName[MAX_PATH];
			NormalizeTextureName( pTextureName, szCleanName, sizeof( szCleanName ) );

			if ( m_TextureExcludes.Find( szCleanName ) != m_TextureExcludes.InvalidIndex() )
			{
				// avoid duplicates
				continue;
			}

			m_TextureExcludes.Insert( szCleanName, nDimensionsLimit );

			// set any existing texture's exclusion
			// textures that don't exist yet will get caught during their creation path
			ITextureInternal *pTexture = FindTexture( szCleanName );
			if ( pTexture )
			{
				pTexture->MarkAsExcluded( ( nDimensionsLimit == 0 ), nDimensionsLimit );
			}
		}
	}
}