Skip to content

Commit

Permalink
Fix Transmitting event (#2273)
Browse files Browse the repository at this point in the history
* Fix TransmittingEvent

* :)

* Prevent breaking plugin

* :)

* Remove warning

* I'm dumb

* Fixing up XML Formatting

---------

Co-authored-by: Yamato <66829532+louis1706@users.noreply.github.com>
  • Loading branch information
BoltonDev and louis1706 authored Dec 18, 2023
1 parent a8f7cea commit 56d014c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 87 deletions.
16 changes: 10 additions & 6 deletions Exiled.Events/EventArgs/Player/TransmittingEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

namespace Exiled.Events.EventArgs.Player
{
using System;

using Exiled.API.Features;
using Exiled.Events.EventArgs.Interfaces;

using PlayerRoles.Voice;

using VoiceChat;

/// <summary>
/// Contains all information regarding the player using the radio.
/// </summary>
Expand All @@ -28,11 +28,14 @@ public class TransmittingEventArgs : IPlayerEvent, IDeniableEvent
/// <param name="voiceModule">
/// <inheritdoc cref="VoiceModule" />
/// </param>
public TransmittingEventArgs(Player player, VoiceModuleBase voiceModule)
/// <param name="isAllowed">
/// <inheritdoc cref="IsAllowed" />
/// </param>
public TransmittingEventArgs(Player player, VoiceModuleBase voiceModule, bool isAllowed = true)
{
Player = player;
VoiceModule = voiceModule;
IsTransmitting = voiceModule == null ? false : voiceModule.ServerIsSending && voiceModule.CurrentChannel == VoiceChatChannel.Radio;
IsAllowed = isAllowed;
}

/// <summary>
Expand All @@ -48,11 +51,12 @@ public TransmittingEventArgs(Player player, VoiceModuleBase voiceModule)
/// <summary>
/// Gets a value indicating whether or not the player is transmitting.
/// </summary>
public bool IsTransmitting { get; }
[Obsolete("IsTransmitting is always true.")]
public bool IsTransmitting => true;

/// <summary>
/// Gets or sets a value indicating whether or not the player can transmit.
/// </summary>
public bool IsAllowed { get; set; } = true;
public bool IsAllowed { get; set; }
}
}
80 changes: 0 additions & 80 deletions Exiled.Events/Patches/Events/Player/Transmitting.cs

This file was deleted.

41 changes: 40 additions & 1 deletion Exiled.Events/Patches/Events/Player/VoiceChatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ namespace Exiled.Events.Patches.Events.Player

using PlayerRoles.Voice;

using VoiceChat;
using VoiceChat.Networking;

using static HarmonyLib.AccessTools;

/// <summary>
/// patches <see cref="VoiceTransceiver.ServerReceiveMessage(NetworkConnection, VoiceMessage)"/> to add the <see cref="Handlers.Player.VoiceChatting"/> event.
/// Patches <see cref="VoiceTransceiver.ServerReceiveMessage(NetworkConnection, VoiceMessage)"/>.
/// Adds the <see cref="Handlers.Player.VoiceChatting"/> event.
/// Adds the <see cref="Handlers.Player.Transmitting"/> event.
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.VoiceChatting))]
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.Transmitting))]
[HarmonyPatch(typeof(VoiceTransceiver), nameof(VoiceTransceiver.ServerReceiveMessage))]
internal static class VoiceChatting
{
Expand All @@ -38,10 +42,12 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

Label retLabel = generator.DefineLabel();
Label isMutedLabel = generator.DefineLabel();
Label skipLabel = generator.DefineLabel();
List<Label> labels;

LocalBuilder ev = generator.DeclareLocal(typeof(VoiceChattingEventArgs));
LocalBuilder player = generator.DeclareLocal(typeof(API.Features.Player));
LocalBuilder voiceModule = generator.DeclareLocal(typeof(VoiceModuleBase));

const int offset = 3;
int index = newInstructions.FindIndex(i => i.Calls(Method(typeof(VoiceModuleBase), nameof(VoiceModuleBase.CheckRateLimit)))) + offset;
Expand Down Expand Up @@ -77,6 +83,8 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Callvirt, PropertyGetter(typeof(Role), nameof(Role.Base))),
new(OpCodes.Isinst, typeof(IVoiceRole)),
new(OpCodes.Callvirt, PropertyGetter(typeof(IVoiceRole), nameof(IVoiceRole.VoiceModule))),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, voiceModule.LocalIndex),

// true
new(OpCodes.Ldc_I4_1),
Expand All @@ -99,6 +107,37 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(VoiceChattingEventArgs), nameof(VoiceChattingEventArgs.VoiceMessage))),
new(OpCodes.Stloc_1),

// if(voiceModule.CurrentChannel != VoiceChatChannel.Radio)
// goto skipLabel;
new(OpCodes.Ldloc_S, voiceModule.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(VoiceModuleBase), nameof(VoiceModuleBase.CurrentChannel))),
new(OpCodes.Ldc_I4_S, (sbyte)VoiceChatChannel.Radio),
new(OpCodes.Ceq),
new(OpCodes.Brfalse_S, skipLabel),

// player
new(OpCodes.Ldloc_S, player.LocalIndex),

// voiceModule
new(OpCodes.Ldloc_S, voiceModule.LocalIndex),

// true
new(OpCodes.Ldc_I4_1),

// TransmittingEventArgs ev = new TransmittingEventArgs(Player, VoiceModuleBase, bool)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(TransmittingEventArgs))[0]),
new(OpCodes.Dup),

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

// if(!ev.IsAllowed)
// return;
new(OpCodes.Callvirt, PropertyGetter(typeof(TransmittingEventArgs), nameof(TransmittingEventArgs.IsAllowed))),
new(OpCodes.Brfalse_S, retLabel),

new CodeInstruction(OpCodes.Nop).WithLabels(skipLabel),
});

newInstructions[newInstructions.Count - 1].WithLabels(retLabel);
Expand Down

0 comments on commit 56d014c

Please sign in to comment.