Decompiled source of ToggleableNightVision v2.1.0

NightVision.dll

Decompiled 4 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using NightVision.Patches;
using UnityEngine;
using UnityEngine.InputSystem;
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("NightVision")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NightVision")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("30558705-be06-4ac4-ba83-bce5f1f25495")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace NightVision
{
	public class PLUGIN_INFO
	{
		public const string PLUGIN_GUID = "Ken.NightVision";

		public const string PLUGIN_NAME = "Toggleable Night Vision";

		public const string PLUGIN_VERSION = "2.1.0";
	}
	[BepInPlugin("Ken.NightVision", "Toggleable Night Vision", "2.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		public static Harmony harmony = new Harmony("Ken.NightVision");

		internal static ManualLogSource mls;

		private static ConfigEntry<string> cfgKeyBind;

		public static ConfigEntry<bool> defaultToggle;

		public static ConfigEntry<float> indoorIntensity;

		public static ConfigEntry<bool> disableSteamValve;

		public static ConfigEntry<bool> toggleFog;

		public static ConfigEntry<bool> enableOutdoor;

		public static ConfigEntry<float> outdoorIntensity;

		public static ConfigEntry<bool> diversityFullDarkness;

		public static ConfigEntry<float> diversityFullDarknessIntensity;

		private void Awake()
		{
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Expected O, but got Unknown
			mls = Logger.CreateLogSource("Ken.NightVision");
			mls.LogInfo((object)"Toggleable Night Vision loaded");
			ConfigureSettings();
			SetDefaultToggle(defaultToggle.Value);
			InputAction val = new InputAction((string)null, (InputActionType)0, "<Keyboard>/" + cfgKeyBind.Value, (string)null, (string)null, (string)null);
			val.performed += OnInsertKeyPressed;
			val.Enable();
			Type[] array = new Type[4]
			{
				typeof(Plugin),
				typeof(NightVisionPatch),
				typeof(NightVisionOutdoors),
				typeof(PlayerControllerBPatch)
			};
			Type[] array2 = array;
			foreach (Type type in array2)
			{
				harmony.PatchAll(type);
			}
		}

		private void ConfigureSettings()
		{
			cfgKeyBind = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Toggle Key", "C", "Button to toggle night vision mode inside the facility.");
			defaultToggle = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Default Behavior", false, "Whether night vision is on or off by default when you load up the game.");
			indoorIntensity = ((BaseUnityPlugin)this).Config.Bind<float>("Indoor Settings", "Intensity", 7500f, "Intensity of the night vision when toggled inside. [Originally was 100000]");
			disableSteamValve = ((BaseUnityPlugin)this).Config.Bind<bool>("Indoor Settings", "Steam Valve", false, "Toggling fog also disables steam valve.");
			toggleFog = ((BaseUnityPlugin)this).Config.Bind<bool>("Outdoor Settings", "Toggle Fog", false, "Disable fog when toggling night vision outside.");
			enableOutdoor = ((BaseUnityPlugin)this).Config.Bind<bool>("Outdoor Settings", "Enable Outside", true, "Enable night vision to work outside.");
			outdoorIntensity = ((BaseUnityPlugin)this).Config.Bind<float>("Outdoor Settings", "Intensity", 10f, "Intensity of night vision when toggled outside.");
			diversityFullDarkness = ((BaseUnityPlugin)this).Config.Bind<bool>("Mod Compatability", "Diversity - Full Darkness", false, "Change default brightness when night vision is off.\nThis mod overrides the settings in Diversity config.");
			diversityFullDarknessIntensity = ((BaseUnityPlugin)this).Config.Bind<float>("Mod Compatability", "Diversity - Full Darkness Intensity", 1f, "How intense the darkness will be. Set values between 0-1.");
		}

		private void SetDefaultToggle(bool value)
		{
			NightVisionPatch.toggled = value;
			NightVisionOutdoors.toggled = value;
			PlayerControllerBPatch.toggled = value;
		}

		private void OnInsertKeyPressed(CallbackContext obj)
		{
			PlayerControllerB val = StartOfRound.Instance?.localPlayerController;
			if (!((Object)(object)val == (Object)null) && !val.inTerminalMenu && !val.isTypingChat)
			{
				NightVisionPatch.toggled = !NightVisionPatch.toggled;
				NightVisionOutdoors.toggled = !NightVisionOutdoors.toggled;
				PlayerControllerBPatch.toggled = !PlayerControllerBPatch.toggled;
				mls.LogInfo((object)("Night mode " + (NightVisionPatch.toggled ? "enabled" : "disabled")));
			}
		}
	}
}
namespace NightVision.Patches
{
	[HarmonyPatch(typeof(PlayerControllerB), "Update")]
	internal class PlayerControllerBPatch
	{
		internal static Object[] cameras;

		internal static bool toggled;

		internal static int cacheRefreshLimit = 20;

		internal static int cachePasses = 0;

		[HarmonyPostfix]
		private static void UpdatePostfix(PlayerControllerB __instance)
		{
			//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Expected O, but got Unknown
			if (!Plugin.toggleFog.Value || (Object)(object)__instance == (Object)null)
			{
				return;
			}
			if (cachePasses++ >= cacheRefreshLimit)
			{
				cachePasses = 0;
				cameras = null;
			}
			if (cameras == null)
			{
				cameras = Resources.FindObjectsOfTypeAll(typeof(HDAdditionalCameraData));
			}
			bool flag = ((Object)(object)__instance == (Object)null || !__instance.isInsideFactory || Plugin.disableSteamValve.Value) && toggled;
			Object[] array = cameras;
			foreach (Object val in array)
			{
				HDAdditionalCameraData val2 = (HDAdditionalCameraData)((val is HDAdditionalCameraData) ? val : null);
				if (((Object)((Component)val2).gameObject).name != "MapCamera")
				{
					val2.customRenderingSettings = true;
					((BitArray128)(ref val2.renderingPathCustomFrameSettingsOverrideMask.mask))[28u] = true;
					((FrameSettings)(ref val2.renderingPathCustomFrameSettings)).SetEnabled((FrameSettingsField)28, !flag);
				}
			}
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB), "SetNightVisionEnabled")]
	internal class NightVisionPatch
	{
		internal static bool toggled;

		[HarmonyPrefix]
		private static void NightVisionPrefix(PlayerControllerB __instance)
		{
			if (toggled)
			{
				__instance.nightVision.intensity = Plugin.indoorIntensity.Value;
				__instance.nightVision.range = 100000f;
				__instance.nightVision.shadowStrength = 0f;
				__instance.nightVision.shadows = (LightShadows)0;
				__instance.nightVision.shape = (LightShape)2;
			}
			else
			{
				float num = (Plugin.diversityFullDarkness.Value ? Mathf.Clamp(1f - Plugin.diversityFullDarknessIntensity.Value, 0f, 1f) : 1f);
				__instance.nightVision.intensity = 366.9317f * num;
				__instance.nightVision.range = 12f;
				__instance.nightVision.shadowStrength = 1f;
				__instance.nightVision.shadows = (LightShadows)0;
				__instance.nightVision.shape = (LightShape)0;
			}
		}
	}
	[HarmonyPatch(typeof(TimeOfDay), "SetInsideLightingDimness")]
	internal static class NightVisionOutdoors
	{
		internal static bool toggled;

		private static float sigmoid(float x, float midpoint, float steepness)
		{
			return 1f / (1f + Mathf.Exp((0f - steepness) * (x - midpoint)));
		}

		[HarmonyPostfix]
		private static void InsideLightingPostfix(TimeOfDay __instance)
		{
			if (Plugin.enableOutdoor.Value)
			{
				HDAdditionalLightData component = ((Component)__instance.sunIndirect).GetComponent<HDAdditionalLightData>();
				if (toggled)
				{
					float value = Plugin.outdoorIntensity.Value;
					component.lightDimmer = Mathf.Lerp(10f * value, 10000f * value, sigmoid(__instance.globalTime, 950f, 0.1f));
					component.intensity = Mathf.Lerp(value, 1000f * value, sigmoid(__instance.globalTime, 950f, 0.1f));
					component.shadowDimmer = 0f;
					component.useRayTracedShadows = true;
					component.lightShadowRadius = 0f;
				}
				else
				{
					component.lightDimmer = Mathf.Lerp(component.lightDimmer, 1f, 5f * Time.deltaTime);
					component.shadowDimmer = 1f;
					component.useRayTracedShadows = false;
					component.lightShadowRadius = 0.5f;
				}
			}
		}
	}
}