Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
RegistryKey.cs
Go to the documentation of this file.
1using System;
5using System.IO;
8
9namespace Internal.Win32;
10
11internal sealed class RegistryKey : IDisposable
12{
13 private readonly SafeRegistryHandle _hkey;
14
16 {
17 _hkey = hkey;
18 }
19
21 {
22 if (_hkey != null)
23 {
24 _hkey.Dispose();
25 }
26 }
27
28 public void DeleteValue(string name, bool throwOnMissingValue)
29 {
30 int num = Interop.Advapi32.RegDeleteValue(_hkey, name);
31 if (num == 2 || num == 206)
32 {
34 {
36 }
37 num = 0;
38 }
39 }
40
42 {
43 return new RegistryKey(new SafeRegistryHandle(hKey, ownsHandle: false));
44 }
45
46 public RegistryKey OpenSubKey(string name)
47 {
48 return OpenSubKey(name, writable: false);
49 }
50
51 public RegistryKey OpenSubKey(string name, bool writable)
52 {
54 int num = Interop.Advapi32.RegOpenKeyEx(_hkey, name, 0, writable ? 131103 : 131097, out hkResult);
55 if (num == 0 && !hkResult.IsInvalid)
56 {
57 return new RegistryKey(hkResult);
58 }
59 if (num == 5 || num == 1346)
60 {
62 }
63 return null;
64 }
65
66 public string[] GetSubKeyNames()
67 {
69 char[] array = ArrayPool<char>.Shared.Rent(256);
70 try
71 {
72 int lpcbName = array.Length;
73 int num;
74 while ((num = Interop.Advapi32.RegEnumKeyEx(_hkey, list.Count, array, ref lpcbName, null, null, null, null)) != 259)
75 {
76 if (num == 0)
77 {
78 list.Add(new string(array, 0, lpcbName));
79 lpcbName = array.Length;
80 }
81 else
82 {
83 Win32Error(num, null);
84 }
85 }
86 }
87 finally
88 {
89 ArrayPool<char>.Shared.Return(array);
90 }
91 return list.ToArray();
92 }
93
94 public string[] GetValueNames()
95 {
97 char[] array = ArrayPool<char>.Shared.Rent(100);
98 try
99 {
100 int lpcbValueName = array.Length;
101 int num;
102 while ((num = Interop.Advapi32.RegEnumValue(_hkey, list.Count, array, ref lpcbValueName, IntPtr.Zero, null, null, null)) != 259)
103 {
104 switch (num)
105 {
106 case 0:
107 list.Add(new string(array, 0, lpcbValueName));
108 break;
109 case 234:
110 {
111 char[] array2 = array;
112 int num2 = array2.Length;
113 array = null;
114 ArrayPool<char>.Shared.Return(array2);
115 array = ArrayPool<char>.Shared.Rent(checked(num2 * 2));
116 break;
117 }
118 default:
119 Win32Error(num, null);
120 break;
121 }
122 lpcbValueName = array.Length;
123 }
124 }
125 finally
126 {
127 if (array != null)
128 {
129 ArrayPool<char>.Shared.Return(array);
130 }
131 }
132 return list.ToArray();
133 }
134
135 public object GetValue(string name)
136 {
137 return GetValue(name, null);
138 }
139
140 [return: NotNullIfNotNull("defaultValue")]
141 public object GetValue(string name, object defaultValue)
142 {
143 object result = defaultValue;
144 int lpType = 0;
145 int lpcbData = 0;
146 int num = Interop.Advapi32.RegQueryValueEx(_hkey, name, (int[])null, ref lpType, (byte[])null, ref lpcbData);
147 if (num != 0 && num != 234)
148 {
149 return result;
150 }
151 if (lpcbData < 0)
152 {
153 lpcbData = 0;
154 }
155 switch (lpType)
156 {
157 case 0:
158 case 3:
159 case 5:
160 {
161 byte[] array3 = new byte[lpcbData];
163 result = array3;
164 break;
165 }
166 case 11:
167 if (lpcbData <= 8)
168 {
169 long lpData = 0L;
171 result = lpData;
172 break;
173 }
174 goto case 0;
175 case 4:
176 if (lpcbData <= 4)
177 {
178 int lpData2 = 0;
180 result = lpData2;
181 break;
182 }
183 goto case 11;
184 case 1:
185 {
186 if (lpcbData % 2 == 1)
187 {
188 try
189 {
191 }
193 {
195 }
196 }
197 char[] array4 = new char[lpcbData / 2];
199 result = ((array4.Length == 0 || array4[^1] != 0) ? new string(array4) : new string(array4, 0, array4.Length - 1));
200 break;
201 }
202 case 2:
203 {
204 if (lpcbData % 2 == 1)
205 {
206 try
207 {
209 }
211 {
213 }
214 }
215 char[] array5 = new char[lpcbData / 2];
217 result = ((array5.Length == 0 || array5[^1] != 0) ? new string(array5) : new string(array5, 0, array5.Length - 1));
218 result = Environment.ExpandEnvironmentVariables((string)result);
219 break;
220 }
221 case 7:
222 {
223 if (lpcbData % 2 == 1)
224 {
225 try
226 {
228 }
230 {
232 }
233 }
234 char[] array = new char[lpcbData / 2];
236 if (array.Length != 0 && array[^1] != 0)
237 {
238 Array.Resize(ref array, array.Length + 1);
239 }
240 string[] array2 = Array.Empty<string>();
241 int num2 = 0;
242 int num3 = 0;
243 int num4 = array.Length;
244 while (num == 0 && num3 < num4)
245 {
246 int i;
247 for (i = num3; i < num4 && array[i] != 0; i++)
248 {
249 }
250 string text = null;
251 if (i < num4)
252 {
253 if (i - num3 > 0)
254 {
255 text = new string(array, num3, i - num3);
256 }
257 else if (i != num4 - 1)
258 {
259 text = string.Empty;
260 }
261 }
262 else
263 {
264 text = new string(array, num3, num4 - num3);
265 }
266 num3 = i + 1;
267 if (text != null)
268 {
269 if (array2.Length == num2)
270 {
271 Array.Resize(ref array2, (num2 > 0) ? (num2 * 2) : 4);
272 }
273 array2[num2++] = text;
274 }
275 }
276 Array.Resize(ref array2, num2);
277 result = array2;
278 break;
279 }
280 }
281 return result;
282 }
283
284 internal void SetValue(string name, string value)
285 {
286 if (value == null)
287 {
288 throw new ArgumentNullException("value");
289 }
290 if (name != null && name.Length > 16383)
291 {
292 throw new ArgumentException(SR.Arg_RegValStrLenBug, "name");
293 }
294 int num = Interop.Advapi32.RegSetValueEx(_hkey, name, 0, 1, value, checked(value.Length * 2 + 2));
295 if (num != 0)
296 {
297 Win32Error(num, null);
298 }
299 }
300
301 internal static void Win32Error(int errorCode, string str)
302 {
303 switch (errorCode)
304 {
305 case 5:
306 if (str != null)
307 {
309 }
310 throw new UnauthorizedAccessException();
311 case 2:
313 default:
315 }
316 }
317}
static RegistryKey OpenBaseKey(IntPtr hKey)
object GetValue(string name, object defaultValue)
object GetValue(string name)
readonly SafeRegistryHandle _hkey
void DeleteValue(string name, bool throwOnMissingValue)
RegistryKey OpenSubKey(string name, bool writable)
void SetValue(string name, string value)
RegistryKey(SafeRegistryHandle hkey)
RegistryKey OpenSubKey(string name)
static void Win32Error(int errorCode, string str)
static int RegSetValueEx(SafeRegistryHandle hKey, string lpValueName, int Reserved, int dwType, string lpData, int cbData)
static int RegQueryValueEx(SafeRegistryHandle hKey, string lpValueName, int[] lpReserved, ref int lpType, [Out] byte[] lpData, ref int lpcbData)
static int RegEnumValue(SafeRegistryHandle hKey, int dwIndex, char[] lpValueName, ref int lpcbValueName, IntPtr lpReserved_MustBeZero, int[] lpType, byte[] lpData, int[] lpcbData)
static int RegEnumKeyEx(SafeRegistryHandle hKey, int dwIndex, char[] lpName, ref int lpcbName, int[] lpReserved, [Out] char[] lpClass, int[] lpcbClass, long[] lpftLastWriteTime)
static int RegOpenKeyEx(SafeRegistryHandle hKey, string lpSubKey, int ulOptions, int samDesired, out SafeRegistryHandle hkResult)
static int RegDeleteValue(SafeRegistryHandle hKey, string lpValueName)
static string GetMessage(int errorCode)
Definition Interop.cs:113
void Add(TKey key, TValue value)
static string ExpandEnvironmentVariables(string name)
static string Arg_RegGetOverflowBug
Definition SR.cs:386
static string Arg_RegSubKeyValueAbsent
Definition SR.cs:390
static string Security_RegistryPermission
Definition SR.cs:1858
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Arg_RegValStrLenBug
Definition SR.cs:392
static string UnauthorizedAccess_RegistryKeyGeneric_Key
Definition SR.cs:2026
static string Arg_RegKeyNotFound
Definition SR.cs:388
Definition SR.cs:7
static readonly IntPtr Zero
Definition IntPtr.cs:18