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/Handlers/Internal/Round.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static void OnRestartingRound()
/// <inheritdoc cref="Handlers.Player.OnChangingRole(ChangingRoleEventArgs)" />
public static void OnChangingRole(ChangingRoleEventArgs ev)
{
if (!ev.Player.IsHost && ev.NewRole == RoleTypeId.Spectator && ev.Reason is not(SpawnReason.Destroyed or SpawnReason.Died) && Events.Instance.Config.ShouldDropInventory)
if (!ev.Player.IsHost && ev.NewRole == RoleTypeId.Spectator && ev.Reason is not SpawnReason.Destroyed && Events.Instance.Config.ShouldDropInventory)
ev.Player.Inventory.ServerDropEverything();
}

Expand Down
63 changes: 63 additions & 0 deletions EXILED/Exiled.Events/Patches/Fixes/Fix1344Dupe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// -----------------------------------------------------------------------
// <copyright file="Fix1344Dupe.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.Patches.Fixes
{
using System.Collections.Generic;
using System.Reflection.Emit;

using Exiled.API.Features.Pools;
using HarmonyLib;
using InventorySystem;
using InventorySystem.Items;
using InventorySystem.Items.Usables.Scp1344;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches the <see cref="Scp1344Item.OnPlayerInventoryDropped"/> method.
/// Fixes the dupe where 2 copies of SCP-1344 can be created when a player dies.
/// Bug not reported to NW yet (rare in vanilla servers).
/// </summary>
[HarmonyPatch(typeof(Scp1344Item), nameof(Scp1344Item.OnPlayerInventoryDropped))]
public class Fix1344Dupe
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label returnLabel = generator.DefineLabel();

newInstructions[newInstructions.Count - 1].labels.Add(returnLabel);

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

newInstructions.InsertRange(index, new[]
{
// this.OwnerInventory.UserInventory.Items
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp1344Item), nameof(Scp1344Item.OwnerInventory))),
new(OpCodes.Ldfld, Field(typeof(Inventory), nameof(Inventory.UserInventory))),
new(OpCodes.Ldfld, Field(typeof(InventoryInfo), nameof(InventoryInfo.Items))),

// this.ItemSerial
new(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp1344Item), nameof(Scp1344Item.ItemSerial))),

// if (!this.OwnerInventory.UserInventory.Items.ContainsKey(this.ItemSerial) return;
new(OpCodes.Callvirt, Method(typeof(Dictionary<ushort, ItemBase>), nameof(Dictionary<ushort, ItemBase>.ContainsKey))),
new(OpCodes.Brfalse_S, returnLabel),
});

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

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