From 1ba25f5999ce5d37f179bc8d6a85b103054155c1 Mon Sep 17 00:00:00 2001 From: Yamato <66829532+louis1706@users.noreply.github.com> Date: Thu, 19 Oct 2023 20:09:05 +0200 Subject: [PATCH] StaminaUsage Patch now use Postfix instead of Transpiler & KickingEvent Transpiler (#2143) * StaminaUsage * Kicking Patch * Fix error * Fix for Experymental * Fix stupidity * Optimise --------- Co-authored-by: Nao <60253860+NaoUnderscore@users.noreply.github.com> Co-authored-by: Misaka-ZeroTwo <45165615+Misaka-ZeroTwo@users.noreply.github.com> --- .../EventArgs/Player/KickingEventArgs.cs | 12 ++- .../Patches/Events/Player/Kicking.cs | 85 ++++++++++++------- Exiled.Events/Patches/Generic/StaminaUsage.cs | 57 +------------ 3 files changed, 67 insertions(+), 87 deletions(-) diff --git a/Exiled.Events/EventArgs/Player/KickingEventArgs.cs b/Exiled.Events/EventArgs/Player/KickingEventArgs.cs index 12dd99ef64..6489761640 100644 --- a/Exiled.Events/EventArgs/Player/KickingEventArgs.cs +++ b/Exiled.Events/EventArgs/Player/KickingEventArgs.cs @@ -7,6 +7,8 @@ namespace Exiled.Events.EventArgs.Player { + using System; + using System.Linq; using System.Reflection; using API.Features; @@ -18,6 +20,7 @@ namespace Exiled.Events.EventArgs.Player /// public class KickingEventArgs : IPlayerEvent, IDeniableEvent { + private readonly string startkickmessage; private bool isAllowed; private Player issuer; private Player target; @@ -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; } @@ -75,7 +78,12 @@ public Player Target /// /// Gets or sets the full kick message. /// - 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; + } /// /// Gets or sets a value indicating whether or not action is taken against the target. diff --git a/Exiled.Events/Patches/Events/Player/Kicking.cs b/Exiled.Events/Patches/Events/Player/Kicking.cs index 156dee9562..95d1723150 100644 --- a/Exiled.Events/Patches/Events/Player/Kicking.cs +++ b/Exiled.Events/Patches/Events/Player/Kicking.cs @@ -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; /// /// Patches . @@ -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 Transpiler(IEnumerable instructions, ILGenerator generator) { - try + List newInstructions = ListPool.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.Pool.Return(newInstructions); } } } \ No newline at end of file diff --git a/Exiled.Events/Patches/Generic/StaminaUsage.cs b/Exiled.Events/Patches/Generic/StaminaUsage.cs index f45df94d1d..d03ec8a4a6 100644 --- a/Exiled.Events/Patches/Generic/StaminaUsage.cs +++ b/Exiled.Events/Patches/Generic/StaminaUsage.cs @@ -1,4 +1,4 @@ -// ----------------------------------------------------------------------- +// ----------------------------------------------------------------------- // // Copyright (c) Exiled Team. All rights reserved. // Licensed under the CC BY-SA 3.0 license. @@ -11,9 +11,7 @@ namespace Exiled.Events.Patches.Generic using Exiled.API.Features; using HarmonyLib; - using Mirror; using PlayerRoles.FirstPersonControl; - using UnityEngine; /// /// Patches . @@ -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; } } } \ No newline at end of file