Decompiled source of ImprovedSelector v0.3.2

ImprovedSelector.dll

Decompiled an hour 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.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyVersion("0.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 ImprovedSelector
{
	internal sealed class AlphaMap
	{
		internal readonly int Width;

		internal readonly int Height;

		internal readonly float[] Alpha;

		internal readonly float CenterCoverage;

		private AlphaMap(int width, int height, float[] alpha)
		{
			Width = width;
			Height = height;
			Alpha = alpha;
			float num = 0f;
			int num2 = 0;
			for (int i = height / 4; i < height * 3 / 4; i++)
			{
				for (int j = width / 4; j < width * 3 / 4; j++)
				{
					num += alpha[i * width + j];
					num2++;
				}
			}
			CenterCoverage = ((num2 > 0) ? (num / (float)num2) : 1f);
		}

		internal static AlphaMap Read(Sprite sprite)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: 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_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Expected O, but got Unknown
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)sprite == (Object)null || (Object)(object)sprite.texture == (Object)null)
			{
				return null;
			}
			Rect rect = sprite.rect;
			Rect textureRect = sprite.textureRect;
			int num = Mathf.RoundToInt(((Rect)(ref rect)).width);
			int num2 = Mathf.RoundToInt(((Rect)(ref rect)).height);
			if (num <= 0 || num2 <= 0 || Mathf.RoundToInt(((Rect)(ref textureRect)).width) != num || Mathf.RoundToInt(((Rect)(ref textureRect)).height) != num2)
			{
				return null;
			}
			Texture texture = (Texture)(object)sprite.texture;
			Vector2 val = default(Vector2);
			((Vector2)(ref val))..ctor(((Rect)(ref textureRect)).width / (float)texture.width, ((Rect)(ref textureRect)).height / (float)texture.height);
			Vector2 val2 = default(Vector2);
			((Vector2)(ref val2))..ctor(((Rect)(ref textureRect)).x / (float)texture.width, ((Rect)(ref textureRect)).y / (float)texture.height);
			RenderTexture temporary = RenderTexture.GetTemporary(num, num2, 0, (RenderTextureFormat)0);
			RenderTexture active = RenderTexture.active;
			Texture2D val3 = new Texture2D(num, num2, (TextureFormat)4, false);
			try
			{
				Graphics.Blit(texture, temporary, val, val2);
				RenderTexture.active = temporary;
				val3.ReadPixels(new Rect(0f, 0f, (float)num, (float)num2), 0, 0);
				Color32[] pixels = val3.GetPixels32();
				float[] array = new float[pixels.Length];
				for (int i = 0; i < pixels.Length; i++)
				{
					array[i] = (float)(int)pixels[i].a / 255f;
				}
				return new AlphaMap(num, num2, array);
			}
			finally
			{
				RenderTexture.active = active;
				RenderTexture.ReleaseTemporary(temporary);
				Object.Destroy((Object)(object)val3);
			}
		}
	}
	internal static class FrameScanner
	{
		private const string FrameName = "Selection Frame";

		private const BindingFlags Flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;

		private static readonly Dictionary<string, FieldInfo> FieldCache = new Dictionary<string, FieldInfo>();

		private static readonly HashSet<string> LoggedErrors = new HashSet<string>();

		private static ManualLogSource log;

		internal static void Initialize(ManualLogSource logger)
		{
			log = logger;
		}

		internal static void Report(Exception e)
		{
			if (LoggedErrors.Add(e.ToString()))
			{
				log.LogError((object)e);
			}
		}

		internal static void AttachField<T>(T owner, string fieldName, bool inwardOnly) where T : Component
		{
			FieldInfo field = GetField(typeof(T), fieldName);
			if (field == null)
			{
				return;
			}
			object? value = field.GetValue(owner);
			GameObject val = (GameObject)((value is GameObject) ? value : null);
			if (!((Object)(object)val == (Object)null))
			{
				SpriteRenderer[] componentsInChildren = val.GetComponentsInChildren<SpriteRenderer>(true);
				for (int i = 0; i < componentsInChildren.Length; i++)
				{
					GlowFrame.Attach(componentsInChildren[i], inwardOnly);
				}
			}
		}

		internal static void AttachNamed(Component owner, string frameName = "Selection Frame")
		{
			AttachNamedRenderers(owner.gameObject, frameName);
		}

		internal static void AttachNamedInField<T>(T owner, string fieldName, string frameName = "Selection Frame") where T : Component
		{
			FieldInfo field = GetField(typeof(T), fieldName);
			if (!(field == null))
			{
				object value = field.GetValue(owner);
				Component val = (Component)((value is Component) ? value : null);
				GameObject val2 = (GameObject)(((Object)(object)val != (Object)null) ? val.gameObject : ((value is GameObject) ? value : null));
				if (!((Object)(object)val2 == (Object)null))
				{
					AttachNamedRenderers(val2, frameName);
				}
			}
		}

		private static void AttachNamedRenderers(GameObject root, string frameName)
		{
			SpriteRenderer[] componentsInChildren = root.GetComponentsInChildren<SpriteRenderer>(true);
			foreach (SpriteRenderer val in componentsInChildren)
			{
				if (((Object)val).name == frameName)
				{
					GlowFrame.Attach(val, inwardOnly: false);
				}
			}
		}

		private static FieldInfo GetField(Type owner, string fieldName)
		{
			string text = owner.FullName + "." + fieldName;
			if (FieldCache.TryGetValue(text, out var value))
			{
				return value;
			}
			FieldInfo field = owner.GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			if (field == null)
			{
				log.LogWarning((object)("Could not find " + text + ", the game may have been updated"));
			}
			FieldCache[text] = field;
			return field;
		}
	}
	internal sealed class GlowFrame : MonoBehaviour
	{
		internal const string GlowName = "Improved Selector Glow";

		private const string ReferenceName = "Background";

		private const float SolidThreshold = 0.8f;

		private const float BloomLevel = 1f;

		private static readonly Dictionary<Sprite, bool> SolidCache = new Dictionary<Sprite, bool>();

		private SpriteRenderer source;

		private SpriteRenderer glow;

		private bool solid;

		private bool centered;

		internal static void Attach(SpriteRenderer renderer, bool inwardOnly)
		{
			if ((Object)(object)renderer == (Object)null || (Object)(object)renderer.sprite == (Object)null || ((Object)renderer).name == "Improved Selector Glow" || (Object)(object)((Component)renderer).GetComponent<GlowFrame>() != (Object)null)
			{
				return;
			}
			if (!SolidCache.TryGetValue(renderer.sprite, out var value))
			{
				AlphaMap alphaMap = AlphaMap.Read(renderer.sprite);
				if (alphaMap == null)
				{
					return;
				}
				value = alphaMap.CenterCoverage > 0.8f;
				SolidCache[renderer.sprite] = value;
			}
			SpriteRenderer val = (value ? FindReference(renderer) : null);
			Sprite shape = (((Object)(object)val != (Object)null) ? val.sprite : renderer.sprite);
			GlowMode mode = GlowMode.Both;
			if ((Object)(object)val == (Object)null && inwardOnly)
			{
				mode = ((!value) ? GlowMode.Inward : GlowMode.InwardSolid);
			}
			bool isCentered = (Object)(object)val != (Object)null;
			Sprite val2 = GlowSprite.Get(shape, mode, isCentered);
			if (!((Object)(object)val2 == (Object)null))
			{
				((Component)renderer).gameObject.AddComponent<GlowFrame>().Build(renderer, val2, value, isCentered);
			}
		}

		private static SpriteRenderer FindReference(SpriteRenderer renderer)
		{
			Transform parent = ((Component)renderer).transform.parent;
			if ((Object)(object)parent == (Object)null)
			{
				return null;
			}
			SpriteRenderer[] componentsInChildren = ((Component)parent).GetComponentsInChildren<SpriteRenderer>(true);
			foreach (SpriteRenderer val in componentsInChildren)
			{
				if ((Object)(object)val != (Object)(object)renderer && ((Object)val).name == "Background" && (Object)(object)val.sprite != (Object)null)
				{
					return val;
				}
			}
			return null;
		}

		private void Build(SpriteRenderer sourceRenderer, Sprite glowSprite, bool isSolid, bool isCentered)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			source = sourceRenderer;
			solid = isSolid;
			centered = isCentered;
			GameObject val = new GameObject("Improved Selector Glow");
			val.layer = ((Component)source).gameObject.layer;
			val.transform.SetParent(((Component)this).transform, false);
			glow = val.AddComponent<SpriteRenderer>();
			glow.sprite = glowSprite;
			((Renderer)glow).sharedMaterial = GlowMaterial.Get() ?? ((Renderer)source).sharedMaterial;
			((Renderer)glow).sortingLayerID = ((Renderer)source).sortingLayerID;
			((Renderer)glow).sortingOrder = ((Renderer)source).sortingOrder;
			glow.flipX = source.flipX;
			glow.flipY = source.flipY;
			Sync();
		}

		private void LateUpdate()
		{
			if (!((Object)(object)source == (Object)null) && !((Object)(object)glow == (Object)null))
			{
				Sync();
			}
		}

		private void OnDestroy()
		{
			if ((Object)(object)glow != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)glow).gameObject);
			}
			if ((Object)(object)source != (Object)null)
			{
				((Renderer)source).enabled = true;
			}
		}

		private void Sync()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: 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_0047: 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_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0141: Unknown result type (might be due to invalid IL or missing references)
			//IL_0146: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: 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)
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
			((Renderer)source).enabled = false;
			glow.color = Brighten(source.color);
			((Component)glow).transform.localPosition = Vector3.zero;
			((Component)glow).transform.localRotation = Quaternion.identity;
			float num = GlowSprite.PadFor(glow.sprite.pixelsPerUnit);
			if ((int)source.drawMode != 0)
			{
				glow.drawMode = (SpriteDrawMode)1;
				((Component)glow).transform.localScale = Vector3.one;
				((Component)glow).transform.localPosition = PivotOffset(source.size);
				glow.size = source.size + new Vector2(num, num);
				return;
			}
			if (!solid)
			{
				glow.drawMode = (SpriteDrawMode)0;
				((Component)glow).transform.localScale = Vector3.one;
				return;
			}
			Vector3 lossyScale = ((Component)this).transform.lossyScale;
			float num2 = Mathf.Abs(lossyScale.x);
			float num3 = Mathf.Abs(lossyScale.y);
			if (!(num2 < 0.0001f) && !(num3 < 0.0001f))
			{
				Bounds bounds = source.sprite.bounds;
				Vector2 val = Vector2.op_Implicit(((Bounds)(ref bounds)).size);
				glow.drawMode = (SpriteDrawMode)1;
				((Component)glow).transform.localScale = new Vector3(1f / num2, 1f / num3, 1f);
				((Component)glow).transform.localPosition = PivotOffset(val);
				glow.size = new Vector2(val.x * num2 + num, val.y * num3 + num);
			}
		}

		private Vector3 PivotOffset(Vector2 extent)
		{
			//IL_0019: 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)
			//IL_002e: 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_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: 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_005b: 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_0008: Unknown result type (might be due to invalid IL or missing references)
			if (!centered)
			{
				return Vector3.zero;
			}
			Vector2 pivot = source.sprite.pivot;
			Rect rect = source.sprite.rect;
			Vector2 val = pivot / ((Rect)(ref rect)).size;
			return new Vector3((0.5f - val.x) * extent.x, (0.5f - val.y) * extent.y, 0f);
		}

		private static Color Brighten(Color color)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: 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_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			float num = Mathf.Max(new float[3] { color.r, color.g, color.b });
			if (num < 0.001f)
			{
				return color;
			}
			float num2 = 1f / num;
			return new Color(color.r * num2, color.g * num2, color.b * num2, color.a);
		}
	}
	internal static class GlowMaterial
	{
		private static readonly string[] ShaderNames = new string[2] { "Sprites/Default", "UI/Default" };

		private static Material cached;

		private static bool searched;

		internal static Material Get()
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			if ((Object)(object)cached != (Object)null || searched)
			{
				return cached;
			}
			searched = true;
			string[] shaderNames = ShaderNames;
			for (int i = 0; i < shaderNames.Length; i++)
			{
				Shader val = Shader.Find(shaderNames[i]);
				if ((Object)(object)val != (Object)null)
				{
					cached = new Material(val)
					{
						name = "Improved Selector Glow"
					};
					return cached;
				}
			}
			Logger.CreateLogSource("Improved Selector").LogWarning((object)"No alpha blended shader found, the glow will use the frame material");
			return null;
		}
	}
	internal enum GlowMode
	{
		Both,
		Inward,
		InwardSolid
	}
	internal static class GlowSprite
	{
		private const float ReachWorld = 0.04f;

		private const float InwardWorld = 0.03f;

		private const float InwardSigmaRatio = 0.3f;

		private const float SigmaRatio = 0.38f;

		private const float Peak = 0.95f;

		private const float Steepness = 2f;

		private const float Floor = 0.03f;

		private const float OpaqueThreshold = 0.05f;

		private const int MinimumRadius = 2;

		private static readonly Dictionary<(Sprite, GlowMode, bool), Sprite> Cache = new Dictionary<(Sprite, GlowMode, bool), Sprite>();

		internal static float PadFor(float pixelsPerUnit)
		{
			return (float)RadiusFor(pixelsPerUnit) * 2f / pixelsPerUnit;
		}

		internal static Sprite Get(Sprite shape, GlowMode mode, bool centered)
		{
			if ((Object)(object)shape == (Object)null)
			{
				return null;
			}
			(Sprite, GlowMode, bool) key = (shape, mode, centered);
			if (Cache.TryGetValue(key, out var value))
			{
				return value;
			}
			Sprite val = Create(shape, mode, centered);
			Cache[key] = val;
			return val;
		}

		private static int RadiusFor(float pixelsPerUnit)
		{
			return Mathf.Max(2, Mathf.RoundToInt(0.04f * pixelsPerUnit));
		}

		private static int InwardReachFor(float pixelsPerUnit)
		{
			return Mathf.Max(2, Mathf.RoundToInt(0.03f * pixelsPerUnit));
		}

		private static Sprite Create(Sprite shape, GlowMode mode, bool centered)
		{
			//IL_01eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0209: Unknown result type (might be due to invalid IL or missing references)
			//IL_020e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0219: Unknown result type (might be due to invalid IL or missing references)
			//IL_0220: Unknown result type (might be due to invalid IL or missing references)
			//IL_0227: Unknown result type (might be due to invalid IL or missing references)
			//IL_022f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0238: Unknown result type (might be due to invalid IL or missing references)
			//IL_0245: Unknown result type (might be due to invalid IL or missing references)
			//IL_024a: Unknown result type (might be due to invalid IL or missing references)
			//IL_024f: Unknown result type (might be due to invalid IL or missing references)
			//IL_026b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0282: Unknown result type (might be due to invalid IL or missing references)
			//IL_0299: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_0304: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0309: Unknown result type (might be due to invalid IL or missing references)
			//IL_031b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0320: Unknown result type (might be due to invalid IL or missing references)
			//IL_032a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0331: Expected O, but got Unknown
			AlphaMap alphaMap = AlphaMap.Read(shape);
			if (alphaMap == null)
			{
				return null;
			}
			float pixelsPerUnit = shape.pixelsPerUnit;
			int num = RadiusFor(pixelsPerUnit);
			int num2 = InwardReachFor(pixelsPerUnit);
			float num3 = (float)num * 0.38f;
			int num4 = alphaMap.Width + num * 2;
			int num5 = alphaMap.Height + num * 2;
			float[] array = new float[num4 * num5];
			for (int i = 0; i < alphaMap.Height; i++)
			{
				for (int j = 0; j < alphaMap.Width; j++)
				{
					array[(i + num) * num4 + j + num] = alphaMap.Alpha[i * alphaMap.Width + j];
				}
			}
			float[] array2;
			if (mode == GlowMode.InwardSolid)
			{
				array2 = InwardGradient(FindExterior(array, num4, num5), num4, num5, (float)num2 * 0.3f);
			}
			else
			{
				array2 = Blur(array, num4, num5, num3);
				if (mode == GlowMode.Inward)
				{
					bool[] array3 = FindExterior(array, num4, num5);
					float[] array4 = new float[array.Length];
					for (int k = 0; k < array4.Length; k++)
					{
						array4[k] = (array3[k] ? 0f : 1f);
					}
					float[] array5 = Blur(array4, num4, num5, Mathf.Max(0.8f, num3 * 0.5f));
					for (int l = 0; l < array2.Length; l++)
					{
						array2[l] *= array5[l];
					}
				}
			}
			float num6 = 0f;
			for (int m = 0; m < array2.Length; m++)
			{
				if (array2[m] > num6)
				{
					num6 = array2[m];
				}
			}
			if (num6 <= 0f)
			{
				return null;
			}
			Color32[] array6 = (Color32[])(object)new Color32[array2.Length];
			for (int n = 0; n < array2.Length; n++)
			{
				float num7 = Mathf.Pow(Mathf.Clamp01((array2[n] / num6 - 0.03f) / 0.97f), 2f) * 0.95f;
				array6[n] = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, (byte)Mathf.RoundToInt(num7 * 255f));
			}
			Texture2D val = new Texture2D(num4, num5, (TextureFormat)4, false)
			{
				name = "Improved Selector Glow",
				filterMode = (FilterMode)1,
				wrapMode = (TextureWrapMode)1
			};
			val.SetPixels32(array6);
			val.Apply(false, true);
			Vector4 val2 = shape.border + new Vector4((float)num, (float)num, (float)num, (float)num);
			if (mode == GlowMode.InwardSolid)
			{
				int num8 = num + num2;
				int num9 = (Mathf.Min(num4, num5) - 2) / 2;
				((Vector4)(ref val2))..ctor(Mathf.Min(Mathf.Max(val2.x, (float)num8), (float)num9), Mathf.Min(Mathf.Max(val2.y, (float)num8), (float)num9), Mathf.Min(Mathf.Max(val2.z, (float)num8), (float)num9), Mathf.Min(Mathf.Max(val2.w, (float)num8), (float)num9));
			}
			Vector2 val3 = (centered ? new Vector2(0.5f, 0.5f) : new Vector2((shape.pivot.x + (float)num) / (float)num4, (shape.pivot.y + (float)num) / (float)num5));
			Sprite obj = Sprite.Create(val, new Rect(0f, 0f, (float)num4, (float)num5), val3, shape.pixelsPerUnit, 0u, (SpriteMeshType)0, val2);
			((Object)obj).name = "Improved Selector Glow";
			return obj;
		}

		private static float[] Blur(float[] source, int width, int height, float sigma)
		{
			int num = Mathf.CeilToInt(sigma * 2.5f);
			float[] array = new float[num * 2 + 1];
			float num2 = 0f;
			for (int i = -num; i <= num; i++)
			{
				num2 += (array[i + num] = Mathf.Exp((float)(-(i * i)) / (2f * sigma * sigma)));
			}
			for (int j = 0; j < array.Length; j++)
			{
				array[j] /= num2;
			}
			float[] array2 = new float[source.Length];
			for (int k = 0; k < height; k++)
			{
				for (int l = 0; l < width; l++)
				{
					float num3 = 0f;
					for (int m = -num; m <= num; m++)
					{
						int num4 = l + m;
						if (num4 >= 0 && num4 < width)
						{
							num3 += source[k * width + num4] * array[m + num];
						}
					}
					array2[k * width + l] = num3;
				}
			}
			float[] array3 = new float[source.Length];
			for (int n = 0; n < height; n++)
			{
				for (int num5 = 0; num5 < width; num5++)
				{
					float num6 = 0f;
					for (int num7 = -num; num7 <= num; num7++)
					{
						int num8 = n + num7;
						if (num8 >= 0 && num8 < height)
						{
							num6 += array2[num8 * width + num5] * array[num7 + num];
						}
					}
					array3[n * width + num5] = num6;
				}
			}
			return array3;
		}

		private static float[] InwardGradient(bool[] exterior, int width, int height, float sigma)
		{
			float[] array = new float[exterior.Length];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = (exterior[i] ? 0f : 1000000f);
			}
			for (int j = 0; j < height; j++)
			{
				for (int k = 0; k < width; k++)
				{
					int num = j * width + k;
					float num2 = array[num];
					if (k > 0)
					{
						num2 = Mathf.Min(num2, array[num - 1] + 1f);
					}
					if (j > 0)
					{
						num2 = Mathf.Min(num2, array[num - width] + 1f);
						if (k > 0)
						{
							num2 = Mathf.Min(num2, array[num - width - 1] + 1.4142f);
						}
						if (k < width - 1)
						{
							num2 = Mathf.Min(num2, array[num - width + 1] + 1.4142f);
						}
					}
					array[num] = num2;
				}
			}
			for (int num3 = height - 1; num3 >= 0; num3--)
			{
				for (int num4 = width - 1; num4 >= 0; num4--)
				{
					int num5 = num3 * width + num4;
					float num6 = array[num5];
					if (num4 < width - 1)
					{
						num6 = Mathf.Min(num6, array[num5 + 1] + 1f);
					}
					if (num3 < height - 1)
					{
						num6 = Mathf.Min(num6, array[num5 + width] + 1f);
						if (num4 < width - 1)
						{
							num6 = Mathf.Min(num6, array[num5 + width + 1] + 1.4142f);
						}
						if (num4 > 0)
						{
							num6 = Mathf.Min(num6, array[num5 + width - 1] + 1.4142f);
						}
					}
					array[num5] = num6;
				}
			}
			float[] array2 = new float[array.Length];
			for (int l = 0; l < array2.Length; l++)
			{
				if (!exterior[l])
				{
					float num7 = Mathf.Max(array[l] - 1f, 0f);
					array2[l] = Mathf.Exp((0f - num7 * num7) / (2f * sigma * sigma));
				}
			}
			return array2;
		}

		private static bool[] FindExterior(float[] alpha, int width, int height)
		{
			bool[] array = new bool[alpha.Length];
			Stack<int> stack = new Stack<int>();
			for (int i = 0; i < width; i++)
			{
				Seed(alpha, array, stack, i);
				Seed(alpha, array, stack, (height - 1) * width + i);
			}
			for (int j = 0; j < height; j++)
			{
				Seed(alpha, array, stack, j * width);
				Seed(alpha, array, stack, j * width + width - 1);
			}
			while (stack.Count > 0)
			{
				int num = stack.Pop();
				int num2 = num % width;
				int num3 = num / width;
				if (num2 > 0)
				{
					Seed(alpha, array, stack, num - 1);
				}
				if (num2 < width - 1)
				{
					Seed(alpha, array, stack, num + 1);
				}
				if (num3 > 0)
				{
					Seed(alpha, array, stack, num - width);
				}
				if (num3 < height - 1)
				{
					Seed(alpha, array, stack, num + width);
				}
			}
			return array;
		}

		private static void Seed(float[] alpha, bool[] exterior, Stack<int> pending, int index)
		{
			if (!exterior[index] && !(alpha[index] >= 0.05f))
			{
				exterior[index] = true;
				pending.Push(index);
			}
		}
	}
	[BepInPlugin("com.king_tajin.improvedselector", "Improved Selector", "0.3.2")]
	[BepInProcess("Slots and Daggers.exe")]
	public class Plugin : BaseUnityPlugin
	{
		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			FrameScanner.Initialize(((BaseUnityPlugin)this).Logger);
			Harmony val = new Harmony("com.king_tajin.improvedselector");
			Type[] array = new Type[8]
			{
				typeof(SpinnerWheelFramePatch),
				typeof(PowerUpButtonFramePatch),
				typeof(EquipmentButtonFramePatch),
				typeof(ModDongleFramePatch),
				typeof(ProgressIconFramePatch),
				typeof(UpgradeShopOptionFramePatch),
				typeof(ContinueButtonFramePatch),
				typeof(LevelSelectScreenFramePatch)
			};
			foreach (Type type in array)
			{
				try
				{
					val.PatchAll(type);
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)this).Logger.LogError((object)ex);
				}
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Improved Selector loaded");
		}
	}
	[HarmonyPatch(typeof(SpinnerWheel), "OnEnable")]
	internal static class SpinnerWheelFramePatch
	{
		private static void Postfix(SpinnerWheel __instance)
		{
			try
			{
				FrameScanner.AttachField<SpinnerWheel>(__instance, "selectionFrame", inwardOnly: true);
			}
			catch (Exception e)
			{
				FrameScanner.Report(e);
			}
		}
	}
	[HarmonyPatch(typeof(PowerUpButton), "Initialize")]
	internal static class PowerUpButtonFramePatch
	{
		private static void Postfix(PowerUpButton __instance)
		{
			try
			{
				FrameScanner.AttachField<PowerUpButton>(__instance, "selectionFrameActive", inwardOnly: false);
				FrameScanner.AttachField<PowerUpButton>(__instance, "selectionFramePassive", inwardOnly: false);
			}
			catch (Exception e)
			{
				FrameScanner.Report(e);
			}
		}
	}
	[HarmonyPatch(typeof(EquipmentSelectionButton), "OnEnable")]
	internal static class EquipmentButtonFramePatch
	{
		private static void Postfix(EquipmentSelectionButton __instance)
		{
			try
			{
				FrameScanner.AttachField<EquipmentSelectionButton>(__instance, "lockedInFrame", inwardOnly: false);
				FrameScanner.AttachNamed((Component)(object)__instance, "Frame B");
			}
			catch (Exception e)
			{
				FrameScanner.Report(e);
			}
		}
	}
	[HarmonyPatch(typeof(ModDongle), "Setup")]
	internal static class ModDongleFramePatch
	{
		private static void Postfix(ModDongle __instance)
		{
			try
			{
				FrameScanner.AttachNamed((Component)(object)__instance);
			}
			catch (Exception e)
			{
				FrameScanner.Report(e);
			}
		}
	}
	[HarmonyPatch(typeof(ProgressIcon), "Start")]
	internal static class ProgressIconFramePatch
	{
		private static void Postfix(ProgressIcon __instance)
		{
			try
			{
				FrameScanner.AttachNamed((Component)(object)__instance);
			}
			catch (Exception e)
			{
				FrameScanner.Report(e);
			}
		}
	}
	[HarmonyPatch(typeof(UpgradeShopOption), "Initialize")]
	internal static class UpgradeShopOptionFramePatch
	{
		private static void Postfix(UpgradeShopOption __instance)
		{
			try
			{
				FrameScanner.AttachNamed((Component)(object)__instance);
			}
			catch (Exception e)
			{
				FrameScanner.Report(e);
			}
		}
	}
	[HarmonyPatch(typeof(ContinueButtonManager), "Start")]
	internal static class ContinueButtonFramePatch
	{
		private static void Postfix(ContinueButtonManager __instance)
		{
			try
			{
				FrameScanner.AttachNamed((Component)(object)__instance);
				FrameScanner.AttachNamedInField<ContinueButtonManager>(__instance, "animationParent", "Selection Frame");
			}
			catch (Exception e)
			{
				FrameScanner.Report(e);
			}
		}
	}
	[HarmonyPatch(typeof(LevelSelectionManager), "Start")]
	internal static class LevelSelectScreenFramePatch
	{
		private static void Postfix(LevelSelectionManager __instance)
		{
			try
			{
				FrameScanner.AttachNamedInField<LevelSelectionManager>(__instance, "levelSelectionScreenParent", "Frame B");
			}
			catch (Exception e)
			{
				FrameScanner.Report(e);
			}
		}
	}
}