Skip to content

Commit

Permalink
fix(game)!: ⬆️ version 0.2.0f compat (#7)
Browse files Browse the repository at this point in the history
* fix(game): ⬆️ version 0.2.0f compat

No longer works with 0.1.2

* fix(input): multi place builder patches
  • Loading branch information
Xenira committed Dec 15, 2023
1 parent e233308 commit 094c289
Show file tree
Hide file tree
Showing 14 changed files with 160 additions and 116 deletions.
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"input",
"render",
"ui",
"player"
"player",
"game"
]
}
}
5 changes: 3 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ v.0.1.0
:toc: left
:icons: font
:source-highlighter: highlightjs
:game-version: 0.1.2a
:game-version: 0.2.0f

This is a mod for the game https://store.steampowered.com/app/1457320/Techtonica/[Techtonica] that adds VR support.

Expand Down Expand Up @@ -69,12 +69,13 @@ 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
- Enable ik (The game ships with `FinalIK` so it should be possible. Probably just not networked yet.)

== Troubleshooting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@
using Plugin.Input;
using UnityEngine;

namespace TechtonicaVR.VRCamera.Patch;
namespace TechtonicaVR.VRCamera.Patch.Builder;

[HarmonyPatch]
public class BuilderRaycastPatch
{
private const int EXPECTED_DECONSTRUCTION_UPDATE_PATCH_COUNT = 3;
private const int EXPECTED_FREEFORM_UPDATE_PLACEMENT_PATCH_COUNT = 2;
private const int EXPECTED_TARGET_DECONSTRUCTABLE_UPDATE_PATCH_COUNT = 1;
private const int EXPECTED_TARGET_LOGISTICS_OBJECT_PATCH_COUNT = 2;

static MethodInfo raycastMethod = typeof(Physics).GetMethod(nameof(Physics.Raycast), [typeof(Vector3), typeof(Vector3), typeof(RaycastHit).MakeByRefType(), typeof(float), typeof(int), typeof(QueryTriggerInteraction)]);
static MethodInfo vectorMultiplyMethod = typeof(Vector3).GetMethod("op_Multiply", [typeof(Vector3), typeof(float)]);
static MethodInfo vectorAddMethod = typeof(Vector3).GetMethod("op_Addition", [typeof(Vector3), typeof(Vector3)]);
Expand All @@ -18,7 +23,7 @@ public class BuilderRaycastPatch
static MethodInfo vectorMultiplyPatchMethod = typeof(BuilderRaycastPatch).GetMethod(nameof(PatchedVectorMultiply));
static MethodInfo vectorAddPatchMethod = typeof(BuilderRaycastPatch).GetMethod(nameof(PatchedVectorAdd));

[HarmonyPatch(typeof(PlayerBuilder), nameof(PlayerBuilder.cameraOrigin), MethodType.Getter)]
[HarmonyPatch(typeof(PlayerBuilder), nameof(PlayerBuilder.CameraOrigin), MethodType.Getter)]
[HarmonyPrefix]
public static bool cameraOriginPrefix(PlayerBuilder __instance, ref Vector3 __result)
{
Expand All @@ -31,7 +36,7 @@ public static bool cameraOriginPrefix(PlayerBuilder __instance, ref Vector3 __re
return false;
}

[HarmonyPatch(typeof(PlayerBuilder), nameof(PlayerBuilder.cameraDirection), MethodType.Getter)]
[HarmonyPatch(typeof(PlayerBuilder), nameof(PlayerBuilder.CameraDirection), MethodType.Getter)]
[HarmonyPrefix]
public static bool cameraDirectionPrefix(PlayerBuilder __instance, ref Vector3 __result)
{
Expand All @@ -44,30 +49,6 @@ public static bool cameraDirectionPrefix(PlayerBuilder __instance, ref Vector3 _
return false;
}

[HarmonyPatch(typeof(PlayerBuilder), nameof(PlayerBuilder.DeconstructionUpdate))]
[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> DeconstructionUpdateTranspiler(IEnumerable<CodeInstruction> instructions)
{
var patchCnt = 0;
foreach (var instruction in instructions)
{
if (instruction.Calls(raycastMethod))
{
yield return new CodeInstruction(OpCodes.Call, raycastPatchMethod);
patchCnt++;
}
else
{
yield return instruction;
}
}

if (patchCnt != 3)
{
Plugin.Logger.LogError("Failed to patch PlayerBuilder.DeconstructionUpdate");
}
}

[HarmonyPatch(typeof(PlayerBuilder), nameof(PlayerBuilder.FreeformUpdatePlacement))]
[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> FreeformUpdatePlacementTranspiler(IEnumerable<CodeInstruction> instructions)
Expand All @@ -91,9 +72,9 @@ public static IEnumerable<CodeInstruction> FreeformUpdatePlacementTranspiler(IEn
}
}

if (patchCnt != 2)
if (patchCnt != EXPECTED_FREEFORM_UPDATE_PLACEMENT_PATCH_COUNT)
{
Plugin.Logger.LogError("Failed to patch PlayerBuilder.FreeformUpdatePlacement");
Plugin.Logger.LogError($"[PlayerBuilder.FreeformUpdatePlacement] Patch count mismatch: {patchCnt} != {EXPECTED_FREEFORM_UPDATE_PLACEMENT_PATCH_COUNT}");
}
}

Expand All @@ -115,9 +96,9 @@ public static IEnumerable<CodeInstruction> HasTargetDeconstructableUpdateTranspi
}
}

if (patchCnt != 1)
if (patchCnt != EXPECTED_TARGET_DECONSTRUCTABLE_UPDATE_PATCH_COUNT)
{
Plugin.Logger.LogError("Failed to patch PlayerBuilder.HasTargetDeconstructable");
Plugin.Logger.LogError($"[PlayerBuilder.HasTargetDeconstructable] Patch count mismatch: {patchCnt} != {EXPECTED_TARGET_DECONSTRUCTABLE_UPDATE_PATCH_COUNT}");
}
}

Expand All @@ -139,9 +120,9 @@ public static IEnumerable<CodeInstruction> CheckTargetLogisticsObjectTranspiler(
}
}

if (patchCnt != 1)
if (patchCnt != EXPECTED_TARGET_LOGISTICS_OBJECT_PATCH_COUNT)
{
Plugin.Logger.LogError("Failed to patch PlayerBuilder.CheckTargetLogisticsObject");
Plugin.Logger.LogError($"[PlayerBuilder.CheckTargetLogisticsObject] Patch count mismatch: {patchCnt} != {EXPECTED_TARGET_LOGISTICS_OBJECT_PATCH_COUNT}");
}
}

Expand Down
34 changes: 34 additions & 0 deletions plugin/src/camera/patch/builder/builder_reassemble_patch.cs
Original file line number Diff line number Diff line change
@@ -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<CodeInstruction> ReassembleTranspiler(IEnumerable<CodeInstruction> instructions)
{
return Transpile.reassemblePatch<FloorBuilder>(instructions);
}

[HarmonyPatch(typeof(ResearchCoreBuilder), nameof(ResearchCoreBuilder.Reassemble))]
[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> ResearchCoreReassembleTranspiler(IEnumerable<CodeInstruction> instructions)
{
return Transpile.reassemblePatch<ResearchCoreBuilder>(instructions);
}

[HarmonyPatch(typeof(StairsBuilder), nameof(StairsBuilder.Reassemble))]
[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> StairsBuilderTranspiler(IEnumerable<CodeInstruction> instructions)
{
return Transpile.reassemblePatch<StairsBuilder>(instructions);
}
}
16 changes: 16 additions & 0 deletions plugin/src/camera/patch/builder/conveyor_builder_patch.cs
Original file line number Diff line number Diff line change
@@ -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<CodeInstruction> ReassembleTranspiler(IEnumerable<CodeInstruction> instructions)
{
return Transpile.reassemblePatch<ConveyorBuilder>(instructions, 5);
}
}
16 changes: 16 additions & 0 deletions plugin/src/camera/patch/builder/multi_place_builder_patch.cs
Original file line number Diff line number Diff line change
@@ -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<CodeInstruction> ReassembleTranspiler(IEnumerable<CodeInstruction> instructions)
{
return Transpile.reassemblePatch<MultiPlaceBuilder>(instructions, 25);
}
}
78 changes: 0 additions & 78 deletions plugin/src/camera/patch/builder_reassemble_patch.cs

This file was deleted.

5 changes: 5 additions & 0 deletions plugin/src/camera/patch/target_raycast_patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class TargetRaycastPatch
[HarmonyPatch(typeof(PlayerFirstPersonController), nameof(PlayerFirstPersonController.UpdateAimingRaycasts))]
public static bool UpdateAimingRaycastsPostfix(PlayerFirstPersonController __instance)
{
if (SteamVRInputMapper.rightHandObject == null)
{
return true;
}

var right_hand_transform = SteamVRInputMapper.rightHandObject.transform;
var forward = -right_hand_transform.up;

Expand Down
14 changes: 13 additions & 1 deletion plugin/src/input/input_patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ static bool SetVibration(float leftIntensity, float rightIntensity)
[HarmonyPrefix]
[HarmonyPatch(typeof(Rewired.Player), nameof(Rewired.Player.GetAxis2D), [typeof(int), typeof(int)])]
static bool GetAxis2D(ref Vector2 __result, int xAxisActionId, int yAxisActionId)
{
return handleAxisInput(ref __result, xAxisActionId, yAxisActionId);
}

[HarmonyPrefix]
[HarmonyPatch(typeof(Rewired.Player), nameof(Rewired.Player.GetAxis2DRaw), [typeof(int), typeof(int)])]
static bool GetAxis2DRaw(ref Vector2 __result, int xAxisActionId, int yAxisActionId)
{
return handleAxisInput(ref __result, xAxisActionId, yAxisActionId);
}

private static bool handleAxisInput(ref Vector2 __result, int xAxisActionId, int yAxisActionId)
{
if (xAxisActionId == RewiredConsts.Action.Move_Horizontal && yAxisActionId == RewiredConsts.Action.Move_Vertical)
{
Expand Down Expand Up @@ -58,7 +70,7 @@ static bool GetAxis2D(ref Vector2 __result, int xAxisActionId, int yAxisActionId
[HarmonyPatch(typeof(PlayerFirstPersonController), nameof(PlayerFirstPersonController.Move))]
static void Move(PlayerFirstPersonController __instance)
{
if (!__instance.m_IsGrounded || InputHandler.instance.playerInputBlocked || InputHandler.instance.playerInputBlockedOverride)
if (!__instance.m_IsGrounded || InputHandler.instance.playerInputBlocked || InputHandler.instance.playerInputBlockedOverride && VRCameraManager.mainCamera != null)
{
return;
}
Expand Down
56 changes: 56 additions & 0 deletions plugin/src/util/patch/Transpile.cs
Original file line number Diff line number Diff line change
@@ -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<CodeInstruction> reassemblePatch<T>(IEnumerable<CodeInstruction> 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;
}
}
Binary file modified strip-dll/Assembly-CSharp.dll
Binary file not shown.
Binary file modified strip-dll/Unity.XR.Management.dll
Binary file not shown.
Binary file modified strip-dll/UnityEngine.CoreModule.dll
Binary file not shown.
Binary file modified strip-dll/UnityEngine.dll
Binary file not shown.

0 comments on commit 094c289

Please sign in to comment.