Decompiled source of ClearView v1.0.0

ClearView.dll

Decompiled 20 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.PostProcessing;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("ClearView")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ClearView")]
[assembly: AssemblyTitle("ClearView")]
[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.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace ClearView
{
	[BepInPlugin("com.dreich.clearview", "Clear View", "1.0.0")]
	public class ClearViewPlugin : BaseUnityPlugin
	{
		public const string Guid = "com.dreich.clearview";

		public const string Name = "Clear View";

		public const string Version = "1.0.0";

		private const string Section = "Screen Effects";

		private const float RefreshSeconds = 0.5f;

		private const float NoLensDirt = 0f;

		private static ConfigEntry<bool> modEnabled;

		private static ConfigEntry<bool> lensFlares;

		private static ConfigEntry<bool> lensDirt;

		private static ConfigEntry<bool> heatDistortion;

		private static ConfigEntry<bool> vignette;

		private static ConfigEntry<bool> filmGrain;

		private static ConfigEntry<bool> reflections;

		private static ConfigEntry<bool> eyeAdaptation;

		private static ConfigEntry<bool> colorGrading;

		private static ConfigEntry<bool> dithering;

		private static readonly Dictionary<Behaviour, bool> originalBehaviours = new Dictionary<Behaviour, bool>();

		private static readonly Dictionary<PostProcessingModel, bool> originalModels = new Dictionary<PostProcessingModel, bool>();

		private static BloomModel dirtyBloom;

		private static float originalLensDirt;

		private static float nextRefresh;

		private void Awake()
		{
			modEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("Screen Effects", "Enabled", true, "Turns off the screen effects picked below. Bloom, ambient occlusion, sun shafts, anti-aliasing, chromatic aberration, motion blur and depth of field are already in the game's graphics settings.");
			lensFlares = ((BaseUnityPlugin)this).Config.Bind<bool>("Screen Effects", "TurnOffLensFlares", true, "Turn off lens flares from the sun and bright lights.");
			lensDirt = ((BaseUnityPlugin)this).Config.Bind<bool>("Screen Effects", "TurnOffLensDirt", true, "Turn off the smudges and specks that show up on the screen when looking towards bright light.");
			heatDistortion = ((BaseUnityPlugin)this).Config.Bind<bool>("Screen Effects", "TurnOffHeatDistortion", true, "Turn off the wavy heat haze, like in the Ashlands.");
			vignette = ((BaseUnityPlugin)this).Config.Bind<bool>("Screen Effects", "TurnOffVignette", true, "Turn off the darkening around the edges of the screen.");
			filmGrain = ((BaseUnityPlugin)this).Config.Bind<bool>("Screen Effects", "TurnOffFilmGrain", true, "Turn off film grain noise.");
			reflections = ((BaseUnityPlugin)this).Config.Bind<bool>("Screen Effects", "TurnOffScreenSpaceReflections", true, "Turn off screen space reflections.");
			eyeAdaptation = ((BaseUnityPlugin)this).Config.Bind<bool>("Screen Effects", "TurnOffEyeAdaptation", false, "Turn off the automatic brightness change when moving between dark and bright places. Dark places can look darker without it.");
			colorGrading = ((BaseUnityPlugin)this).Config.Bind<bool>("Screen Effects", "TurnOffColorGrading", false, "Turn off colour grading and tone mapping. This changes how the whole game looks, so it's off by default.");
			dithering = ((BaseUnityPlugin)this).Config.Bind<bool>("Screen Effects", "TurnOffDithering", false, "Turn off dithering, which hides colour banding in the sky and fog. Very cheap, so it's off by default.");
			((BaseUnityPlugin)this).Config.SettingChanged += delegate
			{
				nextRefresh = 0f;
			};
		}

		private void Update()
		{
			if (!modEnabled.Value)
			{
				RestoreAll();
			}
			else
			{
				if (Time.unscaledTime < nextRefresh)
				{
					return;
				}
				nextRefresh = Time.unscaledTime + 0.5f;
				Camera mainCamera = Utils.GetMainCamera();
				if (!((Object)(object)mainCamera == (Object)null))
				{
					Apply((Behaviour)(object)((Component)mainCamera).GetComponent<FlareLayer>(), lensFlares.Value);
					Apply((Behaviour)(object)((Component)mainCamera).GetComponent<HeatDistortImageEffect>(), heatDistortion.Value);
					PostProcessingBehaviour component = ((Component)mainCamera).GetComponent<PostProcessingBehaviour>();
					PostProcessingProfile val = (((Object)(object)component != (Object)null) ? component.profile : null);
					if (!((Object)(object)val == (Object)null))
					{
						ApplyLensDirt(val.bloom, lensDirt.Value);
						Apply((PostProcessingModel)(object)val.vignette, vignette.Value);
						Apply((PostProcessingModel)(object)val.grain, filmGrain.Value);
						Apply((PostProcessingModel)(object)val.screenSpaceReflection, reflections.Value);
						Apply((PostProcessingModel)(object)val.eyeAdaptation, eyeAdaptation.Value);
						Apply((PostProcessingModel)(object)val.colorGrading, colorGrading.Value);
						Apply((PostProcessingModel)(object)val.dithering, dithering.Value);
					}
				}
			}
		}

		private void OnDestroy()
		{
			RestoreAll();
		}

		private static void Apply(Behaviour effect, bool turnOff)
		{
			if ((Object)(object)effect == (Object)null)
			{
				return;
			}
			if (!turnOff)
			{
				Restore(effect);
				return;
			}
			if (!originalBehaviours.ContainsKey(effect))
			{
				originalBehaviours[effect] = effect.enabled;
			}
			if (effect.enabled)
			{
				effect.enabled = false;
			}
		}

		private static void Apply(PostProcessingModel effect, bool turnOff)
		{
			if (effect == null)
			{
				return;
			}
			if (!turnOff)
			{
				Restore(effect);
				return;
			}
			if (!originalModels.ContainsKey(effect))
			{
				originalModels[effect] = effect.enabled;
			}
			if (effect.enabled)
			{
				effect.enabled = false;
			}
		}

		private static void ApplyLensDirt(BloomModel bloom, bool turnOff)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: 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)
			if (bloom == null)
			{
				return;
			}
			if (!turnOff)
			{
				RestoreLensDirt();
				return;
			}
			Settings settings = bloom.settings;
			if (dirtyBloom == null)
			{
				dirtyBloom = bloom;
				originalLensDirt = settings.lensDirt.intensity;
			}
			if (settings.lensDirt.intensity != 0f)
			{
				settings.lensDirt.intensity = 0f;
				bloom.settings = settings;
			}
		}

		private static void RestoreLensDirt()
		{
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			if (dirtyBloom != null)
			{
				Settings settings = dirtyBloom.settings;
				settings.lensDirt.intensity = originalLensDirt;
				dirtyBloom.settings = settings;
				dirtyBloom = null;
			}
		}

		private static void Restore(Behaviour effect)
		{
			if (originalBehaviours.TryGetValue(effect, out var value))
			{
				effect.enabled = value;
				originalBehaviours.Remove(effect);
			}
		}

		private static void Restore(PostProcessingModel effect)
		{
			if (originalModels.TryGetValue(effect, out var value))
			{
				effect.enabled = value;
				originalModels.Remove(effect);
			}
		}

		private static void RestoreAll()
		{
			foreach (KeyValuePair<Behaviour, bool> originalBehaviour in originalBehaviours)
			{
				if ((Object)(object)originalBehaviour.Key != (Object)null)
				{
					originalBehaviour.Key.enabled = originalBehaviour.Value;
				}
			}
			originalBehaviours.Clear();
			foreach (KeyValuePair<PostProcessingModel, bool> originalModel in originalModels)
			{
				originalModel.Key.enabled = originalModel.Value;
			}
			originalModels.Clear();
			RestoreLensDirt();
		}
	}
}