2002-01-16 10:01:32 +00:00
#include "stdafx.h"
/*
-----------------------------------------------------------------------------
2002-05-19 01:59:48 +00:00
Class: ThemeManager
2002-01-16 10:01:32 +00:00
2002-02-28 19:40:40 +00:00
Desc: See header.
2002-01-16 10:01:32 +00:00
2002-05-19 01:59:48 +00:00
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
2002-02-28 19:40:40 +00:00
Chris Danford
2002-01-16 10:01:32 +00:00
-----------------------------------------------------------------------------
*/
#include "ThemeManager.h"
2002-05-01 19:14:55 +00:00
#include "RageLog.h"
2002-07-23 01:41:40 +00:00
#include "PrefsManager.h"
2002-07-27 19:29:51 +00:00
#include "RageException.h"
2002-08-31 10:33:13 +00:00
#include "RageTimer.h"
2002-07-31 19:40:40 +00:00
#include "GameState.h"
#include "GameDef.h"
2002-08-13 23:26:46 +00:00
#include "IniFile.h"
2002-01-16 10:01:32 +00:00
ThemeManager * THEME = NULL ; // global object accessable from anywhere in the program
2002-07-23 01:41:40 +00:00
const CString BASE_THEME_NAME = "default" ;
const CString THEMES_DIR = "Themes \\ " ;
2002-05-19 01:59:48 +00:00
2002-03-19 07:09:49 +00:00
ThemeManager :: ThemeManager ()
{
2002-08-13 23:26:46 +00:00
m_pIniMetrics = new IniFile ;
2002-08-31 10:33:13 +00:00
/* Update the metric cache on the first call to GetMetric. */
2002-09-03 06:33:08 +00:00
m_uNextReloadTicks = - 1 ;
2002-08-13 23:26:46 +00:00
2002-07-23 01:41:40 +00:00
m_sCurThemeName = BASE_THEME_NAME ; // Use te base theme for now. It's up to PrefsManager to change this.
2002-03-19 07:09:49 +00:00
CStringArray arrayThemeNames ;
2002-07-31 19:40:40 +00:00
GetAllThemeNames ( arrayThemeNames );
2002-07-23 01:41:40 +00:00
2002-08-12 16:02:45 +00:00
// Disabled for now... it takes ages here to run - bbf
// for( int i=0; i<arrayThemeNames.GetSize(); i++ )
// AssertThemeIsComplete( arrayThemeNames[i] );
2002-03-19 07:09:49 +00:00
}
2002-08-13 23:26:46 +00:00
ThemeManager ::~ ThemeManager ()
{
delete m_pIniMetrics ;
}
2002-07-31 19:40:40 +00:00
void ThemeManager :: GetAllThemeNames ( CStringArray & AddTo )
2002-03-19 07:09:49 +00:00
{
2002-07-23 01:41:40 +00:00
GetDirListing ( THEMES_DIR + " \\ *" , AddTo , true );
2002-03-19 07:09:49 +00:00
// strip out the folder called "CVS"
2002-05-19 01:59:48 +00:00
for ( int i = AddTo . GetSize () - 1 ; i >= 0 ; i -- )
2002-08-27 16:53:25 +00:00
if ( 0 == stricmp ( AddTo [ i ], "cvs" ) )
2002-03-19 07:09:49 +00:00
AddTo . RemoveAt ( i );
2002-07-23 01:41:40 +00:00
}
2002-07-31 19:40:40 +00:00
void ThemeManager :: GetThemeNamesForCurGame ( CStringArray & AddTo )
{
GetAllThemeNames ( AddTo );
2002-08-27 16:53:25 +00:00
/*
2002-07-31 19:40:40 +00:00
// strip out announcers that don't have the current game name in them
CString sGameName = GAMESTATE->GetCurrentGameDef()->m_szName;
sGameName.MakeLower();
for( int i=AddTo.GetSize()-1; i>=0; i-- )
{
CString sLowercaseVer = AddTo[i];
sLowercaseVer.MakeLower();
if( sLowercaseVer.Find(sGameName)==-1 )
AddTo.RemoveAt(i);
}
2002-08-27 16:53:25 +00:00
*/
2002-07-31 19:40:40 +00:00
}
2002-07-23 01:41:40 +00:00
bool ThemeManager :: DoesThemeExist ( CString sThemeName )
{
CStringArray asThemeNames ;
2002-07-31 19:40:40 +00:00
GetAllThemeNames ( asThemeNames );
2002-07-23 01:41:40 +00:00
for ( int i = 0 ; i < asThemeNames . GetSize (); i ++ )
2002-07-27 19:29:51 +00:00
{
2002-07-23 01:41:40 +00:00
if ( 0 == stricmp ( sThemeName , asThemeNames [ i ]) )
return true ;
2002-07-27 19:29:51 +00:00
}
2002-07-23 01:41:40 +00:00
return false ;
}
2002-05-19 01:59:48 +00:00
void ThemeManager :: SwitchTheme ( CString sThemeName )
2002-03-19 07:09:49 +00:00
{
2002-07-31 19:40:40 +00:00
if ( 0 == stricmp ( BASE_THEME_NAME , sThemeName ) )
m_sCurThemeName = "" ; // can't select the base theme
2002-07-27 19:29:51 +00:00
else if ( ! DoesThemeExist ( sThemeName ) )
2002-07-23 01:41:40 +00:00
m_sCurThemeName = BASE_THEME_NAME ;
else
m_sCurThemeName = sThemeName ;
2002-08-13 23:26:46 +00:00
// update hashes for metrics files
m_iHashForCurThemeMetrics = GetHashForFile ( GetMetricsPathFromName ( m_sCurThemeName ) );
m_iHashForBaseThemeMetrics = GetHashForFile ( GetMetricsPathFromName ( BASE_THEME_NAME ) );
// read new metrics. First read base metrics, then read cur theme's metrics, overriding base theme
m_pIniMetrics -> Reset ();
m_pIniMetrics -> SetPath ( GetMetricsPathFromName ( BASE_THEME_NAME ) );
m_pIniMetrics -> ReadFile ();
m_pIniMetrics -> SetPath ( GetMetricsPathFromName ( m_sCurThemeName ) );
m_pIniMetrics -> ReadFile ();
2002-03-19 07:09:49 +00:00
}
2002-05-19 01:59:48 +00:00
CString ThemeManager :: GetThemeDirFromName ( CString sThemeName )
2002-03-19 07:09:49 +00:00
{
2002-07-23 01:41:40 +00:00
return THEMES_DIR + sThemeName + " \\ " ;
2002-02-09 04:30:27 +00:00
}
2002-08-13 23:26:46 +00:00
CString ThemeManager :: GetPathTo ( CString sAssetCategory , CString sFileName )
{
2002-08-26 19:30:58 +00:00
#ifdef _DEBUG
2002-08-22 09:31:32 +00:00
try_element_again :
2002-08-26 19:30:58 +00:00
#endif
2002-08-22 09:31:32 +00:00
2002-08-13 23:26:46 +00:00
sAssetCategory . MakeLower ();
sFileName . MakeLower ();
const CString sCurrentThemeDir = GetThemeDirFromName ( m_sCurThemeName );
const CString sDefaultThemeDir = GetThemeDirFromName ( BASE_THEME_NAME );
CStringArray asPossibleElementFilePaths ;
// look for a redirect
GetDirListing ( sCurrentThemeDir + sAssetCategory + " \\ " + sFileName + "*.redir" , asPossibleElementFilePaths , false , true );
if ( asPossibleElementFilePaths . GetSize () > 0 )
{
CStdioFile file ;
file . Open ( asPossibleElementFilePaths [ 0 ], CFile :: modeRead );
CString sLine ;
file . ReadString ( sLine );
}
///////////////////////////////////////
// Search both the current theme and the default theme dirs for this element
///////////////////////////////////////
2002-08-31 19:13:45 +00:00
static const char * graphic_masks [] = {
"*.sprite" , "*.png" , "*.jpg" , "*.bmp" , "*.gif" , "*.redir" ,
"*.avi" , "*.mpg" , "*.mpeg" , NULL
};
static const char * sound_masks [] = { ".set" , ".mp3" , ".ogg" , ".wav" , ".redir" , NULL };
2002-09-03 22:31:06 +00:00
static const char * font_masks [] = { " 16x16.png" , ".redir" , NULL };
2002-08-31 19:13:45 +00:00
const char ** asset_masks = NULL ;
if ( sAssetCategory == "graphics" ) asset_masks = graphic_masks ;
else if ( sAssetCategory == "sounds" ) asset_masks = sound_masks ;
else if ( sAssetCategory == "fonts" ) asset_masks = font_masks ;
else ASSERT ( 0 ); // Unknown theme asset category
int i ;
for ( i = 0 ; asset_masks [ i ]; ++ i )
GetDirListing ( sCurrentThemeDir + sAssetCategory + " \\ " + sFileName + asset_masks [ i ],
asPossibleElementFilePaths , false , true );
for ( i = 0 ; asset_masks [ i ]; ++ i )
GetDirListing ( sDefaultThemeDir + sAssetCategory + " \\ " + sFileName + asset_masks [ i ],
asPossibleElementFilePaths , false , true );
2002-08-13 23:26:46 +00:00
if ( asPossibleElementFilePaths . GetSize () == 0 )
{
2002-08-22 09:31:32 +00:00
#ifdef _DEBUG
if ( IDRETRY == AfxMessageBox ( ssprintf ( "The theme element %s/%s is missing. Correct this and click Retry, or Cancel to break." , sAssetCategory , sFileName ), MB_RETRYCANCEL ) )
goto try_element_again ;
#endif
throw RageException ( "Theme element '%s/%s' could not be found in '%s' or '%s'." ,
sAssetCategory ,
sFileName ,
2002-08-13 23:26:46 +00:00
GetThemeDirFromName ( m_sCurThemeName ),
GetThemeDirFromName ( BASE_THEME_NAME ) );
}
else
{
asPossibleElementFilePaths [ 0 ]. MakeLower ();
if ( asPossibleElementFilePaths [ 0 ]. GetLength () > 5 && asPossibleElementFilePaths [ 0 ]. Right ( 5 ) == "redir" ) // this is a redirect file
{
CString sRedirFilePath = asPossibleElementFilePaths [ 0 ];
CString sDir , sFName , sExt ;
splitrelpath ( sRedirFilePath , sDir , sFName , sExt );
CStdioFile file ;
file . Open ( sRedirFilePath , CFile :: modeRead );
CString sNewFileName ;
file . ReadString ( sNewFileName );
2002-09-05 18:49:23 +00:00
// CString sNewFilePath = sDir+"\\"+sNewFileName; // This is what it used to be, FONTS were getting extra slashes
// at the start of their file names, so I took out this extra slash - Andy.
CString sNewFilePath = sDir + sNewFileName ;
2002-08-13 23:26:46 +00:00
if ( sNewFileName == "" || ! DoesFileExist ( sNewFilePath ) )
{
throw RageException ( "The redirect '%s' points to the file '%s', which does not exist. Verify that this redirect is correct." , sRedirFilePath , sNewFilePath );
}
else
return sNewFilePath ;
}
else
{
return asPossibleElementFilePaths [ 0 ];
}
}
2002-05-19 06:13:16 +00:00
return "" ;
2002-01-16 10:01:32 +00:00
}
2002-05-19 01:59:48 +00:00
2002-08-13 23:26:46 +00:00
CString ThemeManager :: GetMetricsPathFromName ( CString sThemeName )
{
return GetThemeDirFromName ( sThemeName ) + "metrics.ini" ;
}
CString ThemeManager :: GetMetric ( CString sScreenName , CString sValueName )
{
2002-08-26 19:30:58 +00:00
#ifdef _DEBUG
2002-08-22 09:31:32 +00:00
try_metric_again :
2002-08-26 19:30:58 +00:00
#endif
2002-08-13 23:26:46 +00:00
CString sCurMetricPath = GetMetricsPathFromName ( m_sCurThemeName );
CString sDefaultMetricPath = GetMetricsPathFromName ( BASE_THEME_NAME );
2002-08-31 10:33:13 +00:00
// Is our metric cache out of date?
2002-09-03 06:33:08 +00:00
if ( m_uNextReloadTicks == - 1 || :: GetTickCount () > m_uNextReloadTicks )
2002-08-13 23:26:46 +00:00
{
2002-09-03 06:33:08 +00:00
m_uNextReloadTicks = GetTickCount () + 1000 ;
2002-08-31 10:33:13 +00:00
if ( m_iHashForCurThemeMetrics != GetHashForFile ( sCurMetricPath ) ||
m_iHashForBaseThemeMetrics != GetHashForFile ( sDefaultMetricPath ) )
{
SwitchTheme ( m_sCurThemeName ); // force a reload of the metrics cache
}
2002-08-13 23:26:46 +00:00
}
CString sValue ;
if ( m_pIniMetrics -> GetValue ( sScreenName , sValueName , sValue ) )
2002-09-03 22:31:06 +00:00
{
sValue . Replace ( "::" , " \n " ); // "::" means newline since you can't use line breaks in an ini file.
2002-08-13 23:26:46 +00:00
return sValue ;
2002-09-03 22:31:06 +00:00
}
2002-08-13 23:26:46 +00:00
2002-08-22 09:31:32 +00:00
#ifdef _DEBUG
if ( IDRETRY == AfxMessageBox ( ssprintf ( "The theme metric %s-%s is missing. Correct this and click Retry, or Cancel to break." , sScreenName , sValueName ), MB_RETRYCANCEL ) )
goto try_metric_again ;
#endif
2002-08-13 23:26:46 +00:00
throw RageException ( "Theme metric '%s : %s' could not be found in '%s' or '%s'." ,
sScreenName ,
sValueName ,
sCurMetricPath ,
sDefaultMetricPath
);
}
int ThemeManager :: GetMetricI ( CString sScreenName , CString sValueName )
{
return atoi ( GetMetric ( sScreenName , sValueName ) );
}
float ThemeManager :: GetMetricF ( CString sScreenName , CString sValueName )
{
return ( float ) atof ( GetMetric ( sScreenName , sValueName ) );
}
bool ThemeManager :: GetMetricB ( CString sScreenName , CString sValueName )
{
return atoi ( GetMetric ( sScreenName , sValueName ) ) != 0 ;
}
D3DXCOLOR ThemeManager :: GetMetricC ( CString sScreenName , CString sValueName )
{
float r = 1 , b = 1 , g = 1 , a = 1 ; // initialize in case sscanf fails
CString sValue = GetMetric ( sScreenName , sValueName );
char szValue [ 40 ];
strcpy ( szValue , sValue );
int result = sscanf ( szValue , "%f,%f,%f,%f" , & r , & g , & b , & a );
if ( result != 4 )
{
LOG -> Warn ( "The color value '%s' for theme metric '%s : %s' is invalid." , szValue , sScreenName , sValueName );
ASSERT ( 0 );
}
return D3DXCOLOR ( r , g , b , a );
}