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
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// -----------------------------------------------------------------------
// <copyright file="UpgradedInventoryItemEventArgs.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.EventArgs.Scp914
{
using API.Features;
using API.Features.Items;
using global::Scp914;
using Interfaces;
using InventorySystem.Items;
using InventorySystem.Items.Pickups;

/// <summary>
/// Contains all information before SCP-914 upgrades an item.
/// </summary>
public class UpgradedInventoryItemEventArgs : IItemEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="UpgradedInventoryItemEventArgs" /> class.
/// </summary>
/// <param name="player">
/// <inheritdoc cref="Player" />
/// </param>
/// <param name="item">
/// <inheritdoc cref="Item" />
/// </param>
/// <param name="knobSetting">
/// <inheritdoc cref="KnobSetting" />
/// </param>
/// <param name="result">
/// <inheritdoc cref="Result" />
/// </param>
public UpgradedInventoryItemEventArgs(Player player, ItemBase item, Scp914KnobSetting knobSetting, ItemBase[] result)
{
Player = player;
Item = Item.Get(item);
KnobSetting = knobSetting;
Result = result;
}

/// <summary>
/// Gets SCP-914 working knob setting.
/// </summary>
public Scp914KnobSetting KnobSetting { get; }

/// <summary>
/// Gets a list of items to be upgraded inside SCP-914.
/// </summary>
public Item Item { get; }

/// <summary>
/// Gets the <see cref="Player" /> who owns the item to be upgraded.
/// </summary>
public Player Player { get; }

/// <summary>
/// Gets the array of items created as a result of SCP-914 upgraded.
/// </summary>
public ItemBase[] Result { get; }
}
}
64 changes: 64 additions & 0 deletions EXILED/Exiled.Events/EventArgs/Scp914/UpgradedPickupEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// -----------------------------------------------------------------------
// <copyright file="UpgradedPickupEventArgs.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.EventArgs.Scp914
{
using Exiled.API.Features.Pickups;
using Exiled.Events.EventArgs.Interfaces;
using global::Scp914;
using InventorySystem.Items.Pickups;
using UnityEngine;

/// <summary>
/// Contains all information before SCP-914 upgrades an item.
/// </summary>
public class UpgradedPickupEventArgs : IPickupEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="UpgradedPickupEventArgs" /> class.
/// </summary>
/// <param name="item">
/// <inheritdoc cref="Pickup" />
/// </param>
/// <param name="newPos">
/// <inheritdoc cref="OutputPosition" />
/// </param>
/// <param name="knobSetting">
/// <inheritdoc cref="KnobSetting" />
/// </param>
/// <param name="result">
/// <inheritdoc cref="Result" />
/// </param>
public UpgradedPickupEventArgs(ItemPickupBase item, Vector3 newPos, Scp914KnobSetting knobSetting, ItemPickupBase[] result)
{
Pickup = Pickup.Get(item);
OutputPosition = newPos;
KnobSetting = knobSetting;
Result = result;
}

/// <summary>
/// Gets a list of items to be upgraded inside SCP-914.
/// </summary>
public Pickup Pickup { get; }

/// <summary>
/// Gets the position the item will be output to.
/// </summary>
public Vector3 OutputPosition { get; }

/// <summary>
/// Gets SCP-914 working knob setting.
/// </summary>
public Scp914KnobSetting KnobSetting { get; }

/// <summary>
/// Gets the array of items created as a result of SCP-914 upgraded.
/// </summary>
public ItemPickupBase[] Result { get; }
}
}
22 changes: 22 additions & 0 deletions EXILED/Exiled.Events/Handlers/Scp914.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,21 @@ public static class Scp914
/// </summary>
public static Event<UpgradingPickupEventArgs> UpgradingPickup { get; set; } = new();

/// <summary>
/// Invoked after SCP-914 upgrades a Pickup.
/// </summary>
public static Event<UpgradedPickupEventArgs> UpgradedPickup { get; set; } = new();

/// <summary>
/// Invoked before SCP-914 upgrades an item in a player's inventory.
/// </summary>
public static Event<UpgradingInventoryItemEventArgs> UpgradingInventoryItem { get; set; } = new();

/// <summary>
/// Invoked after SCP-914 upgrades an item in a player's inventory.
/// </summary>
public static Event<UpgradedInventoryItemEventArgs> UpgradedInventoryItem { get; set; } = new();

/// <summary>
/// Invoked before SCP-914 upgrades a player.
/// </summary>
Expand All @@ -48,12 +58,24 @@ public static class Scp914
/// <param name="ev">The <see cref="UpgradingPickupEventArgs" /> instance.</param>
public static void OnUpgradingPickup(UpgradingPickupEventArgs ev) => UpgradingPickup.InvokeSafely(ev);

/// <summary>
/// Called after SCP-914 upgrades a item.
/// </summary>
/// <param name="ev">The <see cref="UpgradedPickupEventArgs" /> instance.</param>
public static void OnUpgradedPickup(UpgradedPickupEventArgs ev) => UpgradedPickup.InvokeSafely(ev);

/// <summary>
/// Called before SCP-914 upgrades an item in a player's inventory.
/// </summary>
/// <param name="ev">The <see cref="UpgradingInventoryItemEventArgs" /> instance.</param>
public static void OnUpgradingInventoryItem(UpgradingInventoryItemEventArgs ev) => UpgradingInventoryItem.InvokeSafely(ev);

/// <summary>
/// Called after SCP-914 upgrades an item in a player's inventory.
/// </summary>
/// <param name="ev">The <see cref="UpgradedInventoryItemEventArgs" /> instance.</param>
public static void OnUpgradedInventoryItem(UpgradedInventoryItemEventArgs ev) => UpgradedInventoryItem.InvokeSafely(ev);

/// <summary>
/// Called before SCP-914 upgrades a player.
/// </summary>
Expand Down
73 changes: 73 additions & 0 deletions EXILED/Exiled.Events/Patches/Events/Scp914/UpgradedPickup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// -----------------------------------------------------------------------
// <copyright file="UpgradedPickup.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.Events.Scp914
{
using System.Collections.Generic;
using System.Reflection.Emit;

using API.Features.Pools;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Scp914;

using global::Scp914;

using Handlers;

using HarmonyLib;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="Scp914Upgrader.ProcessPickup" />.
/// Adds the <see cref="Scp914.UpgradedPickup" /> event.
/// </summary>
[EventPatch(typeof(Scp914), nameof(Scp914.UpgradedPickup))]
[HarmonyPatch(typeof(Scp914Upgrader), nameof(Scp914Upgrader.ProcessPickup))]
internal static class UpgradedPickup
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

int index = newInstructions.FindLastIndex(instruction => instruction.opcode == OpCodes.Ldloc_2);

List<Label> label = newInstructions[index].ExtractLabels();

newInstructions.InsertRange(
index,
new CodeInstruction[]
{
// pickup
new(OpCodes.Ldarg_0),

// outputPos
new(OpCodes.Ldloc_1),

// knobSetting
new(OpCodes.Ldarg_2),

// resultingPickups
new(OpCodes.Ldloc_S, 4),
new(OpCodes.Ldfld, Field(typeof(Scp914Result), nameof(Scp914Result.ResultingPickups))),

// UpgradedPickupEventArgs ev = new(pickup, outputPos, knobSetting, resultingPickups)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(UpgradedPickupEventArgs))[0]),

// Handlers.Scp914.OnUpgradingPickup(ev);
new(OpCodes.Call, Method(typeof(Scp914), nameof(Scp914.OnUpgradedPickup))),
});

newInstructions[index].WithLabels(label);

foreach (CodeInstruction t in newInstructions)
yield return t;

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
}
}
84 changes: 84 additions & 0 deletions EXILED/Exiled.Events/Patches/Events/Scp914/UpgradedPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// -----------------------------------------------------------------------
// <copyright file="UpgradedPlayer.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.Events.Scp914
{
using System.CodeDom;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;

using API.Features;
using API.Features.Pools;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Scp914;
using global::Scp914;
using HarmonyLib;
using Mono.Cecil.Cil;
using PlayerRoles.FirstPersonControl;
using UnityEngine;

using static HarmonyLib.AccessTools;

using OpCode = System.Reflection.Emit.OpCode;
using Scp914 = Handlers.Scp914;

/// <summary>
/// Patches <see cref="Scp914Upgrader.ProcessPlayer" />
/// to add the <see cref="Scp914.UpgradedInventoryItem" /> event.
/// </summary>
[EventPatch(typeof(Scp914), nameof(Scp914.UpgradedInventoryItem))]
[HarmonyPatch(typeof(Scp914Upgrader), nameof(Scp914Upgrader.ProcessPlayer))]
internal static class UpgradedPlayer
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

int index = newInstructions.FindIndex(x => x.opcode == OpCodes.Stloc_S && x.operand is LocalBuilder lb && lb.LocalIndex == 11) + 1;

LocalBuilder curSetting = generator.DeclareLocal(typeof(Scp914KnobSetting));
List<Label> label = newInstructions[index].ExtractLabels();

newInstructions.InsertRange(
index,
new CodeInstruction[]
{
// setting = curSetting
new(OpCodes.Ldloc_S, curSetting.LocalIndex),
new(OpCodes.Starg_S, 3),

// Player.Get(ply)
new(OpCodes.Ldarg_0),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

// itemBase
new(OpCodes.Ldloc_S, 7),

// setting
new(OpCodes.Ldarg_S, 3),

// resultingItems
new(OpCodes.Ldloc_S, 11),
new(OpCodes.Ldfld, Field(typeof(Scp914Result), nameof(Scp914Result.ResultingItems))),

// UpgradedInventoryItemEventArgs ev = new(player, itemBase, setting, resultingItems)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(UpgradedInventoryItemEventArgs))[0]),

// Handlers.Scp914.OnUpgradedInventoryItem(ev);
new(OpCodes.Call, Method(typeof(Scp914), nameof(Scp914.OnUpgradedInventoryItem))),
});

newInstructions[index].WithLabels(label);

foreach (CodeInstruction t in newInstructions)
yield return t;

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// -----------------------------------------------------------------------
// <copyright file="UpgradingItem.cs" company="ExMod Team">
// <copyright file="UpgradingPickup.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
Expand Down Expand Up @@ -28,7 +28,7 @@ namespace Exiled.Events.Patches.Events.Scp914
/// </summary>
[EventPatch(typeof(Scp914), nameof(Scp914.UpgradingPickup))]
[HarmonyPatch(typeof(Scp914Upgrader), nameof(Scp914Upgrader.ProcessPickup))]
internal static class UpgradingItem
internal static class UpgradingPickup
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
Expand Down
4 changes: 2 additions & 2 deletions EXILED/Exiled.Events/Patches/Events/Scp914/UpgradingPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
offset = 1;

// index = newInstructions.FindIndex(x => x.opcode == OpCodes.Stloc_S && x.operand is LocalBuilder { LocalIndex: 10 }) + offset;
ConstructorInfo plugin_api_constructor = typeof(LabApi.Events.Arguments.Scp914Events.Scp914ProcessingInventoryItemEventArgs)
ConstructorInfo lab_api_constructor = typeof(LabApi.Events.Arguments.Scp914Events.Scp914ProcessingInventoryItemEventArgs)
.GetConstructor(new[]
{
typeof(InventorySystem.Items.ItemBase),
typeof(Scp914KnobSetting),
typeof(ReferenceHub),
});
index = newInstructions.FindIndex(x => x.Is(OpCodes.Newobj, plugin_api_constructor)) + offset;
index = newInstructions.FindIndex(x => x.Is(OpCodes.Newobj, lab_api_constructor)) + offset;

// ridtp lcz914
// noclip
Expand Down