Skip to content

Commit

Permalink
StaminaUsage Patch now use Postfix instead of Transpiler & KickingEve…
Browse files Browse the repository at this point in the history
…nt Transpiler (#2143)

* StaminaUsage

* Kicking Patch

* Fix error

* Fix for Experymental

* Fix stupidity

* Optimise

---------

Co-authored-by: Nao <[email protected]>
Co-authored-by: Misaka-ZeroTwo <[email protected]>
  • Loading branch information
3 people authored Oct 19, 2023
1 parent 7028a8b commit 1ba25f5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 87 deletions.
12 changes: 10 additions & 2 deletions Exiled.Events/EventArgs/Player/KickingEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

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

using API.Features;
Expand All @@ -18,6 +20,7 @@ namespace Exiled.Events.EventArgs.Player
/// </summary>
public class KickingEventArgs : IPlayerEvent, IDeniableEvent
{
private readonly string startkickmessage;
private bool isAllowed;
private Player issuer;
private Player target;
Expand Down Expand Up @@ -45,7 +48,7 @@ public KickingEventArgs(Player target, Player issuer, string reason, string full
Target = target;
Player = issuer ?? Server.Host;
Reason = reason;
FullMessage = fullMessage;
startkickmessage = fullMessage;
IsAllowed = isAllowed;
}

Expand Down Expand Up @@ -75,7 +78,12 @@ public Player Target
/// <summary>
/// Gets or sets the full kick message.
/// </summary>
public string FullMessage { get; set; }
public string FullMessage
{
get => startkickmessage + Reason;
[Obsolete("this will be remove use Reason instead of FullMessage")]
set => Reason = value.StartsWith(startkickmessage) ? value.Remove(0, startkickmessage.Count()) : value;
}

/// <summary>
/// Gets or sets a value indicating whether or not action is taken against the target.
Expand Down
85 changes: 53 additions & 32 deletions Exiled.Events/Patches/Events/Player/Kicking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@

namespace Exiled.Events.Patches.Events.Player
{
#pragma warning disable SA1313
using System;

using API.Features;
using System.Collections.Generic;
using System.Reflection.Emit;

using CommandSystem;
using Exiled.API.Features;
using Exiled.API.Features.Pools;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Player;

using HarmonyLib;
using PluginAPI.Events;

using Log = API.Features.Log;
using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="BanPlayer.KickUser(ReferenceHub, ICommandSender , string)" />.
Expand All @@ -29,41 +28,63 @@ namespace Exiled.Events.Patches.Events.Player
[HarmonyPatch(typeof(BanPlayer), nameof(BanPlayer.KickUser), typeof(ReferenceHub), typeof(ICommandSender), typeof(string))]
internal static class Kicking
{
private static bool Prefix(ReferenceHub target, ICommandSender issuer, string reason, ref bool __result)
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
try
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

LocalBuilder ev = generator.DeclareLocal(typeof(KickingEventArgs));

Label returnLabel = generator.DefineLabel();

newInstructions.InsertRange(0, new CodeInstruction[]
{
string message = $"You have been kicked. {(!string.IsNullOrEmpty(reason) ? "Reason: " + reason : string.Empty)}";
// Player.Get(ICommandSender);
new(OpCodes.Ldarg_1),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ICommandSender) })),

KickingEventArgs ev = new(Player.Get(target), Player.Get(issuer), reason, message);
// Player.Get(ReferenceHub);
new(OpCodes.Ldarg_0),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

Handlers.Player.OnKicking(ev);
// reason
new(OpCodes.Ldarg_3),

if (!ev.IsAllowed)
{
__result = false;
return false;
}
// kick start message
new(OpCodes.Ldstr, newInstructions.Find(x => x.opcode == OpCodes.Ldstr).operand),

reason = ev.Reason;
message = ev.FullMessage;
// true
new(OpCodes.Ldc_I4_1),

if (!EventManager.ExecuteEvent(new PlayerKickedEvent(target, issuer, reason)))
{
__result = false;
return false;
}
// KickingEventArgs ev = new(Player, Player, string, string, bool)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(KickingEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev.LocalIndex),

ServerConsole.Disconnect(target.gameObject, message);
// Player.OnKicking(ev);
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnKicking))),

__result = true;
return false;
}
catch (Exception exception)
{
Log.Error($"Exiled.Events.Patches.Events.Player.Kicking: {exception}\n{exception.StackTrace}");
return true;
}
// if (!ev.IsAllowed)
// return;
new(OpCodes.Callvirt, PropertyGetter(typeof(KickingEventArgs), nameof(KickingEventArgs.IsAllowed))),
new(OpCodes.Brfalse_S, returnLabel),

new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(BanningEventArgs), nameof(BanningEventArgs.Reason))),
new(OpCodes.Starg_S, 3),

new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(BanningEventArgs), nameof(BanningEventArgs.Target))),
new(OpCodes.Callvirt, PropertyGetter(typeof(Player), nameof(Player.ReferenceHub))),
new(OpCodes.Starg_S, 0),
});

newInstructions[newInstructions.FindIndex(x => x.opcode == OpCodes.Ret) - 1].WithLabels(returnLabel);

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
}
}
57 changes: 4 additions & 53 deletions Exiled.Events/Patches/Generic/StaminaUsage.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
// <copyright file="StaminaUsage.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
Expand All @@ -11,9 +11,7 @@ namespace Exiled.Events.Patches.Generic

using Exiled.API.Features;
using HarmonyLib;
using Mirror;
using PlayerRoles.FirstPersonControl;
using UnityEngine;

/// <summary>
/// Patches <see cref="FpcStateProcessor.UpdateMovementState"/>.
Expand All @@ -22,57 +20,10 @@ namespace Exiled.Events.Patches.Generic
[HarmonyPatch(typeof(FpcStateProcessor), nameof(FpcStateProcessor.UpdateMovementState))]
internal class StaminaUsage
{
private static bool Prefix(FpcStateProcessor __instance, PlayerMovementState state, ref PlayerMovementState __result)
private static void Postfix(FpcStateProcessor __instance, PlayerMovementState state, ref PlayerMovementState __result)
{
if (!Player.TryGet(__instance._hub, out Player player))
return true;

bool isCrouching = state == PlayerMovementState.Crouching;
float height = __instance._mod.CharacterControllerSettings.Height;
float num1 = height * __instance._mod.CrouchHeightRatio;

if (__instance.UpdateCrouching(isCrouching, num1, height) || __instance._firstUpdate)
{
__instance._firstUpdate = false;

float num2 = Mathf.Lerp(0.0f, (float)((height - (double)num1) / 2.0), __instance.CrouchPercent);
float num3 = Mathf.Lerp(height, num1, __instance.CrouchPercent);

float radius = __instance._mod.CharController.radius;

__instance._mod.CharController.height = num3;
__instance._mod.CharController.center = Vector3.down * num2;
__instance._camPivot.localPosition = Vector3.up * (float)((num3 / 2.0) - num2 - radius + 0.08799999952316284);
}

if (!NetworkServer.active || __instance._useRate == 0.0)
{
__result = state;
return false;
}

if (state == PlayerMovementState.Sprinting)
{
if (__instance._stat.CurValue > 0.0 && !__instance.SprintingDisabled)
{
__instance._stat.CurValue = !player.IsUsingStamina ? 1 : Mathf.Clamp01(__instance._stat.CurValue - (Time.deltaTime * __instance.ServerUseRate));
__instance._regenStopwatch.Restart();
__result = PlayerMovementState.Sprinting;
return false;
}

state = PlayerMovementState.Walking;
}

if (__instance._stat.CurValue >= 1.0)
{
__result = state;
return false;
}

__instance._stat.CurValue = !player.IsUsingStamina ? 1 : Mathf.Clamp01(__instance._stat.CurValue + (__instance.ServerRegenRate * Time.deltaTime));
__result = state;
return false;
if (Player.TryGet(__instance.Hub, out Player player) && !player.IsUsingStamina)
__instance._stat.CurValue = __instance._stat.MaxValue;
}
}
}

0 comments on commit 1ba25f5

Please sign in to comment.