forked from mattpannella/pupdate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsManager.cs
140 lines (119 loc) · 3.46 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
using System;
using System.Text.Json;
using System.IO;
namespace pannella.analoguepocket;
public class SettingsManager
{
private Settings _settings;
private string _settingsFile;
private const string OLD_DEFAULT = "pocket-roms";
private const string NEW_DEFAULT = "openFPGA-Files";
private const string SETTINGS_FILENAME = "pocket_updater_settings.json";
public SettingsManager(string settingsPath, List<Core>? cores = null)
{
_settings = new Settings();
string file = Path.Combine(settingsPath, SETTINGS_FILENAME);
if (File.Exists(file))
{
string json = File.ReadAllText(file);
_settings = JsonSerializer.Deserialize<Settings>(json);
//hack to force people over to new default :)
if(_settings.config.archive_name == OLD_DEFAULT) {
_settings.config.archive_name = NEW_DEFAULT;
}
}
//bandaid to fix old settings files
if(_settings.config == null) {
_settings.config = new Config();
}
_settingsFile = file;
if(cores != null) {
_initializeCoreSettings(cores);
}
SaveSettings();
}
//loop through every core, and add any missing ones to the settings file
private void _initializeCoreSettings(List<Core> cores)
{
if(_settings.coreSettings == null) {
_settings.coreSettings = new Dictionary<string,CoreSettings>();
}
foreach(Core core in cores)
{
if(!_settings.coreSettings.ContainsKey(core.identifier)) {
EnableCore(core.identifier);
}
}
}
public bool SaveSettings()
{
var options = new JsonSerializerOptions { WriteIndented = true };
File.WriteAllText(_settingsFile, JsonSerializer.Serialize(_settings, options));
return true;
}
public void DisableCore(string name)
{
if(_settings.coreSettings.ContainsKey(name))
{
_settings.coreSettings[name].skip = true;
}
else
{
CoreSettings core = new CoreSettings();
core.skip = true;
_settings.coreSettings.Add(name, core);
}
}
public void EnableCore(string name)
{
if (_settings.coreSettings.ContainsKey(name))
{
_settings.coreSettings[name].skip = false;
}
else
{
CoreSettings core = new CoreSettings();
core.skip = false;
_settings.coreSettings.Add(name, core);
}
}
public void UpdateCore(CoreSettings core, string name)
{
if (_settings.coreSettings.ContainsKey(name))
{
_settings.coreSettings[name] = core;
}
else
{
_settings.coreSettings.Add(name, core);
}
}
public CoreSettings GetCoreSettings(string name)
{
if (_settings.coreSettings.ContainsKey(name))
{
return _settings.coreSettings[name];
}
else
{
return new CoreSettings();
}
}
public Firmware GetCurrentFirmware()
{
return _settings.firmware;
}
public void SetFirmwareVersion(string version)
{
_settings.firmware.version = version;
SaveSettings();
}
public Config GetConfig()
{
return _settings.config;
}
public void UpdateConfig(Config config)
{
_settings.config = config;
}
}