aboutsummaryrefslogtreecommitdiff
path: root/mp/src/public/keyvaluescompiler.h
blob: ff6cc40da2deeb9e7ae312a76a06f033c66c82f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================

#ifndef KEYVALUESCOMPILER_H
#define KEYVALUESCOMPILER_H
#ifdef _WIN32
#pragma once
#endif

#include "tier0/platform.h"
#include "tier1/utlbuffer.h"
#include "tier1/utlsymbol.h"
#include "tier1/utldict.h"

class KeyValues;

#define COMPILED_KEYVALUES_ID	MAKEID( 'V', 'K', 'V', 'F' )

#define COMPILED_KEYVALUES_VERSION	1

struct KVHeader_t
{
	int			fileid;
	int			version;
	int			numStrings;
};

#pragma pack(1)
struct KVFile_t
{
	KVFile_t() :
		filename( 0 ),
		firstElement( 0 ),
		numElements( 0 )
	{
	}
	short			filename;
	short			firstElement;
	short			numElements;
};

struct KVInfo_t
{
	KVInfo_t() :
		key( 0 ),
		value( 0 ),
		parentIndex( -1 ),
		issubtree( false )
	{
	}

	inline void SetParent( int index )
	{
		Assert( index <= 32768 );
		parentIndex = index;
	}

	inline short GetParent() const
	{
		return parentIndex;
	}

	inline void SetSubTree( bool state )
	{
		issubtree = state;
	}

	inline bool IsSubTree() const
	{
		return issubtree;
	}

	short		key;
	short		value;

private:

	short		parentIndex;
	bool		issubtree;
};
#pragma pack()

//-----------------------------------------------------------------------------
// Purpose: stringtable is a session global string table.
//-----------------------------------------------------------------------------
class CCompiledKeyValuesWriter
{
public:

	CCompiledKeyValuesWriter()
	{
		m_StringTable.AddString( "" );
	}

	void AppendKeyValuesFile( char const *filename );
	void WriteFile( char const *outfile );

private:

	void Describe( const KVFile_t& file );

	void BuildKVData_R( KeyValues *kv, int parent );

	void WriteStringTable( CUtlBuffer& buf );
	void WriteData( CUtlBuffer& buf );
	void WriteFiles( CUtlBuffer &buf );

	CUtlVector< KVFile_t >		m_Files;
	CUtlVector< KVInfo_t >		m_Data;

	CUtlSymbolTable				m_StringTable;
};


class CRunTimeKeyValuesStringTable
{
public:

	bool ReadStringTable( int numStrings, CUtlBuffer& buf );
	
	inline int Count() const
	{
		return m_Strings.Count();
	}

	inline char const *Lookup( short index )
	{
		return m_Strings[ index ];
	}

private:
	CUtlVector< const char * >	m_Strings; 
};

class CCompiledKeyValuesReader
{
public:

	CCompiledKeyValuesReader();
	
	bool		LoadFile( char const *filename );

	KeyValues	*Instance( char const *kvfilename );
	bool		InstanceInPlace( KeyValues& head, char const *kvfilename );
	bool		LookupKeyValuesRootKeyName( char const *kvfilename, char *outbuf, size_t bufsize );

	int			First() const;
	int			Next( int i ) const;
	int			InvalidIndex() const;

	void		GetFileName( int index, char *buf, size_t bufsize );

private:

	struct FileInfo_t
	{
		FileInfo_t() : 
			hFile( 0 ),
			nFirstIndex( 0 ), 
			nCount( 0 ) 
		{
		}
		FileNameHandle_t	hFile;
		short				nFirstIndex;
		short				nCount;

		static bool Less( const FileInfo_t& lhs, const FileInfo_t& rhs )
		{
			return lhs.hFile < rhs.hFile;
		}
	};

	KeyValues *CreateFromData( const FileInfo_t& info );
	bool CreateInPlaceFromData( KeyValues& head, const FileInfo_t& info );

	// Now get the actual files
	CUtlRBTree< FileInfo_t, unsigned short >	m_Dict;
	CUtlVector< KVInfo_t >		m_Data;

	CRunTimeKeyValuesStringTable		m_StringTable;

	CUtlBuffer							m_LoadBuffer;
};

#endif // KEYVALUESCOMPILER_H