Skip to content

Commit 268e91c

Browse files
committed
feat: Update C# For v4 spec
Still need to fill out the rest of the client API, but gets devices and runs vibration for now.
1 parent 4dc427b commit 268e91c

16 files changed

+345
-560
lines changed

Buttplug.Test/Core/ButtplugMessagesTests.cs

Lines changed: 9 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -118,20 +118,21 @@ void CheckMsg(DeviceList msg)
118118
119119
CheckMsg(newMsg);
120120
}
121-
121+
*/
122122
[Test]
123-
public void TestVibrateCmd()
123+
public void TestOutputCmd()
124124
{
125-
void CheckMsg(VibrateCmd msg)
125+
void CheckMsg(OutputCmd msg)
126126
{
127127
msg.Id.Should().Be(4);
128128
msg.DeviceIndex.Should().Be(2);
129-
msg.Speeds.Count.Should().Be(1);
130-
msg.Speeds[0].Index.Should().Be(0);
131-
msg.Speeds[0].Speed.Should().Be(0.5);
129+
msg.FeatureIndex.Should().Be(0);
130+
var cmd = new OutputCommand();
131+
cmd.Vibrate = new OutputCommandValue(10);
132+
msg.Command.Should().Be(cmd);
132133
}
133134

134-
var msg = new VibrateCmd(2, new List<VibrateCmd.VibrateSubcommand> { new VibrateCmd.VibrateSubcommand(0, 0.5) }, 4);
135+
var msg = new OutputCmd(2, new List<VibrateCmd.VibrateSubcommand> { new VibrateCmd.VibrateSubcommand(0, 0.5) }, 4);
135136
CheckMsg(msg);
136137

137138
var newMsg = CheckParsedVersion<VibrateCmd>(msg, 1,
@@ -141,50 +142,6 @@ void CheckMsg(VibrateCmd msg)
141142

142143
_parser.Invoking(x => x.Serialize(msg, 0)).Should().Throw<ButtplugMessageException>();
143144
}
144-
*/
145-
146-
[Test]
147-
public void TestLinearCmd()
148-
{
149-
void CheckMsg(LinearCmd msg)
150-
{
151-
msg.Id.Should().Be(4);
152-
msg.DeviceIndex.Should().Be(2);
153-
msg.Vectors.Count.Should().Be(1);
154-
msg.Vectors[0].Index.Should().Be(0);
155-
msg.Vectors[0].Duration.Should().Be(100);
156-
msg.Vectors[0].Position.Should().Be(0.5);
157-
}
158-
159-
var checkMsg = new LinearCmd(2, new List<LinearCmd.VectorSubcommand> { new LinearCmd.VectorSubcommand(0, 100, 0.5) }, 4);
160-
CheckMsg(checkMsg);
161-
162-
var newMsg = CheckParsedVersion<LinearCmd>(checkMsg,
163-
"[{\"LinearCmd\":{\"Vectors\":[{\"Duration\":100,\"Index\":0,\"Position\":0.5}],\"DeviceIndex\":2,\"Id\":4}}]");
164-
165-
CheckMsg(newMsg);
166-
}
167-
168-
[Test]
169-
public void TestRotateCmd()
170-
{
171-
void CheckMsg(RotateCmd msg)
172-
{
173-
msg.Id.Should().Be(4);
174-
msg.DeviceIndex.Should().Be(2);
175-
msg.Rotations.Count.Should().Be(1);
176-
msg.Rotations[0].Index.Should().Be(0);
177-
msg.Rotations[0].Speed.Should().Be(0.5);
178-
msg.Rotations[0].Clockwise.Should().Be(true);
179-
}
180-
181-
var checkMsg = new RotateCmd(2, new List<RotateCmd.RotateSubcommand> { new RotateCmd.RotateSubcommand(0, 0.5, true) }, 4);
182-
CheckMsg(checkMsg);
183-
184-
var newMsg = CheckParsedVersion<RotateCmd>(checkMsg,
185-
"[{\"RotateCmd\":{\"Rotations\":[{\"Clockwise\":true,\"Index\":0,\"Speed\":0.5}],\"DeviceIndex\":2,\"Id\":4}}]");
186-
187-
CheckMsg(newMsg);
188-
}
145+
189146
}
190147
}

Buttplug.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1414
.editorconfig = .editorconfig
1515
EndProjectSection
1616
EndProject
17+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ButtplugTestApp", "ButtplugTestApp\ButtplugTestApp.csproj", "{3B72FE80-EF0C-42F5-96EC-3DFA8A293997}"
18+
EndProject
1719
Global
1820
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1921
Debug|Any CPU = Debug|Any CPU
@@ -30,6 +32,10 @@ Global
3032
{A773EBBF-45BE-4411-986D-907EC25A11D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
3133
{A773EBBF-45BE-4411-986D-907EC25A11D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
3234
{A773EBBF-45BE-4411-986D-907EC25A11D8}.Release|Any CPU.Build.0 = Release|Any CPU
35+
{3B72FE80-EF0C-42F5-96EC-3DFA8A293997}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36+
{3B72FE80-EF0C-42F5-96EC-3DFA8A293997}.Debug|Any CPU.Build.0 = Debug|Any CPU
37+
{3B72FE80-EF0C-42F5-96EC-3DFA8A293997}.Release|Any CPU.ActiveCfg = Release|Any CPU
38+
{3B72FE80-EF0C-42F5-96EC-3DFA8A293997}.Release|Any CPU.Build.0 = Release|Any CPU
3339
EndGlobalSection
3440
GlobalSection(SolutionProperties) = preSolution
3541
HideSolutionNode = FALSE

Buttplug/Client/ButtplugClient.cs

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -139,29 +139,19 @@ public async Task ConnectAsync(IButtplugClientConnector connector, CancellationT
139139
Convert.ToInt32(Math.Round(si.MaxPingTime / 2.0, 0)));
140140
}
141141

142-
if (si.MessageVersion < ButtplugConsts.CurrentSpecVersion)
142+
if (si.ProtocolVersionMajor < ButtplugConsts.ProtocolVersionMajor)
143143
{
144144
await DisconnectAsync().ConfigureAwait(false);
145145
throw new ButtplugHandshakeException(
146-
$"Buttplug Server's schema version ({si.MessageVersion}) is less than the client's ({ButtplugConsts.CurrentSpecVersion}). A newer server is required.",
146+
$"Buttplug Server's schema version ({si.ProtocolVersionMajor}) is less than the client's ({ButtplugConsts.ProtocolVersionMajor}). A newer server is required.",
147147
res.Id);
148148
}
149149

150150
// Get full device list and populate internal list
151151
var resp = await _handler.SendMessageAsync(new RequestDeviceList()).ConfigureAwait(false);
152152
if (resp is DeviceList list)
153153
{
154-
foreach (var d in list.Devices)
155-
{
156-
if (_devices.ContainsKey(d.DeviceIndex))
157-
{
158-
continue;
159-
}
160-
161-
var device = new ButtplugClientDevice(_handler, d);
162-
_devices[d.DeviceIndex] = device;
163-
DeviceAdded?.Invoke(this, new DeviceAddedEventArgs(device));
164-
}
154+
HandleDeviceList(list);
165155
}
166156
else
167157
{
@@ -174,9 +164,7 @@ public async Task ConnectAsync(IButtplugClientConnector connector, CancellationT
174164
throw new ButtplugHandshakeException(
175165
"Received unknown response to DeviceList handshake query");
176166
}
177-
178167
break;
179-
180168
case Error e:
181169
await DisconnectAsync().ConfigureAwait(false);
182170
throw ButtplugException.FromError(e);
@@ -247,6 +235,30 @@ private void ConnectorErrorHandler(object sender, ButtplugExceptionEventArgs exc
247235
ErrorReceived?.Invoke(this, exception);
248236
}
249237

238+
private void HandleDeviceList(DeviceList listMsg)
239+
{
240+
foreach (var pair in listMsg.Devices)
241+
{
242+
if (!_devices.ContainsKey(pair.Key))
243+
{
244+
var dev = new ButtplugClientDevice(_handler, pair.Value);
245+
_devices.TryAdd(pair.Key, dev);
246+
DeviceAdded?.Invoke(this, new DeviceAddedEventArgs(dev));
247+
}
248+
}
249+
250+
foreach (var index in _devices.Keys)
251+
{
252+
if (!listMsg.Devices.ContainsKey(index))
253+
{
254+
if (_devices.TryRemove(index, out var oldDev))
255+
{
256+
DeviceRemoved?.Invoke(this, new DeviceRemovedEventArgs(oldDev));
257+
}
258+
}
259+
}
260+
}
261+
250262
/// <summary>
251263
/// Message Received event handler. Either tries to match incoming messages as replies to
252264
/// messages we've sent, or fires an event related to an incoming event, like device
@@ -260,28 +272,8 @@ private async void MessageReceivedHandler(object sender, MessageReceivedEventArg
260272

261273
switch (msg)
262274
{
263-
case DeviceAdded d:
264-
var dev = new ButtplugClientDevice(_handler, d);
265-
_devices.AddOrUpdate(d.DeviceIndex, dev, (u, device) => dev);
266-
DeviceAdded?.Invoke(this, new DeviceAddedEventArgs(dev));
267-
break;
268-
269-
case DeviceRemoved d:
270-
if (!_devices.ContainsKey(d.DeviceIndex))
271-
{
272-
ErrorReceived?.Invoke(this,
273-
new ButtplugExceptionEventArgs(
274-
new ButtplugDeviceException(
275-
"Got device removed message for unknown device.",
276-
msg.Id)));
277-
return;
278-
}
279-
280-
if (_devices.TryRemove(d.DeviceIndex, out var oldDev))
281-
{
282-
DeviceRemoved?.Invoke(this, new DeviceRemovedEventArgs(oldDev));
283-
}
284-
275+
case DeviceList d:
276+
HandleDeviceList(d);
285277
break;
286278

287279
case ScanningFinished _:

0 commit comments

Comments
 (0)