-
Notifications
You must be signed in to change notification settings - Fork 0
/
Configuration.cs
105 lines (98 loc) · 3.5 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Gma.System.MouseKeyHook.HotKeys;
using Microsoft.Win32;
using Newtonsoft.Json;
namespace OHK
{
public class HotKey
{
public Keys Key { get; set; }
public string Scene { get; set; }
public string Source { get; set; }
public int Delay { get; set; }
public string Type { get; set; }
public bool IsPressed { get; set; }
}
class Configuration
{
private const string ConfigFolder = "Config";
private const string ConfigFile = "config.json";
public static OHKConfig OHK;
private const string RegPath = @"SOFTWARE\"+Constant.Author+@"\"+Constant.appName;
static Configuration()
{
try
{
OHK = new OHKConfig();
using (RegistryKey rk = Registry.CurrentUser.OpenSubKey(RegPath))
{
if (rk == null)
{
using (RegistryKey rki = Registry.CurrentUser.CreateSubKey(RegPath))
{
rki.SetValue("IP", OHK.Ip);
rki.SetValue("Port", OHK.Port);
rki.SetValue("Password", OHK.Password);
string hotkeysJSON = JsonConvert.SerializeObject(OHK.hotKeys, Formatting.Indented);
rki.SetValue("HotKeys", hotkeysJSON);
}
}
else
{
OHK.Ip = rk.GetValue("IP").ToString();
OHK.Port = rk.GetValue("Port").ToString();
OHK.Password = rk.GetValue("Password").ToString();
//DebugLogForm.Instance.Log($"reg: { rk.GetValue("HotKeys") }");
OHK.hotKeys = JsonConvert.DeserializeObject<HotKey[]>(rk.GetValue("HotKeys").ToString());
}
}
}
catch (Exception ex)
{
DebugLogForm.Instance.Log($"Registry exception: {ex}");
}
}
private void Log(string text)
{
DebugLogForm.Instance.Log(text);
}
static public void SaveConfig()
{
using (RegistryKey rk = Registry.CurrentUser.OpenSubKey(RegPath, true))
{
if (rk != null)
{
rk.SetValue("IP", OHK.Ip);
rk.SetValue("Port", OHK.Port);
rk.SetValue("Password", OHK.Password);
string hotkeysJSON = JsonConvert.SerializeObject(OHK.hotKeys, Formatting.Indented);
DebugLogForm.Instance.Log($"Hotkeys: {hotkeysJSON}");
rk.SetValue("HotKeys", hotkeysJSON);
}
}
}
public class OHKConfig
{
public string Ip;
public string Port;
public string Password;
public int ReconnectDelay;
[JsonProperty("Hotkeys")]
public HotKey[] hotKeys;
public OHKConfig()
{
Ip = "127.0.0.1";
Port = "4455";
Password = "testtest";
ReconnectDelay = 5;
hotKeys = new HotKey[]
{
new HotKey { Key = Keys.G, Scene = "", Source = "", Delay = 300, Type = "Show" }
};
}
}
}
}