Skip to content

Commit 0ec1919

Browse files
committed
2 parents 753d706 + 24702aa commit 0ec1919

1 file changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using FrooxEngine;
2+
using HarmonyLib;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using System.Runtime.CompilerServices;
7+
using System.Reflection.Emit;
8+
using Elements.Core;
9+
using MonkeyLoader.Logging;
10+
using System.Linq;
11+
using System.Reflection;
12+
using System.Xml.Linq;
13+
14+
namespace CommunityBugFixCollection
15+
{
16+
[HarmonyPatchCategory(nameof(FixGrabberYoinkingUsers))]
17+
[HarmonyPatch(typeof(Grabber))]
18+
19+
internal sealed class FixGrabberYoinkingUsers : ResoniteBugFixMonkey<FixGrabberYoinkingUsers>
20+
{
21+
public override IEnumerable<string> Authors => Contributors.Onan;
22+
23+
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> Instructions)
24+
{
25+
Logger.Debug(() => $"Patching IL instructions!");
26+
Logger.Debug(() => $"Patching IL instructions!");
27+
Logger.Debug(() => $"Patching IL instructions!");
28+
Logger.Debug(() => $"Patching IL instructions!");
29+
Logger.Debug(() => $"Patching IL instructions!");
30+
Logger.Debug(() => $"Patching IL instructions!");
31+
Logger.Debug(() => $"Patching IL instructions!");
32+
Logger.Debug(() => $"Patching IL instructions!");
33+
List<CodeInstruction> Instructions2 = new(Instructions);
34+
35+
36+
object loadfield = Instructions2.Find((o) =>
37+
{
38+
return o.opcode == OpCodes.Ldfld && o.operand.ToString().Contains("_grabbedObjects");
39+
}).operand;
40+
41+
int injectLocation = Instructions2.FindIndex((o) =>
42+
{
43+
return o.opcode == OpCodes.Call && o.operand.ToString().Contains("EndUndoBatch");
44+
});
45+
46+
Instructions2.InsertRange(injectLocation + 1, new CodeInstruction[]{
47+
new CodeInstruction(OpCodes.Ldarg_0),
48+
new CodeInstruction(OpCodes.Ldarg_0),
49+
new CodeInstruction(OpCodes.Ldfld, loadfield),
50+
new CodeInstruction(OpCodes.Callvirt, AccessTools.DeclaredMethod(typeof(FixGrabberYoinkingUsers), nameof(CorrectGrabberPositionLaser)))}
51+
);
52+
53+
54+
return Instructions2;
55+
}
56+
57+
private static IEnumerable<MethodBase> TargetMethods()
58+
{
59+
Logger.Debug(() => $"Finding valid methods!");
60+
Logger.Debug(() => $"Finding valid methods!");
61+
Logger.Debug(() => $"Finding valid methods!");
62+
Logger.Debug(() => $"Finding valid methods!");
63+
Logger.Debug(() => $"Finding valid methods!");
64+
Logger.Debug(() => $"Finding valid methods!");
65+
Logger.Debug(() => $"Finding valid methods!");
66+
Logger.Debug(() => $"Finding valid methods!");
67+
Logger.Debug(() => $"Finding valid methods!");
68+
69+
var methodNames = new[]
70+
{
71+
AccessTools.DeclaredMethod(typeof(Grabber), nameof(Grabber.Grab), new Type[]{ typeof(List<IGrabbable>), typeof(bool)})
72+
};
73+
74+
return methodNames;
75+
}
76+
77+
public static void CorrectGrabberPositionLaser(Grabber __instance, List<IGrabbable> _grabbedObjects)
78+
{
79+
if (!Enabled)
80+
return;
81+
82+
float3 position = float3.Zero;
83+
84+
float3[] positions_original = new float3[_grabbedObjects.Count];
85+
86+
for (int i = 0; i < positions_original.Length; i++)
87+
{
88+
position += _grabbedObjects[i].Slot.GlobalPosition;
89+
positions_original[i] = _grabbedObjects[i].Slot.GlobalPosition;
90+
}
91+
92+
93+
position /= _grabbedObjects.Count;
94+
95+
96+
97+
__instance.HolderSlot.GlobalPosition = position;
98+
99+
for (int i = 0; i < positions_original.Length; i++)
100+
{
101+
_grabbedObjects[i].Slot.GlobalPosition = positions_original[i];
102+
}
103+
104+
105+
Logger.Debug(() => $"Correcting grab position!");
106+
}
107+
}
108+
109+
110+
}

0 commit comments

Comments
 (0)