Skip to content

Commit

Permalink
LabratEyeTracking | v1.3.0
Browse files Browse the repository at this point in the history
Switched to MelonLoader
  • Loading branch information
200Tigersbloxed committed Jan 17, 2022
1 parent dd364e3 commit a3990fa
Show file tree
Hide file tree
Showing 10 changed files with 188 additions and 60 deletions.
83 changes: 83 additions & 0 deletions LabratEyeTracking/LabratEyeTracking/ConfigHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
using System;
using System.Reflection;
using MelonLoader;

namespace LabratEyeTracking
{
public static class ConfigHelper
{
public static Config LoadedConfig;
private static readonly string myCategory = "LabratEyeTracking";

private static MelonPreferences_Category category;

public static bool DoesEntryExist(string key) => MelonPreferences.HasEntry(category.Identifier, key);

private static void CreateEntryInConfig<T>(string key, string description, T defaultValue) =>
MelonPreferences.CreateEntry(category.Identifier, key, defaultValue, key, description);

private static T GetEntryInConfig<T>(string key) =>
MelonPreferences.GetEntry<T>(category.Identifier, key).Value;

private static void CreateConfig()
{
category = MelonPreferences.CreateCategory(myCategory);
LogHelper.Debug("Created Config!");
}

public static Config LoadConfig()
{
CreateConfig();
Config newConfig = new Config();
foreach (FieldInfo field in newConfig.GetType().GetFields())
{
// Try and get it's method
MethodInfo reload =
field.FieldType.GetMethod("ReloadValues", BindingFlags.Public | BindingFlags.Instance);
object fieldValue = field.GetValue(newConfig);
if (reload != null)
reload.Invoke(fieldValue, null);
else
LogHelper.Warn("Could not find ReloadValues method for Field " + field.Name);
}

LoadedConfig = newConfig;
LogHelper.Debug("Loaded Config!");
return newConfig;
}

public class Config
{
public ConfigValue<int> sdkType = new ConfigValue<int>("sdkType",
() => CreateEntryInConfig("sdkType", "Select the runtime used for Eye Tracking.", 0),
() => GetEntryInConfig<int>("sdkType"));
}

public class ConfigValue<T>
{
public string Key { get; }
public T Value { get; set; }
private Func<T> GetEntry;

public ConfigValue(string Key, Action CreateEntry, Func<T> GetEntry)
{
this.Key = Key;
if (!DoesEntryExist(Key))
{
CreateEntry.Invoke();
LogHelper.Debug($"Created entry {Key}");
}
this.GetEntry = GetEntry;
GetEntry.Invoke();
}

public T ReloadValues()
{
T value = GetEntry.Invoke();
Value = value;
LogHelper.Debug($"Reloaded Config Value {Key} and got Value {value}");
return value;
}
}
}
}
43 changes: 43 additions & 0 deletions LabratEyeTracking/LabratEyeTracking/EyeFollower.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using UnityEngine;

namespace LabratEyeTracking
{
public class EyeDriver : MonoBehaviour
{
private bool set = false;
private Transform playerHead;

private void OnEnable()
{
if(gameObject.GetComponent<Collider>() != null)
Destroy(gameObject.GetComponent<Collider>());
}

private void FixedUpdate()
{
if (playerHead != null)
{
Vector3 origin = playerHead.position + UniversalEyeData.CombinedEye.origin;
Vector3 direction = playerHead.forward + UniversalEyeData.CombinedEye.direction;
RaycastHit hit;
if (Physics.Raycast(origin, direction, out hit, Mathf.Infinity))
{
Debug.DrawRay(origin, direction * hit.distance, Color.yellow);
transform.position = hit.transform.position;
}
}
else
{
playerHead = GameHelper.FindPlayerModel().transform.Find("SteamVRObjects").Find("VRCamera");
if (!set && playerHead != null)
{
set = true;
playerHead.position = Vector3.zero;
playerHead.rotation = new Quaternion(0, 0, 0, 0);
LogHelper.Debug("Found Player Head!");
}
}
}
}
}
2 changes: 1 addition & 1 deletion LabratEyeTracking/LabratEyeTracking/HarmonyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static void updateDiscord()

public static void Patch()
{
Harmony.CreateAndPatchAll(typeof(discordPatch));
HarmonyLib.Harmony.CreateAndPatchAll(typeof(discordPatch));
LogHelper.Debug("Patched Harmony!");
}
}
Expand Down
29 changes: 10 additions & 19 deletions LabratEyeTracking/LabratEyeTracking/LabratEyeTracking.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,26 @@
</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 Include="Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>E:\SteamLibrary\steamapps\common\SCP Labrat\SCP Labrat_Data\Managed\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp">
<HintPath>..\..\..\SteamLibrary\steamapps\common\SCP Labrat\SCP Labrat_Data\Managed\Assembly-CSharp.dll</HintPath>
<Reference Include="MelonLoader, Version=0.5.2.0, Culture=neutral, PublicKeyToken=null">
<HintPath>E:\SteamLibrary\steamapps\common\SCP Labrat\MelonLoader\MelonLoader.dll</HintPath>
</Reference>
<Reference Include="BepInEx, Version=5.4.17.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\SteamLibrary\steamapps\common\SCP Labrat\BepInEx\core\BepInEx.dll</HintPath>
</Reference>
<Reference Include="Managed-PVR">
<HintPath>..\..\..\UnitySaves\pimaxETSDK\Assets\Plugins\Managed-PVR.dll</HintPath>
</Reference>
<Reference Include="SteamVR">
<HintPath>..\..\..\SteamLibrary\steamapps\common\SCP Labrat\SCP Labrat_Data\Managed\SteamVR.dll</HintPath>
</Reference>
<Reference Include="System.Buffers">
<HintPath>..\..\..\UnityPackageExtractor\Assets\Glia\Plugins\System.Buffers.dll</HintPath>
<Reference Include="SteamVR, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>E:\SteamLibrary\steamapps\common\SCP Labrat\SCP Labrat_Data\Managed\SteamVR.dll</HintPath>
</Reference>
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\SteamLibrary\steamapps\common\SCP Labrat\SCP Labrat_Data\Managed\UnityEngine.dll</HintPath>
<HintPath>E:\SteamLibrary\steamapps\common\SCP Labrat\SCP Labrat_Data\Managed\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\SteamLibrary\steamapps\common\SCP Labrat\SCP Labrat_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
<HintPath>E:\SteamLibrary\steamapps\common\SCP Labrat\SCP Labrat_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.PhysicsModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\SteamLibrary\steamapps\common\SCP Labrat\SCP Labrat_Data\Managed\UnityEngine.PhysicsModule.dll</HintPath>
<HintPath>E:\SteamLibrary\steamapps\common\SCP Labrat\SCP Labrat_Data\Managed\UnityEngine.PhysicsModule.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<HintPath>..\..\SteamLibrary\steamapps\common\SCP Labrat\SCP Labrat_Data\Managed\UnityEngine.UI.dll</HintPath>
<HintPath>E:\SteamLibrary\steamapps\common\SCP Labrat\SCP Labrat_Data\Managed\UnityEngine.UI.dll</HintPath>
</Reference>
</ItemGroup>

Expand Down
15 changes: 5 additions & 10 deletions LabratEyeTracking/LabratEyeTracking/LogHelper.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
using BepInEx.Logging;
using UnityEngine;
using Logger = BepInEx.Logging.Logger;
using MelonLoader;

namespace LabratEyeTracking
{
public class LogHelper
{
private static readonly ManualLogSource _logger = Logger.CreateLogSource("LabratEyeTracking");
private static readonly MelonLogger.Instance _logger = new MelonLogger.Instance("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);
public static void Debug(object message) => _logger.Msg(message);
public static void Warn(object message) => _logger.Warning(message);
public static void Error(object message) => _logger.Error(message);
}
}
38 changes: 19 additions & 19 deletions LabratEyeTracking/LabratEyeTracking/MainMod.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
using System;
using System.Collections;
using System.Threading;
using System.Threading.Tasks;
using BepInEx;
using BepInEx.Configuration;
using MelonLoader;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace LabratEyeTracking
{
[BepInPlugin("lol.fortnite.www.labrateyetracking", "LabratEyeTracking", "1.2.2")]
[BepInProcess("SCP Labrat.exe")]
class MainMod : BaseUnityPlugin
class MainMod : MelonMod
{
// Cache values so we don't waste CPU getting them
private GameObject PlayerModel = null;
Expand All @@ -21,24 +17,21 @@ class MainMod : BaseUnityPlugin

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

// Config Values
private ConfigEntry<int> sdkType;

// IEyeTracking Stuff
public static IEyeTracking currentEyeTrackingRuntime = null;

void Awake()
public override void OnApplicationLateStart()
{
HarmonyHelper.Patch();
// Load Config
sdkType = Config.Bind(new ConfigDefinition("SDK", "SDKs Configuration"), 0);
ConfigHelper.Config lc = ConfigHelper.LoadConfig();
// Get unmanaged assemblies where they need to be
UnmanagedAssemblyManager.Initialize(sdkType.Value);
UnmanagedAssemblyManager.Initialize(lc.sdkType.Value);
// Subscribe to Scene Loading Events
LogHelper.Debug("Subscribing to Scene Events...");
SceneManager.sceneLoaded += OnSceneLoaded;
LogHelper.Debug("Subscribed to Scene Events!");
switch (sdkType.Value)
switch (lc.sdkType.Value)
{
case 1:
// Start the SRanipal SDK
Expand All @@ -50,8 +43,8 @@ void Awake()
break;
default:
// None were selected
LogHelper.Warn($"Config values was set to {sdkType.Value}, which is not recognized as an SDK Type! Have you changed the config yet?");
LogHelper.Warn("You can find the config under 'BepInEx/config/lol.fortnite.www.labrateyetracking.cfg'");
LogHelper.Warn($"Config values was set to {lc.sdkType.Value}, which is not recognized as an SDK Type! Have you changed the config yet?");
LogHelper.Warn("You can find the config at 'UserData/MelonPreferences.cfg' in the LabratEyeTracking section");
LogHelper.Warn("Set the Value to 1 for SRanipal, or set the value to 2 for Pimax");
break;
}
Expand Down Expand Up @@ -89,9 +82,11 @@ void Awake()
};
}

void OnApplicationQuit()
public override void OnPreferencesSaved() => ConfigHelper.LoadConfig();

public override void OnApplicationQuit()
{
if (currentEyeTrackingRuntime.EyeTrackingEnabled)
if (currentEyeTrackingRuntime?.EyeTrackingEnabled ?? false)
{
LogHelper.Debug("Killing the Current Eye Tracker's SDK...");
currentEyeTrackingRuntime.Kill();
Expand All @@ -103,7 +98,7 @@ void SetupEyeValues()
PlayerModel = GameHelper.FindPlayerModel();
BlinkContainer = GameHelper.FindBlinkContainer(PlayerModel);
BlinkComponent = GameHelper.GetBlinkComponent(BlinkContainer);
if(sdkType.Value != 0) { GameHelper.SetupBlinkComponent(BlinkComponent); }
if(ConfigHelper.LoadedConfig.sdkType.Value != 0) { GameHelper.SetupBlinkComponent(BlinkComponent); }
// soon
//GameHelper.SetupUI();
LogHelper.Debug("Scene Values Setup!");
Expand All @@ -117,11 +112,16 @@ void OnSceneLoaded(Scene scene, LoadSceneMode mode)
currentScene = scene;
if (GameHelper.IsGameScene(scene))
{
/*
Task.Run(() =>
{
Thread.Sleep(2000);
SetupEyeValues();
GameObject newsphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
newsphere.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
newsphere.AddComponent<EyeDriver>();
});
*/
}
else
{
Expand All @@ -134,7 +134,7 @@ void OnSceneLoaded(Scene scene, LoadSceneMode mode)
}
catch (Exception e)
{
LogHelper.Critical("Type failed to load. EXCEPTION: " + e.ToString());
LogHelper.Error("Type failed to load. EXCEPTION: " + e.ToString());
}
}

Expand Down
9 changes: 7 additions & 2 deletions LabratEyeTracking/LabratEyeTracking/PimaxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,18 @@ private void UpdateEyeData()
{
x = eyeTracker.LeftEye.Gaze.x,
y = eyeTracker.LeftEye.Gaze.y,
Widen = eyeTracker.LeftEye.Openness
Widen = eyeTracker.LeftEye.Openness,
origin = eyeTracker.LeftEye.GazeOrigin.normalized,
direction = eyeTracker.LeftEye.GazeDirection.normalized

};
Eye RightEye = new Eye()
{
x = eyeTracker.RightEye.Gaze.x,
y = eyeTracker.RightEye.Gaze.y,
Widen = eyeTracker.RightEye.Openness
Widen = eyeTracker.RightEye.Openness,
origin = eyeTracker.RightEye.GazeOrigin.normalized,
direction = eyeTracker.RightEye.GazeDirection.normalized
};
UniversalEyeData.UpdateLeftEyeData(LeftEye);
UniversalEyeData.UpdateRightEyeData(RightEye);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using MelonLoader;
using LabratEyeTracking;

[assembly: MelonInfo(typeof(MainMod), "LabratEyeTracking", "1.3.0", "200Tigersbloxed")]
[assembly: MelonGame("Bezbro Games", "SCP Labrat")]
8 changes: 6 additions & 2 deletions LabratEyeTracking/LabratEyeTracking/SRanipalHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,17 @@ public void UpdateEyeData()
{
x = EyeData.verbose_data.left.pupil_position_in_sensor_area.x,
y = EyeData.verbose_data.left.pupil_position_in_sensor_area.y,
Widen = EyeData.verbose_data.left.eye_openness
Widen = EyeData.verbose_data.left.eye_openness,
origin = EyeData.verbose_data.left.gaze_origin_mm.normalized,
direction = EyeData.verbose_data.left.gaze_direction_normalized
};
Eye RightEye = new Eye()
{
x = EyeData.verbose_data.right.pupil_position_in_sensor_area.x,
y = EyeData.verbose_data.right.pupil_position_in_sensor_area.y,
Widen = EyeData.verbose_data.right.eye_openness
Widen = EyeData.verbose_data.right.eye_openness,
origin = EyeData.verbose_data.right.gaze_origin_mm.normalized,
direction = EyeData.verbose_data.right.gaze_direction_normalized
};
UniversalEyeData.UpdateLeftEyeData(LeftEye);
UniversalEyeData.UpdateRightEyeData(RightEye);
Expand Down
Loading

0 comments on commit a3990fa

Please sign in to comment.