Skip to content

Commit 229c6c7

Browse files
Rysik5318Misaka-ZeroTwolouis1706VALERA771
authored
Added Emotion Events and Player.Emotion (#257)
* SigmoEmotionPatchAdd * Oops * Oops * Fucking Exiled Dev CI / build * Update Player.cs * Update EXILED/Exiled.Events/Handlers/Player.cs Co-authored-by: Yamato <66829532+louis1706@users.noreply.github.com> * 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 <45165615+Misaka-ZeroTwo@users.noreply.github.com> Co-authored-by: Yamato <66829532+louis1706@users.noreply.github.com> Co-authored-by: VALERA771 <72030575+VALERA771@users.noreply.github.com>
1 parent 9633893 commit 229c6c7

5 files changed

Lines changed: 207 additions & 0 deletions

File tree

EXILED/Exiled.API/Features/Player.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ namespace Exiled.API.Features
4747
using Mirror.LiteNetLib4Mirror;
4848
using PlayerRoles;
4949
using PlayerRoles.FirstPersonControl;
50+
using PlayerRoles.FirstPersonControl.Thirdperson.Subcontrollers;
5051
using PlayerRoles.RoleAssign;
5152
using PlayerRoles.Spectating;
5253
using PlayerRoles.Voice;
@@ -732,6 +733,15 @@ public bool IsBypassModeEnabled
732733
set => ReferenceHub.serverRoles.BypassMode = value;
733734
}
734735

736+
/// <summary>
737+
/// Gets or sets the player's emotion.
738+
/// </summary>
739+
public EmotionPresetType Emotion
740+
{
741+
get => EmotionSync.GetEmotionPreset(ReferenceHub);
742+
set => EmotionSync.ServerSetEmotionPreset(ReferenceHub, value);
743+
}
744+
735745
/// <summary>
736746
/// Gets or sets a value indicating whether the player is muted.
737747
/// </summary>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// -----------------------------------------------------------------------
2+
// <copyright file="ChangedEmotionEventArgs.cs" company="ExMod Team">
3+
// Copyright (c) ExMod Team. All rights reserved.
4+
// Licensed under the CC BY-SA 3.0 license.
5+
// </copyright>
6+
// -----------------------------------------------------------------------
7+
8+
namespace Exiled.Events.EventArgs.Player
9+
{
10+
using Exiled.API.Features;
11+
using Exiled.Events.EventArgs.Interfaces;
12+
using PlayerRoles.FirstPersonControl.Thirdperson.Subcontrollers;
13+
14+
/// <summary>
15+
/// Contains all the information after the player's emotion.
16+
/// </summary>
17+
public class ChangedEmotionEventArgs : IPlayerEvent
18+
{
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="ChangedEmotionEventArgs"/> class.
21+
/// </summary>
22+
/// <param name="hub"><inheritdoc cref="Player"/></param>
23+
/// <param name="emotionPresetType"><inheritdoc cref="EmotionPresetType"/></param>
24+
public ChangedEmotionEventArgs(ReferenceHub hub, EmotionPresetType emotionPresetType)
25+
{
26+
Player = Player.Get(hub);
27+
EmotionPresetType = emotionPresetType;
28+
}
29+
30+
/// <summary>
31+
/// Gets the player's emotion.
32+
/// </summary>
33+
public EmotionPresetType EmotionPresetType { get; }
34+
35+
/// <inheritdoc/>
36+
public Player Player { get; }
37+
}
38+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// -----------------------------------------------------------------------
2+
// <copyright file="ChangingEmotionEventArgs.cs" company="ExMod Team">
3+
// Copyright (c) ExMod Team. All rights reserved.
4+
// Licensed under the CC BY-SA 3.0 license.
5+
// </copyright>
6+
// -----------------------------------------------------------------------
7+
8+
namespace Exiled.Events.EventArgs.Player
9+
{
10+
using Exiled.API.Features;
11+
using Exiled.Events.EventArgs.Interfaces;
12+
using PlayerRoles.FirstPersonControl.Thirdperson.Subcontrollers;
13+
14+
/// <summary>
15+
/// Contains all the information before the player's emotion changes.
16+
/// </summary>
17+
public class ChangingEmotionEventArgs : IDeniableEvent, IPlayerEvent
18+
{
19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="ChangingEmotionEventArgs"/> class.
21+
/// </summary>
22+
/// <param name="hub"><inheritdoc cref="Player"/></param>
23+
/// <param name="newEmotionPresetType"><inheritdoc cref="NewEmotionPresetType"/></param>
24+
/// <param name="oldEmotionPresetType"><inheritdoc cref="OldEmotionPresetType"/></param>
25+
/// <param name="isAllowed"><inheritdoc cref="IsAllowed"/></param>
26+
public ChangingEmotionEventArgs(ReferenceHub hub, EmotionPresetType newEmotionPresetType, EmotionPresetType oldEmotionPresetType, bool isAllowed = true)
27+
{
28+
Player = Player.Get(hub);
29+
NewEmotionPresetType = newEmotionPresetType;
30+
OldEmotionPresetType = oldEmotionPresetType;
31+
IsAllowed = isAllowed;
32+
}
33+
34+
/// <inheritdoc/>
35+
public bool IsAllowed { get; set; }
36+
37+
/// <summary>
38+
/// Gets the old player's emotion.
39+
/// </summary>
40+
public EmotionPresetType OldEmotionPresetType { get; }
41+
42+
/// <summary>
43+
/// Gets or sets the new player's emotion.
44+
/// </summary>
45+
public EmotionPresetType NewEmotionPresetType { get; set; }
46+
47+
/// <inheritdoc/>
48+
public Player Player { get; }
49+
}
50+
}

EXILED/Exiled.Events/Handlers/Player.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,28 @@ public class Player
549549
/// </summary>
550550
public static Event<ChangingNicknameEventArgs> ChangingNickname { get; set; } = new();
551551

552+
/// <summary>
553+
/// Invoked before a player's emotion changed.
554+
/// </summary>
555+
public static Event<ChangingEmotionEventArgs> ChangingEmotion { get; set; } = new();
556+
557+
/// <summary>
558+
/// Invoked after a player's emotion changed.
559+
/// </summary>
560+
public static Event<ChangedEmotionEventArgs> ChangedEmotion { get; set; } = new();
561+
562+
/// <summary>
563+
/// Called before a player's emotion changed.
564+
/// </summary>
565+
/// <param name="ev">The <see cref="ChangingEmotionEventArgs"/> instance.</param>
566+
public static void OnChangingEmotion(ChangingEmotionEventArgs ev) => ChangingEmotion.InvokeSafely(ev);
567+
568+
/// <summary>
569+
/// Called after a player's emotion changed.
570+
/// </summary>
571+
/// <param name="ev">The <see cref="ChangedEmotionEventArgs"/> instance.</param>
572+
public static void OnChangedEmotion(ChangedEmotionEventArgs ev) => ChangedEmotion.InvokeSafely(ev);
573+
552574
/// <summary>
553575
/// Called before reserved slot is resolved for a <see cref="API.Features.Player"/>.
554576
/// </summary>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// -----------------------------------------------------------------------
2+
// <copyright file="Emotion.cs" company="ExMod Team">
3+
// Copyright (c) ExMod Team. All rights reserved.
4+
// Licensed under the CC BY-SA 3.0 license.
5+
// </copyright>
6+
// -----------------------------------------------------------------------
7+
8+
namespace Exiled.Events.Patches.Events.Player
9+
{
10+
using System.Collections.Generic;
11+
using System.Reflection.Emit;
12+
13+
using Exiled.API.Features.Pools;
14+
using Exiled.Events.Attributes;
15+
using Exiled.Events.EventArgs.Player;
16+
using HarmonyLib;
17+
using PlayerRoles.FirstPersonControl.Thirdperson.Subcontrollers;
18+
19+
using static HarmonyLib.AccessTools;
20+
21+
/// <summary>
22+
/// Patches <see cref="EmotionSync.ServerSetEmotionPreset"/>.
23+
/// Adds the <see cref="Handlers.Player.ChangingEmotion" /> event and
24+
/// <see cref="Handlers.Player.ChangedEmotion" /> event.
25+
/// </summary>
26+
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.ChangingEmotion))]
27+
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.ChangedEmotion))]
28+
[HarmonyPatch(typeof(EmotionSync), nameof(EmotionSync.ServerSetEmotionPreset))]
29+
internal static class Emotion
30+
{
31+
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
32+
{
33+
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);
34+
35+
Label ret = generator.DefineLabel();
36+
LocalBuilder ev = generator.DeclareLocal(typeof(ChangingEmotionEventArgs));
37+
38+
int index = newInstructions.FindIndex(x => x.opcode == OpCodes.Ldsfld);
39+
40+
newInstructions.InsertRange(index, new CodeInstruction[]
41+
{
42+
// ChangingEmotionEventArgs ev = new(hub, preset, EmotionSync.GetEmotionPreset(hub), true);
43+
new CodeInstruction(OpCodes.Ldarg_0).MoveLabelsFrom(newInstructions[index]),
44+
new(OpCodes.Ldarg_1),
45+
new(OpCodes.Ldarg_0),
46+
new(OpCodes.Call, Method(typeof(EmotionSync), nameof(EmotionSync.GetEmotionPreset))),
47+
new(OpCodes.Ldc_I4_1),
48+
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ChangingEmotionEventArgs))[0]),
49+
new(OpCodes.Dup),
50+
new(OpCodes.Dup),
51+
new(OpCodes.Stloc_S, ev),
52+
53+
// Handlers.Player.OnChangingEmotion(ev);
54+
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnChangingEmotion))),
55+
56+
// if (!ev.IsAllowed)
57+
// return;
58+
new(OpCodes.Callvirt, PropertyGetter(typeof(ChangingEmotionEventArgs), nameof(ChangingEmotionEventArgs.IsAllowed))),
59+
new(OpCodes.Brfalse, ret),
60+
61+
// preset = ev.EmotionPresetTypeNew
62+
new(OpCodes.Ldloc_S, ev),
63+
new(OpCodes.Callvirt, PropertyGetter(typeof(ChangingEmotionEventArgs), nameof(ChangingEmotionEventArgs.NewEmotionPresetType))),
64+
new(OpCodes.Starg_S, 1),
65+
});
66+
67+
newInstructions.InsertRange(newInstructions.Count - 1, new CodeInstruction[]
68+
{
69+
// ChangedEmotionEventArgs ev = new(hub, EmotionSync.GetEmotionPreset(hub));
70+
new CodeInstruction(OpCodes.Ldarg_0),
71+
new(OpCodes.Ldarg_0),
72+
new(OpCodes.Call, Method(typeof(EmotionSync), nameof(EmotionSync.GetEmotionPreset))),
73+
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(ChangedEmotionEventArgs))[0]),
74+
75+
// Handlers.Player.OnChangedEmotion(ev);
76+
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnChangedEmotion))),
77+
});
78+
79+
newInstructions[newInstructions.Count - 1].labels.Add(ret);
80+
81+
for (int z = 0; z < newInstructions.Count; z++)
82+
yield return newInstructions[z];
83+
84+
ListPool<CodeInstruction>.Pool.Return(newInstructions);
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)