Skip to content

Commit

Permalink
Update 1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
PITR-DEV committed Apr 21, 2024
1 parent b39f849 commit 3061796
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 132 deletions.
101 changes: 82 additions & 19 deletions ButtplugManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace UKButt
public class ButtplugManager : BaseUnityPlugin
{
public static ButtplugManager Instance;
public static readonly plog.Logger Log = new plog.Logger("UKButt");
public bool emergencyStop = false;

private ButtplugClient buttplugClient;
Expand All @@ -23,6 +24,10 @@ public class ButtplugManager : BaseUnityPlugin
private UnscaledTimeSince _unscaledTimeSinceVibes;
private UnscaledTimeSince _timeSinceVibes;
private UnscaledTimeSince _timeSinceVibeUpdate;

// Guh guh
private readonly Queue<string> _logQueue = new Queue<string>();
private readonly Queue<string> _errorLogQueue = new Queue<string>();

private float TimeSinceVibes => PrefsManager.Instance.GetBoolLocal(UKButtProperties.UseUnscaledTime, true) ? (float)_unscaledTimeSinceVibes : (float)_timeSinceVibes;

Expand Down Expand Up @@ -51,11 +56,17 @@ public class ButtplugManager : BaseUnityPlugin

private void Awake()
{
Logger.LogInfo("Initializing UKButt");
gameObject.hideFlags = HideFlags.HideAndDontSave;
}

private void Start()
{
Log.Info("Initializing UKButt");
Instance = this;
// Start at the slowest Linear time.
_moveTime = LinearTimeMax;

Log.Info("Patching Game...");
var harmony = HarmonyLib.Harmony.CreateAndPatchAll(typeof(CameraPatch));
harmony.PatchAll(typeof(CameraPatch)); // Intercepts shake calls
harmony.PatchAll(typeof(CheatsPatch)); // Adds the bindable emergency stop cheat
Expand All @@ -68,37 +79,63 @@ private void Awake()
harmony.PatchAll(typeof(StyleDescendRank));

// Register the command to the ULTRAKILL console
Log.Info("Registering \"UKButt\" command");
GameConsole.Console.Instance.RegisterCommand(new Commands.UKButt(GameConsole.Console.Instance));

// Connect the buttplug.io client
Task.Run(RestartClient);
Task.Run(ReconnectClient);
}

public async void RestartClient()
public void TryRestartClient()
{
if (buttplugClient != null)
{
Logger.LogInfo("Disconnecting from Buttplug server");
await buttplugClient.DisconnectAsync();
buttplugClient = null;
}
Log.Info("Restarting Buttplug client...");
Task.Run(ReconnectClient);
}

private Uri GetConnectionUri()
{
return new Uri($"{PrefsManager.Instance.GetStringLocal(UKButtProperties.SocketUri, "ws://localhost:12345")}/buttplug");
}

private async Task TryKillClient()
{
if (buttplugClient == null) return;
_logQueue.Enqueue("Disconnecting from Buttplug server...");
buttplugClient.DeviceAdded -= AddDevice;
buttplugClient.DeviceRemoved -= RemoveDevice;
buttplugClient.ScanningFinished -= ScanningFinished;
buttplugClient.ErrorReceived -= ErrorReceived;
buttplugClient.ServerDisconnect -= ServerDisconnect;

if (buttplugClient.IsScanning) await buttplugClient.StopScanningAsync();
if (buttplugClient.Connected) await buttplugClient.DisconnectAsync();

buttplugClient = null;
}

private async Task ReconnectClient()
{
var uri = GetConnectionUri();

await TryKillClient();

buttplugClient = new ButtplugClient("ULTRAKILL");
buttplugClient.DeviceAdded += AddDevice;
buttplugClient.DeviceRemoved += RemoveDevice;
buttplugClient.ScanningFinished += ScanningFinished;
buttplugClient.ErrorReceived += ErrorReceived;
buttplugClient.ServerDisconnect += ServerDisconnect;

Logger.LogInfo("Connecting to Buttplug server");
await buttplugClient.ConnectAsync(new ButtplugWebsocketConnectorOptions(new Uri($"{PrefsManager.Instance.GetStringLocal(UKButtProperties.SocketUri, "ws://localhost:12345")}/buttplug")));

var startScanningTask = buttplugClient.StartScanningAsync();
_logQueue.Enqueue("Connecting to Buttplug server...");
try
{
await startScanningTask;
await buttplugClient.ConnectAsync(new ButtplugWebsocketConnectorOptions(uri));

Task.Run(buttplugClient.StartScanningAsync);
}
catch (ButtplugException ex)
catch (Exception ex)
{
Logger.LogError($"Scanning failed: {ex.InnerException.Message}");
_errorLogQueue.Enqueue(ex.ToString());
}
}

Expand All @@ -124,6 +161,21 @@ public static void Tap(bool isMenu = false)

private void Update()
{
if (_logQueue.Count > 0 || _errorLogQueue.Count > 0)
{
while (_logQueue.Count > 0)
{
var message = _logQueue.Dequeue();
Log.Info(message);
}

while (_errorLogQueue.Count > 0)
{
var message = _errorLogQueue.Dequeue();
Log.Error(message);
}
}

if (buttplugClient == null) return;
if (emergencyStop) currentSpeed = 0;

Expand Down Expand Up @@ -214,19 +266,30 @@ private void UpdateHookArm()

private void AddDevice(object sender, DeviceAddedEventArgs args)
{
Debug.Log("Device Added: " + args.Device.Name);
_logQueue.Enqueue("Device Added: " + args.Device.Name);
connectedDevices.Add(args.Device);
}

private void RemoveDevice(object sender, DeviceRemovedEventArgs args)
{
Debug.Log("Device Removed: " + args.Device.Name);
_logQueue.Enqueue("Device Removed: " + args.Device.Name);
connectedDevices.Remove(args.Device);
}

private void ScanningFinished(object sender, EventArgs args)
{
Debug.Log("Scanning Finished");
_logQueue.Enqueue("Scanning Finished");
}

private void ErrorReceived(object sender, ButtplugExceptionEventArgs args)
{
_errorLogQueue.Enqueue("Error: " + args.Exception.Message);
}

private void ServerDisconnect(object sender, EventArgs args)
{
_logQueue.Enqueue("Server Disconnected");
Task.Run(TryKillClient);
}

private void OnDestroy()
Expand Down
2 changes: 1 addition & 1 deletion PluginInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ internal class PluginInfo
{
public const string GUID = "dev.pitr.ukbutt";
public const string NAME = "UKButt";
public const string VERSION = "1.2.0";
public const string VERSION = "1.4.0";
}
}
4 changes: 3 additions & 1 deletion UKButt.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
<Reference Include="Newtonsoft.Json">
<HintPath>lib\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="plog">
<HintPath>lib\plog.dll</HintPath>
</Reference>
<Reference Include="System" />

<Reference Include="System.Core" />
Expand Down Expand Up @@ -87,7 +90,6 @@
<Compile Include="PluginInfo.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UKButtProperties.cs" />
<Compile Include="ULTRAKILL\CommandRootHelper.cs" />
<Compile Include="ULTRAKILL\UKButt.cs" />
<Compile Include="ULTRAKILL\UKButtCycleMode.cs" />
<Compile Include="ULTRAKILL\UKButtStopCheat.cs" />
Expand Down
108 changes: 0 additions & 108 deletions ULTRAKILL/CommandRootHelper.cs

This file was deleted.

5 changes: 2 additions & 3 deletions ULTRAKILL/UKButt.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using GameConsole;
using GameConsole.CommandTree;

namespace UKButt.Commands
{
public class UKButt : CommandRootBackport
public class UKButt : CommandRoot
{
public UKButt(Console con) : base(con) { }
public override string Name => "UKButt";
Expand Down Expand Up @@ -105,7 +104,7 @@ protected override Branch BuildTree(Console con)
}),
Leaf("restart_client", () =>
{
Task.Run(ButtplugManager.Instance.RestartClient);
ButtplugManager.Instance.TryRestartClient();
})
});

Expand Down
Binary file added lib/plog.dll
Binary file not shown.

0 comments on commit 3061796

Please sign in to comment.