From 6fe683b4a9e32a47e0d7d0d7f3fc678d8b37d1d9 Mon Sep 17 00:00:00 2001 From: Lea Date: Mon, 20 May 2024 21:30:27 +0200 Subject: [PATCH] Vibrate on beat --- Patches/UpdateBeat.cs | 5 ++++- Utils/ButtplugManager.cs | 27 ++++++++++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/Patches/UpdateBeat.cs b/Patches/UpdateBeat.cs index dbcf357..56a7082 100644 --- a/Patches/UpdateBeat.cs +++ b/Patches/UpdateBeat.cs @@ -1,4 +1,5 @@ using HarmonyLib; +using MetalButtplug.Utils; namespace MetalButtplug.Patches; @@ -11,9 +12,11 @@ internal class UpdateBeatPatch { var controller = player.ScoreController; Plugin.Log.LogInfo( - $"Quarter beat, multiplier: {controller.GetCurrentTierIndex()}/{controller.GetNumTiers()} ({1 << controller.GetCurrentTierIndex()}x)" + $"Quarter beat, multiplier: {controller.GetCurrentTierIndex()}/{controller.GetNumTiers()-1} ({1 << controller.GetCurrentTierIndex()}x)" ); + ButtplugManager.Vibrate((float)controller.GetCurrentTierIndex() / ((float)controller.GetNumTiers() - 1), 100); + } } } diff --git a/Utils/ButtplugManager.cs b/Utils/ButtplugManager.cs index 13d0463..c0c2727 100644 --- a/Utils/ButtplugManager.cs +++ b/Utils/ButtplugManager.cs @@ -1,11 +1,12 @@ using System; +using System.Linq; +using System.Threading.Tasks; using Buttplug.Client; using Buttplug.Client.Connectors.WebsocketConnector; namespace MetalButtplug.Utils; internal static class ButtplugManager { - public static bool enabled = false; static ButtplugClient client; public async static void Init() { @@ -17,7 +18,6 @@ internal static class ButtplugManager { var connector = new ButtplugWebsocketConnector(new Uri("ws://localhost:12345")); await client.ConnectAsync(connector); Plugin.Log.LogInfo("Connected to buttplug server!"); - enabled = true; } catch(Exception ex) { Plugin.Log.LogWarning($"Failed to connect: {ex.Message}\nDisabling buttplug integration - Is Intiface running?"); return; @@ -28,15 +28,32 @@ internal static class ButtplugManager { Plugin.Log.LogInfo($"Found devices:"); foreach (ButtplugClientDevice device in client.Devices) { - Plugin.Log.LogInfo($"- ${device.DisplayName}"); + Plugin.Log.LogInfo($"- {device.Name}"); } } private static void OnDeviceAdd(object sender, DeviceAddedEventArgs e) { - Plugin.Log.LogInfo($"Device added: {e.Device.DisplayName}"); + Plugin.Log.LogInfo($"Device added: {e.Device.Name}"); } private static void OnDeviceRemove(object sender, DeviceRemovedEventArgs e) { - Plugin.Log.LogInfo($"Device removed: {e.Device.DisplayName}"); + Plugin.Log.LogInfo($"Device removed: {e.Device.Name}"); + } + + public async static void Vibrate(float strength, int duration) { + if (!client.Connected) return; + if (strength > 1) strength = 1; + if (strength <= 0) return; // No need to waste time if we're not going to vibrate anyway + + Task[] tasks = client.Devices + .Where((device) => device.VibrateAttributes.Count > 0) + .Select(async (device) => { + await device.VibrateAsync(strength); + await Task.Delay(duration); + await device.Stop(); + }) + .ToArray(); + + await Task.WhenAll(tasks); } }