Decompiled source of ResolutionAdjust v1.0.1

ResolutionAdjust.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("ResolutionAdjust")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("ResolutionAdjust")]
[assembly: AssemblyTitle("ResolutionAdjust")]
[assembly: AssemblyVersion("1.0.1.0")]
[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 ResolutionAdjust
{
	[BepInPlugin("Miles.ResolutionAdjust", "ResolutionAdjust", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "Miles.ResolutionAdjust";

		public const string Name = "ResolutionAdjust";

		public const string Version = "1.0.1";

		internal static ManualLogSource Log;

		internal static ConfigEntry<int> Width;

		internal static ConfigEntry<bool> HideAboveMonitor;

		private void Awake()
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Width = ((BaseUnityPlugin)this).Config.Bind<int>("Resolution", "Width", 1920, "Width of the game's render resolution. Easiest to change in game: Settings > Pixel Resolution. The height follows the game's 860:520 shape (1920 = 1920x1161). A width that isn't in the menu shows up there as Custom.");
			HideAboveMonitor = ((BaseUnityPlugin)this).Config.Bind<bool>("Resolution", "HideAboveMonitor", true, "Hide resolutions wider than your monitor from the menu.");
			ResolutionMenu.Build();
			new Harmony("Miles.ResolutionAdjust").PatchAll(typeof(ResolutionMenu));
			if (Chainloader.PluginInfos.ContainsKey("HDLethalCompany"))
			{
				Log.LogWarning((object)"HDLethalCompany is installed too. Set its EnableRes to false so the two mods don't fight over the resolution.");
			}
			Log.LogInfo((object)string.Format("{0} {1} loaded: {2} resolutions in the menu.", "ResolutionAdjust", "1.0.1", ResolutionMenu.Presets.Count));
		}
	}
	internal static class ResolutionMenu
	{
		internal class Preset
		{
			public int W;

			public int H;

			public string Label;
		}

		internal static readonly List<Preset> Presets = new List<Preset>();

		private const int VanillaW = 860;

		private const int VanillaH = 520;

		private static int monitorWidth;

		private static bool settingsLoaded;

		internal static void Build()
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			Presets.Clear();
			try
			{
				monitorWidth = ((Display.main != null) ? Display.main.systemWidth : 0);
			}
			catch
			{
			}
			if (monitorWidth <= 0)
			{
				try
				{
					Resolution currentResolution = Screen.currentResolution;
					monitorWidth = ((Resolution)(ref currentResolution)).width;
				}
				catch
				{
				}
			}
			bool flag = Plugin.HideAboveMonitor.Value && monitorWidth > 0;
			(int, string)[] array = new(int, string)[4]
			{
				(3840, "4K"),
				(2560, "1440p"),
				(1920, "1080p"),
				(1280, "720p")
			};
			for (int i = 0; i < array.Length; i++)
			{
				var (num, tag) = array[i];
				if (!flag || num <= monitorWidth + 1)
				{
					AddWide(num, tag);
				}
			}
			Add(860, 520, "Vanilla");
			Add(620, 364, "Low");
			Add(400, 260, "Retro");
			Add(186, 104, "Very Retro");
			if (monitorWidth > 860 && Find(monitorWidth) < 0)
			{
				AddWide(monitorWidth, "Native");
			}
			int value = Plugin.Width.Value;
			if (value > 0 && Find(value) < 0)
			{
				AddWide(value, "Custom");
			}
			Presets.Sort((Preset a, Preset b) => b.W.CompareTo(a.W));
		}

		private static void AddWide(int w, string tag)
		{
			Add(w, (int)Math.Round((double)w * 520.0 / 860.0), tag);
		}

		private static void Add(int w, int h, string tag)
		{
			Presets.Add(new Preset
			{
				W = w,
				H = h,
				Label = $"{w}x{h} ({tag})"
			});
		}

		private static int Find(int width)
		{
			for (int i = 0; i < Presets.Count; i++)
			{
				if (Math.Abs(Presets[i].W - width) <= 1)
				{
					return i;
				}
			}
			return -1;
		}

		private static int ConfigIndex()
		{
			int num = Find(Plugin.Width.Value);
			if (num < 0)
			{
				return DefaultIndex();
			}
			return num;
		}

		private static int DefaultIndex()
		{
			int num = Find(1920);
			if (num >= 0)
			{
				return num;
			}
			for (num = 0; num < Presets.Count; num++)
			{
				if (Presets[num].W <= 1920)
				{
					return num;
				}
			}
			return 0;
		}

		private static int Clamp(int i)
		{
			return Mathf.Clamp(i, 0, Presets.Count - 1);
		}

		private static int VanillaEquivalent(Preset p)
		{
			if (p.W >= 860)
			{
				return 0;
			}
			if (p.W >= 620)
			{
				return 1;
			}
			if (p.W >= 400)
			{
				return 2;
			}
			return 3;
		}

		private static void Apply(Preset p, RenderTexture tex)
		{
			Resize(tex, p.W, p.H);
			try
			{
				PlayerControllerB val = (((Object)(object)StartOfRound.Instance != (Object)null) ? StartOfRound.Instance.localPlayerController : null);
				RenderTexture val2 = (((Object)(object)val != (Object)null && (Object)(object)val.gameplayCamera != (Object)null) ? val.gameplayCamera.targetTexture : null);
				if ((Object)(object)val2 != (Object)null && (Object)(object)val2 != (Object)(object)tex)
				{
					Resize(val2, p.W, p.H);
				}
			}
			catch
			{
			}
		}

		private static void Resize(RenderTexture tex, int w, int h)
		{
			if (!((Object)(object)tex == (Object)null) && (((Texture)tex).width != w || ((Texture)tex).height != h))
			{
				tex.Release();
				((Texture)tex).width = w;
				((Texture)tex).height = h;
			}
		}

		private static void ApplySaved()
		{
			IngamePlayerSettings instance = IngamePlayerSettings.Instance;
			if (!((Object)(object)instance == (Object)null) && settingsLoaded)
			{
				Apply(Presets[Clamp(instance.settings.pixelRes)], instance.playerGameplayScreenTex);
			}
		}

		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		[HarmonyPostfix]
		private static void SettingsCtorPostfix(Settings __instance)
		{
			if (Presets.Count > 0)
			{
				__instance.pixelRes = DefaultIndex();
			}
		}

		[HarmonyPatch(typeof(IngamePlayerSettings), "LoadSettingsFromPrefs")]
		[HarmonyPostfix]
		private static void LoadPostfix(IngamePlayerSettings __instance)
		{
			int pixelRes = ConfigIndex();
			__instance.settings.pixelRes = pixelRes;
			__instance.unsavedSettings.pixelRes = pixelRes;
			settingsLoaded = true;
		}

		[HarmonyPatch(typeof(IngamePlayerSettings), "SetPixelResolution")]
		[HarmonyPrefix]
		private static bool SetPixelResolutionPrefix(int value, IngamePlayerSettings __instance)
		{
			value = Clamp(value);
			__instance.unsavedSettings.pixelRes = value;
			Apply(Presets[value], __instance.playerGameplayScreenTex);
			return false;
		}

		[HarmonyPatch(typeof(IngamePlayerSettings), "SaveSettingsToPrefs")]
		[HarmonyPrefix]
		private static void SavePrefix(IngamePlayerSettings __instance, out int __state)
		{
			__state = -1;
			if (settingsLoaded)
			{
				Preset preset = Presets[Clamp(__instance.settings.pixelRes)];
				if (Find(Plugin.Width.Value) != Presets.IndexOf(preset))
				{
					Plugin.Width.Value = preset.W;
				}
				__state = __instance.settings.pixelRes;
				__instance.settings.pixelRes = VanillaEquivalent(preset);
			}
		}

		[HarmonyPatch(typeof(IngamePlayerSettings), "SaveSettingsToPrefs")]
		[HarmonyPostfix]
		private static void SavePostfix(IngamePlayerSettings __instance, int __state)
		{
			if (__state >= 0)
			{
				__instance.settings.pixelRes = __state;
			}
		}

		[HarmonyPatch(typeof(PlayerControllerB), "Start")]
		[HarmonyPostfix]
		[HarmonyPriority(0)]
		private static void PlayerStartPostfix()
		{
			ApplySaved();
		}

		[HarmonyPatch(typeof(SettingsOption), "OnEnable")]
		[HarmonyPostfix]
		private static void OptionEnablePostfix(SettingsOption __instance)
		{
			FillDropdown(__instance);
		}

		[HarmonyPatch(typeof(SettingsOption), "SetValueToMatchSettings")]
		[HarmonyPostfix]
		private static void OptionMatchPostfix(SettingsOption __instance)
		{
			FillDropdown(__instance);
		}

		private static void FitText(TMP_Text t)
		{
			if (!((Object)(object)t == (Object)null) && !t.enableAutoSizing)
			{
				float fontSize = t.fontSize;
				t.enableWordWrapping = false;
				t.overflowMode = (TextOverflowModes)1;
				t.fontSizeMax = fontSize;
				t.fontSizeMin = fontSize * 0.55f;
				t.enableAutoSizing = true;
			}
		}

		private static void FillDropdown(SettingsOption option)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Invalid comparison between Unknown and I4
			if ((int)option.optionType != 16)
			{
				return;
			}
			TMP_Dropdown componentInChildren = ((Component)option).GetComponentInChildren<TMP_Dropdown>(true);
			if ((Object)(object)componentInChildren == (Object)null || (Object)(object)IngamePlayerSettings.Instance == (Object)null)
			{
				return;
			}
			bool flag = componentInChildren.options.Count == Presets.Count;
			int num = 0;
			while (flag && num < Presets.Count)
			{
				flag = componentInChildren.options[num].text == Presets[num].Label;
				num++;
			}
			if (!flag)
			{
				FitText(componentInChildren.itemText);
				FitText(componentInChildren.captionText);
				List<string> list = new List<string>(Presets.Count);
				foreach (Preset preset in Presets)
				{
					list.Add(preset.Label);
				}
				componentInChildren.ClearOptions();
				componentInChildren.AddOptions(list);
			}
			componentInChildren.SetValueWithoutNotify(Clamp(IngamePlayerSettings.Instance.settings.pixelRes));
			componentInChildren.RefreshShownValue();
		}
	}
}