Decompiled source of RemoverOfFog v1.1.0

BepInEx/plugins/FogRemover.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using FogRemover.Components;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.SceneManagement;

[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("FogRemover")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+f69b0932c8b249a8964a77fe3f0f0a47ebccb339")]
[assembly: AssemblyProduct("FogRemover")]
[assembly: AssemblyTitle("FogRemover")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
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;
		}
	}
	[CompilerGenerated]
	[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 FogRemover
{
	[BepInPlugin("grug.lethalcompany.fogremover", "Remove fog", "1.1.0")]
	public class FogRemover : BaseUnityPlugin
	{
		public const string modGUID = "grug.lethalcompany.fogremover";

		public const string modName = "Remove fog";

		public const string modVersion = "1.1.0";

		private readonly Harmony harmony = new Harmony("grug.lethalcompany.fogremover");

		public static ManualLogSource mls;

		private static FogRemover instance;

		public static GameObject holder;

		public static ConfigEntry<bool> disableAll;

		public static ConfigEntry<bool> diagnosticMode;

		public static ConfigEntry<bool> smartAmbientMode;

		public void Awake()
		{
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Expected O, but got Unknown
			disableAll = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "All fog disable?", false, "Legacy mode: disables Fog on every Volume in the scene, not just ambient ones. Can cause visual side effects (loss of HDRP light scattering fill = darker scenes) and may cause lag. Prefer 'Smart ambient fog removal?' instead. Use at your own risk.");
			diagnosticMode = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Diagnostic mode?", false, "Logs every Volume in the scene (name, Fog override state, fog type/density, global vs local/bounded) on every scene load. Does not change fog removal behavior. Use to investigate a moon's fog setup, then turn back off.");
			smartAmbientMode = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Smart ambient fog removal?", true, "Disables Fog on any GLOBAL Volume (moon-wide atmosphere/sky fog), regardless of its object name, instead of only one literally named 'VolumeMain'. Fixes moons that ship their own custom-named sky/fog volume (e.g. custom moon packs). Excludes local/bounded Volumes (indoor pockets, security cameras) and known gameplay status-effect profiles (Poison/Drunk/Underwater/Flashbang/InsideFactorySky) so those aren't broken. This is the recommended mode. Takes priority over 'All fog disable?' when both are enabled.");
			mls = ((BaseUnityPlugin)this).Logger;
			if ((Object)(object)instance == (Object)null)
			{
				instance = this;
			}
			if ((Object)(object)holder == (Object)null)
			{
				holder = new GameObject("FogRemoverHolder");
				Object.DontDestroyOnLoad((Object)(object)holder);
				((Object)holder).hideFlags = (HideFlags)61;
				holder.AddComponent<RemoveFog>();
			}
		}
	}
	[HarmonyPatch(typeof(NetworkManager))]
	internal static class NetworkPrefabPatch
	{
		private static readonly string MOD_GUID = "grug.lethalcompany.fogremover";

		[HarmonyPostfix]
		[HarmonyPatch("SetSingleton")]
		private static void RegisterPrefab()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Expected O, but got Unknown
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject(MOD_GUID + " Prefab");
			((Object)val).hideFlags = (HideFlags)(((Object)val).hideFlags | 0x3D);
			Object.DontDestroyOnLoad((Object)(object)val);
			NetworkObject obj = val.AddComponent<NetworkObject>();
			typeof(NetworkObject).GetField("GlobalObjectIdHash", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(obj, GetHash(MOD_GUID));
			NetworkManager.Singleton.PrefabHandler.AddNetworkPrefab(val);
		}

		private static uint GetHash(string value)
		{
			return value?.Aggregate(17u, (uint current, char c) => (current * 31) ^ c) ?? 0;
		}
	}
}
namespace FogRemover.Components
{
	internal class RemoveFog : MonoBehaviour
	{
		private const float ScanWindowSeconds = 10f;

		private const float DiagnosticLogIntervalSeconds = 1f;

		private float scanUntil = -1f;

		private float nextDiagnosticLogAt = -1f;

		private bool removedThisScene;

		private static readonly string[] StatusEffectProfileNames = new string[7] { "PoisonFilter", "DrunkFilter", "UnderwaterFilter", "FlashbangedFilter", "InsideFactorySkyVolume", "SeeingRedFilter", "InsanityVolume" };

		public void Awake()
		{
			FogRemover.mls.LogInfo((object)"Fog Remover active; watching for scene loads.");
			SceneManager.sceneLoaded += OnSceneLoaded;
			scanUntil = Time.time + 10f;
		}

		private void OnDestroy()
		{
			SceneManager.sceneLoaded -= OnSceneLoaded;
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			removedThisScene = false;
			scanUntil = Time.time + 10f;
			nextDiagnosticLogAt = -1f;
		}

		public void Update()
		{
			if (Time.time > scanUntil)
			{
				return;
			}
			if (FogRemover.diagnosticMode.Value && Time.time >= nextDiagnosticLogAt)
			{
				nextDiagnosticLogAt = Time.time + 1f;
				LogAllVolumes();
			}
			bool flag = false;
			int num = 0;
			Volume[] array = Object.FindObjectsOfType<Volume>();
			Fog val2 = default(Fog);
			Fog val3 = default(Fog);
			foreach (Volume val in array)
			{
				if (FogRemover.smartAmbientMode.Value)
				{
					if (IsSmartAmbientTarget(val) && (Object)(object)val.sharedProfile != (Object)null && val.sharedProfile.TryGet<Fog>(ref val2))
					{
						((VolumeComponent)val2).active = false;
						((VolumeParameter<bool>)(object)val2.enabled).value = false;
						((VolumeParameter)val2.enabled).overrideState = false;
						flag = true;
						num++;
					}
				}
				else if ((FogRemover.disableAll.Value || ((Object)((Component)val).gameObject).name == "VolumeMain") && (Object)(object)val.sharedProfile != (Object)null && val.sharedProfile.TryGet<Fog>(ref val3))
				{
					((VolumeComponent)val3).active = false;
					((VolumeParameter<bool>)(object)val3.enabled).value = false;
					((VolumeParameter)val3.enabled).overrideState = false;
					flag = true;
				}
			}
			if (flag && !removedThisScene)
			{
				removedThisScene = true;
				if (FogRemover.smartAmbientMode.Value)
				{
					FogRemover.mls.LogInfo((object)$"[Smart] Fog removed for current scene ({num} ambient volume(s)).");
				}
				else
				{
					FogRemover.mls.LogInfo((object)"Fog removed for current scene.");
				}
			}
			if (removedThisScene && !FogRemover.disableAll.Value && !FogRemover.diagnosticMode.Value)
			{
				scanUntil = -1f;
			}
		}

		private static bool IsSmartAmbientTarget(Volume v)
		{
			if (!v.isGlobal)
			{
				return false;
			}
			if ((Object)(object)v.sharedProfile == (Object)null)
			{
				return false;
			}
			Fog val = default(Fog);
			if (!v.sharedProfile.TryGet<Fog>(ref val))
			{
				return false;
			}
			string text = ((Object)v.sharedProfile).name ?? string.Empty;
			string[] statusEffectProfileNames = StatusEffectProfileNames;
			foreach (string value in statusEffectProfileNames)
			{
				if (text.IndexOf(value, StringComparison.OrdinalIgnoreCase) >= 0)
				{
					return false;
				}
			}
			return true;
		}

		private void LogAllVolumes()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_021e: Unknown result type (might be due to invalid IL or missing references)
			Volume[] array = Object.FindObjectsOfType<Volume>();
			ManualLogSource mls = FogRemover.mls;
			Scene activeScene = SceneManager.GetActiveScene();
			mls.LogInfo((object)$"[Diag] Scene '{((Scene)(ref activeScene)).name}': found {array.Length} Volume(s).");
			Volume[] array2 = array;
			Fog val2 = default(Fog);
			foreach (Volume val in array2)
			{
				string hierarchyPath = GetHierarchyPath(((Component)val).transform);
				bool isGlobal = val.isGlobal;
				float weight = val.weight;
				float priority = val.priority;
				bool flag = (Object)(object)((Component)val).GetComponent<Collider>() != (Object)null;
				if ((Object)(object)val.sharedProfile == (Object)null)
				{
					FogRemover.mls.LogInfo((object)$"[Diag]  Volume '{hierarchyPath}' | global={isGlobal} weight={weight} priority={priority} hasCollider={flag} | sharedProfile=NULL");
				}
				else if (!val.sharedProfile.TryGet<Fog>(ref val2))
				{
					FogRemover.mls.LogInfo((object)$"[Diag]  Volume '{hierarchyPath}' | global={isGlobal} weight={weight} priority={priority} hasCollider={flag} | profile='{((Object)val.sharedProfile).name}' NO Fog override");
				}
				else
				{
					FogRemover.mls.LogInfo((object)($"[Diag]  Volume '{hierarchyPath}' | global={isGlobal} weight={weight} priority={priority} hasCollider={flag} | profile='{((Object)val.sharedProfile).name}' " + $"Fog: active={((VolumeComponent)val2).active} enabled(override={((VolumeParameter)val2.enabled).overrideState},value={((VolumeParameter<bool>)(object)val2.enabled).value}) " + $"meanFreePath={((VolumeParameter<float>)(object)val2.meanFreePath).value} baseHeight={((VolumeParameter<float>)(object)val2.baseHeight).value} " + $"maximumHeight={((VolumeParameter<float>)(object)val2.maximumHeight).value} albedo={((VolumeParameter<Color>)(object)val2.albedo).value} " + $"enableVolumetricFog={((VolumeParameter<bool>)(object)val2.enableVolumetricFog).value}"));
				}
			}
		}

		private static string GetHierarchyPath(Transform t)
		{
			string text = ((Object)t).name;
			Transform parent = t.parent;
			int num = 0;
			while ((Object)(object)parent != (Object)null && num < 6)
			{
				text = ((Object)parent).name + "/" + text;
				parent = parent.parent;
				num++;
			}
			return text;
		}
	}
}