Decompiled source of CM Scaler v1.0.0

CM_Scaler.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
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 HarmonyLib;
using Microsoft.CodeAnalysis;
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("CM_Scaler")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+2772b3e63a67d5018b8cdec9ba1154055516bdeb")]
[assembly: AssemblyProduct("CM_Scaler")]
[assembly: AssemblyTitle("CM_Scaler")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/FabulatorK/CM_Scaler")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 BepInEx
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class BepInAutoPluginAttribute : Attribute
	{
		public BepInAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace BepInEx.Preloader.Core.Patching
{
	[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
	[Conditional("CodeGeneration")]
	internal sealed class PatcherAutoPluginAttribute : Attribute
	{
		public PatcherAutoPluginAttribute(string? id = null, string? name = null, string? version = null)
		{
		}
	}
}
namespace CM_Scaler
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("io.github.fabulatork.cm_scaler", "CM_Scaler", "1.0.0")]
	public class CMScalerPlugin : BaseUnityPlugin
	{
		internal const string CmGuid = "com.bepis.bepinex.configurationmanager";

		internal static ConfigEntry<bool> AutoScaleEntry;

		internal static ConfigEntry<float> ManualScaleEntry;

		public const string Id = "io.github.fabulatork.cm_scaler";

		public static string Name => "CM_Scaler";

		public static string Version => "1.0.0";

		private void Awake()
		{
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Expected O, but got Unknown
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Expected O, but got Unknown
			AutoScaleEntry = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Auto scale", true, "Fit the Configuration Manager UI to the screen resolution (screen height / 1080, min 1.0, max 2.5). Turn off to use Manual scale.");
			ManualScaleEntry = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Manual scale", 1f, new ConfigDescription("UI scale used when Auto scale is off.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 3f), Array.Empty<object>()));
			if (!Chainloader.PluginInfos.ContainsKey("com.bepis.bepinex.configurationmanager"))
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"ConfigurationManager not present - nothing to scale.");
				return;
			}
			if (!ScalerPatch.TryResolveMembers(out string missing))
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("ConfigurationManager internals have changed (" + missing + " not found) - scaling disabled."));
				return;
			}
			ScalerPatch.Apply(new Harmony("io.github.fabulatork.cm_scaler"));
			((BaseUnityPlugin)this).Logger.LogInfo((object)(Name + " " + Version + " patched ConfigurationManager."));
		}
	}
	internal static class ScalerPatch
	{
		private static MethodInfo _onGui = null;

		private static MethodInfo _calcWindowRect = null;

		private static MethodInfo _setSettingWindowRect = null;

		private static MethodInfo _setLeftColumnWidth = null;

		private static MethodInfo _setRightColumnWidth = null;

		private static FieldInfo _screenRect = null;

		private static Matrix4x4 _savedMatrix;

		private static Matrix4x4 _scaleMatrix = Matrix4x4.identity;

		private static float _appliedScale;

		private static int _lastScreenHeight;

		internal static float CurrentScale()
		{
			if (CMScalerPlugin.AutoScaleEntry.Value)
			{
				return Mathf.Clamp((float)Screen.height / 1080f, 1f, 2.5f);
			}
			return CMScalerPlugin.ManualScaleEntry.Value;
		}

		internal static bool TryResolveMembers(out string missing)
		{
			Type type = AccessTools.TypeByName("ConfigurationManager.ConfigurationManager");
			if (type == null)
			{
				missing = "type ConfigurationManager.ConfigurationManager";
				return false;
			}
			_onGui = AccessTools.Method(type, "OnGUI", (Type[])null, (Type[])null);
			_calcWindowRect = AccessTools.Method(type, "CalculateWindowRect", (Type[])null, (Type[])null);
			_setSettingWindowRect = AccessTools.PropertySetter(type, "SettingWindowRect");
			_setLeftColumnWidth = AccessTools.PropertySetter(type, "LeftColumnWidth");
			_setRightColumnWidth = AccessTools.PropertySetter(type, "RightColumnWidth");
			_screenRect = AccessTools.Field(type, "_screenRect");
			List<string> list = new List<string>();
			if (_onGui == null)
			{
				list.Add("OnGUI");
			}
			if (_calcWindowRect == null)
			{
				list.Add("CalculateWindowRect");
			}
			if (_setSettingWindowRect == null)
			{
				list.Add("SettingWindowRect setter");
			}
			if (_setLeftColumnWidth == null)
			{
				list.Add("LeftColumnWidth setter");
			}
			if (_setRightColumnWidth == null)
			{
				list.Add("RightColumnWidth setter");
			}
			if (_screenRect == null)
			{
				list.Add("_screenRect");
			}
			missing = string.Join(", ", list);
			return list.Count == 0;
		}

		internal static void Apply(Harmony harmony)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_0038: Expected O, but got Unknown
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Expected O, but got Unknown
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Expected O, but got Unknown
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Expected O, but got Unknown
			harmony.Patch((MethodBase)_onGui, new HarmonyMethod(typeof(ScalerPatch), "OnGuiPrefix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(ScalerPatch), "OnGuiFinalizer", (Type[])null), (HarmonyMethod)null);
			harmony.Patch((MethodBase)_calcWindowRect, (HarmonyMethod)null, new HarmonyMethod(typeof(ScalerPatch), "CalculateWindowRectPostfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			HarmonyMethod val = new HarmonyMethod(typeof(ScalerPatch), "ScreenPointConversionPrefix", (Type[])null);
			HarmonyMethod val2 = new HarmonyMethod(typeof(ScalerPatch), "ScreenPointConversionPostfix", (Type[])null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(GUIUtility), "GUIToScreenPoint", (Type[])null, (Type[])null), val, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			harmony.Patch((MethodBase)AccessTools.Method(typeof(GUIUtility), "ScreenToGUIPoint", (Type[])null, (Type[])null), val, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		private static void ScreenPointConversionPrefix(ref Matrix4x4 __state)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			__state = GUI.matrix;
			if (Mathf.Abs(_appliedScale - 1f) >= 0.001f && GUI.matrix == _scaleMatrix)
			{
				GUI.matrix = Matrix4x4.identity;
			}
		}

		private static void ScreenPointConversionPostfix(ref Matrix4x4 __state)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			GUI.matrix = __state;
		}

		private static void OnGuiPrefix(object __instance)
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			float num = CurrentScale();
			if (num != _appliedScale || Screen.height != _lastScreenHeight)
			{
				_appliedScale = num;
				_lastScreenHeight = Screen.height;
				_calcWindowRect.Invoke(__instance, null);
			}
			_savedMatrix = GUI.matrix;
			if (Mathf.Abs(num - 1f) >= 0.001f)
			{
				_scaleMatrix = Matrix4x4.Scale(new Vector3(num, num, 1f));
				GUI.matrix = _scaleMatrix;
			}
		}

		private static void OnGuiFinalizer()
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			GUI.matrix = _savedMatrix;
		}

		private static void CalculateWindowRectPostfix(object __instance)
		{
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
			float num = CurrentScale();
			if (!(Mathf.Abs(num - 1f) < 0.001f))
			{
				float num2 = (float)Screen.width / num;
				float num3 = (float)Screen.height / num;
				float num4 = Mathf.Min(num2, 650f);
				float num5 = ((num3 < 560f) ? num3 : (num3 - 100f));
				Rect val = default(Rect);
				((Rect)(ref val))..ctor((float)Mathf.RoundToInt((num2 - num4) / 2f), (float)Mathf.RoundToInt((num3 - num5) / 2f), num4, num5);
				_setSettingWindowRect.Invoke(__instance, new object[1] { val });
				int num6 = Mathf.RoundToInt(((Rect)(ref val)).width / 2.5f);
				_setLeftColumnWidth.Invoke(__instance, new object[1] { num6 });
				_setRightColumnWidth.Invoke(__instance, new object[1] { (int)((Rect)(ref val)).width - num6 - 115 });
				_screenRect.SetValue(__instance, (object)new Rect(0f, 0f, num2, num3));
			}
		}
	}
}