Decompiled source of ChatResizer v1.2.0

plugins/ChatResizer.dll

Decompiled 2 weeks 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 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("ChatResizer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+ae3c39a8a00dc39d424dc2688d58cdd5d0ca3ebc")]
[assembly: AssemblyProduct("ChatResizer")]
[assembly: AssemblyTitle("ChatResizer")]
[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 ChatResizer
{
	public static class CommandHelper
	{
		public struct Param
		{
			public string Name;

			public string TypeStr;

			public bool Optional;

			public Param(string name, string typeStr, bool optional = false)
			{
				Name = name;
				TypeStr = typeStr;
				Optional = optional;
			}
		}

		public static void Register(string name, string category, Action<string[]> handler, string desc, params Param[] parameters)
		{
			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[] parameters2 = constructorInfo.GetParameters();
				Array array = Array.CreateInstance(type2, parameters.Length);
				for (int i = 0; i < parameters.Length; i++)
				{
					Param param = parameters[i];
					object obj = Enum.Parse(type3, param.TypeStr);
					object[] array2 = new object[parameters2.Length];
					array2[0] = param.Name;
					array2[1] = obj;
					for (int j = 2; j < parameters2.Length; j++)
					{
						if (parameters2[j].ParameterType == typeof(bool) && j == 2)
						{
							array2[j] = param.Optional;
						}
						else if (parameters2[j].HasDefaultValue)
						{
							array2[j] = parameters2[j].DefaultValue;
						}
						else
						{
							array2[j] = null;
						}
					}
					object value = constructorInfo.Invoke(array2);
					array.SetValue(value, i);
				}
				MethodInfo method = type.GetMethod("Register", new Type[5]
				{
					typeof(string),
					typeof(string),
					typeof(Action<string[]>),
					typeof(string),
					array.GetType()
				});
				if (method != null)
				{
					method.Invoke(null, new object[5] { name, category, handler, desc, array });
					Debug.Log((object)("[ChatResizer] Registered command /" + name + " in category " + category));
				}
			}
			catch (Exception ex)
			{
				Debug.LogWarning((object)("[ChatResizer] 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.chatresizer", "Chat Resizer", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public static ConfigEntry<float> ScaleMultiplier;

		public static ConfigEntry<float> FontSize;

		private static Transform chatPanelLocal;

		private static Transform chatPanelGlobal;

		private void Awake()
		{
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Expected O, but got Unknown
			ScaleMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Scale", 1f, "Chat scale multiplier (0.25 = quarter, 2.0 = double)");
			FontSize = ((BaseUnityPlugin)this).Config.Bind<float>("General", "FontSize", 16f, "Message font size");
			Harmony val = new Harmony("com.jai.ontogether.chatresizer");
			Type type = AccessTools.TypeByName("TextChannelManager");
			val.Patch((MethodBase)AccessTools.Method(type, "AddMessageUI", new Type[4]
			{
				typeof(string),
				typeof(string),
				typeof(bool),
				typeof(int)
			}, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "AddMessageUI_Postfix", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			CommandHelper.Register("resize-chat", "UI", HandleResizeChatCommand, "Usage: /resize-chat <scale> [fontSize]. Scale can be 0.25-2.0, font size can be 8-32.", new CommandHelper.Param("scale", "Float", optional: true), new CommandHelper.Param("fontSize", "Float", optional: true));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Chat Resizer loaded");
		}

		private static void SetupChatPanels()
		{
			UIManager i = MonoSingleton<UIManager>.I;
			if ((Object)(object)i == (Object)null)
			{
				return;
			}
			if ((Object)(object)i.TextContentLocalTransform != (Object)null)
			{
				Transform parent = i.TextContentLocalTransform.parent;
				Transform val = ((parent != null) ? parent.parent : null);
				if ((Object)(object)val != (Object)null)
				{
					chatPanelLocal = val.parent;
				}
			}
			if ((Object)(object)i.TextContentGlobalTransform != (Object)null)
			{
				Transform parent2 = i.TextContentGlobalTransform.parent;
				Transform val2 = ((parent2 != null) ? parent2.parent : null);
				if ((Object)(object)val2 != (Object)null)
				{
					chatPanelGlobal = val2.parent;
				}
			}
		}

		private static void HandleResizeChatCommand(string[] args)
		{
			if (args.Length == 0)
			{
				CommandHelper.Notify($"Current chat scale: {ScaleMultiplier.Value:P0}, font size: {FontSize.Value:F0}. Usage: /resize-chat 1.5 [fontsize]");
				return;
			}
			if ((Object)(object)chatPanelLocal == (Object)null || (Object)(object)chatPanelGlobal == (Object)null)
			{
				SetupChatPanels();
			}
			if (float.TryParse(args[0], out var result))
			{
				result = Mathf.Clamp(result, 0.25f, 2f);
				ScaleMultiplier.Value = result;
				if (args.Length >= 2 && float.TryParse(args[1], out var result2))
				{
					result2 = Mathf.Clamp(result2, 8f, 32f);
					FontSize.Value = result2;
					CommandHelper.Notify($"Chat resized: {result:P0} scale, {result2:F0}pt font");
				}
				else
				{
					CommandHelper.Notify($"Chat scale set to {result:P0}");
				}
				ApplyChatScale(result);
				((ConfigEntryBase)ScaleMultiplier).ConfigFile.Save();
				((ConfigEntryBase)FontSize).ConfigFile.Save();
			}
			else
			{
				CommandHelper.Notify("Invalid scale value. Usage: /resize-chat 1.5 [fontsize]");
			}
		}

		private static void ApplyChatScale(float scale)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			Vector3 localScale = default(Vector3);
			((Vector3)(ref localScale))..ctor(scale, scale, 1f);
			if ((Object)(object)chatPanelLocal != (Object)null)
			{
				chatPanelLocal.localScale = localScale;
			}
			if ((Object)(object)chatPanelGlobal != (Object)null)
			{
				chatPanelGlobal.localScale = localScale;
			}
		}

		private static void AddMessageUI_Postfix(string userName, string text, bool isLocal, int senderIndex)
		{
			try
			{
				UIManager i = MonoSingleton<UIManager>.I;
				if ((Object)(object)i == (Object)null)
				{
					return;
				}
				Transform val = (isLocal ? i.TextContentLocalTransform : i.TextContentGlobalTransform);
				if (!((Object)(object)val == (Object)null) && val.childCount != 0)
				{
					TMP_Text componentInChildren = ((Component)val.GetChild(val.childCount - 1)).gameObject.GetComponentInChildren<TMP_Text>(true);
					if ((Object)(object)componentInChildren != (Object)null)
					{
						componentInChildren.fontSize = FontSize.Value;
					}
				}
			}
			catch
			{
			}
		}
	}
}