Decompiled source of Enhanced Lighting v1.0.0

EmpressEnhancedLighting.dll

Decompiled a day ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.PostProcessing;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Omniscye")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("EmpressEnhancedLighting")]
[assembly: AssemblyTitle("EmpressEnhancedLighting")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
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;
		}
	}
}
namespace EnhancedLighting
{
	[BepInPlugin("com.empress.enhancedlighting", "EnhancedLighting", "1.0.1")]
	public class EnhancedLightingPlugin : BaseUnityPlugin
	{
		public static ConfigEntry<bool> cfgEnabled;

		public static ConfigEntry<float> cfgShadowDistance;

		public static ConfigEntry<int> cfgShadowCascades;

		public static ConfigEntry<float> cfgAmbientDarkness;

		public static ConfigEntry<bool> cfgUseReflectionProbe;

		public static ConfigEntry<float> cfgProbeUpdateRate;

		public static ConfigEntry<float> cfgBloomThreshold;

		public static ConfigEntry<float> cfgLightRangeMultiplier;

		private Harmony _harmony;

		public static Camera MainCam;

		private static GameObject _probeHolder;

		private static ReflectionProbe _playerProbe;

		private Coroutine _probeUpdateRoutine;

		public static Dictionary<int, float> LightRangeCache = new Dictionary<int, float>();

		private void Awake()
		{
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_0153: Expected O, but got Unknown
			cfgEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enable Visuals", true, "Toggle the mod effects.");
			cfgShadowDistance = ((BaseUnityPlugin)this).Config.Bind<float>("Shadows", "Shadow Distance", 150f, "Distance shadows are rendered.");
			cfgShadowCascades = ((BaseUnityPlugin)this).Config.Bind<int>("Shadows", "Shadow Cascades", 4, "Number of shadow splits.");
			cfgAmbientDarkness = ((BaseUnityPlugin)this).Config.Bind<float>("Lighting", "Ambient Crush", 0.4f, "Multiplies game ambient light. Lower = Darker.");
			cfgUseReflectionProbe = ((BaseUnityPlugin)this).Config.Bind<bool>("Reflections", "Player Reflection Probe", true, "Enables fake GI via player probe.");
			cfgProbeUpdateRate = ((BaseUnityPlugin)this).Config.Bind<float>("Reflections", "Probe Update Rate", 0.06f, "Seconds between probe updates (0.06 = ~15FPS).");
			cfgBloomThreshold = ((BaseUnityPlugin)this).Config.Bind<float>("PostProcess", "Bloom Threshold", 1.2f, "Threshold for bloom glow.");
			cfgLightRangeMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("Lighting", "Light Range Boost", 1.5f, "Multiplier for point light range.");
			cfgShadowDistance.SettingChanged += delegate
			{
				ApplyShadowSettings();
			};
			cfgShadowCascades.SettingChanged += delegate
			{
				ApplyShadowSettings();
			};
			_harmony = new Harmony("com.empress.enhancedlighting");
			_harmony.PatchAll();
			SceneManager.sceneLoaded += OnSceneLoaded;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"EnhancedLighting Loaded. Systems Normalized.");
		}

		private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
		{
			LightRangeCache.Clear();
			MainCam = Camera.main;
			ApplyShadowSettings();
			if ((Object)(object)MainCam != (Object)null && cfgUseReflectionProbe.Value)
			{
				CreatePlayerReflectionProbe();
			}
		}

		private void ApplyShadowSettings()
		{
			if (cfgEnabled.Value)
			{
				QualitySettings.shadowDistance = cfgShadowDistance.Value;
				QualitySettings.shadowCascades = cfgShadowCascades.Value;
				QualitySettings.shadowProjection = (ShadowProjection)1;
				QualitySettings.shadowResolution = (ShadowResolution)3;
			}
		}

		private void CreatePlayerReflectionProbe()
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)_probeHolder))
			{
				Object.Destroy((Object)(object)_probeHolder);
			}
			_probeHolder = new GameObject("EnhancedLighting_Probe");
			_probeHolder.transform.SetParent(((Component)MainCam).transform);
			_probeHolder.transform.localPosition = Vector3.zero;
			_playerProbe = _probeHolder.AddComponent<ReflectionProbe>();
			_playerProbe.mode = (ReflectionProbeMode)1;
			_playerProbe.refreshMode = (ReflectionProbeRefreshMode)2;
			_playerProbe.timeSlicingMode = (ReflectionProbeTimeSlicingMode)0;
			_playerProbe.resolution = 128;
			_playerProbe.cullingMask = ~(1 << LayerMask.NameToLayer("Player"));
			_playerProbe.hdr = true;
			_playerProbe.intensity = 1f;
			_playerProbe.boxProjection = false;
			_playerProbe.farClipPlane = 50f;
			_playerProbe.clearFlags = (ReflectionProbeClearFlags)1;
			if (_probeUpdateRoutine != null)
			{
				((MonoBehaviour)this).StopCoroutine(_probeUpdateRoutine);
			}
			_probeUpdateRoutine = ((MonoBehaviour)this).StartCoroutine(ProbeUpdater());
		}

		private IEnumerator ProbeUpdater()
		{
			WaitForSeconds wait = new WaitForSeconds(cfgProbeUpdateRate.Value);
			while (true)
			{
				if ((Object)(object)_playerProbe != (Object)null && ((Behaviour)_playerProbe).isActiveAndEnabled && cfgEnabled.Value)
				{
					_playerProbe.RenderProbe();
				}
				yield return wait;
			}
		}
	}
	[HarmonyPatch(typeof(EnvironmentDirector), "Update")]
	public class AmbientPatch
	{
		private static FieldRef<EnvironmentDirector, float> _lerpRef = AccessTools.FieldRefAccess<EnvironmentDirector, float>("DarkAdaptationLerp");

		private static void Postfix(EnvironmentDirector __instance)
		{
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			if (EnhancedLightingPlugin.cfgEnabled.Value && !((Object)(object)LevelGenerator.Instance?.Level == (Object)null))
			{
				float num = _lerpRef.Invoke(__instance);
				Color ambientColor = LevelGenerator.Instance.Level.AmbientColor;
				Color ambientColorAdaptation = LevelGenerator.Instance.Level.AmbientColorAdaptation;
				Color val = Color.Lerp(ambientColor, ambientColorAdaptation, __instance.DarkAdaptationCurve.Evaluate(num));
				RenderSettings.ambientLight = val * EnhancedLightingPlugin.cfgAmbientDarkness.Value;
			}
		}
	}
	[HarmonyPatch(typeof(LightManager), "HandleLightActivation")]
	public class LightQualityPatch
	{
		private static void Prefix(PropLight propLight)
		{
			if (EnhancedLightingPlugin.cfgEnabled.Value && !((Object)(object)propLight == (Object)null) && !((Object)(object)propLight.lightComponent == (Object)null))
			{
				int instanceID = ((Object)propLight).GetInstanceID();
				if (!EnhancedLightingPlugin.LightRangeCache.ContainsKey(instanceID))
				{
					EnhancedLightingPlugin.LightRangeCache[instanceID] = propLight.lightComponent.range;
				}
				float num = EnhancedLightingPlugin.LightRangeCache[instanceID];
				propLight.lightComponent.range = num * EnhancedLightingPlugin.cfgLightRangeMultiplier.Value;
				if (propLight.originalIntensity > 1f)
				{
					propLight.lightComponent.shadows = (LightShadows)2;
					propLight.lightComponent.shadowStrength = 0.9f;
					propLight.lightComponent.shadowBias = 0.05f;
				}
			}
		}
	}
	[HarmonyPatch(typeof(PostProcessing), "Setup")]
	public class PostProcessInjector
	{
		private static void Postfix(PostProcessing __instance)
		{
			//IL_005f: 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_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Unknown result type (might be due to invalid IL or missing references)
			if (!EnhancedLightingPlugin.cfgEnabled.Value)
			{
				return;
			}
			PostProcessProfile profile = __instance.volume.profile;
			if (!((Object)(object)profile == (Object)null))
			{
				AmbientOcclusion val = default(AmbientOcclusion);
				if (!profile.TryGetSettings<AmbientOcclusion>(ref val))
				{
					val = profile.AddSettings<AmbientOcclusion>();
				}
				((PostProcessEffectSettings)val).active = true;
				((ParameterOverride<AmbientOcclusionMode>)(object)val.mode).value = (AmbientOcclusionMode)1;
				((ParameterOverride<float>)(object)val.intensity).value = 1f;
				((ParameterOverride<Color>)(object)val.color).value = Color.black;
				((ParameterOverride<float>)(object)val.radius).value = 0.6f;
				ColorGrading val2 = default(ColorGrading);
				if (profile.TryGetSettings<ColorGrading>(ref val2))
				{
					((PostProcessEffectSettings)val2).active = true;
					((ParameterOverride<Tonemapper>)(object)val2.tonemapper).value = (Tonemapper)2;
					((ParameterOverride<float>)(object)val2.postExposure).value = 0.5f;
					((ParameterOverride<float>)(object)val2.saturation).value = 5f;
					((ParameterOverride<float>)(object)val2.contrast).value = 15f;
				}
				Bloom val3 = default(Bloom);
				if (profile.TryGetSettings<Bloom>(ref val3))
				{
					((ParameterOverride<float>)(object)val3.threshold).value = EnhancedLightingPlugin.cfgBloomThreshold.Value;
					((ParameterOverride<float>)(object)val3.intensity).value = 3f;
					((ParameterOverride<float>)(object)val3.diffusion).value = 5f;
				}
				ScreenSpaceReflections val4 = default(ScreenSpaceReflections);
				if (!profile.TryGetSettings<ScreenSpaceReflections>(ref val4))
				{
					val4 = profile.AddSettings<ScreenSpaceReflections>();
				}
				((ParameterOverride<ScreenSpaceReflectionPreset>)(object)val4.preset).value = (ScreenSpaceReflectionPreset)3;
				((PostProcessEffectSettings)val4).active = true;
			}
		}
	}
	[HarmonyPatch(typeof(GraphicsManager), "UpdateShadowDistance")]
	public class ShadowDistanceBypass
	{
		private static bool Prefix()
		{
			return !EnhancedLightingPlugin.cfgEnabled.Value;
		}
	}
	[HarmonyPatch(typeof(GraphicsManager), "UpdateShadowQuality")]
	public class ShadowQualityBypass
	{
		private static bool Prefix()
		{
			if (EnhancedLightingPlugin.cfgEnabled.Value)
			{
				return false;
			}
			return true;
		}
	}
}