From 1ce643c783471fbcd3311b58e624c337f8215366 Mon Sep 17 00:00:00 2001 From: JJ Date: Thu, 3 Oct 2024 02:40:21 -0500 Subject: [PATCH] very minor cleanup --- .../RPCServer_Attribute_Method_Patch.cs | 108 +++++++++--------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/src/RustServerMetrics/HarmonyPatches/Delayed/RPCServer_Attribute_Method_Patch.cs b/src/RustServerMetrics/HarmonyPatches/Delayed/RPCServer_Attribute_Method_Patch.cs index 486bcbe..0b65012 100644 --- a/src/RustServerMetrics/HarmonyPatches/Delayed/RPCServer_Attribute_Method_Patch.cs +++ b/src/RustServerMetrics/HarmonyPatches/Delayed/RPCServer_Attribute_Method_Patch.cs @@ -7,76 +7,76 @@ using System.Reflection.Emit; using UnityEngine; -namespace RustServerMetrics.HarmonyPatches.Delayed +namespace RustServerMetrics.HarmonyPatches.Delayed; + +[DelayedHarmonyPatch] +[HarmonyPatch] +internal class RPCServer_Attribute_Method_Patch { - [DelayedHarmonyPatch] - [HarmonyPatch] - internal class RPCServer_Attribute_Method_Patch + [HarmonyPrepare] + public static bool Prepare() { - [HarmonyPrepare] - public static bool Prepare() + if (RustServerMetricsLoader.__serverStarted) { - if (!RustServerMetricsLoader.__serverStarted) - { - Debug.Log("Note: Cannot patch RPCServer_Attribute_Method_Patch yet. We will patch it upon server start."); - return false; - } - return true; } + + Debug.Log("Note: Cannot patch RPCServer_Attribute_Method_Patch yet. We will patch it upon server start."); + return false; + } - [HarmonyTargetMethods] - public static IEnumerable TargetMethods(Harmony harmonyInstance) - { - var baseNetworkableType = typeof(BaseNetworkable); - var baseNetworkableAssembly = baseNetworkableType.Assembly; - Stack typesToScan = new Stack(baseNetworkableAssembly.GetTypes()); + [HarmonyTargetMethods] + public static IEnumerable TargetMethods(Harmony harmonyInstance) + { + var baseNetworkableType = typeof(BaseNetworkable); + var baseNetworkableAssembly = baseNetworkableType.Assembly; + var typesToScan = new Stack(baseNetworkableAssembly.GetTypes()); - while (typesToScan.TryPop(out Type type)) + while (typesToScan.TryPop(out var type)) + { + foreach (var subType in type.GetNestedTypes()) { - var subTypes = type.GetNestedTypes(); - for (int i = 0; i < subTypes.Length; i++) - typesToScan.Push(subTypes[i]); - - var methods = type.GetMethods(); - for (int i = 0; i < methods.Length; i++) + typesToScan.Push(subType); + } + + foreach (var method in type.GetMethods()) + { + if (method.DeclaringType == method.ReflectedType && method.GetCustomAttribute() != null) { - var method = methods[i]; - if (method.DeclaringType == method.ReflectedType && method.GetCustomAttribute() != null) - { - yield return method; - } + yield return method; } } - } + } + } - [HarmonyTranspiler] - public static IEnumerable Transpile(IEnumerable originalInstructions, MethodBase methodBase, ILGenerator ilGenerator) - { - List ret = originalInstructions.ToList(); - LocalBuilder local = ilGenerator.DeclareLocal(typeof(DateTime)); + [HarmonyTranspiler] + public static IEnumerable Transpile(IEnumerable originalInstructions, MethodBase methodBase, ILGenerator ilGenerator) + { + var ret = originalInstructions.ToList(); + var local = ilGenerator.DeclareLocal(typeof(DateTime)); - ret.InsertRange(0, new CodeInstruction [] - { - new (OpCodes.Call, AccessTools.Property(typeof(DateTime), nameof(DateTime.UtcNow)).GetMethod), - new (OpCodes.Stloc, local) - }); + ret.InsertRange(0, new CodeInstruction [] + { + new (OpCodes.Call, AccessTools.Property(typeof(DateTime), nameof(DateTime.UtcNow)).GetMethod), + new (OpCodes.Stloc, local) + }); - return Helpers.Postfix( - ret, - CustomPostfix, - new CodeInstruction(OpCodes.Ldstr, $"{methodBase.DeclaringType?.Name}.{methodBase.Name}"), - new CodeInstruction(OpCodes.Ldloc, local)); - } + return Helpers.Postfix( + ret, + CustomPostfix, + new CodeInstruction(OpCodes.Ldstr, $"{methodBase.DeclaringType?.Name}.{methodBase.Name}"), + new CodeInstruction(OpCodes.Ldloc, local)); + } - public static void CustomPostfix(string methodName, DateTime __state) + public static void CustomPostfix(string methodName, DateTime __state) + { + if (MetricsLogger.Instance == null) { - if (MetricsLogger.Instance == null) - return; - - var duration = DateTime.UtcNow - __state; - MetricsLogger.Instance.ServerRpcCalls.LogTime(methodName, duration.TotalMilliseconds); + return; } + + var duration = DateTime.UtcNow - __state; + MetricsLogger.Instance.ServerRpcCalls.LogTime(methodName, duration.TotalMilliseconds); } -} +} \ No newline at end of file