Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
AppContextConfigHelper.cs
Go to the documentation of this file.
2
3namespace System;
4
5internal static class AppContextConfigHelper
6{
7 internal static bool GetBooleanConfig(string configName, bool defaultValue)
8 {
9 if (!AppContext.TryGetSwitch(configName, out var isEnabled))
10 {
11 return defaultValue;
12 }
13 return isEnabled;
14 }
15
16 internal static bool GetBooleanConfig(string switchName, string envVariable, bool defaultValue = false)
17 {
18 if (!AppContext.TryGetSwitch(switchName, out var isEnabled))
19 {
20 string environmentVariable = Environment.GetEnvironmentVariable(envVariable);
21 return (environmentVariable == null) ? defaultValue : (bool.IsTrueStringIgnoreCase(environmentVariable) || environmentVariable.Equals("1"));
22 }
23 return isEnabled;
24 }
25
26 internal static int GetInt32Config(string configName, int defaultValue, bool allowNegative = true)
27 {
28 try
29 {
30 object data = AppContext.GetData(configName);
31 int num = defaultValue;
32 if (!(data is uint num2))
33 {
34 if (data is string text)
35 {
36 num = ((!text.StartsWith('0')) ? int.Parse(text, NumberStyles.AllowLeadingSign, NumberFormatInfo.InvariantInfo) : ((text.Length < 2 || text[1] != 'x') ? Convert.ToInt32(text, 8) : Convert.ToInt32(text, 16)));
37 }
38 else if (data is IConvertible convertible)
39 {
40 num = convertible.ToInt32(NumberFormatInfo.InvariantInfo);
41 }
42 }
43 else
44 {
45 num = (int)num2;
46 }
47 return (!allowNegative && num < 0) ? defaultValue : num;
48 }
49 catch (FormatException)
50 {
51 return defaultValue;
52 }
53 catch (OverflowException)
54 {
55 return defaultValue;
56 }
57 }
58
59 internal static short GetInt16Config(string configName, short defaultValue, bool allowNegative = true)
60 {
61 try
62 {
63 object data = AppContext.GetData(configName);
64 short num = defaultValue;
65 if (!(data is uint num2))
66 {
67 if (data is string text)
68 {
69 num = (text.StartsWith("0x") ? Convert.ToInt16(text, 16) : ((!text.StartsWith("0")) ? short.Parse(text, NumberStyles.AllowLeadingSign, NumberFormatInfo.InvariantInfo) : Convert.ToInt16(text, 8)));
70 }
71 else if (data is IConvertible convertible)
72 {
73 num = convertible.ToInt16(NumberFormatInfo.InvariantInfo);
74 }
75 }
76 else
77 {
78 num = (short)num2;
79 if ((uint)num != num2)
80 {
81 return defaultValue;
82 }
83 }
84 return (!allowNegative && num < 0) ? defaultValue : num;
85 }
86 catch (FormatException)
87 {
88 return defaultValue;
89 }
90 catch (OverflowException)
91 {
92 return defaultValue;
93 }
94 }
95}
static bool GetBooleanConfig(string switchName, string envVariable, bool defaultValue=false)
static short GetInt16Config(string configName, short defaultValue, bool allowNegative=true)
static bool GetBooleanConfig(string configName, bool defaultValue)
static int GetInt32Config(string configName, int defaultValue, bool allowNegative=true)
static ? object GetData(string name)
Definition AppContext.cs:31
static bool TryGetSwitch(string switchName, out bool isEnabled)
Definition AppContext.cs:74
static int ToInt32(object? value)
Definition Convert.cs:1320
static short ToInt16(object? value)
Definition Convert.cs:1038
static ? string GetEnvironmentVariable(string variable)