This repository has been archived by the owner on Feb 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Settings.cs
268 lines (238 loc) · 7.41 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Text;
namespace ListenMoeClient
{
enum Setting
{
//UI and form settings
LocationX,
LocationY,
TopMost,
SizeX,
SizeY,
FormOpacity,
CustomColors,
JPOPBaseColor,
JPOPAccentColor,
KPOPBaseColor,
KPOPAccentColor,
CustomBaseColor,
CustomAccentColor,
Scale,
CloseToTray,
HideFromAltTab,
ThumbnailButton,
//Visualiser settings
EnableVisualiser,
VisualiserResolutionFactor,
FftSize,
VisualiserBarWidth,
VisualiserOpacity,
VisualiserBars,
VisualiserFadeEdges,
JPOPVisualiserColor,
KPOPVisualiserColor,
CustomVisualiserColor,
//Stream
StreamType,
//Misc
UpdateAutocheck,
UpdateInterval,
Volume,
OutputDeviceGuid,
Token,
Username,
DiscordPresence
}
enum StreamType
{
Jpop,
Kpop
}
//I should have just used a json serialiser
static class Settings
{
public const int DEFAULT_WIDTH = 512;
public const int DEFAULT_HEIGHT = 64;
public const int DEFAULT_RIGHT_PANEL_WIDTH = 64;
public const int DEFAULT_PLAY_PAUSE_SIZE = 20;
private const string settingsFileLocation = "listenMoeSettings.ini";
static readonly object settingsMutex = new object();
static readonly object fileMutex = new object();
static Dictionary<Type, object> typedSettings = new Dictionary<Type, object>();
static readonly Dictionary<char, Type> typePrefixes = new Dictionary<char, Type>()
{
{ 'i', typeof(int) },
{ 'f', typeof(float) },
{ 'b', typeof(bool) },
{ 's', typeof(string) },
{ 'c', typeof(Color) },
{ 't', typeof(StreamType) }
};
static readonly Dictionary<Type, char> reverseTypePrefixes = new Dictionary<Type, char>()
{
{ typeof(int), 'i'},
{ typeof(float), 'f'},
{ typeof(bool), 'b'},
{ typeof(string), 's'},
{ typeof(Color), 'c' },
{ typeof(StreamType), 't' }
};
//Deserialisation
static readonly Dictionary<Type, Func<string, (bool Success, object Result)>> parseActions = new Dictionary<Type, Func<string, (bool, object)>>()
{
{ typeof(int), s => {
bool success = int.TryParse(s, out int i);
return (success, i);
}},
{ typeof(float), s => {
bool success = float.TryParse(s, out float f);
return (success, f);
}},
{ typeof(bool), s => {
bool success = bool.TryParse(s, out bool b);
return (success, b);
}},
{ typeof(string), s => {
return (true, s);
}},
{ typeof(Color), s => {
if (int.TryParse(s.Replace("#", ""), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int argb))
return (true, Color.FromArgb(255, Color.FromArgb(argb)));
else
throw new Exception("Could not parse color '" + s + "'. Check your settings file for any errors.");
}},
{ typeof(StreamType), s => {
if (s == "jpop")
return (true, StreamType.Jpop);
else if (s == "kpop")
return (true, StreamType.Kpop);
throw new Exception("Could not parse StreamType.");
}}
};
//Serialisation
static readonly Dictionary<Type, Func<dynamic, string>> saveActions = new Dictionary<Type, Func<dynamic, string>>()
{
{ typeof(int), i => i.ToString() },
{ typeof(float), f => f.ToString() },
{ typeof(bool), b => b.ToString() },
{ typeof(string), s => s },
{ typeof(Color), c => ("#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2")).ToLowerInvariant() },
{ typeof(StreamType), st => st == StreamType.Jpop ? "jpop" : "kpop" }
};
static Settings() => LoadDefaultSettings();
public static T Get<T>(Setting key)
{
lock (settingsMutex)
{
return ((Dictionary<Setting, T>)(typedSettings[typeof(T)]))[key];
}
}
public static void Set<T>(Setting key, T value)
{
Type t = typeof(T);
lock (settingsMutex)
{
if (!typedSettings.ContainsKey(t))
{
typedSettings.Add(t, new Dictionary<Setting, T>());
}
((Dictionary<Setting, T>)typedSettings[t])[key] = value;
}
}
private static void LoadDefaultSettings()
{
Set(Setting.LocationX, 100);
Set(Setting.LocationY, 100);
Set(Setting.VisualiserResolutionFactor, 3);
Set(Setting.UpdateInterval, 3600); //in seconds
Set(Setting.SizeX, DEFAULT_WIDTH);
Set(Setting.SizeY, DEFAULT_HEIGHT);
Set(Setting.FftSize, 2048);
Set(Setting.Volume, 0.3f);
Set(Setting.VisualiserBarWidth, 3.0f);
Set(Setting.VisualiserOpacity, 0.5f);
Set(Setting.FormOpacity, 1.0f);
Set(Setting.Scale, 1.0f);
Set(Setting.TopMost, false);
Set(Setting.UpdateAutocheck, true);
Set(Setting.CloseToTray, false);
Set(Setting.HideFromAltTab, false);
Set(Setting.ThumbnailButton, true);
Set(Setting.EnableVisualiser, true);
Set(Setting.VisualiserBars, true);
Set(Setting.VisualiserFadeEdges, false);
Set(Setting.Token, "");
Set(Setting.Username, "");
Set(Setting.OutputDeviceGuid, "");
Set(Setting.JPOPVisualiserColor, Color.FromArgb(255, 1, 91));
Set(Setting.JPOPBaseColor, Color.FromArgb(33, 35, 48));
Set(Setting.JPOPAccentColor, Color.FromArgb(255, 1, 91));
Set(Setting.KPOPVisualiserColor, Color.FromArgb(48, 169, 237));
Set(Setting.KPOPBaseColor, Color.FromArgb(33, 35, 48));
Set(Setting.KPOPAccentColor, Color.FromArgb(48, 169, 237));
Set(Setting.CustomColors, false);
Set(Setting.CustomVisualiserColor, Color.FromArgb(255, 1, 91));
Set(Setting.CustomBaseColor, Color.FromArgb(33, 35, 48));
Set(Setting.CustomAccentColor, Color.FromArgb(255, 1, 91));
Set(Setting.StreamType, StreamType.Jpop);
Set(Setting.DiscordPresence, true);
}
public static void LoadSettings()
{
if (!File.Exists(settingsFileLocation))
{
WriteSettings();
return;
}
string[] lines = File.ReadAllLines(settingsFileLocation);
foreach (string line in lines)
{
string[] parts = line.Split(new char[] { '=' }, 2);
if (string.IsNullOrWhiteSpace(parts[0]))
continue;
char prefix = parts[0][0];
Type t = typePrefixes[prefix];
Func<string, (bool Success, object Result)> parseAction = parseActions[t];
(bool success, object o) = parseAction(parts[1]);
if (!success)
continue;
if (!Enum.TryParse(parts[0].Substring(1), out Setting settingKey))
continue;
MethodInfo setMethod = typeof(Settings).GetMethod("Set", BindingFlags.Static | BindingFlags.Public);
MethodInfo genericSet = setMethod.MakeGenericMethod(t);
genericSet.Invoke(null, new object[] { settingKey, o });
}
}
public static void WriteSettings()
{
StringBuilder sb = new StringBuilder();
lock (settingsMutex)
{
foreach (KeyValuePair<Type, object> dict in typedSettings)
{
Type t = dict.Key;
System.Collections.IDictionary typedDict = (System.Collections.IDictionary)dict.Value;
Func<dynamic, string> saveAction = saveActions[t];
foreach (dynamic setting in typedDict)
{
sb.AppendLine(reverseTypePrefixes[t] + ((Setting)setting.Key).ToString() + "=" + saveAction(setting.Value));
}
}
}
lock (fileMutex)
{
using (FileStream fileStream = new FileStream(settingsFileLocation, FileMode.Create, FileAccess.Write))
{
using (StreamWriter streamWriter = new StreamWriter(fileStream))
streamWriter.Write(sb.ToString());
}
}
}
}
}