Skip to content

Commit

Permalink
very minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Jexs committed Oct 3, 2024
1 parent 0e835b1 commit 1ce643c
Showing 1 changed file with 54 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<MethodBase> TargetMethods(Harmony harmonyInstance)
{
var baseNetworkableType = typeof(BaseNetworkable);
var baseNetworkableAssembly = baseNetworkableType.Assembly;
Stack<Type> typesToScan = new Stack<Type>(baseNetworkableAssembly.GetTypes());
[HarmonyTargetMethods]
public static IEnumerable<MethodBase> TargetMethods(Harmony harmonyInstance)
{
var baseNetworkableType = typeof(BaseNetworkable);
var baseNetworkableAssembly = baseNetworkableType.Assembly;
var typesToScan = new Stack<Type>(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<BaseEntity.RPC_Server>() != null)
{
var method = methods[i];
if (method.DeclaringType == method.ReflectedType && method.GetCustomAttribute<BaseEntity.RPC_Server>() != null)
{
yield return method;
}
yield return method;
}
}
}
}
}

[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> Transpile(IEnumerable<CodeInstruction> originalInstructions, MethodBase methodBase, ILGenerator ilGenerator)
{
List<CodeInstruction> ret = originalInstructions.ToList();
LocalBuilder local = ilGenerator.DeclareLocal(typeof(DateTime));
[HarmonyTranspiler]
public static IEnumerable<CodeInstruction> Transpile(IEnumerable<CodeInstruction> 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);
}
}
}

0 comments on commit 1ce643c

Please sign in to comment.