diff --git a/MiraAPI.Example/ExampleEventHandlers.cs b/MiraAPI.Example/ExampleEventHandlers.cs index 231d202..21904e4 100644 --- a/MiraAPI.Example/ExampleEventHandlers.cs +++ b/MiraAPI.Example/ExampleEventHandlers.cs @@ -13,28 +13,12 @@ public static class ExampleEventHandlers // Register event handlers here MiraEventManager.RegisterEventHandler>(FreezeButtonClickHandler, 1); MiraEventManager.RegisterEventHandler>(FreezeButtonCancelledHandler); - MiraEventManager.RegisterEventHandler(EnterVentHandler); - MiraEventManager.RegisterEventHandler(ExitVentHandler); + MiraEventManager.RegisterEventHandler(UpdateSystemEventHandler); } - public static void EnterVentHandler(EnterVentEvent @event) + public static void UpdateSystemEventHandler(UpdateSystemEvent @event) { - Logger.Warning("Entering vent!"); - var random = new System.Random(); - if (random.Next(0, 2) == 0) - { - @event.Cancel(); - } - } - - public static void ExitVentHandler(ExitVentEvent @event) - { - Logger.Warning("Exiting vent!"); - var random = new System.Random(); - if (random.Next(0, 2) == 0) - { - @event.Cancel(); - } + Logger.Error(@event.SystemType.ToString()); } // Example event handler diff --git a/MiraAPI/Events/Vanilla/CloseDoorsEvent.cs b/MiraAPI/Events/Vanilla/CloseDoorsEvent.cs new file mode 100644 index 0000000..8d0b9bf --- /dev/null +++ b/MiraAPI/Events/Vanilla/CloseDoorsEvent.cs @@ -0,0 +1,21 @@ +namespace MiraAPI.Events.Vanilla; + +/// +/// Event fired when a player closes the doors in a room. +/// +public class CloseDoorsEvent : MiraCancelableEvent +{ + /// + /// Gets the room that the doors were closed in. + /// + public SystemTypes Room { get; } + + /// + /// Initializes a new instance of the class. + /// + /// The room that was closed. + public CloseDoorsEvent(SystemTypes room) + { + Room = room; + } +} diff --git a/MiraAPI/Patches/Events/HudButtonEventPatches.cs b/MiraAPI/Patches/Events/HudButtonEventPatches.cs index 42dde11..8ea7c5b 100644 --- a/MiraAPI/Patches/Events/HudButtonEventPatches.cs +++ b/MiraAPI/Patches/Events/HudButtonEventPatches.cs @@ -1,6 +1,7 @@ using HarmonyLib; using MiraAPI.Events; using MiraAPI.Events.Vanilla; +using Reactor.Utilities; namespace MiraAPI.Patches.Events; diff --git a/MiraAPI/Patches/Events/SabotageEventPatches.cs b/MiraAPI/Patches/Events/SabotageEventPatches.cs index fc93bdd..b8a1fd2 100644 --- a/MiraAPI/Patches/Events/SabotageEventPatches.cs +++ b/MiraAPI/Patches/Events/SabotageEventPatches.cs @@ -11,10 +11,19 @@ namespace MiraAPI.Patches.Events; public static class SabotageEventPatches { [HarmonyPrefix] - [HarmonyPatch(typeof(ShipStatus), nameof(ShipStatus.UpdateSystem), typeof(SystemTypes), typeof(PlayerControl), typeof(byte))] - public static bool ShipStatusUpdateSystemPrefix(ShipStatus __instance, SystemTypes systemType, PlayerControl player, byte amount) + [HarmonyPatch(typeof(ShipStatus), nameof(ShipStatus.RpcUpdateSystem), typeof(SystemTypes), typeof(byte))] + public static bool ShipStatusUpdateSystemPrefix(ShipStatus __instance, SystemTypes systemType, byte amount) { - var @event = new UpdateSystemEvent(systemType, player, amount); + var @event = new UpdateSystemEvent(systemType, PlayerControl.LocalPlayer, amount); + MiraEventManager.InvokeEvent(@event); + return !@event.IsCancelled; + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(ShipStatus), nameof(ShipStatus.RpcCloseDoorsOfType))] + public static bool ShipStatusCloseDoorsOfTypePrefix(ShipStatus __instance, SystemTypes type) + { + var @event = new CloseDoorsEvent(type); MiraEventManager.InvokeEvent(@event); return !@event.IsCancelled; } diff --git a/MiraAPI/Patches/Events/StartMeetingEventPatch.cs b/MiraAPI/Patches/Events/StartMeetingEventPatch.cs index f818363..00f8ba4 100644 --- a/MiraAPI/Patches/Events/StartMeetingEventPatch.cs +++ b/MiraAPI/Patches/Events/StartMeetingEventPatch.cs @@ -9,10 +9,11 @@ namespace MiraAPI.Patches.Events; public static class StartMeetingEventPatch { [HarmonyPrefix] - [HarmonyPatch(typeof(PlayerControl), nameof(PlayerControl.ReportDeadBody))] - public static bool PlayerControlReportDeadBodyPrefix(PlayerControl __instance, NetworkedPlayerInfo? target) + [HarmonyPatch(typeof(PlayerControl), nameof(PlayerControl.CmdReportDeadBody))] + public static bool PlayerControlCmdReportDeadBodyPrefix(PlayerControl __instance, NetworkedPlayerInfo? target) { var body = target != null ? Helpers.GetBodyById(target.PlayerId) : null; + var @event = new StartMeetingEvent(__instance, target, body); MiraEventManager.InvokeEvent(@event); return !@event.IsCancelled; diff --git a/MiraAPI/Patches/Events/VentEventPatches.cs b/MiraAPI/Patches/Events/VentEventPatches.cs index 667f35a..f978f15 100644 --- a/MiraAPI/Patches/Events/VentEventPatches.cs +++ b/MiraAPI/Patches/Events/VentEventPatches.cs @@ -14,6 +14,7 @@ namespace MiraAPI.Patches.Events; [HarmonyPatch] public static class VentEventPatches { + // necessary because Vent.Use is inlined in il2cpp. private static bool _showButtons; [HarmonyPostfix] @@ -24,8 +25,8 @@ public static class VentEventPatches } [HarmonyPrefix] - [HarmonyPatch(typeof(PlayerPhysics), nameof(PlayerPhysics.CoEnterVent))] - public static bool EnterVentPrefix(PlayerPhysics __instance, int id, ref Il2CppSystem.Collections.IEnumerator __result) + [HarmonyPatch(typeof(PlayerPhysics), nameof(PlayerPhysics.RpcEnterVent))] + public static bool PlayerPhysicsRpcEnterVentPrefix(PlayerPhysics __instance, int id) { var pc = __instance.myPlayer; var vent = ShipStatus.Instance.AllVents.FirstOrDefault(v => v.Id == id); @@ -33,12 +34,6 @@ public static class VentEventPatches var @event = new EnterVentEvent(pc, vent); MiraEventManager.InvokeEvent(@event); - if (@event.IsCancelled) - { - Logger.Error("Enter vent event cancelled!"); - __result = Helpers.EmptyCoroutine().WrapToIl2Cpp(); - } - if (pc.AmOwner) { _showButtons = !@event.IsCancelled; @@ -47,8 +42,8 @@ public static class VentEventPatches } [HarmonyPrefix] - [HarmonyPatch(typeof(PlayerPhysics), nameof(PlayerPhysics.CoExitVent))] - public static bool ExitVentPrefix(PlayerPhysics __instance, int id, ref Il2CppSystem.Collections.IEnumerator __result) + [HarmonyPatch(typeof(PlayerPhysics), nameof(PlayerPhysics.RpcExitVent))] + public static bool PlayerPhysicsRpcExitVentPrefix(PlayerPhysics __instance, int id) { var pc = __instance.myPlayer; var vent = ShipStatus.Instance.AllVents.First(v => v.Id == id); @@ -56,12 +51,6 @@ public static class VentEventPatches var @event = new ExitVentEvent(pc, vent); MiraEventManager.InvokeEvent(@event); - if (@event.IsCancelled) - { - Logger.Error("Exit vent event cancelled!"); - __result = Helpers.EmptyCoroutine().WrapToIl2Cpp(); - } - if (pc.AmOwner) { _showButtons = @event.IsCancelled;