forked from Macro-Deck-App/Macro-Deck
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PluginConfiguration.cs
88 lines (76 loc) · 3.27 KB
/
PluginConfiguration.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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
namespace SuchByte.MacroDeck.Plugins
{
public class PluginConfiguration
{
public static void SetValue(MacroDeckPlugin plugin, string key, string value)
{
try
{
Dictionary<string, string> pluginConfig;
if (!File.Exists(Path.Combine(MacroDeck.PluginConfigPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower() + ".json")))
{
pluginConfig = new Dictionary<string, string>();
}
else
{
pluginConfig = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(Path.Combine(MacroDeck.PluginConfigPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower() + ".json")), new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
});
}
pluginConfig[key] = value;
JsonSerializer serializer = new JsonSerializer
{
TypeNameHandling = TypeNameHandling.Auto,
NullValueHandling = NullValueHandling.Ignore,
};
using (StreamWriter sw = new StreamWriter(Path.Combine(MacroDeck.PluginConfigPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower() + ".json")))
using (JsonWriter writer = new JsonTextWriter(sw))
{
serializer.Serialize(writer, pluginConfig);
}
} catch { }
}
public static string GetValue(MacroDeckPlugin plugin = null, string key = "")
{
string value = "";
try
{
if (plugin == null || key == null) return value;
Dictionary<string, string> pluginConfig;
if (!File.Exists(Path.Combine(MacroDeck.PluginConfigPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower() + ".json")))
{
pluginConfig = new Dictionary<string, string>();
}
else
{
pluginConfig = JsonConvert.DeserializeObject<Dictionary<string, string>>(File.ReadAllText(Path.Combine(MacroDeck.PluginConfigPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower() + ".json")), new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.Auto,
NullValueHandling = NullValueHandling.Ignore,
Error = (sender, args) => { args.ErrorContext.Handled = true; }
});
}
if (pluginConfig != null && !String.IsNullOrWhiteSpace(pluginConfig[key]))
{
value = pluginConfig[key];
}
} catch {}
return value;
}
public static void DeletePluginConfig(MacroDeckPlugin plugin)
{
try
{
File.Delete(Path.Combine(MacroDeck.PluginConfigPath, plugin.Author.ToLower() + "_" + plugin.Name.ToLower() + ".json"));
}catch { }
}
}
}