Decompiled source of ScanRecolor v1.1.5

ScanRecolor.dll

Decompiled 2 months 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.InputSystem;
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+299b7aa673cb87e11068e44dd04c7c40c9566e19")]
[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 ConfigEntry<bool> RandomColor { 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 / opacity.", (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>()));
			RandomColor = Plugin.BepInExConfig().Bind<bool>("Color", "RandomColor", false, "Random color on each scan (ignores other Color settings)");
			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();
			};
			RandomColor.SettingChanged += delegate
			{
				if (!RandomColor.Value)
				{
					HUDManagerPatch.SetScanColor();
				}
			};
			VignetteIntensity.SettingChanged += delegate
			{
				HUDManagerPatch.UpdateVignetteIntensity();
			};
			RecolorScanLines.SettingChanged += delegate
			{
				HUDManagerPatch.UpdateScanTexture();
			};
		}
	}
	[HarmonyPatch]
	internal static class HUDManagerPatch
	{
		private static readonly float ScanDuration = 1.3f;

		private static Texture2D _baseTexture = null;

		private static MeshRenderer _scanRenderer;

		private static Volume _scanVolume;

		private static Vignette _scanVignette;

		private static Bloom _scanBloom;

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

		public static MeshRenderer ScanRenderer
		{
			get
			{
				if (_scanRenderer == null || ((Renderer)_scanRenderer).material == null)
				{
					if (HUDManager.Instance == null || HUDManager.Instance.scanEffectAnimator == null)
					{
						return null;
					}
					if (!((Component)HUDManager.Instance.scanEffectAnimator).TryGetComponent<MeshRenderer>(ref _scanRenderer))
					{
						return null;
					}
				}
				return _scanRenderer;
			}
		}

		public static Volume ScanVolume => _scanVolume ?? (_scanVolume = Object.FindObjectsByType<Volume>((FindObjectsSortMode)0)?.Where(delegate(Volume v)
		{
			bool? obj;
			if (v == null)
			{
				obj = null;
			}
			else
			{
				VolumeProfile profile = v.profile;
				obj = ((profile == null) ? null : ((Object)profile).name?.StartsWith("ScanVolume"));
			}
			bool? flag = obj;
			return flag.GetValueOrDefault();
		})?.FirstOrDefault());

		public static Vignette ScanVignette
		{
			get
			{
				Vignette obj = _scanVignette;
				if (obj == null)
				{
					Volume scanVolume = ScanVolume;
					VolumeComponent? obj2 = ((scanVolume == null) ? null : scanVolume.profile?.components?.Where((VolumeComponent c) => ((c == null) ? null : ((Object)c).name?.StartsWith("Vignette")).GetValueOrDefault())?.FirstOrDefault());
					obj = (_scanVignette = (Vignette)(object)((obj2 is Vignette) ? obj2 : null));
				}
				return obj;
			}
		}

		public static Bloom ScanBloom
		{
			get
			{
				Bloom obj = _scanBloom;
				if (obj == null)
				{
					Volume scanVolume = ScanVolume;
					VolumeComponent? obj2 = ((scanVolume == null) ? null : scanVolume.profile?.components?.Where((VolumeComponent c) => ((c == null) ? null : ((Object)c).name?.StartsWith("Bloom")).GetValueOrDefault())?.FirstOrDefault());
					obj = (_scanBloom = (Bloom)(object)((obj2 is Bloom) ? obj2 : null));
				}
				return obj;
			}
		}

		private static Color RandomColor => new Color(Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0f, 1f), Random.Range(0.26f, 1f));

		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_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			if (HasScanMaterial)
			{
				Color color = ((Renderer)ScanRenderer).material.color;
				color.a = alpha;
				((Renderer)ScanRenderer).material.color = color;
			}
		}

		public static void SetScanColor(Color? scanColor = null)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: 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_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			if (HasScanMaterial)
			{
				((Renderer)ScanRenderer).material.color = (Color)(((??)scanColor) ?? ScanColor());
			}
			if (ScanVignette != null)
			{
				((VolumeParameter<Color>)(object)ScanVignette.color).value = (Color)(((??)scanColor) ?? ScanColor(1f));
				UpdateVignetteIntensity();
			}
			if (ScanBloom != null)
			{
				((VolumeParameter<Color>)(object)ScanBloom.tint).Override((Color)(((??)scanColor) ?? ScanColor()));
				UpdateScanTexture();
			}
		}

		public static void UpdateVignetteIntensity()
		{
			if (ScanVignette != 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_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Expected O, but got Unknown
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			if (ScanBloom == null || ScanBloom.dirtTexture == null)
			{
				return;
			}
			if (_baseTexture == 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 (_baseTexture != null && ScanBloom != null && ScanBloom.dirtTexture != null)
			{
				((VolumeParameter<Texture>)(object)ScanBloom.dirtTexture).Override((Texture)(object)_baseTexture);
			}
		}

		[HarmonyPatch(typeof(HUDManager), "Start")]
		[HarmonyPostfix]
		public static void HUDManagerStartPostfix()
		{
			_scanRenderer = null;
			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);
			}
		}

		[HarmonyPatch(typeof(HUDManager), "PingScan_performed")]
		[HarmonyPrefix]
		public static void HUDManagerPingScanPerformedPrefix(CallbackContext context)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			if (Config.Instance.RandomColor.Value && HUDManager.Instance.CanPlayerScan())
			{
				SetScanColor(RandomColor);
			}
		}
	}
	[BepInPlugin("Niro.ScanRecolor", "ScanRecolor", "1.1.5")]
	public class Plugin : BaseUnityPlugin
	{
		public const string modGUID = "Niro.ScanRecolor";

		public const string modName = "ScanRecolor";

		public const string modVersion = "1.1.5";

		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)
		{
		}
	}
}