Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Exiled.Events.EventArgs.Player
{
using Exiled.API.Features;
using Exiled.Events.EventArgs.Interfaces;

using MapGeneration;

/// <summary>
Expand All @@ -19,7 +20,7 @@ public class RoomChangedEventArgs : IPlayerEvent
/// Initializes a new instance of the <see cref="RoomChangedEventArgs"/> class.
/// </summary>
/// <param name="player">The player whose room has changed.</param>
/// <param name="oldRoom">The room identifier before the change (Can be null on round start).</param>
/// <param name="oldRoom">The room identifier before the change.</param>
/// <param name="newRoom">The room identifier after the change.</param>
public RoomChangedEventArgs(ReferenceHub player, RoomIdentifier oldRoom, RoomIdentifier newRoom)
{
Expand Down
59 changes: 59 additions & 0 deletions EXILED/Exiled.Events/EventArgs/Player/ZoneChangedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// -----------------------------------------------------------------------
// <copyright file="ZoneChangedEventArgs.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.Player
{
using Exiled.API.Enums;
using Exiled.API.Features;
using Exiled.Events.EventArgs.Interfaces;

/// <summary>
/// Contains the information when a player changes zones.
/// </summary>
public class ZoneChangedEventArgs : IPlayerEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="ZoneChangedEventArgs"/> class.
/// </summary>
/// <param name="player">The player whose zone has changed.</param>
/// <param name="oldRoom">The previous room the player was in.</param>
/// <param name="newRoom">The new room the player entered.</param>
/// <param name="oldZone">The previous zone the player was in.</param>
/// <param name="newZone">The new zone the player entered.</param>
public ZoneChangedEventArgs(Player player, Room oldRoom, Room newRoom, ZoneType oldZone, ZoneType newZone)
{
Player = player;
OldRoom = oldRoom;
NewRoom = newRoom;
OldZone = oldZone;
NewZone = newZone;
}

/// <inheritdoc/>
public Player Player { get; }

/// <summary>
/// Gets the previous zone the player was in.
/// </summary>
public ZoneType OldZone { get; }

/// <summary>
/// Gets the new zone the player entered.
/// </summary>
public ZoneType NewZone { get; }

/// <summary>
/// Gets the previous room the player was in.
/// </summary>
public Room OldRoom { get; }

/// <summary>
/// Gets the new room the player entered.
/// </summary>
public Room NewRoom { get; }
}
}
15 changes: 9 additions & 6 deletions EXILED/Exiled.Events/Features/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ private record AsyncRegistration(CustomAsyncEventHandler handler, int priority);

private readonly List<AsyncRegistration> innerAsyncEvent = new();

private bool patched;

/// <summary>
/// Initializes a new instance of the <see cref="Event"/> class.
/// </summary>
Expand All @@ -60,6 +58,11 @@ public Event()
/// </summary>
public static IReadOnlyList<Event> List => EventsValue;

/// <summary>
/// Gets a value indicating whether the Harmony patch for this event has been applied.
/// </summary>
public bool Patched { get; private set; } = !Events.Instance.Config.UseDynamicPatching;

/// <summary>
/// Subscribes a <see cref="CustomEventHandler"/> to the inner event, and checks patches if dynamic patching is enabled.
/// </summary>
Expand Down Expand Up @@ -124,10 +127,10 @@ public void Subscribe(CustomEventHandler handler, int priority)
{
Log.Assert(Events.Instance is not null, $"{nameof(Events.Instance)} is null, please ensure you have exiled_events enabled!");

if (Events.Instance.Config.UseDynamicPatching && !patched)
if (Events.Instance.Config.UseDynamicPatching && !Patched)
{
Events.Instance.Patcher.Patch(this);
patched = true;
Patched = true;
}

if (handler == null)
Expand Down Expand Up @@ -163,10 +166,10 @@ public void Subscribe(CustomAsyncEventHandler handler, int priority)
{
Log.Assert(Events.Instance is not null, $"{nameof(Events.Instance)} is null, please ensure you have exiled_events enabled!");

if (Events.Instance.Config.UseDynamicPatching && !patched)
if (Events.Instance.Config.UseDynamicPatching && !Patched)
{
Events.Instance.Patcher.Patch(this);
patched = true;
Patched = true;
}

if (handler == null)
Expand Down
15 changes: 9 additions & 6 deletions EXILED/Exiled.Events/Features/Event{T}.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ private record AsyncRegistration(CustomAsyncEventHandler<T> handler, int priorit

private readonly List<AsyncRegistration> innerAsyncEvent = new();

private bool patched;

/// <summary>
/// Initializes a new instance of the <see cref="Event{T}"/> class.
/// </summary>
Expand All @@ -65,6 +63,11 @@ public Event()
/// </summary>
public static IReadOnlyDictionary<Type, Event<T>> Dictionary => TypeToEvent;

/// <summary>
/// Gets a value indicating whether the Harmony patch for this event has been applied.
/// </summary>
public bool Patched { get; private set; } = !Events.Instance.Config.UseDynamicPatching;

/// <summary>
/// Subscribes a target <see cref="CustomEventHandler{TEventArgs}"/> to the inner event and checks if patching is possible, if dynamic patching is enabled.
/// </summary>
Expand Down Expand Up @@ -129,10 +132,10 @@ public void Subscribe(CustomEventHandler<T> handler, int priority)
{
Log.Assert(Events.Instance is not null, $"{nameof(Events.Instance)} is null, please ensure you have exiled_events enabled!");

if (Events.Instance.Config.UseDynamicPatching && !patched)
if (Events.Instance.Config.UseDynamicPatching && !Patched)
{
Events.Instance.Patcher.Patch(this);
patched = true;
Patched = true;
}

if (handler == null)
Expand Down Expand Up @@ -168,10 +171,10 @@ public void Subscribe(CustomAsyncEventHandler<T> handler, int priority)
{
Log.Assert(Events.Instance is not null, $"{nameof(Events.Instance)} is null, please ensure you have exiled_events enabled!");

if (Events.Instance.Config.UseDynamicPatching && !patched)
if (Events.Instance.Config.UseDynamicPatching && !Patched)
{
Events.Instance.Patcher.Patch(this);
patched = true;
Patched = true;
}

if (handler == null)
Expand Down
29 changes: 28 additions & 1 deletion EXILED/Exiled.Events/Handlers/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ namespace Exiled.Events.Handlers
{
using System;

using Exiled.API.Enums;
using Exiled.API.Features;

#pragma warning disable IDE0079
#pragma warning disable IDE0060
#pragma warning disable SA1623 // Property summary documentation should match accessors

using Exiled.Events.EventArgs.Player;

using Exiled.Events.Features;

using LabApi.Events.Arguments.PlayerEvents;

/// <summary>
Expand Down Expand Up @@ -505,6 +509,11 @@ public class Player
/// </summary>
public static Event<RoomChangedEventArgs> RoomChanged { get; set; } = new();

/// <summary>
/// Invoked when a <see cref="API.Features.Player"/> changes zones.
/// </summary>
public static Event<ZoneChangedEventArgs> ZoneChanged { get; set; } = new();

/// <summary>
/// Invoked before a <see cref="API.Features.Player"/> toggles the NoClip mode.
/// </summary>
Expand Down Expand Up @@ -835,7 +844,25 @@ public class Player
/// Called when a <see cref="API.Features.Player"/> changes rooms.
/// </summary>
/// <param name="ev">The <see cref="RoomChangedEventArgs"/> instance.</param>
public static void OnRoomChanged(RoomChangedEventArgs ev) => RoomChanged.InvokeSafely(ev);
public static void OnRoomChanged(RoomChangedEventArgs ev)
{
RoomChanged.InvokeSafely(ev);

if (!ZoneChanged.Patched)
return;

ZoneType oldZone = ev.OldRoom?.Zone ?? ZoneType.Unspecified;
ZoneType newZone = ev.NewRoom?.Zone ?? ZoneType.Unspecified;

if (oldZone != newZone)
OnZoneChanged(new ZoneChangedEventArgs(ev.Player, ev.OldRoom, ev.NewRoom, oldZone, newZone));
}

/// <summary>
/// Called when a <see cref="API.Features.Player"/> changes zones.
/// </summary>
/// <param name="ev">The <see cref="ZoneChangedEventArgs"/> instance.</param>
public static void OnZoneChanged(ZoneChangedEventArgs ev) => ZoneChanged.InvokeSafely(ev);

/// <summary>
/// Called before a <see cref="API.Features.Player"/> escapes.
Expand Down
3 changes: 2 additions & 1 deletion EXILED/Exiled.Events/Patches/Events/Player/ChangedRoom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace Exiled.Events.Patches.Events.Player
/// Patches <see cref="CurrentRoomPlayerCache.ValidateCache"/> to add the <see cref="Player.RoomChanged"/> event.
/// </summary>
[EventPatch(typeof(Player), nameof(Player.RoomChanged))]
[EventPatch(typeof(Player), nameof(Player.ZoneChanged))]
[HarmonyPatch(typeof(CurrentRoomPlayerCache), nameof(CurrentRoomPlayerCache.ValidateCache))]
internal class ChangedRoom
{
Expand All @@ -40,7 +41,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

newInstructions.InsertRange(index, new CodeInstruction[]
{
// referenceHub
// hub
new(OpCodes.Ldarg_0),
new(OpCodes.Ldfld, Field(typeof(CurrentRoomPlayerCache), nameof(CurrentRoomPlayerCache._roleManager))),
new(OpCodes.Callvirt, PropertyGetter(typeof(PlayerRoleManager), nameof(PlayerRoleManager.Hub))),
Expand Down
Loading