1
0
mirror of https://github.com/alliedmodders/hl2sdk.git synced 2025-01-07 09:43:40 +08:00

Hackfix KeyValues class so at least self-owned ones work.

Still completely incorrect for working with ones from the game.
This commit is contained in:
Nicholas Hastings 2015-10-08 12:54:18 -04:00
parent 27c00af76b
commit b1d180a2d7
3 changed files with 44 additions and 37 deletions

View File

@ -23,6 +23,7 @@
#include "utlvector.h"
#include "Color.h"
#include "exprevaluator.h"
#include <vstdlib/IKeyValuesSystem.h>
class IBaseFileSystem;
class CUtlBuffer;
@ -120,8 +121,8 @@ public:
void SetName( const char *setName);
// gets the name as a unique int
int GetNameSymbol() const;
int GetNameSymbolCaseSensitive() const;
HKeySymbol GetNameSymbol() const;
HKeySymbol GetNameSymbolCaseSensitive() const;
// File access. Set UsesEscapeSequences true, if resource file/buffer uses Escape Sequences (eg \n, \t)
void UsesEscapeSequences(bool state); // default false
@ -137,7 +138,7 @@ public:
// Find a keyValue, create it if it is not found.
// Set bCreate to true to create the key if it doesn't already exist (which ensures a valid pointer will be returned)
KeyValues *FindKey(const char *keyName, bool bCreate = false);
KeyValues *FindKey(int keySymbol) const;
KeyValues *FindKey(HKeySymbol keySymbol) const;
KeyValues *CreateNewKey(); // creates a new key, with an autogenerated name. name is guaranteed to be an integer, of value 1 higher than the highest other integer key name
void AddSubKey( KeyValues *pSubkey ); // Adds a subkey. Make sure the subkey isn't a child of some other keyvalues
void RemoveSubKey(KeyValues *subKey); // removes a subkey from the list, DOES NOT DELETE IT
@ -187,15 +188,15 @@ public:
bool IsEmpty(const char *keyName = NULL);
// Data access
int GetInt( int keySymbol, int defaultValue = 0 );
uint64 GetUint64( int keySymbol, uint64 defaultValue = 0 );
float GetFloat( int keySymbol, float defaultValue = 0.0f );
const char *GetString( int keySymbol, const char *defaultValue = "" );
const wchar_t *GetWString( int keySymbol, const wchar_t *defaultValue = L"" );
void *GetPtr( int keySymbol, void *defaultValue = (void*)0 );
Color GetColor( int keySymbol /* default value is all black */);
bool GetBool( int keySymbol, bool defaultValue = false ) { return GetInt( keySymbol, defaultValue ? 1 : 0 ) ? true : false; }
bool IsEmpty( int keySymbol );
int GetInt(HKeySymbol keySymbol, int defaultValue = 0);
uint64 GetUint64(HKeySymbol keySymbol, uint64 defaultValue = 0);
float GetFloat(HKeySymbol keySymbol, float defaultValue = 0.0f);
const char *GetString(HKeySymbol keySymbol, const char *defaultValue = "");
const wchar_t *GetWString(HKeySymbol keySymbol, const wchar_t *defaultValue = L"");
void *GetPtr(HKeySymbol keySymbol, void *defaultValue = (void*) 0);
Color GetColor(HKeySymbol keySymbol /* default value is all black */);
bool GetBool(HKeySymbol keySymbol, bool defaultValue = false) { return GetInt(keySymbol, defaultValue ? 1 : 0) ? true : false; }
bool IsEmpty(HKeySymbol keySymbol);
// Key writing
void SetWString( const char *keyName, const wchar_t *value );
@ -207,11 +208,13 @@ public:
void SetColor( const char *keyName, Color value);
void SetBool( const char *keyName, bool value ) { SetInt( keyName, value ? 1 : 0 ); }
#if 0
// Memory allocation (optimized)
void *operator new( size_t iAllocSize );
void *operator new( size_t iAllocSize, int nBlockUse, const char *pFileName, int nLine );
void operator delete( void *pMem );
void operator delete( void *pMem, int nBlockUse, const char *pFileName, int nLine );
#endif
KeyValues& operator=( KeyValues& src );
@ -322,8 +325,7 @@ private:
bool EvaluateConditional( const char *pExpressionString, GetSymbolProc_t pfnEvaluateSymbolProc );
uint32 m_iKeyName : 24; // keyname is a symbol defined in KeyValuesSystem
uint32 m_iKeyNameCaseSensitive1 : 8; // 1st part of case sensitive symbol defined in KeyValueSystem
HKeySymbol m_iKeyName; // keyname is a symbol defined in KeyValuesSystem
// These are needed out of the union because the API returns string pointers
char *m_sValue;
@ -340,7 +342,6 @@ private:
char m_iDataType;
char m_bHasEscapeSequences; // true, if while parsing this KeyValue, Escape Sequences are used (default false)
uint16 m_iKeyNameCaseSensitive2; // 2nd part of case sensitive symbol defined in KeyValueSystem;
KeyValues *m_pPeer; // pointer to next key in list
KeyValues *m_pSub; // pointer to Start of a new sub key list
@ -376,50 +377,50 @@ struct KeyValuesUnpackStructure
//-----------------------------------------------------------------------------
// inline methods
//-----------------------------------------------------------------------------
inline int KeyValues::GetInt( int keySymbol, int defaultValue )
inline int KeyValues::GetInt( HKeySymbol keySymbol, int defaultValue )
{
KeyValues *dat = FindKey( keySymbol );
return dat ? dat->GetInt( (const char *)NULL, defaultValue ) : defaultValue;
}
inline uint64 KeyValues::GetUint64( int keySymbol, uint64 defaultValue )
inline uint64 KeyValues::GetUint64(HKeySymbol keySymbol, uint64 defaultValue)
{
KeyValues *dat = FindKey( keySymbol );
return dat ? dat->GetUint64( (const char *)NULL, defaultValue ) : defaultValue;
}
inline float KeyValues::GetFloat( int keySymbol, float defaultValue )
inline float KeyValues::GetFloat(HKeySymbol keySymbol, float defaultValue)
{
KeyValues *dat = FindKey( keySymbol );
return dat ? dat->GetFloat( (const char *)NULL, defaultValue ) : defaultValue;
}
inline const char *KeyValues::GetString( int keySymbol, const char *defaultValue )
inline const char *KeyValues::GetString(HKeySymbol keySymbol, const char *defaultValue)
{
KeyValues *dat = FindKey( keySymbol );
return dat ? dat->GetString( (const char *)NULL, defaultValue ) : defaultValue;
}
inline const wchar_t *KeyValues::GetWString( int keySymbol, const wchar_t *defaultValue )
inline const wchar_t *KeyValues::GetWString(HKeySymbol keySymbol, const wchar_t *defaultValue)
{
KeyValues *dat = FindKey( keySymbol );
return dat ? dat->GetWString( (const char *)NULL, defaultValue ) : defaultValue;
}
inline void *KeyValues::GetPtr( int keySymbol, void *defaultValue )
inline void *KeyValues::GetPtr(HKeySymbol keySymbol, void *defaultValue)
{
KeyValues *dat = FindKey( keySymbol );
return dat ? dat->GetPtr( (const char *)NULL, defaultValue ) : defaultValue;
}
inline Color KeyValues::GetColor( int keySymbol )
inline Color KeyValues::GetColor(HKeySymbol keySymbol)
{
Color defaultValue( 0, 0, 0, 0 );
KeyValues *dat = FindKey( keySymbol );
return dat ? dat->GetColor( ) : defaultValue;
}
inline bool KeyValues::IsEmpty( int keySymbol )
inline bool KeyValues::IsEmpty(HKeySymbol keySymbol)
{
KeyValues *dat = FindKey( keySymbol );
return dat ? dat->IsEmpty( ) : true;

View File

@ -12,9 +12,9 @@
#include "vstdlib/vstdlib.h"
// handle to a KeyValues key name symbol
typedef int HKeySymbol;
#define INVALID_KEY_SYMBOL (-1)
class KeyValues;
DECLARE_HANDLE_32BIT(HKeySymbol)
#define INVALID_KEY_SYMBOL (HKeySymbol::MakeHandle(~0))
//-----------------------------------------------------------------------------
// Purpose: Interface to shared data repository for KeyValues (included in vgui_controls.lib)
@ -42,6 +42,12 @@ public:
// symbol table access from code with case-preserving requirements (used for key names)
virtual HKeySymbol GetSymbolForStringCaseSensitive( HKeySymbol &hCaseInsensitiveSymbol, const char *name, bool bCreate = true ) = 0;
virtual HKeySymbol GetCaseInsensitiveSymbolFromCaseSensitiveSymbol( HKeySymbol symbol ) = 0;
virtual const char *CopyString( const char * ) = 0;
virtual void ReleaseStringCopy( const char * ) = 0;
virtual const wchar_t *CopyWString( const wchar_t * ) = 0;
virtual void ReleaseWStringCopy( const wchar_t * ) = 0;
};
VSTDLIB_INTERFACE IKeyValuesSystem *KeyValuesSystem();

View File

@ -51,7 +51,7 @@ public:
// entering a new keyvalues block, save state for errors
// Not save symbols instead of pointers because the pointers can move!
int Push( int symName )
int Push(HKeySymbol symName)
{
if ( m_errorIndex < MAX_ERROR_STACK )
{
@ -70,7 +70,7 @@ public:
}
// Allows you to keep the same stack level, but change the name as you parse peers
void Reset( int stackLevel, int symName )
void Reset(int stackLevel, HKeySymbol symName)
{
Assert( stackLevel >= 0 && stackLevel < m_errorIndex );
m_errorStack[stackLevel] = symName;
@ -98,7 +98,7 @@ public:
}
private:
int m_errorStack[MAX_ERROR_STACK];
HKeySymbol m_errorStack[MAX_ERROR_STACK];
const char *m_pFilename;
int m_errorIndex;
int m_maxErrorIndex;
@ -118,16 +118,16 @@ public:
{
g_KeyValuesErrorStack.Pop();
}
CKeyErrorContext( int symName )
CKeyErrorContext( HKeySymbol symName )
{
Init( symName );
}
void Reset( int symName )
void Reset( HKeySymbol symName )
{
g_KeyValuesErrorStack.Reset( m_stackLevel, symName );
}
private:
void Init( int symName )
void Init( HKeySymbol symName )
{
m_stackLevel = g_KeyValuesErrorStack.Push( symName );
}
@ -275,9 +275,7 @@ KeyValues::KeyValues( const char *setName, const char *firstKey, int firstValue,
//-----------------------------------------------------------------------------
void KeyValues::Init()
{
m_iKeyName = 0;
m_iKeyNameCaseSensitive1 = 0;
m_iKeyNameCaseSensitive2 = 0;
m_iKeyName = HKeySymbol::MakeHandle(0);
m_iDataType = TYPE_NONE;
m_pSub = NULL;
@ -359,7 +357,7 @@ const char *KeyValues::GetName( void ) const
//-----------------------------------------------------------------------------
// Purpose: Get the symbol name of the current key section
//-----------------------------------------------------------------------------
int KeyValues::GetNameSymbol() const
HKeySymbol KeyValues::GetNameSymbol() const
{
return m_iKeyName;
}
@ -714,7 +712,7 @@ void KeyValues::RecursiveSaveToFile( IBaseFileSystem *filesystem, FileHandle_t f
//-----------------------------------------------------------------------------
// Purpose: looks up a key by symbol name
//-----------------------------------------------------------------------------
KeyValues *KeyValues::FindKey(int keySymbol) const
KeyValues *KeyValues::FindKey(HKeySymbol keySymbol) const
{
for (KeyValues *dat = m_pSub; dat != NULL; dat = dat->m_pPeer)
{
@ -2337,6 +2335,7 @@ bool KeyValues::ReadAsBinary( CUtlBuffer &buffer )
#include "tier0/memdbgoff.h"
#if 0
//-----------------------------------------------------------------------------
// Purpose: memory allocator
//-----------------------------------------------------------------------------
@ -2366,6 +2365,7 @@ void KeyValues::operator delete( void *pMem, int nBlockUse, const char *pFileNam
{
KeyValuesSystem()->FreeKeyValuesMemory((KeyValues *)pMem);
}
#endif
void KeyValues::UnpackIntoStructure( KeyValuesUnpackStructure const *pUnpackTable, void *pDest )
{