-
Автор темы
- #1
KeyValues.cpp:
#include <Windows.h>
#include "KeyValues.hpp"
#include "../Globals.hpp"
typedef C_KeyValues* (__thiscall* FindKey_t)(void*, const char*, bool);
typedef const char* (__thiscall* GetString_t)(void*, const char*, const char*);
typedef bool(__thiscall* LoadFromBuffer_t)(C_KeyValues*, const char*, const char*, void*, void*, void*, void*);
typedef void(__thiscall* SetString_t)(C_KeyValues*, const char*);
void* C_KeyValues::operator new( size_t allocatedsize )
{
static PVOID pKeyValuesSystem;
if ( !pKeyValuesSystem )
pKeyValuesSystem = ( reinterpret_cast< PVOID ( __cdecl* )( ) >( GetProcAddress( GetModuleHandleA( _S( "vstdlib.dll" ) ), _S( "KeyValuesSystem" ) ) ) )( );
return GetVirtual< PVOID( __thiscall* )( PVOID, size_t ) >( pKeyValuesSystem, 2 )( pKeyValuesSystem, allocatedsize );
}
void C_KeyValues::operator delete( void* mem )
{
static PVOID pKeyValuesSystem;
if (!pKeyValuesSystem)
pKeyValuesSystem = ( reinterpret_cast< PVOID ( __cdecl* )( ) >( GetProcAddress( GetModuleHandleA( _S( "vstdlib.dll" ) ), _S( "KeyValuesSystem" ) ) ) )( );
return GetVirtual< void( __thiscall* )( PVOID, PVOID ) >( pKeyValuesSystem, 3 )( pKeyValuesSystem, mem );
}
void C_KeyValues::Init( )
{
m_pValue = NULL;
m_bHasEscapeSequences = false;
m_iUnk1 = 0;
m_iUnk2 = 0;
m_iKeyName = -1;
m_iDataType = TYPE_NONE;
m_pSub = NULL;
m_pPeer = NULL;
m_pChain = NULL;
m_sValue = NULL;
m_wsValue = NULL;
memset( unused, 0, sizeof( unused ) );
}
C_KeyValues* C_KeyValues::FindKey( const char* strKeyName, bool bCreate )
{
return ( ( FindKey_t )( g_Globals.m_AddressList.m_KeyValuesFindKey ) )( this, strKeyName, bCreate );
}
const char* C_KeyValues::GetString( C_KeyValues* pThis, const char* strKeyName, const char* strDefaultValue )
{
return ( ( GetString_t )( g_Globals.m_AddressList.m_KeyValuesGetString ) )( pThis, strKeyName, strDefaultValue );
}
bool C_KeyValues::LoadFromBuffer( C_KeyValues* pThis, const char* pszFirst, const char* pszSecond, PVOID pSomething, PVOID pAnother, PVOID pLast, PVOID pAnother2 )
{
return ( ( LoadFromBuffer_t )( g_Globals.m_AddressList.m_KeyValuesLoadFromBuffer ) )( pThis, pszFirst, pszSecond, pSomething, pAnother, pLast, pAnother2 );
}
void C_KeyValues::SetString( const char* strName, const char* strValue )
{
C_KeyValues* pKeyValues = this->FindKey( strName, 1 );
if ( pKeyValues )
( ( SetString_t )( g_Globals.m_AddressList.m_KeyValuesSetString ) )( pKeyValues, strValue );
}
void C_KeyValues::SetInt( const char* strName, int iValue )
{
auto pKeyValues = FindKey( strName, 1 );
if ( !pKeyValues )
return;
m_iValue = iValue;
m_iDataType = 2;
}
void C_KeyValues::SetFloat( const char* strName, float flValue )
{
auto pKeyValues = FindKey( strName, 1 );
if ( !pKeyValues )
return;
m_flValue = flValue;
m_iDataType = 3;
}
KeyValues.hpp:
#pragma once
class C_KeyValues
{
public:
void* operator new( size_t allocatedsize );
void operator delete( void* mem );
C_KeyValues( const char* strName )
{
Init( );
SetName( strName );
}
void SetName( const char* strName )
{
m_iKeyName = 2;
}
enum types_t
{
TYPE_NONE = 0,
TYPE_STRING,
TYPE_INT,
TYPE_FLOAT,
TYPE_PTR,
TYPE_WSTRING,
TYPE_COLOR,
TYPE_UINT64,
TYPE_NUMTYPES,
};
int m_iKeyName; // keyname is a symbol defined in KeyValuesSystem
int m_iUnk1;
int m_iUnk2;
char* m_sValue;
wchar_t* m_wsValue;
union
{
int m_iValue;
float m_flValue;
void* m_pValue;
unsigned char m_Color[4];
};
char m_iDataType;
char m_bHasEscapeSequences; // true, if while parsing this KeyValue, Escape Sequences are used (default false)
char m_bEvaluateConditionals; // true, if while parsing this KeyValue, conditionals blocks are evaluated (default true)
char unused[ 1 ];
C_KeyValues* m_pPeer; // pointer to next key in list
C_KeyValues* m_pSub; // pointer to Start of a new sub key list
C_KeyValues* m_pChain;// Search here if it's not in our list
template<class CDATA> CDATA GetValueByKey( const char* keyname )
{
C_KeyValues* pFind = FindKey( keyname, false );
if ( pFind )
{
CDATA return_value;
std::stringstream ss( GetString( this, keyname, NULL ) );
ss >> return_value;
return return_value;
}
else return NULL;
}
void Init( );
C_KeyValues* FindKey( const char* keyName, bool bCreate );
const char* GetString( C_KeyValues* pThis, const char* keyName, const char* defaultValue );
bool LoadFromBuffer( C_KeyValues* pThis, const char* pszFirst, const char* pszSecond, void* pSomething = 0, void* pAnother = 0, void* pLast = 0, void* pAnother2 = 0 );
void SetString( const char* name, const char* value );
void SetInt( const char* name, int value );
void SetFloat( const char* name, float value );
};