-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathConfigParser.cs
60 lines (50 loc) · 1.5 KB
/
ConfigParser.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Threading;
namespace WindowsAudioVolumeManager {
public class ConfigParser {
const string configFile = "volumeConfig.json";
public ConfigObject SavedConfigObject;
public bool Dirty = false;
private readonly MainWindow Window;
public ConfigParser(MainWindow window) {
Window = window;
}
public void LoadConfig() {
if (File.Exists(configFile)) {
string jsonString = File.ReadAllText(configFile);
SavedConfigObject = JsonSerializer.Deserialize<ConfigObject>(jsonString);
Window.DefaultSessionElement.ScalarVolume = SavedConfigObject.DefaultVolumeScalar;
Window.HasModifiedDefaultSession = true;
Window.SavedSessions = SavedConfigObject.SavedSessions;
}
}
public void SaveConfig() {
if (SavedConfigObject == null) {
SavedConfigObject = new ConfigObject();
SavedConfigObject.DefaultVolumeScalar = Window.DefaultSessionElement.ScalarVolume;
SavedConfigObject.SavedSessions = Window.SavedSessions;
}
File.WriteAllText(configFile, JsonSerializer.Serialize(SavedConfigObject));
}
public void DirtySaverThread() {
while (true) {
Thread.Sleep(1000);
try {
if (Dirty) {
SaveConfig();
Dirty = false;
}
} catch (Exception e) {
Console.WriteLine(e);
}
}
}
public class ConfigObject {
public float DefaultVolumeScalar { get; set; }
public List<SavedAudioSession> SavedSessions { get; set; }
}
}
}