Decompiled source of OriginalFogOverhaul v1.1.1

BepInEx/plugins/OriginalFogOverhaul/OriginalFogOverhaul.dll

Decompiled 6 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Rendering;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("OriginalFogOverhaul")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: AssemblyInformationalVersion("1.1.1")]
[assembly: AssemblyProduct("OriginalFogOverhaul")]
[assembly: AssemblyTitle("OriginalFogOverhaul")]
[assembly: AssemblyVersion("1.1.1.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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;
		}
	}
}
namespace FogDensity65
{
	[HarmonyPatch(typeof(PreLitVolume), "CollectCommands", new Type[] { typeof(CommandBuffer) })]
	internal static class FogQualityPatch
	{
		internal struct SavedQuality
		{
			internal int Mask;

			internal int Aa;

			internal int Blur;

			internal int Post;

			internal int AppliedAa;

			internal int AppliedBlur;

			internal int AppliedPost;
		}

		[HarmonyPrefix]
		[HarmonyPriority(0)]
		internal static void Prefix(PreLitVolume __instance, out SavedQuality __state)
		{
			__state = default(SavedQuality);
			try
			{
				if (!((Object)(object)__instance == (Object)null) && !Plugin.Faulted && Plugin.ModEnabled.Value && Plugin.QualityEnabled.Value)
				{
					__state.Aa = __instance.FogAACount;
					__state.Blur = __instance.IndirectBlurSamples;
					__state.Post = __instance.FogPostBlur;
					__state.AppliedAa = Math.Min(__state.Aa, Math.Clamp(Plugin.MaxAaSamples.Value, 1, 8));
					__state.AppliedBlur = Math.Min(__state.Blur, Math.Clamp(Plugin.MaxIndirectBlurSamples.Value, 1, 5));
					__state.AppliedPost = ((!Plugin.DisablePostBlur.Value) ? __state.Post : 0);
					if (__state.Aa > 0 && __state.Aa != __state.AppliedAa)
					{
						__state.Mask |= 1;
						__instance.FogAACount = __state.AppliedAa;
					}
					if (__state.Blur > 0 && __state.Blur != __state.AppliedBlur)
					{
						__state.Mask |= 2;
						__instance.IndirectBlurSamples = __state.AppliedBlur;
					}
					if (__state.Post > 0 && __state.Post != __state.AppliedPost)
					{
						__state.Mask |= 4;
						__instance.FogPostBlur = __state.AppliedPost;
					}
				}
			}
			catch (Exception ex)
			{
				Restore(__instance, __state);
				__state = default(SavedQuality);
				Plugin.Fail(ex);
			}
		}

		[HarmonyFinalizer]
		internal static Exception? Finalizer(PreLitVolume __instance, SavedQuality __state, Exception? __exception)
		{
			Restore(__instance, __state);
			return __exception;
		}

		private static void Restore(PreLitVolume volume, SavedQuality saved)
		{
			if (saved.Mask == 0 || (Object)(object)volume == (Object)null)
			{
				return;
			}
			try
			{
				if ((saved.Mask & 1) != 0 && volume.FogAACount == saved.AppliedAa)
				{
					volume.FogAACount = saved.Aa;
				}
			}
			catch (Exception ex)
			{
				Plugin.Fail(ex);
			}
			try
			{
				if ((saved.Mask & 2) != 0 && volume.IndirectBlurSamples == saved.AppliedBlur)
				{
					volume.IndirectBlurSamples = saved.Blur;
				}
			}
			catch (Exception ex2)
			{
				Plugin.Fail(ex2);
			}
			try
			{
				if ((saved.Mask & 4) != 0 && volume.FogPostBlur == saved.AppliedPost)
				{
					volume.FogPostBlur = saved.Post;
				}
			}
			catch (Exception ex3)
			{
				Plugin.Fail(ex3);
			}
		}
	}
	[BepInPlugin("local.gtfo.fogdensity65", "OriginalFogOverhaul", "1.1.1")]
	[BepInIncompatibility("LegacyFogSystem")]
	public sealed class Plugin : BasePlugin
	{
		public const string Id = "local.gtfo.fogdensity65";

		internal static ConfigEntry<bool> ModEnabled;

		internal static ConfigEntry<int> DensityPercent;

		internal static ConfigEntry<bool> QualityEnabled;

		internal static ConfigEntry<int> MaxAaSamples;

		internal static ConfigEntry<int> MaxIndirectBlurSamples;

		internal static ConfigEntry<bool> DisablePostBlur;

		internal static ManualLogSource Logger;

		internal static bool Faulted;

		public override void Load()
		{
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Expected O, but got Unknown
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Expected O, but got Unknown
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Expected O, but got Unknown
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_011c: Expected O, but got Unknown
			//IL_022d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0234: Expected O, but got Unknown
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Expected O, but got Unknown
			Logger = ((BasePlugin)this).Log;
			ModEnabled = ((BasePlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable this mod. Restart the game after changing settings.");
			DensityPercent = ((BasePlugin)this).Config.Bind<int>("General", "DensityPercent", 65, new ConfigDescription("Fog density to keep (%). 100 = original. Does not change infection.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 100), Array.Empty<object>()));
			QualityEnabled = ((BasePlugin)this).Config.Bind<bool>("FogQuality", "Enabled", true, "Enable the fog quality settings below. No update rate limit.");
			MaxAaSamples = ((BasePlugin)this).Config.Bind<int>("FogQuality", "MaxAaSamples", 1, new ConfigDescription("Fog anti-aliasing sample limit. Lower = less detail.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 8), Array.Empty<object>()));
			MaxIndirectBlurSamples = ((BasePlugin)this).Config.Bind<int>("FogQuality", "MaxIndirectBlurSamples", 1, new ConfigDescription("Indirect fog blur limit. 1 and 2 use the same lowest setting.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 5), Array.Empty<object>()));
			DisablePostBlur = ((BasePlugin)this).Config.Bind<bool>("FogQuality", "DisablePostBlur", true, "Disable fog post-blur. May look grainy or flicker.");
			((BasePlugin)this).Config.Save();
			Faulted = false;
			Harmony val = new Harmony("local.gtfo.fogdensity65");
			bool flag = default(bool);
			try
			{
				val.PatchAll(typeof(FogGlobalsPatch));
				val.PatchAll(typeof(FogHeightPatch));
				val.PatchAll(typeof(FogQualityPatch));
				ManualLogSource log = ((BasePlugin)this).Log;
				BepInExInfoLogInterpolatedStringHandler val2 = new BepInExInfoLogInterpolatedStringHandler(157, 6, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("OriginalFogOverhaul 1.1.1 loaded. Enabled=");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<bool>(ModEnabled.Value);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(", DensityPercent=");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(DensityPercent.Value);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(", QualityEnabled=");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<bool>(QualityEnabled.Value);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(", MaxAaSamples=");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(MaxAaSamples.Value);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(", MaxIndirectBlurSamples=");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<int>(MaxIndirectBlurSamples.Value);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(", DisablePostBlur=");
					((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<bool>(DisablePostBlur.Value);
					((BepInExLogInterpolatedStringHandler)val2).AppendLiteral(". No update throttling.");
				}
				log.LogInfo(val2);
			}
			catch (Exception ex)
			{
				val.UnpatchSelf();
				ManualLogSource log2 = ((BasePlugin)this).Log;
				BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(47, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("OriginalFogOverhaul could not patch; disabled. ");
					((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<Exception>(ex);
				}
				log2.LogError(val3);
			}
		}

		internal static bool TryGetScale(out float scale)
		{
			scale = 1f;
			if (Faulted || !ModEnabled.Value || DensityPercent.Value >= 100)
			{
				return false;
			}
			scale = (float)Math.Clamp(DensityPercent.Value, 1, 100) / 100f;
			return true;
		}

		internal static void Fail(Exception ex)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Expected O, but got Unknown
			if (!Faulted)
			{
				Faulted = true;
				ManualLogSource logger = Logger;
				bool flag = default(bool);
				BepInExErrorLogInterpolatedStringHandler val = new BepInExErrorLogInterpolatedStringHandler(101, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val).AppendLiteral("OriginalFogOverhaul stopped applying overrides after an error. Restart with Enabled=false if needed. ");
					((BepInExLogInterpolatedStringHandler)val).AppendFormatted<Exception>(ex);
				}
				logger.LogError(val);
			}
		}
	}
	[HarmonyPatch(typeof(PreLitVolume), "CollectCommands_Globals", new Type[] { typeof(CommandBuffer) })]
	internal static class FogGlobalsPatch
	{
		[HarmonyPostfix]
		[HarmonyPriority(0)]
		internal static void Postfix(PreLitVolume __instance, CommandBuffer __0)
		{
			try
			{
				if (Plugin.TryGetScale(out var scale) && !((Object)(object)__instance == (Object)null) && __0 != null)
				{
					float fogDensity = __instance.m_fogDensity;
					if (float.IsFinite(fogDensity) && !(fogDensity < 0f))
					{
						__0.SetGlobalFloat(PreLitVolume._FogDensity, fogDensity * scale);
					}
				}
			}
			catch (Exception ex)
			{
				Plugin.Fail(ex);
			}
		}
	}
	[HarmonyPatch(typeof(PreLitVolume), "SetDensityHeightParams", new Type[] { })]
	internal static class FogHeightPatch
	{
		[HarmonyPostfix]
		[HarmonyPriority(0)]
		internal static void Postfix(PreLitVolume __instance)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				if (Plugin.TryGetScale(out var scale) && !((Object)(object)__instance == (Object)null))
				{
					Vector4 densityHeightParams = __instance.m_densityHeightParams;
					if (float.IsFinite(densityHeightParams.z) && !(densityHeightParams.z < 0f))
					{
						densityHeightParams.z *= scale;
						Shader.SetGlobalVector(PreLitVolume._DensityHeightParams, densityHeightParams);
					}
				}
			}
			catch (Exception ex)
			{
				Plugin.Fail(ex);
			}
		}
	}
}