Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
AppContext.cs
Go to the documentation of this file.
4using System.IO;
10
11namespace System;
12
13public static class AppContext
14{
16
18
19 private static string s_defaultBaseDirectory;
20
21 public static string BaseDirectory => (GetData("APP_CONTEXT_BASE_DIRECTORY") as string) ?? s_defaultBaseDirectory ?? (s_defaultBaseDirectory = GetBaseDirectoryCore());
22
23 public static string? TargetFrameworkName => Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName;
24
26
28
29 public static event EventHandler? ProcessExit;
30
31 public static object? GetData(string name)
32 {
33 if (name == null)
34 {
35 throw new ArgumentNullException("name");
36 }
37 if (s_dataStore == null)
38 {
39 return null;
40 }
42 {
43 s_dataStore.TryGetValue(name, out var value);
44 return value;
45 }
46 }
47
48 public static void SetData(string name, object? data)
49 {
50 if (name == null)
51 {
52 throw new ArgumentNullException("name");
53 }
54 if (s_dataStore == null)
55 {
57 }
59 {
60 s_dataStore[name] = data;
61 }
62 }
63
73
74 public static bool TryGetSwitch(string switchName, out bool isEnabled)
75 {
76 if (switchName == null)
77 {
78 throw new ArgumentNullException("switchName");
79 }
80 if (switchName.Length == 0)
81 {
82 throw new ArgumentException(SR.Argument_EmptyName, "switchName");
83 }
84 if (s_switches != null)
85 {
87 {
88 if (s_switches.TryGetValue(switchName, out isEnabled))
89 {
90 return true;
91 }
92 }
93 }
94 if (GetData(switchName) is string value && bool.TryParse(value, out isEnabled))
95 {
96 return true;
97 }
98 isEnabled = false;
99 return false;
100 }
101
102 public static void SetSwitch(string switchName, bool isEnabled)
103 {
104 if (switchName == null)
105 {
106 throw new ArgumentNullException("switchName");
107 }
108 if (switchName.Length == 0)
109 {
110 throw new ArgumentException(SR.Argument_EmptyName, "switchName");
111 }
112 if (s_switches == null)
113 {
115 }
117 {
119 }
120 }
121
122 internal unsafe static void Setup(char** pNames, char** pValues, int count)
123 {
125 for (int i = 0; i < count; i++)
126 {
127 s_dataStore.Add(new string(pNames[i]), new string(pValues[i]));
128 }
129 }
130
131 [UnconditionalSuppressMessage("SingleFile", "IL3000: Avoid accessing Assembly file path when publishing as a single file", Justification = "Single File apps should always set APP_CONTEXT_BASE_DIRECTORY therefore code handles Assembly.Location equals null")]
132 private static string GetBaseDirectoryCore()
133 {
134 string path = Assembly.GetEntryAssembly()?.Location;
135 string text = Path.GetDirectoryName(path);
136 if (text == null)
137 {
138 return string.Empty;
139 }
141 {
142 text += "\\";
143 }
144 return text;
145 }
146
148 {
149 if (s_switches != null)
150 {
152 {
154 {
155 ev.LogAppContextSwitch(s_switch.Key, s_switch.Value ? 1 : 0);
156 }
157 }
158 }
159 if (s_dataStore == null)
160 {
161 return;
162 }
164 {
165 if (s_switches != null)
166 {
168 {
170 return;
171 }
172 }
173 LogDataStore(ev, null);
174 }
176 {
178 {
179 if (item.Value is string value && bool.TryParse(value, out var result) && (switches == null || !switches.ContainsKey(item.Key)))
180 {
181 ev.LogAppContextSwitch(item.Key, result ? 1 : 0);
182 }
183 }
184 }
185 }
186}
static string BaseDirectory
Definition AppContext.cs:21
static string GetBaseDirectoryCore()
static ? EventHandler ProcessExit
Definition AppContext.cs:29
static void OnProcessExit()
Definition AppContext.cs:64
static ? object GetData(string name)
Definition AppContext.cs:31
static void SetSwitch(string switchName, bool isEnabled)
static string s_defaultBaseDirectory
Definition AppContext.cs:19
static ? string TargetFrameworkName
Definition AppContext.cs:23
static void LogSwitchValues(RuntimeEventSource ev)
static Dictionary< string, object > s_dataStore
Definition AppContext.cs:15
static ? EventHandler< FirstChanceExceptionEventArgs > FirstChanceException
Definition AppContext.cs:27
static ? UnhandledExceptionEventHandler UnhandledException
Definition AppContext.cs:25
static void SetData(string name, object? data)
Definition AppContext.cs:48
static unsafe void Setup(char **pNames, char **pValues, int count)
static Dictionary< string, bool > s_switches
Definition AppContext.cs:17
static bool TryGetSwitch(string switchName, out bool isEnabled)
Definition AppContext.cs:74
static AppDomain CurrentDomain
Definition AppDomain.cs:28
static readonly EventArgs Empty
Definition EventArgs.cs:9
static bool EndsInDirectorySeparator(ReadOnlySpan< char > path)
Definition Path.cs:848
static ? string GetDirectoryName(string? path)
Definition Path.cs:121
static ? Assembly GetEntryAssembly()
Definition Assembly.cs:486
static string Argument_EmptyName
Definition SR.cs:584
Definition SR.cs:7
static int CompareExchange(ref int location1, int value, int comparand)
delegate void UnhandledExceptionEventHandler(object sender, UnhandledExceptionEventArgs e)