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.API/Features/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ public ScpSpawnPreferences.SpawnPreferences ScpPreferences
public bool HasFlashlightModuleEnabled => CurrentItem is Firearm firearm && firearm.FlashlightEnabled;

/// <summary>
/// Gets a value indicating whether the player is jumping.
/// Gets or sets a value indicating whether the player is jumping.
/// </summary>
public bool IsJumping
{
Expand Down
48 changes: 11 additions & 37 deletions EXILED/Exiled.Events/Patches/Events/Scp3114/Dancing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace Exiled.Events.Patches.Events.Scp3114
{
#pragma warning disable SA1402 // File may only contain a single type

using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
Expand All @@ -35,7 +33,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

int index = newInstructions.FindIndex(x => x.opcode == OpCodes.Brfalse_S);
int index = newInstructions.FindIndex(x => x.opcode == OpCodes.Brfalse);

Label continueLabel = generator.DefineLabel();

Expand All @@ -58,64 +56,40 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(DancingEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev.LocalIndex),
new(OpCodes.Stloc, ev),

// Handlers.Scp3114.OnDancing(ev);
new(OpCodes.Call, Method(typeof(Handlers.Scp3114), nameof(Handlers.Scp3114.OnDancing))),

// if (ev.IsAllowed)
// goto continueLabel;
new(OpCodes.Callvirt, PropertyGetter(typeof(DancingEventArgs), nameof(DancingEventArgs.IsAllowed))),
new(OpCodes.Brtrue_S, continueLabel),
new(OpCodes.Brtrue, continueLabel),

// return
new(OpCodes.Ret),

// ev.NewState
new CodeInstruction(OpCodes.Ldloc_S, ev.LocalIndex).WithLabels(continueLabel),
new CodeInstruction(OpCodes.Ldloc, ev).WithLabels(continueLabel),
new(OpCodes.Callvirt, PropertyGetter(typeof(DancingEventArgs), nameof(DancingEventArgs.IsDancing))),
});

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];
index = newInstructions.FindIndex(x => x.opcode == OpCodes.Ldc_I4_0);

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
}
newInstructions.RemoveRange(index, 3);

/// <summary>
/// Patches <see cref="Scp3114Dance.ServerProcessCmd"/>
/// to add <see cref="Handlers.Scp3114.Dancing"/> event.
/// </summary>
[HarmonyPatch(typeof(Scp3114Dance), nameof(Scp3114Dance.ServerWriteRpc))]
internal class ChooseDanceType
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

int offset = -4;
int index = newInstructions.FindIndex(x => x.operand == (object)Method(typeof(NetworkWriter), nameof(NetworkWriter.WriteByte))) + offset;

newInstructions.RemoveRange(index, 4);

// replace "writer.WriteByte((byte)UnityEngine.Random.Range(0, 255))"
// with "writer.WriteByte(ChooseDanceType.DanceType)"
newInstructions.InsertRange(index, new CodeInstruction[]
newInstructions.InsertRange(
index,
new[]
{
// Handle(Player.Get(this.Owner));
new(OpCodes.Ldarg_0),

new(OpCodes.Call, Method(typeof(ChooseDanceType), nameof(Handle))),
new CodeInstruction(OpCodes.Ldloc, ev),
new(OpCodes.Call, PropertyGetter(typeof(DancingEventArgs), nameof(DancingEventArgs.DanceType))),
});

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

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

private static byte Handle(Scp3114Dance scp3114Dance)
=> (byte)Player.Get(scp3114Dance.Owner).Role.As<API.Features.Roles.Scp3114Role>().DanceType;
}
}
Loading