-
Notifications
You must be signed in to change notification settings - Fork 4
/
GoldSrc.KeyValues.pas
231 lines (191 loc) · 7.85 KB
/
GoldSrc.KeyValues.pas
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
(*=========== (C) Copyright 2020, Alexander B. All rights reserved. ===========*)
(* *)
(* Module: *)
(* GoldSrc.KeyValues *)
(* *)
(* License: *)
(* You may freely use this code provided you retain this copyright message. *)
(* *)
(* Description: *)
(*=============================================================================*)
unit GoldSrc.KeyValues;
interface
uses
Xander.ThisWrap;
{$A2}
{$Z2}
type
KeyValueType = (kvNone, kvStr, kvInt, kvFloat, kvPointer, kvWStr, kvColor, kvUInt64);
{$A4}
{$Z4}
type
PIFileSystem = Pointer;
PVKeyValues = ^VKeyValues;
VKeyValues = record
GetName: function: PAnsiChar; stdcall;
GetNameSymbol: function: Integer; stdcall;
LoadFromFile: function(FileSystem: PIFileSystem; ResourceName: PAnsiChar; PathID: PAnsiChar = nil): Boolean; stdcall;
SaveToFile: function(FileSystem: PIFileSystem; ResourceName: PAnsiChar; PathID: PAnsiChar = nil): Boolean; stdcall;
{$IFDEF MSWINDOWS}
FindKeyByValue: function(KeySymbol: Integer): Pointer; stdcall;
FindKeyByName: function(KeyName: PAnsiChar; Create: Boolean = False): Pointer; stdcall;
{$ELSE}
FindKeyByName: function(KeyName: PAnsiChar; Create: Boolean = False): Pointer; stdcall;
FindKeyByValue: function(KeySymbol: Integer): Pointer; stdcall;
{$ENDIF}
CreateNewKey: function: Pointer; stdcall; // 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
RemoveSubKey: procedure(SubKey: Pointer); stdcall; // removes a subkey from the list, DOES NOT DELETE IT
GetFirstSubKey: function: Pointer; stdcall; // returns the first subkey in the list
GetNextKey: function: Pointer; stdcall; // returns the next subkey
GetInt: function(KeyName: PAnsiChar = nil; DefaultValue: Integer = 0): Integer; stdcall;
GetFloat: function(KeyName: PAnsiChar = nil; DefaultValue: Single = 0.0): Single; stdcall;
GetString: function(KeyValue: PAnsiChar; DefaultValue: PAnsiChar): PAnsiChar; stdcall;
GetWString: function(KeyName: PAnsiChar; DefaultValue: PWideChar): PWideChar; stdcall;
GetPtr: function(KeyName: PAnsiChar = nil; DefaultValue: Pointer = nil): Pointer; stdcall;
IsEmpty: function(KeyName: PAnsiChar = nil): Boolean; stdcall;
SetWString: procedure(KeyName: PAnsiChar; Value: PWideChar); stdcall;
SetString: procedure(KeyName: PAnsiChar; Value: PAnsiChar); stdcall;
SetInt: procedure(KeyName: PAnsiChar; Value: Integer); stdcall;
SetFloat: procedure(KeyName: PAnsiChar; Value: Single); stdcall;
SetPtr: procedure(KeyName: PAnsiChar; Value: Pointer); stdcall;
MakeCopy: function: Pointer; stdcall;
Clear: procedure; stdcall;
GetDataType: function(KeyName: PAnsiChar = nil): KeyValueType; stdcall;
deleteThis: procedure; stdcall;
end;
type
PKeyValues = ^KeyValues;
DKeyValues = record
KeyName: Integer;
KeyValue: record
case Byte of
0: (Integer: Integer);
1: (Float: Single);
2: (Ptr: Pointer);
3: (Color: Integer);
4: (AString: PAnsiChar);
5: (WString: PWideChar);
end;
DataType: KeyValueType;
AllocationSize: Word;
Peer: PKeyValues;
Sub: PKeyValues;
end;
KeyValues = record
VTable: PVKeyValues;
Data: DKeyValues;
end;
const
INVALID_KEY_SYMBOL = -1;
type
PListPanelItem = ^ListPanelItem;
ListPanelItem = record
KV: PKeyValues;
UserData: Cardinal;
DragData: Pointer;
Image: Boolean;
ImageIndex: Integer;
ImageIndexSelected: Integer;
Icon: Pointer;
end;
HKeySymbol = Integer;
PVKeyValuesSystem = ^VKeyValuesSystem;
VKeyValuesSystem = record
Destroy: procedure(Dispose: Boolean); stdcall;
RegisterSizeofKeyValues: procedure(Size: Integer); stdcall;
AllocKeyValuesMemory: function(Size: Integer): Pointer; stdcall;
FreeKeyValuesMemory: procedure(Mem: Pointer); stdcall;
GetSymbolForString: function(Name: PAnsiChar): HKeySymbol; stdcall;
GetStringForSymbol: function(Symbol: HKeySymbol): PAnsiChar; stdcall;
GetLocalizedFromANSI: procedure(ANSI: PAnsiChar; OutBuf: PWideChar; UnicodeBufferSizeInBytes: Integer); stdcall;
GetANSIFromLocalized: procedure(WChar: PWideChar; OutBuf: PAnsiChar; AnsiBufferSizeInBytes: Integer); stdcall;
AddKeyValuesToMemoryLeakList: procedure(Mem: Pointer; Name: HKeySymbol); stdcall;
RemoveKeyValuesFromMemoryLeakList: procedure(Mem: Pointer); stdcall;
end;
PIKeyValuesSystem = ^IKeyValuesSystem;
IKeyValuesSystem = record
VTable: PVKeyValuesSystem;
end;
type
TKeyValueEnumerator = reference to procedure(KeyValue: PKeyValues; Name: PAnsiChar);
KeyValuesHelper = record helper for KeyValues
procedure EnumKeys(Enumerator: TKeyValueEnumerator);
function ReadInt: Integer; overload;
function ReadInt(KeyName: PAnsiChar; Default: Integer): Integer; overload;
function ReadString: PAnsiChar; overload;
function ReadString(KeyName, Default: PAnsiChar): PAnsiChar; overload;
function ReadPtr: Pointer; overload;
function ReadPtr(KeyName: PansiChar; Default: Pointer): Pointer; overload;
procedure WriteInt(Value: Integer); overload;
procedure WriteInt(KeyName: PAnsiChar; Value: Integer); overload;
procedure WriteString(Value: PAnsiChar); overload;
procedure WriteString(KeyName, Value: PAnsiChar); overload;
procedure WriteWString(KeyName: PAnsiChar; Value: PWideChar); overload;
procedure WriteWString(Value: PWideChar); overload;
end;
implementation
procedure KeyValuesHelper.EnumKeys(Enumerator: TKeyValueEnumerator);
var
KV: PKeyValues;
begin
if @Enumerator = nil then
Exit;
KV := @Self;
KV := ThisCall(KV, @KV.VTable.GetFirstSubKey);
while True do
begin
Enumerator(KV, ThisCall(KV, @KV.VTable.GetName));
KV := ThisCall(KV, @KV.VTable.GetNextKey);
if KV = nil then
Break;
end;
end;
function KeyValuesHelper.ReadInt(KeyName: PAnsiChar; Default: Integer): Integer;
begin
Result := Integer(ThisCall(@Self, @Self.VTable.GetInt, KeyName, Default));
end;
function KeyValuesHelper.ReadPtr: Pointer;
begin
Result := ThisCall(@Self, @Self.VTable.GetPtr, nil, nil);
end;
function KeyValuesHelper.ReadPtr(KeyName: PansiChar; Default: Pointer): Pointer;
begin
Result := ThisCall(@Self, @Self.VTable.GetPtr, KeyName, Default);
end;
function KeyValuesHelper.ReadString(KeyName, Default: PAnsiChar): PAnsiChar;
begin
Result := ThisCall(@Self, @Self.VTable.GetString, KeyName, Default);
end;
function KeyValuesHelper.ReadString: PAnsiChar;
begin
Result := ThisCall(@Self, @Self.VTable.GetString, nil, nil);
end;
procedure KeyValuesHelper.WriteInt(KeyName: PAnsiChar; Value: Integer);
begin
ThisCall(@Self, @Self.VTable.SetInt, KeyName, Value);
end;
procedure KeyValuesHelper.WriteString(KeyName, Value: PAnsiChar);
begin
ThisCall(@Self, @Self.VTable.SetString, KeyName, Value);
end;
procedure KeyValuesHelper.WriteWString(KeyName: PAnsiChar; Value: PWideChar);
begin
ThisCall(@Self, @Self.VTable.SetWString, KeyName, Value);
end;
procedure KeyValuesHelper.WriteWString(Value: PWideChar);
begin
ThisCall(@Self, @Self.VTable.SetWString, nil, Value);
end;
procedure KeyValuesHelper.WriteInt(Value: Integer);
begin
ThisCall(@Self, @Self.VTable.SetInt, nil, Value);
end;
procedure KeyValuesHelper.WriteString(Value: PAnsiChar);
begin
ThisCall(@Self, @Self.VTable.SetString, nil, Value);
end;
function KeyValuesHelper.ReadInt: Integer;
begin
Result := Integer(ThisCall(@Self, @Self.VTable.GetInt, nil, 0));
end;
end.