Skip to content

Commit

Permalink
feat: [Exiled::API] StopDancing and StartDancing for SCP-3114 Role (#…
Browse files Browse the repository at this point in the history
…189)

* Added Dance

* Fix for DanceType.None

* Fixes

* Removed Debug message

* Fixed the difference

---------

Co-authored-by: Yamato <[email protected]>
Co-authored-by: VALERA771 <[email protected]>
  • Loading branch information
3 people authored Dec 30, 2024
1 parent ff302f9 commit 607e85d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 2 deletions.
23 changes: 22 additions & 1 deletion EXILED/Exiled.API/Features/Roles/Scp3114Role.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public float WarningTime
/// <summary>
/// Gets or sets the bound dance.
/// </summary>
internal DanceType DanceType { get; set; }
internal DanceType DanceType { get; set; } = DanceType.None;

/// <summary>
/// Updates the identity of SCP-3114.
Expand Down Expand Up @@ -250,5 +250,26 @@ public void PlaySound(Scp3114VoiceLines.VoiceLinesName voiceLine = Scp3114VoiceL
/// <param name="alreadySpawned">The List of Roles already spawned.</param>
/// <returns>The Spawn Chance.</returns>
public float GetSpawnChance(List<RoleTypeId> alreadySpawned) => Base is ISpawnableScp spawnableScp ? spawnableScp.GetSpawnChance(alreadySpawned) : 0;

/// <summary>
/// SCP-3114 starts dancing.
/// </summary>
/// <param name="danceType">The dance you want to do.</param>
public void StartDancing(DanceType danceType)
{
Dance.IsDancing = true;
DanceType = danceType;
Dance._serverStartPos = new RelativePositioning.RelativePosition(Dance.CastRole.FpcModule.Position);
Dance.ServerSendRpc(true);
}

/// <summary>
/// Stops the SCP-3114 from Dancing.
/// </summary>
public void StopDancing()
{
Dance.IsDancing = false;
Dance.ServerSendRpc(true);
}
}
}
72 changes: 72 additions & 0 deletions EXILED/Exiled.Events/Patches/Fixes/ServerSideDancesPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// -----------------------------------------------------------------------
// <copyright file="ServerSideDancesPatch.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.Fixes
{
#pragma warning disable SA1313
using System.Collections.Generic;
using System.Reflection.Emit;

using API.Features.Pools;
using Exiled.API.Features;
using HarmonyLib;
using Mirror;
using PlayerRoles.PlayableScps.Scp3114;
using PlayerRoles.Subroutines;
using UnityEngine;

using static HarmonyLib.AccessTools;

using Scp3114Role = API.Features.Roles.Scp3114Role;

/// <summary>
/// Patches the <see cref="Scp3114Dance.DanceVariant"/>.
/// Fix that the game doesn't write this.
/// </summary>
[HarmonyPatch(typeof(Scp3114Dance), nameof(Scp3114Dance.ServerWriteRpc))]
internal class ServerSideDancesPatch
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

Label skip = generator.DefineLabel();

newInstructions.Add(new CodeInstruction(OpCodes.Ret));
newInstructions[newInstructions.Count - 1].labels.Add(skip);

int offset = 4;
int index = newInstructions.FindIndex(x => x.Calls(Method(typeof(SubroutineBase), nameof(SubroutineBase.ServerWriteRpc)))) + offset;

newInstructions.InsertRange(index, new List<CodeInstruction>()
{
new CodeInstruction(OpCodes.Br_S, skip),
});

foreach (CodeInstruction instruction in newInstructions)
yield return instruction;

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}

private static void Postfix(ref Scp3114Dance __instance, NetworkWriter writer)
{
Player player = Player.Get(__instance.Owner);

Scp3114Role role = player.Role as Scp3114Role;

if (player != null && role.DanceType != API.Enums.DanceType.None)
{
writer.WriteByte((byte)role.DanceType);
return;
}

writer.WriteByte((byte)Random.Range(0, 255));
return;
}
}
}
1 change: 0 additions & 1 deletion EXILED/Exiled.Example/Commands/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace Exiled.Example.Commands
using System;

using CommandSystem;

using Exiled.API.Features;
using Exiled.API.Features.Pickups;

Expand Down

0 comments on commit 607e85d

Please sign in to comment.