-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathManagerConfiguration.cs
151 lines (112 loc) · 4.86 KB
/
ManagerConfiguration.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
using System;
using System.Configuration;
using System.Diagnostics;
using System.Drawing;
using System.Runtime.InteropServices;
using XInputium.XInput;
namespace XinputWindowsManager
{
public class ManagerConfiguration
{
public const string BUTTON_A = "A";
public const string BUTTON_B = "B";
public const string BUTTON_X = "X";
public const string BUTTON_Y = "Y";
public const string BUTTON_LB = "LB";
public const string BUTTON_RB = "RB";
public const string BUTTON_LT = "LT";
public const string BUTTON_RT = "RT";
public const string BUTTON_LS = "LS";
public const string BUTTON_RS = "RS";
public const string BUTTON_BACK = "BACK";
public const string BUTTON_START = "START";
public const string BUTTON_DPAD_UP = "DPAD_UP";
public const string BUTTON_DPAD_DOWN = "DPAD_DOWN";
public const string BUTTON_DPAD_LEFT = "DPAD_LEFT";
public const string BUTTON_DPAD_RIGHT = "DPAD_RIGHT";
public bool StartEnabled { get; set; }
public int XinputPollingDelayMsecs { get; set; }
public double MaxCursorMovementSpeed { get; set; }
public double LeftStickThreshold { get; set; }
public double RightStickThreshold { get; set; }
public double TriggerThreshold { get; set; }
public string[] ToggleManagerCombination { get; set; }
public ManagerConfiguration()
{
try
{
this.StartEnabled = this.ReadBoolSetting("START_ENABLED");
this.XinputPollingDelayMsecs = this.ReadIntSetting("XINPUT_POLLING_DELAY_MSECS", minvalue: 4, maxvalue: 32);
this.MaxCursorMovementSpeed = this.ReadDoubleSetting("MAX_CURSOR_MOVEMENT_SPEED", minvalue: 5.0, maxvalue: 50.0);
this.LeftStickThreshold = this.ReadDoubleSetting("LEFT_STICK_THRESHOLD", minvalue: 0.0, maxvalue: 1.0);
this.RightStickThreshold = this.ReadDoubleSetting("RIGHT_STICK_THRESHOLD", minvalue: 0.0, maxvalue: 1.0);
this.TriggerThreshold = this.ReadDoubleSetting("TRIGGER_THRESHOLD", minvalue: 0.0, maxvalue: 1.0);
this.ToggleManagerCombination = this.ParseToggleCombination();
}
catch (Exception e)
{
throw new ArgumentException($"Invalid App.Config. {e.Message}");
}
}
private string ReadSetting(string setting)
{
string value = ConfigurationManager.AppSettings[setting];
if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException($"Setting {setting} not available or empty");
}
return value;
}
private bool ReadBoolSetting(string setting)
{
return bool.Parse(this.ReadSetting(setting));
}
private int ReadIntSetting(string setting, int minvalue, int maxvalue)
{
int v = int.Parse(this.ReadSetting(setting));
if (v < minvalue || v > maxvalue)
{
throw new ArgumentException($"Invalid value for setting {setting}. Value must be between {minvalue} and {maxvalue}.");
}
return v;
}
private double ReadDoubleSetting(string setting, double minvalue, double maxvalue)
{
double v = double.Parse(this.ReadSetting(setting));
if (v < minvalue || v > maxvalue)
{
throw new ArgumentException($"Invalid value for setting {setting}. Value must be between {minvalue} and {maxvalue}.");
}
return v;
}
private string[] ParseToggleCombination()
{
string rawValue = this.ReadSetting("TOGGLE_MANAGER_COMBINATION");
string[] splitValues = rawValue.Split(',');
foreach (string button in splitValues)
{
Debug.WriteLine($"Combination button: {button}");
if ( button != BUTTON_A
&& button != BUTTON_B
&& button != BUTTON_X
&& button != BUTTON_Y
&& button != BUTTON_LB
&& button != BUTTON_RB
&& button != BUTTON_LS
&& button != BUTTON_RS
&& button != BUTTON_LT
&& button != BUTTON_RT
&& button != BUTTON_BACK
&& button != BUTTON_START
&& button != BUTTON_DPAD_UP
&& button != BUTTON_DPAD_DOWN
&& button != BUTTON_DPAD_LEFT
&& button != BUTTON_DPAD_RIGHT)
{
throw new ArgumentException($"Invalid button value in TOGGLE_MANAGER_COMBINATION: {button}");
}
}
return splitValues;
}
}
}