Decompiled source of ScanRecolor v1.1.2

ScanRecolor.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: IgnoresAccessChecksTo("Unity.Netcode.Runtime")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("ScanRecolor")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("A scan recoloring mod for Lethal Company")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+5e9e58aa107f72a7ccfe23969bdac41ef24dc295")]
[assembly: AssemblyProduct("ScanRecolor")]
[assembly: AssemblyTitle("ScanRecolor")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 ScanRecolor
{
	public sealed class Config
	{
		private static Config instance;

		public ConfigEntry<bool> FadeOut { get; set; }

		public ConfigEntry<bool> RecolorScanLines { get; set; }

		public ConfigEntry<int> Red { get; set; }

		public ConfigEntry<int> Green { get; set; }

		public ConfigEntry<int> Blue { get; set; }

		public ConfigEntry<float> Alpha { get; set; }

		public ConfigEntry<float> VignetteIntensity { get; set; }

		public static Config Instance
		{
			get
			{
				if (instance == null)
				{
					instance = new Config();
				}
				return instance;
			}
		}

		public void Setup()
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Expected O, but got Unknown
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0086: Expected O, but got Unknown
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Expected O, but got Unknown
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Expected O, but got Unknown
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Expected O, but got Unknown
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			//IL_016f: Expected O, but got Unknown
			FadeOut = Plugin.BepInExConfig().Bind<bool>("General", "FadeOut", false, new ConfigDescription("Fade out effect for scan color.", (AcceptableValueBase)null, Array.Empty<object>()));
			RecolorScanLines = Plugin.BepInExConfig().Bind<bool>("General", "RecolorScanLines", true, new ConfigDescription("Recolor the blue horizontal scan lines texture aswell.", (AcceptableValueBase)null, Array.Empty<object>()));
			Red = Plugin.BepInExConfig().Bind<int>("Color", "Red", 0, new ConfigDescription("Red scan color.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 255), Array.Empty<object>()));
			Green = Plugin.BepInExConfig().Bind<int>("Color", "Green", 12, new ConfigDescription("Green scan color.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 255), Array.Empty<object>()));
			Blue = Plugin.BepInExConfig().Bind<int>("Color", "Blue", 255, new ConfigDescription("Blue scan color.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 255), Array.Empty<object>()));
			Alpha = Plugin.BepInExConfig().Bind<float>("Color", "Alpha", 0.26f, new ConfigDescription("Alpha / opaticty.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			VignetteIntensity = Plugin.BepInExConfig().Bind<float>("Color", "VignetteIntensity", 0.46f, new ConfigDescription("Intensity of the vignette / borders effect during scan.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			Red.SettingChanged += delegate
			{
				HUDManagerPatch.SetScanColor();
			};
			Green.SettingChanged += delegate
			{
				HUDManagerPatch.SetScanColor();
			};
			Blue.SettingChanged += delegate
			{
				HUDManagerPatch.SetScanColor();
			};
			Alpha.SettingChanged += delegate
			{
				HUDManagerPatch.SetScanColor();
			};
			FadeOut.SettingChanged += delegate
			{
				HUDManagerPatch.SetScanColor();
			};
			VignetteIntensity.SettingChanged += delegate
			{
				HUDManagerPatch.UpdateVignetteIntensity();
			};
			RecolorScanLines.SettingChanged += delegate
			{
				HUDManagerPatch.UpdateScanTexture();
			};
		}
	}
	[HarmonyPatch]
	internal class HUDManagerPatch
	{
		private static readonly float ScanDuration = 1.3f;

		private static Texture2D _baseTexture = null;

		private static MeshRenderer ScanRenderer = ((Component)HUDManager.Instance.scanEffectAnimator).GetComponent<MeshRenderer>();

		private static readonly Volume ScanVolume = (from v in Object.FindObjectsByType<Volume>((FindObjectsSortMode)0)
			where ((Object)v.profile).name.StartsWith("ScanVolume")
			select v).FirstOrDefault();

		private static readonly Vignette ScanVignette;

		private static readonly Bloom ScanBloom;

		private static bool HasScanMaterial
		{
			get
			{
				if ((Object)(object)ScanRenderer != (Object)null)
				{
					return (Object)(object)((Renderer)ScanRenderer).material != (Object)null;
				}
				return false;
			}
		}

		private static float ScanProgress => 1f / ScanDuration * (HUDManager.Instance.playerPingingScan + 1f);

		private static float ColorToFloat(int color)
		{
			return 0.003921569f * (float)color;
		}

		private static Color ScanColor(float? overrideAlpha = null)
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			return new Color(ColorToFloat(Config.Instance.Red.Value), ColorToFloat(Config.Instance.Green.Value), ColorToFloat(Config.Instance.Blue.Value), overrideAlpha.GetValueOrDefault(Config.Instance.Alpha.Value));
		}

		public static void SetScanColorAlpha(float alpha)
		{
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: 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 (!HasScanMaterial && (Object)(object)HUDManager.Instance.scanEffectAnimator != (Object)null)
			{
				ScanRenderer = ((Component)HUDManager.Instance.scanEffectAnimator).GetComponent<MeshRenderer>();
			}
			if (HasScanMaterial)
			{
				Color color = ((Renderer)ScanRenderer).material.color;
				color.a = alpha;
				((Renderer)ScanRenderer).material.color = color;
			}
		}

		public static void SetScanColor()
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			if (!HasScanMaterial && (Object)(object)HUDManager.Instance.scanEffectAnimator != (Object)null)
			{
				ScanRenderer = ((Component)HUDManager.Instance.scanEffectAnimator).GetComponent<MeshRenderer>();
			}
			if (HasScanMaterial)
			{
				((Renderer)ScanRenderer).material.color = ScanColor();
			}
			if ((Object)(object)ScanVignette != (Object)null)
			{
				((VolumeParameter<Color>)(object)ScanVignette.color).value = ScanColor(1f);
				UpdateVignetteIntensity();
			}
			if ((Object)(object)ScanBloom != (Object)null)
			{
				((VolumeParameter<Color>)(object)ScanBloom.tint).Override(ScanColor());
				UpdateScanTexture();
			}
		}

		public static void UpdateVignetteIntensity()
		{
			if ((Object)(object)ScanVignette != (Object)null)
			{
				((VolumeParameter<float>)(object)ScanVignette.intensity).value = Config.Instance.VignetteIntensity.Value;
			}
		}

		public static void UpdateScanTexture()
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			if (Config.Instance.RecolorScanLines.Value)
			{
				RecolorScanTexture(ScanColor());
			}
			else
			{
				RevertScanTexture();
			}
		}

		public static void RecolorScanTexture(Color color)
		{
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Expected O, but got Unknown
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)ScanBloom == (Object)null || (VolumeParameter<Texture>)(object)ScanBloom.dirtTexture == (Texture)null)
			{
				return;
			}
			if ((Object)(object)_baseTexture == (Object)null)
			{
				Texture obj = Utils.MakeTextureReadable(((VolumeParameter<Texture>)(object)ScanBloom.dirtTexture).value);
				_baseTexture = (Texture2D)(object)((obj is Texture2D) ? obj : null);
				if (!((Texture)_baseTexture).isReadable)
				{
					Plugin.mls.LogWarning((object)"Unable to read ScanTexture.");
					return;
				}
			}
			Texture2D texture = new Texture2D(((Texture)_baseTexture).width, ((Texture)_baseTexture).height);
			texture.SetPixels(_baseTexture.GetPixels());
			texture.Apply();
			Utils.RecolorTexture(ref texture, color);
			((VolumeParameter<Texture>)(object)ScanBloom.dirtTexture).Override((Texture)(object)Object.Instantiate<Texture2D>(texture));
		}

		public static void RevertScanTexture()
		{
			if (!((Object)(object)_baseTexture == (Object)null) && !((Object)(object)ScanBloom == (Object)null) && !((VolumeParameter<Texture>)(object)ScanBloom.dirtTexture == (Texture)null))
			{
				((VolumeParameter<Texture>)(object)ScanBloom.dirtTexture).Override((Texture)(object)_baseTexture);
			}
		}

		[HarmonyPatch(typeof(HUDManager), "Start")]
		[HarmonyPostfix]
		public static void HUDManagerStartPostfix()
		{
			SetScanColor();
			UpdateScanTexture();
		}

		[HarmonyPatch(typeof(HUDManager), "Update")]
		[HarmonyPostfix]
		public static void HUDManagerUpdatePostfix()
		{
			if (Config.Instance.FadeOut.Value && HUDManager.Instance.playerPingingScan > -1f)
			{
				SetScanColorAlpha(ScanProgress * Config.Instance.Alpha.Value);
			}
		}

		static HUDManagerPatch()
		{
			VolumeComponent? obj = ScanVolume.profile.components.Where((VolumeComponent c) => ((Object)c).name.StartsWith("Vignette")).FirstOrDefault();
			ScanVignette = (Vignette)(object)((obj is Vignette) ? obj : null);
			VolumeComponent? obj2 = ScanVolume.profile.components.Where((VolumeComponent c) => ((Object)c).name.StartsWith("Bloom")).FirstOrDefault();
			ScanBloom = (Bloom)(object)((obj2 is Bloom) ? obj2 : null);
		}
	}
	[BepInPlugin("Niro.ScanRecolor", "ScanRecolor", "1.1.2")]
	public class Plugin : BaseUnityPlugin
	{
		public const string modGUID = "Niro.ScanRecolor";

		public const string modName = "ScanRecolor";

		public const string modVersion = "1.1.2";

		private readonly Harmony harmony = new Harmony("Niro.ScanRecolor");

		private static Plugin instance;

		internal static ManualLogSource mls;

		public static ConfigFile BepInExConfig()
		{
			return ((BaseUnityPlugin)instance).Config;
		}

		public void Awake()
		{
			if (instance == null)
			{
				instance = this;
			}
			mls = Logger.CreateLogSource("Niro.ScanRecolor");
			Config.Instance.Setup();
			mls.LogMessage((object)"Plugin ScanRecolor loaded!");
			harmony.PatchAll(typeof(HUDManagerPatch));
		}
	}
	internal class Utils
	{
		public static Texture MakeTextureReadable(Texture original)
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected O, but got Unknown
			RenderTexture temporary = RenderTexture.GetTemporary(original.width, original.height, 0, (RenderTextureFormat)7, (RenderTextureReadWrite)1);
			Graphics.Blit(original, temporary);
			RenderTexture active = RenderTexture.active;
			RenderTexture.active = temporary;
			Texture2D val = new Texture2D(original.width, original.height);
			val.ReadPixels(new Rect(0f, 0f, (float)((Texture)temporary).width, (float)((Texture)temporary).height), 0, 0);
			val.Apply();
			RenderTexture.active = active;
			RenderTexture.ReleaseTemporary(temporary);
			return (Texture)val;
		}

		public static void RecolorTexture(ref Texture2D texture, Color color)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			float num = color.r + color.g + color.b;
			List<Color> list = texture.GetPixels().ToList();
			Plugin.mls.LogDebug((object)("ScanTexture pixel count: " + list.Count));
			for (int num2 = list.Count - 1; num2 >= 0; num2--)
			{
				float num3 = list[num2].r + list[num2].g + list[num2].b;
				if (!(num3 < 0.05f) && !(list[num2].a < 0.05f))
				{
					float num4 = ((num == 0f) ? 0f : (num3 / num));
					list[num2] = new Color(color.r * num4, color.g * num4, color.b * num4);
				}
			}
			texture.SetPixels(list.ToArray());
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}