diff --git a/GameMod/GameMod.csproj b/GameMod/GameMod.csproj index 9ef14085..5e5a6a32 100644 --- a/GameMod/GameMod.csproj +++ b/GameMod/GameMod.csproj @@ -127,6 +127,7 @@ + diff --git a/GameMod/MPNoDupes.cs b/GameMod/MPNoDupes.cs new file mode 100644 index 00000000..c2d4d71c --- /dev/null +++ b/GameMod/MPNoDupes.cs @@ -0,0 +1,43 @@ +using System.Collections.Generic; +using System.Reflection; +using System.Reflection.Emit; +using Harmony; +using Overload; + +namespace GameMod { + /// + /// Prevents stock items from being picked up by multiple pilots in the same frame. + /// + [HarmonyPatch(typeof(Item), "OnTriggerEnter")] + class MPNoDupes_OnTriggerEnter { + public static bool Prefix(Item __instance) { + return __instance.m_type != ItemType.NONE; + } + + public static void SetItemTypeToNoneAndDestroy(Item item) { + item.m_type = ItemType.NONE; + UnityEngine.Object.Destroy(item.c_go); + } + + public static IEnumerable Transpiler(IEnumerable codes) { + var ldfldCount = 0; + + foreach (var code in codes) { + if (code.opcode == OpCodes.Ldfld) { + ldfldCount++; + + if (ldfldCount == 39) { + continue; + } + } + + if (code.opcode == OpCodes.Call && ((MethodInfo)code.operand).Name == "Destroy") { + yield return new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(MPNoDupes_OnTriggerEnter), "SetItemTypeToNoneAndDestroy")); + continue; + } + + yield return code; + } + } + } +}