Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
StartupHookProvider.cs
Go to the documentation of this file.
3using System.IO;
6
7namespace System;
8
9internal static class StartupHookProvider
10{
11 private struct StartupHookNameOrPath
12 {
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 }
37 {
39 }
40 if (!(AppContext.GetData("STARTUP_HOOKS") is string text))
41 {
42 return;
43 }
44 Span<char> span = stackalloc char[4]
45 {
48 ' ',
49 ','
50 };
51 ReadOnlySpan<char> readOnlySpan = span;
52 string[] array = text.Split(Path.PathSeparator);
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 {
71 }
72 }
73 if (text2.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
74 {
76 }
77 try
78 {
79 array2[i].AssemblyName = new AssemblyName(text2);
80 }
81 catch (Exception innerException)
82 {
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 }
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}
static ? object GetData(string name)
Definition AppContext.cs:31
static bool TryGetSwitch(string switchName, out bool isEnabled)
Definition AppContext.cs:74
static readonly char PathSeparator
Definition Path.cs:77
static readonly char AltDirectorySeparatorChar
Definition Path.cs:73
static readonly char DirectorySeparatorChar
Definition Path.cs:71
static bool IsPathFullyQualified(string path)
Definition Path.cs:264
virtual ? Type GetType(string name)
Definition Assembly.cs:305
object? Invoke(object? obj, object?[]? parameters)
static string Argument_InvalidStartupHookSignature
Definition SR.cs:740
static string Argument_InvalidStartupHookSimpleAssemblyName
Definition SR.cs:2114
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Argument_StartupHookAssemblyLoadFailed
Definition SR.cs:2116
Definition SR.cs:7
static void CallStartupHook(StartupHookNameOrPath startupHook)
static readonly char Delimiter
Definition Type.cs:17
static readonly Type[] EmptyTypes
Definition Type.cs:19