From 04ee35d708fa622415c841737d65d234fea78264 Mon Sep 17 00:00:00 2001 From: XtraCube <72575280+XtraCube@users.noreply.github.com> Date: Sun, 8 Dec 2024 14:56:13 -0500 Subject: [PATCH] basic "sabotage" event --- MiraAPI/Events/Vanilla/UpdateSystemEvent.cs | 28 +++++++++++++++++++ .../Patches/Events/HudButtonEventPatches.cs | 1 + .../Patches/Events/SabotageEventPatches.cs | 21 ++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 MiraAPI/Events/Vanilla/UpdateSystemEvent.cs create mode 100644 MiraAPI/Patches/Events/SabotageEventPatches.cs diff --git a/MiraAPI/Events/Vanilla/UpdateSystemEvent.cs b/MiraAPI/Events/Vanilla/UpdateSystemEvent.cs new file mode 100644 index 0000000..3353c36 --- /dev/null +++ b/MiraAPI/Events/Vanilla/UpdateSystemEvent.cs @@ -0,0 +1,28 @@ +namespace MiraAPI.Events.Vanilla; + +/// +/// Cancelable event that is invoked before a system is updated. Usually used for sabotage events. +/// +public class UpdateSystemEvent : MiraCancelableEvent +{ + /// + /// Gets the type of system that will be updated. + /// + public SystemTypes SystemType { get; } + + /// + /// Gets the byte amount the system is being updated by. + /// + public byte Amount { get; } + + /// + /// Initializes a new instance of the class. + /// + /// The SystemType being updated. + /// Amount to update System to. + public UpdateSystemEvent(SystemTypes systemType, byte amount) + { + SystemType = systemType; + Amount = amount; + } +} diff --git a/MiraAPI/Patches/Events/HudButtonEventPatches.cs b/MiraAPI/Patches/Events/HudButtonEventPatches.cs index 212bb75..0643b4f 100644 --- a/MiraAPI/Patches/Events/HudButtonEventPatches.cs +++ b/MiraAPI/Patches/Events/HudButtonEventPatches.cs @@ -7,6 +7,7 @@ namespace MiraAPI.Patches.Events; /// /// Patch for button related MiraEvents. /// +[HarmonyPatch] public static class HudButtonEventPatches { [HarmonyPrefix] diff --git a/MiraAPI/Patches/Events/SabotageEventPatches.cs b/MiraAPI/Patches/Events/SabotageEventPatches.cs new file mode 100644 index 0000000..9cd6fc3 --- /dev/null +++ b/MiraAPI/Patches/Events/SabotageEventPatches.cs @@ -0,0 +1,21 @@ +using HarmonyLib; +using MiraAPI.Events; +using MiraAPI.Events.Vanilla; + +namespace MiraAPI.Patches.Events; + +/// +/// Used for patching sabotage/system related MiraEvents. +/// +[HarmonyPatch] +public static class SabotageEventPatches +{ + [HarmonyPrefix] + [HarmonyPatch(typeof(ShipStatus), nameof(ShipStatus.RpcUpdateSystem), typeof(SystemTypes), typeof(byte))] + public static bool ShipStatusRpcUpdateSystemPrefix(ShipStatus __instance, SystemTypes systemType, byte amount) + { + var @event = new UpdateSystemEvent(systemType, amount); + MiraEventManager.InvokeEvent(@event); + return !@event.IsCancelled; + } +}