-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfigurationStore.cs
175 lines (136 loc) · 5.73 KB
/
ConfigurationStore.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
using System.Collections.Generic;
using Windows.Storage;
using SimpleJSON;
using StereoKit;
namespace DutchSkies
{
public class ConfigurationStore
{
public enum ConfigType
{
MAP_SET, LANDMARK_SET, OBSERVER_SET, DISCORD_WEBHOOK
};
/*
* section: "map_sets", "landmark_sets", "observers"
*
* "map_sets"
* "Netherlands" -> "{ "id": "...", [....] }"
* ...
*
* "landmark_sets"
* "Park" -> "{ "id": "...", [....] }"
* ...
*
* "observers"
* "Home" -> "{ "id": "...", ...}"
*
*/
const string OPTIONS_CONTAINER = "options";
protected static string type2section(ConfigType type)
{
if (type == ConfigType.MAP_SET)
return "map_sets";
else if (type == ConfigType.LANDMARK_SET)
return "landmark_sets";
else if (type == ConfigType.OBSERVER_SET)
return "observers";
else
{
Log.Warn($"No section for type '{type}'!");
return "";
}
}
public static void Store(ConfigType type, string id, JSONNode data)
{
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
string section = type2section(type);
Log.Info($"Storing configuration section '{section}', id '{id}'");
ApplicationDataContainer type_container;
if (localSettings.Containers.ContainsKey(section))
type_container = localSettings.Containers[section];
else
type_container = localSettings.CreateContainer(section, ApplicationDataCreateDisposition.Always);
if (type_container.Values.ContainsKey(id))
Log.Info($"Overwriting configuration '{id}' (section '{section}')");
type_container.Values[id] = data.ToString();
}
public static void Delete(ConfigType type, string id)
{
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
string section = type2section(type);
if (!localSettings.Containers.ContainsKey(section))
{
Log.Err($"No configurations of type '{section}' stored (id '{id}')");
return;
}
ApplicationDataContainer type_container = localSettings.Containers[section];
type_container.Values.Remove(id);
}
public static void DeleteAllOfType(ConfigType type)
{
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
string section = type2section(type);
localSettings.DeleteContainer(section);
}
public static void List(ConfigType type, ref List<string> items)
{
items.Clear();
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
string section = type2section(type);
if (!localSettings.Containers.ContainsKey(section))
return;
ApplicationDataContainer type_container = localSettings.Containers[section];
foreach (string id in type_container.Values.Keys)
items.Add(id);
items.Sort();
}
public static JSONNode Load(ConfigType type, string id)
{
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
string section = type2section(type);
if (!localSettings.Containers.ContainsKey(section))
{
Log.Err($"No configurations of type '{section}' stored (id '{id}')");
return null;
}
ApplicationDataContainer type_container = localSettings.Containers[section];
if (!type_container.Values.ContainsKey(id))
{
Log.Err($"No configuration '{id}' found (type '{section}')");
return null;
}
string sdata = type_container.Values[id] as string;
// XXX handle error
return JSONNode.Parse(sdata);
}
// String options
public static void StoreOption(string id, string value)
{
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
Log.Info($"Storing configuration option '{id}', value '{value}'");
ApplicationDataContainer type_container;
if (localSettings.Containers.ContainsKey(OPTIONS_CONTAINER))
type_container = localSettings.Containers[OPTIONS_CONTAINER];
else
type_container = localSettings.CreateContainer(OPTIONS_CONTAINER, ApplicationDataCreateDisposition.Always);
if (type_container.Values.ContainsKey(id))
Log.Info($"Overwriting configuration option '{id}'");
type_container.Values[id] = value;
}
public static bool LoadOption(string id, out string value)
{
value = "";
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
if (!localSettings.Containers.ContainsKey(OPTIONS_CONTAINER))
return false;
ApplicationDataContainer type_container = localSettings.Containers[OPTIONS_CONTAINER];
if (!type_container.Values.ContainsKey(id))
{
Log.Err($"No configuration option '{id}' found");
return false;
}
value = type_container.Values[id] as string;
return true;
}
}
}