From 35689487aede2e701f57555632dc56537de4d831 Mon Sep 17 00:00:00 2001 From: ang-xd Date: Sun, 2 Feb 2025 16:08:29 -0500 Subject: [PATCH] Updated events --- MiraAPI.Example/ExampleEventHandlers.cs | 11 ++++++ .../Events/Vanilla/AdminButtonClickEvent.cs | 5 ++- .../Vanilla/Map/PlayerOpenSabotageEvent.cs | 21 +++++++++++ .../Vanilla/SabotageButtonClickEvent.cs | 5 ++- .../Vanilla/Usables/PlayerCanUseEvent.cs | 15 ++++++++ .../Events/Vanilla/Usables/PlayerUseEvent.cs | 34 ++++++++++++++++++ MiraAPI/Events/Vanilla/UseButtonClickEvent.cs | 5 ++- MiraAPI/Patches/Events/CanUsePatches.cs | 36 +++++++++++++++++++ MiraAPI/Patches/Events/MapBehaviourPatches.cs | 21 +++++++++++ MiraAPI/Patches/Events/UsePatches.cs | 35 ++++++++++++++++++ MiraAPI/Patches/Events/VentEventPatches.cs | 4 +-- MiraAPI/Patches/VentPatches.cs | 16 +++++++-- MiraAPI/Utilities/Extensions.cs | 23 ++++++++++-- 13 files changed, 221 insertions(+), 10 deletions(-) create mode 100644 MiraAPI/Events/Vanilla/Map/PlayerOpenSabotageEvent.cs create mode 100644 MiraAPI/Events/Vanilla/Usables/PlayerCanUseEvent.cs create mode 100644 MiraAPI/Events/Vanilla/Usables/PlayerUseEvent.cs create mode 100644 MiraAPI/Patches/Events/CanUsePatches.cs create mode 100644 MiraAPI/Patches/Events/MapBehaviourPatches.cs create mode 100644 MiraAPI/Patches/Events/UsePatches.cs diff --git a/MiraAPI.Example/ExampleEventHandlers.cs b/MiraAPI.Example/ExampleEventHandlers.cs index 21904e4..9d18bc1 100644 --- a/MiraAPI.Example/ExampleEventHandlers.cs +++ b/MiraAPI.Example/ExampleEventHandlers.cs @@ -1,6 +1,7 @@ using MiraAPI.Events; using MiraAPI.Events.Mira; using MiraAPI.Events.Vanilla; +using MiraAPI.Events.Vanilla.Usables; using MiraAPI.Example.Buttons.Freezer; using Reactor.Utilities; @@ -14,6 +15,16 @@ public static class ExampleEventHandlers MiraEventManager.RegisterEventHandler>(FreezeButtonClickHandler, 1); MiraEventManager.RegisterEventHandler>(FreezeButtonCancelledHandler); MiraEventManager.RegisterEventHandler(UpdateSystemEventHandler); + MiraEventManager.RegisterEventHandler(PlayerControlCanUse); + MiraEventManager.RegisterEventHandler(PlayerControlUse); + } + + public static void PlayerControlCanUse(PlayerCanUseEvent @event) + { + } + + public static void PlayerControlUse(PlayerUseEvent @event) + { } public static void UpdateSystemEventHandler(UpdateSystemEvent @event) diff --git a/MiraAPI/Events/Vanilla/AdminButtonClickEvent.cs b/MiraAPI/Events/Vanilla/AdminButtonClickEvent.cs index c37b933..de02bb9 100644 --- a/MiraAPI/Events/Vanilla/AdminButtonClickEvent.cs +++ b/MiraAPI/Events/Vanilla/AdminButtonClickEvent.cs @@ -1,8 +1,11 @@ -namespace MiraAPI.Events.Vanilla; +using System; + +namespace MiraAPI.Events.Vanilla; /// /// Button click event for from Vanilla Among Us. /// +[Obsolete("This class is deprecated. Use PlayerUseEvent instead.")] public class AdminButtonClickEvent : MiraCancelableEvent { /// diff --git a/MiraAPI/Events/Vanilla/Map/PlayerOpenSabotageEvent.cs b/MiraAPI/Events/Vanilla/Map/PlayerOpenSabotageEvent.cs new file mode 100644 index 0000000..8b2b6cb --- /dev/null +++ b/MiraAPI/Events/Vanilla/Map/PlayerOpenSabotageEvent.cs @@ -0,0 +1,21 @@ +namespace MiraAPI.Events.Vanilla.Map; + +/// +/// Sabotage from Vanilla Among Us. +/// +public class PlayerOpenSabotageEvent : MiraCancelableEvent +{ + /// + /// Gets the MapBehaviour. + /// + public MapBehaviour MapBehaviour { get; } + + /// + /// Initializes a new instance of the class. + /// + /// The MapBehaviour. + public PlayerOpenSabotageEvent(MapBehaviour mapBehaviour) + { + MapBehaviour = mapBehaviour; + } +} diff --git a/MiraAPI/Events/Vanilla/SabotageButtonClickEvent.cs b/MiraAPI/Events/Vanilla/SabotageButtonClickEvent.cs index b657486..9d3d2ba 100644 --- a/MiraAPI/Events/Vanilla/SabotageButtonClickEvent.cs +++ b/MiraAPI/Events/Vanilla/SabotageButtonClickEvent.cs @@ -1,8 +1,11 @@ -namespace MiraAPI.Events.Vanilla; +using System; + +namespace MiraAPI.Events.Vanilla; /// /// Button click event for from Vanilla Among Us. /// +[Obsolete("This class is deprecated. Use PlayerOpenSabotageEvent instead.")] public class SabotageButtonClickEvent : MiraCancelableEvent { /// diff --git a/MiraAPI/Events/Vanilla/Usables/PlayerCanUseEvent.cs b/MiraAPI/Events/Vanilla/Usables/PlayerCanUseEvent.cs new file mode 100644 index 0000000..f9ba115 --- /dev/null +++ b/MiraAPI/Events/Vanilla/Usables/PlayerCanUseEvent.cs @@ -0,0 +1,15 @@ +namespace MiraAPI.Events.Vanilla.Usables; + +/// +/// Event for if a can use an from Vanilla Among Us. Will always be ran locally. +/// +public class PlayerCanUseEvent : PlayerUseEvent +{ + /// + /// Initializes a new instance of the class. + /// + /// The IUsable. + public PlayerCanUseEvent(IUsable usable) : base(usable) + { + } +} diff --git a/MiraAPI/Events/Vanilla/Usables/PlayerUseEvent.cs b/MiraAPI/Events/Vanilla/Usables/PlayerUseEvent.cs new file mode 100644 index 0000000..655a46c --- /dev/null +++ b/MiraAPI/Events/Vanilla/Usables/PlayerUseEvent.cs @@ -0,0 +1,34 @@ +namespace MiraAPI.Events.Vanilla; + +/// +/// Event for using a from Vanilla Among Us. Will always be ran locally. +/// +public class PlayerUseEvent : MiraCancelableEvent +{ + /// + /// Gets the instance of that was used. + /// + public IUsable Usable { get; } + + /// + /// Gets a value indicating whether the IUsable is a , , or . + /// + public bool IsPrimaryConsole { get; } + + /// + /// Gets a value indicating whether the IUsable is a . + /// + public bool IsVent { get; } + + /// + /// Initializes a new instance of the class. + /// + /// The IUsable. + public PlayerUseEvent(IUsable usable) + { + Usable = usable; + + IsPrimaryConsole = usable.TryCast() || usable.TryCast() || usable.TryCast(); + IsVent = usable.TryCast(); + } +} diff --git a/MiraAPI/Events/Vanilla/UseButtonClickEvent.cs b/MiraAPI/Events/Vanilla/UseButtonClickEvent.cs index 043b166..8b859fb 100644 --- a/MiraAPI/Events/Vanilla/UseButtonClickEvent.cs +++ b/MiraAPI/Events/Vanilla/UseButtonClickEvent.cs @@ -1,8 +1,11 @@ -namespace MiraAPI.Events.Vanilla; +using System; + +namespace MiraAPI.Events.Vanilla; /// /// Button click event for from Vanilla Among Us. /// +[Obsolete("This class is deprecated. Use PlayerUseEvent instead.")] public class UseButtonClickEvent : MiraCancelableEvent { /// diff --git a/MiraAPI/Patches/Events/CanUsePatches.cs b/MiraAPI/Patches/Events/CanUsePatches.cs new file mode 100644 index 0000000..56a88e6 --- /dev/null +++ b/MiraAPI/Patches/Events/CanUsePatches.cs @@ -0,0 +1,36 @@ +using HarmonyLib; +using MiraAPI.Events; +using MiraAPI.Events.Vanilla.Usables; +using System.Collections.Generic; +using System.Reflection; + +namespace MiraAPI.Patches.Events; + +[HarmonyPatch] +public static class CanUsePatches +{ + public static IEnumerable TargetMethods() + { + yield return AccessTools.Method(typeof(Console), nameof(Console.CanUse)); + yield return AccessTools.Method(typeof(MapConsole), nameof(MapConsole.CanUse)); + yield return AccessTools.Method(typeof(SystemConsole), nameof(SystemConsole.CanUse)); + yield return AccessTools.Method(typeof(Ladder), nameof(Ladder.CanUse)); + yield return AccessTools.Method(typeof(PlatformConsole), nameof(PlatformConsole.CanUse)); + yield return AccessTools.Method(typeof(OpenDoorConsole), nameof(OpenDoorConsole.CanUse)); + yield return AccessTools.Method(typeof(DoorConsole), nameof(DoorConsole.CanUse)); + yield return AccessTools.Method(typeof(OptionsConsole), nameof(OptionsConsole.CanUse)); + yield return AccessTools.Method(typeof(ZiplineConsole), nameof(ZiplineConsole.CanUse)); + } + + [HarmonyPriority(Priority.Last)] + [HarmonyPrefix] + public static bool CanUsePatch(Il2CppSystem.Object __instance, [HarmonyArgument(0)] NetworkedPlayerInfo pc, [HarmonyArgument(1)] out bool canUse, [HarmonyArgument(2)] out bool couldUse) + { + var @event = new PlayerCanUseEvent(__instance.Cast()); + MiraEventManager.InvokeEvent(@event); + + canUse = couldUse = false; + + return !@event.IsCancelled; + } +} diff --git a/MiraAPI/Patches/Events/MapBehaviourPatches.cs b/MiraAPI/Patches/Events/MapBehaviourPatches.cs new file mode 100644 index 0000000..a409226 --- /dev/null +++ b/MiraAPI/Patches/Events/MapBehaviourPatches.cs @@ -0,0 +1,21 @@ +using HarmonyLib; +using MiraAPI.Events; +using MiraAPI.Events.Vanilla.Map; + +namespace MiraAPI.Patches.Events; + +/// +/// Patch for map related MiraEvents. +/// +[HarmonyPatch] +public static class MapBehaviourPatches +{ + [HarmonyPrefix] + [HarmonyPatch(typeof(MapBehaviour), nameof(MapBehaviour.ShowSabotageMap))] + public static bool MapShowSabotagePatch(MapBehaviour __instance) + { + var @event = new PlayerOpenSabotageEvent(__instance); + MiraEventManager.InvokeEvent(@event); + return !@event.IsCancelled; + } +} diff --git a/MiraAPI/Patches/Events/UsePatches.cs b/MiraAPI/Patches/Events/UsePatches.cs new file mode 100644 index 0000000..beb1f69 --- /dev/null +++ b/MiraAPI/Patches/Events/UsePatches.cs @@ -0,0 +1,35 @@ +using HarmonyLib; +using MiraAPI.Events; +using MiraAPI.Events.Vanilla; +using System.Collections.Generic; +using System.Reflection; + +namespace MiraAPI.Patches.Events; + +[HarmonyPatch] +public static class UsePatches +{ + public static IEnumerable TargetMethods() + { + yield return AccessTools.Method(typeof(Console), nameof(Console.Use)); + yield return AccessTools.Method(typeof(MapConsole), nameof(MapConsole.Use)); + yield return AccessTools.Method(typeof(SystemConsole), nameof(SystemConsole.Use)); + yield return AccessTools.Method(typeof(Vent), nameof(Vent.Use)); + yield return AccessTools.Method(typeof(Ladder), nameof(Ladder.Use)); + yield return AccessTools.Method(typeof(PlatformConsole), nameof(PlatformConsole.Use)); + yield return AccessTools.Method(typeof(OpenDoorConsole), nameof(OpenDoorConsole.Use)); + yield return AccessTools.Method(typeof(DoorConsole), nameof(DoorConsole.Use)); + yield return AccessTools.Method(typeof(OptionsConsole), nameof(OptionsConsole.Use)); + yield return AccessTools.Method(typeof(ZiplineConsole), nameof(ZiplineConsole.Use)); + } + + [HarmonyPriority(Priority.Last)] + [HarmonyPrefix] + public static bool UsePatch(Il2CppSystem.Object __instance) + { + var @event = new PlayerUseEvent(__instance.Cast()); + MiraEventManager.InvokeEvent(@event); + + return !@event.IsCancelled; + } +} diff --git a/MiraAPI/Patches/Events/VentEventPatches.cs b/MiraAPI/Patches/Events/VentEventPatches.cs index 2617ff8..e8e5661 100644 --- a/MiraAPI/Patches/Events/VentEventPatches.cs +++ b/MiraAPI/Patches/Events/VentEventPatches.cs @@ -1,7 +1,7 @@ -using System.Linq; -using HarmonyLib; +using HarmonyLib; using MiraAPI.Events; using MiraAPI.Events.Vanilla; +using System.Linq; namespace MiraAPI.Patches.Events; diff --git a/MiraAPI/Patches/VentPatches.cs b/MiraAPI/Patches/VentPatches.cs index 66c9aae..621abbc 100644 --- a/MiraAPI/Patches/VentPatches.cs +++ b/MiraAPI/Patches/VentPatches.cs @@ -1,4 +1,6 @@ using HarmonyLib; +using MiraAPI.Events; +using MiraAPI.Events.Vanilla.Usables; using MiraAPI.Roles; using MiraAPI.Utilities; using UnityEngine; @@ -18,6 +20,15 @@ public static class VentPatches [HarmonyPatch(nameof(Vent.CanUse))] public static void VentCanUsePostfix(Vent __instance, ref float __result, [HarmonyArgument(0)] NetworkedPlayerInfo pc, [HarmonyArgument(1)] ref bool canUse, [HarmonyArgument(2)] ref bool couldUse) { + var @event = new PlayerCanUseEvent(__instance.Cast()); + MiraEventManager.InvokeEvent(@event); + + if (@event.IsCancelled) + { + canUse = couldUse = false; + return; + } + var @object = pc.Object; var role = @object.Data.Role; @@ -29,10 +40,10 @@ public static class VentPatches { switch (canVent) { - case true when modifiers.Exists(x => x.CanVent().HasValue && x.CanVent()==false): + case true when modifiers.Exists(x => x.CanVent().HasValue && x.CanVent() == false): couldUse = canUse = false; return; - case false when modifiers.Exists(x => x.CanVent().HasValue && x.CanVent()==true): + case false when modifiers.Exists(x => x.CanVent().HasValue && x.CanVent() == true): couldUse = true; break; } @@ -48,6 +59,7 @@ public static class VentPatches num = Vector2.Distance(center, position); canUse &= num <= __instance.UsableDistance && !PhysicsHelpers.AnythingBetween(@object.Collider, center, position, Constants.ShipOnlyMask, false); } + __result = num; } } diff --git a/MiraAPI/Utilities/Extensions.cs b/MiraAPI/Utilities/Extensions.cs index 032cff3..636601b 100644 --- a/MiraAPI/Utilities/Extensions.cs +++ b/MiraAPI/Utilities/Extensions.cs @@ -1,6 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; using HarmonyLib; using MiraAPI.GameOptions; using MiraAPI.Modifiers; @@ -8,6 +5,10 @@ using MiraAPI.Networking; using MiraAPI.Roles; using Reactor.Networking.Attributes; using Reactor.Utilities; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; using UnityEngine; namespace MiraAPI.Utilities; @@ -17,6 +18,22 @@ namespace MiraAPI.Utilities; /// public static class Extensions { + public static IEnumerable GetTypesSafe(this Assembly assembly) + { + try + { + return assembly.GetTypes(); + } + catch (ReflectionTypeLoadException ex) + { + return ex.Types.Where(t => t != null); + } + catch + { + return Enumerable.Empty(); + } + } + internal static NetData GetNetData(this ICustomRole role) { var count = role.GetCount();