Decompiled source of HDLethalCompany v1.5.6

BepInEx/plugins/HDLethalCompany.dll

Decompiled 5 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using GameNetcodeStuff;
using HDLethalCompany.Patch;
using HDLethalCompany.Tools;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("HDLethalCompanyRemake")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HDLethalCompanyRemake")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7f379bc1-1fd4-4c72-9247-a181862eec6b")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace HDLethalCompany
{
	[BepInPlugin("HDLethalCompany", "HDLethalCompany-Sligili", "1.5.6")]
	public class HDLethalCompanyInitialization : BaseUnityPlugin
	{
		private static ConfigEntry<float> config_ResMult;

		private static ConfigEntry<bool> config_EnablePostProcessing;

		private static ConfigEntry<bool> config_EnableFog;

		private static ConfigEntry<bool> config_EnableAntialiasing;

		private static ConfigEntry<bool> config_EnableResolution;

		private static ConfigEntry<bool> config_EnableFoliage;

		private static ConfigEntry<int> config_FogQuality;

		private static ConfigEntry<int> config_TextureQuality;

		private static ConfigEntry<int> config_LOD;

		private static ConfigEntry<int> config_ShadowmapQuality;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Expected O, but got Unknown
			((BaseUnityPlugin)this).Logger.LogInfo((object)"HDLethalCompany loaded");
			ConfigFile();
			GraphicsPatch.assetBundle = AssetBundle.LoadFromFile(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "HDLethalCompany/hdlethalcompany"));
			_harmony = new Harmony("HDLethalCompany");
			_harmony.PatchAll(typeof(GraphicsPatch));
		}

		private void ConfigFile()
		{
			config_ResMult = ((BaseUnityPlugin)this).Config.Bind<float>("RESOLUTION", "Value", 2.233f, "Resolution Scale Multiplier - <EXAMPLES -> | 1.000 = 860x520p | 2.233 =~ 1920x1080p | 2.977 = 2560x1440p | 4.465 = 3840x2060p > - The UI scanned elements have slightly incorrect offsets after 3.000");
			config_EnableResolution = ((BaseUnityPlugin)this).Config.Bind<bool>("RESOLUTION", "EnableRes", true, "Resolution Fix - In case you wanna use another resolution mod or apply any widescreen mod while keeping the graphics settings");
			config_EnableAntialiasing = ((BaseUnityPlugin)this).Config.Bind<bool>("EFFECTS", "EnableAA", false, "Anti-Aliasing (Unity's SMAA)");
			config_EnablePostProcessing = ((BaseUnityPlugin)this).Config.Bind<bool>("EFFECTS", "EnablePP", true, "Post-Processing (Color grading)");
			config_TextureQuality = ((BaseUnityPlugin)this).Config.Bind<int>("EFFECTS", "TextureQuality", 3, "Texture Resolution Quality - <PRESETS -> | 0 = VERY LOW (1/8) | 1 = LOW (1/4) | 2 = MEDIUM (1/2) | 3 = HIGH (1/1 VANILLA) >");
			config_FogQuality = ((BaseUnityPlugin)this).Config.Bind<int>("EFFECTS", "FogQuality", 1, "Volumetric Fog Quality - <PRESETS -> | 0 = VERY LOW | 1 = VANILLA FOG | 2 = MEDIUM | 3 = HIGH >");
			config_EnableFog = ((BaseUnityPlugin)this).Config.Bind<bool>("EFFECTS", "EnableFOG", true, "Volumetric Fog Toggle - Use this as a last resource in case lowering the fog quality is not enough to get decent performance");
			config_LOD = ((BaseUnityPlugin)this).Config.Bind<int>("EFFECTS", "LOD", 1, "Level Of Detail - <PRESETS -> | 0 = LOW (HALF DISTANCE) | 1 = VANILLA | 2 = HIGH (TWICE THE DISTANCE) >");
			config_ShadowmapQuality = ((BaseUnityPlugin)this).Config.Bind<int>("EFFECTS", "ShadowQuality", 3, "Shadows Resolution - <PRESETS -> 0 = VERY LOW (SHADOWS DISABLED)| 1 = LOW (256) | 2 = MEDIUM (1024) | 3 = VANILLA (2048) > - Shadowmap max resolution");
			config_EnableFoliage = ((BaseUnityPlugin)this).Config.Bind<bool>("EFFECTS", "EnableF", true, "Foliage Toggle - If the game camera should or not render bushes/grass (trees won't be affected)");
			GraphicsPatch.m_enableFoliage = config_EnableFoliage.Value;
			GraphicsPatch.m_enableResolutionFix = config_EnableResolution.Value;
			GraphicsPatch.m_setShadowQuality = config_ShadowmapQuality.Value;
			GraphicsPatch.m_setLOD = config_LOD.Value;
			GraphicsPatch.m_setTextureResolution = config_TextureQuality.Value;
			GraphicsPatch.m_setFogQuality = config_FogQuality.Value;
			GraphicsPatch.multiplier = config_ResMult.Value;
			GraphicsPatch.anchorOffsetZ = 0.123f * config_ResMult.Value + 0.877f;
			GraphicsPatch.m_widthResolution = 860f * config_ResMult.Value;
			GraphicsPatch.m_heightResolution = 520f * config_ResMult.Value;
			GraphicsPatch.m_enableAntiAliasing = config_EnableAntialiasing.Value;
			GraphicsPatch.m_enableFog = config_EnableFog.Value;
			GraphicsPatch.m_enablePostProcessing = config_EnablePostProcessing.Value;
		}
	}
	public static class PluginInfo
	{
		public const string Guid = "HDLethalCompany";

		public const string Name = "HDLethalCompany-Sligili";

		public const string Ver = "1.5.6";
	}
}
namespace HDLethalCompany.Tools
{
	public class Reflection
	{
		public static object GetInstanceField(Type type, object instance, string fieldName)
		{
			BindingFlags bindingAttr = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
			FieldInfo field = type.GetField(fieldName, bindingAttr);
			return field.GetValue(instance);
		}

		public static object CallMethod(object instance, string methodName, params object[] args)
		{
			MethodInfo method = instance.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
			if (method != null)
			{
				return method.Invoke(instance, args);
			}
			return null;
		}
	}
}
namespace HDLethalCompany.Patch
{
	internal class GraphicsPatch
	{
		public static bool m_enablePostProcessing;

		public static bool m_enableFog;

		public static bool m_enableAntiAliasing;

		public static bool m_enableResolutionFix;

		public static bool m_enableFoliage = true;

		public static int m_setFogQuality;

		public static int m_setTextureResolution;

		public static int m_setLOD;

		public static int m_setShadowQuality;

		private static HDRenderPipelineAsset myAsset;

		public static AssetBundle assetBundle;

		public static float anchorOffsetX = 439.48f;

		public static float anchorOffsetY = 244.8f;

		public static float anchorOffsetZ;

		public static float multiplier;

		public static float m_widthResolution;

		public static float m_heightResolution;

		[HarmonyPatch(typeof(PlayerControllerB), "Start")]
		[HarmonyPrefix]
		private static void StartPrefix(PlayerControllerB __instance)
		{
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			Object[] array = Resources.FindObjectsOfTypeAll(typeof(HDAdditionalCameraData));
			foreach (Object obj in array)
			{
				HDAdditionalCameraData val = (HDAdditionalCameraData)(object)((obj is HDAdditionalCameraData) ? obj : null);
				if (!(((Object)((Component)val).gameObject).name == "MapCamera"))
				{
					val.customRenderingSettings = true;
					ToggleCustomPass(val, m_enablePostProcessing);
					SetLevelOfDetail(val);
					ToggleVolumetricFog(val, m_enableFog);
					if (!m_enableFoliage)
					{
						LayerMask val2 = LayerMask.op_Implicit(((Component)val).GetComponent<Camera>().cullingMask);
						val2 = LayerMask.op_Implicit(LayerMask.op_Implicit(val2) & -1025);
						((Component)val).GetComponent<Camera>().cullingMask = LayerMask.op_Implicit(val2);
					}
					SetShadowQuality(assetBundle, val);
					if (!(((Object)((Component)val).gameObject).name == "SecurityCamera") && !(((Object)((Component)val).gameObject).name == "ShipCamera"))
					{
						SetAntiAliasing(val);
					}
				}
			}
			array = null;
			SetTextureQuality();
			SetFogQuality();
			if (m_enableResolutionFix && multiplier != 1f)
			{
				int width = (int)Math.Round(m_widthResolution, 0);
				int height = (int)Math.Round(m_heightResolution, 0);
				((Texture)__instance.gameplayCamera.targetTexture).width = width;
				((Texture)__instance.gameplayCamera.targetTexture).height = height;
			}
		}

		[HarmonyPatch(typeof(RoundManager), "GenerateNewFloor")]
		[HarmonyPrefix]
		private static void RoundPostFix()
		{
			SetFogQuality();
			if (m_setLOD == 0)
			{
				RemoveLodFromGameObject("CatwalkStairs");
			}
		}

		[HarmonyPatch(typeof(HUDManager), "UpdateScanNodes")]
		[HarmonyPostfix]
		private static void UpdateScanNodesPostfix(PlayerControllerB playerScript, HUDManager __instance)
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0249: Unknown result type (might be due to invalid IL or missing references)
			//IL_024e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0253: Unknown result type (might be due to invalid IL or missing references)
			//IL_0264: Unknown result type (might be due to invalid IL or missing references)
			//IL_0276: Unknown result type (might be due to invalid IL or missing references)
			//IL_028b: 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_02b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c7: 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_02fe: Unknown result type (might be due to invalid IL or missing references)
			if (anchorOffsetZ > 1.238f)
			{
				anchorOffsetZ = 1.238f;
			}
			if (!m_enableResolutionFix || multiplier == 1f)
			{
				return;
			}
			Vector3 zero = Vector3.zero;
			bool flag = false;
			for (int i = 0; i < __instance.scanElements.Length; i++)
			{
				if ((Traverse.Create((object)__instance).Field("scanNodes").GetValue() as Dictionary<RectTransform, ScanNodeProperties>).Count > 0 && (Traverse.Create((object)__instance).Field("scanNodes").GetValue() as Dictionary<RectTransform, ScanNodeProperties>).TryGetValue(__instance.scanElements[i], out var value) && (Object)(object)value != (Object)null)
				{
					try
					{
						if ((bool)Reflection.CallMethod(__instance, "NodeIsNotVisible", value, i))
						{
							continue;
						}
						if (!((Component)__instance.scanElements[i]).gameObject.activeSelf)
						{
							((Component)__instance.scanElements[i]).gameObject.SetActive(true);
							((Component)__instance.scanElements[i]).GetComponent<Animator>().SetInteger("colorNumber", value.nodeType);
							if (value.creatureScanID != -1)
							{
								Traverse.Create((object)__instance).Method("AttemptScanNewCreature", new object[1] { value.creatureScanID });
							}
						}
						goto IL_0186;
					}
					catch (Exception arg)
					{
						Debug.LogError((object)$"Error in updatescanNodes A: {arg}");
						goto IL_0186;
					}
				}
				(Traverse.Create((object)__instance).Field("scanNodes").GetValue() as Dictionary<RectTransform, ScanNodeProperties>).Remove(__instance.scanElements[i]);
				((Component)__instance.scanElements[i]).gameObject.SetActive(false);
				continue;
				IL_0186:
				try
				{
					Traverse.Create((object)__instance).Field("scanElementText").SetValue((object)((Component)__instance.scanElements[i]).gameObject.GetComponentsInChildren<TextMeshProUGUI>());
					if ((Traverse.Create((object)__instance).Field("scanElementText").GetValue() as TextMeshProUGUI[]).Length > 1)
					{
						((TMP_Text)(Traverse.Create((object)__instance).Field("scanElementText").GetValue() as TextMeshProUGUI[])[0]).text = value.headerText;
						((TMP_Text)(Traverse.Create((object)__instance).Field("scanElementText").GetValue() as TextMeshProUGUI[])[1]).text = value.subText;
					}
					if (value.nodeType == 2)
					{
						flag = true;
					}
					zero = playerScript.gameplayCamera.WorldToScreenPoint(((Component)value).transform.position);
					((Transform)__instance.scanElements[i]).position = new Vector3(((Transform)__instance.scanElements[i]).position.x, ((Transform)__instance.scanElements[i]).position.y, 12.17f * anchorOffsetZ);
					__instance.scanElements[i].anchoredPosition = Vector2.op_Implicit(new Vector3(zero.x - anchorOffsetX * multiplier, zero.y - anchorOffsetY * multiplier));
					if (!(multiplier > 3f))
					{
						((Transform)__instance.scanElements[i]).localScale = new Vector3(multiplier, multiplier, multiplier);
					}
					else
					{
						((Transform)__instance.scanElements[i]).localScale = new Vector3(3f, 3f, 3f);
					}
				}
				catch (Exception arg2)
				{
					Debug.LogError((object)$"Error in updatescannodes B: {arg2}");
				}
			}
			try
			{
				if (!flag)
				{
					__instance.totalScrapScanned = 0;
					Traverse.Create((object)__instance).Field("totalScrapScannedDisplayNum").SetValue((object)0);
					Traverse.Create((object)__instance).Field("addToDisplayTotalInterval").SetValue((object)0.35f);
				}
				__instance.scanInfoAnimator.SetBool("display", (int)Reflection.GetInstanceField(typeof(HUDManager), __instance, "scannedScrapNum") >= 2 && flag);
			}
			catch (Exception arg3)
			{
				Debug.LogError((object)$"Error in updatescannodes C: {arg3}");
			}
		}

		public static void SetShadowQuality(AssetBundle assetBundle, HDAdditionalCameraData cameraData)
		{
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)assetBundle == (Object)null)
			{
				Debug.LogError((object)"HDLETHALCOMPANY: Something is wrong with the Asset Bundle - Null");
				return;
			}
			((BitArray128)(ref cameraData.renderingPathCustomFrameSettingsOverrideMask.mask))[20u] = true;
			((FrameSettings)(ref cameraData.renderingPathCustomFrameSettings)).SetEnabled((FrameSettingsField)20, (m_setShadowQuality != 0) ? true : false);
			myAsset = (HDRenderPipelineAsset)((m_setShadowQuality != 1) ? ((m_setShadowQuality != 2) ? ((object)(HDRenderPipelineAsset)QualitySettings.renderPipeline) : ((object)(myAsset = assetBundle.LoadAsset<HDRenderPipelineAsset>("Assets/HDLethalCompany/MediumShadowsAsset.asset")))) : (myAsset = assetBundle.LoadAsset<HDRenderPipelineAsset>("Assets/HDLethalCompany/VeryLowShadowsAsset.asset")));
			QualitySettings.renderPipeline = (RenderPipelineAsset)(object)myAsset;
		}

		public static void SetLevelOfDetail(HDAdditionalCameraData cameraData)
		{
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			if (m_setLOD != 1)
			{
				((BitArray128)(ref cameraData.renderingPathCustomFrameSettingsOverrideMask.mask))[60u] = true;
				((BitArray128)(ref cameraData.renderingPathCustomFrameSettingsOverrideMask.mask))[61u] = true;
				((FrameSettings)(ref cameraData.renderingPathCustomFrameSettings)).SetEnabled((FrameSettingsField)60, true);
				((FrameSettings)(ref cameraData.renderingPathCustomFrameSettings)).SetEnabled((FrameSettingsField)61, true);
				cameraData.renderingPathCustomFrameSettings.lodBiasMode = (LODBiasMode)2;
				cameraData.renderingPathCustomFrameSettings.lodBias = ((m_setLOD == 0) ? 0.6f : 2.3f);
				if (m_setLOD == 0 && ((Component)cameraData).GetComponent<Camera>().farClipPlane > 180f)
				{
					((Component)cameraData).GetComponent<Camera>().farClipPlane = 170f;
				}
			}
		}

		public static void SetTextureQuality()
		{
			if (m_setTextureResolution < 3)
			{
				int globalTextureMipmapLimit = 3 - m_setTextureResolution;
				QualitySettings.globalTextureMipmapLimit = globalTextureMipmapLimit;
			}
		}

		public static void SetAntiAliasing(HDAdditionalCameraData cameraData)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			if (m_enableAntiAliasing)
			{
				cameraData.antialiasing = (AntialiasingMode)3;
			}
		}

		public static void ToggleCustomPass(HDAdditionalCameraData cameraData, bool enable)
		{
			((BitArray128)(ref cameraData.renderingPathCustomFrameSettingsOverrideMask.mask))[6u] = true;
			((FrameSettings)(ref cameraData.renderingPathCustomFrameSettings)).SetEnabled((FrameSettingsField)6, enable);
		}

		public static void ToggleVolumetricFog(HDAdditionalCameraData cameraData, bool enable)
		{
			((BitArray128)(ref cameraData.renderingPathCustomFrameSettingsOverrideMask.mask))[28u] = true;
			((FrameSettings)(ref cameraData.renderingPathCustomFrameSettings)).SetEnabled((FrameSettingsField)28, enable);
		}

		public static void SetFogQuality()
		{
			Object[] array = Resources.FindObjectsOfTypeAll(typeof(Volume));
			if (array.Length == 0)
			{
				Debug.LogError((object)"No volumes found");
				return;
			}
			Fog val2 = default(Fog);
			foreach (Object obj in array)
			{
				Volume val = (Volume)(object)((obj is Volume) ? obj : null);
				if (val.sharedProfile.TryGet<Fog>(ref val2))
				{
					((VolumeParameter<int>)(object)((VolumeComponentWithQuality)val2).quality).Override(3);
					switch (m_setFogQuality)
					{
					case -1:
						val2.volumetricFogBudget = 0.05f;
						val2.resolutionDepthRatio = 0.5f;
						break;
					case 0:
						val2.volumetricFogBudget = 0.05f;
						val2.resolutionDepthRatio = 0.5f;
						break;
					case 2:
						val2.volumetricFogBudget = 0.333f;
						val2.resolutionDepthRatio = 0.666f;
						break;
					case 3:
						val2.volumetricFogBudget = 0.666f;
						val2.resolutionDepthRatio = 0.5f;
						break;
					}
				}
			}
		}

		public static void RemoveLodFromGameObject(string name)
		{
			Object[] array = Resources.FindObjectsOfTypeAll(typeof(LODGroup));
			foreach (Object obj in array)
			{
				LODGroup val = (LODGroup)(object)((obj is LODGroup) ? obj : null);
				if (((Object)((Component)val).gameObject).name == name)
				{
					val.enabled = false;
				}
			}
		}
	}
}