From f68e1b345d219fa5a56de78972ce008d827c8fb7 Mon Sep 17 00:00:00 2001
From: IRacle <79921583+IRacle1@users.noreply.github.com>
Date: Thu, 26 Dec 2024 19:01:03 +0300
Subject: [PATCH 1/3] fix: microhid events (#339)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* feat/fix: microhid events
* fix: nuh that build errorš„
* Update EXILED/Exiled.Events/EventArgs/Item/ChangingMicroHIDPickupStateEventArgs.cs
---------
Co-authored-by: Yamato <66829532+louis1706@users.noreply.github.com>
---
.../ChangingMicroHIDPickupStateEventArgs.cs | 58 +++++++++++++++
.../Player/ChangingMicroHIDStateEventArgs.cs | 6 +-
EXILED/Exiled.Events/Handlers/Item.cs | 11 +++
.../Events/Player/ChangingMicroHIDState.cs | 70 ++++++++++++-------
4 files changed, 118 insertions(+), 27 deletions(-)
create mode 100644 EXILED/Exiled.Events/EventArgs/Item/ChangingMicroHIDPickupStateEventArgs.cs
diff --git a/EXILED/Exiled.Events/EventArgs/Item/ChangingMicroHIDPickupStateEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Item/ChangingMicroHIDPickupStateEventArgs.cs
new file mode 100644
index 000000000..a70c37fde
--- /dev/null
+++ b/EXILED/Exiled.Events/EventArgs/Item/ChangingMicroHIDPickupStateEventArgs.cs
@@ -0,0 +1,58 @@
+// -----------------------------------------------------------------------
+//
+// Copyright (c) ExMod Team. All rights reserved.
+// Licensed under the CC BY-SA 3.0 license.
+//
+// -----------------------------------------------------------------------
+
+namespace Exiled.Events.EventArgs.Item
+{
+ using Exiled.API.Features.Pickups;
+
+ using Interfaces;
+ using InventorySystem.Items.MicroHID.Modules;
+ using InventorySystem.Items.Pickups;
+
+ ///
+ /// Contains all information before MicroHID pickup state is changed.
+ ///
+ public class ChangingMicroHIDPickupStateEventArgs : IDeniableEvent, IPickupEvent
+ {
+ ///
+ /// Initializes a new instance of the class.
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ public ChangingMicroHIDPickupStateEventArgs(ItemPickupBase microHID, MicroHidPhase newPhase, bool isAllowed = true)
+ {
+ MicroHID = Pickup.Get(microHID);
+ NewPhase = newPhase;
+ IsAllowed = isAllowed;
+ }
+
+ ///
+ /// Gets the MicroHID instance.
+ ///
+ public MicroHIDPickup MicroHID { get; }
+
+ ///
+ /// Gets or sets the new MicroHID state.
+ ///
+ public MicroHidPhase NewPhase { get; set; }
+
+ ///
+ /// Gets or sets a value indicating whether the MicroHID state can be changed.
+ ///
+ public bool IsAllowed { get; set; }
+
+ ///
+ public Pickup Pickup => MicroHID;
+ }
+}
\ No newline at end of file
diff --git a/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs b/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs
index f472a7096..2881241cd 100644
--- a/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs
+++ b/EXILED/Exiled.Events/EventArgs/Player/ChangingMicroHIDStateEventArgs.cs
@@ -12,6 +12,8 @@ namespace Exiled.Events.EventArgs.Player
using API.Features;
using API.Features.Items;
using Interfaces;
+
+ using InventorySystem.Items;
using InventorySystem.Items.MicroHID;
using InventorySystem.Items.MicroHID.Modules;
@@ -32,9 +34,9 @@ public class ChangingMicroHIDStateEventArgs : IDeniableEvent, IItemEvent
///
///
///
- public ChangingMicroHIDStateEventArgs(Item microHID, MicroHidPhase newPhase, bool isAllowed = true)
+ public ChangingMicroHIDStateEventArgs(ItemBase microHID, MicroHidPhase newPhase, bool isAllowed = true)
{
- MicroHID = microHID.As();
+ MicroHID = Item.Get(microHID);
NewPhase = newPhase;
IsAllowed = isAllowed;
}
diff --git a/EXILED/Exiled.Events/Handlers/Item.cs b/EXILED/Exiled.Events/Handlers/Item.cs
index 22c8907a2..1c3f8b15e 100644
--- a/EXILED/Exiled.Events/Handlers/Item.cs
+++ b/EXILED/Exiled.Events/Handlers/Item.cs
@@ -53,6 +53,11 @@ public static class Item
///
public static Event UsingRadioPickupBattery { get; set; } = new();
+ ///
+ /// Invoked before a state is changed.
+ ///
+ public static Event ChangingMicroHIDPickupState { get; set; } = new();
+
///
/// Called before the ammo of an firearm is changed.
///
@@ -94,5 +99,11 @@ public static class Item
///
/// The instance.
public static void OnUsingRadioPickupBattery(UsingRadioPickupBatteryEventArgs ev) => UsingRadioPickupBattery.InvokeSafely(ev);
+
+ ///
+ /// Called before a state is changed.
+ ///
+ /// The instance.
+ public static void OnChangingMicroHIDPickupState(ChangingMicroHIDPickupStateEventArgs ev) => ChangingMicroHIDPickupState.InvokeSafely(ev);
}
}
\ No newline at end of file
diff --git a/EXILED/Exiled.Events/Patches/Events/Player/ChangingMicroHIDState.cs b/EXILED/Exiled.Events/Patches/Events/Player/ChangingMicroHIDState.cs
index c45000b67..fdab78241 100644
--- a/EXILED/Exiled.Events/Patches/Events/Player/ChangingMicroHIDState.cs
+++ b/EXILED/Exiled.Events/Patches/Events/Player/ChangingMicroHIDState.cs
@@ -14,6 +14,7 @@ namespace Exiled.Events.Patches.Events.Player
using API.Features.Items;
using API.Features.Pools;
using Exiled.Events.Attributes;
+ using Exiled.Events.EventArgs.Item;
using Exiled.Events.EventArgs.Player;
using HarmonyLib;
using InventorySystem.Items.MicroHID;
@@ -34,43 +35,43 @@ private static IEnumerable Transpiler(IEnumerable newInstructions = ListPool.Pool.Get(instructions);
Label returnLabel = generator.DefineLabel();
+ Label pickupLabel = generator.DefineLabel();
+ Label continueLabel = generator.DefineLabel();
- LocalBuilder ev = generator.DeclareLocal(typeof(ChangingMicroHIDStateEventArgs));
+ LocalBuilder item = generator.DeclareLocal(typeof(MicroHIDItem));
int offset = 1;
int index = newInstructions.FindIndex(x => x.opcode == OpCodes.Ret) + offset;
newInstructions.InsertRange(index, new[]
{
- // Item.Get(this.Serial);
+ // MicroHIDItem item = Item.Get(this._prevItem);
+ // if (item is null)
+ // pickup code;
+ // else
+ // itemCode;
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
- new(OpCodes.Ldfld, Field(typeof(CycleController), nameof(CycleController.Serial))),
- new(OpCodes.Call, GetDeclaredMethods(typeof(Item)).Find(x => !x.IsGenericMethod && x.IsStatic && x.GetParameters().FirstOrDefault()?.ParameterType == typeof(ushort))),
+ new(OpCodes.Ldfld, Field(typeof(CycleController), nameof(CycleController._prevItem))),
+ new(OpCodes.Dup),
+ new(OpCodes.Stloc_S, item.LocalIndex),
+ new(OpCodes.Brfalse_S, pickupLabel),
- // value
- new(OpCodes.Ldarg_1),
+ // bool allowed = CallItemEvent(microHidItem, phase)
+ new(OpCodes.Ldloc_S, item.LocalIndex),
+ new(OpCodes.Ldarga_S, 1),
+ new(OpCodes.Call, Method(typeof(ChangingMicroHIDState), nameof(ChangingMicroHIDState.CallItemEvent))),
- // true
- new(OpCodes.Ldc_I4_1),
+ new CodeInstruction(OpCodes.Br_S, continueLabel),
- // ChangerMicroHIDStateEventArgs ev = new(Item.Get(this.Serial), value, true);
- new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ChangingMicroHIDStateEventArgs))[0]),
- new(OpCodes.Dup),
- new(OpCodes.Dup),
- new(OpCodes.Stloc_S, ev.LocalIndex),
-
- // Handlers.Player.OnChangingMicroHIDState(ev);
- new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnChangingMicroHIDState))),
+ // bool allowed = CallPickupEvent(Serial, phase)
+ new CodeInstruction(OpCodes.Ldarg_0).WithLabels(pickupLabel),
+ new(OpCodes.Ldfld, Field(typeof(CycleController), nameof(CycleController.Serial))),
+ new(OpCodes.Ldarga_S, 1),
+ new(OpCodes.Call, Method(typeof(ChangingMicroHIDState), nameof(ChangingMicroHIDState.CallPickupEvent))),
- // if (!ev.IsAllowed)
+ // if (!allowed)
// return;
- new(OpCodes.Callvirt, PropertyGetter(typeof(ChangingMicroHIDStateEventArgs), nameof(ChangingMicroHIDStateEventArgs.IsAllowed))),
- new(OpCodes.Brfalse_S, returnLabel),
-
- // value = ev.NewPhase;
- new(OpCodes.Ldloc_S, ev.LocalIndex),
- new(OpCodes.Callvirt, PropertyGetter(typeof(ChangingMicroHIDStateEventArgs), nameof(ChangingMicroHIDStateEventArgs.NewPhase))),
- new(OpCodes.Starg_S, 1),
+ new CodeInstruction(OpCodes.Brfalse_S, returnLabel).WithLabels(continueLabel),
});
newInstructions[newInstructions.Count - 1].labels.Add(returnLabel);
@@ -80,5 +81,24 @@ private static IEnumerable Transpiler(IEnumerable.Pool.Return(newInstructions);
}
+
+ private static bool CallItemEvent(MicroHIDItem microHIDItem, ref MicroHidPhase phase)
+ {
+ ChangingMicroHIDStateEventArgs ev = new(microHIDItem, phase);
+ Handlers.Player.OnChangingMicroHIDState(ev);
+ phase = ev.NewPhase;
+ return ev.IsAllowed;
+ }
+
+ private static bool CallPickupEvent(ushort serial, ref MicroHidPhase phase)
+ {
+ if (!MicroHIDPickup.PickupsBySerial.TryGetValue(serial, out MicroHIDPickup pickup))
+ return true;
+
+ ChangingMicroHIDPickupStateEventArgs ev = new(pickup, phase);
+ Handlers.Item.OnChangingMicroHIDPickupState(ev);
+ phase = ev.NewPhase;
+ return ev.IsAllowed;
+ }
}
-}
\ No newline at end of file
+}
From d513ff1e8b2e4005ff588abdc947742fb2e2bc1f Mon Sep 17 00:00:00 2001
From: Yamato
Date: Thu, 26 Dec 2024 17:32:25 +0100
Subject: [PATCH 2/3] ItemAddReason.AdminCommand has been choose
---
EXILED/Exiled.API/Features/Player.cs | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/EXILED/Exiled.API/Features/Player.cs b/EXILED/Exiled.API/Features/Player.cs
index 778f94133..8bf776590 100644
--- a/EXILED/Exiled.API/Features/Player.cs
+++ b/EXILED/Exiled.API/Features/Player.cs
@@ -2781,7 +2781,7 @@ public void AddItem(Firearm item, IEnumerable identifiers)
/// The of the item to be added.
/// The reason the item was added.
/// The that was added.
- public Item AddItem(Pickup pickup, ItemAddReason addReason = ItemAddReason.Undefined) => Item.Get(Inventory.ServerAddItem(pickup.Type, addReason, pickup.Serial, pickup.Base));
+ public Item AddItem(Pickup pickup, ItemAddReason addReason = ItemAddReason.AdminCommand) => Item.Get(Inventory.ServerAddItem(pickup.Type, addReason, pickup.Serial, pickup.Base));
///
/// Adds an item to the player's inventory.
@@ -2791,7 +2791,7 @@ public void AddItem(Firearm item, IEnumerable identifiers)
/// The that was added.
public Item AddItem(FirearmPickup pickup, IEnumerable identifiers)
{
- Firearm firearm = Item.Get(Inventory.ServerAddItem(pickup.Type, ItemAddReason.Undefined, pickup.Serial, pickup.Base));
+ Firearm firearm = Item.Get(Inventory.ServerAddItem(pickup.Type, ItemAddReason.AdminCommand, pickup.Serial, pickup.Base));
if (identifiers is not null)
firearm.AddAttachment(identifiers);
From b1920da33203d5b49f005a3d7909ab42b5fd1932 Mon Sep 17 00:00:00 2001
From: Yamato <66829532+louis1706@users.noreply.github.com>
Date: Thu, 26 Dec 2024 19:24:20 +0100
Subject: [PATCH 3/3] fix: RoomLightControllersValue Fix (#346)
* RoomLightControllersValue Fix
* remove non used using
---
EXILED/Exiled.API/Features/Room.cs | 2 -
.../Generic/RoomLightControllersList.cs | 39 +++++++++++++++++++
2 files changed, 39 insertions(+), 2 deletions(-)
create mode 100644 EXILED/Exiled.Events/Patches/Generic/RoomLightControllersList.cs
diff --git a/EXILED/Exiled.API/Features/Room.cs b/EXILED/Exiled.API/Features/Room.cs
index f0440ad64..4efae3acd 100644
--- a/EXILED/Exiled.API/Features/Room.cs
+++ b/EXILED/Exiled.API/Features/Room.cs
@@ -517,8 +517,6 @@ private void InternalCreate()
Log.Error($"[ROOMTYPE UNKNOWN] {this} Name : {gameObject?.name} Shape : {Identifier?.Shape}");
#endif
- RoomLightControllersValue.AddRange(gameObject.GetComponentsInChildren());
-
RoomLightControllers = RoomLightControllersValue.AsReadOnly();
GetComponentsInChildren().ForEach(component =>
diff --git a/EXILED/Exiled.Events/Patches/Generic/RoomLightControllersList.cs b/EXILED/Exiled.Events/Patches/Generic/RoomLightControllersList.cs
new file mode 100644
index 000000000..129585e26
--- /dev/null
+++ b/EXILED/Exiled.Events/Patches/Generic/RoomLightControllersList.cs
@@ -0,0 +1,39 @@
+// -----------------------------------------------------------------------
+//
+// Copyright (c) ExMod Team. All rights reserved.
+// Licensed under the CC BY-SA 3.0 license.
+//
+// -----------------------------------------------------------------------
+
+namespace Exiled.Events.Patches.Generic
+{
+ using Exiled.API.Features;
+#pragma warning disable SA1313
+#pragma warning disable SA1402
+
+ using HarmonyLib;
+
+ ///
+ /// Patch for adding to list.
+ ///
+ [HarmonyPatch(typeof(RoomLightController), nameof(RoomLightController.Start))]
+ internal class RoomLightControllersList
+ {
+ private static void Postfix(RoomLightController __instance)
+ {
+ Room.Get(__instance.Room).RoomLightControllersValue.Add(__instance);
+ }
+ }
+
+ ///
+ /// Patch for removing to list.
+ ///
+ [HarmonyPatch(typeof(RoomLightController), nameof(RoomLightController.OnDestroy))]
+ internal class RoomLightControllersList2
+ {
+ private static void Postfix(RoomLightController __instance)
+ {
+ Room.Get(__instance.Room).RoomLightControllersValue.Remove(__instance);
+ }
+ }
+}
\ No newline at end of file