Decompiled source of LCUltrawide Community v1.1.3

plugins/LCUltrawide_Community.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyCompany("LethalCompanyModding")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription(" Ultrawide monitor support for the game Lethal Company ")]
[assembly: AssemblyFileVersion("1.1.3.0")]
[assembly: AssemblyInformationalVersion("1.1.3+7261c674454ef4f9927f1fa08ce785c77184bc8c")]
[assembly: AssemblyProduct("LCUltrawide_Community")]
[assembly: AssemblyTitle("com.github.lethalcompanymodding.LCUltrawide")]
[assembly: AssemblyMetadata("RepositoryUrl", "https://github.com/LethalCompanyModding/LCUltrawide")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.3.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.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]
	[Microsoft.CodeAnalysis.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]
	[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;
		}
	}
}
internal static class LCMPluginInfo
{
	public const string PLUGIN_GUID = "com.github.lethalcompanymodding.LCUltrawide";

	public const string PLUGIN_NAME = "LCUltrawide_Community";

	public const string PLUGIN_VERSION = "1.1.3";
}
namespace LCUltrawide
{
	[BepInPlugin("com.github.lethalcompanymodding.LCUltrawide", "LCUltrawide_Community", "1.1.3")]
	public class Plugin : BaseUnityPlugin
	{
		private static ConfigEntry<int> configResW;

		private static ConfigEntry<int> configResH;

		private static ConfigEntry<float> configUIScale;

		private static ConfigEntry<float> configUIAspect;

		internal static ManualLogSource Log;

		private const float aspectUpdateTime = 1f;

		private static bool aspectAutoDetect;

		private static float prevAspect;

		private static float prevTime;

		private const float fDefaultHelmetWidth = 0.3628f;

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin com.github.lethalcompanymodding.LCUltrawide is loaded!");
			configResW = ((BaseUnityPlugin)this).Config.Bind<int>("Resolution Override", "Width", 0, "Horizontal rendering resolution override.\nIf set to 0, the resolution will be automatically adjusted to fit your monitors aspect ratio.\nGame default value: 860");
			configResH = ((BaseUnityPlugin)this).Config.Bind<int>("Resolution Override", "Height", 0, "Vertical rendering resolution override.\nIf set to 0, the original resolution will be used.\nGame default value: 520");
			configUIScale = ((BaseUnityPlugin)this).Config.Bind<float>("UI", "Scale", 1f, "Changes the size of UI elements on the screen.");
			configUIAspect = ((BaseUnityPlugin)this).Config.Bind<float>("UI", "AspectRatio", 0f, "Changes the aspect ratio of the in-game HUD, a higher number makes the HUD wider.\n(0 = auto, 1.33 = 4:3, 1.77 = 16:9, 2.33 = 21:9, 3.55 = 32:9)");
			aspectAutoDetect = configResW.Value <= 0;
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony.CreateAndPatchAll(typeof(Plugin), (string)null);
		}

		public static void ChangeAspectRatio(float newAspect)
		{
			//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0288: Unknown result type (might be due to invalid IL or missing references)
			//IL_029e: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_031e: Unknown result type (might be due to invalid IL or missing references)
			HUDManager instance = HUDManager.Instance;
			if (instance == null)
			{
				Log.LogError((object)"Unable to access hudManager");
				return;
			}
			Texture texture = instance.playerScreenTexture.texture;
			RenderTexture val = (RenderTexture)(object)((texture is RenderTexture) ? texture : null);
			if (val == null)
			{
				Log.LogError((object)"Unable to read player screen texture");
				return;
			}
			val.Release();
			((Texture)val).height = ((configResH.Value > 0) ? configResH.Value : ((Texture)val).height);
			((Texture)val).width = ((configResW.Value > 0) ? configResW.Value : Convert.ToInt32((float)((Texture)val).height * newAspect));
			GameObject val2 = GameObject.Find("TerminalScript");
			Terminal val3 = default(Terminal);
			if ((Object)(object)val2 != (Object)null && val2.TryGetComponent<Terminal>(ref val3))
			{
				RenderTexture playerScreenTexHighRes = val3.playerScreenTexHighRes;
				playerScreenTexHighRes.Release();
				((Texture)playerScreenTexHighRes).height = ((configResH.Value > 0) ? configResH.Value : ((Texture)playerScreenTexHighRes).height);
				((Texture)playerScreenTexHighRes).width = ((configResW.Value > 0) ? configResW.Value : Convert.ToInt32((float)((Texture)playerScreenTexHighRes).height * newAspect));
			}
			Camera val4 = GameNetworkManager.Instance?.localPlayerController?.gameplayCamera;
			if (val4 == null)
			{
				Log.LogWarning((object)"Unable to acquire Game Camera, not resetting aspect ratio");
			}
			if (val4 != null)
			{
				val4.ResetAspect();
			}
			GameObject val5 = GameObject.Find("Systems/UI/Canvas/Panel");
			AspectRatioFitter val6 = default(AspectRatioFitter);
			if ((Object)(object)val5 != (Object)null && val5.TryGetComponent<AspectRatioFitter>(ref val6))
			{
				val6.aspectRatio = newAspect;
			}
			GameObject val7 = GameObject.Find("Systems/UI/Canvas");
			CanvasScaler val8 = default(CanvasScaler);
			if ((Object)(object)val7 != (Object)null && val7.TryGetComponent<CanvasScaler>(ref val8))
			{
				float num = 500f / configUIScale.Value;
				float num2 = num * newAspect;
				val8.referenceResolution = new Vector2(num2, num);
			}
			GameObject hUDContainer = instance.HUDContainer;
			AspectRatioFitter val9 = default(AspectRatioFitter);
			if ((Object)(object)hUDContainer != (Object)null && hUDContainer.TryGetComponent<AspectRatioFitter>(ref val9))
			{
				val9.aspectRatio = ((configUIAspect.Value > 0f) ? configUIAspect.Value : newAspect);
			}
			GameObject val10 = GameObject.Find("Systems/UI/UICamera");
			Camera val11 = default(Camera);
			if ((Object)(object)val10 != (Object)null && val10.TryGetComponent<Camera>(ref val11))
			{
				val11.fieldOfView = Math.Min(106f / ((configUIAspect.Value > 0f) ? configUIAspect.Value : newAspect), 60f);
			}
			GameObject gameObject = ((Component)instance.Inventory.canvasGroup).gameObject;
			RectTransform val12 = default(RectTransform);
			if ((Object)(object)gameObject != (Object)null && gameObject.TryGetComponent<RectTransform>(ref val12))
			{
				val12.anchoredPosition = Vector2.zero;
				val12.anchorMax = new Vector2(0.5f, 0f);
				val12.anchorMin = new Vector2(0.5f, 0.5f);
				val12.pivot = new Vector2(0.5f, 0f);
			}
			GameObject val13 = GameObject.Find("PlayerHUDHelmetModel");
			Transform val14 = default(Transform);
			if ((Object)(object)val13 != (Object)null && val13.TryGetComponent<Transform>(ref val14))
			{
				Vector3 localScale = val14.localScale;
				localScale.x = 0.3628f * Math.Max(newAspect / 2.3f, 1f);
				val14.localScale = localScale;
			}
		}

		[HarmonyPatch(typeof(HUDManager), "Start")]
		[HarmonyPostfix]
		private static void HUDManagerStart(HUDManager __instance)
		{
			if (!aspectAutoDetect)
			{
				ChangeAspectRatio(1.77f);
			}
			prevAspect = 0f;
			prevTime = 0f;
		}

		[HarmonyPatch(typeof(HUDManager), "Update")]
		[HarmonyPostfix]
		private static void HUDManagerUpdate(HUDManager __instance)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			if (aspectAutoDetect && Time.time > prevTime + 1f)
			{
				Vector2 renderingDisplaySize = ((Graphic)__instance.playerScreenTexture).canvas.renderingDisplaySize;
				float num = renderingDisplaySize.x / renderingDisplaySize.y;
				if (!Mathf.Approximately(num, prevAspect))
				{
					ChangeAspectRatio(num);
					prevAspect = num;
					Debug.Log((object)("New Aspect Ratio: " + num));
				}
				prevTime = Time.time;
			}
		}

		[HarmonyPatch(typeof(HUDManager), "UpdateScanNodes")]
		[HarmonyPostfix]
		private static void HUDManagerUpdateScanNodes(PlayerControllerB playerScript, HUDManager __instance, Dictionary<RectTransform, ScanNodeProperties> ___scanNodes)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			RectTransform[] scanElements = __instance.scanElements;
			GameObject gameObject = ((Component)__instance.playerScreenTexture).gameObject;
			RectTransform val = default(RectTransform);
			if (!gameObject.TryGetComponent<RectTransform>(ref val))
			{
				return;
			}
			Rect rect = val.rect;
			for (int i = 0; i < scanElements.Length; i++)
			{
				if (___scanNodes.TryGetValue(scanElements[i], out ScanNodeProperties value))
				{
					Vector3 val2 = playerScript.gameplayCamera.WorldToViewportPoint(((Component)value).transform.position);
					scanElements[i].anchoredPosition = new Vector2(((Rect)(ref rect)).xMin + ((Rect)(ref rect)).width * val2.x, ((Rect)(ref rect)).yMin + ((Rect)(ref rect)).height * val2.y);
				}
			}
		}
	}
}