Skip to content

Commit

Permalink
Added Emotion Events and Player.Emotion (#257)
Browse files Browse the repository at this point in the history
* SigmoEmotionPatchAdd

* Oops

* Oops

* Fucking Exiled Dev CI / build

* Update Player.cs

* Update EXILED/Exiled.Events/Handlers/Player.cs

Co-authored-by: Yamato <[email protected]>

* Update EXILED/Exiled.Events/Handlers/Player.cs

* Sigmoemotionadd (#11)

* Say hi to transpilers

* Update Emotion.cs

* Allows setting emotion

* missing dup

---------

Co-authored-by: Misaka-ZeroTwo <[email protected]>
Co-authored-by: Yamato <[email protected]>
Co-authored-by: VALERA771 <[email protected]>
  • Loading branch information
4 people authored Dec 7, 2024
1 parent 9633893 commit 229c6c7
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 0 deletions.
10 changes: 10 additions & 0 deletions EXILED/Exiled.API/Features/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace Exiled.API.Features
using Mirror.LiteNetLib4Mirror;
using PlayerRoles;
using PlayerRoles.FirstPersonControl;
using PlayerRoles.FirstPersonControl.Thirdperson.Subcontrollers;
using PlayerRoles.RoleAssign;
using PlayerRoles.Spectating;
using PlayerRoles.Voice;
Expand Down Expand Up @@ -732,6 +733,15 @@ public bool IsBypassModeEnabled
set => ReferenceHub.serverRoles.BypassMode = value;
}

/// <summary>
/// Gets or sets the player's emotion.
/// </summary>
public EmotionPresetType Emotion
{
get => EmotionSync.GetEmotionPreset(ReferenceHub);
set => EmotionSync.ServerSetEmotionPreset(ReferenceHub, value);
}

/// <summary>
/// Gets or sets a value indicating whether the player is muted.
/// </summary>
Expand Down
38 changes: 38 additions & 0 deletions EXILED/Exiled.Events/EventArgs/Player/ChangedEmotionEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// -----------------------------------------------------------------------
// <copyright file="ChangedEmotionEventArgs.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.Features;
using Exiled.Events.EventArgs.Interfaces;
using PlayerRoles.FirstPersonControl.Thirdperson.Subcontrollers;

/// <summary>
/// Contains all the information after the player's emotion.
/// </summary>
public class ChangedEmotionEventArgs : IPlayerEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="ChangedEmotionEventArgs"/> class.
/// </summary>
/// <param name="hub"><inheritdoc cref="Player"/></param>
/// <param name="emotionPresetType"><inheritdoc cref="EmotionPresetType"/></param>
public ChangedEmotionEventArgs(ReferenceHub hub, EmotionPresetType emotionPresetType)
{
Player = Player.Get(hub);
EmotionPresetType = emotionPresetType;
}

/// <summary>
/// Gets the player's emotion.
/// </summary>
public EmotionPresetType EmotionPresetType { get; }

/// <inheritdoc/>
public Player Player { get; }
}
}
50 changes: 50 additions & 0 deletions EXILED/Exiled.Events/EventArgs/Player/ChangingEmotionEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// -----------------------------------------------------------------------
// <copyright file="ChangingEmotionEventArgs.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.Features;
using Exiled.Events.EventArgs.Interfaces;
using PlayerRoles.FirstPersonControl.Thirdperson.Subcontrollers;

/// <summary>
/// Contains all the information before the player's emotion changes.
/// </summary>
public class ChangingEmotionEventArgs : IDeniableEvent, IPlayerEvent
{
/// <summary>
/// Initializes a new instance of the <see cref="ChangingEmotionEventArgs"/> class.
/// </summary>
/// <param name="hub"><inheritdoc cref="Player"/></param>
/// <param name="newEmotionPresetType"><inheritdoc cref="NewEmotionPresetType"/></param>
/// <param name="oldEmotionPresetType"><inheritdoc cref="OldEmotionPresetType"/></param>
/// <param name="isAllowed"><inheritdoc cref="IsAllowed"/></param>
public ChangingEmotionEventArgs(ReferenceHub hub, EmotionPresetType newEmotionPresetType, EmotionPresetType oldEmotionPresetType, bool isAllowed = true)
{
Player = Player.Get(hub);
NewEmotionPresetType = newEmotionPresetType;
OldEmotionPresetType = oldEmotionPresetType;
IsAllowed = isAllowed;
}

/// <inheritdoc/>
public bool IsAllowed { get; set; }

/// <summary>
/// Gets the old player's emotion.
/// </summary>
public EmotionPresetType OldEmotionPresetType { get; }

/// <summary>
/// Gets or sets the new player's emotion.
/// </summary>
public EmotionPresetType NewEmotionPresetType { get; set; }

/// <inheritdoc/>
public Player Player { get; }
}
}
22 changes: 22 additions & 0 deletions EXILED/Exiled.Events/Handlers/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,28 @@ public class Player
/// </summary>
public static Event<ChangingNicknameEventArgs> ChangingNickname { get; set; } = new();

/// <summary>
/// Invoked before a player's emotion changed.
/// </summary>
public static Event<ChangingEmotionEventArgs> ChangingEmotion { get; set; } = new();

/// <summary>
/// Invoked after a player's emotion changed.
/// </summary>
public static Event<ChangedEmotionEventArgs> ChangedEmotion { get; set; } = new();

/// <summary>
/// Called before a player's emotion changed.
/// </summary>
/// <param name="ev">The <see cref="ChangingEmotionEventArgs"/> instance.</param>
public static void OnChangingEmotion(ChangingEmotionEventArgs ev) => ChangingEmotion.InvokeSafely(ev);

/// <summary>
/// Called after a player's emotion changed.
/// </summary>
/// <param name="ev">The <see cref="ChangedEmotionEventArgs"/> instance.</param>
public static void OnChangedEmotion(ChangedEmotionEventArgs ev) => ChangedEmotion.InvokeSafely(ev);

/// <summary>
/// Called before reserved slot is resolved for a <see cref="API.Features.Player"/>.
/// </summary>
Expand Down
87 changes: 87 additions & 0 deletions EXILED/Exiled.Events/Patches/Events/Player/Emotion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// -----------------------------------------------------------------------
// <copyright file="Emotion.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.Player
{
using System.Collections.Generic;
using System.Reflection.Emit;

using Exiled.API.Features.Pools;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Player;
using HarmonyLib;
using PlayerRoles.FirstPersonControl.Thirdperson.Subcontrollers;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="EmotionSync.ServerSetEmotionPreset"/>.
/// Adds the <see cref="Handlers.Player.ChangingEmotion" /> event and
/// <see cref="Handlers.Player.ChangedEmotion" /> event.
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.ChangingEmotion))]
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.ChangedEmotion))]
[HarmonyPatch(typeof(EmotionSync), nameof(EmotionSync.ServerSetEmotionPreset))]
internal static class Emotion
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label ret = generator.DefineLabel();
LocalBuilder ev = generator.DeclareLocal(typeof(ChangingEmotionEventArgs));

int index = newInstructions.FindIndex(x => x.opcode == OpCodes.Ldsfld);

newInstructions.InsertRange(index, new CodeInstruction[]
{
// ChangingEmotionEventArgs ev = new(hub, preset, EmotionSync.GetEmotionPreset(hub), true);
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
new(OpCodes.Ldarg_1),
new(OpCodes.Ldarg_0),
new(OpCodes.Call, Method(typeof(EmotionSync), nameof(EmotionSync.GetEmotionPreset))),
new(OpCodes.Ldc_I4_1),
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ChangingEmotionEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev),

// Handlers.Player.OnChangingEmotion(ev);
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnChangingEmotion))),

// if (!ev.IsAllowed)
// return;
new(OpCodes.Callvirt, PropertyGetter(typeof(ChangingEmotionEventArgs), nameof(ChangingEmotionEventArgs.IsAllowed))),
new(OpCodes.Brfalse, ret),

// preset = ev.EmotionPresetTypeNew
new(OpCodes.Ldloc_S, ev),
new(OpCodes.Callvirt, PropertyGetter(typeof(ChangingEmotionEventArgs), nameof(ChangingEmotionEventArgs.NewEmotionPresetType))),
new(OpCodes.Starg_S, 1),
});

newInstructions.InsertRange(newInstructions.Count - 1, new CodeInstruction[]
{
// ChangedEmotionEventArgs ev = new(hub, EmotionSync.GetEmotionPreset(hub));
new CodeInstruction(OpCodes.Ldarg_0),
new(OpCodes.Ldarg_0),
new(OpCodes.Call, Method(typeof(EmotionSync), nameof(EmotionSync.GetEmotionPreset))),
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ChangedEmotionEventArgs))[0]),

// Handlers.Player.OnChangedEmotion(ev);
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnChangedEmotion))),
});

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 229c6c7

Please sign in to comment.