Skip to content

Commit

Permalink
Fix HRtoVRChat
Browse files Browse the repository at this point in the history
Please see the README for the latest release of HRtoVRChat!
  • Loading branch information
200Tigersbloxed committed May 1, 2022
1 parent c411305 commit 5ae23b9
Show file tree
Hide file tree
Showing 22 changed files with 388 additions and 5 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
168 changes: 168 additions & 0 deletions HRtoVRChat/HRtoVRChat/HRManagers/OmniceptManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using System;
using System.Threading;
using HP.Omnicept;
using HP.Omnicept.Messaging;
using HP.Omnicept.Messaging.Messages;
using UnhollowerBaseLib;

namespace HRtoVRChat.HRManagers;

public class OmniceptManager : HRManager
{
public int HR { get; set; }

private Glia m_gliaClient;
private GliaValueCache m_gliaValCache;
private bool m_isConnected;
private HeartRate lastHeartRate;

private Thread _worker;
private CancellationTokenSource token;
private void VerifyDeadThread()
{
if (_worker != null)
{
if(_worker.IsAlive)
_worker.Abort();
_worker = null;
}
token = new CancellationTokenSource();
}

private void StopGlia()
{
// Verify Glia is Disposed
if(m_gliaValCache != null)
m_gliaValCache?.Stop();
if(m_gliaClient != null)
m_gliaClient?.Dispose();
m_gliaValCache = null;
m_gliaClient = null;
m_isConnected = false;
Glia.cleanupNetMQConfig();
}

private bool StartGlia(bool isTesting = false)
{
// Verify Glia is Disposed
StopGlia();
bool ret;

// Start Glia
try
{
m_gliaClient = new Glia("HRtoVRChat",
new SessionLicense(String.Empty, String.Empty, LicensingModel.Core, false));
m_gliaValCache = new GliaValueCache(m_gliaClient.Connection);
SubscriptionList sl = new SubscriptionList
{
Subscriptions =
{
new Subscription(MessageTypes.ABI_MESSAGE_HEART_RATE, String.Empty, String.Empty,
String.Empty, String.Empty, new MessageVersionSemantic("1.0.0"))
}
};
m_gliaClient.setSubscriptions(sl);
m_isConnected = true;
ret = true;
}
catch (Exception)
{
m_isConnected = false;
ret = false;
}
if (!m_isConnected || isTesting)
StopGlia();
if (isTesting)
return ret;
return m_isConnected;
}

void HandleMessage(ITransportMessage msg)
{
switch (msg.Header.MessageType)
{
case MessageTypes.ABI_MESSAGE_HEART_RATE:
lastHeartRate = m_gliaClient.Connection.Build<HeartRate>(msg);
break;
}
}

ITransportMessage RetrieveMessage()
{
ITransportMessage msg = null;
if (m_gliaValCache != null)
{
try
{
msg = m_gliaValCache.GetNext();
}
catch (HP.Omnicept.Errors.TransportError e)
{
LogHelper.Error("OmniceptManager", e.Message);
}
}
return msg;
}

public bool Init(string d1)
{
bool status = StartGlia(true);
if (status)
{
LogHelper.Log("OmniceptManager", "Started Omnicept!");
StartThread();
}
else
LogHelper.Error("OmniceptManager", "Failed to start Omnicept!");
return status;
}

public void StartThread()
{
VerifyDeadThread();
_worker = new Thread(() =>
{
IL2CPP.il2cpp_thread_attach(IL2CPP.il2cpp_domain_get());
LogHelper.Debug("OmniceptManager", "Omnicept Thread Started");
StartGlia();
while (!token.IsCancellationRequested)
{
// Update message
try
{
if (m_isConnected)
{
ITransportMessage msg = RetrieveMessage();
if(msg != null)
HandleMessage(msg);
}
}
catch (Exception e)
{
LogHelper.Error("OmniceptManager" , "Failed to get message! " + e);
}
if (lastHeartRate != null)
{
HR = (int)lastHeartRate.Rate;
}
Thread.Sleep(10);
}
// Thread-Safe; don't need to call it in this thread :)
//StopGlia();
});
_worker.Start();
}

public int GetHR() => HR;

public void Stop()
{
StopGlia();
token.Cancel();
}

public bool IsOpen() => m_isConnected;

public bool IsActive() => IsOpen();
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
85 changes: 85 additions & 0 deletions HRtoVRChat/HRtoVRChat/HRManagers/SDKManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System.Linq;
using System.Threading;
using HRtoVRChat_OSC_SDK;
using SuperSimpleTcp;

namespace HRtoVRChat.HRManagers;

public class SDKManager : HRManager
{
private Thread _worker;
private CancellationTokenSource token;

private SimpleTcpServer server;

private int HR;
private bool isActive;
private bool isOpen;

public bool Init(string d1)
{
if (_worker != null)
{
token.Cancel();
}

token = new CancellationTokenSource();
_worker = new Thread(() =>
{
server = new SimpleTcpServer(d1);
server.Events.ClientConnected += (sender, args) => LogHelper.Log("SDKManager", "SDK Connected!");
server.Events.ClientDisconnected += (sender, args) => LogHelper.Log("SDKManager", "SDK Disconnected!");
server.Events.DataReceived += (sender, args) =>
{
byte[] data = args.Data;
object? fakeDeserialize = Messages.DeserializeMessage(data);
string messageType = Messages.GetMessageType(fakeDeserialize);
switch (messageType)
{
case "HRMessage":
Messages.HRMessage hrm = Messages.DeserializeMessage<Messages.HRMessage>(data);
HR = hrm.HR;
isActive = hrm.IsActive;
isOpen = hrm.IsOpen;
break;
case "GetHRData":
Messages.HRMessage hrm_ghrd = new Messages.HRMessage
{
HR = HR,
IsActive = isActive,
IsOpen = isOpen
};
server.Send(args.IpPort, hrm_ghrd.Serialize());
break;
default:
LogHelper.Warn("SDKManager", "Unknown Debug Message: " + messageType);
break;
}
};
server.Start();
while (!token.IsCancellationRequested)
{
int c = server.GetClients().ToList().Count;
if (c <= 0)
{
// reset all data
HR = 0;
isOpen = false;
isActive = false;
}
Thread.Sleep(10);
}
server?.Stop();
});
_worker.Start();
return true;
}

public int GetHR() => HR;

public void Stop() => token.Cancel();

public bool IsOpen() => isOpen;

public bool IsActive() => isActive;
}
65 changes: 65 additions & 0 deletions HRtoVRChat/HRtoVRChat/HRtoVRChat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,39 @@
<EmbeddedResource Include="hrtovrchat">
<CustomToolNamespace>HRtoVRChat</CustomToolNamespace>
</EmbeddedResource>
<None Remove="HRManagers\OmniceptLibs\AsyncIO.dll" />
<EmbeddedResource Include="HRManagers\OmniceptLibs\AsyncIO.dll" />
<None Remove="HRManagers\OmniceptLibs\Google.Protobuf.dll" />
<EmbeddedResource Include="HRManagers\OmniceptLibs\Google.Protobuf.dll" />
<None Remove="HRManagers\OmniceptLibs\lib-client-csharp.dll" />
<EmbeddedResource Include="HRManagers\OmniceptLibs\lib-client-csharp.dll" />
<None Remove="HRManagers\OmniceptLibs\NetMQ.dll" />
<EmbeddedResource Include="HRManagers\OmniceptLibs\NetMQ.dll" />
<None Remove="HRManagers\OmniceptLibs\SemanticVersioning.dll" />
<EmbeddedResource Include="HRManagers\OmniceptLibs\SemanticVersioning.dll" />
<None Remove="HRManagers\SDKLibs\HRtoVRChat_OSC_SDK.dll" />
<EmbeddedResource Include="HRManagers\SDKLibs\HRtoVRChat_OSC_SDK.dll" />
<None Remove="HRManagers\SDKLibs\protobuf-net.dll" />
<EmbeddedResource Include="HRManagers\SDKLibs\protobuf-net.dll" />
<None Remove="HRManagers\SDKLibs\protobuf-net.Core.dll" />
<EmbeddedResource Include="HRManagers\SDKLibs\protobuf-net.Core.dll" />
<None Remove="HRManagers\SDKLibs\SuperSimpleTcp.dll" />
<EmbeddedResource Include="HRManagers\SDKLibs\SuperSimpleTcp.dll" />
<None Remove="HRManagers\SDKLibs\System.Buffers.dll" />
<EmbeddedResource Include="HRManagers\SDKLibs\System.Buffers.dll" />
<None Remove="HRManagers\SDKLibs\System.Collections.Immutable.dll" />
<EmbeddedResource Include="HRManagers\SDKLibs\System.Collections.Immutable.dll" />
<None Remove="HRManagers\SDKLibs\System.Memory.dll" />
<EmbeddedResource Include="HRManagers\SDKLibs\System.Memory.dll" />
<None Remove="HRManagers\SDKLibs\System.Numerics.Vectors.dll" />
<EmbeddedResource Include="HRManagers\SDKLibs\System.Numerics.Vectors.dll" />
<None Remove="HRManagers\SDKLibs\System.Runtime.CompilerServices.Unsafe.dll" />
<EmbeddedResource Include="HRManagers\SDKLibs\System.Runtime.CompilerServices.Unsafe.dll" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="ILRepack" Version="2.1.0-beta1" />
<PackageReference Include="SemanticVersioning" Version="2.0.0" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Net.Requests" Version="4.3.0" />
<PackageReference Include="System.Net.WebSockets.Client" Version="4.3.2" />
Expand All @@ -31,15 +61,33 @@
<Reference Include="Assembly-CSharp">
<HintPath>$(VRChatLocation)\MelonLoader\Managed\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="AsyncIO, Version=0.1.25.0, Culture=neutral, PublicKeyToken=44a94435bd6f33f8">
<HintPath>..\..\HP\HP Omnicept SDK\1.13.1\bin\Release\csharp\AsyncIO.dll</HintPath>
</Reference>
<Reference Include="Google.Protobuf, Version=3.11.4.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604">
<HintPath>..\..\HP\HP Omnicept SDK\1.13.1\bin\Release\csharp\Google.Protobuf.dll</HintPath>
</Reference>
<Reference Include="HRtoVRChat_OSC_SDK, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>C:\Users\lukej\Downloads\HRtoVRChat_OSC_SDK\net48\HRtoVRChat_OSC_SDK.dll</HintPath>
</Reference>
<Reference Include="Il2Cppmscorlib">
<HintPath>$(VRChatLocation)\MelonLoader\Managed\Il2Cppmscorlib.dll</HintPath>
</Reference>
<Reference Include="lib-client-csharp, Version=1.13.1.3402, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\HP\HP Omnicept SDK\1.13.1\bin\Release\csharp\lib-client-csharp.dll</HintPath>
</Reference>
<Reference Include="MelonLoader">
<HintPath>$(VRChatLocation)\MelonLoader\MelonLoader.dll</HintPath>
</Reference>
<Reference Include="NetMQ, Version=4.0.0.1, Culture=neutral, PublicKeyToken=a6decef4ddc58b3a">
<HintPath>..\..\HP\HP Omnicept SDK\1.13.1\bin\Release\csharp\NetMQ.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>$(VRChatLocation)\MelonLoader\Managed\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="SuperSimpleTcp, Version=2.6.0.7, Culture=neutral, PublicKeyToken=null">
<HintPath>C:\Users\lukej\Downloads\HRtoVRChat_OSC_SDK\net48\SuperSimpleTcp.dll</HintPath>
</Reference>
<Reference Include="System.Net" />
<Reference Include="UnhollowerBaseLib">
<HintPath>$(VRChatLocation)\MelonLoader\Managed\UnhollowerBaseLib.dll</HintPath>
Expand Down Expand Up @@ -67,4 +115,21 @@
</Reference>
</ItemGroup>

<ItemGroup>
<Folder Include="HRManagers\OmniceptLibs" />
<Folder Include="HRManagers\SDKLibs" />
</ItemGroup>

<!--
Huge shoutout to benaclejames for help with Assembly Resolution!
https://github.com/benaclejames/VRCFaceTracking/blob/master/VRCFaceTracking/VRCFaceTracking.csproj#L140
-->
<Target Name="AfterResolveReferences">
<ItemGroup>
<EmbeddedResource Include="@(ReferenceCopyLocalPaths)" Condition="'%(ReferenceCopyLocalPaths.Extension)' == '.dll'">
<LogicalName>%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName>
</EmbeddedResource>
</ItemGroup>
</Target>

</Project>
Loading

0 comments on commit 5ae23b9

Please sign in to comment.