forked from LISTEN-moe/windows-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Settings.cs
232 lines (207 loc) · 6.17 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
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,
BaseColor,
AccentColor,
Scale,
CloseToTray,
HideFromAltTab,
ThumbnailButton,
//Visualiser settings
EnableVisualiser,
VisualiserResolutionFactor,
FftSize,
VisualiserBarWidth,
VisualiserTransparency,
VisualiserBars,
VisualiserFadeEdges,
VisualiserColor,
//Misc
UpdateAutocheck,
UpdateInterval,
Volume,
OutputDeviceGuid,
Token,
Username
}
//I should have just used a json serialiser
static class Settings
{
public const int DEFAULT_WIDTH = 512;
public const int DEFAULT_HEIGHT = 58;
public const int DEFAULT_RIGHT_PANEL_WIDTH = 56;
public const int DEFAULT_PLAY_PAUSE_SIZE = 18;
private const string settingsFileLocation = "listenMoeSettings.ini";
static object settingsMutex = new object();
static object fileMutex = new object();
static Dictionary<Type, object> typedSettings = new Dictionary<Type, object>();
static Dictionary<char, Type> typePrefixes = new Dictionary<char, Type>()
{
{ 'i', typeof(int) },
{ 'f', typeof(float) },
{ 'b', typeof(bool) },
{ 's', typeof(string) },
{ 'c', typeof(Color) }
};
static Dictionary<Type, char> reverseTypePrefixes = new Dictionary<Type, char>()
{
{ typeof(int), 'i'},
{ typeof(float), 'f'},
{ typeof(bool), 'b'},
{ typeof(string), 's'},
{ typeof(Color), 'c' }
};
//Deserialisation
static 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.");
}}
};
//Serialisation
static 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() }
};
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.VisualiserTransparency, 0.5f); //TODO: rename this to opacity
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.VisualiserColor, Color.FromArgb(255, 1, 91));
Set(Setting.BaseColor, Color.FromArgb(33, 35, 48));
Set(Setting.AccentColor, Color.FromArgb(255, 1, 91));
}
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];
var 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 (var dict in typedSettings)
{
Type t = dict.Key;
var typedDict = (System.Collections.IDictionary)dict.Value;
var saveAction = saveActions[t];
foreach (dynamic setting in typedDict)
{
sb.AppendLine(reverseTypePrefixes[t] + ((Setting)setting.Key).ToString() + "=" + saveAction(setting.Value));
}
}
}
lock (fileMutex)
{
using (var fileStream = new FileStream(settingsFileLocation, FileMode.Create, FileAccess.Write))
{
using (var streamWriter = new StreamWriter(fileStream))
streamWriter.Write(sb.ToString());
}
}
}
}
}