-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTools.cs
239 lines (216 loc) · 11.1 KB
/
Tools.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using Microsoft.Win32;
using System;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Windows.Forms;
using System.Resources;
using System.Collections;
using System.Reflection;
using System.IO;
using System.Xml;
using RConControl.Properties;
using System.Xml.Serialization;
using System.Configuration;
using System.Diagnostics;
namespace RConControl {
public class Tools {
/// <summary>
/// Get CultureInfo type of native name
/// </summary>
public static CultureInfo GetCultureByNativeName(String nativeName) {
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.NeutralCultures);
var culture = cultures.FirstOrDefault(c =>
c.NativeName.Equals(nativeName, StringComparison.InvariantCultureIgnoreCase));
return culture;
}
/// <summary>
/// Get CultureInfo type of Two letter ISO
/// </summary>
public static CultureInfo GetCultureByTwoLetterISO(String twoLetter) {
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.NeutralCultures);
var culture = cultures.FirstOrDefault(c =>
c.TwoLetterISOLanguageName.Equals(twoLetter, StringComparison.InvariantCultureIgnoreCase));
return culture;
}
/// <summary>
/// Set Autorun in case of Settings value
/// </summary>
public static void SetAutorun() {
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey(GlobalConstants.AUTORUN_REGKEY, true);
if (Settings.Default.Autorun) rkApp.SetValue(Application.ProductName, Application.ExecutablePath.ToString());
else rkApp.DeleteValue(Application.ProductName, false);
}
/// <summary>
/// Set Autorun in case of Settings value
/// </summary>
public static bool GetAutorun() {
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey(GlobalConstants.AUTORUN_REGKEY, true);
string regKey = (string)rkApp.GetValue(Application.ProductName);
if (Settings.Default.Autorun) {
if (regKey != null && regKey.Contains(Application.ExecutablePath.ToString())) {
return true;
} else {
Settings.Default.Autorun = false;
}
}
return false;
}
/// <summary>
/// Get all config files in config folder and load its content
/// </summary>
/// <returns>List with ConfigFiles</returns>
public static List<ConfigFile> GetAllConfigFiles() {
List<ConfigFile> resultList = new List<ConfigFile>();
string path = Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), GlobalConstants.PATH_CONFIGS);
try {
if (Directory.Exists(path)) {
string[] fileList = Directory.GetFiles(path, "*.cfg");
foreach (string file in fileList) {
string[] readFile = File.ReadAllLines(file);
if (readFile.Length > 0) {
string name = null;
StringBuilder fileContent = new StringBuilder();
foreach (string readLine in readFile) {
string line = readLine.Trim();
if (line.IndexOf("//") == 0 && line.Contains("Name:")) {
int startIdx = line.IndexOf(':') + 1;
name = line.Substring(startIdx, line.Length - startIdx);
} else if (name == null) {
name = file;
}
if (line.IndexOf("//") != 0 && !String.IsNullOrEmpty(line)) {
fileContent.AppendLine(line);
}
}
if (name != null && fileContent.Length > 0) {
resultList.Add(new ConfigFile { name = name, content = fileContent.ToString().TrimEnd('\r', '\n') });
}
}
}
} else {
throw new Exception("directory does not exist");
}
} catch (Exception ex) {
throw ex;
}
return resultList;
}
/// <summary>
/// Check if configs folder exists. if not, configs where extracted
/// </summary>
public static void CheckForConfigs() {
string path = Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), GlobalConstants.PATH_CONFIGS);
if (!Directory.Exists(path)) ExtractConfigFiles();
}
/// <summary>
/// Extract config files that are embedded
/// </summary>
private static void ExtractConfigFiles() {
byte[] buffer = new byte[2048];
int read;
Assembly assembly = Assembly.GetExecutingAssembly();
string[] names = assembly.GetManifestResourceNames();
string pathToExtract = Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), GlobalConstants.PATH_CONFIGS);
Directory.CreateDirectory(pathToExtract);
foreach (string name in names) {
if (name.Contains(GlobalConstants.PATH_CONFIGS_RESOURCE)) {
int firstIdx = name.LastIndexOf(GlobalConstants.PATH_CONFIGS_RESOURCE);
string fileName = name.Substring(firstIdx + GlobalConstants.PATH_CONFIGS_RESOURCE.Length);
using (BinaryReader reader = new BinaryReader(assembly.GetManifestResourceStream(name))) {
using (BinaryWriter writer = new BinaryWriter(File.OpenWrite(Path.Combine(GlobalConstants.PATH_CONFIGS, fileName)))) {
while ((read = reader.Read(buffer, 0, buffer.Length)) > 0) {
writer.Write(buffer, 0, read);
}
writer.Close();
}
reader.Close();
}
}
}
}
/// <summary>
/// Key Value Pair Class for SettingsSet
/// </summary>
/// <typeparam name="T">Type for value</typeparam>
public class SettingsKeyValue<T> {
[XmlElement(ElementName = "Key")]
public string Key { get; set; }
[XmlElement(ElementName = "Value")]
public T Value { get; set; }
}
/// <summary>
/// Settings Set class
/// </summary>
public class SettingsSet {
[XmlElement(ElementName = "Strings")]
public List<SettingsKeyValue<string>> mValuesString = new List<SettingsKeyValue<string>>();
[XmlElement(ElementName = "Integer")]
public List<SettingsKeyValue<int>> mValuesInt = new List<SettingsKeyValue<int>>();
[XmlElement(ElementName = "Bool")]
public List<SettingsKeyValue<bool>> mValuesBool = new List<SettingsKeyValue<bool>>();
[XmlElement(ElementName = "ConfigFiles")]
public List<SettingsKeyValue<ConfigFile>> mValuesConfigFile = new List<SettingsKeyValue<ConfigFile>>();
[XmlElement(ElementName = "HotKeys")]
public List<SettingsKeyValue<HotKeyObject>> mValuesHotKeyData = new List<SettingsKeyValue<HotKeyObject>>();
private Language mLangMan = Language.Instance;
public void ReadSettings() {
foreach (SettingsProperty property in Settings.Default.Properties) {
if (property.PropertyType == typeof(string)) {
mValuesString.Add(new SettingsKeyValue<string> { Key = property.Name, Value = (string)Settings.Default[property.Name] });
} else if (property.PropertyType == typeof(int)) {
mValuesInt.Add(new SettingsKeyValue<int> { Key = property.Name, Value = (int)Settings.Default[property.Name] });
} else if (property.PropertyType == typeof(bool)) {
mValuesBool.Add(new SettingsKeyValue<bool> { Key = property.Name, Value = (bool)Settings.Default[property.Name] });
} else if (property.PropertyType == typeof(ConfigFile)) {
mValuesConfigFile.Add(new SettingsKeyValue<ConfigFile> { Key = property.Name, Value = (ConfigFile)Settings.Default[property.Name] });
} else if (property.PropertyType == typeof(HotKeyObject)) {
mValuesHotKeyData.Add(new SettingsKeyValue<HotKeyObject> { Key = property.Name, Value = (HotKeyObject)Settings.Default[property.Name] });
}
}
}
public void LoadSettings() {
foreach (SettingsKeyValue<string> value in mValuesString) Settings.Default[value.Key] = value.Value;
foreach (SettingsKeyValue<int> value in mValuesInt) Settings.Default[value.Key] = value.Value;
foreach (SettingsKeyValue<bool> value in mValuesBool) Settings.Default[value.Key] = value.Value;
foreach (SettingsKeyValue<ConfigFile> value in mValuesConfigFile) Settings.Default[value.Key] = value.Value;
foreach (SettingsKeyValue<HotKeyObject> value in mValuesHotKeyData) Settings.Default[value.Key] = value.Value;
mLangMan.SwitchLang();
Tools.SetAutorun();
Settings.Default.Save();
}
}
/// <summary>
/// Writes settings to XML
/// </summary>
/// <param name="path">path to write</param>
public static void WriteXML(string path) {
try {
SettingsSet settings = new SettingsSet();
settings.ReadSettings();
XmlSerializer xmls = new XmlSerializer(settings.GetType());
using (StreamWriter writer = new StreamWriter(path)) {
xmls.Serialize(writer, settings);
}
} catch (Exception ex) {
ErrorLogger.Log(ex);
}
}
/// <summary>
/// Read XML file and loads settings
/// </summary>
/// <param name="path">path to read</param>
public static void ReadXML(string path) {
try {
XmlSerializer xmls = new XmlSerializer(typeof(SettingsSet));
using (StreamReader file = new StreamReader(path)) {
SettingsSet settings = (SettingsSet)xmls.Deserialize(file);
settings.LoadSettings();
}
} catch (Exception ex) {
ErrorLogger.Log(ex);
}
}
}
}