FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
SharpGenTools/SharpGen.Runtime/TypeDataStorage.cs at main · SharpGenTools/SharpGenTools · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
SharpGenTools
/
SharpGenTools
Public
Notifications
You must be signed in to change notification settings
Fork
60
Star
536
Code
Issues
44
Pull requests
1
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
SharpGenTools
/
SharpGen.Runtime
/
TypeDataStorage.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
193 lines (158 loc) · 6.01 KB
Breadcrumbs
SharpGenTools
/
SharpGen.Runtime
/
TypeDataStorage.cs
Copy path
File metadata and controls
193 lines (158 loc) · 6.01 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// #define FORCE_REFLECTION_ONLY
#nullable enable
using
System
;
using
System
.
Collections
.
Concurrent
;
using
System
.
Collections
.
Generic
;
using
System
.
Diagnostics
;
using
System
.
Diagnostics
.
CodeAnalysis
;
using
System
.
Linq
;
using
System
.
Reflection
;
using
System
.
Runtime
.
CompilerServices
;
using
SharpGen
.
Runtime
.
Trimming
;
namespace
SharpGen
.
Runtime
;
public
static
unsafe
class
TypeDataStorage
{
private
static
readonly
ConcurrentDictionary
<
Guid
,
IntPtr
>
vtblByGuid
=
new
(
)
;
static
TypeDataStorage
(
)
{
Storage
<
IUnknown
>
.
Guid
=
new
Guid
(
0x00000000
,
0x0000
,
0x0000
,
0xC0
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x00
,
0x46
)
;
Storage
<
IInspectable
>
.
Guid
=
new
Guid
(
0xAF86E2E0
,
0xB12D
,
0x4C6A
,
0x9C
,
0x5A
,
0xD7
,
0xAA
,
0x65
,
0x10
,
0x1E
,
0x90
)
;
Storage
<
IUnknown
>
.
SourceVtbl
=
ComObjectVtbl
.
Vtbl
;
Storage
<
IInspectable
>
.
SourceVtbl
=
InspectableVtbl
.
Vtbl
;
TypeDataRegistrationHelper
helper
=
new
(
)
;
{
helper
.
Add
(
ComObjectVtbl
.
Vtbl
)
;
helper
.
Register
<
IUnknown
>
(
)
;
}
{
helper
.
Add
(
ComObjectVtbl
.
Vtbl
)
;
helper
.
Add
(
InspectableVtbl
.
Vtbl
)
;
helper
.
Register
<
IInspectable
>
(
)
;
}
}
[
MethodImpl
(
Utilities
.
MethodAggressiveOptimization
)
]
internal
static
Guid
GetGuid
<
T
>
(
)
where
T
:
ICallbackable
{
#if
!
FORCE_REFLECTION_ONLY
ref
var
guid
=
ref
Storage
<
T
>
.
Guid
;
if
(
guid
!=
default
)
return
guid
;
#if
NETSTANDARD1_3
return
guid
=
typeof
(
T
)
.
GetTypeInfo
(
)
.
GUID
;
#else
return
guid
=
typeof
(
T
)
.
GUID
;
#endif
#else
#if
NETSTANDARD1_3
return
typeof
(
T
)
.
GetTypeInfo
(
)
.
GUID
;
#else
return
typeof
(
T
)
.
GUID
;
#endif
#endif
}
internal
static
void
Register
<
T
>
(
void
*
vtbl
)
where
T
:
ICallbackable
{
#if
!
FORCE_REFLECTION_ONLY
Register
(
GetGuid
<
T
>
(
)
,
vtbl
)
;
#endif
}
internal
static
void
Register
(
Guid
guid
,
void
*
vtbl
)
=>
vtblByGuid
[
guid
]
=
new
IntPtr
(
vtbl
)
;
internal
static
IntPtr
[
]
?
GetSourceVtbl
<
T
>
(
)
where
T
:
ICallbackable
{
#if
!
FORCE_REFLECTION_ONLY
if
(
Storage
<
T
>
.
SourceVtbl
is
{
}
storedVtbl
)
return
storedVtbl
;
#endif
return
GetSourceVtblFromReflection
(
typeof
(
T
)
)
;
}
private
static
IntPtr
[
]
?
GetSourceVtblFromReflection
(
#if
NET6_0_OR_GREATER
[
DynamicallyAccessedMembers
(
DynamicallyAccessedMemberTypes
.
PublicFields
|
DynamicallyAccessedMemberTypes
.
NonPublicFields
|
DynamicallyAccessedMemberTypes
.
PublicProperties
|
DynamicallyAccessedMemberTypes
.
NonPublicProperties
)
]
#endif
Type
type
)
{
const
string
vtbl
=
"Vtbl"
;
var
vtblAttribute
=
VtblAttribute
.
Get
(
type
)
;
Debug
.
Assert
(
vtblAttribute
is
not
null
,
$
"Type
{
type
.
FullName
}
has no Vtbl attribute"
)
;
var
vtblType
=
vtblAttribute
!
.
Type
;
#if
NETSTANDARD1_3
static
bool
Predicate
(
MemberInfo
x
)
=>
x
.
Name
==
vtbl
;
if
(
vtblType
.
GetRuntimeFields
(
)
.
FirstOrDefault
(
Predicate
)
?
.
GetValue
(
null
)
is
IntPtr
[
]
vtblFieldValue
)
{
return
vtblFieldValue
;
}
if
(
vtblType
.
GetRuntimeProperties
(
)
.
FirstOrDefault
(
Predicate
)
?
.
GetValue
(
null
)
is
IntPtr
[
]
vtblPropertyValue
)
{
return
vtblPropertyValue
;
}
#else
const
BindingFlags
flags
=
BindingFlags
.
Public
|
BindingFlags
.
NonPublic
|
BindingFlags
.
FlattenHierarchy
|
BindingFlags
.
Static
;
if
(
vtblType
.
GetField
(
vtbl
,
flags
)
?
.
GetValue
(
null
)
is
IntPtr
[
]
vtblFieldValue
)
{
return
vtblFieldValue
;
}
if
(
vtblType
.
GetProperty
(
vtbl
,
flags
)
?
.
GetValue
(
null
)
is
IntPtr
[
]
vtblPropertyValue
)
{
return
vtblPropertyValue
;
}
#endif
Debug
.
Fail
(
$
"Type
{
type
.
FullName
}
has no Vtbl field or property"
)
;
return
null
;
}
internal
static
bool
GetTargetVtbl
(
#if
NET6_0_OR_GREATER
[
DynamicallyAccessedMembers
(
DynamicallyAccessedMemberTypes
.
Interfaces
)
]
#endif
TypeInfo
type
,
out
void
*
pointer
)
{
#if
!
FORCE_REFLECTION_ONLY
if
(
vtblByGuid
.
TryGetValue
(
type
.
GUID
,
out
var
ptr
)
)
{
pointer
=
ptr
.
ToPointer
(
)
;
return
true
;
}
#endif
if
(
GetSourceVtblFromReflection
(
type
.
AsType
(
)
)
is
{
}
sourceVtbl
)
{
pointer
=
RegisterFromReflection
(
type
,
sourceVtbl
)
.
ToPointer
(
)
;
return
true
;
}
pointer
=
default
;
return
false
;
}
private
static
IntPtr
RegisterFromReflection
(
#if
NET6_0_OR_GREATER
[
DynamicallyAccessedMembers
(
DynamicallyAccessedMemberTypes
.
Interfaces
)
]
#endif
TypeInfo
type
,
IntPtr
[
]
sourceVtbl
)
{
var
callbackable
=
typeof
(
ICallbackable
)
.
GetTypeInfo
(
)
;
TypeDataRegistrationHelper
helper
=
new
(
)
;
List
<
RegisterInheritanceItem
>
items
=
new
(
)
;
foreach
(
var
iface
in
type
.
ImplementedInterfaces
)
{
var
typeInfo
=
iface
.
GetTypeInfoWithNestedPreservedInterfaces
(
)
;
if
(
callbackable
==
typeInfo
||
!
callbackable
.
IsAssignableFrom
(
typeInfo
)
)
continue
;
var
iSourceVtbl
=
GetSourceVtblFromReflection
(
iface
)
;
if
(
iSourceVtbl
is
null
)
throw
new
Exception
(
$
"Failed to reflect Vtbl out of an
{
nameof
(
ICallbackable
)
}
interface '
{
iface
.
FullName
}
'."
)
;
items
.
Add
(
new
RegisterInheritanceItem
(
typeInfo
,
typeInfo
.
ImplementedInterfaces
.
Count
(
)
,
iSourceVtbl
)
)
;
}
// TODO: properly sort items by inheritance
// TODO: verify single inheritance
items
.
Sort
(
static
(
x
,
y
)
=>
Comparer
<
int
>
.
Default
.
Compare
(
x
.
InterfaceCount
,
y
.
InterfaceCount
)
)
;
foreach
(
var
(
_
,
_
,
iSourceVtbl
)
in
items
)
helper
.
Add
(
iSourceVtbl
)
;
helper
.
Add
(
sourceVtbl
)
;
return
helper
.
Register
(
type
)
;
}
private
record
struct
RegisterInheritanceItem
(
TypeInfo
Type
,
int
InterfaceCount
,
IntPtr
[
]
SourceVtbl
)
;
[
SuppressMessage
(
"ReSharper"
,
"StaticMemberInGenericType"
)
]
[
SuppressMessage
(
"Usage"
,
"CA2211:Non-constant fields should not be visible"
)
]
public
static
class
Storage
<
T
>
where
T
:
ICallbackable
{
public
static
Guid
Guid
;
public
static
IntPtr
[
]
?
SourceVtbl
;
}
}
Back
|
FazBrowse Home
|
New Git URL