2010-03-13 22:10:45 +01:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2010-06-05 09:52:14 +02:00
// Copyright (C) 2001-2008 Martio Figueroa
2010-03-13 22:10:45 +01:00
//
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version
// ==============================================================
# include "config.h"
# include "util.h"
2010-03-16 22:44:22 +01:00
# include "game_constants.h"
2010-03-20 11:52:55 +01:00
# include "platform_util.h"
2010-03-22 00:40:35 +01:00
# include "game_util.h"
2010-06-12 20:27:39 +02:00
# include <map>
# include "conversion.h"
# include "window.h"
# include <stdexcept>
2010-07-03 09:02:31 +02:00
# include <fstream>
2010-03-22 00:40:35 +01:00
# include "leak_dumper.h"
2010-03-20 11:52:55 +01:00
using namespace Shared : : Platform ;
2010-03-22 00:40:35 +01:00
using namespace Shared : : Util ;
2010-06-12 20:27:39 +02:00
using namespace std ;
2010-03-13 22:10:45 +01:00
namespace Glest { namespace Game {
2010-12-11 09:00:49 +01:00
int GameConstants : : networkFramePeriod = 20 ;
int GameConstants : : updateFps = 40 ;
int GameConstants : : cameraFps = 100 ;
const float GameConstants : : normalMultiplier = 1.0f ;
2011-01-02 13:18:14 +01:00
const float GameConstants : : easyMultiplier = 0.5f ;
const float GameConstants : : ultraMultiplier = 2.0f ;
const float GameConstants : : megaMultiplier = 3.0f ;
2010-03-16 22:44:22 +01:00
const char * GameConstants : : folder_path_maps = " maps " ;
const char * GameConstants : : folder_path_scenarios = " scenarios " ;
const char * GameConstants : : folder_path_techs = " techs " ;
const char * GameConstants : : folder_path_tilesets = " tilesets " ;
const char * GameConstants : : folder_path_tutorials = " tutorials " ;
2010-06-29 08:50:35 +02:00
const char * GameConstants : : NETWORK_SLOT_UNCONNECTED_SLOTNAME = " ??? " ;
2010-03-16 22:44:22 +01:00
2010-08-02 05:15:11 +02:00
const char * GameConstants : : folder_path_screenshots = " screens/ " ;
2010-09-04 02:32:56 +02:00
const char * GameConstants : : OBSERVER_SLOTNAME = " *Observer* " ;
2010-09-11 10:09:33 +02:00
const char * GameConstants : : RANDOMFACTION_SLOTNAME = " *Random* " ;
2010-09-04 02:32:56 +02:00
2011-01-26 10:09:59 +01:00
const char * GameConstants : : playerTextureCacheLookupKey = " playerTextureCache " ;
const char * GameConstants : : factionPreviewTextureCacheLookupKey = " factionPreviewTextureCache " ;
2010-11-05 07:46:30 +01:00
const char * GameConstants : : application_name = " MegaGlest " ;
2010-10-22 09:28:55 +02:00
2010-12-09 21:41:11 +01:00
const char * GameConstants : : pathCacheLookupKey = " pathCache_ " ;
const char * GameConstants : : path_data_CacheLookupKey = " data " ;
const char * GameConstants : : path_ini_CacheLookupKey = " ini " ;
const char * GameConstants : : path_logs_CacheLookupKey = " logs " ;
const char * Config : : glest_ini_filename = " glest.ini " ;
const char * Config : : glestuser_ini_filename = " glestuser.ini " ;
2010-03-13 22:10:45 +01:00
// =====================================================
// class Config
// =====================================================
2010-06-12 20:27:39 +02:00
const string defaultNotFoundValue = " ~~NOT FOUND~~ " ;
2010-03-22 00:40:35 +01:00
2010-06-12 20:27:39 +02:00
map < ConfigType , Config > Config : : configList ;
2010-09-09 23:07:39 +02:00
Config : : Config ( ) {
fileLoaded . first = false ;
fileLoaded . second = false ;
cfgType . first = cfgMainGame ;
cfgType . second = cfgUserGame ;
fileName . first = " " ;
fileName . second = " " ;
2011-03-04 23:54:04 +01:00
fileNameParameter . first = " " ;
fileNameParameter . second = " " ;
2010-09-09 23:07:39 +02:00
fileLoaded . first = false ;
fileLoaded . second = false ;
}
2010-06-12 20:27:39 +02:00
Config : : Config ( std : : pair < ConfigType , ConfigType > type , std : : pair < string , string > file , std : : pair < bool , bool > fileMustExist ) {
fileLoaded . first = false ;
fileLoaded . second = false ;
cfgType = type ;
fileName = file ;
2011-03-04 23:54:04 +01:00
fileNameParameter = file ;
2010-12-09 21:41:11 +01:00
if ( getGameReadWritePath ( GameConstants : : path_ini_CacheLookupKey ) ! = " " ) {
fileName . first = getGameReadWritePath ( GameConstants : : path_ini_CacheLookupKey ) + fileName . first ;
fileName . second = getGameReadWritePath ( GameConstants : : path_ini_CacheLookupKey ) + fileName . second ;
2010-06-12 20:27:39 +02:00
}
2010-07-12 08:30:54 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.first = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.first.c_str());
2010-06-12 20:27:39 +02:00
if ( fileMustExist . first = = true | |
( fileMustExist . first = = false & & fileExists ( fileName . first ) = = true ) ) {
properties . first . load ( fileName . first ) ;
fileLoaded . first = true ;
2010-07-12 08:30:54 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.first = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.first.c_str());
2010-06-12 20:27:39 +02:00
}
2010-07-12 08:30:54 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.first = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.first.c_str());
2010-06-12 20:27:39 +02:00
if ( properties . first . getString ( " UserOverrideFile " , defaultNotFoundValue . c_str ( ) ) ! = defaultNotFoundValue ) {
fileName . second = properties . first . getString ( " UserOverrideFile " ) ;
2010-03-22 00:40:35 +01:00
}
2010-07-12 08:30:54 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.second = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.second.c_str());
2010-06-12 20:27:39 +02:00
if ( fileMustExist . second = = true | |
( fileMustExist . second = = false & & fileExists ( fileName . second ) = = true ) ) {
properties . second . load ( fileName . second ) ;
fileLoaded . second = true ;
2010-07-12 08:30:54 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.second = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.second.c_str());
2010-06-12 20:27:39 +02:00
}
2010-07-03 09:02:31 +02:00
try {
if ( fileName . second ! = " " & & fileExists ( fileName . second ) = = false ) {
2010-12-22 01:31:30 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] attempting to auto-create cfgFile.second = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , fileName . second . c_str ( ) ) ;
2010-07-03 09:02:31 +02:00
std : : ofstream userFile ;
userFile . open ( fileName . second . c_str ( ) , ios_base : : out | ios_base : : trunc ) ;
userFile . close ( ) ;
}
}
catch ( const exception & ex ) {
2010-10-06 22:22:06 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] Error [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
2010-07-03 09:02:31 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] ERROR trying to auto-create cfgFile.second = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , fileName . second . c_str ( ) ) ;
}
2010-07-12 08:30:54 +02:00
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] cfgFile.second = [%s]\n",__FILE__,__FUNCTION__,__LINE__,fileName.second.c_str());
2010-03-13 22:10:45 +01:00
}
2010-06-12 20:27:39 +02:00
Config & Config : : getInstance ( std : : pair < ConfigType , ConfigType > type , std : : pair < string , string > file , std : : pair < bool , bool > fileMustExist ) {
if ( configList . find ( type . first ) = = configList . end ( ) ) {
2010-12-22 01:31:30 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-09 23:07:39 +02:00
2010-06-12 20:27:39 +02:00
Config config ( type , file , fileMustExist ) ;
2010-09-09 23:07:39 +02:00
2010-12-22 01:31:30 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-09 23:07:39 +02:00
2010-06-12 20:27:39 +02:00
configList . insert ( map < ConfigType , Config > : : value_type ( type . first , config ) ) ;
2010-09-09 23:07:39 +02:00
2010-12-22 01:31:30 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-06-12 20:27:39 +02:00
}
return configList . find ( type . first ) - > second ;
2010-03-13 22:10:45 +01:00
}
2010-09-09 23:07:39 +02:00
void Config : : CopyAll ( Config * src , Config * dest ) {
dest - > properties = src - > properties ;
dest - > cfgType = src - > cfgType ;
dest - > fileName = src - > fileName ;
2011-03-04 23:54:04 +01:00
dest - > fileNameParameter = src - > fileNameParameter ;
2010-09-09 23:07:39 +02:00
dest - > fileLoaded = src - > fileLoaded ;
}
void Config : : reload ( ) {
2010-12-22 01:31:30 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-09 23:07:39 +02:00
2011-03-04 23:54:04 +01:00
std : : pair < ConfigType , ConfigType > type = std : : make_pair ( cfgType . first , cfgType . second ) ;
Config newconfig ( type , std : : make_pair ( fileNameParameter . first , fileNameParameter . second ) , std : : make_pair ( true , false ) ) ;
2010-09-09 23:07:39 +02:00
2010-12-22 01:31:30 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-09 23:07:39 +02:00
Config & oldconfig = configList . find ( type . first ) - > second ;
CopyAll ( & newconfig , & oldconfig ) ;
2010-12-22 01:31:30 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] \n " , __FILE__ , __FUNCTION__ , __LINE__ ) ;
2010-09-09 23:07:39 +02:00
}
2010-03-13 22:10:45 +01:00
void Config : : save ( const string & path ) {
2010-06-12 20:27:39 +02:00
if ( fileLoaded . second = = true ) {
if ( path ! = " " ) {
fileName . second = path ;
}
properties . second . save ( fileName . second ) ;
return ;
}
if ( path ! = " " ) {
fileName . first = path ;
}
properties . first . save ( fileName . first ) ;
2010-03-13 22:10:45 +01:00
}
int Config : : getInt ( const char * key , const char * defaultValueIfNotFound ) const {
2010-06-12 20:27:39 +02:00
if ( fileLoaded . second = = true & &
properties . second . getString ( key , defaultNotFoundValue . c_str ( ) ) ! = defaultNotFoundValue ) {
return properties . second . getInt ( key , defaultValueIfNotFound ) ;
}
return properties . first . getInt ( key , defaultValueIfNotFound ) ;
2010-03-13 22:10:45 +01:00
}
bool Config : : getBool ( const char * key , const char * defaultValueIfNotFound ) const {
2010-06-12 20:27:39 +02:00
if ( fileLoaded . second = = true & &
properties . second . getString ( key , defaultNotFoundValue . c_str ( ) ) ! = defaultNotFoundValue ) {
return properties . second . getBool ( key , defaultValueIfNotFound ) ;
}
return properties . first . getBool ( key , defaultValueIfNotFound ) ;
2010-03-13 22:10:45 +01:00
}
float Config : : getFloat ( const char * key , const char * defaultValueIfNotFound ) const {
2010-06-12 20:27:39 +02:00
if ( fileLoaded . second = = true & &
properties . second . getString ( key , defaultNotFoundValue . c_str ( ) ) ! = defaultNotFoundValue ) {
return properties . second . getFloat ( key , defaultValueIfNotFound ) ;
}
return properties . first . getFloat ( key , defaultValueIfNotFound ) ;
2010-03-13 22:10:45 +01:00
}
const string Config : : getString ( const char * key , const char * defaultValueIfNotFound ) const {
2010-06-12 20:27:39 +02:00
if ( fileLoaded . second = = true & &
properties . second . getString ( key , defaultNotFoundValue . c_str ( ) ) ! = defaultNotFoundValue ) {
return properties . second . getString ( key , defaultValueIfNotFound ) ;
}
return properties . first . getString ( key , defaultValueIfNotFound ) ;
2010-03-13 22:10:45 +01:00
}
int Config : : getInt ( const string & key , const char * defaultValueIfNotFound ) const {
2010-06-12 20:27:39 +02:00
if ( fileLoaded . second = = true & &
properties . second . getString ( key , defaultNotFoundValue . c_str ( ) ) ! = defaultNotFoundValue ) {
return properties . second . getInt ( key , defaultValueIfNotFound ) ;
}
return properties . first . getInt ( key , defaultValueIfNotFound ) ;
2010-03-13 22:10:45 +01:00
}
bool Config : : getBool ( const string & key , const char * defaultValueIfNotFound ) const {
2010-06-12 20:27:39 +02:00
if ( fileLoaded . second = = true & &
properties . second . getString ( key , defaultNotFoundValue . c_str ( ) ) ! = defaultNotFoundValue ) {
return properties . second . getBool ( key , defaultValueIfNotFound ) ;
}
return properties . first . getBool ( key , defaultValueIfNotFound ) ;
2010-03-13 22:10:45 +01:00
}
float Config : : getFloat ( const string & key , const char * defaultValueIfNotFound ) const {
2010-06-12 20:27:39 +02:00
if ( fileLoaded . second = = true & &
properties . second . getString ( key , defaultNotFoundValue . c_str ( ) ) ! = defaultNotFoundValue ) {
return properties . second . getFloat ( key , defaultValueIfNotFound ) ;
}
return properties . first . getFloat ( key , defaultValueIfNotFound ) ;
2010-03-13 22:10:45 +01:00
}
const string Config : : getString ( const string & key , const char * defaultValueIfNotFound ) const {
2010-06-12 20:27:39 +02:00
if ( fileLoaded . second = = true & &
properties . second . getString ( key , defaultNotFoundValue . c_str ( ) ) ! = defaultNotFoundValue ) {
return properties . second . getString ( key , defaultValueIfNotFound ) ;
}
return properties . first . getString ( key , defaultValueIfNotFound ) ;
}
2011-03-04 21:26:47 +01:00
SDLKey Config : : translateSpecialStringToSDLKey ( char c ) const {
2011-03-08 03:07:35 +01:00
SDLKey result = SDLK_UNKNOWN ;
2011-03-04 21:26:47 +01:00
if ( c < 0 ) {
switch ( c ) {
case vkAdd :
result = SDLK_PLUS ;
break ;
case vkSubtract :
result = SDLK_MINUS ;
break ;
case vkAlt :
result = SDLK_RALT ;
break ;
case vkControl :
result = SDLK_RCTRL ;
break ;
case vkShift :
result = SDLK_RSHIFT ;
break ;
case vkEscape :
result = SDLK_ESCAPE ;
break ;
case vkUp :
result = SDLK_UP ;
break ;
case vkLeft :
result = SDLK_LEFT ;
break ;
case vkRight :
result = SDLK_RIGHT ;
break ;
case vkDown :
result = SDLK_DOWN ;
break ;
case vkReturn :
result = SDLK_RETURN ;
break ;
case vkBack :
result = SDLK_BACKSPACE ;
break ;
case vkTab :
result = SDLK_TAB ;
break ;
case vkF1 :
result = SDLK_F1 ;
break ;
case vkF2 :
result = SDLK_F2 ;
break ;
case vkF3 :
result = SDLK_F3 ;
break ;
case vkF4 :
result = SDLK_F4 ;
break ;
case vkF5 :
result = SDLK_F5 ;
break ;
case vkF6 :
result = SDLK_F6 ;
break ;
case vkF7 :
result = SDLK_F7 ;
break ;
case vkF8 :
result = SDLK_F8 ;
break ;
case vkF9 :
result = SDLK_F9 ;
break ;
case vkF10 :
result = SDLK_F10 ;
break ;
case vkF11 :
result = SDLK_F11 ;
break ;
case vkF12 :
result = SDLK_F12 ;
break ;
case vkDelete :
result = SDLK_DELETE ;
break ;
case vkPrint :
result = SDLK_PRINT ;
break ;
case vkPause :
result = SDLK_PAUSE ;
break ;
}
}
else {
result = static_cast < SDLKey > ( c ) ;
}
return result ;
}
2010-06-12 20:27:39 +02:00
char Config : : translateStringToCharKey ( const string & value ) const {
char result = 0 ;
if ( IsNumeric ( value . c_str ( ) ) = = true ) {
result = strToInt ( value ) ;
}
else if ( value . substr ( 0 , 2 ) = = " vk " ) {
if ( value = = " vkLeft " ) {
result = vkLeft ;
}
else if ( value = = " vkRight " ) {
result = vkRight ;
}
else if ( value = = " vkUp " ) {
result = vkUp ;
}
else if ( value = = " vkDown " ) {
result = vkDown ;
}
else if ( value = = " vkAdd " ) {
result = vkAdd ;
}
else if ( value = = " vkSubtract " ) {
result = vkSubtract ;
}
else if ( value = = " vkEscape " ) {
result = vkEscape ;
}
2010-09-09 23:07:39 +02:00
else if ( value = = " vkF1 " ) {
result = vkF1 ;
}
else if ( value = = " vkF2 " ) {
result = vkF2 ;
}
else if ( value = = " vkF3 " ) {
result = vkF3 ;
}
else if ( value = = " vkF4 " ) {
result = vkF4 ;
}
else if ( value = = " vkF5 " ) {
result = vkF5 ;
}
else if ( value = = " vkF6 " ) {
result = vkF6 ;
}
else if ( value = = " vkF7 " ) {
result = vkF7 ;
}
else if ( value = = " vkF8 " ) {
result = vkF8 ;
}
else if ( value = = " vkF9 " ) {
result = vkF9 ;
}
else if ( value = = " vkF10 " ) {
result = vkF10 ;
}
else if ( value = = " vkF11 " ) {
result = vkF11 ;
}
else if ( value = = " vkF12 " ) {
result = vkF12 ;
}
2011-01-03 23:22:52 +01:00
else if ( value = = " vkPrint " ) {
result = vkPrint ;
}
else if ( value = = " vkPause " ) {
result = vkPause ;
}
2010-06-12 20:27:39 +02:00
else {
2011-01-03 23:22:52 +01:00
string sError = " Unsupported key translation [ " + value + " ] " ;
2010-06-12 20:27:39 +02:00
throw runtime_error ( sError . c_str ( ) ) ;
}
}
else if ( value . length ( ) > = 1 ) {
2010-06-13 19:55:39 +02:00
if ( value . length ( ) = = 3 & & value [ 0 ] = = ' \' ' & & value [ 2 ] = = ' \' ' ) {
result = value [ 1 ] ;
}
else {
2011-03-07 00:45:46 +01:00
bool foundKey = false ;
if ( value . length ( ) > 1 ) {
for ( int i = SDLK_UNKNOWN ; i < SDLK_LAST ; + + i ) {
SDLKey key = static_cast < SDLKey > ( i ) ;
string keyName = SDL_GetKeyName ( key ) ;
if ( value = = keyName ) {
if ( key > 255 ) {
if ( value = = " left " ) {
result = vkLeft ;
}
else if ( value = = " right " ) {
result = vkRight ;
}
else if ( value = = " up " ) {
result = vkUp ;
}
else if ( value = = " down " ) {
result = vkDown ;
}
else if ( value = = " add " ) {
result = vkAdd ;
}
else if ( value = = " subtract " ) {
result = vkSubtract ;
}
else if ( value = = " escape " ) {
result = vkEscape ;
}
else if ( value = = " f1 " ) {
result = vkF1 ;
}
else if ( value = = " f2 " ) {
result = vkF2 ;
}
else if ( value = = " f3 " ) {
result = vkF3 ;
}
else if ( value = = " f4 " ) {
result = vkF4 ;
}
else if ( value = = " f5 " ) {
result = vkF5 ;
}
else if ( value = = " f6 " ) {
result = vkF6 ;
}
else if ( value = = " f7 " ) {
result = vkF7 ;
}
else if ( value = = " f8 " ) {
result = vkF8 ;
}
else if ( value = = " f9 " ) {
result = vkF9 ;
}
else if ( value = = " f10 " ) {
result = vkF10 ;
}
else if ( value = = " f11 " ) {
result = vkF11 ;
}
else if ( value = = " f12 " ) {
result = vkF12 ;
}
else if ( value = = " print-screen " ) {
result = vkPrint ;
}
else if ( value = = " pause " ) {
result = vkPause ;
}
else {
result = - key ;
}
}
else {
result = key ;
}
foundKey = true ;
break ;
}
}
}
if ( foundKey = = false ) {
result = value [ 0 ] ;
}
2010-06-13 19:55:39 +02:00
}
2010-06-12 20:27:39 +02:00
}
else {
string sError = " Unsupported key translation " + value ;
throw runtime_error ( sError . c_str ( ) ) ;
}
return result ;
}
char Config : : getCharKey ( const char * key ) const {
if ( fileLoaded . second = = true & &
properties . second . getString ( key , defaultNotFoundValue . c_str ( ) ) ! = defaultNotFoundValue ) {
string value = properties . second . getString ( key ) ;
return translateStringToCharKey ( value ) ;
}
string value = properties . first . getString ( key ) ;
return translateStringToCharKey ( value ) ;
2010-03-13 22:10:45 +01:00
}
void Config : : setInt ( const string & key , int value ) {
2010-06-12 20:27:39 +02:00
if ( fileLoaded . second = = true ) {
properties . second . setInt ( key , value ) ;
return ;
}
properties . first . setInt ( key , value ) ;
2010-03-13 22:10:45 +01:00
}
void Config : : setBool ( const string & key , bool value ) {
2010-06-12 20:27:39 +02:00
if ( fileLoaded . second = = true ) {
properties . second . setBool ( key , value ) ;
return ;
}
properties . first . setBool ( key , value ) ;
2010-03-13 22:10:45 +01:00
}
void Config : : setFloat ( const string & key , float value ) {
2010-06-12 20:27:39 +02:00
if ( fileLoaded . second = = true ) {
properties . second . setFloat ( key , value ) ;
return ;
}
properties . first . setFloat ( key , value ) ;
2010-03-13 22:10:45 +01:00
}
void Config : : setString ( const string & key , const string & value ) {
2010-06-12 20:27:39 +02:00
if ( fileLoaded . second = = true ) {
properties . second . setString ( key , value ) ;
return ;
}
properties . first . setString ( key , value ) ;
2010-03-13 22:10:45 +01:00
}
2011-01-03 21:59:18 +01:00
vector < pair < string , string > > Config : : getPropertiesFromContainer ( const Properties & propertiesObj ) const {
vector < pair < string , string > > result ;
int count = propertiesObj . getPropertyCount ( ) ;
for ( int i = 0 ; i < count ; + + i ) {
pair < string , string > property ;
property . first = propertiesObj . getKey ( i ) ;
property . second = propertiesObj . getString ( i ) ;
result . push_back ( property ) ;
}
return result ;
}
vector < pair < string , string > > Config : : getMergedProperties ( ) const {
vector < pair < string , string > > result = getMasterProperties ( ) ;
vector < pair < string , string > > resultUser = getUserProperties ( ) ;
for ( int i = 0 ; i < resultUser . size ( ) ; + + i ) {
const pair < string , string > & propertyUser = resultUser [ i ] ;
bool overrideProperty = false ;
for ( int j = 0 ; j < result . size ( ) ; + + j ) {
pair < string , string > & property = result [ j ] ;
// Take the user property and override the original value
if ( property . first = = propertyUser . first ) {
2011-01-03 23:03:41 +01:00
overrideProperty = true ;
2011-01-03 21:59:18 +01:00
property . second = propertyUser . second ;
break ;
}
}
if ( overrideProperty = = false ) {
result . push_back ( propertyUser ) ;
}
}
return result ;
}
vector < pair < string , string > > Config : : getMasterProperties ( ) const {
return getPropertiesFromContainer ( properties . first ) ;
}
vector < pair < string , string > > Config : : getUserProperties ( ) const {
return getPropertiesFromContainer ( properties . second ) ;
}
2011-03-04 23:54:04 +01:00
void Config : : setUserProperties ( const vector < pair < string , string > > & valueList ) {
Properties & propertiesObj = properties . second ;
for ( int idx = 0 ; idx < valueList . size ( ) ; + + idx ) {
const pair < string , string > & nameValuePair = valueList [ idx ] ;
propertiesObj . setString ( nameValuePair . first , nameValuePair . second ) ;
}
}
string Config : : getFileName ( bool userFilename ) const {
string result = fileName . second ;
if ( userFilename = = false ) {
result = fileName . first ;
}
return result ;
}
2010-03-13 22:10:45 +01:00
string Config : : toString ( ) {
2010-06-12 20:27:39 +02:00
return properties . first . toString ( ) ;
2010-03-13 22:10:45 +01:00
}
2010-03-21 06:33:13 +01:00
vector < string > Config : : getPathListForType ( PathType type , string scenarioDir ) {
2010-03-16 22:44:22 +01:00
vector < string > pathList ;
2010-12-09 21:41:11 +01:00
string data_path = getGameReadWritePath ( GameConstants : : path_data_CacheLookupKey ) ;
2010-03-20 01:46:16 +01:00
string userData = getString ( " UserData_Root " , " " ) ;
if ( userData ! = " " ) {
if ( userData [ userData . size ( ) - 1 ] ! = ' / ' & & userData [ userData . size ( ) - 1 ] ! = ' \\ ' ) {
userData + = ' / ' ;
}
2011-03-10 02:23:34 +01:00
if ( data_path = = " " ) {
userData = data_path + userData ;
}
else {
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " extractLastDirectoryFromPath(userData) [%s] from userData [%s] \n " , extractLastDirectoryFromPath ( userData ) . c_str ( ) , userData . c_str ( ) ) ;
userData = data_path + extractLastDirectoryFromPath ( userData ) ;
}
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s Line: %d] userData path [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , userData . c_str ( ) ) ;
2010-07-27 20:56:12 +02:00
if ( isdir ( userData . c_str ( ) ) = = false ) {
createDirectoryPaths ( userData ) ;
2011-03-10 02:23:34 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s Line: %d] creating path [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , userData . c_str ( ) ) ;
2010-07-27 20:56:12 +02:00
}
2011-01-01 11:16:25 +01:00
string userDataMaps = userData + GameConstants : : folder_path_maps ;
if ( isdir ( userDataMaps . c_str ( ) ) = = false ) {
createDirectoryPaths ( userDataMaps ) ;
2011-03-10 02:23:34 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s Line: %d] creating path [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , userDataMaps . c_str ( ) ) ;
2011-01-01 11:16:25 +01:00
}
string userDataScenarios = userData + GameConstants : : folder_path_scenarios ;
if ( isdir ( userDataScenarios . c_str ( ) ) = = false ) {
createDirectoryPaths ( userDataScenarios ) ;
2011-03-10 02:23:34 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s Line: %d] creating path [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , userDataScenarios . c_str ( ) ) ;
2011-01-01 11:16:25 +01:00
}
string userDataTechs = userData + GameConstants : : folder_path_techs ;
if ( isdir ( userDataTechs . c_str ( ) ) = = false ) {
createDirectoryPaths ( userDataTechs ) ;
2011-03-10 02:23:34 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s Line: %d] creating path [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , userDataTechs . c_str ( ) ) ;
2011-01-01 11:16:25 +01:00
}
string userDataTilesets = userData + GameConstants : : folder_path_tilesets ;
if ( isdir ( userDataTilesets . c_str ( ) ) = = false ) {
createDirectoryPaths ( userDataTilesets ) ;
2011-03-10 02:23:34 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s Line: %d] creating path [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , userDataTilesets . c_str ( ) ) ;
2011-01-01 11:16:25 +01:00
}
string userDataTutorials = userData + GameConstants : : folder_path_tutorials ;
if ( isdir ( userDataTutorials . c_str ( ) ) = = false ) {
createDirectoryPaths ( userDataTutorials ) ;
2011-03-10 02:23:34 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " In [%s::%s Line: %d] creating path [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , userDataTutorials . c_str ( ) ) ;
2011-01-01 11:16:25 +01:00
}
2010-03-20 01:46:16 +01:00
}
2010-03-21 06:33:13 +01:00
if ( scenarioDir ! = " " ) {
2010-12-09 21:41:11 +01:00
pathList . push_back ( data_path + scenarioDir ) ;
2010-03-21 06:33:13 +01:00
}
2010-03-20 01:46:16 +01:00
2010-03-16 22:44:22 +01:00
switch ( type ) {
case ptMaps :
2010-12-09 21:41:11 +01:00
pathList . push_back ( data_path + GameConstants : : folder_path_maps ) ;
2010-03-20 01:46:16 +01:00
if ( userData ! = " " ) {
pathList . push_back ( userData + string ( GameConstants : : folder_path_maps ) ) ;
2010-03-16 22:44:22 +01:00
}
break ;
case ptScenarios :
2010-12-09 21:41:11 +01:00
pathList . push_back ( data_path + GameConstants : : folder_path_scenarios ) ;
2010-03-20 01:46:16 +01:00
if ( userData ! = " " ) {
pathList . push_back ( userData + string ( GameConstants : : folder_path_scenarios ) ) ;
2010-03-16 22:44:22 +01:00
}
break ;
case ptTechs :
2010-12-09 21:41:11 +01:00
pathList . push_back ( data_path + GameConstants : : folder_path_techs ) ;
2010-03-20 01:46:16 +01:00
if ( userData ! = " " ) {
pathList . push_back ( userData + string ( GameConstants : : folder_path_techs ) ) ;
2010-03-16 22:44:22 +01:00
}
break ;
case ptTilesets :
2010-12-09 21:41:11 +01:00
pathList . push_back ( data_path + GameConstants : : folder_path_tilesets ) ;
2010-03-20 01:46:16 +01:00
if ( userData ! = " " ) {
pathList . push_back ( userData + string ( GameConstants : : folder_path_tilesets ) ) ;
2010-03-16 22:44:22 +01:00
}
break ;
case ptTutorials :
2010-12-09 21:41:11 +01:00
pathList . push_back ( data_path + GameConstants : : folder_path_tutorials ) ;
2010-03-20 01:46:16 +01:00
if ( userData ! = " " ) {
pathList . push_back ( userData + string ( GameConstants : : folder_path_tutorials ) ) ;
2010-03-16 22:44:22 +01:00
}
break ;
}
return pathList ;
}
2010-03-13 22:10:45 +01:00
} } // end namespace