Decompiled source of Cano Signs v1.2.0

Cano.Signs.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("Cano")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Enhanced signs: per-sign neon light switch, bigger character limit, rich-text codes kept in the editor, custom colour, neon, render distance.")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyInformationalVersion("1.2.0")]
[assembly: AssemblyProduct("Cano.Signs")]
[assembly: AssemblyTitle("Cano.Signs")]
[assembly: AssemblyVersion("1.2.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 Cano.Signs
{
	internal sealed class ConfigurationManagerAttributes
	{
		public int? Order;
	}
	internal static class Cfg
	{
		private const string General = "General";

		private const string Editor = "Sign Editor";

		private const string Text = "Sign Text";

		private const string Neon = "Neon Signs";

		private const string Marks = "Neon Light Switch";

		private static ConfigFile _file;

		private static int _order = 100;

		public static ConfigEntry<bool> Enabled;

		public static ConfigEntry<bool> DebugLogging;

		public static ConfigEntry<bool> EnableCustomColor;

		public static ConfigEntry<bool> EnableNeon;

		public static ConfigEntry<bool> EnableMarks;

		public static ConfigEntry<int> CharLimit;

		public static ConfigEntry<bool> KeepRichText;

		public static ConfigEntry<string> CustomColor;

		public static ConfigEntry<float> RenderDistance;

		public static ConfigEntry<string> NeonColor;

		public static ConfigEntry<float> NeonBrightness;

		public static ConfigEntry<bool> NeonLight;

		public static ConfigEntry<KeyboardShortcut> MarkKey;

		public static ConfigEntry<bool> ShowIcons;

		public static ConfigEntry<string> MsgLightOn;

		public static ConfigEntry<string> MsgLightOff;

		public static ConfigEntry<string> MsgLookAtSign;

		public static void Bind(ConfigFile file)
		{
			//IL_0255: Unknown result type (might be due to invalid IL or missing references)
			_file = file;
			ConfigMigration.Run(file, new Dictionary<string, string>
			{
				{ "2 - Editor", "Sign Editor" },
				{ "3 - Text", "Sign Text" },
				{ "4 - Neon", "Neon Signs" },
				{ "5 - Marks", "Neon Light Switch" }
			}, new List<ConfigMigration.KeyMove>
			{
				new ConfigMigration.KeyMove("1 - General", "Enabled", "General", "Enable Mod"),
				new ConfigMigration.KeyMove("3 - Text", "Use Custom Colour", "General", "Enable Custom Text Colour"),
				new ConfigMigration.KeyMove("4 - Neon", "Neon Signs", "General", "Enable Neon Signs")
			}, null);
			Enabled = B("General", "Enable Mod", def: true, "Master switch for the whole mod. Off = signs behave exactly like vanilla.");
			EnableCustomColor = B("General", "Enable Custom Text Colour", def: false, "Master switch for the Sign Text colour: uses the colour set in the Sign Text section as the default sign text colour.");
			EnableNeon = B("General", "Enable Neon Signs", def: false, "Master switch for the Neon Signs sections: glowing sign text (HDR boost, shader glow) and the optional neon light.");
			EnableMarks = B("General", "Enable Neon Light Switch", def: true, "Master switch for the per-sign light switch (hover icon, shortcut and messages in the Neon Light Switch section). Only does something while Neon Signs and Neon Light are on.");
			DebugLogging = B("General", "Debug Logging", def: false, "Writes extra detail about what the mod is doing to the BepInEx log (LogOutput.log). Leave off unless you are tracking down a problem.");
			CharLimit = B("Sign Editor", "Character Limit", 999, "Maximum characters per sign (vanilla is 50).", (AcceptableValueBase)(object)new AcceptableValueRange<int>(50, 999));
			KeepRichText = B("Sign Editor", "Keep Rich Text Codes Visible", def: true, "Keeps tags like <color=red> visible and editable in the sign editor instead of hiding them.");
			CustomColor = B("Sign Text", "Custom Colour (Hex)", "#FFFFFF", "Hex colour, e.g. #FFD27F.");
			RenderDistance = B("Sign Text", "Render Distance (m)", 192f, "Sign text further away than this is not rendered. 192 = always on.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(10f, 192f));
			NeonColor = B("Neon Signs", "Neon Colour (Hex)", "#FFFFFF", "Neon colour for text without colour codes, e.g. #00FFFF.");
			NeonBrightness = B("Neon Signs", "Neon Brightness", 2f, "Text and glow strength. 1 = normal.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 10f));
			NeonLight = B("Neon Signs", "Neon Light", def: true, "Neon signs with text, within 40 m, cast a real point light in the colours of their text, so overlapping neon lights blend like physical light. To stay light on the system only the nearest signs light up, as many as the game's own Point Lights graphics setting allows (Settings > Graphics > Point Lights). Individual signs can be switched off with the Marks shortcut. Turn this off for the best performance.");
			MarkKey = B<KeyboardShortcut>("Neon Light Switch", "Toggle Light Shortcut", new KeyboardShortcut((KeyCode)112, (KeyCode[])(object)new KeyCode[2]
			{
				(KeyCode)304,
				(KeyCode)308
			}), "Flips the sign you look at between tick (glow and light) and X (glow only). Needs Neon Signs and Neon Light on.");
			ShowIcons = B("Neon Light Switch", "Show Hover Icon", def: true, "Show '[shortcut] tick / X' under the name of the sign you look at. Off = no icon (the shortcut still works).");
			MsgLightOn = B("Neon Light Switch", "Light On Message", "Sign light on", "Message when a sign's light is switched on (tick). Empty = no message.");
			MsgLightOff = B("Neon Light Switch", "Light Off Message", "Sign light off", "Message when a sign's light is switched off (X). Empty = no message.");
			MsgLookAtSign = B("Neon Light Switch", "Look At Sign Message", "Look at a sign to toggle its light", "Message when you press the shortcut without looking at a sign. Empty = no message.");
		}

		private static ConfigEntry<T> B<T>(string section, string key, T def, string desc, AcceptableValueBase range = null)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			return _file.Bind<T>(section, key, def, new ConfigDescription(desc, range, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					Order = _order--
				}
			}));
		}

		public static bool TryHex(string hex, out Color c)
		{
			//IL_0001: 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)
			c = Color.white;
			if (string.IsNullOrWhiteSpace(hex))
			{
				return false;
			}
			hex = hex.Trim();
			return ColorUtility.TryParseHtmlString(hex.StartsWith("#") ? hex : ("#" + hex), ref c);
		}
	}
	internal static class ConfigMigration
	{
		internal sealed class KeyMove
		{
			public string OldSection;

			public string OldKey;

			public string NewSection;

			public string NewKey;

			public KeyMove(string oldSection, string oldKey, string newSection, string newKey)
			{
				OldSection = oldSection;
				OldKey = oldKey;
				NewSection = newSection;
				NewKey = newKey;
			}
		}

		private static readonly PropertyInfo OrphansProperty = typeof(ConfigFile).GetProperty("OrphanedEntries", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		private static Dictionary<ConfigDefinition, string> Orphans(ConfigFile config)
		{
			try
			{
				return (OrphansProperty == null) ? null : (OrphansProperty.GetValue(config, null) as Dictionary<ConfigDefinition, string>);
			}
			catch
			{
				return null;
			}
		}

		internal static void Move(ConfigFile config, ConfigEntryBase entry, string oldSection, string oldKey)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Expected O, but got Unknown
			if (config == null || entry == null)
			{
				return;
			}
			Dictionary<ConfigDefinition, string> dictionary = Orphans(config);
			if (dictionary != null)
			{
				ConfigDefinition key = new ConfigDefinition(oldSection, oldKey);
				if (dictionary.TryGetValue(key, out var value))
				{
					dictionary.Remove(key);
					Apply(entry, value);
				}
			}
		}

		internal static void Run(ConfigFile config, IDictionary<string, string> sections, IList<KeyMove> keys, IList<string[]> dropped)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Expected O, but got Unknown
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Expected O, but got Unknown
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Expected O, but got Unknown
			if (config == null)
			{
				return;
			}
			Dictionary<ConfigDefinition, string> dictionary = Orphans(config);
			if (dictionary == null || dictionary.Count == 0)
			{
				return;
			}
			if (dropped != null)
			{
				foreach (string[] item in dropped)
				{
					dictionary.Remove(new ConfigDefinition(item[0], item[1]));
				}
			}
			if (keys != null)
			{
				foreach (KeyMove key3 in keys)
				{
					ConfigDefinition key = new ConfigDefinition(key3.OldSection, key3.OldKey);
					if (dictionary.TryGetValue(key, out var value))
					{
						dictionary.Remove(key);
						dictionary[new ConfigDefinition(key3.NewSection, key3.NewKey)] = value;
					}
				}
			}
			if (sections == null)
			{
				return;
			}
			foreach (ConfigDefinition item2 in new List<ConfigDefinition>(dictionary.Keys))
			{
				if (sections.TryGetValue(item2.Section, out var value2))
				{
					ConfigDefinition key2 = new ConfigDefinition(value2, item2.Key);
					string value3 = dictionary[item2];
					dictionary.Remove(item2);
					if (!dictionary.ContainsKey(key2))
					{
						dictionary[key2] = value3;
					}
				}
			}
		}

		private static void Apply(ConfigEntryBase entry, string raw)
		{
			try
			{
				entry.SetSerializedValue(raw);
			}
			catch
			{
			}
		}
	}
	internal static class SignMarks
	{
		public const string Key = "Cano.Signs_LightOff";

		public static bool IsLightOff(ZNetView v)
		{
			if ((Object)(object)v != (Object)null && v.IsValid())
			{
				return v.GetZDO().GetInt("Cano.Signs_LightOff", 0) == 1;
			}
			return false;
		}

		public static void Poll()
		{
			//IL_000c: 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_0086: Unknown result type (might be due to invalid IL or missing references)
			if (!Input.anyKeyDown)
			{
				return;
			}
			KeyboardShortcut value = Cfg.MarkKey.Value;
			if (!((KeyboardShortcut)(ref value)).IsDown() || Typing())
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				return;
			}
			GameObject hoverObject = ((Humanoid)localPlayer).GetHoverObject();
			Sign val = (((Object)(object)hoverObject != (Object)null) ? hoverObject.GetComponentInParent<Sign>() : null);
			if ((Object)(object)val == (Object)null)
			{
				Say(Cfg.MsgLookAtSign.Value);
				return;
			}
			ZNetView component = ((Component)val).GetComponent<ZNetView>();
			if (!((Object)(object)component == (Object)null) && component.IsValid() && PrivateArea.CheckAccess(((Component)val).transform.position, 0f, true, false))
			{
				bool flag = !IsLightOff(component);
				if (!component.IsOwner())
				{
					component.ClaimOwnership();
				}
				component.GetZDO().Set("Cano.Signs_LightOff", flag ? 1 : 0);
				SignVisual component2 = ((Component)val).GetComponent<SignVisual>();
				if ((Object)(object)component2 != (Object)null)
				{
					component2.Apply();
				}
				Say(flag ? Cfg.MsgLightOff.Value : Cfg.MsgLightOn.Value);
			}
		}

		private static void Say(string text)
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer != (Object)null && !string.IsNullOrEmpty(text))
			{
				((Character)localPlayer).Message((MessageType)2, text, 0, (Sprite)null, false);
			}
		}

		private static bool Typing()
		{
			if ((!((Object)(object)Chat.instance != (Object)null) || !Chat.instance.HasFocus()) && !Console.IsVisible() && !TextInput.IsVisible())
			{
				return Menu.IsVisible();
			}
			return true;
		}

		public static string Label(KeyboardShortcut s)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			StringBuilder stringBuilder = new StringBuilder();
			foreach (KeyCode modifier in ((KeyboardShortcut)(ref s)).Modifiers)
			{
				stringBuilder.Append(((object)modifier/*cast due to .constrained prefix*/).ToString().Replace("Left", "").Replace("Right", "")).Append('+');
			}
			return stringBuilder.Append(((KeyboardShortcut)(ref s)).MainKey).ToString();
		}
	}
	internal static class MarkHint
	{
		private const string Reserve = "   ";

		private static int _frame = -10;

		private static bool _off;

		private static bool _shown;

		private static string _label;

		private static string _hint;

		private static string _placedText;

		private static Image _icon;

		private static Sprite _tick;

		private static Sprite _cross;

		public static void Invalidate()
		{
			_label = null;
			_hint = null;
		}

		public static string Request(bool lightOff)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			_frame = Time.frameCount;
			_off = lightOff;
			if (_hint == null)
			{
				_label = "[" + SignMarks.Label(Cfg.MarkKey.Value) + "]";
				_hint = "\n" + _label + "   ";
			}
			return _hint;
		}

		public static void Tick()
		{
			if (Time.frameCount - _frame > 1)
			{
				if (_shown)
				{
					Hide();
				}
				return;
			}
			TMP_Text val = (TMP_Text)(object)(((Object)(object)Hud.instance != (Object)null) ? Hud.instance.m_hoverName : null);
			if (((Object)(object)val == (Object)null || !((Component)val).gameObject.activeInHierarchy || !Place(val)) && _shown)
			{
				Hide();
			}
		}

		public static void Reset()
		{
			_frame = -10;
			Hide();
		}

		private static void Hide()
		{
			_shown = false;
			_placedText = null;
			if ((Object)(object)_icon != (Object)null)
			{
				((Component)_icon).gameObject.SetActive(false);
			}
		}

		private static bool Place(TMP_Text text)
		{
			//IL_0043: 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_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: 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_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: 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_00ba: Unknown result type (might be due to invalid IL or missing references)
			if (!EnsureIcon(text))
			{
				return false;
			}
			if (!string.Equals(text.text, _placedText, StringComparison.Ordinal))
			{
				text.ForceMeshUpdate(false, false);
				TMP_TextInfo textInfo = text.textInfo;
				int num = FindLabelEnd(textInfo);
				if (num < 0)
				{
					return false;
				}
				TMP_CharacterInfo val = textInfo.characterInfo[num];
				float num2 = text.fontSize * 0.85f;
				_placedText = text.text;
				((Graphic)_icon).rectTransform.sizeDelta = new Vector2(num2, num2);
				((Transform)((Graphic)_icon).rectTransform).localPosition = new Vector3(val.topRight.x + num2 * 0.6f, (val.topRight.y + val.bottomRight.y) * 0.5f, 0f);
			}
			Sprite val2 = (_off ? _cross : _tick);
			if ((Object)(object)_icon.sprite != (Object)(object)val2)
			{
				_icon.sprite = val2;
			}
			if (!_shown)
			{
				((Component)_icon).gameObject.SetActive(true);
				_shown = true;
			}
			return true;
		}

		private static int FindLabelEnd(TMP_TextInfo info)
		{
			int characterCount = info.characterCount;
			for (int i = 0; i + _label.Length <= characterCount; i++)
			{
				bool flag = true;
				for (int j = 0; j < _label.Length && flag; j++)
				{
					flag = info.characterInfo[i + j].character == _label[j];
				}
				if (flag)
				{
					return i + _label.Length - 1;
				}
			}
			return -1;
		}

		private static bool EnsureIcon(TMP_Text text)
		{
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_icon != (Object)null)
			{
				return true;
			}
			_tick = Load("Mark_tick.png");
			_cross = Load("Mark_cross.png");
			if ((Object)(object)_tick == (Object)null || (Object)(object)_cross == (Object)null)
			{
				return false;
			}
			GameObject val = new GameObject("Cano.Signs.MarkIcon", new Type[2]
			{
				typeof(RectTransform),
				typeof(Image)
			});
			val.transform.SetParent(text.transform, false);
			_icon = val.GetComponent<Image>();
			((Graphic)_icon).raycastTarget = false;
			_icon.preserveAspect = true;
			return true;
		}

		private static Sprite Load(string resource)
		{
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource);
			if (stream == null)
			{
				SignsPlugin.Log.LogWarning((object)("Embedded image missing: " + resource));
				return null;
			}
			byte[] array = new byte[stream.Length];
			stream.Read(array, 0, array.Length);
			Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false)
			{
				filterMode = (FilterMode)1
			};
			ImageConversion.LoadImage(val, array);
			return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f), 100f);
		}
	}
	[BepInPlugin("Cano.Signs", "Cano.Signs", "1.2.0")]
	public class SignsPlugin : BaseUnityPlugin
	{
		[HarmonyPatch]
		private static class Patches
		{
			private static bool _richCaptured;

			private static bool _vanillaRich;

			[HarmonyPostfix]
			[HarmonyPatch(typeof(Sign), "Awake")]
			private static void SignAwake(Sign __instance)
			{
				if (VisualOn)
				{
					SignVisual.Attach(__instance);
				}
			}

			[HarmonyPostfix]
			[HarmonyPatch(typeof(Sign), "GetHoverText")]
			private static void SignHover(Sign __instance, ref string __result)
			{
				if (MarksOn && Cfg.ShowIcons.Value && !string.IsNullOrEmpty(__result))
				{
					__result += MarkHint.Request(SignMarks.IsLightOff(((Component)__instance).GetComponent<ZNetView>()));
				}
			}

			[HarmonyPrefix]
			[HarmonyPatch(typeof(TextInput), "RequestText")]
			private static void RequestText(TextInput __instance, TextReceiver sign, ref int charLimit)
			{
				if (!(sign is Sign))
				{
					return;
				}
				bool value = Cfg.Enabled.Value;
				if (value && Cfg.CharLimit.Value > charLimit)
				{
					charLimit = Cfg.CharLimit.Value;
				}
				TMP_InputField inputField = (TMP_InputField)(object)__instance.m_inputField;
				if (!((Object)(object)inputField == (Object)null))
				{
					if (!_richCaptured)
					{
						_vanillaRich = inputField.richText;
						_richCaptured = true;
					}
					inputField.richText = (!value || !Cfg.KeepRichText.Value) && _vanillaRich;
				}
			}
		}

		public const string Guid = "Cano.Signs";

		public const string Version = "1.2.0";

		internal const float MaxDistance = 192f;

		internal static bool VisualOn;

		internal static bool TickOn;

		internal static bool MarksOn;

		internal static bool LimitDistance;

		internal static bool CustomOn;

		internal static bool NeonOn;

		internal static bool NeonLight;

		internal static float Distance;

		internal static float Brightness;

		internal static Color Custom;

		internal static Color Neon;

		internal static ManualLogSource Log;

		private static bool _wasVisual;

		private static bool _wasMarks;

		private Harmony _harmony;

		internal static void Debug(string message)
		{
			if (Cfg.DebugLogging != null && Cfg.DebugLogging.Value)
			{
				Log.LogInfo((object)("[debug] " + message));
			}
		}

		private void Awake()
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Cfg.Bind(((BaseUnityPlugin)this).Config);
			((BaseUnityPlugin)this).Config.SettingChanged += delegate
			{
				Refresh();
			};
			Refresh();
			_harmony = new Harmony("Cano.Signs");
			_harmony.PatchAll(typeof(Patches));
		}

		private void Update()
		{
			SignVisual.TickSlice();
			SignVisual.FadeLights();
			if (MarksOn)
			{
				SignMarks.Poll();
			}
		}

		private void LateUpdate()
		{
			if (MarksOn)
			{
				MarkHint.Tick();
			}
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			SignVisual.DetachAll();
		}

		private void Refresh()
		{
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			bool value = Cfg.Enabled.Value;
			Distance = Cfg.RenderDistance.Value;
			LimitDistance = value && Distance < 191.99f;
			CustomOn = value && Cfg.EnableCustomColor.Value && Cfg.TryHex(Cfg.CustomColor.Value, out Custom);
			NeonOn = value && Cfg.EnableNeon.Value;
			NeonLight = Cfg.NeonLight.Value;
			Brightness = Cfg.NeonBrightness.Value;
			if (!Cfg.TryHex(Cfg.NeonColor.Value, out Neon))
			{
				Neon = Color.white;
			}
			MarksOn = NeonOn && NeonLight && Cfg.EnableMarks.Value;
			if (_wasMarks && !MarksOn)
			{
				MarkHint.Reset();
			}
			_wasMarks = MarksOn;
			MarkHint.Invalidate();
			Debug("Refresh: neon=" + NeonOn + " light=" + NeonLight + " marks=" + MarksOn + " custom=" + CustomOn + " limitDistance=" + LimitDistance);
			TickOn = NeonOn || LimitDistance;
			VisualOn = CustomOn || TickOn;
			((Behaviour)this).enabled = TickOn;
			if (VisualOn)
			{
				if (!_wasVisual)
				{
					SignVisual.AttachAll();
				}
				SignVisual.ApplyAll();
			}
			else if (_wasVisual)
			{
				SignVisual.DetachAll();
			}
			_wasVisual = VisualOn;
		}
	}
	internal static class SignColors
	{
		public static bool HasVisibleText(string text)
		{
			if (string.IsNullOrEmpty(text))
			{
				return false;
			}
			for (int i = 0; i < text.Length; i++)
			{
				if (text[i] == '<')
				{
					int num = text.IndexOf('>', i + 1);
					if (num >= 0)
					{
						i = num;
						continue;
					}
				}
				if (!char.IsWhiteSpace(text[i]))
				{
					return true;
				}
			}
			return false;
		}

		public static Color Blended(string text, Color baseColor)
		{
			//IL_0013: 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_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_013c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0198: Unknown result type (might be due to invalid IL or missing references)
			if (string.IsNullOrEmpty(text) || text.IndexOf('<') < 0)
			{
				return baseColor;
			}
			Dictionary<int, int> dictionary = new Dictionary<int, int>();
			Dictionary<int, Color> dictionary2 = new Dictionary<int, Color>();
			List<Color> stack = new List<Color>();
			int num = 0;
			while (num < text.Length)
			{
				int num2 = text.IndexOf('<', num);
				int num3 = ((num2 < 0) ? (-1) : text.IndexOf('>', num2 + 1));
				if (num3 < 0)
				{
					Vote(text, num, text.Length, stack, baseColor, dictionary, dictionary2);
					break;
				}
				Vote(text, num, num2, stack, baseColor, dictionary, dictionary2);
				HandleTag(text.Substring(num2 + 1, num3 - num2 - 1).Trim(), stack, baseColor);
				num = num3 + 1;
			}
			if (dictionary.Count == 0)
			{
				return baseColor;
			}
			if (dictionary.Count == 1)
			{
				using Dictionary<int, Color>.ValueCollection.Enumerator enumerator = dictionary2.Values.GetEnumerator();
				if (enumerator.MoveNext())
				{
					return enumerator.Current;
				}
			}
			float num4 = 0f;
			float num5 = 0f;
			float num6 = 0f;
			Color val;
			foreach (KeyValuePair<int, int> item in dictionary)
			{
				val = dictionary2[item.Key];
				Color linear = ((Color)(ref val)).linear;
				num4 += linear.r * (float)item.Value;
				num5 += linear.g * (float)item.Value;
				num6 += linear.b * (float)item.Value;
			}
			float num7 = Mathf.Max(num4, Mathf.Max(num5, num6));
			if (num7 <= 1E-05f)
			{
				return baseColor;
			}
			val = new Color(num4 / num7, num5 / num7, num6 / num7, 1f);
			return ((Color)(ref val)).gamma;
		}

		private static void HandleTag(string tag, List<Color> stack, Color baseColor)
		{
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			if (tag.Length == 0)
			{
				return;
			}
			string text = tag.ToLowerInvariant();
			if (text == "/color")
			{
				if (stack.Count > 0)
				{
					stack.RemoveAt(stack.Count - 1);
				}
				return;
			}
			string text2 = (text.StartsWith("color=") ? tag.Substring(6) : (text.StartsWith("#") ? tag : null));
			if (text2 != null)
			{
				text2 = text2.Trim().Trim('"', '\'').Trim();
				Color val = default(Color);
				stack.Add(ColorUtility.TryParseHtmlString(text2, ref val) ? val : ((stack.Count > 0) ? stack[stack.Count - 1] : baseColor));
			}
		}

		private static void Vote(string text, int from, int to, List<Color> stack, Color baseColor, Dictionary<int, int> weights, Dictionary<int, Color> colors)
		{
			//IL_003f: 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_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: 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_0099: Unknown result type (might be due to invalid IL or missing references)
			int num = 0;
			for (int i = from; i < to; i++)
			{
				if (!char.IsWhiteSpace(text[i]))
				{
					num++;
				}
			}
			if (num != 0)
			{
				Color val = ((stack.Count > 0) ? stack[stack.Count - 1] : baseColor);
				int key = (Mathf.RoundToInt(val.r * 255f) << 16) | (Mathf.RoundToInt(val.g * 255f) << 8) | Mathf.RoundToInt(val.b * 255f);
				weights.TryGetValue(key, out var value);
				weights[key] = value + num;
				colors[key] = val;
			}
		}

		public static Color ForLight(Color c)
		{
			//IL_0000: 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_001b: Unknown result type (might be due to invalid IL or missing references)
			float num = default(float);
			float num2 = default(float);
			float num3 = default(float);
			Color.RGBToHSV(c, ref num, ref num2, ref num3);
			if (!(num3 < 0.05f))
			{
				return Color.HSVToRGB(num, num2, 1f);
			}
			return Color.white;
		}
	}
	internal sealed class SignVisual : MonoBehaviour
	{
		private const float LightRange = 40f;

		private static readonly List<SignVisual> All = new List<SignVisual>();

		private static int _cursor;

		private const float FadeSpeed = 2.5f;

		private const float SwapMargin = 3f;

		private static readonly List<SignVisual> Lit = new List<SignVisual>();

		private static readonly List<SignVisual> LightOn = new List<SignVisual>();

		private static Dictionary<int, Material> _materials = new Dictionary<int, Material>();

		private TMP_Text _text;

		private ZNetView _view;

		private Color _origColor;

		private Color _glowColor;

		private Material _origMaterial;

		private string _lastText;

		private bool _known;

		private bool _hasText;

		private bool _glowing;

		private Light _light;

		private bool _slot;

		private float _lightDist;

		private float _fade;

		private float _baseIntensity;

		private float _side;

		private int _lightKey;

		public static void Attach(Sign sign)
		{
			if ((Object)(object)sign != (Object)null && (Object)(object)((Component)sign).GetComponent<SignVisual>() == (Object)null)
			{
				((Component)sign).gameObject.AddComponent<SignVisual>();
			}
		}

		public static void AttachAll()
		{
			Sign[] array = Object.FindObjectsByType<Sign>((FindObjectsSortMode)0);
			for (int i = 0; i < array.Length; i++)
			{
				Attach(array[i]);
			}
		}

		public static void ApplyAll()
		{
			Dictionary<int, Material> materials = _materials;
			_materials = new Dictionary<int, Material>();
			for (int num = All.Count - 1; num >= 0; num--)
			{
				All[num].Apply();
			}
			DestroyMaterials(materials);
		}

		private static void DestroyMaterials(Dictionary<int, Material> set)
		{
			foreach (Material value in set.Values)
			{
				if ((Object)(object)value != (Object)null)
				{
					Object.Destroy((Object)(object)value);
				}
			}
			set.Clear();
		}

		public static void DetachAll()
		{
			SignVisual[] array = All.ToArray();
			foreach (SignVisual obj in array)
			{
				obj.Restore();
				Object.Destroy((Object)(object)obj);
			}
			All.Clear();
			Lit.Clear();
			LightOn.Clear();
			DestroyMaterials(_materials);
		}

		public static void TickSlice()
		{
			if (All.Count == 0)
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			TrimLights();
			int num = Mathf.Max(1, All.Count / 30);
			while (num > 0 && All.Count > 0)
			{
				if (_cursor >= All.Count)
				{
					_cursor = 0;
				}
				All[_cursor++].Tick(localPlayer);
				num--;
			}
		}

		private static int LightCap()
		{
			int lightLimit = LightLod.m_lightLimit;
			if (lightLimit >= 0)
			{
				return lightLimit;
			}
			return int.MaxValue;
		}

		private static SignVisual Farthest()
		{
			SignVisual signVisual = null;
			for (int i = 0; i < Lit.Count; i++)
			{
				if ((Object)(object)signVisual == (Object)null || Lit[i]._lightDist > signVisual._lightDist)
				{
					signVisual = Lit[i];
				}
			}
			return signVisual;
		}

		private static void TrimLights()
		{
			int num = LightCap();
			while (Lit.Count > num)
			{
				Farthest().ReleaseSlot();
			}
		}

		public static void FadeLights()
		{
			if (LightOn.Count == 0)
			{
				return;
			}
			float num = Time.unscaledDeltaTime * 2.5f;
			for (int num2 = LightOn.Count - 1; num2 >= 0; num2--)
			{
				SignVisual signVisual = LightOn[num2];
				if ((Object)(object)signVisual == (Object)null || (Object)(object)signVisual._light == (Object)null)
				{
					LightOn.RemoveAt(num2);
				}
				else
				{
					signVisual._fade = Mathf.MoveTowards(signVisual._fade, signVisual._slot ? 1f : 0f, num);
					if (signVisual._fade <= 0f && !signVisual._slot)
					{
						((Behaviour)signVisual._light).enabled = false;
						LightOn.RemoveAt(num2);
					}
					else
					{
						float num3 = signVisual._baseIntensity * signVisual._fade;
						if (!Mathf.Approximately(signVisual._light.intensity, num3))
						{
							signVisual._light.intensity = num3;
						}
					}
				}
			}
		}

		private void Awake()
		{
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			Sign component = ((Component)this).GetComponent<Sign>();
			_text = (TMP_Text)(object)(((Object)(object)component != (Object)null) ? component.m_textWidget : null);
			_view = ((Component)this).GetComponent<ZNetView>();
			if (!((Object)(object)_text == (Object)null))
			{
				_origColor = ((Graphic)_text).color;
				_origMaterial = _text.fontSharedMaterial;
			}
		}

		private void OnEnable()
		{
			All.Add(this);
			Apply();
		}

		private void OnDisable()
		{
			All.Remove(this);
			LightOff();
		}

		private void OnDestroy()
		{
			LightOff();
			if ((Object)(object)_light != (Object)null)
			{
				Object.Destroy((Object)(object)((Component)_light).gameObject);
			}
		}

		public void Apply()
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_text == (Object)null))
			{
				((Graphic)_text).color = (SignsPlugin.NeonOn ? SignsPlugin.Neon : (SignsPlugin.CustomOn ? SignsPlugin.Custom : _origColor));
				if (!SignsPlugin.LimitDistance)
				{
					((Behaviour)_text).enabled = true;
				}
				_known = false;
				Player localPlayer = Player.m_localPlayer;
				SyncNeon();
				UpdateLight(((Behaviour)_text).enabled, DistanceTo(localPlayer), localPlayer);
			}
		}

		private void Tick(Player player)
		{
			if (!((Object)(object)_text == (Object)null))
			{
				float num = DistanceTo(player);
				bool flag = !SignsPlugin.LimitDistance || (Object)(object)player == (Object)null || num <= SignsPlugin.Distance;
				if (((Behaviour)_text).enabled != flag)
				{
					((Behaviour)_text).enabled = flag;
				}
				SyncNeon();
				UpdateLight(flag, num, player);
			}
		}

		private float DistanceTo(Player p)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)p != (Object)null))
			{
				return 0f;
			}
			return Vector3.Distance(((Component)p).transform.position, ((Component)this).transform.position);
		}

		private void Restore()
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_text != (Object)null)
			{
				((Graphic)_text).color = _origColor;
				RestoreMaterial();
				((Behaviour)_text).enabled = true;
			}
			_glowing = false;
			LightOff();
		}

		private void SyncNeon()
		{
			//IL_0044: 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_004e: Unknown result type (might be due to invalid IL or missing references)
			string text = _text.text;
			bool flag = !_known || text != _lastText;
			if (flag)
			{
				_known = true;
				_lastText = text;
				_hasText = HasText(text);
				_glowColor = SignColors.Blended(text, SignsPlugin.Neon);
			}
			bool flag2 = SignsPlugin.NeonOn && _hasText;
			if (flag2)
			{
				if (flag || !_glowing)
				{
					ApplyNeonMaterial();
				}
			}
			else if (_glowing)
			{
				RestoreMaterial();
			}
			_glowing = flag2;
		}

		private bool HasText(string text)
		{
			if ((Object)(object)_view != (Object)null && _view.IsValid() && _view.GetZDO().GetString(ZDOVars.s_text, (string)null) == null)
			{
				return false;
			}
			return SignColors.HasVisibleText(text);
		}

		private void ApplyNeonMaterial()
		{
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Expected O, but got Unknown
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)_origMaterial == (Object)null))
			{
				float brightness = SignsPlugin.Brightness;
				Color val = default(Color);
				((Color)(ref val))..ctor(_glowColor.r, _glowColor.g, _glowColor.b, 1f);
				int instanceID = ((Object)_origMaterial).GetInstanceID();
				instanceID = instanceID * 31 + Mathf.RoundToInt(val.r * 64f);
				instanceID = instanceID * 31 + Mathf.RoundToInt(val.g * 64f);
				instanceID = instanceID * 31 + Mathf.RoundToInt(val.b * 64f);
				instanceID = instanceID * 31 + Mathf.RoundToInt(brightness * 20f);
				if (!_materials.TryGetValue(instanceID, out var value) || (Object)(object)value == (Object)null)
				{
					value = new Material(_origMaterial);
					Configure(value, val, brightness);
					_materials[instanceID] = value;
				}
				_text.fontSharedMaterial = value;
			}
		}

		private static void Configure(Material m, Color g, float b)
		{
			//IL_000e: 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_00c0: Unknown result type (might be due to invalid IL or missing references)
			SetColor(m, "_FaceColor", new Color(b, b, b, 1f));
			m.EnableKeyword("GLOW_ON");
			m.EnableKeyword("UNDERLAY_ON");
			float num = Mathf.Clamp01((b - 0.5f) / 9.5f);
			SetColor(m, "_GlowColor", g);
			SetFloat(m, "_GlowPower", Mathf.Lerp(0.3f, 1f, num));
			SetFloat(m, "_GlowOuter", Mathf.Lerp(0.4f, 1f, num));
			SetFloat(m, "_GlowInner", 0.1f);
			SetFloat(m, "_GlowOffset", 0f);
			g.a = Mathf.Lerp(0.55f, 1f, num);
			SetColor(m, "_UnderlayColor", g);
			SetFloat(m, "_UnderlayOffsetX", 0f);
			SetFloat(m, "_UnderlayOffsetY", 0f);
			SetFloat(m, "_UnderlayDilate", Mathf.Lerp(0.3f, 1f, num));
			SetFloat(m, "_UnderlaySoftness", Mathf.Lerp(0.4f, 1f, num));
		}

		private void RestoreMaterial()
		{
			if ((Object)(object)_origMaterial != (Object)null && (Object)(object)_text.fontSharedMaterial != (Object)(object)_origMaterial)
			{
				_text.fontSharedMaterial = _origMaterial;
			}
		}

		private static void SetColor(Material m, string prop, Color c)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			if (m.HasProperty(prop))
			{
				m.SetColor(prop, c);
			}
		}

		private static void SetFloat(Material m, string prop, float v)
		{
			if (m.HasProperty(prop))
			{
				m.SetFloat(prop, v);
			}
		}

		private void ReleaseSlot()
		{
			if (_slot)
			{
				Lit.Remove(this);
				_slot = false;
			}
		}

		private void LightOff()
		{
			ReleaseSlot();
			if ((Object)(object)_light != (Object)null)
			{
				((Behaviour)_light).enabled = false;
				_light.intensity = 0f;
			}
			_fade = 0f;
			LightOn.Remove(this);
		}

		private void UpdateLight(bool shown, float dist, Player player)
		{
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b6: Expected O, but got Unknown
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0204: Unknown result type (might be due to invalid IL or missing references)
			//IL_024c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0251: Unknown result type (might be due to invalid IL or missing references)
			//IL_0255: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0264: Unknown result type (might be due to invalid IL or missing references)
			bool flag = _glowing && SignsPlugin.NeonLight && shown && dist <= 40f && !SignMarks.IsLightOff(_view);
			if (flag)
			{
				_lightDist = dist;
				if (!_slot)
				{
					if (Lit.Count >= LightCap())
					{
						SignVisual signVisual = Farthest();
						if ((Object)(object)signVisual == (Object)null || signVisual._lightDist <= dist + 3f)
						{
							flag = false;
						}
						else
						{
							signVisual.ReleaseSlot();
						}
					}
					if (flag)
					{
						Lit.Add(this);
						_slot = true;
					}
				}
			}
			if (!flag)
			{
				ReleaseSlot();
				return;
			}
			if ((Object)(object)_light == (Object)null)
			{
				GameObject val = new GameObject("Cano_NeonLight");
				val.transform.SetParent(((Component)this).transform, false);
				_light = val.AddComponent<Light>();
				_light.type = (LightType)2;
				_light.shadows = (LightShadows)0;
				((Behaviour)_light).enabled = false;
			}
			float brightness = SignsPlugin.Brightness;
			Color val2 = SignColors.ForLight(_glowColor);
			int num = Mathf.RoundToInt(val2.r * 255f) * 31 + Mathf.RoundToInt(val2.g * 255f);
			num = num * 31 + Mathf.RoundToInt(val2.b * 255f);
			num = num * 31 + Mathf.RoundToInt(brightness * 20f);
			if (num != _lightKey || !((Behaviour)_light).enabled)
			{
				_lightKey = num;
				_light.color = val2;
				_baseIntensity = Mathf.Clamp(0.4f + brightness * 0.5f, 0.5f, 5.5f);
				_light.range = Mathf.Clamp(3f + brightness * 0.8f, 3f, 11f);
			}
			Transform transform = _text.transform;
			Vector3 forward = transform.forward;
			float num2 = (((Object)(object)player != (Object)null && Vector3.Dot(((Component)player).transform.position - transform.position, forward) < 0f) ? (-1f) : 1f);
			if (num2 != _side || !((Behaviour)_light).enabled)
			{
				_side = num2;
				((Component)_light).transform.position = transform.position + forward * num2 * 0.35f;
			}
			if (!((Behaviour)_light).enabled)
			{
				_fade = 0f;
				_light.intensity = 0f;
				((Behaviour)_light).enabled = true;
				if (!LightOn.Contains(this))
				{
					LightOn.Add(this);
				}
			}
		}
	}
}