Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion EXILED/Exiled.Events/EventArgs/Player/JumpingEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public float Speed
}

/// <summary>
/// Gets or sets a value indicating whether the client data can be synchronized with the server.
/// Gets or sets a value indicating whether or not the player can jump.
/// </summary>
public bool IsAllowed { get; set; }
}
Expand Down
28 changes: 9 additions & 19 deletions EXILED/Exiled.Events/Patches/Events/Item/ReceivingPreference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

int offset = 1;
int index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Ret) + offset;
int index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Stloc_2) + offset;

LocalBuilder ev = generator.DeclareLocal(typeof(ReceivingPreferenceEventArgs));
LocalBuilder curCode = generator.DeclareLocal(typeof(uint));

Label cdc = generator.DefineLabel();
Label ret = generator.DefineLabel();
Expand All @@ -54,14 +53,6 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
index,
new CodeInstruction[]
{
// dictionary::TryGetValue(AttachmentsSetupPreference::Weapon, *mem_0x02)
new(OpCodes.Ldloc_1),
new(OpCodes.Ldarg_1),
new(OpCodes.Ldfld, Field(typeof(AttachmentsSetupPreference), nameof(AttachmentsSetupPreference.Weapon))),
new(OpCodes.Ldloca_S, curCode.LocalIndex),
new(OpCodes.Callvirt, Method(typeof(Dictionary<ItemType, uint>), nameof(Dictionary<ItemType, uint>.TryGetValue))),
new(OpCodes.Brfalse_S, cdc),

// API::Features::Player::Get(referenceHub)
new(OpCodes.Ldloc_0),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),
Expand All @@ -70,12 +61,11 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Ldarg_1),
new(OpCodes.Ldfld, Field(typeof(AttachmentsSetupPreference), nameof(AttachmentsSetupPreference.Weapon))),

// currentCode
new(OpCodes.Ldloc_S, curCode.LocalIndex),
// playerPreference (old attachments)
new(OpCodes.Ldloc_1),

// AttachmentsSetupPreference::AttachmentsCode
new(OpCodes.Ldarg_1),
new(OpCodes.Ldfld, Field(typeof(AttachmentsSetupPreference), nameof(AttachmentsSetupPreference.AttachmentsCode))),
// attachmentsCode (new attachments)
new(OpCodes.Ldloc_2),

// true
new(OpCodes.Ldc_I4_1),
Expand All @@ -84,18 +74,18 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ReceivingPreferenceEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev.LocalIndex),
new(OpCodes.Stloc, ev),

// Handlers::Item::OnReceivingPreference(ev)
new(OpCodes.Call, Method(typeof(Item), nameof(Item.OnReceivingPreference))),

// ev.IsAllowed
new(OpCodes.Callvirt, PropertyGetter(typeof(ReceivingPreferenceEventArgs), nameof(ReceivingPreferenceEventArgs.IsAllowed))),
new(OpCodes.Brfalse_S, ret),
new(OpCodes.Brfalse, ret),

// **AttachmentSetupPreference::AttachmentsCode = ev::NewCode
new(OpCodes.Ldarga_S, 1),
new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Ldarga, 1),
new(OpCodes.Ldloc, ev),
new(OpCodes.Callvirt, PropertyGetter(typeof(ReceivingPreferenceEventArgs), nameof(ReceivingPreferenceEventArgs.NewCode))),
new(OpCodes.Stfld, Field(typeof(AttachmentsSetupPreference), nameof(AttachmentsSetupPreference.AttachmentsCode))),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

Label ret = generator.DefineLabel();

int offset = -3;
int offset = -4;
int index = newInstructions.FindIndex(i => i.opcode == OpCodes.Newobj && (ConstructorInfo)i.operand == GetDeclaredConstructors(typeof(LabApi.Events.Arguments.ServerEvents.CassieQueuingScpTerminationEventArgs))[0]) + offset;

newInstructions.InsertRange(
Expand Down
9 changes: 7 additions & 2 deletions EXILED/Exiled.Events/Patches/Events/Player/ChangedRoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ namespace Exiled.Events.Patches.Events.Player
using Exiled.Events.EventArgs.Player;
using Exiled.Events.Handlers;
using HarmonyLib;
using LabApi.Events.Handlers;
using MapGeneration;
using PlayerRoles;
using UnityEngine;

using static HarmonyLib.AccessTools;
Expand All @@ -34,12 +34,17 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

Label jump = generator.DefineLabel();
int offset = 2;
int index = newInstructions.FindIndex(x => x == (object)Method(typeof(Object), "op_Inequality", new[] { typeof(Object), typeof(Object) })) + offset;
int index = newInstructions.FindIndex(x => x.Calls(Method(typeof(Object), "op_Inequality", new[] { typeof(Object), typeof(Object) }))) + offset;

newInstructions[index].labels.Add(jump);

newInstructions.InsertRange(index, new CodeInstruction[]
{
// referenceHub
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(typeof(CurrentRoomPlayerCache), nameof(CurrentRoomPlayerCache._roleManager))),
new(OpCodes.Callvirt, PropertyGetter(typeof(PlayerRoleManager), nameof(PlayerRoleManager.Hub))),

// oldRoom
new(OpCodes.Ldloc_2),

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace Exiled.Events.Patches.Events.Player
using InventorySystem.Items;
using InventorySystem.Items.Radio;

using LabApi.Events.Arguments.PlayerEvents;

using static HarmonyLib.AccessTools;

/// <summary>
Expand All @@ -40,7 +42,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
LocalBuilder ev = generator.DeclareLocal(typeof(ChangingRadioPresetEventArgs));

const int offset = -5;
int index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Newobj) + offset;
int index = newInstructions.FindIndex(instruction => instruction.operand == (object)Constructor(typeof(PlayerChangingRadioRangeEventArgs), new[] { typeof(ReferenceHub), typeof(RadioItem), typeof(RadioMessages.RadioRangeLevel) })) + offset;

newInstructions.InsertRange(
index,
Expand Down
43 changes: 34 additions & 9 deletions EXILED/Exiled.Events/Patches/Events/Player/Jumping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,41 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

LocalBuilder ev = generator.DeclareLocal(typeof(JumpingEventArgs));
LocalBuilder jumping = generator.DeclareLocal(typeof(bool));

Label ret = generator.DefineLabel();
Label cont = generator.DefineLabel();
Label cancel = generator.DefineLabel();

const int offset = 1;
int index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Stfld) + offset;
// fun fact, the "int num = ProcessJump() ? 1 : 0;" in target method doesn't actually store anything, the bool result from ProcessJump is simply kept on stack.
// our patch needs to know when player is jumping so we modify method to store that.
int offset = 1;
int index = newInstructions.FindIndex(instruction => instruction.Calls(Method(typeof(FpcJumpController), nameof(FpcJumpController.ProcessJump)))) + offset;

// after ProcessJump, store its result
newInstructions.Insert(index, new CodeInstruction(OpCodes.Stloc, jumping));

// offset here is 0
index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Brfalse_S);

// make br_false use stored value
newInstructions.Insert(index, new CodeInstruction(OpCodes.Ldloc, jumping));

// The FindIndex finds when storing the field inside the moveDirection vector, so our patch occurs right before "this.MoveDirection = moveDirection;"
offset = 1;
index = newInstructions.FindIndex(instruction => instruction.opcode == OpCodes.Stfld) + offset;

newInstructions[index].WithLabels(cont);

newInstructions.InsertRange(
index,
new[]
{
// if not jumping, skip Jumping event
new(OpCodes.Ldloc, jumping),
new(OpCodes.Brfalse, cont),

// Player.Get(this.Hub)
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(typeof(FpcMotor), nameof(FpcMotor.Hub))),
new(OpCodes.Call, Method(typeof(API.Features.Player), nameof(API.Features.Player.Get), new[] { typeof(ReferenceHub) })),

Expand All @@ -59,23 +82,25 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(JumpingEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev.LocalIndex),
new(OpCodes.Stloc, ev),

// Player.OnJumping(ev)
new(OpCodes.Call, Method(typeof(Player), nameof(Player.OnJumping))),

// if (!ev.IsAllowed)
// return;
new(OpCodes.Callvirt, PropertyGetter(typeof(JumpingEventArgs), nameof(JumpingEventArgs.IsAllowed))),
new(OpCodes.Brfalse_S, ret),
new(OpCodes.Brfalse, cancel),

// moveDir = ev.Direction
new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Ldloc, ev),
new(OpCodes.Callvirt, PropertyGetter(typeof(JumpingEventArgs), nameof(JumpingEventArgs.Direction))),
new(OpCodes.Stloc_0),
});
new(OpCodes.Br, cont),

newInstructions[newInstructions.Count - 1].WithLabels(ret);
new CodeInstruction(OpCodes.Ldc_I4_0).WithLabels(cancel),
new(OpCodes.Stloc, jumping),
});

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];
Expand Down
Loading