//-----------------------------------------------------------------------------
// Purpose: Sorts the group.
//-----------------------------------------------------------------------------
void CTextureGroup::Sort(void)
{
	m_Textures.Sort(SortTexturesProc);

	// Redo the name map.
	m_TextureNameMap.RemoveAll();
	for ( int i=0; i < m_Textures.Count(); i++ )
	{
		IEditorTexture *pTex = m_Textures[i];
		m_TextureNameMap.Insert( pTex->GetName(), i );
	}

	// Changing the order means we don't know where we should be loading from
	m_nTextureToLoad = 0;
}
//-----------------------------------------------------------------------------
// Purpose: 
// Input  : pszName - 
//			piIndex - 
//			bDummy - 
// Output : 
//-----------------------------------------------------------------------------
IEditorTexture *CTextureSystem::FindActiveTexture(LPCSTR pszInputName, int *piIndex, BOOL bDummy)
{

	// The .vmf file format gets confused if there are backslashes in material names,
	// so make sure they're all using forward slashes here.
	char szName[MAX_PATH];
	Q_StrSubst( pszInputName, "\\", "/", szName, sizeof( szName ) );
	const char *pszName = szName;
	IEditorTexture *pTex = NULL;
	//
	// Check the cache first.
	//
	if (m_pLastTex && !stricmp(pszName, m_pLastTex->GetName()))
	{
		if (piIndex)
		{
			*piIndex = m_nLastIndex;
		}

		return m_pLastTex;
	}

	int iIndex = 0;

	// We're finding by name, so we don't care what the format is as long as the name matches.
	if ( m_pActiveGroup )
	{
		pTex = m_pActiveGroup->FindTextureByName( pszName, &iIndex, tfNone );
		if ( pTex )
		{
			if ( piIndex )
				*piIndex = iIndex;
			
			m_pLastTex = pTex;
			m_nLastIndex = iIndex;
			
			return pTex;
		}
	}

	//
	// Let's try again, this time with \textures\ decoration
	// TODO: remove this?
	//
	{
		iIndex = 0;
		char szBuf[512];

		sprintf(szBuf, "textures\\%s", pszName);

		for (int i = strlen(szBuf) -1; i >= 0; i--)
		{
			if (szBuf[i] == '/')
				szBuf[i] = '\\';
		}

		strlwr(szBuf);

		if ( m_pActiveGroup )
		{
			pTex = m_pActiveGroup->FindTextureByName( szBuf, &iIndex, tfNone );
			if ( pTex )
			{
				if ( piIndex )
					*piIndex = iIndex;
				
				m_pLastTex = pTex;
				m_nLastIndex = iIndex;
				
				return pTex;
			}
		}
	}
	//
	// Caller doesn't want dummies.
	//
	if (!bDummy)
	{
		return(NULL);
	}

	Assert(!piIndex);

	//
	// Check the list of dummies for a texture with the same name and texture format.
	//
	if (m_pActiveContext)
	{
		int nDummyCount = m_pActiveContext->Dummies.Count();
		for (int nDummy = 0; nDummy < nDummyCount; nDummy++)
		{
			IEditorTexture *pTex = m_pActiveContext->Dummies.Element(nDummy);
			if (!strcmpi(pszName, pTex->GetName()))
			{
				m_pLastTex = pTex;
				m_nLastIndex = -1;
				return(pTex);
			}
		}

		//
		// Not found; add a dummy as a placeholder for the missing texture.
		//
		pTex = AddDummy(pszName, g_pGameConfig->GetTextureFormat());
	}

	if (pTex != NULL)
	{
		m_pLastTex = pTex;
		m_nLastIndex = -1;
	}

	return(pTex);
}