Skip to content

Commit

Permalink
Merge pull request #4 from Earthmark/main
Browse files Browse the repository at this point in the history
Migrated to using ALVR v20
  • Loading branch information
dfgHiatus authored May 15, 2023
2 parents 10fb0fb + 9e1b9bc commit 619131c
Show file tree
Hide file tree
Showing 14 changed files with 919 additions and 1,023 deletions.
600 changes: 0 additions & 600 deletions Interface/ALXR/ALXRModule.cs

This file was deleted.

73 changes: 73 additions & 0 deletions Interface/AlvrConnection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using BaseX;
using System;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using OscCore;

namespace QuestProModule;

public class AlvrConnection : IDisposable
{
private readonly SyncCell<FbMessage> _messageTarget;

/// <summary>
/// A message owned by this reader that is currently being mutated.
/// </summary>
private FbMessage _workingMessage = new();

private readonly UdpClient _client;
private readonly CancellationTokenSource _stopToken = new();
private readonly Task _listenTask;

public AlvrConnection(int port, SyncCell<FbMessage> target)
{
_messageTarget = target;
_client = new UdpClient(port);
_listenTask = Task.Run(ListenAsync);
UniLog.Log($"Opening Alvr connection on {port}");
}

private async Task ListenAsync()
{
while (!_stopToken.IsCancellationRequested)
{
try
{
var got = await _client.ReceiveAsync();
var message = new OscMessageRaw(new ArraySegment<byte>(got.Buffer));
switch (message.Address)
{
case "/tracking/eye/left/Quat":
case "/tracking/eye/left/Active":
case "/tracking/eye/right/Quat":
case "/tracking/eye/right/Active":
// Ignore these, we do this ourselves.
break;
case "/tracking/eye_htc":
case "/tracking/lip_htc":
UniLog.Error("Unexpected ALVR message in loading area, please use facebook eye tracking.");
break;
case "/tracking/face_fb":
_workingMessage.ParseOsc(message);
if (!_stopToken.IsCancellationRequested)
{
_messageTarget.Swap(ref _workingMessage);
}
break;
}
}
catch (Exception ex)
{
UniLog.Error(ex.Message);
}
}
}

public void Dispose()
{
UniLog.Log("Alvr connection closing.");
_stopToken.Cancel();
_client.Dispose();
}
}
87 changes: 87 additions & 0 deletions Interface/AlvrMonitor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using BaseX;
using System;
using System.Diagnostics;
using System.IO;
using System.Timers;

namespace QuestProModule;

internal class AlvrMonitor : IDisposable
{
public string ClientName { get; set; }
public string ClientPath { get; set; }
public string Arguments { get; set; }

private readonly Timer _timer;

private Process _foundProcess;

public AlvrMonitor()
{
_timer = new(1000);
_timer.Elapsed += (o, e) => CheckProcess();
}

void CheckProcess()
{
if (!CheckIfRunning())
{
StartClient();
}
}

private bool CheckIfRunning()
{
Process[] alvrClients = Process.GetProcessesByName("alxr-client");
foreach (Process client in alvrClients)
{
if (_foundProcess is { HasExited: false })
{
client.Dispose();
continue;
}

WatchProcess(client);
}

return !(_foundProcess?.HasExited ?? true);
}

private void WatchProcess(Process process)
{
_foundProcess?.Dispose();
_foundProcess = null;

if (process.HasExited)
{
return;
}

_foundProcess = process;

_foundProcess.Exited += (o, e) => _timer.Start();
}

private void StartClient()
{
try
{
string directory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string clientPath = Path.Combine(directory, ClientPath);
var process = Process.Start(clientPath, Arguments);

UniLog.Log($"Starting ALXR at: {process.StartInfo.FileName}");
WatchProcess(process);
}
catch
{
UniLog.Log($"Failed to start process at {ClientPath}");
}
}

public void Dispose()
{
_timer.Dispose();
_foundProcess?.Dispose();
}
}
98 changes: 0 additions & 98 deletions Interface/EyeDevice.cs

This file was deleted.

Loading

0 comments on commit 619131c

Please sign in to comment.