This repository has been archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Utils.cs
199 lines (188 loc) · 6.76 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Microsoft.Win32;
using System.Text;
namespace TrifleJS
{
/// <summary>
/// Various tools to make life easier
/// </summary>
public class Utils
{
/// <summary>
/// Debug message to console
/// </summary>
/// <param name="message"></param>
/// <param name="args"></param>
public static void Debug(object message, params object[] args)
{
if (Program.Verbose)
{
ConsoleColor normalColor = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.DarkMagenta;
if (args != null)
{
Console.WriteLine(String.Format(message as string, args));
}
else
{
Console.WriteLine(message);
}
Console.ForegroundColor = normalColor;
}
}
/// <summary>
/// Tries reading a registry key for the current user, returns null if not found
/// </summary>
/// <param name="path"></param>
/// <param name="name"></param>
/// <returns></returns>
public static object TryReadRegistryKey(string path, string name)
{
return TryReadRegistryKey(Registry.CurrentUser, path, name);
}
/// <summary>
/// Tries reading a registry key using a specific path, returns null if not found
/// </summary>
/// <param name="path"></param>
/// <param name="name"></param>
/// <returns></returns>
public static object TryReadRegistryKey(RegistryKey root, string path, string name)
{
try
{
// Use current user, no need for admin permissions
RegistryKey key = root.OpenSubKey(path);
if (key != null)
{
return key.GetValue(name);
}
}
catch { }
return null;
}
/// <summary>
/// Tries to write a key to registry catching any exceptions, returns true if the key was written successfully
/// </summary>
/// <param name="path"></param>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="kind"></param>
/// <returns></returns>
public static bool TryWriteRegistryKey(string path, string name, object value, RegistryValueKind kind)
{
try
{
// Use current user, no need for admin permissions
RegistryKey key = Registry.CurrentUser.OpenSubKey(path, true);
if (key == null)
{
key = CreateRegistryPath(Registry.CurrentUser, path);
}
key.SetValue(name, value, kind);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Creates a path in the registry, including all necessary parent paths
/// </summary>
/// <param name="key"></param>
/// <param name="path"></param>
/// <returns></returns>
private static RegistryKey CreateRegistryPath(RegistryKey key, string path)
{
foreach (string name in path.Split('\\'))
{
RegistryKey subkey = key.OpenSubKey(name, true);
if (subkey == null)
{
key.CreateSubKey(name);
subkey = key.OpenSubKey(name, true);
}
key = subkey;
};
return key;
}
/// <summary>
/// Converts a COM object to an array of objects
/// </summary>
/// <param name="comObject"></param>
/// <returns></returns>
public static object[] COMToArray(object comObject)
{
List<object> output = new List<object>();
int? length = (int?)(comObject.GetType()).InvokeMember("length", System.Reflection.BindingFlags.GetProperty, null, comObject, null);
for (int i = 0; i < length; i++)
{
output.Add(comObject.GetType().InvokeMember(String.Format("{0}", i), BindingFlags.GetProperty, null, comObject, null));
}
return output.ToArray();
}
/// <summary>
/// Converts a COM object to dictionary (useful for outputting error information)
/// </summary>
/// <param name="comObject"></param>
/// <returns></returns>
public static Dictionary<string, object> COMToErrorObject(object comObject)
{
Dictionary<string, object> output = new Dictionary<string, object>();
PropertyInfo[] props = comObject.GetType().GetProperties();
foreach (PropertyInfo prop in props)
{
output.Add(prop.Name, comObject.GetType().InvokeMember(prop.Name, BindingFlags.GetProperty, null, comObject, null));
}
return output;
}
/// <summary>
/// Serialises an object to JSON string
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static string Serialize(object obj)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(obj, Formatting.Indented);
}
/// <summary>
/// Deserialises a JSON string into an object
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static object Deserialize(string json)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
}
/// <summary>
/// Returns a unique ID string (16^8 = 4.3 trillion combinations)
/// </summary>
/// <returns></returns>
public static string NewUid()
{
return Guid.NewGuid().ToString().Substring(0, 8);
}
/// <summary>
/// Note that windows uses "UTF-8" instead of "UTF8"
/// so we have to sanitize these strings
/// </summary>
internal static Encoding GetEncoding(string encoding)
{
if (encoding != null)
{
if (encoding.IndexOf("utf", StringComparison.InvariantCultureIgnoreCase) == 0
&& !encoding.Contains("-"))
{
encoding = Regex.Replace(encoding, "utf", "UTF-", RegexOptions.IgnoreCase);
}
return Encoding.GetEncoding(encoding);
}
return Encoding.UTF8;
}
}
}