forked from dim-s/soulengine
-
Notifications
You must be signed in to change notification settings - Fork 2
/
UnitClass.pas
195 lines (157 loc) · 3.8 KB
/
UnitClass.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
unit UnitClass;
interface
uses Windows, Generics.Collections, Rtti, TypInfo, System.SysUtils, vcl.dialogs;
Type
UnitArray = array of UTF8String;
TUnitType = record
lowerName, lowerName2: string;
base: PTypeInfo;
end;
TUnitTypeArray = array of TUnitType;
TUnitClass = record
private
TypeCount, UnitsCount: Integer;
Test: TUnitTypeArray;
Units: UnitArray;
public
Rtti: TRttiContext;
function GetUnits: UnitArray;
function IsUnits(UnitName: string): Boolean;
procedure PTypeInfoUpdute();
function FindTypeInfo(ClassName: string): PTypeInfo; overload;
function FindTypeInfo(TType: PTypeInfo): string; overload;
function FindType(ClassName: string): TRttiType; overload;
function FindType(TType: PTypeInfo): TRttiType; overload;
function GetList(): TUnitTypeArray;
end;
var
TClassUnits: TUnitClass;
implementation
function TUnitClass.GetList(): TUnitTypeArray;
begin
if TypeCount <> LibModuleList^.TypeInfo^.TypeCount then
PTypeInfoUpdute();
Result := Test;
end;
procedure TUnitClass.PTypeInfoUpdute();
var
T: TPackageTypeInfo;
Units: UnitArray;
currUnit: Integer;
I, r, idx: Integer;
typeIter: PPTypeInfo;
typeName, tmp: string;
begin
T := LibModuleList^.TypeInfo^;
TypeCount := T.TypeCount;
idx := 0;
currUnit := 0;
Units := GetUnits;
FillChar(Test, sizeof(TUnitTypeArray), 0);
SetLength(Test, TypeCount);
for I := 0 to TypeCount - 1 do
begin
typeIter := T.TypeTable^[I];
if typeIter = nil then
Continue;
if IntPtr(typeIter) = 1 then
begin
Inc(currUnit);
Continue;
end;
if typeIter^ = nil then
Continue;
typeName := Units[currUnit] + '.' + typeIter^^.NameFld.ToString;
tmp := '';
for r := 1 to Length(typeName) do
begin
case typeName[r] of
',':
tmp := tmp + '.';
'.':
tmp := tmp + '\';
'<', '>':
;
else
tmp := tmp + typeName[r];
end;
end;
Test[idx].lowerName := Lowercase(tmp);
Test[idx].lowerName2 := Lowercase(typeIter^^.NameFld.ToString);
Test[idx].base := typeIter^;
Inc(idx);
end;
SetLength(Test, idx);
end;
function TUnitClass.FindTypeInfo(ClassName: string): PTypeInfo;
var
v: TUnitType;
begin
ClassName := Lowercase(ClassName);
for v in GetList do
begin
if (v.lowerName = ClassName) or (v.lowerName2 = ClassName) then
exit(v.base);
end;
Result := nil;
end;
function TUnitClass.FindTypeInfo(TType: PTypeInfo): string;
var
v: TUnitType;
begin
for v in GetList do
begin
if v.base = TType then
exit(v.lowerName);
end;
Result := '';
end;
function TUnitClass.FindType(ClassName: string): TRttiType;
begin
Result := Rtti.GetType(FindTypeInfo(ClassName));
end;
function TUnitClass.FindType(TType: PTypeInfo): TRttiType;
begin
Result := Rtti.GetType(TType);
end;
function TUnitClass.GetUnits: UnitArray;
var
P: Pointer;
I, len, Offset: Integer;
T: TPackageTypeInfo;
begin
T := LibModuleList^.TypeInfo^;
if UnitsCount <> T.UnitCount then
begin
UnitsCount := T.UnitCount;
SetLength(Units, UnitsCount);
P := Pointer(T.UnitNames);
Offset := 0;
for I := 0 to UnitsCount - 1 do
begin
len := PByte(NativeInt(P) + Offset)^;
if len = 0 then
Continue;
Inc(Offset, 1);
SetLength(Units[I], len);
Move(Pointer(NativeInt(P) + Offset)^, Units[I][1], len);
Inc(Offset, len);
end;
end;
Result := Units;
end;
function TUnitClass.IsUnits(UnitName: string): Boolean;
var
TUnitName: UTF8String;
begin
UnitName := Lowercase(UnitName);
for TUnitName in GetUnits do
if Lowercase(String(TUnitName)) = UnitName then
exit(true);
Result := false;
end;
initialization
finalization
SetLength(TClassUnits.Units, 0);
SetLength(TClassUnits.Test, 0);
end.