From e628f702e131796d47c2ce11d23cd9d1bd0933ba Mon Sep 17 00:00:00 2001 From: Xenira <1288524+Xenira@users.noreply.github.com> Date: Fri, 15 Dec 2023 22:07:48 +0100 Subject: [PATCH] fix(input): :lock: multi place builder patches --- .vscode/settings.json | 5 +- README.adoc | 2 +- .../{ => builder}/builder_raycast_patch.cs | 2 +- .../patch/builder/builder_reassemble_patch.cs | 34 ++++++++ .../patch/builder/conveyor_builder_patch.cs | 16 ++++ .../builder/multi_place_builder_patch.cs | 16 ++++ .../camera/patch/builder_reassemble_patch.cs | 80 ------------------- plugin/src/util/patch/Transpile.cs | 56 +++++++++++++ 8 files changed, 127 insertions(+), 84 deletions(-) rename plugin/src/camera/patch/{ => builder}/builder_raycast_patch.cs (99%) create mode 100644 plugin/src/camera/patch/builder/builder_reassemble_patch.cs create mode 100644 plugin/src/camera/patch/builder/conveyor_builder_patch.cs create mode 100644 plugin/src/camera/patch/builder/multi_place_builder_patch.cs delete mode 100644 plugin/src/camera/patch/builder_reassemble_patch.cs create mode 100644 plugin/src/util/patch/Transpile.cs diff --git a/.vscode/settings.json b/.vscode/settings.json index 486133b..03c5632 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,7 @@ "input", "render", "ui", - "player" + "player", + "game" ] -} \ No newline at end of file +} diff --git a/README.adoc b/README.adoc index 129b529..7729382 100644 --- a/README.adoc +++ b/README.adoc @@ -69,10 +69,10 @@ While the mod is in a playable state, it is still in early development. Some fea - Button prompts are not for VR controllers. - Haptics are played on both controllers by the game. One improvement would be to play them on the hand that is actually holding the tool. - Use and interact buttons are mapped to the same button. This might cause issues when interacting with objects. -- The manual crank button is not mapped to a button on the controller. - Machine info not visible when targeting with hand. - Crafting queue not visible. - The game is locked to 60fps when running in windowed mode. This is based on the refresh rate of your monitor. To unlock the framerate, switch to fullscreen mode. (For now) +- Menus are a little jittery === Cool stuff to try - Tobii eye tracking for dynamic foveated rendering diff --git a/plugin/src/camera/patch/builder_raycast_patch.cs b/plugin/src/camera/patch/builder/builder_raycast_patch.cs similarity index 99% rename from plugin/src/camera/patch/builder_raycast_patch.cs rename to plugin/src/camera/patch/builder/builder_raycast_patch.cs index 435c95e..a518f2a 100644 --- a/plugin/src/camera/patch/builder_raycast_patch.cs +++ b/plugin/src/camera/patch/builder/builder_raycast_patch.cs @@ -5,7 +5,7 @@ using Plugin.Input; using UnityEngine; -namespace TechtonicaVR.VRCamera.Patch; +namespace TechtonicaVR.VRCamera.Patch.Builder; [HarmonyPatch] public class BuilderRaycastPatch diff --git a/plugin/src/camera/patch/builder/builder_reassemble_patch.cs b/plugin/src/camera/patch/builder/builder_reassemble_patch.cs new file mode 100644 index 0000000..47e0f20 --- /dev/null +++ b/plugin/src/camera/patch/builder/builder_reassemble_patch.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; +using HarmonyLib; +using TechtonicaVR.Util.Patch; + +namespace TechtonicaVR.VRCamera.Patch.Builder; + +[HarmonyPatch] +public class BuilderReassemblePatch +{ + private const int EXPECTED_PATCH_COUNT = 12; + + + + [HarmonyPatch(typeof(FloorBuilder), nameof(FloorBuilder.Reassemble))] + [HarmonyTranspiler] + public static IEnumerable ReassembleTranspiler(IEnumerable instructions) + { + return Transpile.reassemblePatch(instructions); + } + + [HarmonyPatch(typeof(ResearchCoreBuilder), nameof(ResearchCoreBuilder.Reassemble))] + [HarmonyTranspiler] + public static IEnumerable ResearchCoreReassembleTranspiler(IEnumerable instructions) + { + return Transpile.reassemblePatch(instructions); + } + + [HarmonyPatch(typeof(StairsBuilder), nameof(StairsBuilder.Reassemble))] + [HarmonyTranspiler] + public static IEnumerable StairsBuilderTranspiler(IEnumerable instructions) + { + return Transpile.reassemblePatch(instructions); + } +} diff --git a/plugin/src/camera/patch/builder/conveyor_builder_patch.cs b/plugin/src/camera/patch/builder/conveyor_builder_patch.cs new file mode 100644 index 0000000..73cd022 --- /dev/null +++ b/plugin/src/camera/patch/builder/conveyor_builder_patch.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using HarmonyLib; +using TechtonicaVR.Util.Patch; + +namespace TechtonicaVR.VRCamera.Patch.Builder; + +[HarmonyPatch] +class ConveyorBuilderPatch +{ + [HarmonyPatch(typeof(ConveyorBuilder), nameof(ConveyorBuilder.GetTargetEndPt))] + [HarmonyTranspiler] + public static IEnumerable ReassembleTranspiler(IEnumerable instructions) + { + return Transpile.reassemblePatch(instructions, 5); + } +} diff --git a/plugin/src/camera/patch/builder/multi_place_builder_patch.cs b/plugin/src/camera/patch/builder/multi_place_builder_patch.cs new file mode 100644 index 0000000..e08dd85 --- /dev/null +++ b/plugin/src/camera/patch/builder/multi_place_builder_patch.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using HarmonyLib; +using TechtonicaVR.Util.Patch; + +namespace TechtonicaVR.VRCamera.Patch.Builder; + +[HarmonyPatch] +class MultiPlaceBuilderPatch +{ + [HarmonyPatch(typeof(MultiPlaceBuilder), nameof(MultiPlaceBuilder.Reassemble))] + [HarmonyTranspiler] + public static IEnumerable ReassembleTranspiler(IEnumerable instructions) + { + return Transpile.reassemblePatch(instructions, 25); + } +} diff --git a/plugin/src/camera/patch/builder_reassemble_patch.cs b/plugin/src/camera/patch/builder_reassemble_patch.cs deleted file mode 100644 index ca2b26a..0000000 --- a/plugin/src/camera/patch/builder_reassemble_patch.cs +++ /dev/null @@ -1,80 +0,0 @@ -using System.Collections.Generic; -using System.Reflection; -using System.Reflection.Emit; -using HarmonyLib; -using Plugin.Input; -using UnityEngine; - -namespace TechtonicaVR.VRCamera.Patch; - -[HarmonyPatch] -public class BuilderReassemblePatch -{ - private const int EXPECTED_PATCH_COUNT = 12; - - private static MethodInfo getTransformMethod = typeof(Component).GetMethod("get_transform"); - private static MethodInfo getForwardMethod = typeof(Transform).GetMethod("get_forward"); - - private static MethodInfo getTransformPatchMethod = typeof(BuilderReassemblePatch).GetMethod(nameof(patchedGetTransform)); - private static MethodInfo getForwardPatchMethod = typeof(BuilderReassemblePatch).GetMethod(nameof(PatchedGetForward)); - - [HarmonyPatch(typeof(FloorBuilder), nameof(FloorBuilder.Reassemble))] - [HarmonyTranspiler] - public static IEnumerable ReassembleTranspiler(IEnumerable instructions) - { - return reassemblePatch(instructions); - } - - [HarmonyPatch(typeof(ResearchCoreBuilder), nameof(ResearchCoreBuilder.Reassemble))] - [HarmonyTranspiler] - public static IEnumerable ResearchCoreReassembleTranspiler(IEnumerable instructions) - { - return reassemblePatch(instructions); - } - - [HarmonyPatch(typeof(StairsBuilder), nameof(StairsBuilder.Reassemble))] - [HarmonyTranspiler] - public static IEnumerable StairsBuilderTranspiler(IEnumerable instructions) - { - return reassemblePatch(instructions); - } - - private static IEnumerable reassemblePatch(IEnumerable instructions) - { - var patchCnt = 0; - foreach (var instruction in instructions) - { - if (instruction.Calls(getTransformMethod)) - { - yield return new CodeInstruction(OpCodes.Pop); - yield return new CodeInstruction(OpCodes.Call, getTransformPatchMethod); - patchCnt++; - } - else if (instruction.Calls(getForwardMethod)) - { - yield return new CodeInstruction(OpCodes.Pop); - yield return new CodeInstruction(OpCodes.Call, getForwardPatchMethod); - patchCnt++; - } - else - { - yield return instruction; - } - } - - if (patchCnt != EXPECTED_PATCH_COUNT) - { - Plugin.Logger.LogError($"ReassembleTranspiler: Patch count mismatch: {patchCnt} != {EXPECTED_PATCH_COUNT}"); - } - } - - public static Transform patchedGetTransform() - { - return SteamVRInputMapper.rightHandObject.transform; - } - - public static Vector3 PatchedGetForward() - { - return -SteamVRInputMapper.rightHandObject.transform.up; - } -} diff --git a/plugin/src/util/patch/Transpile.cs b/plugin/src/util/patch/Transpile.cs new file mode 100644 index 0000000..226837d --- /dev/null +++ b/plugin/src/util/patch/Transpile.cs @@ -0,0 +1,56 @@ +using System.Collections.Generic; +using System.Reflection; +using System.Reflection.Emit; +using HarmonyLib; +using Plugin.Input; +using UnityEngine; + +namespace TechtonicaVR.Util.Patch; + +public class Transpile +{ + public static MethodInfo getTransformMethod = typeof(Component).GetMethod("get_transform"); + public static MethodInfo getForwardMethod = typeof(Transform).GetMethod("get_forward"); + + public static MethodInfo getTransformPatchMethod = typeof(Transpile).GetMethod(nameof(patchedGetTransform)); + public static MethodInfo getForwardPatchMethod = typeof(Transpile).GetMethod(nameof(PatchedGetForward)); + + public static IEnumerable reassemblePatch(IEnumerable instructions, int expectedPatchCount = 12) + { + var patchCnt = 0; + foreach (var instruction in instructions) + { + if (instruction.Calls(getTransformMethod)) + { + yield return new CodeInstruction(OpCodes.Pop); + yield return new CodeInstruction(OpCodes.Call, getTransformPatchMethod); + patchCnt++; + } + else if (instruction.Calls(getForwardMethod)) + { + yield return new CodeInstruction(OpCodes.Pop); + yield return new CodeInstruction(OpCodes.Call, getForwardPatchMethod); + patchCnt++; + } + else + { + yield return instruction; + } + } + + if (patchCnt != expectedPatchCount) + { + Plugin.Logger.LogError($"[{typeof(T)}]: Patch count mismatch: {patchCnt} != {expectedPatchCount}"); + } + } + + public static Transform patchedGetTransform() + { + return SteamVRInputMapper.rightHandObject.transform; + } + + public static Vector3 PatchedGetForward() + { + return -SteamVRInputMapper.rightHandObject.transform.up; + } +}