/
DeviceConfigurationManager.cs
287 lines (244 loc) · 11.1 KB
/
DeviceConfigurationManager.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Buttplug.Core;
using Buttplug.Devices.Protocols;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Buttplug.Devices.Configuration
{
public class DeviceConfigurationManager
{
private static DeviceConfigurationManager _manager;
public static DeviceConfigurationManager Manager
{
get
{
if (_manager == null)
{
throw new NullReferenceException("Must load manager from file or resource before using!");
}
return _manager;
}
}
// Types and Configurations are kept in separate dictionaries, as there may be cases where we
// have a protocol but no matching configuration, or vice versa.
protected readonly Dictionary<string, Type> _protocolTypes = new Dictionary<string, Type>();
protected readonly Dictionary<string, List<IProtocolConfiguration>> _protocolConfigs = new Dictionary<string, List<IProtocolConfiguration>>();
protected readonly List<IProtocolConfiguration> _whiteList = new List<IProtocolConfiguration>();
protected readonly List<IProtocolConfiguration> _blackList = new List<IProtocolConfiguration>();
public static bool HasManager => _manager != null;
[NotNull]
private readonly JsonSerializer _serializer;
protected DeviceConfigurationManager()
{
// Names in this list must match the device config file keys.
AddProtocol("aneros", typeof(AnerosProtocol));
AddProtocol("lovense", typeof(LovenseProtocol));
AddProtocol("kiiroo-v2", typeof(KiirooGen2Protocol));
AddProtocol("cueme", typeof(CuemeProtocol));
AddProtocol("kiiroo-v1", typeof(KiirooGen1Protocol));
AddProtocol("kiiroo-v2-vibrator", typeof(KiirooGen2VibeProtocol));
AddProtocol("kiiroo-v21-vibrator", typeof(KiirooGen21VibeProtocol));
AddProtocol("lelo-f1s", typeof(LeloF1sProtocol));
AddProtocol("libo", typeof(LiBoProtocol));
AddProtocol("lovehoney-desire", typeof(LovehoneyDesireProtocol));
AddProtocol("magic-motion", typeof(MagicMotionProtocol));
AddProtocol("mysteryvibe", typeof(MysteryVibeProtocol));
AddProtocol("picobong", typeof(PicobongProtocol));
AddProtocol("prettylove", typeof(PrettyLoveProtocol));
AddProtocol("vibratissimo", typeof(VibratissimoProtocol));
AddProtocol("vorze-sa", typeof(VorzeSAProtocol));
AddProtocol("wevibe", typeof(WeVibeProtocol));
AddProtocol("youcups", typeof(YoucupsProtocol));
AddProtocol("vorze-cyclone-x", typeof(CycloneX10Protocol));
AddProtocol("youou", typeof(YououProtocol));
AddProtocol("kiiroo-v21", typeof(KiirooGen21Protocol));
AddProtocol("realtouch", typeof(RealTouchProtocol));
AddProtocol("svakom", typeof(SvakomProtocol));
AddProtocol("realov", typeof(RealovProtocol));
AddProtocol("motorbunny", typeof(MotorbunnyProtocol));
// ET312 turned off until deadlocks and caching are fixed. See #593, #594, #595
// AddProtocol("erostek-et312", typeof(ErostekET312Protocol));
// todo We need a way to be able to turn off xinput, in case the protocol is blacklisted?
AddProtocol("xinput", typeof(XInputProtocol));
_serializer = new JsonSerializer { MissingMemberHandling = MissingMemberHandling.Error };
}
private void LoadOrAppendConfigurationObject(string aConfigString, bool aCanAddProtocols = true)
{
var configObj = JObject.Parse(aConfigString);
foreach (var jsonObj in ((JObject)configObj["protocols"]).Properties())
{
var protocolName = jsonObj.Name;
if (!aCanAddProtocols && !_protocolConfigs.ContainsKey(protocolName))
{
throw new ButtplugDeviceException("Cannot add protocols in user configuration files.");
}
// We sometimes have null protocols, like "xinput". Just skip.
if (!jsonObj.Value.HasValues)
{
continue;
}
foreach (var busObj in ((JObject)jsonObj.Value).Properties())
{
IProtocolConfiguration config = null;
switch (busObj.Name)
{
case "btle":
config = busObj.Value.ToObject<BluetoothLEProtocolConfiguration>(_serializer);
break;
case "usb":
config = busObj.Value.ToObject<USBProtocolConfiguration>(_serializer);
break;
case "hid":
config = busObj.Value.ToObject<HIDProtocolConfiguration>(_serializer);
break;
case "serial":
config = busObj.Value.ToObject<SerialProtocolConfiguration>(_serializer);
break;
default:
// TODO Throw an error message here?
break;
}
if (config == null)
{
continue;
}
AddProtocolConfig(protocolName, config);
}
}
}
/// <summary>
/// Loads JSON configuration file from the configuration packed with the library on compilation.
/// </summary>
public static void LoadBaseConfigurationFromResource()
{
_manager = new DeviceConfigurationManager();
var deviceConfig = ButtplugUtils.GetStringFromFileResource("Buttplug.buttplug-device-config.json");
_manager.LoadOrAppendConfigurationObject(deviceConfig);
}
public static void LoadBaseConfigurationFile(string aFileName)
{
_manager = new DeviceConfigurationManager();
var deviceConfig = File.ReadAllText(aFileName);
_manager.LoadOrAppendConfigurationObject(deviceConfig);
}
/// <summary>
/// Loads user configuration. We require a base configuration to be loaded first, as user
/// configurations should only add on to that.
/// </summary>
/// <param name="aFileName">Path to the user configuration file.</param>
public void LoadUserConfigurationFile(string aFileName)
{
var deviceConfig = File.ReadAllText(aFileName);
LoadUserConfigurationString(deviceConfig);
}
public void LoadUserConfigurationString(string aConfigString)
{
LoadOrAppendConfigurationObject(aConfigString, false);
}
public void AddProtocol(string aProtocolName, Type aProtocolType)
{
_protocolTypes.Add(aProtocolName, aProtocolType);
}
public void AddProtocolConfig(string aProtocolName, IProtocolConfiguration aConfiguration)
{
if (!_protocolConfigs.ContainsKey(aProtocolName))
{
_protocolConfigs.Add(aProtocolName, new List<IProtocolConfiguration>());
}
if (_protocolConfigs[aProtocolName].Any())
{
var config = _protocolConfigs[aProtocolName].Find(aX => aX.GetType() == aConfiguration.GetType());
config?.Merge(aConfiguration);
}
_protocolConfigs[aProtocolName].Add(aConfiguration);
}
public void AddWhitelist(IProtocolConfiguration aConfiguration)
{
_whiteList.Add(aConfiguration);
}
public void AddBlacklist(IProtocolConfiguration aConfiguration)
{
_blackList.Add(aConfiguration);
}
public IEnumerable<ButtplugDeviceFactory> GetAllFactoriesOfType<T>()
where T : IProtocolConfiguration
{
var factories = new List<ButtplugDeviceFactory>();
foreach (var protocolConfigs in _protocolConfigs)
{
foreach (var deviceConfig in protocolConfigs.Value)
{
// todo we should probably log if we fail this check.
if (deviceConfig is T && _protocolTypes.ContainsKey(protocolConfigs.Key))
{
factories.Add(new ButtplugDeviceFactory(deviceConfig, _protocolTypes[protocolConfigs.Key]));
}
}
}
return factories;
}
public ButtplugDeviceFactory Find(IProtocolConfiguration aConfig)
{
if (_whiteList.Any())
{
var found = false;
foreach (var config in _whiteList)
{
if (!aConfig.Matches(config))
{
continue;
}
found = true;
break;
}
// If we found a whitelisted device, continue on to figure out its type.
if (!found)
{
return null;
}
}
if (_blackList.Any())
{
foreach (var config in _blackList)
{
if (aConfig.Matches(config))
{
return null;
}
}
}
foreach (var config in _protocolConfigs)
{
foreach (var deviceConfig in config.Value)
{
if (!deviceConfig.Matches(aConfig))
{
continue;
}
if (!_protocolTypes.ContainsKey(config.Key))
{
// Todo This means we found a device we have config but no protocol for. We should log here and return null.
return null;
}
// We can't create the device just yet, as we need to let the subtype manager try
// to connect to the device and set it up appropriately. Return a device factory
// to let the subtype manager do that.
return new ButtplugDeviceFactory(deviceConfig, _protocolTypes[config.Key]);
}
}
return null;
}
/// <summary>
/// Clears the manager. Used for testing only.
/// </summary>
internal static void Clear()
{
_manager = null;
}
}
}