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
20 changes: 18 additions & 2 deletions EXILED/Exiled.Events/Handlers/Internal/ClientStarted.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,30 @@ public static void OnClientStarted()

foreach (KeyValuePair<uint, GameObject> prefab in NetworkClient.prefabs)
{
if(!prefabs.ContainsKey(prefab.Key) && prefab.Value.TryGetComponent(out Component component))
if (!prefabs.ContainsKey(prefab.Key))
{
if (!prefab.Value.TryGetComponent(out NetworkBehaviour component))
{
Log.Error($"Failed to get component for prefab: {prefab.Value.name} ({prefab.Key})");
continue;
}

prefabs.Add(prefab.Key, (prefab.Value, component));
}
}

foreach (NetworkIdentity ragdollPrefab in RagdollManager.AllRagdollPrefabs)
{
if(!prefabs.ContainsKey(ragdollPrefab.assetId) && ragdollPrefab.gameObject.TryGetComponent(out Component component))
if (!prefabs.ContainsKey(ragdollPrefab.assetId))
{
if (!ragdollPrefab.TryGetComponent(out BasicRagdoll component))
{
Log.Error($"Failed to get component for ragdoll prefab: {ragdollPrefab.name}");
continue;
}

prefabs.Add(ragdollPrefab.assetId, (ragdollPrefab.gameObject, component));
}
}

for (int i = 0; i < EnumUtils<PrefabType>.Values.Length; i++)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
IL_0040: ldc.i4.1
IL_0041: call void InventorySystem.Items.Firearms.Attachments.AttachmentsUtils::ApplyAttachmentsCode(class InventorySystem.Items.Firearms.Firearm, unsigned int32, bool)
*/
int index = newInstructions.FindIndex(i => i.IsLdarg(1)) - 1;
int index = newInstructions.FindIndex(i => i.opcode == OpCodes.Stloc_2) - 2;
List<Label> jumpLabels = newInstructions[index].ExtractLabels();

newInstructions.InsertRange(
Expand All @@ -69,7 +69,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
// if (!ev.IsAllowed) return;
new(OpCodes.Ldloc, ev),
new(OpCodes.Callvirt, PropertyGetter(typeof(ChangingAttachmentsEventArgs), nameof(ChangingAttachmentsEventArgs.IsAllowed))),
new(OpCodes.Brfalse_S, returnLabel),
new(OpCodes.Brfalse, returnLabel),

// msg.AttachmentsCode = ev.NewCode;
new(OpCodes.Ldarga, 1),
Expand Down
18 changes: 9 additions & 9 deletions EXILED/Exiled.Events/Patches/Events/Player/EscapingAndEscaped.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),

// roleTypeId
new(OpCodes.Ldloc_1),
new(OpCodes.Ldloc_2),

// escapeScenario
new(OpCodes.Ldloc_2),
new(OpCodes.Ldloc_3),

// EscapingEventArgs ev = new(Player, RoleTypeId, EscapeScenario)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(EscapingEventArgs))[0]),
Expand All @@ -73,12 +73,12 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
// roleTypeId = ev.NewRole
new(OpCodes.Ldloc, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(EscapingEventArgs), nameof(EscapingEventArgs.NewRole))),
new(OpCodes.Stloc_1),
new(OpCodes.Stloc_2),

// escapeScenario = ev.EscapeScenario
new(OpCodes.Ldloc, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(EscapingEventArgs), nameof(EscapingEventArgs.EscapeScenario))),
new(OpCodes.Stloc_2),
new(OpCodes.Stloc_3),
});

offset = 4;
Expand All @@ -87,23 +87,23 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
newInstructions.InsertRange(index, new CodeInstruction[]
{
// role = ev.Player.Role
new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Ldloc, ev),
new(OpCodes.Callvirt, PropertyGetter(typeof(EscapingEventArgs), nameof(EscapingEventArgs.Player))),
new(OpCodes.Callvirt, PropertyGetter(typeof(Player), nameof(Player.Role))),
new(OpCodes.Stloc_S, role.LocalIndex),
new(OpCodes.Stloc, role),
});

newInstructions.InsertRange(newInstructions.Count - 1, new CodeInstruction[]
{
// ev.Player
new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Ldloc, ev),
new(OpCodes.Callvirt, PropertyGetter(typeof(EscapingEventArgs), nameof(EscapingEventArgs.Player))),

// escapeScenario
new(OpCodes.Ldloc_2),
new(OpCodes.Ldloc_3),

// role
new(OpCodes.Ldloc_S, role.LocalIndex),
new(OpCodes.Ldloc, role),

// EscapedEventArgs ev2 = new(ev.Player, ev.EscapeScenario, role);
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(EscapedEventArgs))[0]),
Expand Down
4 changes: 0 additions & 4 deletions EXILED/Exiled.Events/Patches/Events/Player/Shooting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Callvirt, PropertyGetter(typeof(ShootingEventArgs), nameof(ShootingEventArgs.IsAllowed))),
new(OpCodes.Brtrue_S, continueLabel2),

// Dispose target FpcBacktracker
new(OpCodes.Ldloc_S, 5),
new(OpCodes.Callvirt, Method(typeof(IDisposable), nameof(IDisposable.Dispose))),

new(OpCodes.Leave_S, returnLabel),
new CodeInstruction(OpCodes.Nop).WithLabels(continueLabel2),
};
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