Files
bd2/plugins/BetterExperience/Performance/PerformanceOverlay.cs
T

106 lines
3.4 KiB
C#

using System;
using System.Reflection;
using BD2.GameNames;
using HarmonyLib;
using UnityEngine;
namespace Bd2BetterExperience.Performance;
internal sealed class PerformanceOverlay : IModule
{
public string Id => "PerformanceOverlay";
public string Description => "Hide the native FPS performance overlay.";
public void Install(Harmony harmony)
{
Type fpsCheck = typeof(FPS_Check);
MethodInfo start = fpsCheck?.GetGameMethod(
nameof(FPS_Check.StartFPS),
BindingFlags.Instance | BindingFlags.Public,
null,
Type.EmptyTypes,
null);
MethodInfo render = fpsCheck?.GetGameMethod(
"TextRender",
BindingFlags.Instance | BindingFlags.NonPublic,
null,
Type.EmptyTypes,
null);
MethodInfo onEnable = fpsCheck?.GetGameMethod(
"OnEnable",
BindingFlags.Instance | BindingFlags.NonPublic,
null,
Type.EmptyTypes,
null);
MethodInfo onInitialize = fpsCheck?.GetGameMethod(
nameof(FPS_Check.OnInitialize),
BindingFlags.Instance | BindingFlags.Public,
null,
Type.EmptyTypes,
null);
if (start == null || render == null || onEnable == null || onInitialize == null)
{
throw new MissingMethodException("FPS_Check overlay methods were not found (client version mismatch)");
}
var skip = new HarmonyMethod(typeof(PerformanceOverlay), nameof(SkipPerformanceOverlay));
var hide = new HarmonyMethod(typeof(PerformanceOverlay), nameof(HidePerformanceOverlayPostfix));
harmony.Patch(start, prefix: skip);
harmony.Patch(render, prefix: skip);
harmony.Patch(onEnable, postfix: hide);
harmony.Patch(onInitialize, postfix: hide);
Plugin.Log?.LogInfo("FPS_Check performance overlay disabled");
}
private static bool SkipPerformanceOverlay(object __instance)
{
HidePerformanceOverlay(__instance);
return false;
}
private static void HidePerformanceOverlayPostfix(object __instance)
{
HidePerformanceOverlay(__instance);
}
private static void HidePerformanceOverlay(object instance)
{
if (instance == null)
{
return;
}
try
{
MethodInfo disposeRecorders = instance.GetType().GetGameMethod(
nameof(FPS_Check.DisposeRecorders),
BindingFlags.Instance | BindingFlags.Public,
null,
Type.EmptyTypes,
null);
disposeRecorders?.Invoke(instance, null);
FieldInfo textField = instance.GetType().GetGameField(
"_text",
BindingFlags.Instance | BindingFlags.Public);
object text = textField?.GetValue(instance);
PropertyInfo textProperty = text?.GetType().GetGameProperty(
"text",
BindingFlags.Instance | BindingFlags.Public);
if (textProperty?.CanWrite == true)
{
textProperty.SetValue(text, string.Empty);
}
if (text is Component component && component != null)
{
component.gameObject.SetActive(false);
}
}
catch (Exception ex)
{
Plugin.Log?.LogError("Could not suppress FPS_Check overlay: " + ex);
}
}
}