-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVibrationManager.cs
268 lines (238 loc) · 8.2 KB
/
VibrationManager.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
using Buttplug.Client;
using Buttplug.Client.Connectors.WebsocketConnector;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
using Timer = System.Timers.Timer;
namespace BPGE;
public class EventConfig
{
public int Intensity { get; set; }
public double Duration { get; set; }
}
public class GameConfig
{
public string? Mode { get; set; }
public Dictionary<String, EventConfig> Events { get; set; }
}
public class VibrationManager
{
private BPGEView _bpgeView;
public ButtplugClient Client;
private ButtplugWebsocketConnector _connector;
private Timer _timer;
private int _gameId = 0;
private static readonly int IntensitySize = 10 * 60 * 30;
private int _counter = 0;
private int[] _intensityArray = new int[IntensitySize];
private int _currentVibration = 0;
private Dictionary<string, EventConfig> _intensities = new();
private static string GetFilePath(string fileName)
{
return Path.Combine(Directory.GetCurrentDirectory(), fileName);
}
private static GameConfig GetGameConfig(string fileName)
{
return new DeserializerBuilder().WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build().Deserialize<GameConfig>(File.ReadAllText(GetFilePath(fileName)));
}
private void ResetConfig()
{
_intensities = new Dictionary<string, EventConfig>();
}
private bool LoadGlobalConfig()
{
ResetConfig();
GameConfig globalConfig = null;
if(File.Exists(GetFilePath("global.yaml")))
{
_bpgeView.LogDebug("Loading file global.yaml");
globalConfig = GetGameConfig("global.yaml");
}
if (File.Exists(GetFilePath("global.yml")))
{
_bpgeView.LogDebug("Loading file global.yml");
globalConfig = GetGameConfig("global.yml");
}
if(globalConfig != null)
{
foreach (var (key, value) in globalConfig.Events)
{
_intensities.Add(key, value);
}
return true;
}
_bpgeView.LogInfo("No global config found, using empty config");
return false;
}
private void LoadGameConfig(int gameId)
{
var exists = LoadGlobalConfig();
GameConfig gameConfig = null;
if(File.Exists(GetFilePath($"{gameId}.yaml")))
{
_bpgeView.LogDebug($"Loading file {gameId}.yaml");
gameConfig = GetGameConfig($"{gameId}.yaml");
}else if (File.Exists(GetFilePath($"{gameId}.yml")))
{
_bpgeView.LogDebug($"Loading file {gameId}.yml");
gameConfig = GetGameConfig($"{gameId}.yml");
}
if (gameConfig != null)
{
switch (gameConfig.Mode)
{
case "override":
ResetConfig();
break;
case "append":
break;
default:
if (exists)
{
_bpgeView.LogInfo($"Unknown mode {gameConfig.Mode}, using global config");
}
else
{
_bpgeView.LogInfo($"Unknown mode {gameConfig.Mode}, global config does not exist, no events will be triggered");
}
return;
}
foreach (var (key, value) in gameConfig.Events)
{
_intensities.Add(key, value);
}
}else if(exists)
{
_bpgeView.LogInfo($"No config found for game {gameId}, using global config");
}else
{
_bpgeView.LogInfo($"No config found for game {gameId}, global config does not exist, no events will be triggered");
}
}
private static int CircularIndex(int index)
{
return index % IntensitySize;
}
private int CurrentIndex()
{
return CircularIndex(_counter);
}
public VibrationManager(BPGEView bpgeView)
{
_bpgeView = bpgeView;
}
public async void Init()
{
try
{
_timer = new Timer(100);
Client = new ButtplugClient("BPGE");
_connector = new ButtplugWebsocketConnector(new Uri($"ws://{_bpgeView.IntifaceIP}:{_bpgeView.IntifacePort}"));
await Client.ConnectAsync(_connector);
_timer.Elapsed += (sender, args) =>
{
if(_intensityArray[CurrentIndex()] != _currentVibration)
{
var newIntensity = _intensityArray[CurrentIndex()];
_bpgeView.LogDebug($"Intensity changed from {_currentVibration} to {newIntensity}");
_currentVibration = newIntensity;
VibrateAll(_currentVibration);
}
_counter++;
};
Client.ServerDisconnect += (sender, args) =>
{
_bpgeView.LogInfo("Buttplug Client disconnected");
_bpgeView.bpStatusLabel.Text = "Disconnected";
_bpgeView.btnTest.Enabled = false;
};
Client.DeviceAdded += (sender, args) =>
{
_bpgeView.LogInfo($"Device added: {args.Device.Name}");
};
Client.DeviceRemoved += (sender, args) =>
{
_bpgeView.LogInfo($"Device removed: {args.Device.Name}");
};
_timer.Start();
_bpgeView.LogInfo("Buttplug Client connected");
_bpgeView.bpStatusLabel.Text = "Connected";
_bpgeView.btnTest.Enabled = true;
if (Client.Devices.Length == 0)
{
_bpgeView.LogInfo("No devices found, please manage your devices at Intiface\u00ae Central");
}
LoadGlobalConfig();
}
catch (Exception e)
{
_bpgeView.LogError($"Exception in BPManager: {Environment.NewLine}{e.ToString()}");
}
}
public async void Reconnect()
{
await Client.DisconnectAsync();
Init();
}
public void ProcessEvents(dynamic json)
{
foreach (dynamic item in json)
{
if (item.gameId != _gameId)
{
_bpgeView.LogInfo($"Game changed to {item.gameName} ({item.gameId})");
_gameId = item.gameId;
LoadGameConfig(_gameId);
_bpgeView.LogDebug("Current config:");
foreach (KeyValuePair<string, EventConfig> kvp in _intensities)
{
_bpgeView.LogDebug($"Event: {kvp.Key} Intensity: {kvp.Value.Intensity}% Duration: {kvp.Value.Duration}s");
}
}
if (item.type == "event")
{
foreach (dynamic ev in item.data.events)
{
if (!_intensities.ContainsKey(ev.name.ToString())) continue;
EventConfig eventConfig = _intensities[ev.name.ToString()];
_bpgeView.LogInfo($"Game: {item.gameName} Event: {ev.name} Intensity: {eventConfig.Intensity}% Duration: {eventConfig.Duration}s");
AddToArray(CurrentIndex(), eventConfig.Intensity, eventConfig.Duration);
}
}
}
}
private void AddToArray(int index, int intensity, double duration)
{
if (duration == 0)
{
duration = 5 * 60;
}
if(intensity == 0)
{
ClearArray();
} else
{
for (int i = index; i <= index+(duration * 10); i++)
{
if (_intensityArray[CircularIndex(i)] < intensity)
{
_intensityArray[CircularIndex(i)] = intensity;
}
}
}
}
public void ClearArray()
{
_intensityArray = new int[IntensitySize];
}
private void VibrateAll(int intensity)
{
if (Client.Connected)
{
foreach (var buttplugClientDevice in Client.Devices)
{
buttplugClientDevice.VibrateAsync((double)intensity/100);
}
}
}
}