Skip to content

Commit

Permalink
[Exiled::API] Adding new properties for FpcRole (#65)
Browse files Browse the repository at this point in the history
* 👉👈

* suggestions

* no more bc

---------

Co-authored-by: Vladislav Popovič <vladislavcs@proton.me>
  • Loading branch information
VALERA771 and VladTheCow authored Sep 2, 2024
1 parent 96ccf32 commit c9a1b9a
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 23 deletions.
13 changes: 7 additions & 6 deletions EXILED/Exiled.API/Features/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,13 @@ private set
/// <summary>
/// Gets the <see cref="ReferenceHub"/>'s <see cref="VoiceModule"/>, can be null.
/// </summary>
public VoiceModuleBase VoiceModule => RoleManager.CurrentRole is IVoiceRole voiceRole ? voiceRole.VoiceModule : null;
[Obsolete("Use IVoiceRole::VoiceModule instead.")]
public VoiceModuleBase VoiceModule => Role is Roles.IVoiceRole voiceRole ? voiceRole.VoiceModule : null;

/// <summary>
/// Gets the <see cref="ReferenceHub"/>'s <see cref="PersonalRadioPlayback"/>, can be null.
/// </summary>
public PersonalRadioPlayback RadioPlayback => VoiceModule is IRadioVoiceModule radioVoiceModule ? radioVoiceModule.RadioPlayback : null;
public PersonalRadioPlayback RadioPlayback => Role is Roles.IVoiceRole voiceRole ? voiceRole.VoiceModule is IRadioVoiceModule radioVoiceModule ? radioVoiceModule.RadioPlayback : null : null;

/// <summary>
/// Gets the <see cref="Hints.HintDisplay"/> of the player.
Expand Down Expand Up @@ -793,7 +794,7 @@ public bool IsIntercomMuted
/// <summary>
/// Gets a value indicating whether or not the player is speaking.
/// </summary>
public bool IsSpeaking => VoiceModule != null && VoiceModule.IsSpeaking;
public bool IsSpeaking => Role is Roles.IVoiceRole voiceRole && voiceRole.VoiceModule.IsSpeaking;

/// <summary>
/// Gets the player's voice color.
Expand All @@ -805,13 +806,13 @@ public bool IsIntercomMuted
/// </summary>
public VoiceChatChannel VoiceChannel
{
get => VoiceModule == null ? VoiceChatChannel.None : VoiceModule.CurrentChannel;
get => Role is Roles.IVoiceRole voiceRole ? voiceRole.VoiceModule.CurrentChannel : VoiceChatChannel.None;
set
{
if (VoiceModule == null)
if (Role is not Roles.IVoiceRole voiceRole)
return;

VoiceModule.CurrentChannel = value;
voiceRole.VoiceModule.CurrentChannel = value;
}
}

Expand Down
33 changes: 29 additions & 4 deletions EXILED/Exiled.API/Features/Roles/FpcRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ namespace Exiled.API.Features.Roles
using System.Reflection;

using Exiled.API.Features.Pools;

using HarmonyLib;
using PlayerRoles;
using PlayerRoles.FirstPersonControl;

using PlayerRoles.Ragdolls;
using PlayerRoles.Spectating;
using PlayerRoles.Visibility;
using PlayerRoles.Voice;
using PlayerStatsSystem;
using RelativePositioning;

using UnityEngine;

/// <summary>
/// Defines a role that represents an fpc class.
/// </summary>
public abstract class FpcRole : Role
public abstract class FpcRole : Role, IVoiceRole
{
private static FieldInfo enableFallDamageField;
private bool isUsingStamina = true;
Expand Down Expand Up @@ -243,6 +244,30 @@ public bool IsNoclipEnabled
set => Owner.ReferenceHub.playerStats.GetModule<AdminFlagsStat>().SetFlag(AdminFlags.Noclip, value);
}

/// <summary>
/// Gets or sets a prefab ragdoll for this role.
/// </summary>
public BasicRagdoll Ragdoll
{
get => FirstPersonController.Ragdoll;
set => FirstPersonController.Ragdoll = value;
}

/// <summary>
/// Gets a voice module for this role.
/// </summary>
public VoiceModuleBase VoiceModule => FirstPersonController.VoiceModule;

/// <summary>
/// Gets a <see cref="VisibilityController"/> for this role.
/// </summary>
public VisibilityController VisibilityController => FirstPersonController.VisibilityController;

/// <summary>
/// Gets a <see cref="SpectatableModuleBase"/> for this role.
/// </summary>
public SpectatableModuleBase SpectatableModuleBase => FirstPersonController.SpectatorModule;

/// <summary>
/// Resets the <see cref="Player"/>'s stamina.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions EXILED/Exiled.API/Features/Roles/IVoiceRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// -----------------------------------------------------------------------
// <copyright file="IVoiceRole.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.API.Features.Roles
{
using PlayerRoles.Voice;

/// <summary>
/// Interface for all roles with <see cref="VoiceModuleBase"/>.
/// </summary>
public interface IVoiceRole
{
/// <summary>
/// Gets the <see cref="VoiceModuleBase"/> of the role.
/// </summary>
public VoiceModuleBase VoiceModule { get; }
}
}
6 changes: 5 additions & 1 deletion EXILED/Exiled.API/Features/Roles/NoneRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
namespace Exiled.API.Features.Roles
{
using PlayerRoles;
using PlayerRoles.Voice;

using NoneGameRole = PlayerRoles.NoneRole;

/// <summary>
/// Defines a role that represents players with no role.
/// </summary>
public class NoneRole : Role
public class NoneRole : Role, IVoiceRole
{
/// <summary>
/// Initializes a new instance of the <see cref="NoneRole"/> class.
Expand All @@ -27,5 +28,8 @@ internal NoneRole(PlayerRoleBase baseRole)

/// <inheritdoc/>
public override RoleTypeId Type { get; } = RoleTypeId.None;

/// <inheritdoc/>
public VoiceModuleBase VoiceModule => (Base as NoneGameRole) !.VoiceModule;
}
}
2 changes: 1 addition & 1 deletion EXILED/Exiled.API/Features/Roles/Scp0492Role.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public float SimulatedStare
/// <summary>
/// Gets the <see cref="Ragdoll"/> that SCP-049-2 is currently consuming. Will be <see langword="null"/> if <see cref="IsConsuming"/> is <see langword="false"/>.
/// </summary>
public Ragdoll RagdollConsuming => Ragdoll.Get(ConsumeAbility.CurRagdoll);
public Ragdoll RagdollConsuming => Features.Ragdoll.Get(ConsumeAbility.CurRagdoll);

/// <summary>
/// Gets the amount of time in between SCP-049-2 attacks.
Expand Down
4 changes: 2 additions & 2 deletions EXILED/Exiled.API/Features/Roles/Scp049Role.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ internal Scp049Role(Scp049GameRole baseRole)
/// <summary>
/// Gets the ragdoll that is currently being revived by SCP-049. Will be <see langword="null"/> if <see cref="IsRecalling"/> is <see langword="false"/>.
/// </summary>
public Ragdoll RecallingRagdoll => Ragdoll.Get(ResurrectAbility.CurRagdoll);
public Ragdoll RecallingRagdoll => Features.Ragdoll.Get(ResurrectAbility.CurRagdoll);

/// <summary>
/// Gets all the dead zombies.
Expand Down Expand Up @@ -255,7 +255,7 @@ public bool Resurrect(Player player)
HumeShieldModuleBase humeShield = ResurrectAbility.CastRole.HumeShieldModule;
humeShield.HsCurrent = Mathf.Min(humeShield.HsCurrent + 100f, humeShield.HsMax);

return Resurrect(Ragdoll.GetLast(player));
return Resurrect(Features.Ragdoll.GetLast(player));
}

/// <summary>
Expand Down
6 changes: 5 additions & 1 deletion EXILED/Exiled.API/Features/Roles/Scp079Role.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace Exiled.API.Features.Roles
using PlayerRoles.PlayableScps.Scp079.Pinging;
using PlayerRoles.PlayableScps.Scp079.Rewards;
using PlayerRoles.Subroutines;
using PlayerRoles.Voice;
using RelativePositioning;
using Utils.NonAllocLINQ;

Expand All @@ -32,7 +33,7 @@ namespace Exiled.API.Features.Roles
/// <summary>
/// Defines a role that represents SCP-079.
/// </summary>
public class Scp079Role : Role, ISubroutinedScpRole, ISpawnableScp
public class Scp079Role : Role, ISubroutinedScpRole, ISpawnableScp, IVoiceRole
{
/// <summary>
/// Initializes a new instance of the <see cref="Scp079Role"/> class.
Expand Down Expand Up @@ -374,6 +375,9 @@ public float Scp2176LostTime
/// </summary>
public float EnergyRegenerationSpeed => AuxManager.RegenSpeed;

/// <inheritdoc/>
public VoiceModuleBase VoiceModule => Base.VoiceModule;

/// <summary>
/// Gets the game <see cref="Scp079GameRole"/>.
/// </summary>
Expand Down
17 changes: 14 additions & 3 deletions EXILED/Exiled.API/Features/Roles/Scp3114Role.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Exiled.API.Features.Roles
{
using System;
using System.Collections.Generic;

using Exiled.API.Enums;
Expand Down Expand Up @@ -149,18 +150,28 @@ public RoleTypeId StolenRole
get => Identity.CurIdentity.StolenRole;
set
{
if (Ragdoll is null)
if (IdentityRagdoll is null)
return;

Ragdoll.Role = value;
IdentityRagdoll.Role = value;
UpdateIdentity();
}
}

/// <summary>
/// Gets or sets the SCP-3114's Ragdoll used for it's FakeIdentity.
/// </summary>
public Ragdoll Ragdoll
[Obsolete("Ragdoll in Role now has other meaning. Use IdentityRagdoll instead.")]
public new Ragdoll Ragdoll
{
get => IdentityRagdoll;
set => IdentityRagdoll = value;
}

/// <summary>
/// Gets or sets the SCP-3114's Ragdoll used for it's FakeIdentity.
/// </summary>
public Ragdoll IdentityRagdoll
{
get => Ragdoll.Get(Identity.CurIdentity.Ragdoll);
set
Expand Down
7 changes: 5 additions & 2 deletions EXILED/Exiled.API/Features/Roles/SpectatorRole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ namespace Exiled.API.Features.Roles
using System;

using PlayerRoles;

using PlayerRoles.Voice;
using UnityEngine;

using SpectatorGameRole = PlayerRoles.Spectating.SpectatorRole;

/// <summary>
/// Defines a role that represents a spectator.
/// </summary>
public class SpectatorRole : Role
public class SpectatorRole : Role, IVoiceRole
{
/// <summary>
/// Initializes a new instance of the <see cref="SpectatorRole"/> class.
Expand Down Expand Up @@ -70,5 +70,8 @@ public Player SpectatedPlayer
/// Gets the game <see cref="SpectatorGameRole"/>.
/// </summary>
public new SpectatorGameRole Base { get; }

/// <inheritdoc/>
public VoiceModuleBase VoiceModule => Base.VoiceModule;
}
}
5 changes: 2 additions & 3 deletions EXILED/Exiled.Events/Patches/Events/Player/VoiceChatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
// voiceModule.
new(OpCodes.Ldloc_S, player.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(API.Features.Player), nameof(API.Features.Player.Role))),
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.Isinst, typeof(API.Features.Roles.IVoiceRole)),
new(OpCodes.Callvirt, PropertyGetter(typeof(API.Features.Roles.IVoiceRole), nameof(API.Features.Roles.IVoiceRole.VoiceModule))),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, voiceModule.LocalIndex),

Expand Down

0 comments on commit c9a1b9a

Please sign in to comment.