2015-11-22 04:24:51 +00:00
# include "CVarManager.hpp"
# include "CVar.hpp"
2015-11-22 06:51:25 +00:00
# include <Athena/FileWriter.hpp>
2015-11-22 04:24:51 +00:00
# include <Athena/Utility.hpp>
2015-11-22 06:51:25 +00:00
# include <HECL/Runtime.hpp>
2015-11-22 04:24:51 +00:00
# include <memory>
namespace Retro
{
2015-11-22 06:51:25 +00:00
CVar * com_developer = nullptr ;
CVar * com_configfile = nullptr ;
CVar * com_enableCheats = nullptr ;
CVar * r_clearColor = nullptr ;
LogVisor : : LogModule CVarLog ( " CVarManager " ) ;
CVarManager : : CVarManager ( HECL : : Runtime : : FileStoreManager & store , bool useBinary )
: m_store ( store ) ,
m_useBinary ( useBinary )
2015-11-22 04:24:51 +00:00
{
2015-11-22 06:51:25 +00:00
com_configfile = newCVar ( " config " , " File to store configuration " , std : : string ( " config " ) , CVar : : EFlags : : System ) ;
com_developer = newCVar ( " developer " , " Enables developer mode " , false , ( CVar : : EFlags : : System | CVar : : EFlags : : Cheat | CVar : : EFlags : : ReadOnly ) ) ;
2015-11-22 07:12:38 +00:00
com_enableCheats = newCVar ( " iamaweiner " , " Enable cheats " , false , ( CVar : : EFlags : : System | CVar : : EFlags : : ReadOnly | CVar : : EFlags : : Hidden ) ) ;
2015-11-22 06:51:25 +00:00
r_clearColor = newCVar ( " r_clearcolor " , " Sets the clear color for the frame buffer " , Zeus : : CColor { Zeus : : Comp8 ( 255 ) , 255 , 255 } , ( CVar : : EFlags : : System | CVar : : EFlags : : Archive ) ) ;
2015-11-22 04:24:51 +00:00
}
2015-11-22 06:51:25 +00:00
CVarManager : : ~ CVarManager ( )
2015-11-22 04:24:51 +00:00
{
}
2015-11-22 07:12:38 +00:00
void CVarManager : : update ( )
{
2015-11-22 08:01:51 +00:00
for ( const std : : pair < std : : string , CVar * > & pair : m_cvars )
2015-11-22 07:12:38 +00:00
if ( pair . second - > isModified ( ) )
{
pair . second - > dispatch ( ) ;
pair . second - > clearModified ( ) ;
}
}
2015-11-22 06:51:25 +00:00
bool CVarManager : : registerCVar ( CVar * cvar )
2015-11-22 04:24:51 +00:00
{
std : : string tmp = cvar - > name ( ) ;
Athena : : utility : : tolower ( tmp ) ;
if ( m_cvars . find ( tmp ) ! = m_cvars . end ( ) )
return false ;
m_cvars [ tmp ] = cvar ;
return true ;
}
2015-11-22 08:01:51 +00:00
CVar * CVarManager : : findCVar ( std : : string name )
2015-11-22 04:24:51 +00:00
{
2015-11-22 08:01:51 +00:00
Athena : : utility : : tolower ( name ) ;
if ( m_cvars . find ( name ) = = m_cvars . end ( ) )
2015-11-22 04:24:51 +00:00
return nullptr ;
2015-11-22 08:01:51 +00:00
return m_cvars [ name ] ;
2015-11-22 04:24:51 +00:00
}
2015-11-22 06:51:25 +00:00
std : : vector < CVar * > CVarManager : : archivedCVars ( ) const
2015-11-22 04:24:51 +00:00
{
2015-11-22 06:51:25 +00:00
std : : vector < CVar * > ret ;
2015-11-22 08:01:51 +00:00
for ( const std : : pair < std : : string , CVar * > & pair : m_cvars )
2015-11-22 06:51:25 +00:00
if ( pair . second - > isArchive ( ) )
2015-11-22 04:24:51 +00:00
ret . push_back ( pair . second ) ;
return ret ;
}
2015-11-22 06:51:25 +00:00
std : : vector < CVar * > CVarManager : : cvars ( ) const
2015-11-22 04:24:51 +00:00
{
2015-11-22 06:51:25 +00:00
std : : vector < CVar * > ret ;
2015-11-22 08:01:51 +00:00
for ( const std : : pair < std : : string , CVar * > & pair : m_cvars )
2015-11-22 04:24:51 +00:00
ret . push_back ( pair . second ) ;
return ret ;
}
2015-11-22 06:51:25 +00:00
void CVarManager : : deserialize ( CVar * cvar )
{
if ( ! cvar | | ! cvar - > isArchive ( ) )
return ;
CVarContainer container ;
# if _WIN32
HECL : : SystemString filename = m_store . getStoreRoot ( ) + _S ( ' / ' ) + com_configfile - > toWideLiteral ( ) ;
# else
HECL : : SystemString filename = m_store . getStoreRoot ( ) + _S ( ' / ' ) + com_configfile - > toLiteral ( ) ;
# endif
HECL : : Sstat st ;
if ( m_useBinary )
{
filename + = _S ( " .bin " ) ;
2015-11-22 08:01:51 +00:00
if ( HECL : : Stat ( filename . c_str ( ) , & st ) | | ! S_ISREG ( st . st_mode ) )
return ;
2015-11-22 06:51:25 +00:00
Athena : : io : : FileReader reader ( filename ) ;
if ( reader . isOpen ( ) )
container . read ( reader ) ;
}
else
{
filename + = _S ( " .yaml " ) ;
2015-11-22 08:01:51 +00:00
if ( HECL : : Stat ( filename . c_str ( ) , & st ) | | ! S_ISREG ( st . st_mode ) )
return ;
2015-11-22 06:51:25 +00:00
FILE * f = HECL : : Fopen ( filename . c_str ( ) , _S ( " rb " ) ) ;
if ( f )
container . fromYAMLFile ( f ) ;
fclose ( f ) ;
}
if ( container . cvars . size ( ) > 0 )
{
auto serialized = std : : find_if ( container . cvars . begin ( ) , container . cvars . end ( ) , [ & cvar ] ( const DNACVAR : : CVar & c ) - > bool
{ return c . m_name = = cvar - > name ( ) ; } ) ;
if ( serialized ! = container . cvars . end ( ) )
{
2015-11-22 08:01:51 +00:00
DNACVAR : : CVar & tmp = * serialized ;
2015-11-22 06:51:25 +00:00
if ( tmp . m_type ! = cvar - > type ( ) )
{
CVarLog . report ( LogVisor : : Error , _S ( " Stored type for %s does not match actual type! " ) , tmp . m_name . c_str ( ) ) ;
return ;
}
cvar - > m_value = tmp . m_value ;
}
}
}
void CVarManager : : serialize ( )
{
CVarContainer container ;
for ( const std : : pair < std : : string , CVar * > & pair : m_cvars )
if ( pair . second - > isArchive ( ) )
{
CVar tmp = * pair . second ;
container . cvars . push_back ( tmp ) ;
}
container . cvarCount = container . cvars . size ( ) ;
# if _WIN32
HECL : : SystemString filename = m_store . getStoreRoot ( ) + _S ( ' / ' ) + com_configfile - > toWideLiteral ( ) ;
# else
HECL : : SystemString filename = m_store . getStoreRoot ( ) + _S ( ' / ' ) + com_configfile - > toLiteral ( ) ;
# endif
if ( m_useBinary )
{
filename + = _S ( " .bin " ) ;
Athena : : io : : FileWriter writer ( filename ) ;
if ( writer . isOpen ( ) )
container . write ( writer ) ;
}
else
{
filename + = _S ( " .yaml " ) ;
FILE * f = HECL : : Fopen ( filename . c_str ( ) , _S ( " wb " ) ) ;
if ( f )
container . toYAMLFile ( f ) ;
fclose ( f ) ;
}
}
2015-11-22 04:24:51 +00:00
bool CVarManager : : suppressDeveloper ( )
{
2015-11-22 06:51:25 +00:00
bool oldDeveloper = com_developer - > toBoolean ( ) ;
2015-11-22 04:24:51 +00:00
CVarUnlocker unlock ( com_developer ) ;
2015-11-22 06:51:25 +00:00
com_developer - > fromBoolean ( false ) ;
2015-11-22 04:24:51 +00:00
2015-11-22 06:51:25 +00:00
return oldDeveloper ;
2015-11-22 04:24:51 +00:00
}
void CVarManager : : restoreDeveloper ( bool oldDeveloper )
{
2015-11-22 06:51:25 +00:00
CVarUnlocker unlock ( com_developer ) ;
com_developer - > fromBoolean ( oldDeveloper ) ;
2015-11-22 04:24:51 +00:00
}
}