forked from Exiled-Official/EXILED
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[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
1 parent
9633893
commit 229c6c7
Showing
5 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
EXILED/Exiled.Events/EventArgs/Player/ChangedEmotionEventArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
50
EXILED/Exiled.Events/EventArgs/Player/ChangingEmotionEventArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |