Decompiled source of ColorfulLights v1.11.0

ColorfulLights.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using ComfyLib;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ColorfulLights")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ColorfulLights")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7b47b8f8-811b-488b-a8ee-d20ec60f25f3")]
[assembly: AssemblyFileVersion("1.11.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.11.0.0")]
[module: UnverifiableCode]
namespace ComfyLib
{
	public static class ConfigFileExtensions
	{
		internal sealed class ConfigurationManagerAttributes
		{
			public Action<ConfigEntryBase> CustomDrawer;

			public bool? Browsable;

			public bool? HideDefaultButton;

			public int? Order;
		}

		private static readonly Dictionary<string, int> _sectionToSettingOrder = new Dictionary<string, int>();

		private static int GetSettingOrder(string section)
		{
			if (!_sectionToSettingOrder.TryGetValue(section, out var value))
			{
				value = 0;
			}
			_sectionToSettingOrder[section] = value - 1;
			return value;
		}

		public static ConfigEntry<T> BindInOrder<T>(this ConfigFile config, string section, string key, T defaultValue, string description, AcceptableValueBase acceptableValues)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			return config.Bind<T>(section, key, defaultValue, new ConfigDescription(description, acceptableValues, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					Order = GetSettingOrder(section)
				}
			}));
		}

		public static ConfigEntry<T> BindInOrder<T>(this ConfigFile config, string section, string key, T defaultValue, string description, Action<ConfigEntryBase> customDrawer = null, bool browsable = true, bool hideDefaultButton = false, bool hideSettingName = false)
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Expected O, but got Unknown
			return config.Bind<T>(section, key, defaultValue, new ConfigDescription(description, (AcceptableValueBase)null, new object[1]
			{
				new ConfigurationManagerAttributes
				{
					Browsable = browsable,
					CustomDrawer = customDrawer,
					HideDefaultButton = hideDefaultButton,
					Order = GetSettingOrder(section)
				}
			}));
		}

		public static void OnSettingChanged<T>(this ConfigEntry<T> configEntry, Action settingChangedHandler)
		{
			configEntry.SettingChanged += delegate
			{
				settingChangedHandler();
			};
		}

		public static void OnSettingChanged<T>(this ConfigEntry<T> configEntry, Action<T> settingChangedHandler)
		{
			configEntry.SettingChanged += delegate(object _, EventArgs eventArgs)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				settingChangedHandler((T)((SettingChangedEventArgs)eventArgs).ChangedSetting.BoxedValue);
			};
		}

		public static void OnSettingChanged<T>(this ConfigEntry<T> configEntry, Action<ConfigEntry<T>> settingChangedHandler)
		{
			configEntry.SettingChanged += delegate(object _, EventArgs eventArgs)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				settingChangedHandler((ConfigEntry<T>)((SettingChangedEventArgs)eventArgs).ChangedSetting.BoxedValue);
			};
		}
	}
	public sealed class ExtendedColorConfigEntry
	{
		private static readonly Texture2D _colorTexture = GUIBuilder.CreateColorTexture(10, 10, Color.white);

		private readonly HexColorTextField _hexInput = new HexColorTextField();

		private readonly ColorPalette _colorPalette;

		private bool _showSliders;

		public ConfigEntry<Color> ConfigEntry { get; }

		public Color Value { get; private set; }

		public ColorFloatTextField RedInput { get; } = new ColorFloatTextField("R");


		public ColorFloatTextField GreenInput { get; } = new ColorFloatTextField("G");


		public ColorFloatTextField BlueInput { get; } = new ColorFloatTextField("B");


		public ColorFloatTextField AlphaInput { get; } = new ColorFloatTextField("A");


		public ExtendedColorConfigEntry(ConfigFile config, string section, string key, Color defaultValue, string description)
		{
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			ConfigEntry = config.BindInOrder<Color>(section, key, defaultValue, description, Drawer, browsable: true, hideDefaultButton: false, hideSettingName: false);
			SetValue(ConfigEntry.Value);
		}

		public ExtendedColorConfigEntry(ConfigFile config, string section, string key, Color defaultValue, string description, string colorPaletteKey)
			: this(config, section, key, defaultValue, description)
		{
			//IL_0004: 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)
			ConfigEntry<string> paletteConfigEntry = config.BindInOrder(section, colorPaletteKey, ColorUtility.ToHtmlStringRGBA(defaultValue) + ",FF0000FF,00FF00FF,0000FFFF", "Color palette for: [" + section + "] " + key, null, browsable: false);
			_colorPalette = new ColorPalette(this, paletteConfigEntry);
		}

		public void SetValue(Color value)
		{
			//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_0019: 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)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			ConfigEntry.Value = value;
			Value = value;
			RedInput.SetValue(value.r);
			GreenInput.SetValue(value.g);
			BlueInput.SetValue(value.b);
			AlphaInput.SetValue(value.a);
			_hexInput.SetValue(value);
		}

		public void Drawer(ConfigEntryBase configEntry)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: 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_0062: 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)
			//IL_008c: Invalid comparison between Unknown and I4
			//IL_001b: 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)
			//IL_008e: 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_01b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01da: Unknown result type (might be due to invalid IL or missing references)
			//IL_01df: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ee: 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)
			Color val = (Color)configEntry.BoxedValue;
			if (GUIFocus.HasChanged() || GUIHelper.IsEnterPressed() || Value != val)
			{
				SetValue(val);
			}
			GUILayout.BeginVertical(GUI.skin.box, Array.Empty<GUILayoutOption>());
			GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
			_hexInput.DrawField();
			GUILayout.Space(3f);
			GUIHelper.BeginColor(val);
			GUILayout.Label(string.Empty, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(true) });
			if ((int)Event.current.type == 7)
			{
				GUI.DrawTexture(GUILayoutUtility.GetLastRect(), (Texture)(object)_colorTexture);
			}
			GUIHelper.EndColor();
			GUILayout.Space(3f);
			if (GUILayout.Button(_showSliders ? "∨" : "≡", (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.MinWidth(40f),
				GUILayout.ExpandWidth(false)
			}))
			{
				_showSliders = !_showSliders;
			}
			GUILayout.EndHorizontal();
			if (_showSliders)
			{
				GUILayout.Space(4f);
				GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
				RedInput.DrawField();
				GUILayout.Space(3f);
				GreenInput.DrawField();
				GUILayout.Space(3f);
				BlueInput.DrawField();
				GUILayout.Space(3f);
				AlphaInput.DrawField();
				GUILayout.EndHorizontal();
			}
			if (_colorPalette != null)
			{
				GUILayout.Space(5f);
				_colorPalette.DrawColorPalette();
			}
			GUILayout.EndVertical();
			Color val2 = default(Color);
			((Color)(ref val2))..ctor(RedInput.CurrentValue, GreenInput.CurrentValue, BlueInput.CurrentValue, AlphaInput.CurrentValue);
			if (val2 != val)
			{
				configEntry.BoxedValue = val2;
				SetValue(val2);
			}
			else if (_hexInput.CurrentValue != val)
			{
				configEntry.BoxedValue = _hexInput.CurrentValue;
				SetValue(_hexInput.CurrentValue);
			}
		}
	}
	public sealed class ColorPalette
	{
		private static readonly char[] _partSeparator = new char[1] { ',' };

		private static readonly string _partJoiner = ",";

		private static readonly Texture2D _colorTexture = GUIBuilder.CreateColorTexture(10, 10, Color.white);

		private readonly ExtendedColorConfigEntry _colorConfigEntry;

		private readonly ConfigEntry<string> _paletteConfigEntry;

		private readonly List<Color> _paletteColors;

		public ColorPalette(ExtendedColorConfigEntry colorConfigEntry, ConfigEntry<string> paletteConfigEntry)
		{
			_colorConfigEntry = colorConfigEntry;
			_paletteConfigEntry = paletteConfigEntry;
			_paletteColors = new List<Color>();
			LoadPalette();
		}

		private void LoadPalette()
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			_paletteColors.Clear();
			string[] array = _paletteConfigEntry.Value.Split(_partSeparator, StringSplitOptions.RemoveEmptyEntries);
			Color item = default(Color);
			foreach (string text in array)
			{
				if (ColorUtility.TryParseHtmlString("#" + text, ref item))
				{
					_paletteColors.Add(item);
				}
			}
		}

		private void SavePalette()
		{
			((ConfigEntryBase)_paletteConfigEntry).BoxedValue = string.Join(_partJoiner, _paletteColors.Select((Color color) => ColorUtility.ToHtmlStringRGBA(color)));
		}

		private bool PaletteColorButtons(out int colorIndex)
		{
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			Texture2D background = GUI.skin.button.normal.background;
			GUI.skin.button.normal.background = _colorTexture;
			colorIndex = -1;
			for (int i = 0; i < _paletteColors.Count; i++)
			{
				GUIHelper.BeginColor(_paletteColors[i]);
				if (GUILayout.Button(string.Empty, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(20f) }))
				{
					colorIndex = i;
				}
				GUIHelper.EndColor();
			}
			GUI.skin.button.normal.background = background;
			return colorIndex >= 0;
		}

		private bool AddColorButton()
		{
			return GUILayout.Button("+", (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.MinWidth(25f),
				GUILayout.ExpandWidth(false)
			});
		}

		private bool RemoveColorButton()
		{
			return GUILayout.Button("−", (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.MinWidth(25f),
				GUILayout.ExpandWidth(false)
			});
		}

		private bool ResetColorsButton()
		{
			return GUILayout.Button("❇", (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.MinWidth(25f),
				GUILayout.ExpandWidth(false)
			});
		}

		public void DrawColorPalette()
		{
			//IL_001e: 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)
			GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
			if (AddColorButton())
			{
				_paletteColors.Add(_colorConfigEntry.Value);
				SavePalette();
			}
			GUILayout.Space(2f);
			if (PaletteColorButtons(out var colorIndex))
			{
				if (Event.current.button == 0)
				{
					_colorConfigEntry.SetValue(_paletteColors[colorIndex]);
				}
				else if (Event.current.button == 1 && colorIndex >= 0 && colorIndex < _paletteColors.Count)
				{
					_paletteColors.RemoveAt(colorIndex);
					SavePalette();
				}
			}
			GUILayout.FlexibleSpace();
			if (_paletteColors.Count > 0)
			{
				if (RemoveColorButton())
				{
					_paletteColors.RemoveAt(_paletteColors.Count - 1);
					SavePalette();
				}
			}
			else if (ResetColorsButton())
			{
				((ConfigEntryBase)_paletteConfigEntry).BoxedValue = ((ConfigEntryBase)_paletteConfigEntry).DefaultValue;
				LoadPalette();
			}
			GUILayout.EndHorizontal();
		}
	}
	public sealed class ColorFloatTextField
	{
		private string _fieldText;

		private Color _fieldColor;

		public string Label { get; set; }

		public float CurrentValue { get; private set; }

		public float MinValue { get; private set; }

		public float MaxValue { get; private set; }

		public void SetValue(float value)
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			CurrentValue = Mathf.Clamp(value, MinValue, MaxValue);
			_fieldText = value.ToString("F3", CultureInfo.InvariantCulture);
			_fieldColor = GUI.color;
		}

		public void SetValueRange(float minValue, float maxValue)
		{
			MinValue = Mathf.Min(minValue, minValue);
			MaxValue = Mathf.Max(maxValue, maxValue);
		}

		public ColorFloatTextField(string label)
		{
			Label = label;
			SetValueRange(0f, 1f);
		}

		public void DrawField()
		{
			//IL_002f: 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_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			GUILayout.BeginVertical(Array.Empty<GUILayoutOption>());
			GUILayout.BeginHorizontal(Array.Empty<GUILayoutOption>());
			GUILayout.Label(Label, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(true) });
			GUIHelper.BeginColor(_fieldColor);
			string text = GUILayout.TextField(_fieldText, (GUILayoutOption[])(object)new GUILayoutOption[3]
			{
				GUILayout.MinWidth(45f),
				GUILayout.MaxWidth(55f),
				GUILayout.ExpandWidth(true)
			});
			GUIHelper.EndColor();
			GUILayout.EndHorizontal();
			GUILayout.Space(2f);
			float num = GUILayout.HorizontalSlider(CurrentValue, MinValue, MaxValue, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(true) });
			GUILayout.EndVertical();
			if (num != CurrentValue)
			{
				SetValue(num);
			}
			else if (!(text == _fieldText))
			{
				if (float.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out var result) && result >= MinValue && result <= MaxValue)
				{
					CurrentValue = result;
					_fieldColor = GUI.color;
				}
				else
				{
					_fieldColor = Color.red;
				}
				_fieldText = text;
			}
		}
	}
	public sealed class HexColorTextField
	{
		private Color _textColor = GUI.color;

		public Color CurrentValue { get; private set; }

		public string CurrentText { get; private set; }

		public void SetValue(Color value)
		{
			//IL_0001: 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_0022: 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_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			CurrentValue = value;
			CurrentText = "#" + ((value.a == 1f) ? ColorUtility.ToHtmlStringRGB(value) : ColorUtility.ToHtmlStringRGBA(value));
			_textColor = GUI.color;
		}

		public void DrawField()
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: 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)
			GUIHelper.BeginColor(_textColor);
			string text = GUILayout.TextField(CurrentText, (GUILayoutOption[])(object)new GUILayoutOption[2]
			{
				GUILayout.Width(90f),
				GUILayout.ExpandWidth(false)
			});
			GUIHelper.EndColor();
			if (!(text == CurrentText))
			{
				CurrentText = text;
				Color currentValue = default(Color);
				if (ColorUtility.TryParseHtmlString(text, ref currentValue))
				{
					CurrentValue = currentValue;
				}
				else
				{
					_textColor = Color.red;
				}
			}
		}
	}
	public static class GUIBuilder
	{
		public static Texture2D CreateColorTexture(int width, int height, Color color)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			return CreateColorTexture(width, height, color, 0, color);
		}

		public static Texture2D CreateColorTexture(int width, int height, Color color, int radius, Color outsideColor)
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Expected O, but got Unknown
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0066: 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_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			if (width <= 0 || height <= 0)
			{
				throw new ArgumentException("Texture width and height must be > 0.");
			}
			if (radius < 0 || radius > width || radius > height)
			{
				throw new ArgumentException("Texture radius must be >= 0 and < width/height.");
			}
			Texture2D val = new Texture2D(width, height, (TextureFormat)5, false);
			((Object)val).name = $"w-{width}-h-{height}-rad-{radius}-color-{ColorId(color)}-ocolor-{ColorId(outsideColor)}";
			((Texture)val).wrapMode = (TextureWrapMode)1;
			((Texture)val).filterMode = (FilterMode)2;
			Color[] array = (Color[])(object)new Color[width * height];
			for (int i = 0; i < height; i++)
			{
				for (int j = 0; j < width; j++)
				{
					array[i * width + j] = (IsCornerPixel(j, i, width, height, radius) ? outsideColor : color);
				}
			}
			val.SetPixels(array);
			val.Apply();
			return val;
		}

		private static string ColorId(Color color)
		{
			//IL_000d: 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_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			return $"{color.r:F3}r-{color.g:F3}g-{color.b:F3}b-{color.a:F3}a";
		}

		private static bool IsCornerPixel(int x, int y, int w, int h, int rad)
		{
			if (rad == 0)
			{
				return false;
			}
			int num = Math.Min(x, w - x);
			int num2 = Math.Min(y, h - y);
			if (num == 0 && num2 == 0)
			{
				return true;
			}
			if (num > rad || num2 > rad)
			{
				return false;
			}
			num = rad - num;
			num2 = rad - num2;
			return Math.Round(Math.Sqrt(num * num + num2 * num2)) > (double)rad;
		}
	}
	public static class GUIFocus
	{
		private static int _lastFrameCount;

		private static int _lastHotControl;

		private static int _lastKeyboardControl;

		private static bool _hasChanged;

		public static bool HasChanged()
		{
			int frameCount = Time.frameCount;
			if (_lastFrameCount == frameCount)
			{
				return _hasChanged;
			}
			_lastFrameCount = frameCount;
			int hotControl = GUIUtility.hotControl;
			int keyboardControl = GUIUtility.keyboardControl;
			_hasChanged = hotControl != _lastHotControl || keyboardControl != _lastKeyboardControl;
			if (_hasChanged)
			{
				_lastHotControl = hotControl;
				_lastKeyboardControl = keyboardControl;
			}
			return _hasChanged;
		}
	}
	public static class GUIHelper
	{
		private static readonly Stack<Color> _colorStack = new Stack<Color>();

		public static void BeginColor(Color color)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			_colorStack.Push(GUI.color);
			GUI.color = color;
		}

		public static void EndColor()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			GUI.color = _colorStack.Pop();
		}

		public static bool IsEnterPressed()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Invalid comparison between Unknown and I4
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Invalid comparison between Unknown and I4
			if (Event.current.isKey)
			{
				if ((int)Event.current.keyCode != 13)
				{
					return (int)Event.current.keyCode == 271;
				}
				return true;
			}
			return false;
		}
	}
	public static class ColorExtensions
	{
		public static string GetColorHtmlString(this Color color)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: 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)
			if (color.a != 1f)
			{
				return ColorUtility.ToHtmlStringRGBA(color);
			}
			return ColorUtility.ToHtmlStringRGB(color);
		}

		public static Color SetAlpha(this Color color, float alpha)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			color.a = alpha;
			return color;
		}
	}
	public static class UnityExtensions
	{
		public static T Ref<T>(this T unityObject) where T : Object
		{
			if (!Object.op_Implicit((Object)(object)unityObject))
			{
				return default(T);
			}
			return unityObject;
		}

		public static bool TryGetComponentInParent<T>(this GameObject gameObject, out T component) where T : Component
		{
			component = gameObject.GetComponentInParent<T>();
			return Object.op_Implicit((Object)(object)component);
		}
	}
	public static class ZDOExtensions
	{
		public static bool TryGetVector3(this ZDO zdo, int hash, out Vector3 value)
		{
			//IL_0006: 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)
			if (ZDOExtraData.s_vec3.TryGetValue(zdo.m_uid, out var value2) && value2.TryGetValue(hash, ref value))
			{
				return true;
			}
			value = default(Vector3);
			return false;
		}

		public static bool ClearFloat(this ZDO zdo, int hash)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			if (ZDOHelper.Remove<float>(ZDOExtraData.s_floats, zdo.m_uid, hash))
			{
				zdo.IncreaseDataRevision();
				return true;
			}
			return false;
		}

		public static bool ClearVector3(this ZDO zdo, int hash)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			if (ZDOExtraData.s_vec3.TryGetValue(zdo.m_uid, out var value) && value.Remove(hash))
			{
				ZLog.Log((object)$"Removed Vec3:{hash} from {zdo.m_uid}");
				zdo.IncreaseDataRevision();
				return true;
			}
			return false;
		}
	}
}
namespace ColorfulLights
{
	[BepInPlugin("redseiko.valheim.colorfullights", "ColorfulLights", "1.11.0")]
	public sealed class ColorfulLights : BaseUnityPlugin
	{
		public const string PluginGUID = "redseiko.valheim.colorfullights";

		public const string PluginName = "ColorfulLights";

		public const string PluginVersion = "1.11.0";

		public static readonly int FirePlaceColorHashCode = StringExtensionMethods.GetStableHashCode("FireplaceColor");

		public static readonly int FireplaceColorAlphaHashCode = StringExtensionMethods.GetStableHashCode("FireplaceColorAlpha");

		public static readonly int LightLastColoredByHashCode = StringExtensionMethods.GetStableHashCode("LightLastColoredBy");

		public static readonly int LightLastColoredByHostHashCode = StringExtensionMethods.GetStableHashCode("LightLastColoredByHost");

		private static ManualLogSource _logger;

		private Harmony _harmony;

		private void Awake()
		{
			_logger = ((BaseUnityPlugin)this).Logger;
			PluginConfig.BindConfig(((BaseUnityPlugin)this).Config);
			_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "redseiko.valheim.colorfullights");
		}

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

		public static bool ChangeFireplaceColor(Fireplace targetFireplace)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: 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_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: 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_0129: Unknown result type (might be due to invalid IL or missing references)
			if (!Object.op_Implicit((Object)(object)targetFireplace))
			{
				return false;
			}
			if (!Object.op_Implicit((Object)(object)targetFireplace.m_nview) || !targetFireplace.m_nview.IsValid())
			{
				_logger.LogWarning((object)"Fireplace does not have a valid ZNetView.");
				return false;
			}
			if (!PrivateArea.CheckAccess(((Component)targetFireplace).transform.position, 0f, true, false))
			{
				_logger.LogWarning((object)"Fireplace is within private area with no access.");
				return false;
			}
			Vector3 val = Utils.ColorToVec3(PluginConfig.TargetFireplaceColor.Value);
			float a = PluginConfig.TargetFireplaceColor.Value.a;
			targetFireplace.m_nview.ClaimOwnership();
			targetFireplace.m_nview.m_zdo.Set(FirePlaceColorHashCode, val);
			targetFireplace.m_nview.m_zdo.Set(FireplaceColorAlphaHashCode, a);
			targetFireplace.m_nview.m_zdo.Set(LightLastColoredByHashCode, Player.m_localPlayer.GetPlayerID());
			targetFireplace.m_nview.m_zdo.Set(LightLastColoredByHostHashCode, PrivilegeManager.GetNetworkUserId());
			EffectList fuelAddedEffects = targetFireplace.m_fuelAddedEffects;
			if (fuelAddedEffects != null)
			{
				fuelAddedEffects.Create(((Component)targetFireplace).transform.position, ((Component)targetFireplace).transform.rotation, (Transform)null, 1f, -1);
			}
			FireplaceColor fireplaceColor = default(FireplaceColor);
			if (((Component)targetFireplace).TryGetComponent<FireplaceColor>(ref fireplaceColor))
			{
				fireplaceColor.SetFireplaceColors(val, a);
			}
			return true;
		}
	}
	public sealed class FireplaceColor : MonoBehaviour
	{
		private Vector3 _targetColorVec3;

		private float _targetColorAlpha;

		private ZNetView _netView;

		private readonly List<Light> _lights = new List<Light>();

		private readonly List<ParticleSystem> _systems = new List<ParticleSystem>();

		private readonly List<ParticleSystemRenderer> _renderers = new List<ParticleSystemRenderer>();

		public static long TotalCount { get; private set; }

		public static long CurrentCount { get; private set; }

		public Color TargetColor { get; private set; } = Color.clear;


		private void Awake()
		{
			//IL_001b: 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_002b: Unknown result type (might be due to invalid IL or missing references)
			TotalCount++;
			CurrentCount++;
			TargetColor = Color.clear;
			_targetColorVec3 = Vector3.positiveInfinity;
			_targetColorAlpha = float.NaN;
			_lights.Clear();
			_systems.Clear();
			_renderers.Clear();
			Fireplace component = ((Component)this).GetComponent<Fireplace>();
			if (Object.op_Implicit((Object)(object)component) && Object.op_Implicit((Object)(object)component.m_nview) && component.m_nview.IsValid())
			{
				_netView = component.m_nview;
				CacheComponents(component.m_enabledObject);
				CacheComponents(component.m_enabledObjectHigh);
				CacheComponents(component.m_enabledObjectLow);
				((MonoBehaviour)this).InvokeRepeating("UpdateColors", 0f, 2f);
			}
		}

		private void OnDestroy()
		{
			CurrentCount--;
		}

		private void CacheComponents(GameObject target)
		{
			if (Object.op_Implicit((Object)(object)target))
			{
				_lights.AddRange(target.GetComponentsInChildren<Light>(true));
				_systems.AddRange(target.GetComponentsInChildren<ParticleSystem>(true));
				_renderers.AddRange(target.GetComponentsInChildren<ParticleSystemRenderer>(true));
			}
		}

		public void UpdateColors()
		{
			//IL_0067: 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)
			Vector3 value;
			if (!Object.op_Implicit((Object)(object)_netView) || !_netView.IsValid() || !PluginConfig.IsModEnabled.Value)
			{
				((MonoBehaviour)this).CancelInvoke("UpdateColors");
			}
			else if (_netView.m_zdo.TryGetVector3(ColorfulLights.FirePlaceColorHashCode, out value))
			{
				float @float = _netView.m_zdo.GetFloat(ColorfulLights.FireplaceColorAlphaHashCode, 1f);
				if (!(value == _targetColorVec3) || @float != _targetColorAlpha)
				{
					SetFireplaceColors(value, @float);
				}
			}
		}

		public void SetFireplaceColors(Vector3 colorVec3, float colorAlpha)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: 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)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			TargetColor = Utils.Vec3ToColor(colorVec3).SetAlpha(colorAlpha);
			_targetColorVec3 = colorVec3;
			_targetColorAlpha = colorAlpha;
			SetParticleColors(TargetColor);
		}

		private void SetParticleColors(Color color)
		{
			//IL_0002: 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_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: 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_004d: 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_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: 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)
			MinMaxGradient color2 = default(MinMaxGradient);
			((MinMaxGradient)(ref color2))..ctor(color);
			foreach (ParticleSystem system in _systems)
			{
				ColorOverLifetimeModule colorOverLifetime = system.colorOverLifetime;
				if (((ColorOverLifetimeModule)(ref colorOverLifetime)).enabled)
				{
					((ColorOverLifetimeModule)(ref colorOverLifetime)).color = color2;
				}
				SizeOverLifetimeModule sizeOverLifetime = system.sizeOverLifetime;
				if (((SizeOverLifetimeModule)(ref sizeOverLifetime)).enabled)
				{
					MainModule main = system.main;
					((MainModule)(ref main)).startColor = MinMaxGradient.op_Implicit(color);
				}
			}
			foreach (ParticleSystemRenderer renderer in _renderers)
			{
				((Renderer)renderer).material.color = color;
			}
			foreach (Light light in _lights)
			{
				light.color = color;
			}
		}
	}
	[HarmonyPatch(typeof(Fireplace))]
	internal static class FireplacePatch
	{
		private static readonly string _changeColorHoverTextTemplate = "{0}\n<size={4}>[<color={1}>{2}</color>] Change fire color to: <color=#{3}>#{3}</color></size>";

		[HarmonyPostfix]
		[HarmonyPatch("Awake")]
		private static void AwakePostfix(ref Fireplace __instance)
		{
			FireplaceColor fireplaceColor = default(FireplaceColor);
			if (PluginConfig.IsModEnabled.Value && Object.op_Implicit((Object)(object)__instance) && Object.op_Implicit((Object)(object)__instance.m_nview) && __instance.m_nview.IsValid() && !((Component)__instance).TryGetComponent<FireplaceColor>(ref fireplaceColor))
			{
				((Component)__instance).gameObject.AddComponent<FireplaceColor>();
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("GetHoverText")]
		private static void GetHoverTextPostfix(ref Fireplace __instance, ref string __result)
		{
			//IL_0047: 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)
			if (PluginConfig.IsModEnabled.Value && PluginConfig.ShowChangeColorHoverText.Value && Object.op_Implicit((Object)(object)__instance))
			{
				__result = Localization.instance.Localize(string.Format(_changeColorHoverTextTemplate, __result, "#FFA726", PluginConfig.ChangeColorActionShortcut.Value, PluginConfig.TargetFireplaceColor.Value.GetColorHtmlString(), PluginConfig.ColorPromptFontSize.Value));
			}
		}
	}
	[HarmonyPatch(typeof(Player))]
	internal static class PlayerPatch
	{
		[HarmonyTranspiler]
		[HarmonyPatch("Update")]
		private static IEnumerable<CodeInstruction> UpdateTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: 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_0037: Expected O, but got Unknown
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Expected O, but got Unknown
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Expected O, but got Unknown
			return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
			{
				new CodeMatch((OpCode?)OpCodes.Call, (object)AccessTools.Method(typeof(Player), "UpdateHover", (Type[])null, (Type[])null), (string)null)
			}).Advance(1).InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[3]
			{
				new CodeInstruction(OpCodes.Ldloc_1, (object)null),
				Transpilers.EmitDelegate<Func<bool, bool>>((Func<bool, bool>)UpdateHoverPostDelegate),
				new CodeInstruction(OpCodes.Stloc_1, (object)null)
			})
				.InstructionEnumeration();
		}

		private static bool UpdateHoverPostDelegate(bool takeInput)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			if (takeInput && PluginConfig.IsModEnabled.Value)
			{
				KeyboardShortcut value = PluginConfig.ChangeColorActionShortcut.Value;
				if (((KeyboardShortcut)(ref value)).IsDown() && Object.op_Implicit((Object)(object)Player.m_localPlayer) && Object.op_Implicit((Object)(object)Player.m_localPlayer.m_hovering) && Player.m_localPlayer.m_hovering.TryGetComponentInParent<Fireplace>(out Fireplace component))
				{
					ColorfulLights.ChangeFireplaceColor(component);
					return false;
				}
			}
			return takeInput;
		}
	}
	public static class PluginConfig
	{
		public static ConfigEntry<bool> IsModEnabled { get; private set; }

		public static ConfigEntry<KeyboardShortcut> ChangeColorActionShortcut { get; private set; }

		public static ExtendedColorConfigEntry TargetFireplaceColor { get; private set; }

		public static ConfigEntry<bool> ShowChangeColorHoverText { get; private set; }

		public static ConfigEntry<int> ColorPromptFontSize { get; private set; }

		public static void BindConfig(ConfigFile config)
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			IsModEnabled = config.BindInOrder("_Global", "isModEnabled", defaultValue: true, "Globally enable or disable this mod.");
			ChangeColorActionShortcut = config.BindInOrder<KeyboardShortcut>("Hotkeys", "changeColorActionShortcut", new KeyboardShortcut((KeyCode)101, (KeyCode[])(object)new KeyCode[1] { (KeyCode)304 }), "Keyboard shortcut to change (or clear) the color of the hovered torch/fire.");
			TargetFireplaceColor = new ExtendedColorConfigEntry(config, "Color", "targetFireplaceColor", Color.cyan, "Target color to set any torch/fire to.", "targetFireplaceColoPalette");
			ShowChangeColorHoverText = config.BindInOrder("Hud", "showChangeColorHoverText", defaultValue: false, "Show the 'change color' text when hovering over a lightsoure.");
			ColorPromptFontSize = config.BindInOrder("Hud", "colorPromptFontSize", 16, "Font size for the 'change color' text prompt.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(4, 24));
		}
	}
}