using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("NetworkMetricsFix")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NetworkMetricsFix")]
[assembly: AssemblyCopyright("Copyright © 2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7dae59af-10d7-4616-9f07-f62092bdd092")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace NetworkMetricsFix;
[BepInPlugin("NetworkMetricsFix", "chuxia.NetworkMetricsFix", "1.0.4")]
public class NetworkMetricsFixPlugin : BaseUnityPlugin
{
public static ManualLogSource ManualLog;
public static ConfigEntry<bool> EnableNetworkMetrics;
public void Awake()
{
ManualLog = ((BaseUnityPlugin)this).Logger;
EnableNetworkMetrics = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "EnableNetworkMetrics", false, "Enable Network Metrics.but disable warnings");
Harmony.CreateAndPatchAll(typeof(NetworkMetricsPatch1), (string)null);
if (EnableNetworkMetrics.Value)
{
Harmony.CreateAndPatchAll(typeof(NetworkMetricsPatch2), (string)null);
}
}
}
[HarmonyPatch]
[HarmonyWrapSafe]
public static class NetworkMetricsPatch1
{
public static List<Action<object>> SetWarningDelegates { get; set; }
public static MethodBase TargetMethod()
{
Type type = Type.GetType("Unity.Netcode.NetworkMetrics, Unity.Netcode.Runtime");
ConstructorInfo[] constructors = type.GetConstructors();
int num = 0;
if (num < constructors.Length)
{
return constructors[num];
}
return null;
}
private static void Postfix(object __instance)
{
//IL_0135: Unknown result type (might be due to invalid IL or missing references)
//IL_013c: Expected O, but got Unknown
//IL_0196: Unknown result type (might be due to invalid IL or missing references)
//IL_01a4: Expected O, but got Unknown
if (NetworkMetricsFixPlugin.EnableNetworkMetrics.Value)
{
SetWarningDelegates = new List<Action<object>>();
{
foreach (FieldInfo declaredField in AccessTools.GetDeclaredFields(__instance.GetType()))
{
if (declaredField.FieldType.Name == "EventMetric`1")
{
FieldInfo fieldInfo = AccessTools.DeclaredField(__instance.GetType(), declaredField.Name);
object value = fieldInfo.GetValue(__instance);
AccessTools.DeclaredProperty(value.GetType(), "MaxNumberOfValues").SetValue(value, 1000u);
ParameterExpression parameterExpression = Expression.Parameter(typeof(object));
MemberExpression expression = Expression.Field(Expression.Convert(parameterExpression, __instance.GetType()), fieldInfo);
MemberExpression left = Expression.Property(expression, "WentOverLimit");
BinaryExpression body = Expression.Assign(left, Expression.Constant(false));
Action<object> item = Expression.Lambda<Action<object>>(body, new ParameterExpression[1] { parameterExpression }).Compile();
SetWarningDelegates.Add(item);
}
}
return;
}
}
Harmony val = new Harmony("chuxia.NetworkMetricsFix");
MethodInfo methodInfo = AccessTools.Method(typeof(NetworkMetricsPatch1), "MethodPrefix", (Type[])null, (Type[])null);
foreach (MethodInfo declaredMethod in AccessTools.GetDeclaredMethods(__instance.GetType()))
{
if (declaredMethod.ReturnType == typeof(void))
{
val.Patch((MethodBase)declaredMethod, new HarmonyMethod(methodInfo), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
}
}
}
public static bool MethodPrefix()
{
return false;
}
}
[HarmonyPatch]
[HarmonyWrapSafe]
public static class NetworkMetricsPatch2
{
public static MethodBase TargetMethod()
{
Type type = Type.GetType("Unity.Netcode.NetworkMetrics, Unity.Netcode.Runtime");
return AccessTools.DeclaredMethod(type, "DispatchFrame", (Type[])null, (Type[])null);
}
private static void Prefix(object __instance)
{
foreach (Action<object> setWarningDelegate in NetworkMetricsPatch1.SetWarningDelegates)
{
setWarningDelegate(__instance);
}
}
}