Skip to content

Commit

Permalink
feat: Add support for embedded Buttplug server
Browse files Browse the repository at this point in the history
Fixes #9
  • Loading branch information
blackspherefollower committed Jul 27, 2020
1 parent 2c9f26c commit 9762a3f
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 51 deletions.
4 changes: 4 additions & 0 deletions NogasmChart/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.6.0" newVersion="4.0.6.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Namotion.Reflection" publicKeyToken="c2f9c3bdfae56102" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-1.0.12.0" newVersion="1.0.12.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
92 changes: 64 additions & 28 deletions NogasmChart/ButtplugPanel.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using Buttplug.Client;
using Buttplug.Client.Connectors;
using Buttplug.Client.Connectors.WebsocketConnector;
using Buttplug.Core;
using Buttplug.Core.Logging;
using Buttplug.Core.Messages;
using Buttplug.Server;
using DeviceAddedEventArgs = Buttplug.Client.DeviceAddedEventArgs;
Expand All @@ -32,6 +34,7 @@ public class ButtplugPanelDevice
public Dictionary<uint, bool> Linears = new Dictionary<uint, bool>();
public bool Enabled { get; set; }


public ButtplugPanelDevice(ButtplugClientDevice aDev)
{
Enabled = true;
Expand Down Expand Up @@ -62,8 +65,10 @@ public ButtplugPanelDevice(ButtplugClientDevice aDev)

}

private IButtplugLogManager _logManager = new ButtplugLogManager();
private DeviceManager _devManager = null;
private ButtplugClient _client = null;
internal Dictionary<uint, ButtplugPanelDevice> Devices = new Dictionary<uint, ButtplugPanelDevice>();
internal ConcurrentDictionary<uint, ButtplugPanelDevice> Devices = new ConcurrentDictionary<uint, ButtplugPanelDevice>();

public event PropertyChangedEventHandler PropertyChanged;

Expand Down Expand Up @@ -102,6 +107,15 @@ private async void Connect_Click(object sender, System.Windows.RoutedEventArgs e

if (_client != null)
{
try
{
await _client.StopScanningAsync();
}
catch (Exception ex)
{
// no-op: ignore failures, just stop if possible
}

await _client.DisconnectAsync();
_client = null;
Devices.Clear();
Expand Down Expand Up @@ -141,9 +155,13 @@ private async void Connect_Click(object sender, System.Windows.RoutedEventArgs e
ButtplugTarget.Text = "";
ButtplugTarget.IsEnabled = false;
ButtplugConnType.IsEditable = false;
//conn = new ButtplugEmbeddedConnector(new ButtplugServer());
//break;
return;
if (_devManager == null)
{
_devManager = new DeviceManager(_logManager);
}

conn = new ButtplugEmbeddedConnector(new ButtplugServer("NogasmChart", 0, _devManager));
break;
default:
MessageBox.Show("Invalid Connection type!", "Buttplug Error", MessageBoxButton.OK,
MessageBoxImage.Error);
Expand All @@ -159,7 +177,6 @@ private async void Connect_Click(object sender, System.Windows.RoutedEventArgs e
_client.ServerDisconnect += ClientOnServerDisconnect;
_client.ErrorReceived += ClientOnErrorReceived;
await _client.ConnectAsync();
await _client.StartScanningAsync();
}
catch (Exception e1)
{
Expand All @@ -175,13 +192,26 @@ private async void Connect_Click(object sender, System.Windows.RoutedEventArgs e

_client = null;

ButtplugConnType.IsEnabled = false;
ButtplugConnType.IsEnabled = true;
ButtplugConnType_SelectionChanged(this, null);
Connect.Content = "Connect";
Connect.IsEnabled = true;
return;
}

Connect.Content = "Disconnect";
Connect.IsEnabled = true;


try
{
await _client.StartScanningAsync();
}
catch (Exception e1)
{
MessageBox.Show($"Something went wrong: {e1.Message}", "Buttplug Error", MessageBoxButton.OK,
MessageBoxImage.Warning);
}
}

private void ClientOnErrorReceived(object sender, ButtplugExceptionEventArgs e)
Expand All @@ -194,20 +224,30 @@ private async void ClientOnServerDisconnect(object sender, EventArgs e)
{
try
{
await _client.DisconnectAsync();
} catch (Exception) { }
await _client?.DisconnectAsync();
}
catch (Exception)
{
// no-op: we were just trying to be graceful
}

_client = null;
Devices.Clear();

Dispatcher.Invoke(() =>
try
{
ButtplugConnType.IsEnabled = false;
ButtplugConnType_SelectionChanged(this, null);
Connect.Content = "Connect";
Connect.IsEnabled = true;
});
return;
await Dispatcher.InvokeAsync(() =>
{
ButtplugConnType.IsEnabled = false;
ButtplugConnType_SelectionChanged(this, null);
Connect.Content = "Connect";
Connect.IsEnabled = true;
});
}
catch (TaskCanceledException)
{
//no-op: app shutdown
}
}

private void ClientOnDeviceRemoved(object sender, DeviceRemovedEventArgs e)
Expand All @@ -221,22 +261,18 @@ private void ClientOnDeviceRemoved(object sender, DeviceRemovedEventArgs e)

private void ClientOnDeviceAdded(object sender, DeviceAddedEventArgs e)
{
if (Devices.TryGetValue(e.Device.Index, out var dev))
Devices.AddOrUpdate(e.Device.Index, new ButtplugPanelDevice(e.Device), (i, dev) =>
{
if (dev.Name.Equals(e.Device.Name, StringComparison.Ordinal))
{
dev.Device = e.Device;
dev.IsConnected = true;
}
else
if (!dev.Name.Equals(e.Device.Name, StringComparison.Ordinal))
{
dev = new ButtplugPanelDevice(e.Device);
return new ButtplugPanelDevice(e.Device);
}
}
else
{
Devices.Add(e.Device.Index, new ButtplugPanelDevice(e.Device));
}

dev.Device = e.Device;
dev.IsConnected = true;
return dev;
});

Dispatcher.Invoke(() => { DevicesTree.Items.Refresh(); });
}

Expand Down
36 changes: 21 additions & 15 deletions NogasmChart/NogasmChart.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,32 @@
<Reference Include="BouncyCastle.Crypto, Version=1.8.5.0, Culture=neutral, PublicKeyToken=0e99375e54769942">
<HintPath>..\packages\BouncyCastle.1.8.5\lib\BouncyCastle.Crypto.dll</HintPath>
</Reference>
<Reference Include="Buttplug, Version=0.5.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Buttplug.0.5.6\lib\netstandard2.0\Buttplug.dll</HintPath>
<Reference Include="Buttplug, Version=0.5.9.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Buttplug.0.5.9\lib\netstandard2.0\Buttplug.dll</HintPath>
</Reference>
<Reference Include="Buttplug.Client.Connectors.WebsocketConnector, Version=0.5.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Buttplug.Client.Connectors.WebsocketConnector.0.5.6\lib\netstandard2.0\Buttplug.Client.Connectors.WebsocketConnector.dll</HintPath>
<Reference Include="Buttplug.Client.Connectors.WebsocketConnector, Version=0.5.9.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Buttplug.Client.Connectors.WebsocketConnector.0.5.9\lib\netstandard2.0\Buttplug.Client.Connectors.WebsocketConnector.dll</HintPath>
</Reference>
<Reference Include="Buttplug.Server.Connectors.WebsocketServer, Version=0.5.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Buttplug.Server.Connectors.WebsocketServer.0.5.6\lib\netstandard2.0\Buttplug.Server.Connectors.WebsocketServer.dll</HintPath>
<Reference Include="Buttplug.Server.Connectors.WebsocketServer, Version=0.5.9.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Buttplug.Server.Connectors.WebsocketServer.0.5.9\lib\netstandard2.0\Buttplug.Server.Connectors.WebsocketServer.dll</HintPath>
</Reference>
<Reference Include="Buttplug.Server.Managers.HidSharpManager, Version=0.5.9.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Buttplug.Server.Managers.HidSharpManager.0.5.9\lib\netstandard2.0\Buttplug.Server.Managers.HidSharpManager.dll</HintPath>
</Reference>
<Reference Include="deniszykov.WebSocketListener, Version=4.2.4.0, Culture=neutral, PublicKeyToken=7f78616efb4a208d, processorArchitecture=MSIL">
<HintPath>..\packages\deniszykov.WebSocketListener.4.2.4\lib\net45\deniszykov.WebSocketListener.dll</HintPath>
</Reference>
<Reference Include="HidSharp, Version=2.1.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\HidSharp.2.1.0\lib\net35\HidSharp.dll</HintPath>
</Reference>
<Reference Include="InteractiveDataDisplay.WPF, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\InteractiveDataDisplay.WPF.1.0.0\lib\net452\InteractiveDataDisplay.WPF.dll</HintPath>
</Reference>
<Reference Include="IoTSharp.X509Extensions, Version=1.3.3.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\IoTSharp.X509Extensions.1.3.3\lib\net46\IoTSharp.X509Extensions.dll</HintPath>
<Reference Include="IoTSharp.X509Extensions, Version=1.4.9.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\IoTSharp.X509Extensions.1.4.9\lib\net46\IoTSharp.X509Extensions.dll</HintPath>
</Reference>
<Reference Include="JetBrains.Annotations, Version=2019.1.3.0, Culture=neutral, PublicKeyToken=1010a0d8d6380325, processorArchitecture=MSIL">
<HintPath>..\packages\JetBrains.Annotations.2019.1.3\lib\net20\JetBrains.Annotations.dll</HintPath>
<Reference Include="JetBrains.Annotations, Version=2020.1.0.0, Culture=neutral, PublicKeyToken=1010a0d8d6380325, processorArchitecture=MSIL">
<HintPath>..\packages\JetBrains.Annotations.2020.1.0\lib\net20\JetBrains.Annotations.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CodeAnalysis, Version=3.4.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.CodeAnalysis.Common.3.4.0\lib\netstandard2.0\Microsoft.CodeAnalysis.dll</HintPath>
Expand All @@ -76,14 +82,14 @@
<Reference Include="Microsoft.Maps.MapControl.WPF, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Maps.MapControl.WPF.1.0.0.3\lib\net40-Client\Microsoft.Maps.MapControl.WPF.dll</HintPath>
</Reference>
<Reference Include="Namotion.Reflection, Version=1.0.8.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\Namotion.Reflection.1.0.8\lib\net45\Namotion.Reflection.dll</HintPath>
<Reference Include="Namotion.Reflection, Version=1.0.12.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\Namotion.Reflection.1.0.12\lib\net45\Namotion.Reflection.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NJsonSchema, Version=10.1.4.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.10.1.4\lib\net45\NJsonSchema.dll</HintPath>
<Reference Include="NJsonSchema, Version=10.1.21.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102, processorArchitecture=MSIL">
<HintPath>..\packages\NJsonSchema.10.1.21\lib\net45\NJsonSchema.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
Expand Down Expand Up @@ -121,7 +127,7 @@
<HintPath>..\packages\System.Text.Encoding.CodePages.4.5.1\lib\net461\System.Text.Encoding.CodePages.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Dataflow, Version=4.6.5.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Dataflow.4.11.0\lib\netstandard2.0\System.Threading.Tasks.Dataflow.dll</HintPath>
<HintPath>..\packages\System.Threading.Tasks.Dataflow.4.11.1\lib\net461\System.Threading.Tasks.Dataflow.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.3\lib\netstandard2.0\System.Threading.Tasks.Extensions.dll</HintPath>
Expand Down
18 changes: 10 additions & 8 deletions NogasmChart/packages.config
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="BouncyCastle" version="1.8.5" targetFramework="net461" />
<package id="Buttplug" version="0.5.6" targetFramework="net461" />
<package id="Buttplug.Client.Connectors.WebsocketConnector" version="0.5.6" targetFramework="net461" />
<package id="Buttplug.Server.Connectors.WebsocketServer" version="0.5.6" targetFramework="net461" />
<package id="Buttplug" version="0.5.9" targetFramework="net461" />
<package id="Buttplug.Client.Connectors.WebsocketConnector" version="0.5.9" targetFramework="net461" />
<package id="Buttplug.Server.Connectors.WebsocketServer" version="0.5.9" targetFramework="net461" />
<package id="Buttplug.Server.Managers.HidSharpManager" version="0.5.9" targetFramework="net461" />
<package id="deniszykov.WebSocketListener" version="4.2.4" targetFramework="net461" />
<package id="HidSharp" version="2.1.0" targetFramework="net461" />
<package id="InteractiveDataDisplay.WPF" version="1.0.0" targetFramework="net461" />
<package id="IoTSharp.X509Extensions" version="1.3.3" targetFramework="net461" />
<package id="JetBrains.Annotations" version="2019.1.3" targetFramework="net461" />
<package id="IoTSharp.X509Extensions" version="1.4.9" targetFramework="net461" />
<package id="JetBrains.Annotations" version="2020.1.0" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.Analyzers" version="2.9.6" targetFramework="net461" developmentDependency="true" />
<package id="Microsoft.CodeAnalysis.Common" version="3.4.0" targetFramework="net461" />
<package id="Microsoft.CodeAnalysis.CSharp" version="3.4.0" targetFramework="net461" />
Expand All @@ -18,9 +20,9 @@
<package id="Microsoft.Maps.MapControl.WPF" version="1.0.0.3" targetFramework="net461" />
<package id="Microsoft.NetCore.Analyzers" version="2.9.8" targetFramework="net461" developmentDependency="true" />
<package id="Microsoft.NetFramework.Analyzers" version="2.9.8" targetFramework="net461" developmentDependency="true" />
<package id="Namotion.Reflection" version="1.0.8" targetFramework="net461" />
<package id="Namotion.Reflection" version="1.0.12" targetFramework="net461" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net461" />
<package id="NJsonSchema" version="10.1.4" targetFramework="net461" />
<package id="NJsonSchema" version="10.1.21" targetFramework="net461" />
<package id="System.Buffers" version="4.4.0" targetFramework="net461" />
<package id="System.Collections.Immutable" version="1.5.0" targetFramework="net461" />
<package id="System.Memory" version="4.5.3" targetFramework="net461" />
Expand All @@ -31,7 +33,7 @@
<package id="System.Reflection.Metadata" version="1.6.0" targetFramework="net461" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.7.0" targetFramework="net461" />
<package id="System.Text.Encoding.CodePages" version="4.5.1" targetFramework="net461" />
<package id="System.Threading.Tasks.Dataflow" version="4.11.0" targetFramework="net461" />
<package id="System.Threading.Tasks.Dataflow" version="4.11.1" targetFramework="net461" />
<package id="System.Threading.Tasks.Extensions" version="4.5.3" targetFramework="net461" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net461" />
</packages>

0 comments on commit 9762a3f

Please sign in to comment.