-
Notifications
You must be signed in to change notification settings - Fork 1
/
Configuration.cs
89 lines (74 loc) · 2.13 KB
/
Configuration.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
using Newtonsoft.Json;
using SuchByte.MacroDeck.Plugins;
using System.Linq;
namespace RecklessBoon.MacroDeck.Discord
{
public class Configuration
{
const string VAR_CLIENT_SECRET = "client_secret";
[JsonIgnore]
DiscordPlugin _plugin;
[JsonIgnore]
public string ClientSecret { get; set; }
public string ClientId { get; set; }
public bool Debug { get; set; } = false;
public Configuration(DiscordPlugin plugin)
{
if (plugin != null)
{
_plugin = plugin;
Reload();
}
}
public bool IsFullySet
{
get
{
return ClientId != null && ClientSecret != null;
}
}
public void Save()
{
SaveCredentials();
SaveConfig();
LoadConfig();
}
public void Reload()
{
LoadCredentials();
LoadConfig();
}
protected void LoadCredentials()
{
var creds = PluginCredentials.GetPluginCredentials(_plugin);
var data = creds.Count > 0 ? creds.First() : null;
if (data != null && data.ContainsKey(VAR_CLIENT_SECRET))
{
ClientSecret = data[VAR_CLIENT_SECRET];
}
}
protected void SaveCredentials()
{
CredentialsHelper.UpsertCredential(VAR_CLIENT_SECRET, ClientSecret);
}
protected void LoadConfig()
{
var json = PluginConfiguration.GetValue(_plugin, "config");
if (json != null)
{
try
{
var config = JsonConvert.DeserializeObject<Configuration>(json);
ClientId = config?.ClientId;
Debug = config != null && config.Debug;
}
catch { }
}
}
protected void SaveConfig()
{
var json = JsonConvert.SerializeObject(this);
PluginConfiguration.SetValue(_plugin, "config", json);
}
}
}