-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPreciseRotation.cs
155 lines (142 loc) · 5.85 KB
/
PreciseRotation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using UnityEngine;
namespace PreciseRotation
{
[BepInPlugin("com.github.johndowson.PreciseRotation", "PreciseRotation", "2.0.0")]
public class PreciseRotation : BaseUnityPlugin
{
public static ConfigEntry<int> RotationSteps;
public static ConfigEntry<bool> OverrideRotation;
public static ConfigEntry<KeyCode> RotationModifier;
public static ConfigEntry<KeyCode> NextAxis;
public static bool Precision;
public static int RotationX, RotationY, RotationZ = 0;
public static Axis CurrentAxis = Axis.Y;
public enum Axis
{
X,
Y,
Z,
}
private static readonly Harmony harmony = new(typeof(PreciseRotation).GetCustomAttributes(typeof(BepInPlugin), false)
.Cast<BepInPlugin>()
.First()
.GUID);
#pragma warning disable IDE0051 // Remove unused private members
private void Awake()
{
RotationModifier = Config.Bind("General", "RotationModifier", KeyCode.LeftAlt, "Key to toggle precise rotation");
OverrideRotation = Config.Bind("General", "OverrideRotation", false, "Override rotation steps without the need to hold a button.");
RotationSteps = Config.Bind("General", "RotationSteps", 16, "Number of rotation steps per 180 degrees, Valheim's default is 8");
NextAxis = Config.Bind("General", "NextAxis", KeyCode.V, "Key to toggle rotation axis");
Precision = OverrideRotation.Value;
harmony.PatchAll();
}
private void OnDestroy()
{
harmony.UnpatchSelf();
}
[HarmonyPatch(typeof(Player))]
public static class PieceRotation_Patch
{
[HarmonyPatch("UpdatePlacementGhost")]
[HarmonyTranspiler]
static IEnumerable<CodeInstruction> UpdatePlacementGhostPatch(IEnumerable<CodeInstruction> instructions)
{
var codes = new List<CodeInstruction>(instructions);
for (var i = 0; i < codes.Count; i++)
{
if (codes[i].opcode == OpCodes.Callvirt &&
codes[i + 1].opcode == OpCodes.Ldc_R4 &&
codes[i + 2].opcode == OpCodes.Ldc_R4 &&
codes[i + 3].opcode == OpCodes.Ldarg_0 &&
codes[i + 4].opcode == OpCodes.Ldfld &&
codes[i + 5].opcode == OpCodes.Conv_R4 &&
codes[i + 6].opcode == OpCodes.Mul &&
codes[i + 7].opcode == OpCodes.Ldc_R4 &&
codes[i + 8].opcode == OpCodes.Call)
{
for (int y = 1; y < 8; y++)
codes[i + y].opcode = OpCodes.Nop;
codes[i + 8] = CodeInstruction.Call(typeof(PieceRotation_Patch), "Rotate");
}
}
return codes.AsEnumerable();
}
[HarmonyPatch("UpdatePlacement")]
[HarmonyPrefix]
static void RememberOldRotation(Player __instance, bool takeInput, float dt, ref int __state)
{
__state = __instance.m_placeRotation;
}
[HarmonyPatch("UpdatePlacement")]
[HarmonyPostfix]
static void StoreRotationDifference(Player __instance, bool takeInput, float dt, ref int __state)
{
int difference = -(__state - __instance.m_placeRotation);
switch (CurrentAxis)
{
case Axis.X:
RotationX += difference;
break;
case Axis.Y:
RotationY += difference;
break;
case Axis.Z:
RotationZ += difference;
break;
}
}
[HarmonyPatch("Update")]
[HarmonyPostfix]
static void UpdateRotationAxis(Player __instance)
{
if (Input.GetKeyDown(NextAxis.Value))
ToggleAxis();
}
[HarmonyPatch("Update")]
[HarmonyPostfix]
static void UpdatePrecision(Player __instance)
{
if (!OverrideRotation.Value && Input.GetKeyDown(RotationModifier.Value))
{
Precision = !Precision;
Player.m_localPlayer.Message(MessageHud.MessageType.TopLeft, "Pecision toggled: " + Precision, 0, null);
}
}
public static Quaternion Rotate()
{
var rotationSteps = GetRotationSteps();
return Quaternion.Euler(rotationSteps * (float)RotationX, rotationSteps * (float)RotationY, rotationSteps * (float)RotationZ);
}
public static float GetRotationSteps()
{
float rotationPerStep = 180.0f / 8;
if (Precision)
rotationPerStep = 180.0f / RotationSteps.Value;
return rotationPerStep;
}
public static void ToggleAxis()
{
switch (CurrentAxis)
{
case Axis.X:
CurrentAxis = Axis.Y;
break;
case Axis.Y:
CurrentAxis = Axis.Z;
break;
case Axis.Z:
CurrentAxis = Axis.X;
break;
}
Player.m_localPlayer.Message(MessageHud.MessageType.TopLeft, "Rotation toggled: " + CurrentAxis, 0, null);
}
}
}
}