Decompiled source of PupValheimTuneCompat v1.0.1

Cjayride.PupValheimTuneCompat.dll

Decompiled 3 days ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("assembly_valheim")]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyCompany("Cjayride.PupValheimTuneCompat")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("Cjayride.PupValheimTuneCompat")]
[assembly: AssemblyTitle("Cjayride.PupValheimTuneCompat")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace Cjayride.PupValheimTuneCompat
{
	[BepInPlugin("cjayride.PupValheimTuneCompat", "PUP FPS ValheimTune compat", "1.0.1")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "cjayride.PupValheimTuneCompat";

		public const string PluginName = "PUP FPS ValheimTune compat";

		public const string PluginVersion = "1.0.1";

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony val = new Harmony("cjayride.PupValheimTuneCompat");
			Type type = AccessTools.TypeByName("ServerZoneStreamBlock");
			if (type == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"[PUP+ValheimTune] ServerZoneStreamBlock type not found; guard not installed.");
				return;
			}
			MethodInfo methodInfo = AccessTools.Method(type, "Update", (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"[PUP+ValheimTune] ServerZoneStreamBlock.Update not found; guard not installed.");
				return;
			}
			val.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(PupStreamGuard), "UpdatePrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[PUP+ValheimTune] Patched ServerZoneStreamBlock.Update — will block SendZDOs Harmony when ServerStreaming is off or ValheimTune owns the window.");
		}
	}
	internal static class PupStreamGuard
	{
		private const int VanillaSendWindow = 10240;

		private const int VanillaHeadroom = 2048;

		private const string ValheimTuneGuid = "akoozie.valheimtune";

		private const string PupStreamingType = "ServerZoneStreamBlock";

		public static bool UpdatePrefix(object __instance)
		{
			if (Traverse.Create(__instance).Field("initialized").GetValue<bool>())
			{
				return true;
			}
			if ((Object)(object)ZNet.instance == (Object)null)
			{
				return true;
			}
			if (!ZNet.instance.IsDedicated())
			{
				return true;
			}
			if (!ShouldSuppressPupSendZdosPatch())
			{
				return true;
			}
			Traverse.Create(__instance).Field("initialized").SetValue((object)true);
			Behaviour val = (Behaviour)((__instance is Behaviour) ? __instance : null);
			if (val != null)
			{
				val.enabled = false;
			}
			ManualLogSource log = Plugin.Log;
			if (log != null)
			{
				log.LogInfo((object)("[PUP+ValheimTune] Skipped PUP FPS ServerStreaming SendZDOs patches: " + GetSkipReason()));
			}
			return false;
		}

		private static string GetSkipReason()
		{
			if (IsServerStreamingDisabled())
			{
				return "ServerStreaming.Enabled=false in PUP FPS.cfg";
			}
			if (ValheimTuneOwnsSendWindow())
			{
				return "ValheimTune OverrideSendWindow with non-vanilla SendWindowBytes/MinHeadroomBytes";
			}
			return "compat guard";
		}

		private static bool ShouldSuppressPupSendZdosPatch()
		{
			if (IsServerStreamingDisabled())
			{
				return true;
			}
			return ValheimTuneOwnsSendWindow();
		}

		private static bool IsServerStreamingDisabled()
		{
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			Type type = AccessTools.TypeByName("ServerZoneStreamBlock");
			if (type != null && AccessTools.Field(type, "enabledSetting")?.GetValue(null) is ConfigEntry<bool> val)
			{
				return !val.Value;
			}
			string text = Path.Combine(Paths.ConfigPath, "PUP FPS.cfg");
			if (!File.Exists(text))
			{
				return false;
			}
			return !new ConfigFile(text, true).Bind<bool>("ServerStreaming", "Enabled", true, (ConfigDescription)null).Value;
		}

		private static bool ValheimTuneOwnsSendWindow()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			if (!Chainloader.PluginInfos.ContainsKey("akoozie.valheimtune"))
			{
				return false;
			}
			string text = Path.Combine(Paths.ConfigPath, "akoozie.valheimtune.cfg");
			if (!File.Exists(text))
			{
				return false;
			}
			ConfigFile val = new ConfigFile(text, true);
			if (!val.Bind<bool>("Sync", "OverrideSendWindow", true, (ConfigDescription)null).Value)
			{
				return false;
			}
			int value = val.Bind<int>("Sync", "SendWindowBytes", 10240, (ConfigDescription)null).Value;
			int value2 = val.Bind<int>("Sync", "MinHeadroomBytes", 2048, (ConfigDescription)null).Value;
			if (value == 10240)
			{
				return value2 != 2048;
			}
			return true;
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}