google-site-verification: googlebaca44933768a824.html [C++] My CVar class - Old Royal Hack Forum

Announcement

Collapse
No announcement yet.

[C++] My CVar class

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    [C++] My CVar class

    Code:
    #pragma once
    
    #include "Headers.h"
    
    using std::find;
    using std::make_pair;
    using std::map;
    using std::pair;
    using std::string;
    using std::vector;
    
    // templates are fucking gay
    typedef boost::any var;
    
    class CVar
    {
    private:
    	var val;
    	var min;
    	var max;
    public:
    	CVar::CVar( const var &_min, const var &_max, const var &_value )
    	{
    		min		= _min;
    		max		= _max;
    		val		= _value;
    	}
    	FORCEINLINE var GetCVarMin() const
    	{
    		return min;
    	}
    	FORCEINLINE var GetCVarMax() const
    	{
    		return max;
    	}
    	FORCEINLINE var GetCVarValue() const
    	{
    		return val;
    	}
    	FORCEINLINE void SetCVar( var _val )
    	{
    		/*
    		if( _val > this->max )
    			val = max;
    		else if( _val < this->min )
    			val = min;
    		*/
    		val = _val;
    	}
    };
    typedef map<string, CVar*>cvarmap_t;
    typedef cvarmap_t::iterator it_t;
    
    class CCVarManager
    {
    private:
    	cvarmap_t m_varMap;
    	it_t m_Itr;
    	CVar *GetCVar( string name );
    public:
    	bool  AddCVar( string _name, var _min, var _max, var _value );
    	bool  GetCVarBool( string name );
    	float GetCVarFloat( string name );
    	int   GetCVarInt( string name );
    	void  SetCVar( string name, string value );
    };
    Code:
    #include "CCvar.h"
    
    bool CCVarManager::AddCVar( string _name, var _min, var _max, var _value )
    {
    	CVar *pVar = new CVar( _min, _max, _value );
    
    	if( !pVar )
    		return false;
    
    	m_varMap.insert( make_pair( _name, pVar ) );
    	
    	return true;
    }
    
    CVar *CCVarManager::GetCVar( string name )
    {
    	m_Itr = m_varMap.find( name );
    	
    	if( m_Itr == m_varMap.end() )
    		return NULL;
    
    	return m_Itr->second;
    }
    
    bool CCVarManager::GetCVarBool( string name )
    {
    	CVar *pTmp = GetCVar( name );
    
    	if( pTmp == NULL || pTmp->GetCVarValue().type() != typeid( bool ) )
    		return false;
    
    	return boost::any_cast<bool>( pTmp->GetCVarValue() );
    }
    
    float CCVarManager::GetCVarFloat( string name )
    {
    	CVar *pTmp = GetCVar( name );
    
    	if( pTmp == NULL || pTmp->GetCVarValue().type() != typeid( float ) )
    		return 0.0f;
    
    	return boost::any_cast<float>( pTmp->GetCVarValue() );
    }
    
    int CCVarManager::GetCVarInt( string name )
    {
    	CVar *pTmp = GetCVar( name );
    	
    	if( pTmp == NULL || pTmp->GetCVarValue().type() != typeid( int ) )
    		return 0;
    
    	return boost::any_cast<int>( pTmp->GetCVarValue() );
    }
    
    bool CCVarManager::Load( string name )
    {
    	ifstream _in;
    	string key, line, val; 
    	string path = g_strWorkDir + "\\settings\\";
    
    	if( !_in.is_open() )
    	{
    		path.append( name + ".ini" );
    
    		_in.open( path.c_str(), std::ios_base::in );
    	}
    
    	while( !_in.eof() )
    	{
    		getline( _in, line );
    
    		if( line.empty() )
    			continue;
    
    		string::size_type st = line.find_first_of('=');
    
    		key = line.substr( 0, st );
    		val = line.substr( st+1, line.length()-(st+1) );
    
    		this->SetCVar( key, val );	
    	}
    	_in.close();
    
    	return true;
    }
    
    bool CCVarManager::Save( string name )
    {
    	ofstream _out;
    	string path = g_strWorkDir + "\\settings\\";
    
    	if( !_out.is_open() )
    	{
    		path.append( name + ".ini" );
    		_out.open( path.c_str(), std::ios_base::out );
    	}
    
    	for( m_Itr = m_varMap.begin(); m_Itr != m_varMap.end(); ++m_Itr )
    	{
    		_out << m_Itr->first + "=";
    		
    		if( m_Itr->second )
    		{
    			if( m_Itr->second->GetCVarValue().type() == typeid( bool ))
    			{
    				if( GetCVarBool( m_Itr->first ) == true )
    					_out << "true" << std::endl;
    				else
    					_out << "false" << std::endl;
    			}
    			if( m_Itr->second->GetCVarValue().type() == typeid( int ))
    				_out << GetCVarInt( m_Itr->first ) << std::endl;
    			if( m_Itr->second->GetCVarValue().type() == typeid( float ))
    				_out << GetCVarFloat( m_Itr->first ) << std::endl;
    			if( m_Itr->second->GetCVarValue().type() == typeid( DWORD ))
    				_out << GetCVarUlong( m_Itr->first ) << std::hex << std::endl;
    		}
    	}
    	_out.close();
    	
    	return true;
    }
    
    void CCVarManager::SetCVar( string name, string value ) // hackkkkkk
    {
    	CVar *pVar = GetCVar( name );
    
    	if( pVar == NULL )
    		return;
    	
    	if( pVar->GetCVarValue().type() == typeid( int ) )
    	{
    		int iNew = atoi( value.c_str() );
    		pVar->SetCVar( boost::any_cast<int>( iNew ));
    	}
    	if( pVar->GetCVarValue().type() == typeid( bool ))
    	{
    		bool bValue = false;
    		
    		if( !value.find( "true" ) )
    			pVar->SetCVar( boost::any_cast<bool>( true ) ); // why this is ok but false ain't? o0
    		else
    			pVar->SetCVar( boost::any_cast<bool>( bValue ) );
    	}
    	if( pVar->GetCVarValue().type() == typeid( float ) )
    	{
    		if( !value.find( "." ) ) // more than likely a floating point
    		{
    			float flNew = atof( value.c_str() );
    			pVar->SetCVar( boost::any_cast<float>( flNew ));
    		}
    	}
    }
    Covers bools, floats and ints, more datatypes can be and will be supported later. DWORDs for keybinds, colors etc.

    EDIT: Added Save-function, produces a file like this:
    Code:
    bool=on
    bool2=off
    float=1.25
    float2=2.692
    float3=1.78
    int=1
    int2=6
    EDIT2: Load done too.
    lolmaoman: Germans are born with a lifetime x22 login engraved into their birth certificates. True story.
    I DONT HAVE TEAMVIEWER AND IM NOT GOING TO GIVE ANY 24/7 ONLINE SUPPORT VIA STEAM, XFIRE OR OTHER IM PROGRAMS SO DONT BOTHER ASKING. THANKS.

    #2
    Re: [C++] My CVar class

    Load / SetCVar were not even working as they should, updateddddddd
    lolmaoman: Germans are born with a lifetime x22 login engraved into their birth certificates. True story.
    I DONT HAVE TEAMVIEWER AND IM NOT GOING TO GIVE ANY 24/7 ONLINE SUPPORT VIA STEAM, XFIRE OR OTHER IM PROGRAMS SO DONT BOTHER ASKING. THANKS.

    Comment


      #3
      Re: [C++] My CVar class

      sexy c++

      Comment


        #4
        Re: [C++] My CVar class

        nice class.
        yea, templates are pretty gay
        Originally posted by wav
        My German blood demands I decimate this game quickly.
        XFire - AddMe

        Comment


          #5
          Re: [C++] My CVar class

          handy class.






          gibs coins @
          1KatP9B8KG7mvcoFhdLGua1isG88nYZE8C

          Comment


            #6
            Re: [C++] My CVar class

            >2012
            Almost same.

            Code:
            #pragma once
            
            #include "Includes.hpp"
            
            // templates are fucking gay
            typedef boost::any var;
            
            class CAnyCVar
            {
            protected:
            	var val;
            	var min;
            	var max;
            	std::string strTextval;
            public:
            	CAnyCVar( const std::string &strValue )
            	{
            		strTextval = strValue;
            	}
            	CAnyCVar( const var &_min, const var &_max, const var &_value )
            	{
            		min		= _min;
            		max		= _max;
            		val		= _value;
            	}
            	FORCEINLINE var GetCVarMin() const
            	{
            		return min;
            	}
            	FORCEINLINE var GetCVarMax() const
            	{
            		return max;
            	}
            	FORCEINLINE var GetCVarValue() const
            	{
            		return val;
            	}
            	FORCEINLINE std::string GetCVarTextValue() const
            	{
            		return strTextval;
            	}
            	FORCEINLINE void SetCVar( var _val )
            	{
            		if( GetCVarTextValue().size() != 0 )
            			strTextval = boost::any_cast<std::string>( _val );
            
            		val = _val;
            	}
            };
            
            typedef boost::shared_ptr<CAnyCVar>CVarObject_t;
            typedef std::map<std::string, CVarObject_t>mapVars_t;
            
            class CCVarManager
            {
            protected:
            	mapVars_t m_mapVars;
            public:
            	CVarObject_t GetCVar( const std::string &strName );
            	bool AddCVar( const std::string &strName, var _min, var _max, var _value );
            	bool AddCVar( const std::string &strName, const std::string &strValue );
            	bool GetCVarBool( const std::string &strName );
            	float GetCVarFloat( const std::string &strName );
            	int GetCVarInt( const std::string &strName );
            	std::string GetCVarString( const std::string &strName );
            	void Load( const std::string &strFileName );
            	void Save( const std::string &strFileName );
            	void SetCVar( std::string strName, std::string strValue );
            };
            
            extern CCVarManager *g_pCVarManager;
            Code:
            #include "CVarManager.hpp"
            
            CCVarManager *g_pCVarManager = NULL;
            
            bool CCVarManager::AddCVar( const std::string &strName, const std::string &strValue )
            {
            	CVarObject_t _cvar( new CAnyCVar( strValue ) );
            
            	if( _cvar == nullptr )
            		return false;
            
            	m_mapVars.insert( make_pair( strName, _cvar ) );
            
            	return true;
            }
            
            bool CCVarManager::AddCVar( const std::string &strName, var _min, var _max, var _value )
            {
            	CVarObject_t _cvar( new CAnyCVar( _min, _max, _value ) );
            
            	if( _cvar == nullptr )
            		return false;
            
            	m_mapVars.insert( make_pair( strName, _cvar ) );
            
            	return true;
            }
            
            CVarObject_t CCVarManager::GetCVar( const std::string &strName )
            {
            	std::map<std::string, CVarObject_t>::iterator it = m_mapVars.find( strName );
            	
            	if( it == m_mapVars.end() )
            		return CVarObject_t ( new CAnyCVar( 0, 0, 0 ) ); // fucked up behaviour if GetCVar is called with wrong param
            
            	return it->second;
            }
            
            bool CCVarManager::GetCVarBool( const std::string &strName )
            {
            	boost::shared_ptr<CAnyCVar>_temp( GetCVar( strName ) );
            
            	if( _temp == nullptr || _temp.get()->GetCVarValue().type() != typeid(bool) )
            		return false;
            
            	return boost::any_cast<bool>( _temp.get()->GetCVarValue() );
            }
            
            float CCVarManager::GetCVarFloat( const std::string &strName )
            {
            	boost::shared_ptr<CAnyCVar>_temp( GetCVar( strName ) );
            
            	if( _temp == nullptr || _temp.get()->GetCVarValue().type() != typeid(float) )
            		return 0.0f;
            
            	return boost::any_cast<float>( _temp.get()->GetCVarValue() );
            }
            
            int CCVarManager::GetCVarInt( const std::string &strName )
            {
            	boost::shared_ptr<CAnyCVar>_temp( GetCVar( strName ) );
            
            	if( _temp == nullptr  || _temp.get()->GetCVarValue().type() != typeid(int) )
            		return 0;
            
            	return boost::any_cast<int>( _temp.get()->GetCVarValue() );
            }
            
            std::string CCVarManager::GetCVarString( const std::string &strName )
            {
            	boost::shared_ptr<CAnyCVar>_temp( GetCVar( strName ) );
            
            	if( _temp == nullptr )
            		return "Oops! Something went wrong at CCVarManager::GetCVarString";
            
            	return _temp.get()->GetCVarTextValue();
            	//return boost::any_cast<std::string>( _temp.get()->GetCVarTextValue() ); // any_cast irrelevant
            }
            
            void CCVarManager::Load( const std::string &strName )
            {
            	boost::filesystem::ifstream _inistream;
            	
            	std::string strKey, strLine, strVal;
            	std::string strPath;
            
            	if( !_inistream.is_open() )
            	{
            		strPath = "C:";
            		strPath.append( strName + ".ini" );
            
            		_inistream.open( strPath.c_str(), std::ios_base::in );
            	}
            
            	while( !_inistream.eof() )
            	{
            		std::getline( _inistream, strLine );
            		std::string::size_type _st;
            
            		if( strLine.empty() )
            			continue;
            
            		_st = strLine.find_first_of('=');
            
            		strKey = strLine.substr( 0, _st );
            		strVal = strLine.substr( _st+1, strLine.length()-(_st+1) );
            
            		this->SetCVar( strKey, strVal );
            	}
            
            	_inistream.close();
            }
            
            void CCVarManager::SetCVar( std::string strName, std::string strValue )
            {
            	boost::shared_ptr<CAnyCVar>_temp( GetCVar( strName ) );
            
            	if( _temp == nullptr )
            		return;
            
            	if( _temp->GetCVarValue().type() == typeid( bool ) )
            	{
            		bool bValue = false;
            
            		if( strValue.find( "true" || "on" ) == false )
            			bValue = false;
            		else if( strValue.find( "false" || "off" ) == false )
            			bValue = true;
            
            		_temp->SetCVar( boost::any_cast<bool>( bValue ) );
            	}
            
            	if( _temp->GetCVarValue().type() == typeid( float ) )
            	{
            		if( strValue.find( "." ) == false )
            		{
            			float flNew = boost::lexical_cast<float>( strValue );
            
            			_temp->SetCVar( boost::any_cast<float>( flNew ) );
            		}
            	}
            
            	if( _temp->GetCVarValue().type() == typeid( int ) )
            	{
            		int iNew = boost::lexical_cast<int>( strValue );
            		
            		_temp->SetCVar( boost::any_cast<int>( iNew ) );
            	}
            }
            
            void CCVarManager::Save( const std::string &strFileName )
            {
            	boost::filesystem::ofstream _inistream;
            	std::string strPath;
            
            	if( !_inistream.is_open() )
            	{
            		strPath = "C:";
            		strPath.append( strFileName + ".ini" );
            		_inistream.open( strPath, std::ios_base::out );
            	}
            
            	std::map<std::string, CVarObject_t>::iterator it;
            	
            	for( it = m_mapVars.begin(); it != m_mapVars.end(); ++it )
            	{
            		_inistream << it->first + "=";
            		
            		if( it->second->GetCVarValue().type() == typeid( bool ) )
            		{
            			if( GetCVarBool( it->first ) == true )
            				_inistream << "on" << std::endl;
            			else
            				_inistream << "off" << std::endl;
            		}
            		if( it->second->GetCVarValue().type() == typeid( float ) )
            			_inistream << GetCVarFloat( it->first ) << std::endl;
            		if( it->second->GetCVarValue().type() == typeid( int ) )
            			_inistream << GetCVarInt( it->first ) << std::endl;
            		if( it->second->GetCVarTextValue().size() != 0 )
            			_inistream << GetCVarString( it->first ) << std::endl;
            	}
            	_inistream.close();
            }
            EDIT: GetCVarBool, Float and Int might as well be replaced with:
            Code:
            template <typename T>
            T GetAnyCVarValue( const std::string &strName )
            {
            	boost::shared_ptr<CAnyCVar>_temp( GetCVar( strName ) );
            
            	if( _temp != nullptr && _temp.get()->GetCVarValue().type() == typeid(T) )
            		return boost::any_cast<T>( _temp.get()->GetCVarValue() );
            
                    // handle errors here like wrong name, null CAnyCVar pointer etc.
            }
            Code:
            int iValue = GetAnyCVarValue<int>( "intcvar" );
            Last edited by mencore; 11-01-2012, 01:19 PM.
            lolmaoman: Germans are born with a lifetime x22 login engraved into their birth certificates. True story.
            I DONT HAVE TEAMVIEWER AND IM NOT GOING TO GIVE ANY 24/7 ONLINE SUPPORT VIA STEAM, XFIRE OR OTHER IM PROGRAMS SO DONT BOTHER ASKING. THANKS.

            Comment


              #7
              Re: [C++] My CVar class

              m3nly from m3ncore

              Comment


                #8
                Re: [C++] My CVar class

                GetCVarBool, Int, Float etc. might as well be replaced with

                Code:
                	
                template <typename T>
                T GetAnyCVarValue( const std::string &strName )
                {
                	T ret;
                	boost::shared_ptr<CAnyCVar>_temp( GetCVar( strName ) );
                
                	if( _temp != nullptr && _temp.get()->GetCVarValue().type() == typeid(T) )
                	{
                		try
                		{
                			ret = boost::any_cast<T>( _temp.get()->GetCVarValue() );
                		}
                		catch( boost::bad_any_cast &shitcast )
                		{
                			ret = 0; // hope it doesnt break
                			MessageBox( NULL, shitcast.what(), "boost::bad_anycast", MB_OK );
                		}
                
                		return ret;
                	}
                	return ret;
                }
                Testing:

                Code:
                m_pAppCVars->AddCVar( "int", 0, 5, 1 );
                m_pAppCVars->AddCVar( "float", 0.0, 5.5f, 0.23f );
                m_pAppCVars->AddCVar( "bool", false, true, true );
                
                char szBuf[512];
                sprintf( szBuf, "%i", m_pAppCVars->GetAnyCVarValue<int>( "int" ) );
                MessageBox( NULL, szBuf, NULL, MB_OK );
                
                sprintf( szBuf, "%f", m_pAppCVars->GetAnyCVarValue<float>( "float" ) );
                MessageBox( NULL, szBuf, NULL, MB_OK );
                
                if( m_pAppCVars->GetAnyCVarValue<bool>( "bool" ) == true )
                	sprintf( szBuf, "tru" );
                else
                	sprintf( szBuf, "dat" );
                MessageBox( NULL, szBuf, NULL, MB_OK );
                
                sprintf( szBuf, "does this break? %f", m_pAppCVars->GetAnyCVarValue<bool>( "int" ) );
                MessageBox( NULL, szBuf, NULL, MB_OK );
                Last edited by mencore; 11-01-2012, 01:41 PM.
                lolmaoman: Germans are born with a lifetime x22 login engraved into their birth certificates. True story.
                I DONT HAVE TEAMVIEWER AND IM NOT GOING TO GIVE ANY 24/7 ONLINE SUPPORT VIA STEAM, XFIRE OR OTHER IM PROGRAMS SO DONT BOTHER ASKING. THANKS.

                Comment

                Working...
                X