new-horizons/NewHorizons/Patches/ProfilerPatch.cs
JohnCorby c08fbe194a log
2025-01-29 14:11:55 -08:00

53 lines
1.4 KiB
C#

// #define ENABLE_PROFILER
using HarmonyLib;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using UnityEngine.Profiling;
namespace NewHorizons.Patches;
/// <summary>
/// attach profiler markers to important methods
/// </summary>
[HarmonyPatch]
public static class ProfilerPatch
{
[HarmonyTargetMethods]
public static IEnumerable<MethodBase> TargetMethods()
{
foreach (var type in Assembly.GetExecutingAssembly().GetTypes())
{
if (!type.Name.EndsWith("Builder")) continue;
foreach (var method in type.GetRuntimeMethods())
{
if (!(method.Name.StartsWith("Make") || method.Name.StartsWith("Init"))) continue;
if (method.IsGenericMethod) continue;
Main.Instance.ModHelper.Console.WriteLine($"[profiler] profiling {method.DeclaringType.Name}.{method.Name}");
yield return method;
}
}
}
[HarmonyPrefix]
public static void Prefix(MethodBase __originalMethod, out Stopwatch __state)
{
Profiler.BeginSample($"{__originalMethod.DeclaringType.Name}.{__originalMethod.Name}");
__state = new Stopwatch();
__state.Start();
}
[HarmonyPostfix]
public static void Postfix(MethodBase __originalMethod, Stopwatch __state)
{
Profiler.EndSample();
__state.Stop();
Main.Instance.ModHelper.Console.WriteLine($"[profiler] {__originalMethod.DeclaringType.Name}.{__originalMethod.Name} took {__state.Elapsed.TotalMilliseconds:f1} ms");
}
}