Decompiled source of UIScaler v1.1.0

plugins/UIScaler.dll

Decompiled 2 days ago
using System;
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.Configuration;
using BepInEx.Logging;
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("UIScaler")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+803feb1c283d5680fe79b461f26a46f997ede83a")]
[assembly: AssemblyProduct("UIScaler")]
[assembly: AssemblyTitle("UIScaler")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 UIScaler
{
	public static class CommandHelper
	{
		public static void Register(string name, string category, Action<string[]> handler, string desc, string paramName, string paramTypeStr, bool isOptional = false)
		{
			try
			{
				Type type = Type.GetType("CommandAPI.CommandRegistry, CommandAPI");
				if (type == null)
				{
					return;
				}
				Type type2 = Type.GetType("CommandAPI.Parameter, CommandAPI");
				Type type3 = Type.GetType("CommandAPI.ParameterType, CommandAPI");
				if (type2 == null || type3 == null)
				{
					return;
				}
				ConstructorInfo constructorInfo = type2.GetConstructors()[0];
				ParameterInfo[] parameters = constructorInfo.GetParameters();
				object[] array = new object[parameters.Length];
				array[0] = paramName;
				object obj = Enum.Parse(type3, paramTypeStr);
				array[1] = obj;
				for (int i = 2; i < parameters.Length; i++)
				{
					if (parameters[i].ParameterType == typeof(bool) && i == 2)
					{
						array[i] = isOptional;
					}
					else if (parameters[i].HasDefaultValue)
					{
						array[i] = parameters[i].DefaultValue;
					}
					else
					{
						array[i] = null;
					}
				}
				object value = constructorInfo.Invoke(array);
				Array array2 = Array.CreateInstance(type2, 1);
				array2.SetValue(value, 0);
				MethodInfo method = type.GetMethod("Register", new Type[5]
				{
					typeof(string),
					typeof(string),
					typeof(Action<string[]>),
					typeof(string),
					array2.GetType()
				});
				if (method != null)
				{
					method.Invoke(null, new object[5] { name, category, handler, desc, array2 });
					Debug.Log((object)("[UIScaler] Registered command /" + name + " in category " + category));
				}
			}
			catch (Exception ex)
			{
				Debug.LogWarning((object)("[UIScaler] Failed to register /" + name + ": " + ex.Message));
			}
		}

		public static void Notify(string message)
		{
			try
			{
				Type type = Type.GetType("CommandAPI.Utilities, CommandAPI");
				if (!(type == null))
				{
					MethodInfo method = type.GetMethod("Notify", new Type[1] { typeof(string) });
					if (method != null)
					{
						method.Invoke(null, new object[1] { message });
					}
				}
			}
			catch
			{
			}
		}
	}
	[BepInPlugin("com.jai.ontogether.uiscaler", "UI Scaler", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public static ConfigEntry<float> UIScale;

		public static ManualLogSource Log;

		private static Canvas rootCanvas;

		private void Awake()
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			UIScale = ((BaseUnityPlugin)this).Config.Bind<float>("General", "UIScale", 1f, "Global UI scale (0.5 - 2.0)");
			Harmony val = new Harmony("com.jai.ontogether.uiscaler");
			Type type = AccessTools.TypeByName("UIManager");
			val.Patch((MethodBase)AccessTools.Method(type, "Awake", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "UIManager_Awake_Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			CommandHelper.Register("scale", "UI", HandleScaleCommand, "Usage: /scale <value>. Values: 0.5 - 2.0", "value", "Float", isOptional: true);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"UI Scaler loaded");
		}

		private static void UIManager_Awake_Postfix(UIManager __instance)
		{
			Canvas componentInParent = ((Component)__instance).GetComponentInParent<Canvas>();
			rootCanvas = ((componentInParent != null) ? componentInParent.rootCanvas : null);
			if ((Object)(object)rootCanvas == (Object)null)
			{
				Canvas obj = Object.FindAnyObjectByType<Canvas>();
				rootCanvas = ((obj != null) ? obj.rootCanvas : null);
			}
			if ((Object)(object)rootCanvas != (Object)null)
			{
				ApplyScale(UIScale.Value);
			}
		}

		private static void HandleScaleCommand(string[] args)
		{
			float result;
			if (args.Length == 0)
			{
				CommandHelper.Notify($"Current scale: {UIScale.Value:F2}. Usage: /scale 1.5");
			}
			else if (float.TryParse(args[0], out result))
			{
				result = Mathf.Clamp(result, 0.5f, 2f);
				ApplyScale(result);
				UIScale.Value = result;
				((ConfigEntryBase)UIScale).ConfigFile.Save();
				CommandHelper.Notify($"UI scale set to {result:F2}");
			}
			else
			{
				CommandHelper.Notify("Invalid scale value. Usage: /scale 1.5");
			}
		}

		private static void ApplyScale(float scale)
		{
			if (!((Object)(object)rootCanvas == (Object)null))
			{
				rootCanvas.scaleFactor = scale;
			}
		}
	}
}