Terraria
v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
StartupHookProvider.cs
Go to the documentation of this file.
1
using
System.Diagnostics.CodeAnalysis
;
2
using
System.Diagnostics.Tracing
;
3
using
System.IO
;
4
using
System.Reflection
;
5
using
System.Runtime.Loader
;
6
7
namespace
System
;
8
9
internal
static
class
StartupHookProvider
10
{
11
private
struct
StartupHookNameOrPath
12
{
13
public
AssemblyName
AssemblyName
;
14
15
public
string
Path
;
16
}
17
18
private
static
bool
IsSupported
19
{
20
get
21
{
22
if
(!
AppContext
.
TryGetSwitch
(
"System.StartupHookProvider.IsSupported"
, out var isEnabled))
23
{
24
return
true
;
25
}
26
return
isEnabled;
27
}
28
}
29
30
private
static
void
ProcessStartupHooks
()
31
{
32
if
(!
IsSupported
)
33
{
34
return
;
35
}
36
if
(
EventSource
.
IsSupported
)
37
{
38
RuntimeEventSource
.
Initialize
();
39
}
40
if
(!(
AppContext
.
GetData
(
"STARTUP_HOOKS"
) is
string
text
))
41
{
42
return
;
43
}
44
Span<char>
span = stackalloc
char
[4]
45
{
46
Path
.
DirectorySeparatorChar
,
47
Path
.
AltDirectorySeparatorChar
,
48
' '
,
49
','
50
};
51
ReadOnlySpan<char>
readOnlySpan = span;
52
string
[]
array
=
text
.Split(
Path
.
PathSeparator
);
53
StartupHookNameOrPath
[] array2 =
new
StartupHookNameOrPath
[
array
.Length];
54
for
(
int
i = 0; i <
array
.Length; i++)
55
{
56
string
text2 =
array
[i];
57
if
(
string
.IsNullOrEmpty(text2))
58
{
59
continue
;
60
}
61
if
(
Path
.
IsPathFullyQualified
(text2))
62
{
63
array2[i].
Path
= text2;
64
continue
;
65
}
66
for
(
int
j = 0; j < readOnlySpan.
Length
; j++)
67
{
68
if
(text2.Contains(readOnlySpan[j]))
69
{
70
throw
new
ArgumentException
(
SR
.
Format
(
SR
.
Argument_InvalidStartupHookSimpleAssemblyName
, text2));
71
}
72
}
73
if
(text2.EndsWith(
".dll"
,
StringComparison
.OrdinalIgnoreCase))
74
{
75
throw
new
ArgumentException
(
SR
.
Format
(
SR
.
Argument_InvalidStartupHookSimpleAssemblyName
, text2));
76
}
77
try
78
{
79
array2[i].
AssemblyName
=
new
AssemblyName
(text2);
80
}
81
catch
(
Exception
innerException)
82
{
83
throw
new
ArgumentException
(
SR
.
Format
(
SR
.
Argument_InvalidStartupHookSimpleAssemblyName
, text2), innerException);
84
}
85
}
86
StartupHookNameOrPath
[] array3 = array2;
87
foreach
(
StartupHookNameOrPath
startupHook
in
array3)
88
{
89
CallStartupHook
(startupHook);
90
}
91
}
92
93
[RequiresUnreferencedCode(
"The StartupHookSupport feature switch has been enabled for this app which is being trimmed. Startup hook code is not observable by the trimmer and so required assemblies, types and members may be removed"
)]
94
private
static
void
CallStartupHook
(
StartupHookNameOrPath
startupHook)
95
{
96
Assembly
assembly;
97
try
98
{
99
if
(startupHook.
Path
!=
null
)
100
{
101
assembly =
AssemblyLoadContext
.
Default
.LoadFromAssemblyPath(startupHook.
Path
);
102
}
103
else
104
{
105
if
(startupHook.
AssemblyName
==
null
)
106
{
107
return
;
108
}
109
assembly =
AssemblyLoadContext
.
Default
.LoadFromAssemblyName(startupHook.
AssemblyName
);
110
}
111
}
112
catch
(
Exception
innerException)
113
{
114
throw
new
ArgumentException
(
SR
.
Format
(
SR
.
Argument_StartupHookAssemblyLoadFailed
, startupHook.
Path
?? startupHook.
AssemblyName
.
ToString
()), innerException);
115
}
116
Type
type
= assembly.
GetType
(
"StartupHook"
, throwOnError:
true
);
117
MethodInfo
method =
type
.GetMethod(
"Initialize"
,
BindingFlags
.Static |
BindingFlags
.Public |
BindingFlags
.NonPublic,
null
,
Type
.
EmptyTypes
,
null
);
118
bool
flag =
false
;
119
if
(method ==
null
)
120
{
121
try
122
{
123
method =
type
.GetMethod(
"Initialize"
,
BindingFlags
.Instance |
BindingFlags
.Static |
BindingFlags
.Public |
BindingFlags
.NonPublic);
124
}
125
catch
(
AmbiguousMatchException
)
126
{
127
}
128
if
(!(method !=
null
))
129
{
130
throw
new
MissingMethodException
(
"StartupHook"
,
"Initialize"
);
131
}
132
flag =
true
;
133
}
134
else
if
(method.
ReturnType
!= typeof(
void
))
135
{
136
flag =
true
;
137
}
138
if
(flag)
139
{
140
throw
new
ArgumentException
(
SR
.
Format
(
SR
.
Argument_InvalidStartupHookSignature
,
"StartupHook"
+
Type
.
Delimiter
+
"Initialize"
, startupHook.
Path
?? startupHook.
AssemblyName
.
ToString
()));
141
}
142
method.
Invoke
(
null
,
null
);
143
}
144
}
System.AppContext.GetData
static ? object GetData(string name)
Definition
AppContext.cs:31
System.AppContext.TryGetSwitch
static bool TryGetSwitch(string switchName, out bool isEnabled)
Definition
AppContext.cs:74
System.AppContext
Definition
AppContext.cs:14
System.ArgumentException
Definition
ArgumentException.cs:9
System.Diagnostics.Tracing.EventSource.IsSupported
static bool IsSupported
Definition
EventSource.cs:246
System.Diagnostics.Tracing.EventSource
Definition
EventSource.cs:19
System.Diagnostics.Tracing.RuntimeEventSource.Initialize
static void Initialize()
Definition
RuntimeEventSource.cs:80
System.Diagnostics.Tracing.RuntimeEventSource
Definition
RuntimeEventSource.cs:9
System.Exception
Definition
Exception.cs:15
System.IO.Path.PathSeparator
static readonly char PathSeparator
Definition
Path.cs:77
System.IO.Path.AltDirectorySeparatorChar
static readonly char AltDirectorySeparatorChar
Definition
Path.cs:73
System.IO.Path.DirectorySeparatorChar
static readonly char DirectorySeparatorChar
Definition
Path.cs:71
System.IO.Path.IsPathFullyQualified
static bool IsPathFullyQualified(string path)
Definition
Path.cs:264
System.IO.Path
Definition
Path.cs:8
System.MissingMethodException
Definition
MissingMethodException.cs:9
System.Reflection.AmbiguousMatchException
Definition
AmbiguousMatchException.cs:9
System.Reflection.AssemblyName.ToString
override string ToString()
Definition
AssemblyName.cs:374
System.Reflection.AssemblyName
Definition
AssemblyName.cs:12
System.Reflection.Assembly.GetType
virtual ? Type GetType(string name)
Definition
Assembly.cs:305
System.Reflection.Assembly
Definition
Assembly.cs:16
System.Reflection.MethodBase.Invoke
object? Invoke(object? obj, object?[]? parameters)
Definition
MethodBase.cs:203
System.Reflection.MethodInfo.ReturnType
virtual Type ReturnType
Definition
MethodInfo.cs:19
System.Reflection.MethodInfo
Definition
MethodInfo.cs:7
System.Runtime.Loader.AssemblyLoadContext.Default
static AssemblyLoadContext Default
Definition
AssemblyLoadContext.cs:94
System.Runtime.Loader.AssemblyLoadContext
Definition
AssemblyLoadContext.cs:15
System.SR.Argument_InvalidStartupHookSignature
static string Argument_InvalidStartupHookSignature
Definition
SR.cs:740
System.SR.Argument_InvalidStartupHookSimpleAssemblyName
static string Argument_InvalidStartupHookSimpleAssemblyName
Definition
SR.cs:2114
System.SR.Format
static string Format(string resourceFormat, object p1)
Definition
SR.cs:118
System.SR.Argument_StartupHookAssemblyLoadFailed
static string Argument_StartupHookAssemblyLoadFailed
Definition
SR.cs:2116
System.SR
Definition
SR.cs:7
System.StartupHookProvider.CallStartupHook
static void CallStartupHook(StartupHookNameOrPath startupHook)
Definition
StartupHookProvider.cs:94
System.StartupHookProvider.IsSupported
static bool IsSupported
Definition
StartupHookProvider.cs:19
System.StartupHookProvider.ProcessStartupHooks
static void ProcessStartupHooks()
Definition
StartupHookProvider.cs:30
System.StartupHookProvider
Definition
StartupHookProvider.cs:10
System.Type.Delimiter
static readonly char Delimiter
Definition
Type.cs:17
System.Type.EmptyTypes
static readonly Type[] EmptyTypes
Definition
Type.cs:19
System.Type
Definition
Type.cs:14
System.Diagnostics.CodeAnalysis
Definition
AllowNullAttribute.cs:1
System.Diagnostics.Tracing
Definition
ActivityTracker.cs:4
System.IO
Definition
ConsoleStream.cs:3
System.Reflection.BindingFlags
BindingFlags
Definition
BindingFlags.cs:5
System.Reflection
Definition
ICustomTypeProvider.cs:1
System.Runtime.Loader
Definition
AssemblyDependencyResolver.cs:8
System.ExceptionArgument.text
@ text
System.ExceptionArgument.type
@ type
System.ExceptionArgument.array
@ array
System.StringComparison
StringComparison
Definition
StringComparison.cs:4
System
Definition
BlockingCollection.cs:8
System.ReadOnlySpan.Length
int Length
Definition
ReadOnlySpan.cs:70
System.ReadOnlySpan
Definition
ReadOnlySpan.cs:14
System.Span
Definition
Span.cs:14
System.StartupHookProvider.StartupHookNameOrPath.Path
string Path
Definition
StartupHookProvider.cs:15
System.StartupHookProvider.StartupHookNameOrPath.AssemblyName
AssemblyName AssemblyName
Definition
StartupHookProvider.cs:13
System.StartupHookProvider.StartupHookNameOrPath
Definition
StartupHookProvider.cs:12
source
System.Private.CoreLib
System
StartupHookProvider.cs
Generated by
1.10.0