Skip to content

Commit

Permalink
Update / fix, works 1.16 throughout latest
Browse files Browse the repository at this point in the history
  • Loading branch information
kinsi55 committed Feb 16, 2024
1 parent 1625e01 commit 4c119b9
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 90 deletions.
64 changes: 33 additions & 31 deletions HarmonyPatches/Smoother.cs
@@ -1,7 +1,10 @@
using HarmonyLib;
using IPA.Utilities;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.XR;

Expand All @@ -13,37 +16,46 @@ class wrapper {
}

[HarmonyPatch(typeof(VRController), "Update")]
static class SaberSmoothFilter {
public static bool isSaber = true;

static void Prefix(VRController __instance) {
isSaber = __instance.gameObject.name[0] != 'C';
}
}

[HarmonyPatch]
public static class Smoother {
public static bool enabled = true;

static Dictionary<XRNode, wrapper> idk = new Dictionary<XRNode, wrapper>();

static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) {
if(!ILUtil.CheckIL(instructions, new Dictionary<int, OpCode>() {
{50, OpCodes.Ldarg_0},
{51, OpCodes.Ldfld},
{68, OpCodes.Ret}
})) return instructions;
static Dictionary<XRNode, wrapper> idk
= new Dictionary<XRNode, wrapper>();

ILUtil.InsertFn(50, ref instructions, AccessTools.Method(typeof(Smoother), nameof(YES)));
static IEnumerable<MethodBase> TargetMethods() {
var oldUnity = AccessTools.Method("OpenVRHelper:GetNodePose");

return instructions;
if(oldUnity != null) {
yield return oldUnity;
} else {
yield return AccessTools.Method("UnityXRHelper:GetNodePose");
yield return AccessTools.Method("OculusVRHelper:GetNodePose");
}
}

static float posSmoth = 20f - Mathf.Clamp(PluginConfig.Instance.PositionSmoothing, 0f, 20f);
static float rotSmoth = 20f - Mathf.Clamp(PluginConfig.Instance.RotationSmoothing, 0f, 20f);

static VRController instance;
static void YES() {
if(instance == null || !enabled || !PluginConfig.Instance.Enabled)
static void Postfix(IVRPlatformHelper __instance, XRNode nodeType, ref Vector3 pos, ref Quaternion rot) {
if(!enabled || !PluginConfig.Instance.Enabled || SaberSmoothFilter.isSaber)
return;

wrapper wrapperI = null;
if(nodeType != XRNode.LeftHand && nodeType != XRNode.RightHand)
return;

if(!idk.TryGetValue(instance.node, out wrapperI))
idk.Add(instance.node, wrapperI = new wrapper());
if(!idk.TryGetValue(nodeType, out var wrapperI))
idk.Add(nodeType, wrapperI = new wrapper());

var angDiff = Quaternion.Angle(wrapperI.smoothedRotation, instance.transform.localRotation);
var angDiff = Quaternion.Angle(wrapperI.smoothedRotation, rot);
wrapperI.angleVelocitySnap = Math.Min(wrapperI.angleVelocitySnap + angDiff, 90f);

var snapMulti = Mathf.Clamp(wrapperI.angleVelocitySnap / PluginConfig.Instance.SmallMovementThresholdAngle, 0.1f, 2.5f);
Expand All @@ -53,24 +65,14 @@ public static class Smoother {
}

if(PluginConfig.Instance.PositionSmoothing > 0f) {
wrapperI.smoothedPosition = Vector3.Lerp(wrapperI.smoothedPosition, instance.transform.localPosition, posSmoth * Time.deltaTime * snapMulti);
instance.transform.localPosition = wrapperI.smoothedPosition;
wrapperI.smoothedPosition = Vector3.Lerp(wrapperI.smoothedPosition, pos, posSmoth * Time.deltaTime * snapMulti);
pos = wrapperI.smoothedPosition;
}

if(PluginConfig.Instance.RotationSmoothing > 0f) {
wrapperI.smoothedRotation = Quaternion.Lerp(wrapperI.smoothedRotation, instance.transform.localRotation, rotSmoth * Time.deltaTime * snapMulti);
instance.transform.localRotation = wrapperI.smoothedRotation;
}
}

static void Prefix(VRController __instance, VRControllerTransformOffset ____transformOffset) {
// Check if the VRController's gameObject name starts with "C" (Controller) so that sabers (LeftHand / RightHand) are not smoothed lmao
if(__instance.gameObject.name[0] != 'C') {
instance = null;
return;
wrapperI.smoothedRotation = Quaternion.Lerp(wrapperI.smoothedRotation, rot, rotSmoth * Time.deltaTime * snapMulti);
rot = wrapperI.smoothedRotation;
}

instance = __instance;
}
}
}
31 changes: 0 additions & 31 deletions ILUtil.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Plugin.cs
Expand Up @@ -36,7 +36,7 @@ public class Plugin {

[OnExit]
public void OnApplicationQuit() {
harmony.UnpatchAll(harmony.Id);
harmony.UnpatchSelf();
}
}
}
51 changes: 24 additions & 27 deletions SmoothedController.csproj
Expand Up @@ -16,16 +16,20 @@
<RepositoryType></RepositoryType>
<RepositoryUrl></RepositoryUrl>
<Description></Description>
<Platforms>x64</Platforms>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>portable</DebugType>
</PropertyGroup>
<OutputPath>bin\Debug</OutputPath>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DebugType>none</DebugType>
</PropertyGroup>
<OutputPath>bin\</OutputPath>
</PropertyGroup>

<PropertyGroup Condition="$(DefineConstants.Contains('CIBuild')) OR '$(NCrunch)' == '1'">
<DisableCopyToPlugins>True</DisableCopyToPlugins>
Expand All @@ -34,9 +38,6 @@
<DisableCopyToPlugins>True</DisableCopyToPlugins>
<DisableZipRelease>True</DisableZipRelease>
</PropertyGroup>
<ItemGroup>
<None Remove="UI\settings.bsml" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="UI\settings.bsml" />
</ItemGroup>
Expand All @@ -46,7 +47,7 @@
<HintPath>$(BeatSaberDir)\Libs\0Harmony.dll</HintPath>
</Reference>
<Reference Include="BSML">
<HintPath>E:\SteamLibrary\steamapps\common\Beat Saber\Plugins\BSML.dll</HintPath>
<HintPath>$(BeatSaberDir)\Plugins\BSML.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -56,68 +57,64 @@
<Reference Include="System.Xml" />
<Reference Include="Main">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\Main.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="HMLib">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\HMLib.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="HMUI">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\HMUI.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="IPA.Loader">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\IPA.Loader.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Unity.TextMeshPro">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\Unity.TextMeshPro.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\UnityEngine.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.CoreModule">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\UnityEngine.CoreModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.UI">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\UnityEngine.UI.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.UIElementsModule">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\UnityEngine.UIElementsModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.UIModule">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\UnityEngine.UIModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.VRModule">
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\UnityEngine.VRModule.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.XRModule">
<HintPath>E:\SteamLibrary\steamapps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.XRModule.dll</HintPath>
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\UnityEngine.XRModule.dll</HintPath>
</Reference>
<Reference Include="VRUI">
<HintPath>E:\SteamLibrary\steamapps\common\Beat Saber\Beat Saber_Data\Managed\VRUI.dll</HintPath>
<HintPath>$(BeatSaberDir)\Beat Saber_Data\Managed\VRUI.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="manifest.json" />
</ItemGroup>
<ItemGroup>
<None Include="Directory.Build.props" Condition="Exists('Directory.Build.props')" />
<None Include="Directory.Build.targets" Condition="Exists('Directory.Build.targets')" />
<None Include="BSPLUGIN_TEMPLATE.csproj.user" Condition="Exists('BSPLUGIN_TEMPLATE.csproj.user')" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="BeatSaberModdingTools.Tasks">
<Version>1.4.3</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<IncludeAssets>build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<Reference Update="System.Drawing"></Reference>
</ItemGroup>
<ItemGroup>
<Reference Update="System.IO.Compression.FileSystem"></Reference>
</ItemGroup>
<ItemGroup>
<Reference Update="System.Numerics"></Reference>
</ItemGroup>
<ItemGroup>
<Reference Update="System.Runtime.Serialization"></Reference>
</ItemGroup>
</Project>
11 changes: 11 additions & 0 deletions SuppressRefereces.targets
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

<!-- make all references non-private, so they won't be copied to the output folder -->
<Target Name="ClearReferenceCopyLocalPaths" AfterTargets="ResolveAssemblyReferences">
<ItemGroup>
<ReferenceCopyLocalPaths Remove="@(ReferenceCopyLocalPaths)" />
</ItemGroup>
</Target>

</Project>

0 comments on commit 4c119b9

Please sign in to comment.