Added EFT TrackIR plugin

This commit is contained in:
jonbons 2024-05-04 01:53:49 -05:00
parent 679f2eda5f
commit d79e7a4c62
No known key found for this signature in database
3 changed files with 244 additions and 0 deletions

View File

@ -0,0 +1,45 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>JBTrackIR</AssemblyName>
<Description>My first plugin</Description>
<Version>1.0.0</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
<RestoreAdditionalProjectSources>
https://api.nuget.org/v3/index.json;
https://nuget.bepinex.dev/v3/index.json;
https://nuget.samboy.dev/v3/index.json
</RestoreAdditionalProjectSources>
<RootNamespace>JBTrackIR</RootNamespace>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>..\References\0Harmony.dll</HintPath>
</Reference>
<Reference Include="Aki.Reflection">
<HintPath>..\References\Aki.Reflection.dll</HintPath>
</Reference>
<Reference Include="Assembly-CSharp">
<HintPath>..\References\Assembly-CSharp.dll</HintPath>
</Reference>
<Reference Include="BepInEx">
<HintPath>..\References\BepInEx.dll</HintPath>
</Reference>
<Reference Include="TrackIRUnity">
<HintPath>..\References\TrackIRUnity.dll</HintPath>
</Reference>
<Reference Include="UnityEngine">
<HintPath>..\References\UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>..\References\UnityEngine.CoreModule.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

25
JBTrackIR/JBTrackIR.sln Normal file
View File

@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33516.290
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JBTrackIR", "JBTrackIR.csproj", "{A4AC854D-33F9-464C-A05D-E1030F892CB1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A4AC854D-33F9-464C-A05D-E1030F892CB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A4AC854D-33F9-464C-A05D-E1030F892CB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A4AC854D-33F9-464C-A05D-E1030F892CB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A4AC854D-33F9-464C-A05D-E1030F892CB1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C22410CD-BDAB-4AE8-87C7-E5CF5D1A0FFA}
EndGlobalSection
EndGlobal

174
JBTrackIR/Plugin.cs Normal file
View File

@ -0,0 +1,174 @@
using Aki.Reflection.Patching;
using BepInEx;
using BepInEx.Configuration;
using EFT;
using EFT.Animations;
using EFT.InventoryLogic;
using HarmonyLib;
using RootMotion;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Security.Cryptography;
using TrackIRUnity;
using UnityEngine;
namespace JBTrackIR;
[BepInPlugin("com.jonbons.trackir", "JonBons.TrackIR", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
static ConfigEntry<bool> tirEnabled;
static ConfigEntry<float> tirSensitivityCoef;
static ConfigEntry<int> tirLimitPitchLower;
static ConfigEntry<int> tirLimitPitchUpper;
static ConfigEntry<int> tirLimitYawLower;
static ConfigEntry<int> tirLimitYawUpper;
static TrackIRClient tirClient;
static bool tirRunning = false;
private void Awake()
{
// Plugin startup logic
Logger.LogInfo($"Plugin com.jonbons.trackir is loaded!");
tirClient = new TrackIRClient();
if (tirClient != null && !tirRunning)
{
tirClient.TrackIR_Enhanced_Init();
tirRunning = true;
Logger.LogInfo($"com.jonbons.trackir: trackir is running");
}
BindSettings();
new Transpiler().Enable();
}
private void OnDestroy()
{
if (tirClient != null && tirRunning)
{
tirClient.TrackIR_Shutdown();
tirRunning = false;
}
}
private void BindSettings()
{
tirEnabled = Config.Bind(
"Main Settings",
"TrackIR Enabled",
true,
new ConfigDescription("Enable TrackIR support")
);
tirSensitivityCoef = Config.Bind(
"Main Settings",
"TrackIR Sensitivity coef",
0.5f,
new ConfigDescription("Senstivity coefficient to apply to all TrackIR inputs",
new AcceptableValueRange<float>(0, 1))
);
tirLimitPitchLower = Config.Bind(
"Main Settings",
"TrackIR Pitch lower limit",
-85,
new ConfigDescription("Lower limit of TrackIR pitch angles",
new AcceptableValueRange<int>(-180, 180))
);
tirLimitPitchUpper = Config.Bind(
"Main Settings",
"TrackIR Pitch upper limit",
85,
new ConfigDescription("Upper limit of TrackIR pitch angles",
new AcceptableValueRange<int>(-180, 180))
);
tirLimitYawLower = Config.Bind(
"Main Settings",
"TrackIR Yaw lower limit",
-150,
new ConfigDescription("Lower limit of TrackIR yaw angles",
new AcceptableValueRange<int>(-180, 180))
);
tirLimitYawUpper = Config.Bind(
"Main Settings",
"TrackIR Yaw upper limit",
150,
new ConfigDescription("Upper limit of TrackIR yaw angles",
new AcceptableValueRange<int>(-180, 180))
);
}
[Serializable]
public class Limit
{
public Limit()
{
lower = 0;
upper = 360;
}
public Limit(float low, float up)
{
lower = low;
upper = up;
}
public float lower, upper;
}
public class Transpiler : ModulePatch
{
protected override MethodBase GetTargetMethod()
{
return typeof(Player).GetMethod("Look", BindingFlags.Instance | BindingFlags.Public);
}
[PatchPostfix]
private static void PatchPostfix(ref Player __instance)
{
if (!tirRunning) return;
if (!tirEnabled.Value) return;
float positionReductionFactor = 0.045f * tirSensitivityCoef.Value;
float rotationReductionFactor = 0.045f * tirSensitivityCoef.Value;
Limit positionXLimits = new Limit();
Limit positionYLimits = new Limit();
Limit positionZLimits = new Limit();
Limit pitchLimits = new Limit(tirLimitPitchLower.Value, tirLimitPitchUpper.Value);
Limit yawLimits = new Limit(tirLimitYawLower.Value, tirLimitYawUpper.Value);
Limit rollLimits = new Limit(-100, 100);
bool useLimits = true;
//Logger.LogInfo(string.Format("TIR DATA Start"));
TrackIRClient tirClient = Plugin.tirClient;
TrackIRClient.LPTRACKIRDATA tid = tirClient.client_HandleTrackIRData(); // Data for head tracking
//Logger.LogInfo(string.Format("TIR DATA Pitch = {0}; Yaw = {1}, Roll = {2}", tid.fNPPitch, tid.fNPYaw, tid.fNPRoll));
Vector3 rot = __instance.ProceduralWeaponAnimation.HandsContainer.CameraTransform.localRotation.eulerAngles;
rot.z = 0; // we don't need to use the existing Z value
if (!useLimits)
{
rot.y = tid.fNPYaw * rotationReductionFactor;
rot.x = tid.fNPPitch * rotationReductionFactor;
//rot.z = -tid.fNPRoll * rotationReductionFactor;
}
else
{
rot.y = Mathf.Clamp(tid.fNPYaw * rotationReductionFactor, yawLimits.lower, yawLimits.upper);
rot.x = Mathf.Clamp(tid.fNPPitch * rotationReductionFactor, pitchLimits.lower, pitchLimits.upper);
//rot.z = Mathf.Clamp(-tid.fNPRoll * rotationReductionFactor, rollLimits.lower, rollLimits.upper);
}
__instance.ProceduralWeaponAnimation.SetHeadRotation(rot);
//Logger.LogInfo(string.Format("TIR DATA Final pos = {0}; Final rot = {1}", pos, rot));
}
}
}