-
Notifications
You must be signed in to change notification settings - Fork 2
/
Settings.cs
443 lines (389 loc) · 14.7 KB
/
Settings.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Decal.Adapter;
using Decal.Adapter.Wrappers;
namespace TreeStats
{
public static class Settings
{
// Accessor for settings file
public static string settingsFile { get; set; }
// Tracked settings
public static bool autoMode;
public static bool sendLocation;
public static List<string> trackedCharacters;
public static bool useAccount;
public static string accountName;
public static string accountPassword;
public static bool isLoggedIn;
public static bool useCustomURL;
public static string customURL;
public static bool silent;
internal static void Init(string _settingsFileName)
{
try
{
settingsFile = _settingsFileName;
autoMode = false;
sendLocation = false;
trackedCharacters = new List<string>();
useAccount = false;
accountName = "";
accountPassword = "";
isLoggedIn = false;
useCustomURL = false;
customURL = "";
silent = false;
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void Destroy()
{
try
{
settingsFile = null;
trackedCharacters.Clear();
trackedCharacters = null;
accountName = null;
accountPassword = null;
customURL = null;
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void Save()
{
try
{
Logging.LogMessage("Settings::Save()");
System.IO.StreamWriter sw = new System.IO.StreamWriter(settingsFile, false);
// Write auto mode
sw.WriteLine("auto:" + autoMode.ToString());
// Write send location
sw.WriteLine("loc:" + sendLocation.ToString());
// Write account info if present
sw.WriteLine("use_account:" + useAccount.ToString());
if (accountName.Length > 0 && accountPassword.Length > 0)
{
sw.WriteLine("account:" + accountName + "#" + accountPassword);
}
// Write tracked characters
if (trackedCharacters.Count > 0)
{
// Write character list
StringBuilder characters = new StringBuilder();
characters.Append("characters:");
foreach (string key in trackedCharacters)
{
characters.AppendFormat("{0}#", key);
}
characters = characters.Remove(characters.Length - 1, 1); // Remove last #
sw.WriteLine(characters);
}
// Write custom URL
sw.WriteLine("useCustomURL:" + useCustomURL.ToString());
sw.WriteLine("customURL:" + customURL);
// Silent
sw.WriteLine("silent:" + silent.ToString());
sw.Close();
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void Load()
{
try
{
Logging.LogMessage("Settings::Load()");
if (!File.Exists(settingsFile))
{
return;
}
System.IO.StreamReader sr = new System.IO.StreamReader(settingsFile, true);
// Read in lines from the file
string line = String.Empty;
string[] tokens;
string[] keys;
while ((line = sr.ReadLine()) != null)
{
tokens = line.Split(':');
switch (tokens[0])
{
case "auto": // Auto mode setting, True | False
Logging.LogMessage("Reading auto mode setting with stored value of " + tokens[1]);
// Try to grab token[1] as a bool
if (tokens[1] == "True")
{
autoMode = true;
}
else if (tokens[1] == "False")
{
autoMode = false;
}
break;
case "loc": // Send location setting, True | False
Logging.LogMessage("Reading location setting with stored value of " + tokens[1]);
// Try to grab token[1] as a bool
if (tokens[1] == "True")
{
sendLocation = true;
}
else if (tokens[1] == "False")
{
sendLocation = false;
}
break;
case "characters": // Character keys, #-delimited
Logging.LogMessage("Reading characters setting with stored value of " + tokens[1]);
keys = tokens[1].Split('#');
foreach (string key in keys)
{
if (key.Length > 0)
{
AddCharacter(key);
}
}
break;
case "use_account": // Line 3 is whether to use an account
Logging.LogMessage("Reading use_account setting with stored value of " + tokens[1]);
// Try to grab token[1] as a bool
if (tokens[1] == "True")
{
useAccount = true;
}
else if (tokens[1] == "False")
{
useAccount = false;
}
break;
case "account": // Line 4 is account info, #-separated
Logging.LogMessage("Reading account setting with stored value of " + tokens[1]);
keys = tokens[1].Split('#');
if (keys.Length != 2 && keys[0].Length > 0 && keys[1].Length > 0)
{
break;
}
accountName = keys[0];
accountPassword = keys[1];
break;
case "useCustomURL":
if (tokens.Length == 2)
{
if (tokens[1] == "True")
{
useCustomURL = true;
}
else if (tokens[1] == "False")
{
useCustomURL = false;
}
}
break;
case "customURL":
if (tokens.Length == 2)
{
customURL = tokens[1];
}
break;
case "silent":
if (tokens.Length == 2)
{
if (tokens[1] == "True")
{
silent = true;
}
else if (tokens[1] == "False")
{
silent = false;
}
}
break;
default:
/* Handle legacy support of old settings file format:
*
* The first settings file format had no key name,
* i.e. 'characters:Kolthar al Magus-WintersEbb'
* and was instead just 'Kolthar al Magus-WintersEbb'.
* This default route should catch old settings files.
*/
keys = line.Split('#');
Logging.LogMessage("Found " + keys.Length.ToString() + " characters.");
foreach (string key in keys)
{
Logging.LogMessage(" " + key);
if (key.Length > 0)
{
AddCharacter(key);
}
}
break;
}
}
sr.Close();
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void SetAutoMode(bool state)
{
try
{
if (state == true)
{
Util.WriteToChat("Setting mode to automatic. Any characters you log into will be uploaded automatically.");
autoMode = true;
}
else
{
Util.WriteToChat("Setting mode to manual. Only characters you have explicitly added will be uploaded automatically.");
autoMode = false;
}
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void SetSendLocation(bool state)
{
try
{
if (state == true)
{
Util.WriteToChat("Setting location to visible. Character location will be uploaded.");
sendLocation = true;
}
else
{
Util.WriteToChat("Setting location to hidden. Character location will not be uploaded.");
sendLocation = false;
}
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void ToggleMode()
{
try
{
if (autoMode == true)
{
SetAutoMode(false);
}
else
{
SetAutoMode(true);
}
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void ToggleLocation()
{
try
{
if (sendLocation == true)
{
SetSendLocation(false);
}
else
{
SetSendLocation(true);
}
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void AddCharacter(string key)
{
try
{
if (!trackedCharacters.Exists(k => k == key))
{
Util.WriteToChat("Now tracking " + key + ".");
trackedCharacters.Add(key);
}
else
{
Util.WriteToChat("Already tracking " + key + ".");
}
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static void RemoveCharacter(string key)
{
try
{
if (trackedCharacters.Exists(k => k == key))
{
Util.WriteToChat("No longer tracking " + key + ".");
trackedCharacters.Remove(key);
}
else
{
Util.WriteToChat("Not already tracking " + key + ".");
}
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
internal static bool ShouldSendCharacter(string key)
{
try
{
return Settings.autoMode || Settings.trackedCharacters.Exists(k => k == key);
}
catch (Exception ex)
{
Logging.LogError(ex);
}
return false;
}
internal static void ShowHelp()
{
try
{
Util.WriteToChat("Treestats help:");
Util.WriteToChat("Available commands: help, send, mode, addchar, removechar, account");
Util.WriteToChat("");
Util.WriteToChat("help: Shows this message.");
Util.WriteToChat("send: Sends the currently logged-in character (Max once per minute), no matter what other settings you have set.");
Util.WriteToChat("mode: Toggle mode between automatic and manual tracking.");
Util.WriteToChat(" auto: Automatically upload characters.");
Util.WriteToChat(" manual: Only upload characters you manually add. See add/rem.");
Util.WriteToChat("loc: Toggle between sending or not sending character location.");
Util.WriteToChat("add: Enables tracking for the currently logged in character.");
Util.WriteToChat("rem: Removes tracking for the currently logged in character.");
Util.WriteToChat("account: Create or log into an account.");
Util.WriteToChat(" create {name} {password}: Create an account.");
Util.WriteToChat(" login {name} {password}. Log into an account.");
}
catch (Exception ex)
{
Logging.LogError(ex);
}
}
}
}