Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Transmitting event #2273

Merged
merged 9 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
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;
BoltonDev marked this conversation as resolved.
Show resolved Hide resolved

/// <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
Loading