Skip to content

Commit

Permalink
update keyvalues (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
PerfectLaugh committed Jun 26, 2022
1 parent e78c6fd commit 325244b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
20 changes: 14 additions & 6 deletions public/tier1/KeyValues.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
Expand All @@ -20,6 +20,8 @@
#endif
#endif

#include <vstdlib/IKeyValuesSystem.h>

#include "utlvector.h"
#include "Color.h"
#include "exprevaluator.h"
Expand All @@ -29,7 +31,6 @@ class CUtlBuffer;
class Color;
class KeyValues;
class IKeyValuesDumpContext;
class IKeyValuesSystem;
typedef void * FileHandle_t;

// single byte identifies a xbox kv file in binary format
Expand Down Expand Up @@ -71,7 +72,7 @@ typedef void * FileHandle_t;
class KeyValues
{
public:
KeyValues( const char *setName );
KeyValues( const char *setName, IKeyValuesSystem *customSystem = NULL, bool ownsCustomSystem = false );

//
// AutoDelete class to automatically free the keyvalues.
Expand Down Expand Up @@ -312,7 +313,7 @@ class KeyValues
// If filesystem is null, it'll ignore f.
void InternalWrite( IBaseFileSystem *filesystem, FileHandle_t f, CUtlBuffer *pBuf, const void *pData, int len );

void Init();
void Init(IKeyValuesSystem *customSystem = NULL, bool ownsCustomSystem = false);
const char * ReadToken( CUtlBuffer &buf, bool &wasQuoted, bool &wasConditional );
void WriteIndents( IBaseFileSystem *filesystem, FileHandle_t f, CUtlBuffer *pBuf, int indentLevel );

Expand All @@ -323,6 +324,14 @@ class KeyValues

bool EvaluateConditional( const char *pExpressionString, GetSymbolProc_t pfnEvaluateSymbolProc );

inline IKeyValuesSystem *GetKeyValuesSystem() const {
if (m_pKeyValuesSystem) {
return m_pKeyValuesSystem;
}

return KeyValuesSystem();
}

uint32 m_iKeyName : 24; // keyname is a symbol defined in KeyValuesSystem
uint32 m_iKeyNameCaseSensitive1 : 8; // 1st part of case sensitive symbol defined in KeyValueSystem

Expand All @@ -344,8 +353,7 @@ class KeyValues
uint16 m_iKeyNameCaseSensitive2; // 2nd part of case sensitive symbol defined in KeyValueSystem;

IKeyValuesSystem *m_pKeyValuesSystem;
char m_bOwnsCustomKeyValuesSystem;
char unused[3];
bool m_bOwnsCustomKeyValuesSystem;

KeyValues *m_pPeer; // pointer to next key in list
KeyValues *m_pSub; // pointer to Start of a new sub key list
Expand Down
42 changes: 22 additions & 20 deletions tier1/KeyValues.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
Expand Down Expand Up @@ -211,11 +211,11 @@ static CLeakTrack track;
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
KeyValues::KeyValues( const char *setName )
KeyValues::KeyValues( const char *setName, IKeyValuesSystem *customSystem, bool ownsCustomSystem )
{
TRACK_KV_ADD( this, setName );

Init();
Init( customSystem, ownsCustomSystem );
SetName ( setName );
}

Expand Down Expand Up @@ -284,7 +284,7 @@ KeyValues::KeyValues( const char *setName, const char *firstKey, int firstValue,
//-----------------------------------------------------------------------------
// Purpose: Initialize member variables
//-----------------------------------------------------------------------------
void KeyValues::Init()
void KeyValues::Init(IKeyValuesSystem *customSystem, bool ownsCustomSystem)
{
m_iKeyName = 0;
m_iKeyNameCaseSensitive1 = 0;
Expand All @@ -301,11 +301,8 @@ void KeyValues::Init()

m_bHasEscapeSequences = false;

m_pKeyValuesSystem = 0;
m_bOwnsCustomKeyValuesSystem = 0;

// for future proof
memset( unused, 0, sizeof(unused) );
m_pKeyValuesSystem = customSystem;
m_bOwnsCustomKeyValuesSystem = ownsCustomSystem;
}

//-----------------------------------------------------------------------------
Expand All @@ -316,6 +313,11 @@ KeyValues::~KeyValues()
TRACK_KV_REMOVE( this );

RemoveEverything();

if (m_pKeyValuesSystem && m_bOwnsCustomKeyValuesSystem) {
delete m_pKeyValuesSystem;
m_pKeyValuesSystem = NULL;
}
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -370,7 +372,7 @@ void KeyValues::ChainKeyValue( KeyValues* pChain )
//-----------------------------------------------------------------------------
const char *KeyValues::GetName( void ) const
{
return KeyValuesSystem()->GetStringForSymbol(m_iKeyName);
return GetKeyValuesSystem()->GetStringForSymbol(m_iKeyName);
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -768,7 +770,7 @@ KeyValues *KeyValues::FindKey(const char *keyName, bool bCreate)
}

// lookup the symbol for the search string
HKeySymbol iSearchStr = KeyValuesSystem()->GetSymbolForString( searchStr, bCreate );
HKeySymbol iSearchStr = GetKeyValuesSystem()->GetSymbolForString( searchStr, bCreate );
if ( iSearchStr == INVALID_KEY_SYMBOL )
{
// not found, couldn't possibly be in key value list
Expand Down Expand Up @@ -800,7 +802,7 @@ KeyValues *KeyValues::FindKey(const char *keyName, bool bCreate)
if (bCreate)
{
// we need to create a new key
dat = new KeyValues( searchStr );
dat = new KeyValues( searchStr, m_pKeyValuesSystem );
// Assert(dat != NULL);

// insert new key at end of list
Expand Down Expand Up @@ -867,7 +869,7 @@ KeyValues *KeyValues::CreateNewKey()
KeyValues* KeyValues::CreateKey( const char *keyName )
{
// key wasn't found so just create a new one
KeyValues* dat = new KeyValues( keyName );
KeyValues* dat = new KeyValues( keyName, m_pKeyValuesSystem );

dat->UsesEscapeSequences( m_bHasEscapeSequences != 0 ); // use same format as parent does

Expand Down Expand Up @@ -1425,7 +1427,7 @@ void KeyValues::SetFloat( const char *keyName, float value )

void KeyValues::SetName( const char * setName )
{
m_iKeyName = KeyValuesSystem()->GetSymbolForString( setName );
m_iKeyName = GetKeyValuesSystem()->GetSymbolForString( setName );
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -1534,14 +1536,14 @@ void KeyValues::RecursiveCopyKeyValues( KeyValues& src )
// Handle the immediate child
if( src.m_pSub )
{
m_pSub = new KeyValues( NULL );
m_pSub = new KeyValues( NULL, m_pKeyValuesSystem );
m_pSub->RecursiveCopyKeyValues( *src.m_pSub );
}

// Handle the immediate peer
if( src.m_pPeer )
{
m_pPeer = new KeyValues( NULL );
m_pPeer = new KeyValues( NULL, m_pKeyValuesSystem );
m_pPeer->RecursiveCopyKeyValues( *src.m_pPeer );
}
}
Expand Down Expand Up @@ -1758,7 +1760,7 @@ void KeyValues::ParseIncludedKeys( char const *resourceName, const char *filetoi
// Append included file
Q_strncat( fullpath, filetoinclude, sizeof( fullpath ), COPY_ALL_CHARACTERS );

KeyValues *newKV = new KeyValues( fullpath );
KeyValues *newKV = new KeyValues( fullpath, m_pKeyValuesSystem );

// CUtlSymbol save = s_CurrentFileSymbol; // did that had any use ???

Expand Down Expand Up @@ -1909,7 +1911,7 @@ bool KeyValues::LoadFromBuffer( char const *resourceName, CUtlBuffer &buf, IBase

if ( !pCurrentKey )
{
pCurrentKey = new KeyValues( s );
pCurrentKey = new KeyValues( s, m_pKeyValuesSystem );
Assert( pCurrentKey );

pCurrentKey->UsesEscapeSequences( m_bHasEscapeSequences != 0 ); // same format has parent use
Expand Down Expand Up @@ -2281,7 +2283,7 @@ bool KeyValues::ReadAsBinary( CUtlBuffer &buffer )
{
case TYPE_NONE:
{
dat->m_pSub = new KeyValues("");
dat->m_pSub = new KeyValues("", m_pKeyValuesSystem);
dat->m_pSub->ReadAsBinary( buffer );
break;
}
Expand Down Expand Up @@ -2345,7 +2347,7 @@ bool KeyValues::ReadAsBinary( CUtlBuffer &buffer )
break;

// new peer follows
dat->m_pPeer = new KeyValues("");
dat->m_pPeer = new KeyValues("", m_pKeyValuesSystem);
dat = dat->m_pPeer;
}

Expand Down

0 comments on commit 325244b

Please sign in to comment.