Skip to content

Commit

Permalink
Updated events
Browse files Browse the repository at this point in the history
  • Loading branch information
ang-xd committed Feb 2, 2025
1 parent 51fcc2c commit 3568948
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 10 deletions.
11 changes: 11 additions & 0 deletions MiraAPI.Example/ExampleEventHandlers.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -14,6 +15,16 @@ public static void Initialize()
MiraEventManager.RegisterEventHandler<MiraButtonClickEvent<FreezeButton>>(FreezeButtonClickHandler, 1);
MiraEventManager.RegisterEventHandler<MiraButtonCancelledEvent<FreezeButton>>(FreezeButtonCancelledHandler);
MiraEventManager.RegisterEventHandler<UpdateSystemEvent>(UpdateSystemEventHandler);
MiraEventManager.RegisterEventHandler<PlayerCanUseEvent>(PlayerControlCanUse);
MiraEventManager.RegisterEventHandler<PlayerUseEvent>(PlayerControlUse);
}

public static void PlayerControlCanUse(PlayerCanUseEvent @event)
{
}

public static void PlayerControlUse(PlayerUseEvent @event)
{
}

public static void UpdateSystemEventHandler(UpdateSystemEvent @event)
Expand Down
5 changes: 4 additions & 1 deletion MiraAPI/Events/Vanilla/AdminButtonClickEvent.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace MiraAPI.Events.Vanilla;
using System;

namespace MiraAPI.Events.Vanilla;

/// <summary>
/// Button click event for <see cref="AdminButton"/> from Vanilla Among Us.
/// </summary>
[Obsolete("This class is deprecated. Use PlayerUseEvent instead.")]
public class AdminButtonClickEvent : MiraCancelableEvent
{
/// <summary>
Expand Down
21 changes: 21 additions & 0 deletions MiraAPI/Events/Vanilla/Map/PlayerOpenSabotageEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace MiraAPI.Events.Vanilla.Map;

/// <summary>
/// Sabotage from Vanilla Among Us.
/// </summary>
public class PlayerOpenSabotageEvent : MiraCancelableEvent
{
/// <summary>
/// Gets the MapBehaviour.
/// </summary>
public MapBehaviour MapBehaviour { get; }

/// <summary>
/// Initializes a new instance of the <see cref="PlayerOpenSabotageEvent"/> class.
/// </summary>
/// <param name="mapBehaviour">The MapBehaviour.</param>
public PlayerOpenSabotageEvent(MapBehaviour mapBehaviour)
{
MapBehaviour = mapBehaviour;
}
}
5 changes: 4 additions & 1 deletion MiraAPI/Events/Vanilla/SabotageButtonClickEvent.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace MiraAPI.Events.Vanilla;
using System;

namespace MiraAPI.Events.Vanilla;

/// <summary>
/// Button click event for <see cref="SabotageButton"/> from Vanilla Among Us.
/// </summary>
[Obsolete("This class is deprecated. Use PlayerOpenSabotageEvent instead.")]
public class SabotageButtonClickEvent : MiraCancelableEvent
{
/// <summary>
Expand Down
15 changes: 15 additions & 0 deletions MiraAPI/Events/Vanilla/Usables/PlayerCanUseEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace MiraAPI.Events.Vanilla.Usables;

/// <summary>
/// Event for if a <see cref="PlayerControl"/> can use an <see cref="IUsable"/> from Vanilla Among Us. Will always be ran locally.
/// </summary>
public class PlayerCanUseEvent : PlayerUseEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="PlayerCanUseEvent"/> class.
/// </summary>
/// <param name="usable">The IUsable.</param>
public PlayerCanUseEvent(IUsable usable) : base(usable)
{
}
}
34 changes: 34 additions & 0 deletions MiraAPI/Events/Vanilla/Usables/PlayerUseEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace MiraAPI.Events.Vanilla;

/// <summary>
/// Event for <see cref="PlayerControl"/> using a <see cref="IUsable"/> from Vanilla Among Us. Will always be ran locally.
/// </summary>
public class PlayerUseEvent : MiraCancelableEvent
{
/// <summary>
/// Gets the instance of <see cref="IUsable"/> that was used.
/// </summary>
public IUsable Usable { get; }

/// <summary>
/// Gets a value indicating whether the IUsable is a <see cref="Console"/>, <see cref="MapConsole"/>, or <see cref="SystemConsole"/>.
/// </summary>
public bool IsPrimaryConsole { get; }

/// <summary>
/// Gets a value indicating whether the IUsable is a <see cref="Vent"/>.
/// </summary>
public bool IsVent { get; }

/// <summary>
/// Initializes a new instance of the <see cref="PlayerUseEvent"/> class.
/// </summary>
/// <param name="usable">The IUsable.</param>
public PlayerUseEvent(IUsable usable)
{
Usable = usable;

IsPrimaryConsole = usable.TryCast<Console>() || usable.TryCast<SystemConsole>() || usable.TryCast<MapConsole>();
IsVent = usable.TryCast<Vent>();
}
}
5 changes: 4 additions & 1 deletion MiraAPI/Events/Vanilla/UseButtonClickEvent.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace MiraAPI.Events.Vanilla;
using System;

namespace MiraAPI.Events.Vanilla;

/// <summary>
/// Button click event for <see cref="UseButton"/> from Vanilla Among Us.
/// </summary>
[Obsolete("This class is deprecated. Use PlayerUseEvent instead.")]
public class UseButtonClickEvent : MiraCancelableEvent
{
/// <summary>
Expand Down
36 changes: 36 additions & 0 deletions MiraAPI/Patches/Events/CanUsePatches.cs
Original file line number Diff line number Diff line change
@@ -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<MethodBase> 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<IUsable>());
MiraEventManager.InvokeEvent(@event);

canUse = couldUse = false;

return !@event.IsCancelled;
}
}
21 changes: 21 additions & 0 deletions MiraAPI/Patches/Events/MapBehaviourPatches.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using HarmonyLib;
using MiraAPI.Events;
using MiraAPI.Events.Vanilla.Map;

namespace MiraAPI.Patches.Events;

/// <summary>
/// Patch for map related MiraEvents.
/// </summary>
[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;
}
}
35 changes: 35 additions & 0 deletions MiraAPI/Patches/Events/UsePatches.cs
Original file line number Diff line number Diff line change
@@ -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<MethodBase> 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<IUsable>());
MiraEventManager.InvokeEvent(@event);

return !@event.IsCancelled;
}
}
4 changes: 2 additions & 2 deletions MiraAPI/Patches/Events/VentEventPatches.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
16 changes: 14 additions & 2 deletions MiraAPI/Patches/VentPatches.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using HarmonyLib;
using MiraAPI.Events;
using MiraAPI.Events.Vanilla.Usables;
using MiraAPI.Roles;
using MiraAPI.Utilities;
using UnityEngine;
Expand All @@ -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<IUsable>());
MiraEventManager.InvokeEvent(@event);

if (@event.IsCancelled)
{
canUse = couldUse = false;
return;
}

var @object = pc.Object;
var role = @object.Data.Role;

Expand All @@ -29,10 +40,10 @@ public static void VentCanUsePostfix(Vent __instance, ref float __result, [Harmo
{
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;
}
Expand All @@ -48,6 +59,7 @@ public static void VentCanUsePostfix(Vent __instance, ref float __result, [Harmo
num = Vector2.Distance(center, position);
canUse &= num <= __instance.UsableDistance && !PhysicsHelpers.AnythingBetween(@object.Collider, center, position, Constants.ShipOnlyMask, false);
}

__result = num;
}
}
23 changes: 20 additions & 3 deletions MiraAPI/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using HarmonyLib;
using MiraAPI.GameOptions;
using MiraAPI.Modifiers;
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;
Expand All @@ -17,6 +18,22 @@ namespace MiraAPI.Utilities;
/// </summary>
public static class Extensions
{
public static IEnumerable<Type> GetTypesSafe(this Assembly assembly)
{
try
{
return assembly.GetTypes();
}
catch (ReflectionTypeLoadException ex)
{
return ex.Types.Where(t => t != null);
}
catch
{
return Enumerable.Empty<Type>();
}
}

internal static NetData GetNetData(this ICustomRole role)
{
var count = role.GetCount();
Expand Down

0 comments on commit 3568948

Please sign in to comment.