2010-10-22 03:34:26 +00:00
/*
Object Viewer Qt
Copyright ( C ) 2010 Dzmitry Kamiahin < dnk - 88 @ tut . by >
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# include "stdpch.h"
# include "configuration.h"
// NeL includes
# include <nel/misc/debug.h>
# include <nel/misc/hierarchical_timer.h>
# include <nel/misc/config_file.h>
# include <nel/misc/path.h>
# include "modules.h"
using namespace std ;
using namespace NLMISC ;
2010-11-26 11:54:06 +00:00
namespace NLQT
{
2010-10-22 03:34:26 +00:00
CConfiguration : : CConfiguration ( )
{
2010-11-26 11:54:06 +00:00
2010-10-22 03:34:26 +00:00
}
CConfiguration : : ~ CConfiguration ( )
{
2010-11-26 11:54:06 +00:00
2010-10-22 03:34:26 +00:00
}
void CConfiguration : : init ( )
2010-11-26 11:54:06 +00:00
{
2010-10-22 03:34:26 +00:00
nldebug ( " CConfiguration::init " ) ;
2010-11-26 11:54:06 +00:00
2010-10-22 03:34:26 +00:00
// verify data
nlassert ( ! ConfigCallbacks . size ( ) ) ;
2010-11-26 11:54:06 +00:00
2010-10-22 03:34:26 +00:00
// load config
try
{
ConfigFile . load ( NLQT_CONFIG_FILE ) ;
}
catch ( . . . )
{
nlerror ( " Error opening files object_viewer.cfg or object_viewer_default.cfg. Check this files " ) ;
}
// setup config file callback
Modules : : config ( ) . setCallback ( " SearchPaths " , CConfigCallback ( this , & CConfiguration : : cfcbSearchPaths ) ) ;
}
void CConfiguration : : release ( )
{
nldebug ( " CConfiguration::release " ) ;
2010-11-26 11:54:06 +00:00
2010-10-22 03:34:26 +00:00
Modules : : config ( ) . dropCallback ( " SearchPaths " ) ;
2010-11-26 11:54:06 +00:00
// save and release the config file
if ( ConfigFile . exists ( " SaveConfig " ) & & ConfigFile . getVarPtr ( " SaveConfig " ) - > asBool ( ) )
2010-10-22 03:34:26 +00:00
{
ConfigFile . save ( ) ;
}
ConfigFile . clear ( ) ;
2010-11-26 11:54:06 +00:00
2010-10-22 03:34:26 +00:00
// release the search paths etc
CPath : : releaseInstance ( ) ;
2010-11-26 11:54:06 +00:00
2010-10-22 03:34:26 +00:00
// verify data
nlassert ( ! ConfigCallbacks . size ( ) ) ;
}
2010-11-26 11:54:06 +00:00
2010-10-22 03:34:26 +00:00
void CConfiguration : : updateUtilities ( )
{
//H_AUTO2
CConfigFile : : checkConfigFiles ( ) ;
}
void CConfiguration : : configSearchPaths ( )
{
cfcbSearchPaths ( Modules : : config ( ) . getConfigFile ( ) . getVar ( " SearchPaths " ) ) ;
}
void CConfiguration : : configRemapExtensions ( )
{
CConfigFile : : CVar * var ;
var = ConfigFile . getVarPtr ( " RemapExtensions " ) ;
uint varsize = var - > size ( ) ;
for ( uint i = 0 ; i < varsize ; i + = 2 )
2010-11-26 11:54:06 +00:00
CPath : : remapExtension ( var - > asString ( i ) , var - > asString ( i + 1 ) , true ) ;
2010-10-22 03:34:26 +00:00
}
void CConfiguration : : setAndCallback ( const std : : string & varName , CConfigCallback configCallback )
{
ConfigCallbacks [ varName ] = configCallback ;
ConfigFile . setCallback ( varName , cbConfigCallback ) ;
configCallback ( * ConfigFile . getVarPtr ( varName ) ) ;
}
void CConfiguration : : setCallback ( const std : : string & varName , CConfigCallback configCallback )
{
ConfigCallbacks [ varName ] = configCallback ;
ConfigFile . setCallback ( varName , cbConfigCallback ) ;
}
void CConfiguration : : dropCallback ( const std : : string & varName )
{
ConfigFile . setCallback ( varName , NULL ) ;
ConfigCallbacks . erase ( varName ) ;
}
float CConfiguration : : getValue ( const string & varName , float defaultValue )
{
if ( ConfigFile . exists ( varName ) ) return ConfigFile . getVar ( varName ) . asFloat ( ) ;
CConfigFile : : CVar varToCopy ;
2010-11-26 11:54:06 +00:00
varToCopy . forceAsDouble ( ( double ) defaultValue ) ;
2010-10-22 03:34:26 +00:00
ConfigFile . insertVar ( varName , varToCopy ) ;
return defaultValue ;
}
double CConfiguration : : getValue ( const string & varName , double defaultValue )
{
if ( ConfigFile . exists ( varName ) ) return ConfigFile . getVar ( varName ) . asDouble ( ) ;
CConfigFile : : CVar varToCopy ;
2010-11-26 11:54:06 +00:00
varToCopy . forceAsDouble ( defaultValue ) ;
2010-10-22 03:34:26 +00:00
ConfigFile . insertVar ( varName , varToCopy ) ;
return defaultValue ;
}
int CConfiguration : : getValue ( const string & varName , int defaultValue )
{
if ( ConfigFile . exists ( varName ) ) return ConfigFile . getVar ( varName ) . asInt ( ) ;
CConfigFile : : CVar varToCopy ;
2010-11-26 11:54:06 +00:00
varToCopy . forceAsInt ( defaultValue ) ;
2010-10-22 03:34:26 +00:00
ConfigFile . insertVar ( varName , varToCopy ) ;
return defaultValue ;
}
string CConfiguration : : getValue ( const string & varName , const string & defaultValue )
{
if ( ConfigFile . exists ( varName ) ) return ConfigFile . getVar ( varName ) . asString ( ) ;
CConfigFile : : CVar varToCopy ;
varToCopy . forceAsString ( defaultValue ) ;
ConfigFile . insertVar ( varName , varToCopy ) ;
return defaultValue ;
}
ucstring CConfiguration : : getValue ( const string & varName , const ucstring & defaultValue )
{
if ( ConfigFile . exists ( varName ) ) return ucstring : : makeFromUtf8 ( ConfigFile . getVar ( varName ) . asString ( ) ) ;
CConfigFile : : CVar varToCopy ;
varToCopy . forceAsString ( defaultValue . toUtf8 ( ) ) ;
ConfigFile . insertVar ( varName , varToCopy ) ;
return defaultValue ;
}
bool CConfiguration : : getValue ( const string & varName , bool defaultValue )
{
if ( ConfigFile . exists ( varName ) ) return ConfigFile . getVar ( varName ) . asBool ( ) ;
CConfigFile : : CVar varToCopy ;
2010-11-26 11:54:06 +00:00
varToCopy . forceAsInt ( defaultValue ? 1 : 0 ) ;
2010-10-22 03:34:26 +00:00
ConfigFile . insertVar ( varName , varToCopy ) ;
return defaultValue ;
}
CRGBA CConfiguration : : getValue ( const string & varName , const CRGBA & defaultValue )
{
2010-11-26 11:54:06 +00:00
if ( ConfigFile . exists ( varName ) )
2010-10-22 03:34:26 +00:00
{
return getValue ( ConfigFile . getVar ( varName ) , defaultValue ) ;
}
else
{
// create a new value only if one doesn't exist
CConfigFile : : CVar varToCopy ;
varToCopy . forceAsInt ( defaultValue . R ) ;
varToCopy . setAsInt ( defaultValue . G , 1 ) ;
varToCopy . setAsInt ( defaultValue . B , 2 ) ;
varToCopy . setAsInt ( defaultValue . A , 3 ) ;
ConfigFile . insertVar ( varName , varToCopy ) ;
}
return defaultValue ;
}
CRGBA CConfiguration : : getValue ( const CConfigFile : : CVar & var , const CRGBA & defaultValue )
{
if ( var . size ( ) > = 3 )
{
if ( var . size ( ) > 4 ) nlwarning ( " RGBA value in config value '%s' is too long, ignoring unused values " ) ;
return CRGBA ( ( uint8 ) var . asInt ( 0 ) , ( uint8 ) var . asInt ( 1 ) , ( uint8 ) var . asInt ( 2 ) , var . size ( ) > = 4 ? ( uint8 ) var . asInt ( 3 ) : 255 ) ;
}
2010-11-26 11:54:06 +00:00
nlwarning ( " Invalid RGBA value in config value '%s', reverting to default { %i, %i, %i, %i } " , var . Name . c_str ( ) , ( sint ) defaultValue . R , ( sint ) defaultValue . G , ( sint ) defaultValue . B , ( sint ) defaultValue . A ) ;
2010-10-22 03:34:26 +00:00
return defaultValue ;
}
void CConfiguration : : cbConfigCallback ( NLMISC : : CConfigFile : : CVar & var )
{
Modules : : config ( ) . ConfigCallbacks [ var . Name ] ( var ) ;
}
void CConfiguration : : cfcbSearchPaths ( NLMISC : : CConfigFile : : CVar & var )
{
uint varsize = var . size ( ) ;
for ( uint i = 0 ; i < varsize ; + + i )
2010-11-26 11:54:06 +00:00
CPath : : addSearchPath ( var . asString ( i ) , true , false ) ;
2010-10-22 03:34:26 +00:00
}
} /* namespace NLQT */