Decompiled source of LCUltrawide Community v1.3.1

plugins/LCUltrawide_Community.dll

Decompiled 2 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.3.1.0")]
[assembly: AssemblyInformationalVersion("1.3.1+d3d7dbc5088ac2a280eb7ce65c6acc14fe6d58ea")]
[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.3.1.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.3.1";
}
namespace LCUltrawide
{
	[BepInPlugin("com.github.lethalcompanymodding.LCUltrawide", "LCUltrawide_Community", "1.3.1")]
	public class Plugin : BaseUnityPlugin
	{
		private const float aspectUpdateTime = 1f;

		private static float prevAspect;

		private static float prevTime;

		private const float fDefaultHelmetWidth = 0.3628f;

		private const int defaultScreenTexHeight = 520;

		private const int defaultTerminalTexHighResHeight = 580;

		private static ManualLogSource Log;

		private static ConfigEntry<float> GameCamResMultiplier { get; set; }

		private static ConfigEntry<float> TerminalResMultiplier { get; set; }

		private static ConfigEntry<float> ConfigUIScale { get; set; }

		private static ConfigEntry<float> ConfigUIAspect { get; set; }

		private void Awake()
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Expected O, but got Unknown
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Expected O, but got Unknown
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Expected O, but got Unknown
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Plugin com.github.lethalcompanymodding.LCUltrawide is loaded!");
			GameCamResMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Resolution Override", "Gameplay Camera Resolution Multiplier", 1f, new ConfigDescription("Use this to up or downscale your game camera rendering resolution.\nThe game's default gameplay camera rendering resolution of 860x520 is multiplied by the value in this configuration item.\nGame default value: 1\nWARNING: Increasing the multiplier is more costly on your PC's Hardware. Use with caution!", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 4f), Array.Empty<object>()));
			TerminalResMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Resolution Override", "Terminal Resolution Multiplier", 1f, new ConfigDescription("Use this to up or downscale your terminal rendering resolution.\nThe game's default terminal rendering resolution of 960x580 is multiplied by the value in this configuration item.\nGame default value: 1\nWARNING: Increasing the multiplier is more costly on your PC's Hardware. Use with caution!", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 4f), Array.Empty<object>()));
			ConfigUIScale = ((BaseUnityPlugin)this).Config.Bind<float>("UI", "Scale", 1f, new ConfigDescription("Changes the size of UI elements on the screen.\nGame default value: 1", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 1.5f), Array.Empty<object>()));
			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)");
			((BaseUnityPlugin)this).Config.SettingChanged += delegate
			{
				prevAspect = 0f;
				prevTime = 0f;
			};
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony.CreateAndPatchAll(typeof(Plugin), (string)null);
		}

		public static void ChangeAspectRatio(float newAspect)
		{
			//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_030c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0322: Unknown result type (might be due to invalid IL or missing references)
			//IL_0365: Unknown result type (might be due to invalid IL or missing references)
			//IL_036a: Unknown result type (might be due to invalid IL or missing references)
			//IL_038c: Unknown result type (might be due to invalid IL or missing references)
			Log.LogDebug((object)$"ChangeAspectRatio - {newAspect}");
			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;
			}
			Log.LogDebug((object)"Setting hudmanager playerScreenTexture.texture to preferred height & width");
			val.Release();
			((Texture)val).height = Convert.ToInt32(520f * GameCamResMultiplier.Value);
			((Texture)val).width = Convert.ToInt32((float)((Texture)val).height * newAspect);
			if ((Object)(object)instance.terminalScript != (Object)null)
			{
				RenderTexture playerScreenTexHighRes = instance.terminalScript.playerScreenTexHighRes;
				playerScreenTexHighRes.Release();
				((Texture)playerScreenTexHighRes).height = Convert.ToInt32(580f * TerminalResMultiplier.Value);
				((Texture)playerScreenTexHighRes).width = Convert.ToInt32((float)((Texture)playerScreenTexHighRes).height * newAspect);
				Log.LogDebug((object)"Setting Terminal playerScreenTexHighRes to preferred height & width");
			}
			Camera val2 = GameNetworkManager.Instance?.localPlayerController?.gameplayCamera;
			if (val2 == null)
			{
				Log.LogWarning((object)"Unable to acquire Game Camera, not resetting aspect ratio");
			}
			else
			{
				Log.LogDebug((object)"Resetting gameplayCamera aspect");
			}
			if (val2 != null)
			{
				val2.ResetAspect();
			}
			Transform parent = ((Component)instance.playerScreenTexture).transform.parent;
			Transform val3 = ((parent != null) ? parent.parent : null);
			AspectRatioFitter val4 = default(AspectRatioFitter);
			if ((Object)(object)val3 != (Object)null && ((Component)val3).TryGetComponent<AspectRatioFitter>(ref val4))
			{
				val4.aspectRatio = newAspect;
				Log.LogDebug((object)$"Updating UI/Canvas/Panel AspectRatioFitter aspectRatio to {newAspect}");
			}
			Transform val5 = ((val3 != null) ? val3.parent : null);
			CanvasScaler val6 = default(CanvasScaler);
			if ((Object)(object)val5 != (Object)null && ((Component)val5).gameObject.TryGetComponent<CanvasScaler>(ref val6))
			{
				float num = 500f / ConfigUIScale.Value;
				float num2 = num * newAspect;
				val6.referenceResolution = new Vector2(num2, num);
				Log.LogDebug((object)"Updating UI/Canvas CanvasScaler to preferred height/width");
			}
			GameObject hUDContainer = instance.HUDContainer;
			AspectRatioFitter val7 = default(AspectRatioFitter);
			if ((Object)(object)hUDContainer != (Object)null && hUDContainer.TryGetComponent<AspectRatioFitter>(ref val7))
			{
				val7.aspectRatio = ((ConfigUIAspect.Value > 0f) ? ConfigUIAspect.Value : newAspect);
				Log.LogDebug((object)"Updating HUDManager HUDContainer AspectRatioFitter to preferred height/width");
			}
			Camera uICamera = instance.UICamera;
			GameObject val8 = ((uICamera != null) ? ((Component)uICamera).gameObject : null);
			Camera val9 = default(Camera);
			if ((Object)(object)val8 != (Object)null && val8.TryGetComponent<Camera>(ref val9))
			{
				val9.fieldOfView = Math.Min(106f / ((ConfigUIAspect.Value > 0f) ? ConfigUIAspect.Value : newAspect), 60f);
				Log.LogDebug((object)"Updating Systems/UI/UICamera field of view to fix stretched HUD elements");
			}
			GameObject gameObject = ((Component)instance.Inventory.canvasGroup).gameObject;
			RectTransform val10 = default(RectTransform);
			if ((Object)(object)gameObject != (Object)null && gameObject.TryGetComponent<RectTransform>(ref val10))
			{
				val10.anchoredPosition = Vector2.zero;
				val10.anchorMax = new Vector2(0.5f, 0f);
				val10.anchorMin = new Vector2(0.5f, 0.5f);
				val10.pivot = new Vector2(0.5f, 0f);
				Log.LogDebug((object)"Updating HUDManager Inventory position for preferred resolution");
			}
			Transform parent2 = instance.helmetGoop.transform.parent;
			Transform val11 = ((parent2 != null) ? parent2.parent : null);
			if ((Object)(object)val11 != (Object)null)
			{
				Vector3 localScale = val11.localScale;
				localScale.x = 0.3628f * Math.Max(newAspect / 2.3f, 1f);
				val11.localScale = localScale;
				Log.LogDebug((object)"Updating PlayerHUDHelmetModel transform scale width for preferred resolution");
			}
		}

		[HarmonyPatch(typeof(HUDManager), "Start")]
		[HarmonyPostfix]
		private static void HUDManagerStart()
		{
			prevAspect = 0f;
			prevTime = 0f;
		}

		[HarmonyPatch(typeof(HUDManager), "Update")]
		[HarmonyPostfix]
		private static void HUDManagerUpdate(HUDManager __instance)
		{
			//IL_001d: 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_0023: 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)
			if (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;
					Log.LogDebug((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);
				}
			}
		}

		[HarmonyPatch(typeof(IngamePlayerSettings), "SetPixelResolution")]
		[HarmonyPrefix]
		private static bool SettingsPixelRes(int value)
		{
			Log.LogDebug((object)$"NOT setting PixelResolution to [{value}]");
			return false;
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}