This repository has been archived by the owner on Nov 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
EvilWorks.System.RTTI.pas
310 lines (269 loc) · 8.09 KB
/
EvilWorks.System.RTTI.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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
unit EvilWorks.System.RTTI;
interface
uses
System.Classes,
System.SysUtils,
System.TypInfo,
System.IniFiles,
System.RTTI;
type
EEvilRTTI = class(Exception);
EMethodNotFound = class(EEvilRTTI);
function GetTypeKindName(const aTypeKind: TTypeKind): string;
function InvokeObjectMethod(aClassInstance: TObject; const aMethod: string; const aParams: array of string): string;
type
{ Exceptions }
ERTTIIni = class(Exception);
{ TRTTIIni }
TRTTIIni = class(TPersistent)
strict private
FFileName: string;
protected
{ Override to set the name of the section where properties will be written. }
{ If not overiden, ClassName will be used instead. }
function GetSectionName: string; virtual;
{ Save and load to FileName. }
procedure Save;
procedure Load;
public
{ Set the FileName to/from which to Save/Load. Do it before Load() or Save()... }
property FileName: string read FFileName write FFileName;
published
{ Auto-Streamable properties go here. See Load() and Save() methods for supported data types. }
{ If you nest TRTTIIni object properties, they get auto saved as well to FileName. You'll have }
{ to declare them first (and their GetSectionName if you don't want auto-names). }
end;
implementation
uses
EvilWorks.System.StrUtils;
resourcestring
SErrTypeNotFound = 'TRttiType %s not found.';
SErrTypeMethodNotFound = 'TRttiMethod %s not found.';
function GetTypeKindName(const aTypeKind: TTypeKind): string;
const
TTypeKindNames: array [0 .. 21] of string = (
'tkUnknown', 'tkInteger', 'tkChar', 'tkEnumeration', 'tkFloat', 'tkString', 'tkSet', 'tkClass',
'tkMethod', 'tkWChar', 'tkLString', 'tkWString', 'tkVariant', 'tkArray', 'tkRecord', 'tkInterface',
'tkInt64', 'tkDynArray', 'tkUString', 'tkClassRef', 'tkPointer', 'tkProcedure'
);
begin
Result := TTypeKindNames[Ord(aTypeKind)];
end;
function InvokeObjectMethod(aClassInstance: TObject; const aMethod: string; const aParams: array of string): string;
var
rttiCtx : TRttiContext;
rttiType : TRttiType;
rttiMethod: TRttiMethod;
rttiParams: TArray<TRttiParameter>;
rttiValues: TArray<TValue>;
i : integer;
intVal : integer;
begin
rttiCtx := TRttiContext.Create;
try
rttiType := rttiCtx.GetType(aClassInstance.ClassInfo);
if (rttiType = nil) then
raise EEvilRTTI.CreateFmt(SErrTypeNotFound, [aClassInstance.ClassName]);
rttiMethod := rttiType.GetMethod(aMethod);
if (rttiMethod = nil) then
raise EMethodNotFound.CreateFmt(SErrTypeMethodNotFound, [aMethod]);
rttiParams := rttiMethod.GetParameters;
if (Length(rttiParams) > 0) then
begin
if (Length(rttiParams) > Length(aParams)) then
raise Exception.Create(Format('RTTIInvokeTypeMethod: Parameter counts differ: Given %d, expected %d.', [Length(aParams), Length(rttiParams)]));
SetLength(rttiValues, Length(rttiParams));
for i := 0 to Length(rttiParams) - 1 do
begin
case rttiParams[i].ParamType.TypeKind of
tkUnknown:
begin
raise Exception.Create(Format('RTTIInvokeTypeMethod: Unknown parameter type for Method "%s" at index %d.', [aMethod, i]));
end;
tkChar, tkWChar, tkLString, tkUString, tkString:
begin
rttiValues[i] := aParams[i];
end;
tkWString:
begin
rttiValues[i] := TValue.From(widestring(aParams[i]));
end;
tkInteger:
begin
rttiValues[i] := StrToInt(aParams[i]);
end;
tkInt64:
begin
rttiValues[i] := StrToInt64(aParams[i]);
end;
tkFloat:
begin
rttiValues[i] := StrToFloat(aParams[i]);
end;
tkEnumeration:
begin
rttiValues[i] := TValue.FromOrdinal(rttiParams[i].ParamType.Handle, GetEnumValue(rttiParams[i].ParamType.Handle, aParams[i]));
end;
tkSet:
begin
intVal := StringToSet(rttiParams[i].ParamType.Handle, aParams[i]);
TValue.Make(@intVal, rttiParams[i].ParamType.Handle, rttiValues[i]);
end;
tkVariant:
begin
rttiValues[i] := TValue.FromVariant(variant(aParams[i]));
end;
tkMethod:
begin
end;
tkProcedure:
begin
end;
tkClass:
begin
end;
tkArray:
begin
end;
tkRecord:
begin
end;
tkInterface:
begin
end;
tkDynArray:
begin
end;
tkClassRef:
begin
end;
tkPointer:
begin
end;
end; { case }
end; { for }
end; { if }
Result := rttiMethod.Invoke(aClassInstance, rttiValues).ToString;
finally
rttiCtx.Free;
end;
end;
{ TRTTIIni }
function TRTTIIni.GetSectionName: string;
begin
Result := Self.ClassName;
end;
procedure TRTTIIni.Load;
var
ini : TIniFile;
sub : TRTTIIni;
sl : TStringList;
i : integer;
v : integer;
def : string;
data : string;
rttiContext : TRttiContext;
rttiType : TRttiType;
rttiProperty : TRttiProperty;
rttiProperties: TArray<TRttiProperty>;
rttiVal : TValue;
begin
ini := TIniFile.Create(FileName);
sl := TStringList.Create;
rttiContext := TRttiContext.Create;
try
rttiType := rttiContext.GetType(Self.ClassInfo);
if (rttiType = nil) then
raise ERTTIIni.Create('TRTTIIni.Load(): Context.GetType() failed.');
ini.ReadSection(GetSectionName, sl);
for i := 0 to sl.Count - 1 do
begin
rttiProperty := rttiType.GetProperty(sl[i]);
if (rttiProperty <> nil) then
begin
if (rttiProperty.Visibility <> mvPublished) or (not rttiProperty.IsWritable) or (not rttiProperty.IsReadable) then
Continue;
def := rttiProperty.GetValue(Self).ToString;
data := ini.ReadString(GetSectionName, sl[i], def);
case rttiProperty.GetValue(Self).Kind of
tkWChar, tkLString, tkWString, tkString, tkChar, tkUString:
rttiVal := data;
tkInteger, tkInt64:
rttiVal := StrToInt(data);
tkFloat:
rttiVal := StrToFloat(data);
tkEnumeration:
rttiVal := TValue.FromOrdinal(rttiProperty.GetValue(Self).TypeInfo, GetEnumValue(rttiProperty.GetValue(Self).TypeInfo, data));
tkSet:
begin
v := StringToSet(rttiVal.TypeInfo, data);
TValue.Make(@v, rttiVal.TypeInfo, rttiVal);
end;
end;
rttiProperty.SetValue(Self, rttiVal);
end;
end;
rttiProperties := rttiType.GetProperties;
for rttiProperty in rttiProperties do
begin
if (rttiProperty.Visibility <> mvPublished) or (not rttiProperty.IsReadable) or (not rttiProperty.IsWritable) then
Continue;
if (rttiProperty.GetValue(Self).IsObject) then
begin
if (rttiProperty.GetValue(Self).AsObject is TRTTIIni) then
begin
sub := TRTTIIni(rttiProperty.GetValue(Self).AsObject);
sub.FileName := Self.FileName;
sub.Load;
end;
end;
end;
finally
rttiContext.Free;
sl.Free;
ini.Free;
end;
end;
procedure TRTTIIni.Save;
var
ini : TIniFile;
sub : TRTTIIni;
rttiContext : TRttiContext;
rttiType : TRttiType;
rttiProperties: TArray<TRttiProperty>;
rttiProperty : TRttiProperty;
begin
if (FFileName = '') then
raise ERTTIIni.Create('TRTTIIni.Save(): FileName not specified.');
ini := TIniFile.Create(FileName);
rttiContext := TRttiContext.Create;
try
ForceDirectories(ExtractFileDir(FileName));
rttiType := rttiContext.GetType(Self.ClassInfo);
if (rttiType = nil) then
raise ERTTIIni.Create('TRTTIIni.Save(): Failed to get TRTTIType.');
rttiProperties := rttiType.GetProperties;
for rttiProperty in rttiProperties do
begin
if (rttiProperty.Visibility <> mvPublished) or (not rttiProperty.IsReadable) or (not rttiProperty.IsWritable) then
Continue;
if rttiProperty.GetValue(Self).Kind in [
tkWChar, tkLString, tkWString, tkString, tkChar, tkUString,
tkInteger, tkInt64, tkFloat, tkEnumeration, tkSet] then
ini.WriteString(GetSectionName, rttiProperty.Name, rttiProperty.GetValue(Self).ToString);
if (rttiProperty.GetValue(Self).IsObject) then
begin
if (rttiProperty.GetValue(Self).AsObject is TRTTIIni) then
begin
sub := TRTTIIni(rttiProperty.GetValue(Self).AsObject);
sub.FileName := Self.FileName;
sub.Save;
end;
end;
end;
finally
rttiContext.Free;
ini.Free;
end;
end;
end.