Skip to content

Commit

Permalink
Should reduce bloat code that was required years ago.
Browse files Browse the repository at this point in the history
  • Loading branch information
Undid-Iridium committed Aug 3, 2024
1 parent 0b577df commit 4496fde
Showing 1 changed file with 6 additions and 83 deletions.
89 changes: 6 additions & 83 deletions EXILED/Exiled.Events/Patches/Events/Scp330/InteractingScp330.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@ namespace Exiled.Events.Patches.Events.Scp330
using System.Collections.Generic;
using System.Reflection.Emit;

using API.Features.Pools;

using CustomPlayerEffects;
using Exiled.API.Features.Pools;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Scp330;
using Exiled.Events.Handlers;

using HarmonyLib;

using Interactables.Interobjects;

using InventorySystem;
using InventorySystem.Items.Usables.Scp330;

using static HarmonyLib.AccessTools;
Expand All @@ -45,9 +39,6 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

LocalBuilder ev = generator.DeclareLocal(typeof(InteractingScp330EventArgs));

// Remove original "No scp can touch" logic.
newInstructions.RemoveRange(0, 3);

// Find ServerProcessPickup, insert before it.
int offset = -3;
int index = newInstructions.FindLastIndex(
Expand Down Expand Up @@ -79,69 +70,24 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Brfalse, returnLabel),
});

// Logic to find the only ServerProcessPickup and replace with our own.
int removeServerProcessOffset = -2;
int removeServerProcessIndex = newInstructions.FindLastIndex(
instruction => instruction.Calls(Method(typeof(Scp330Bag), nameof(Scp330Bag.ServerProcessPickup)))) + removeServerProcessOffset;

newInstructions.RemoveRange(removeServerProcessIndex, 3);

// Replace NW server process logic.
newInstructions.InsertRange(
removeServerProcessIndex,
new[]
{
// ldarg.1 is already in the stack

// ev.Candy
new CodeInstruction(OpCodes.Ldloc, ev),
new CodeInstruction(OpCodes.Callvirt, PropertyGetter(typeof(InteractingScp330EventArgs), nameof(InteractingScp330EventArgs.Candy))),

// bag
new CodeInstruction(OpCodes.Ldloca_S, 3),

// ServerProcessPickup(ReferenceHub, CandyKindID, Scp330Bag)
new CodeInstruction(OpCodes.Call, Method(typeof(InteractingScp330), nameof(ServerProcessPickup), new[] { typeof(ReferenceHub), typeof(CandyKindID), typeof(Scp330Bag).MakeByRefType() })),
});

// This is to find the location of RpcMakeSound to remove the original code and add a new sever logic structure (Start point)
int addShouldSeverOffset = 1;
int addShouldSeverIndex = newInstructions.FindLastIndex(
instruction => instruction.Calls(Method(typeof(Scp330Interobject), nameof(Scp330Interobject.RpcMakeSound)))) + addShouldSeverOffset;

// This is to find the location of the next return (End point)
int includeSameLine = 1;
int nextReturn = newInstructions.FindIndex(addShouldSeverIndex, instruction => instruction.opcode == OpCodes.Ret) + includeSameLine;
Label originalLabel = newInstructions[addShouldSeverIndex].ExtractLabels()[0];

// Remove original code from after RpcMakeSound to next return and then fully replace it.
newInstructions.RemoveRange(addShouldSeverIndex, nextReturn - addShouldSeverIndex);

addShouldSeverIndex = newInstructions.FindLastIndex(
instruction => instruction.Calls(Method(typeof(Scp330Interobject), nameof(Scp330Interobject.RpcMakeSound)))) + addShouldSeverOffset;
int serverEffectLocationStart = -1;
int enableEffect = newInstructions.FindLastIndex(
instruction => instruction.LoadsField(Field(typeof(PlayerEffectsController), nameof(ReferenceHub.playerEffectsController)))) + serverEffectLocationStart;

newInstructions.InsertRange(
addShouldSeverIndex,
new CodeInstruction[]
new[]
{
// if (!ev.ShouldSever)
// goto shouldNotSever;
new CodeInstruction(OpCodes.Ldloc, ev.LocalIndex).WithLabels(originalLabel),
new CodeInstruction(OpCodes.Ldloc, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(InteractingScp330EventArgs), nameof(InteractingScp330EventArgs.ShouldSever))),
new(OpCodes.Brfalse, shouldNotSever),

// ev.Player.EnableEffect("SevereHands", 1, 0f, false)
new(OpCodes.Ldloc, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(InteractingScp330EventArgs), nameof(InteractingScp330EventArgs.Player))),
new(OpCodes.Ldstr, nameof(SeveredHands)),
new(OpCodes.Ldc_I4_1),
new(OpCodes.Ldc_R4, 0f),
new(OpCodes.Ldc_I4_0),
new(OpCodes.Callvirt, Method(typeof(Player), nameof(Player.EnableEffect), new[] { typeof(string), typeof(byte), typeof(float), typeof(bool) })),
new(OpCodes.Pop),

// return;
new(OpCodes.Ret),
});

// This will let us jump to the taken candies code and lock until ldarg_0, meaning we allow base game logic handle candy adding.
Expand All @@ -157,28 +103,5 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}

private static bool ServerProcessPickup(ReferenceHub player, CandyKindID candy, out Scp330Bag bag)
{
if (!Scp330Bag.TryGetBag(player, out bag))
{
player.inventory.ServerAddItem(ItemType.SCP330);

if (!Scp330Bag.TryGetBag(player, out bag))
return false;

bag.Candies = new List<CandyKindID> { candy };
bag.ServerRefreshBag();

return true;
}

bool result = bag.TryAddSpecific(candy);

if (bag.AcquisitionAlreadyReceived)
bag.ServerRefreshBag();

return result;
}
}
}

0 comments on commit 4496fde

Please sign in to comment.