Skip to content

Commit

Permalink
feat: PR 133 / Added Scp079 Signal Event (#400)
Browse files Browse the repository at this point in the history
Squashed PR 133:
  • Loading branch information
louis1706 authored Jan 19, 2025
1 parent 65ec461 commit 3333a5e
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 123 deletions.
44 changes: 44 additions & 0 deletions EXILED/Exiled.Events/EventArgs/Scp079/LosingSignalEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// -----------------------------------------------------------------------
// <copyright file="LosingSignalEventArgs.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.EventArgs.Scp079
{
using Exiled.API.Features;
using Exiled.Events.EventArgs.Interfaces;

/// <summary>
/// Contains all information before SCP-079 loses a signal by SCP-2176.
/// </summary>
public class LosingSignalEventArgs : IScp079Event, IDeniableEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="LosingSignalEventArgs" /> class.
/// </summary>
/// <param name="player">
/// <inheritdoc cref="Player" />
/// </param>
public LosingSignalEventArgs(ReferenceHub player)
{
Player = Player.Get(player);
Scp079 = Player.Role.As<API.Features.Roles.Scp079Role>();
IsAllowed = true;
}

/// <summary>
/// Gets the player who's controlling SCP-079.
/// </summary>
public Player Player { get; }

/// <summary>
/// Gets or sets a value indicating whether or not SCP-079 will lose his signal.
/// </summary>
public bool IsAllowed { get; set; }

/// <inheritdoc/>
public API.Features.Roles.Scp079Role Scp079 { get; }
}
}
38 changes: 38 additions & 0 deletions EXILED/Exiled.Events/EventArgs/Scp079/LostSignalEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// -----------------------------------------------------------------------
// <copyright file="LostSignalEventArgs.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.EventArgs.Scp079
{
using Exiled.API.Features;
using Exiled.Events.EventArgs.Interfaces;

/// <summary>
/// Contains all information before SCP-079 loses a signal by SCP-2176.
/// </summary>
public class LostSignalEventArgs : IScp079Event
{
/// <summary>
/// Initializes a new instance of the <see cref="LostSignalEventArgs" /> class.
/// </summary>
/// <param name="player">
/// <inheritdoc cref="Player" />
/// </param>
public LostSignalEventArgs(ReferenceHub player)
{
Player = Player.Get(player);
Scp079 = Player.Role.As<API.Features.Roles.Scp079Role>();
}

/// <summary>
/// Gets the player who's controlling SCP-079.
/// </summary>
public Player Player { get; }

/// <inheritdoc/>
public API.Features.Roles.Scp079Role Scp079 { get; }
}
}
66 changes: 0 additions & 66 deletions EXILED/Exiled.Events/EventArgs/Scp079/StartingSpeakerEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,66 +0,0 @@
// -----------------------------------------------------------------------
// <copyright file="StartingSpeakerEventArgs.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.EventArgs.Scp079
{
using Exiled.API.Features;
using Exiled.API.Features.Roles;
using Exiled.Events.EventArgs.Interfaces;

/// <summary>
/// Contains all information before SCP-079 uses a speaker.
/// </summary>
public class StartingSpeakerEventArgs : IScp079Event, IDeniableEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="StartingSpeakerEventArgs" /> class.
/// </summary>
/// <param name="player">
/// <inheritdoc cref="Player" />
/// </param>
/// <param name="room">
/// <inheritdoc cref="Room" />
/// </param>
/// <param name="auxiliaryPowerCost">
/// <inheritdoc cref="AuxiliaryPowerCost" />
/// </param>
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed" />
/// </param>
public StartingSpeakerEventArgs(Player player, Room room, float auxiliaryPowerCost, bool isAllowed = true)
{
Player = player;
Scp079 = player.Role.As<Scp079Role>();
Room = room;
AuxiliaryPowerCost = auxiliaryPowerCost;
IsAllowed = isAllowed;
}

/// <summary>
/// Gets the player who's controlling SCP-079.
/// </summary>
public Player Player { get; }

/// <inheritdoc/>
public Scp079Role Scp079 { get; }

/// <summary>
/// Gets the room that the speaker is located in.
/// </summary>
public Room Room { get; }

/// <summary>
/// Gets or sets the amount of auxiliary power required to use a speaker through SCP-079.
/// </summary>
public float AuxiliaryPowerCost { get; set; }

/// <summary>
/// Gets or sets a value indicating whether SCP-079 can use the speaker.
/// </summary>
public bool IsAllowed { get; set; }
}
}
57 changes: 0 additions & 57 deletions EXILED/Exiled.Events/EventArgs/Scp079/StoppingSpeakerEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,57 +0,0 @@
// -----------------------------------------------------------------------
// <copyright file="StoppingSpeakerEventArgs.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.EventArgs.Scp079
{
using Exiled.API.Features;
using Exiled.API.Features.Roles;
using Exiled.Events.EventArgs.Interfaces;

/// <summary>
/// Contains all information before SCP-079 finishes using a speaker.
/// </summary>
public class StoppingSpeakerEventArgs : IScp079Event, IDeniableEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="StoppingSpeakerEventArgs" /> class.
/// </summary>
/// <param name="player">
/// <inheritdoc cref="Player" />
/// </param>
/// <param name="room">
/// <inheritdoc cref="Room" />
/// </param>
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed" />
/// </param>
public StoppingSpeakerEventArgs(Player player, Room room, bool isAllowed = true)
{
Player = player;
Scp079 = player.Role.As<Scp079Role>();
Room = room;
IsAllowed = isAllowed;
}

/// <summary>
/// Gets the player who's controlling SCP-079.
/// </summary>
public Player Player { get; }

/// <inheritdoc/>
public Scp079Role Scp079 { get; }

/// <summary>
/// Gets the room that the speaker is located in.
/// </summary>
public Room Room { get; }

/// <summary>
/// Gets or sets a value indicating whether SCP-079 can stop using the speaker.
/// </summary>
public bool IsAllowed { get; set; }
}
}
22 changes: 22 additions & 0 deletions EXILED/Exiled.Events/Handlers/Scp079.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public static class Scp079
/// </summary>
public static Event<ZoneBlackoutEventArgs> ZoneBlackout { get; set; } = new();

/// <summary>
/// Invoked before SCP-079 loses a signal by SCP-2176.
/// </summary>
public static Event<LosingSignalEventArgs> LosingSignal { get; set; } = new();

/// <summary>
/// Invoked after SCP-079 loses a signal by SCP-2176.
/// </summary>
public static Event<LostSignalEventArgs> LostSignal { get; set; } = new();

/// <summary>
/// Called before SCP-079 switches cameras.
/// </summary>
Expand Down Expand Up @@ -159,5 +169,17 @@ public static class Scp079
/// </summary>
/// <param name="ev">The <see cref="PingingEventArgs" /> instance.</param>
public static void OnZoneBlackout(ZoneBlackoutEventArgs ev) => ZoneBlackout.InvokeSafely(ev);

/// <summary>
/// Called before SCP-079 loses a signal by SCP-2176.
/// </summary>
/// <param name="ev">The <see cref="LosingSignalEventArgs" /> instance.</param>
public static void OnLosingSignal(LosingSignalEventArgs ev) => LosingSignal.InvokeSafely(ev);

/// <summary>
/// Called after SCP-079 loses a signal by SCP-2176.
/// </summary>
/// <param name="ev">The <see cref="LostSignalEventArgs" /> instance.</param>
public static void OnLostSignal(LostSignalEventArgs ev) => LostSignal.InvokeSafely(ev);
}
}
86 changes: 86 additions & 0 deletions EXILED/Exiled.Events/Patches/Events/Scp079/Lost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// -----------------------------------------------------------------------
// <copyright file="Lost.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.Patches.Events.Scp079
{
using System.Collections.Generic;
using System.Reflection.Emit;

using API.Features.Pools;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Scp079;
using Exiled.Events.Handlers;

using HarmonyLib;

using PlayerRoles;
using PlayerRoles.PlayableScps.Scp079;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="Scp079LostSignalHandler.ServerLoseSignal(float)" />.
/// Adds the <see cref="Scp079.LosingSignal" /> and <see cref="Scp079.LostSignal"/> event.
/// </summary>
[EventPatch(typeof(Scp079), nameof(Scp079.LosingSignal))]
[EventPatch(typeof(Scp079), nameof(Scp079.LostSignal))]
[HarmonyPatch(typeof(Scp079LostSignalHandler), nameof(Scp079LostSignalHandler.ServerLoseSignal))]
internal static class Lost
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label ret = generator.DefineLabel();

newInstructions.InsertRange(
0,
new[]
{
// Role._lastOwner
new CodeInstruction(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp079LostSignalHandler), nameof(Scp079LostSignalHandler.Role))),
new(OpCodes.Ldfld, Field(typeof(PlayerRoleBase), nameof(PlayerRoleBase._lastOwner))),

// LosingSignalEventArgs ev = new(Role._lastOwner)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(LosingSignalEventArgs))[0]),
new(OpCodes.Dup),

// Scp079.OnLosingSignal(ev)
new(OpCodes.Call, Method(typeof(Scp079), nameof(Scp079.OnLosingSignal))),

// if (!ev.IsAllowed)
// return;
new(OpCodes.Callvirt, PropertyGetter(typeof(LosingSignalEventArgs), nameof(LosingSignalEventArgs.IsAllowed))),
new(OpCodes.Brfalse_S, ret),
});

newInstructions.InsertRange(
newInstructions.Count - 1,
new[]
{
// Role._lastOwner
new CodeInstruction(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(Scp079LostSignalHandler), nameof(Scp079LostSignalHandler.Role))),
new(OpCodes.Ldfld, Field(typeof(PlayerRoleBase), nameof(PlayerRoleBase._lastOwner))),

// LostSignalEventArgs ev = new(Role._lastOwner)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(LostSignalEventArgs))[0]),

// Scp079.OnLosingSignal(ev)
new(OpCodes.Call, Method(typeof(Scp079), nameof(Scp079.OnLostSignal))),
});

newInstructions[newInstructions.Count - 1].labels.Add(ret);

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
}
}

0 comments on commit 3333a5e

Please sign in to comment.