Skip to content

Commit

Permalink
LabratEyeTracking | v1.2.2
Browse files Browse the repository at this point in the history
Optimizations and critical fixes
  • Loading branch information
200Tigersbloxed committed Dec 29, 2021
1 parent 4a5d0e8 commit dd364e3
Show file tree
Hide file tree
Showing 9 changed files with 116 additions and 65 deletions.
1 change: 0 additions & 1 deletion LabratEyeTracking/LabratEyeTracking/Blinking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public static class Blinking

public static void CloseEyes(Activateblink BlinkComponent)
{
BlinkComponent.blinkingEnabled = true;
BlinkComponent.Forcedblink();
IsBlinking = true;
}
Expand Down
7 changes: 1 addition & 6 deletions LabratEyeTracking/LabratEyeTracking/GameHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,8 @@ public static void SetupBlinkComponent(Activateblink blinkcomponent)
blinkcomponent.blinkingEnabled = false; blinkcomponent.fadeIn = 0;
blinkcomponent.fadeOut = 0;
}
public static bool IsGameScene(Scene scene)
{
bool valToReturn = false;
if (scene.name == "Game") { valToReturn = true; }

return valToReturn;
}
public static bool IsGameScene(Scene scene) => scene.name == "Game";

// soon
/*
Expand Down
32 changes: 32 additions & 0 deletions LabratEyeTracking/LabratEyeTracking/HarmonyHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using HarmonyLib;

namespace LabratEyeTracking
{
public class HarmonyHelper
{
public static class initcache
{
public static bool hasinit;
}

class discordPatch
{
[HarmonyPatch(typeof(discordManager), "updateDiscord")]
[HarmonyPostfix]
static void updateDiscord()
{
if (!initcache.hasinit)
{
MainMod.currentEyeTrackingRuntime?.Init();
initcache.hasinit = true;
}
}
}

public static void Patch()
{
Harmony.CreateAndPatchAll(typeof(discordPatch));
LogHelper.Debug("Patched Harmony!");
}
}
}
3 changes: 3 additions & 0 deletions LabratEyeTracking/LabratEyeTracking/LabratEyeTracking.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
</PropertyGroup>

<ItemGroup>
<Reference Include="0Harmony, Version=2.5.5.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\SteamLibrary\steamapps\common\SCP Labrat\BepInEx\core\0Harmony.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp">
<HintPath>..\..\..\SteamLibrary\steamapps\common\SCP Labrat\SCP Labrat_Data\Managed\Assembly-CSharp.dll</HintPath>
</Reference>
Expand Down
12 changes: 8 additions & 4 deletions LabratEyeTracking/LabratEyeTracking/LogHelper.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
using BepInEx.Logging;
using UnityEngine;
using Logger = BepInEx.Logging.Logger;

namespace LabratEyeTracking
{
public class LogHelper
{
public static void Debug(object message) => UnityEngine.Debug.Log("[LabratEyeTracking] (DEBUG): " + message);
public static void Warn(object message) => UnityEngine.Debug.LogWarning("[LabratEyeTracking] (WARN): " + message);
public static void Error(object message) => UnityEngine.Debug.LogError("[LabratEyeTracking] (ERROR): " + message);
public static void Critical(object message) => UnityEngine.Debug.LogError("[LabratEyeTracking] (CRITICAL): " + message);
private static readonly ManualLogSource _logger = Logger.CreateLogSource("LabratEyeTracking");

public static void Debug(object message) => _logger.Log(LogLevel.Info, "[LabratEyeTracking] (DEBUG): " + message);
public static void Warn(object message) => _logger.LogWarning("[LabratEyeTracking] (WARN): " + message);
public static void Error(object message) => _logger.LogError("[LabratEyeTracking] (ERROR): " + message);
public static void Critical(object message) => _logger.LogError("[LabratEyeTracking] (CRITICAL): " + message);
}
}
86 changes: 46 additions & 40 deletions LabratEyeTracking/LabratEyeTracking/MainMod.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
using System;
using System.Collections;
using System.Threading;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace LabratEyeTracking
{
[BepInPlugin("lol.fortnite.www.labrateyetracking", "LabratEyeTracking", "1.2.1")]
[BepInPlugin("lol.fortnite.www.labrateyetracking", "LabratEyeTracking", "1.2.2")]
[BepInProcess("SCP Labrat.exe")]
class MainMod : BaseUnityPlugin
{
private bool SubscribeEyeData = false;

// Cache values so we don't waste CPU getting them
private GameObject PlayerModel = null;
private GameObject BlinkContainer = null;
private Activateblink BlinkComponent = null;
private static Activateblink BlinkComponent = null;
private Scene currentScene;

public static Action<Eye, Eye, Eye> OnEyeDataUpdate = (leftEye, rightEye, combinedEye) => { };

// Config Values
private ConfigEntry<int> sdkType;

// IEyeTracking Stuff
private IEyeTracking currentEyeTrackingRuntime = null;
public static IEyeTracking currentEyeTrackingRuntime = null;

void Awake()
{
HarmonyHelper.Patch();
// Load Config
sdkType = Config.Bind(new ConfigDefinition("SDK", "SDKs Configuration"), 0);
// Get unmanaged assemblies where they need to be
Expand All @@ -39,12 +42,10 @@ void Awake()
{
case 1:
// Start the SRanipal SDK
LogHelper.Debug("Initializing SRanipal SDK...");
currentEyeTrackingRuntime = new SRanipalHelper();
break;
case 2:
// Start the Pimax Eye Tracker
LogHelper.Debug("Initializing Pimax Eye Tracking...");
currentEyeTrackingRuntime = new PimaxHelper();
break;
default:
Expand All @@ -54,8 +55,38 @@ void Awake()
LogHelper.Warn("Set the Value to 1 for SRanipal, or set the value to 2 for Pimax");
break;
}
if (currentEyeTrackingRuntime != null)
currentEyeTrackingRuntime.Init();
// Events
OnEyeDataUpdate += (leftEye, rightEye, combinedEye) =>
{
if (GameHelper.IsGameScene(TryGetScene()))
{
// Verify the EyeTracking is active
if (currentEyeTrackingRuntime.EyeTrackingEnabled)
{
// Check if the user is blinking
if (BlinkComponent != null)
{
// set the UI text soon
//GameHelper.SetUILabelText(SRanipalHelper.EyeData.verbose_data.combined.eye_data.eye_openness.ToString());
// Left and Right are separate, because combined wasn't working for me
if (combinedEye.Widen <= 0.4f)
{
Blinking.UpdateWaitTime(BlinkComponent, Mathf.Infinity);
if (!Blinking.IsBlinking) { Blinking.CloseEyes(BlinkComponent); }
}
else
{
BlinkComponent.waitBetween = 0;
if (Blinking.IsBlinking) { Blinking.OpenEyes(BlinkComponent); }
}
}
else
{
LogHelper.Warn("Blink Component is null!");
}
}
}
};
}

void OnApplicationQuit()
Expand All @@ -72,40 +103,12 @@ void SetupEyeValues()
PlayerModel = GameHelper.FindPlayerModel();
BlinkContainer = GameHelper.FindBlinkContainer(PlayerModel);
BlinkComponent = GameHelper.GetBlinkComponent(BlinkContainer);
if(sdkType.Value != 0) { GameHelper.SetupBlinkComponent(BlinkComponent); SubscribeEyeData = true; }
if(sdkType.Value != 0) { GameHelper.SetupBlinkComponent(BlinkComponent); }
// soon
//GameHelper.SetupUI();
LogHelper.Debug("Scene Values Setup!");
}

void Update()
{
if (GameHelper.IsGameScene(TryGetScene()))
{
// Verify the EyeTracking is active
if (currentEyeTrackingRuntime.EyeTrackingEnabled)
{
// Check if the user is blinking
if (BlinkComponent != null)
{
// set the UI text soon
//GameHelper.SetUILabelText(SRanipalHelper.EyeData.verbose_data.combined.eye_data.eye_openness.ToString());
// Left and Right are separate, because combined wasn't working for me
if (UniversalEyeData.CombinedEye.Widen <= 0.4f)
{
Blinking.UpdateWaitTime(BlinkComponent, Mathf.Infinity);
if (!Blinking.IsBlinking) { Blinking.CloseEyes(BlinkComponent); }
}
else
{
BlinkComponent.waitBetween = 0;
if (Blinking.IsBlinking) { Blinking.OpenEyes(BlinkComponent); }
}
}
}
}
}

void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
LogHelper.Debug("Scene Loaded.");
Expand All @@ -114,8 +117,11 @@ void OnSceneLoaded(Scene scene, LoadSceneMode mode)
currentScene = scene;
if (GameHelper.IsGameScene(scene))
{
SubscribeEyeData = false;
SetupEyeValues();
Task.Run(() =>
{
Thread.Sleep(2000);
SetupEyeValues();
});
}
else
{
Expand Down
5 changes: 3 additions & 2 deletions LabratEyeTracking/LabratEyeTracking/PimaxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ public class PimaxHelper : IEyeTracking

private Thread PimaxWorker;

public void Initialize() => Init();

public void Init()
{
PimaxWorker = new Thread(delegate ()
{
LogHelper.Debug("Initializing Pimax Eye Tracking...");
eyeTracker = new Pimax.EyeTracking.EyeTracker();
eyeTracker.OnStart += OnEyeTrackerStart;
eyeTracker.OnStop += OnEyeTrackerStop;
Expand Down Expand Up @@ -63,6 +62,8 @@ private void UpdateEyeData()
};
UniversalEyeData.UpdateLeftEyeData(LeftEye);
UniversalEyeData.UpdateRightEyeData(RightEye);
MainMod.OnEyeDataUpdate.Invoke(UniversalEyeData.LeftEye, UniversalEyeData.RightEye,
UniversalEyeData.CombinedEye);
}

public void Kill()
Expand Down
33 changes: 21 additions & 12 deletions LabratEyeTracking/LabratEyeTracking/SRanipalHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ViveSR;
using ViveSR.anipal;
using ViveSR.anipal.Eye;
Expand All @@ -15,31 +16,37 @@ namespace LabratEyeTracking
public class SRanipalHelper : IEyeTracking
{
public bool EyeTrackingEnabled { get; set; } = false;
private bool VerifyEyeError(Error error) => error != Error.WORK && error != Error.UNDEFINED && error != (Error)1051;
private Thread SRanipalWorker;

public static EyeData_v2 EyeData;

public void Init()
{
if (EyeTrackingEnabled)
if (!EyeTrackingEnabled)
{
LogHelper.Warn("SRanipal was already active, restarting... Please wait 5 seconds.");
SRanipal_API.Release(SRanipal_Eye_v2.ANIPAL_TYPE_EYE_V2);
Thread.Sleep(5000);
StartThread();
}
StartThread();
else
LogHelper.Warn("SRanipal is already initialized!");
}

public void StartThread()
{
SRanipalWorker = new Thread(delegate()
SRanipalWorker = new Thread(() =>
{
Error eyeError = Error.UNDEFINED;
eyeError = SRanipal_API.Initial(SRanipal_Eye_v2.ANIPAL_TYPE_EYE_V2, IntPtr.Zero);

if (VerifyEyeError(eyeError)) { LogHelper.Error("Failed to Initialize SRanipal Eye Tracking! EXCEPTION: " + eyeError.ToString()); return; }
LogHelper.Debug("SRanipal Eye Tracking Initialized!"); EyeTrackingEnabled = true;
LogHelper.Debug("Initializing SRanipal SDK...");
EyeTrackingEnabled = true;
try
{
SRanipal_API.Initial(SRanipal_Eye_v2.ANIPAL_TYPE_EYE_V2, IntPtr.Zero);
LogHelper.Debug("SRanipal Eye Tracking Initialized!");
}
catch (Exception e)
{
EyeTrackingEnabled = false;
LogHelper.Error("Failed to Initialize SRanipal Eye Tracking! EXCEPTION: " + e);
return;
}
while (EyeTrackingEnabled)
{
UpdateEyeData();
Expand Down Expand Up @@ -88,6 +95,8 @@ public void UpdateEyeData()
};
UniversalEyeData.UpdateLeftEyeData(LeftEye);
UniversalEyeData.UpdateRightEyeData(RightEye);
MainMod.OnEyeDataUpdate.Invoke(UniversalEyeData.LeftEye, UniversalEyeData.RightEye,
UniversalEyeData.CombinedEye);
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions LabratEyeTracking/LabratEyeTracking/UniversalEyeData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class Eye
public float x;
public float y;
public float Widen;

public override string ToString() => $"xy({x},{y}) widen({Widen})";
}

public interface IEyeTracking
Expand Down

0 comments on commit dd364e3

Please sign in to comment.