-
Notifications
You must be signed in to change notification settings - Fork 6
/
SettingsManager.cs
328 lines (289 loc) · 11.1 KB
/
SettingsManager.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using log4net;
using System.Windows;
using Nini.Config;
using KailleraNET.Util;
//http://kaillera.com/raw_server_list2.php?version=0.9
namespace KailleraNET
{
/// <summary>
/// Manages all the settings for the client, including:
/// Server List (stored in servers.txt)
/// Buddy List (stored in buddy.txt)
/// UserNames (stored in username.txt)
///
/// </summary>
///
public class SettingsManager
{
public class iniSettings
{
public bool NotifyBuddyJoinServer = true;
public bool NotifyBuddyLeaveServer = true;
public bool NotifyBuddyJoinedGame = true;
public bool NotifyBuddyLeftGame = true;
public bool NotifyPM = true;
public bool UserJoinedMessage = true;
public bool UserLeftMessage = true;
}
Dictionary<TrayFlags.PopValues, Func<bool>> flagFunctor = new Dictionary<TrayFlags.PopValues, Func<bool>>();
private void initializeDict()
{
flagFunctor.Add(TrayFlags.PopValues.buddyJoinedGame, () => { return getSettings().NotifyBuddyJoinedGame; });
flagFunctor.Add(TrayFlags.PopValues.buddyLeftGame, () => { return getSettings().NotifyBuddyLeftGame; });
flagFunctor.Add(TrayFlags.PopValues.buddyJoinedServer, () => { return getSettings().NotifyBuddyJoinServer; });
flagFunctor.Add(TrayFlags.PopValues.buddyLeftServer, () => { return getSettings().NotifyBuddyLeaveServer; });
flagFunctor.Add(TrayFlags.PopValues.pmRecieved, () => { return getSettings().NotifyPM; });
}
string servers;
string buddy;
string username;
string privMessages;
string ignore;
public static log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
IConfigSource settings = new IniConfigSource("settings.ini");
private static SettingsManager instance;
public static SettingsManager getMgr(string servers = "servers.txt", string buddy = "buddy.txt", string username = "usernames.txt", string privMessages = "pm.txt", string ignore = "ignore.txt")
{
if (instance != null) return instance;
else return new SettingsManager(servers, buddy, username, privMessages, ignore);
}
private SettingsManager()
{
initializeDict();
}
/// <summary>
/// Initializes the settings manager. The default params are the default location of the files
/// </summary>
/// <param name="servers"></param>
/// <param name="buddy"></param>
/// <param name="username"></param>
private SettingsManager(string servers = "servers.txt", string buddy = "buddy.txt", string username = "usernames.txt", string privMessages = "pm.txt", string ignore = "ignore.txt")
{
initializeDict();
log.Debug("Initializing settings manager");
this.servers = servers;
this.buddy = buddy;
this.username = username;
this.privMessages = privMessages;
this.ignore = ignore;
instance = this;
}
/// <summary>
/// Loads all saved servers
/// Throws IOException, ArrayOutOfBounds on failure - should be handled in caller class
/// </summary>
/// <returns>A list of server, ip string pairs</returns>
public List<KeyValuePair<string, string>> getServers()
{
List<KeyValuePair<string, string>> serverList = new List<KeyValuePair<string, string>>();
using (StreamReader serverReader = new StreamReader(servers))
{
while (!serverReader.EndOfStream)
{
//<SERVER_NAME>,<ip>
string line = serverReader.ReadLine();
string[] entry = line.Split(',');
if (entry.Length != 2 || line.Contains('#'))
{
log.Info("Skipping line in server list: \"" + line + "\"");
continue;
}
serverList.Add(new KeyValuePair<string, string>(entry[0], entry[1]));
}
}
return serverList;
}
/// <summary>
/// Loads all saved usernames
/// Throws IOException, ArrayOutOfBounds on failure - should be handled in caller class
/// </summary>
/// <returns>List of username strings</returns>
public List<string> getUsernames()
{
List<string> usernameList = new List<string>();
using (StreamReader usernameReader = new StreamReader(username))
{
while (!usernameReader.EndOfStream)
{
string userName = usernameReader.ReadLine();
if (String.IsNullOrEmpty(userName) || string.IsNullOrWhiteSpace(userName)) continue;
if (userName.Contains('#'))
{
log.Info("Skipping line in userName list: \"" + userName + "\"");
continue;
}
if (userName.Length > 30)
{
log.Warn("Username too long! Skipping: " + userName);
continue;
}
usernameList.Add(userName);
}
}
return usernameList;
}
/// <summary>
/// Checks for duplicate server entries
/// </summary>
/// <param name="serverName"></param>
/// <returns></returns>
public Boolean isDuplicateServer(string serverName)
{
using (StreamReader serverReader = new StreamReader(servers))
{
while (!serverReader.EndOfStream)
{
if (serverReader.ReadLine().Split(',')[0].Equals(serverName))
{
return true;
}
}
serverReader.Close();
}
return false;
}
/// <summary>
/// Checks for duplicate usernames
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private bool isDuplicateUserName(string name)
{
using (StreamReader usernameReader = new StreamReader(username))
{
while (!usernameReader.EndOfStream)
{
if (usernameReader.ReadLine().Equals(name))
{
return true;
}
}
}
return false;
}
/// <summary>
/// Adds a server entry to the default file
/// </summary>
/// <param name="name">Name of server</param>
/// <param name="ip">ip:port string</param>
public void addServer(string name, string ip)
{
if (isDuplicateServer(name))
{
return;
}
using (StreamWriter serverWriter = File.AppendText(servers))
{
serverWriter.WriteLine(name + "," + ip);
serverWriter.Flush();
serverWriter.Close();
}
}
public void addUsername(string name)
{
if (isDuplicateUserName(name))
{
return;
}
using (StreamWriter usernameWriter = File.AppendText(username))
{
usernameWriter.WriteLine(name);
usernameWriter.Flush();
usernameWriter.Close();
}
}
/// <summary>
/// Writes the pm to the file
/// </summary>
/// <param name="pm"></param>
public void writePM(string pm)
{
using (StreamWriter pmWriter = File.AppendText(privMessages))
{
pmWriter.WriteLine(pm);
pmWriter.Flush();
pmWriter.Close();
}
}
public List<string> getPMs()
{
var pms = new List<string>();
using (StreamReader pmReader = new StreamReader(privMessages))
{
while (!pmReader.EndOfStream)
{
pms.Add(pmReader.ReadLine());
}
}
return pms;
}
public Boolean isBuddy(string name)
{
using (StreamReader buddyReader = new StreamReader(buddy))
{
while (!buddyReader.EndOfStream)
{
if (name.Equals(buddyReader.ReadLine()))
return true;
}
}
return false;
}
public Boolean isIgnored(string name)
{
using (StreamReader ignoreReader = new StreamReader(ignore))
{
while (!ignoreReader.EndOfStream)
{
if (name.Equals(ignoreReader.ReadLine()))
return true;
}
}
return false;
}
public void addBuddy(string name)
{
if (isBuddy(name)) return;
using (StreamWriter buddyWriter = File.AppendText(buddy))
{
buddyWriter.WriteLine(name);
buddyWriter.Flush();
buddyWriter.Close();
}
}
/// <summary>
/// Removes the buddy from the list.
/// Note: Overwrites previous file
/// </summary>
/// <param name="name"></param>
public void removeBuddy(string name)
{
if (!isBuddy(name)) return;
List<string> buddies = new List<string>(File.ReadAllLines(buddy));
buddies.Remove(name);
File.WriteAllLines(buddy, buddies);
}
public iniSettings getSettings()
{
iniSettings currSettings = new iniSettings();
currSettings.NotifyBuddyJoinServer = settings.Configs["Notifications"].GetInt("NotifyBuddyJoinServer", 1) == 1;
currSettings.NotifyBuddyLeftGame = settings.Configs["Notifications"].GetInt("NotifyBuddyLeftServer", 1) == 1;
currSettings.NotifyBuddyJoinedGame = settings.Configs["Notifications"].GetInt("NotifyBuddyJoinedGame", 1) == 1;
currSettings.NotifyBuddyLeftGame = settings.Configs["Notifications"].GetInt("NotifyBuddyLeftGame", 1) == 1;
currSettings.NotifyPM = settings.Configs["Notifications"].GetInt("NotifyPM", 1) == 1;
currSettings.UserLeftMessage = settings.Configs["Server Chat"].GetInt("UserJoinedMessage", 1) == 1;
currSettings.UserLeftMessage = settings.Configs["Server Chat"].GetInt("UserLeftMessage", 1) == 1;
return currSettings;
}
internal bool isNotifyEnabled(TrayFlags.PopValues flag)
{
if(!flagFunctor.ContainsKey(flag)) return false;
return flagFunctor[flag].Invoke();
}
}
}