-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetting.cs
345 lines (277 loc) · 9.56 KB
/
Setting.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Data;
using System.Text;
// This is a change i've made
namespace GalaxySmash
{
public static class SettingsLocal
{
private static readonly Dictionary<string, List<string>> Settings = new Dictionary<string, List<string>>(new SettingsKeyComparer());
private static string _settingsFileName;
private static string _settingsDirectory = ".\\";
private static SettingsLoadBehavior _settingsLoadBehavior;
public enum SettingsLoadBehavior { FailIfMissing, ForceCreateNew, CreateNewIfMissing };
public enum LoadState { NewFileCreated, ExistingFileLoaded };
/// <summary>
///
/// </summary>
public static string SettingsDirectory
{
get
{
var dir = new DirectoryInfo(_settingsDirectory);
return dir.FullName;
}
set
{
if (!Directory.Exists(_settingsDirectory))
throw new Exception("Directory '" + _settingsDirectory + "' does not exist");
_settingsDirectory = value;
}
}
public static bool FileExists
{
get { return File.Exists(SettingsFileFullPath); }
}
public static string SettingsFileFullPath
{
get { return Path.Combine(SettingsDirectory, _settingsFileName); }
}
/// <summary>
///
/// </summary>
private static void CreateBlankSettingsFile()
{
var utf8 = Encoding.UTF8;
using (var sw = new StreamWriter(SettingsFileFullPath, false, utf8))
{
sw.Write("<?xml version='1.0' encoding='utf-8' ?><settings></settings>");
sw.Close();
}
}
/// <summary>
/// Loads the settings file.
/// </summary>
/// <returns>TRUE if new settings file was created, otherwise FALSE</returns>
public static LoadState Load(SettingsLoadBehavior settingsLoadBehavior, string fileName)
{
var state = LoadState.ExistingFileLoaded;
_settingsFileName = fileName;
_settingsLoadBehavior = settingsLoadBehavior;
switch (_settingsLoadBehavior)
{
case SettingsLoadBehavior.CreateNewIfMissing:
if (!FileExists)
{
CreateBlankSettingsFile();
state = LoadState.NewFileCreated;
}
break;
case SettingsLoadBehavior.ForceCreateNew:
CreateBlankSettingsFile();
state = LoadState.NewFileCreated;
break;
}
if (!FileExists)
throw new Exception("Settings file not found at: '" + SettingsFileFullPath + "'");
Settings.Clear();
using (var ds = new DataSet())
{
ds.ReadXml(SettingsFileFullPath);
// zero should be ok, since its a valid settings file just with no settings
if (ds.Tables.Count > 1)
throw new Exception("Invalid number of tables '" + ds.Tables.Count + "' in settings file");
// settings file is there, just no settings in it
if (ds.Tables.Count == 0)
return state;
DataTable table = ds.Tables[0];
foreach (DataRow row in table.Rows)
{
if (row.ItemArray.Length != 2)
throw new Exception("Invalid number of rows '" + row.ItemArray.Length + "' in setting. Details: " + row);
AddSetting((string)row[0], (string)row[1]);
}
}
return state;
}
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void UpdateSetting(string key, string value)
{
if (!Settings.ContainsKey(key))
throw new Exception(string.Format("String {0} does not exist", key));
if(Settings[key].Count != 1)
throw new Exception(string.Format("Cannot use update when the are multiple duplicate key names. Found {0} duplicate keys for '{1}'",
Settings[key].Count, key));
AddSetting(key, value, true);
}
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
public static void AddSetting(string key, string value)
{
AddSetting(key, value, false);
}
public static void EnsureSettingExists(string key, string value)
{
if (Settings.ContainsKey(key))
return;
AddSetting(key, value);
}
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
/// <param name="clearExistingValues"></param>
public static void AddSetting(string key, string value, bool clearExistingValues)
{
List<string> values;
if (!Settings.TryGetValue(key, out values))
{
values = new List<string> { value };
Settings.Add(key, values);
}
else
{
if (clearExistingValues)
{
Settings[key].Clear();
Settings[key].Add(value);
}
else if (!Settings[key].Contains(value))
{
Settings[key].Add(value);
}
}
}
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static List<string> Lookup(string key)
{
return Settings.ContainsKey(key) ? Settings[key] : new List<string>();
}
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string FirstValue(string key)
{
return Lookup(key)[0];
}
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static List<string> AllValuesAsString(string key)
{
return Lookup(key);
}
/// <summary>
///
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static List<int> AllValuesAsInt(string key)
{
var ints = new List<int>(Lookup(key).Count);
for (int a = 0; a < ints.Capacity; a++)
ints.Add(int.Parse(Lookup(key)[a]));
return ints;
}
/// <summary>
///
/// </summary>
public static void Save()
{
var xmlSettings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent = true, IndentChars = " " };
using (var xml = XmlWriter.Create(SettingsFileFullPath, xmlSettings))
{
if (xml == null)
throw new Exception("XML Writer is null");
xml.WriteStartElement("settings");
foreach (KeyValuePair<string, List<string>> s in Settings)
{
foreach (string val in s.Value)
{
xml.WriteStartElement("setting");
xml.WriteAttributeString("key", s.Key);
xml.WriteAttributeString("value", val);
xml.WriteEndElement();
}
}
xml.WriteEndElement();
xml.Close();
}
}
public static bool GetAsBool(string key)
{
return bool.Parse(FirstValue(key));
}
public static byte GetAsByte(string key)
{
return byte.Parse(FirstValue(key));
}
public static char GetAsChar(string key)
{
return char.Parse(FirstValue(key));
}
public static decimal GetAsDecimal(string key)
{
return decimal.Parse(FirstValue(key));
}
public static double GetAsDouble(string key)
{
return double.Parse(FirstValue(key));
}
public static float GetAsFloat(string key)
{
return float.Parse(FirstValue(key));
}
public static int GetAsInt(string key)
{
return int.Parse(FirstValue(key));
}
public static long GetAsLong(string key)
{
return long.Parse(FirstValue(key));
}
public static sbyte GetAsSByte(string key)
{
return sbyte.Parse(FirstValue(key));
}
public static short GetAsShort(string key)
{
return short.Parse(FirstValue(key));
}
public static string GetAsString(string key)
{
return FirstValue(key);
}
public static uint GetAsUInt(string key)
{
return uint.Parse(FirstValue(key));
}
public static ulong GetAsULong(string key)
{
return ulong.Parse(FirstValue(key));
}
public static ushort GetAsUShort(string key)
{
return ushort.Parse(FirstValue(key));
}
}
}