Decompiled source of LightUpYourWorld v1.4.0

BepInEx/plugins/LightUpYourWorld/LightUpYourWorld.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyFileVersion("0.1.15.0")]
[assembly: AssemblyVersion("0.1.15.0")]
namespace LightUpYourWorld;

[DefaultExecutionOrder(10000)]
[BepInPlugin("valheim.lightupyourworld", "LightUpYourWorld", "0.1.15")]
public sealed class LightUpYourWorldPlugin : BaseUnityPlugin
{
	internal const string PluginId = "valheim.lightupyourworld";

	internal static LightUpYourWorldPlugin Instance;

	private Harmony harmony;

	private ConfigEntry<KeyCode> openKey;

	private WorldLight target;

	private readonly ColourChannels channels = new ColourChannels();

	private bool visible;

	private bool captured;

	private bool previousVisible;

	private bool previousRequested;

	private bool wheelDragging;

	private CursorLockMode previousLock;

	private float hue;

	private float saturation;

	private float brightness;

	private float acceptAfter;

	private float blockUntil;

	private string targetName;

	private string message = "";

	private Texture2D wheel;

	private GUIStyle title;

	private GUIStyle heading;

	private GUIStyle body;

	private GUIStyle button;

	private GUIStyle centered;

	private GUIStyle small;

	private bool favouritesMode;

	private int favouriteSlot;

	private int favouriteDirection;

	private float favouriteRepeat;

	private string cachedPrompt;

	private bool promptController;

	private KeyCode promptKey;

	private float nextPromptBindings;

	private static readonly MethodInfo TakeInputMethod = AccessTools.Method(typeof(Player), "TakeInput", (Type[])null, (Type[])null);

	private ColourSession session => channels.Current;

	private bool controller => ZInput.IsGamepadActive();

	internal bool IsOpen => visible;

	internal bool BlocksGameplay
	{
		get
		{
			if (!visible)
			{
				return Time.unscaledTime < blockUntil;
			}
			return true;
		}
	}

	private bool flameChannel => channels.SourceSelected;

	private int ChannelSaved
	{
		get
		{
			if (!flameChannel)
			{
				return target.Saved;
			}
			return target.SavedFlame;
		}
	}

	private int ChannelEffect
	{
		get
		{
			if (!flameChannel)
			{
				return target.SavedEffect;
			}
			return target.SavedFlameEffect;
		}
	}

	private string ChannelName
	{
		get
		{
			if (!flameChannel)
			{
				return "Light";
			}
			return "Source";
		}
	}

	internal string HoverPrompt
	{
		get
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: 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_0024: Unknown result type (might be due to invalid IL or missing references)
			bool flag = controller;
			if (cachedPrompt != null && flag == promptController && promptKey == openKey.Value && (!flag || Time.unscaledTime < nextPromptBindings))
			{
				return cachedPrompt;
			}
			promptController = flag;
			promptKey = openKey.Value;
			nextPromptBindings = Time.unscaledTime + 1f;
			if (!flag)
			{
				return cachedPrompt = "[<color=yellow><b>" + ((object)openKey.Value/*cast due to .constrained prefix*/).ToString() + "</b></color>] Change light colour";
			}
			string binding = ((ZInput.instance != null) ? ZInput.instance.GetBoundKeyString("JoyLBumper", true) : "");
			string binding2 = ((ZInput.instance != null) ? ZInput.instance.GetBoundKeyString("JoyUse", true) : "");
			return cachedPrompt = NativeBinding(binding, "LB") + " + " + NativeBinding(binding2, "Interact") + " Change light colour";
		}
	}

	internal static void Warn(string text)
	{
		if (Object.op_Implicit((Object)(object)Instance))
		{
			((BaseUnityPlugin)Instance).Logger.LogWarning((object)text);
		}
	}

	private static string NativeBinding(string binding, string fallback)
	{
		if (string.IsNullOrEmpty(binding))
		{
			binding = fallback;
		}
		if (!binding.Contains("<sprite="))
		{
			return "[<color=yellow><b>" + Localize(binding) + "</b></color>]";
		}
		return binding;
	}

	internal static bool CanTakeInput(Player player)
	{
		if (Object.op_Implicit((Object)(object)player) && TakeInputMethod != null)
		{
			return (bool)TakeInputMethod.Invoke(player, null);
		}
		return false;
	}

	internal static string Localize(string key)
	{
		try
		{
			Type type = AccessTools.TypeByName("Localization");
			PropertyInfo propertyInfo = ((type != null) ? type.GetProperty("instance", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic) : null);
			object obj = ((propertyInfo != null) ? propertyInfo.GetValue(null, null) : null);
			MethodInfo methodInfo = ((type != null) ? type.GetMethod("Localize", new Type[1] { typeof(string) }) : null);
			return (obj != null && methodInfo != null) ? ((string)methodInfo.Invoke(obj, new object[1] { key })) : key;
		}
		catch
		{
			return key;
		}
	}

	private void Awake()
	{
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Expected O, but got Unknown
		Instance = this;
		openKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Controls", "OpenColourMenu", (KeyCode)103, "Look at a placed torch, lamp or campfire and press this key. Controller: hold LB and press your normal Interact button.");
		harmony = new Harmony("valheim.lightupyourworld");
		harmony.PatchAll(typeof(LightUpYourWorldPlugin).Assembly);
		CreateWheel();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"LightUpYourWorld 0.1.15 loaded. G / LB+Interact changes a placed light. No Dye Your Gear dependency.");
	}

	private void OnDestroy()
	{
		Close();
		foreach (WorldLight source in WorldLight.Sources)
		{
			if (Object.op_Implicit((Object)(object)source))
			{
				source.RestoreForUnload();
			}
		}
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
		if (Object.op_Implicit((Object)(object)wheel))
		{
			Object.Destroy((Object)(object)wheel);
		}
		Instance = null;
	}

	private void OnDisable()
	{
		Close();
	}

	private void Update()
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: 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_0328: Unknown result type (might be due to invalid IL or missing references)
		//IL_0333: Unknown result type (might be due to invalid IL or missing references)
		//IL_0338: Unknown result type (might be due to invalid IL or missing references)
		//IL_0339: Unknown result type (might be due to invalid IL or missing references)
		//IL_033c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0350: Unknown result type (might be due to invalid IL or missing references)
		//IL_035a: Unknown result type (might be due to invalid IL or missing references)
		//IL_035f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0364: Unknown result type (might be due to invalid IL or missing references)
		//IL_0395: Unknown result type (might be due to invalid IL or missing references)
		//IL_039c: Unknown result type (might be due to invalid IL or missing references)
		Player localPlayer = Player.m_localPlayer;
		if (!visible)
		{
			if (Object.op_Implicit((Object)(object)localPlayer) && Input.GetKeyDown(openKey.Value) && !BlocksGameplay && CanTakeInput(localPlayer) && !((Character)localPlayer).InPlaceMode())
			{
				Open(WorldLight.Find(((Humanoid)localPlayer).GetHoverObject()));
			}
			return;
		}
		if (!Object.op_Implicit((Object)(object)localPlayer) || !Object.op_Implicit((Object)(object)target) || !target.Ready || ((Character)localPlayer).IsDead() || ((Character)localPlayer).IsTeleporting() || Vector3.Distance(((Component)localPlayer).transform.position, ((Component)target).transform.position) > 8f || Menu.IsVisible() || InventoryGui.IsVisible() || TextInput.IsVisible() || Console.IsVisible() || (Object.op_Implicit((Object)(object)Chat.instance) && Chat.instance.HasFocus()) || Input.GetKeyDown((KeyCode)27) || ZInput.GetButtonDown("JoyMenu"))
		{
			Close();
			return;
		}
		if (channels.Refresh(target.Saved, target.SavedEffect, target.SavedFlame, target.SavedFlameEffect))
		{
			target.PreviewChannels(channels);
			if (!session.Dirty)
			{
				LoadWheel();
			}
		}
		if (Time.unscaledTime < acceptAfter)
		{
			return;
		}
		if (ZInput.GetButtonDown("JoyButtonB"))
		{
			ZInput.ResetButtonStatus("JoyButtonB");
			if (favouritesMode)
			{
				favouritesMode = false;
			}
			else if (session.Dirty)
			{
				CancelPreview();
			}
			else
			{
				Close();
			}
			return;
		}
		if (Input.GetKeyDown((KeyCode)9))
		{
			favouritesMode = !favouritesMode;
		}
		if (ZInput.GetButtonDown("JoyButtonY"))
		{
			ZInput.ResetButtonStatus("JoyButtonY");
			if (favouritesMode)
			{
				SaveFavourite();
			}
			else
			{
				favouritesMode = true;
			}
		}
		if (Input.GetKeyDown((KeyCode)102))
		{
			SaveFavourite();
		}
		if (favouritesMode)
		{
			UpdateFavourites();
			ConsumeDirections();
			return;
		}
		SwitchChannel(ColourChannels.RequestedSource(flameChannel, ZInput.GetButtonDown("JoyDPadLeft") || ZInput.GetButtonDown("JoyHotbarLeft"), ZInput.GetButtonDown("JoyDPadRight") || ZInput.GetButtonDown("JoyHotbarRight"), Input.GetKeyDown((KeyCode)99)));
		if (Input.GetKeyDown((KeyCode)101) || ZInput.GetButtonDown("JoyRStick"))
		{
			CycleEffect();
			ZInput.ResetButtonStatus("JoyRStick");
		}
		float num = ZInput.GetJoyRightStickX();
		float num2 = ZInput.GetJoyRightStickY();
		if (Input.GetKey((KeyCode)276))
		{
			num = -1f;
		}
		if (Input.GetKey((KeyCode)275))
		{
			num = 1f;
		}
		if (Input.GetKey((KeyCode)273))
		{
			num2 = -1f;
		}
		if (Input.GetKey((KeyCode)274))
		{
			num2 = 1f;
		}
		if (Mathf.Abs(num) > 0.25f || Mathf.Abs(num2) > 0.25f)
		{
			Vector2 val = new Vector2(Mathf.Cos(hue * 2f * (float)Math.PI), 0f - Mathf.Sin(hue * 2f * (float)Math.PI)) * saturation;
			val += new Vector2(num, num2) * Mathf.Min(Time.unscaledDeltaTime, 0.05f) * 0.9f;
			if (((Vector2)(ref val)).sqrMagnitude > 1f)
			{
				((Vector2)(ref val)).Normalize();
			}
			saturation = ((Vector2)(ref val)).magnitude;
			if (saturation > 0.001f)
			{
				hue = Mathf.Repeat(Mathf.Atan2(0f - val.y, val.x) / ((float)Math.PI * 2f), 1f);
			}
			Preview();
		}
		if (ZInput.GetButtonDown("JoyLBumper") || Input.GetKeyDown((KeyCode)45) || Input.GetKeyDown((KeyCode)269))
		{
			brightness = Mathf.Clamp01(brightness - 0.05f);
			Preview();
		}
		if (ZInput.GetButtonDown("JoyRBumper") || Input.GetKeyDown((KeyCode)61) || Input.GetKeyDown((KeyCode)270))
		{
			brightness = Mathf.Clamp01(brightness + 0.05f);
			Preview();
		}
		if (Input.GetKeyDown((KeyCode)13) || Input.GetKeyDown((KeyCode)271) || ZInput.GetButtonDown("JoyButtonA"))
		{
			Apply();
			ZInput.ResetButtonStatus("JoyButtonA");
			ZInput.ResetButtonStatus("JoyUse");
		}
		if (Input.GetKeyDown((KeyCode)127) || ZInput.GetButtonDown("JoyButtonX"))
		{
			RestoreDefault();
			ZInput.ResetButtonStatus("JoyButtonX");
		}
		ConsumeDirections();
	}

	private static void ConsumeDirections()
	{
		string[] array = new string[7] { "JoyDPadLeft", "JoyDPadRight", "JoyDPadUp", "JoyDPadDown", "JoyHotbarLeft", "JoyHotbarRight", "JoyHotbarUse" };
		for (int i = 0; i < array.Length; i++)
		{
			ZInput.ResetButtonStatus(array[i]);
		}
	}

	private void UpdateFavourites()
	{
		float joyLeftStickX = ZInput.GetJoyLeftStickX();
		float joyLeftStickY = ZInput.GetJoyLeftStickY();
		int num = ((Mathf.Max(Mathf.Abs(joyLeftStickX), Mathf.Abs(joyLeftStickY)) > 0.5f) ? ((!(Mathf.Abs(joyLeftStickX) > Mathf.Abs(joyLeftStickY))) ? ((joyLeftStickY < 0f) ? (-2) : 2) : ((!(joyLeftStickX < 0f)) ? 1 : (-1))) : 0);
		int num2 = 0;
		if (Input.GetKeyDown((KeyCode)276) || ZInput.GetButtonDown("JoyDPadLeft"))
		{
			num2 = -1;
		}
		else if (Input.GetKeyDown((KeyCode)275) || ZInput.GetButtonDown("JoyDPadRight"))
		{
			num2 = 1;
		}
		else if (Input.GetKeyDown((KeyCode)273) || ZInput.GetButtonDown("JoyDPadUp"))
		{
			num2 = -2;
		}
		else if (Input.GetKeyDown((KeyCode)274) || ZInput.GetButtonDown("JoyDPadDown"))
		{
			num2 = 2;
		}
		if (num2 != 0)
		{
			favouriteSlot = (favouriteSlot + num2 + 10) % 10;
		}
		else if (num != 0 && (num != favouriteDirection || Time.unscaledTime >= favouriteRepeat))
		{
			favouriteSlot = (favouriteSlot + num + 10) % 10;
			favouriteRepeat = Time.unscaledTime + ((num != favouriteDirection) ? 0.4f : 0.18f);
		}
		favouriteDirection = num;
		if (ZInput.GetButtonDown("JoyButtonA") || Input.GetKeyDown((KeyCode)13))
		{
			PreviewFavourite();
			ZInput.ResetButtonStatus("JoyButtonA");
			ZInput.ResetButtonStatus("JoyUse");
		}
		if (ZInput.GetButtonDown("JoyButtonX") || Input.GetKeyDown((KeyCode)127))
		{
			RemoveFavourite();
			ZInput.ResetButtonStatus("JoyButtonX");
		}
	}

	private void PreviewFavourite()
	{
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		if (!LightFavourites.TryGet(Player.m_localPlayer, favouriteSlot, out var colour))
		{
			message = "Empty slot. Save your current colour first.";
			return;
		}
		Color.RGBToHSV(colour, ref hue, ref saturation, ref brightness);
		Preview();
		favouritesMode = false;
		message = "Favourite " + (favouriteSlot + 1) + " previewed. Apply to save on this light.";
	}

	private void SaveFavourite()
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		LightFavourites.Set(Player.m_localPlayer, favouriteSlot, Color.HSVToRGB(hue, saturation, brightness));
		message = "Colour saved in favourite " + (favouriteSlot + 1) + ".";
	}

	private void RemoveFavourite()
	{
		LightFavourites.Remove(Player.m_localPlayer, favouriteSlot);
		message = "Favourite " + (favouriteSlot + 1) + " cleared. Light colour unchanged.";
	}

	private void LateUpdate()
	{
		foreach (WorldLight source in WorldLight.Sources)
		{
			if (Object.op_Implicit((Object)(object)source))
			{
				source.Tick();
			}
		}
		KeepCursor();
	}

	internal void Open(WorldLight source)
	{
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		if (!visible && Object.op_Implicit((Object)(object)source))
		{
			if (!source.CanEdit(Player.m_localPlayer, flash: true))
			{
				((Character)Player.m_localPlayer).Message((MessageType)2, "You cannot change this light here.", 0, (Sprite)null, false);
				return;
			}
			target = source;
			target.RefreshLights();
			targetName = source.DisplayName;
			channels.Open(source.Saved, source.SavedEffect, source.SavedFlame, source.SavedFlameEffect);
			LoadWheel();
			message = "";
			favouritesMode = false;
			favouriteSlot = LightFavourites.FirstEmpty(Player.m_localPlayer);
			favouriteDirection = 0;
			visible = true;
			wheelDragging = false;
			acceptAfter = Time.unscaledTime + 0.3f;
			previousLock = Cursor.lockState;
			previousVisible = Cursor.visible;
			previousRequested = ZCursor.IsRequested;
			captured = true;
			ZInput.ResetButtonStatus("JoyUse");
			ZInput.ResetButtonStatus("JoyButtonA");
			ZInput.ResetButtonStatus("JoyButtonX");
			KeepCursor();
		}
	}

	private void SwitchChannel(bool flame)
	{
		if (flame != flameChannel)
		{
			if (flame && !target.HasFlame)
			{
				message = "No editable visible effect has loaded on this light yet.";
				return;
			}
			wheelDragging = false;
			GUIUtility.hotControl = 0;
			channels.Select(flame);
			channels.Refresh(target.Saved, target.SavedEffect, target.SavedFlame, target.SavedFlameEffect);
			target.PreviewChannels(channels);
			LoadWheel();
			message = (flame ? "Editing the visible source. Light has its own colour." : "Editing surrounding light. Source has its own colour.");
		}
	}

	private void LoadWheel()
	{
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		Color.RGBToHSV((session.Selected != 0) ? WorldLight.Unpack(session.Selected) : (flameChannel ? target.OriginalFlameColour : target.OriginalColour), ref hue, ref saturation, ref brightness);
		brightness = Mathf.Clamp01(brightness);
	}

	private void Preview()
	{
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		int value = WorldLight.Pack(Color.HSVToRGB(hue, saturation, brightness));
		session.Preview(value);
		target.PreviewChannels(channels);
		message = "";
	}

	private void CycleEffect()
	{
		session.PreviewEffect((session.SelectedEffect + 1) % 3);
		Preview();
		message = LightAnimation.Name(session.SelectedEffect) + " previewed. Apply to save.";
	}

	private void Apply()
	{
		if (session.Dirty)
		{
			if (!Object.op_Implicit((Object)(object)target) || !(flameChannel ? target.CommitFlame(session.Selected, session.SelectedEffect) : target.Commit(session.Selected, session.SelectedEffect)))
			{
				message = "Could not save. Check distance and ward access.";
				return;
			}
			session.Applied();
			target.PreviewChannels(channels);
			message = ChannelName + " colour applied and saved.";
		}
	}

	private void RestoreDefault()
	{
		if (!Object.op_Implicit((Object)(object)target) || !(flameChannel ? target.CommitFlame(0) : target.Commit(0)))
		{
			message = "Could not reset. Check distance and ward access.";
			return;
		}
		wheelDragging = false;
		GUIUtility.hotControl = 0;
		session.Open(0);
		target.PreviewChannels(channels);
		LoadWheel();
		message = "Original " + ChannelName + " colours restored. No colour override.";
	}

	private void CancelPreview()
	{
		if (!Object.op_Implicit((Object)(object)target))
		{
			Close();
			return;
		}
		session.Cancel(ChannelSaved, ChannelEffect);
		target.PreviewChannels(channels);
		LoadWheel();
		message = ChannelName + " preview discarded.";
	}

	internal void Close()
	{
		//IL_0057: 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)
		bool num = visible;
		visible = false;
		wheelDragging = false;
		favouritesMode = false;
		if (Object.op_Implicit((Object)(object)target))
		{
			target.EndPreview();
		}
		target = null;
		if (captured)
		{
			if (previousRequested)
			{
				ZCursor.Show();
			}
			else
			{
				ZCursor.Hide();
			}
			ZCursor.LockState = previousLock;
			Cursor.lockState = previousLock;
			Cursor.visible = previousVisible;
			captured = false;
		}
		if (num)
		{
			blockUntil = Time.unscaledTime + 0.15f;
		}
	}

	internal void KeepCursor()
	{
		if (visible)
		{
			ZCursor.Show();
			ZCursor.LockState = (CursorLockMode)0;
			Cursor.lockState = (CursorLockMode)0;
			Cursor.visible = !controller;
		}
	}

	private void Styles()
	{
		//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)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Expected O, but got Unknown
		//IL_003f: 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_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: 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_0066: Expected O, but got Unknown
		//IL_0071: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Expected O, but got Unknown
		//IL_0095: 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_00a2: 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_00b5: Expected O, but got Unknown
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: Expected O, but got Unknown
		//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e6: Expected O, but got Unknown
		//IL_0126: Unknown result type (might be due to invalid IL or missing references)
		if (title == null)
		{
			title = new GUIStyle(GUI.skin.label)
			{
				fontSize = 34,
				fontStyle = (FontStyle)1,
				alignment = (TextAnchor)4
			};
			heading = new GUIStyle(GUI.skin.label)
			{
				fontSize = 25,
				fontStyle = (FontStyle)1,
				alignment = (TextAnchor)4,
				wordWrap = true
			};
			body = new GUIStyle(GUI.skin.label)
			{
				fontSize = 21,
				wordWrap = true
			};
			button = new GUIStyle(GUI.skin.button)
			{
				fontSize = 20,
				fontStyle = (FontStyle)1,
				wordWrap = true
			};
			centered = new GUIStyle(body)
			{
				alignment = (TextAnchor)4
			};
			small = new GUIStyle(body)
			{
				fontSize = 18
			};
			GUIStyle[] array = (GUIStyle[])(object)new GUIStyle[5] { title, heading, body, centered, small };
			for (int i = 0; i < array.Length; i++)
			{
				array[i].normal.textColor = Color.white;
			}
		}
	}

	private void OnGUI()
	{
		//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_002b: 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_0035: 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_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: 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)
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: 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)
		if (visible)
		{
			Styles();
			float num = MenuLayout.Scale(Screen.width, Screen.height);
			Matrix4x4 matrix = GUI.matrix;
			Color color = GUI.color;
			GUI.matrix = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one * num);
			float num2 = (float)Screen.width / num;
			float num3 = (float)Screen.height / num;
			Rect val = new Rect(num2 - 600f, (num3 - 930f) / 2f, 580f, 930f);
			GUI.color = new Color(0.065f, 0.08f, 0.1f, 0.98f);
			GUI.DrawTexture(val, (Texture)(object)Texture2D.whiteTexture);
			GUI.color = Color.white;
			GUI.BeginGroup(val);
			DrawMenu();
			GUI.EndGroup();
			Rect val2 = new Rect(20f, (num3 - 750f) / 2f, 280f, 750f);
			GUI.color = new Color(0.065f, 0.08f, 0.1f, 0.98f);
			GUI.DrawTexture(val2, (Texture)(object)Texture2D.whiteTexture);
			GUI.color = Color.white;
			GUI.BeginGroup(val2);
			DrawFavourites();
			GUI.EndGroup();
			GUI.color = color;
			GUI.matrix = matrix;
		}
	}

	private void DrawMenu()
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: 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)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: 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_0103: Unknown result type (might be due to invalid IL or missing references)
		//IL_0133: Unknown result type (might be due to invalid IL or missing references)
		//IL_0188: 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_01c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0204: Invalid comparison between Unknown and I4
		//IL_0224: Unknown result type (might be due to invalid IL or missing references)
		//IL_022a: Invalid comparison between Unknown and I4
		//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0243: Unknown result type (might be due to invalid IL or missing references)
		//IL_0277: Unknown result type (might be due to invalid IL or missing references)
		//IL_0290: Unknown result type (might be due to invalid IL or missing references)
		//IL_0295: Unknown result type (might be due to invalid IL or missing references)
		//IL_029a: Unknown result type (might be due to invalid IL or missing references)
		//IL_029b: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_0302: Unknown result type (might be due to invalid IL or missing references)
		//IL_0325: Unknown result type (might be due to invalid IL or missing references)
		//IL_0372: Unknown result type (might be due to invalid IL or missing references)
		//IL_0210: Unknown result type (might be due to invalid IL or missing references)
		//IL_0215: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_041f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0443: Unknown result type (might be due to invalid IL or missing references)
		//IL_0461: Unknown result type (might be due to invalid IL or missing references)
		//IL_0470: Unknown result type (might be due to invalid IL or missing references)
		//IL_048e: Unknown result type (might be due to invalid IL or missing references)
		//IL_04e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0567: Unknown result type (might be due to invalid IL or missing references)
		//IL_05ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_0652: Unknown result type (might be due to invalid IL or missing references)
		//IL_06a0: Unknown result type (might be due to invalid IL or missing references)
		GUI.Label(new Rect(20f, 12f, 540f, 50f), "LightUpYourWorld", title);
		GUI.Label(new Rect(25f, 62f, 530f, 34f), targetName, heading);
		Color backgroundColor = GUI.backgroundColor;
		GUI.backgroundColor = (Color)((!flameChannel) ? new Color(1f, 0.75f, 0.3f) : backgroundColor);
		if (GUI.Button(new Rect(30f, 101f, 250f, 40f), ((!flameChannel) ? "✓ " : "") + "Light · surroundings" + (channels.Light.Dirty ? " *" : ""), button))
		{
			SwitchChannel(flame: false);
		}
		bool enabled = GUI.enabled;
		GUI.enabled = target.HasFlame;
		GUI.backgroundColor = (Color)(flameChannel ? new Color(1f, 0.75f, 0.3f) : backgroundColor);
		if (GUI.Button(new Rect(300f, 101f, 250f, 40f), (flameChannel ? "✓ " : "") + "Source · appearance" + (channels.Source.Dirty ? " *" : ""), button))
		{
			SwitchChannel(flame: true);
		}
		GUI.backgroundColor = backgroundColor;
		GUI.enabled = enabled;
		Rect val = default(Rect);
		((Rect)(ref val))..ctor(90f, 147f, 390f, 390f);
		GUI.DrawTexture(val, (Texture)(object)wheel, (ScaleMode)0, true);
		Event current = Event.current;
		if ((int)current.type == 0 && current.button == 0 && ((Rect)(ref val)).Contains(current.mousePosition))
		{
			wheelDragging = true;
			SetFromMouse(current.mousePosition, val);
			current.Use();
		}
		else if ((int)current.type == 3 && wheelDragging)
		{
			SetFromMouse(current.mousePosition, val);
			current.Use();
		}
		else if ((int)current.type == 1 && wheelDragging)
		{
			wheelDragging = false;
			current.Use();
		}
		Vector2 val2 = ((Rect)(ref val)).center + new Vector2(Mathf.Cos(hue * 2f * (float)Math.PI), 0f - Mathf.Sin(hue * 2f * (float)Math.PI)) * (saturation * (((Rect)(ref val)).width / 2f));
		GUI.color = Color.black;
		GUI.DrawTexture(new Rect(val2.x - 7f, val2.y - 7f, 14f, 14f), (Texture)(object)Texture2D.whiteTexture);
		GUI.color = Color.white;
		GUI.DrawTexture(new Rect(val2.x - 4f, val2.y - 4f, 8f, 8f), (Texture)(object)Texture2D.whiteTexture);
		GUI.Label(new Rect(30f, 550f, 160f, 32f), "Brightness " + Mathf.RoundToInt(brightness * 100f) + "%", body);
		float num = GUI.HorizontalSlider(new Rect(200f, 560f, 345f, 25f), brightness, 0f, 1f);
		if (Mathf.Abs(num - brightness) > 0.0001f)
		{
			brightness = num;
			Preview();
		}
		string text = (session.Dirty ? ("Unsaved " + ChannelName + " preview") : ((session.Saved != 0) ? (ChannelName + " colour applied") : ("Original " + ChannelName + " colour")));
		GUI.Label(new Rect(30f, 588f, 520f, 32f), text, centered);
		GUI.color = Color.HSVToRGB(hue, saturation, brightness);
		GUI.DrawTexture(new Rect(30f, 631f, 100f, 38f), (Texture)(object)Texture2D.whiteTexture);
		GUI.color = Color.white;
		if (GUI.Button(new Rect(145f, 628f, 405f, 44f), "Effect: " + LightAnimation.Name(session.SelectedEffect), button))
		{
			CycleEffect();
		}
		bool enabled2 = GUI.enabled;
		GUI.enabled = session.Dirty;
		if (GUI.Button(new Rect(20f, 690f, 172f, 60f), ((controller && !favouritesMode) ? "A · Apply " : "Apply ") + ChannelName, button))
		{
			Apply();
		}
		GUI.enabled = Object.op_Implicit((Object)(object)target) && (ChannelSaved != 0 || session.Dirty);
		if (GUI.Button(new Rect(204f, 690f, 172f, 60f), (controller && !favouritesMode) ? "X · Default" : "Restore default", button))
		{
			RestoreDefault();
		}
		GUI.enabled = enabled2;
		string text2 = (session.Dirty ? ("Cancel " + ChannelName) : (channels.AnyDirty ? "Discard & close" : "Close"));
		if (GUI.Button(new Rect(388f, 690f, 172f, 60f), ((controller && !favouritesMode) ? "B · " : "") + text2, button))
		{
			if (session.Dirty)
			{
				CancelPreview();
			}
			else
			{
				Close();
			}
		}
		GUI.Label(new Rect(30f, 761f, 520f, 40f), (message.Length > 0) ? message : (flameChannel ? "Colours the flame, wisp, liquid or glowing core." : "Colours nearby surfaces. Use Source for the visible centre."), small);
		GUI.Label(new Rect(30f, 801f, 520f, 112f), controller ? "Right stick: colour · LB/RB: brightness\nA: apply · X: default · B: cancel / close\nD-pad: left Light / right Source · Y: favourites\nRight stick press: change effect\n* Unsaved preview in that setting" : "Click / drag the wheel; slider: brightness\nArrow keys: colour · +/-: brightness\nEnter: apply · Delete: default · Esc: close\nC: Source / Light · E: effect\nTab: favourites · F: save · * Unsaved preview", small);
	}

	private void DrawFavourites()
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_024e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_010d: 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_02de: Unknown result type (might be due to invalid IL or missing references)
		//IL_0147: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Unknown result type (might be due to invalid IL or missing references)
		//IL_015c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0193: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_032b: Unknown result type (might be due to invalid IL or missing references)
		GUI.Label(new Rect(10f, 20f, 260f, 45f), "Favourite colours", heading);
		GUI.Label(new Rect(15f, 76f, 250f, 55f), (!controller) ? "10 slots saved with your character" : (favouritesMode ? "Choose a slot" : "Y · Open favourites"), centered);
		Rect val = default(Rect);
		for (int i = 0; i < 10; i++)
		{
			((Rect)(ref val))..ctor((float)(20 + i % 2 * 125), (float)(120 + i / 2 * 64), 115f, 56f);
			Color colour;
			bool flag = LightFavourites.TryGet(Player.m_localPlayer, i, out colour);
			Color color = GUI.color;
			GUI.color = ((i == favouriteSlot) ? new Color(1f, 0.72f, 0.15f) : new Color(0.4f, 0.43f, 0.46f));
			GUI.DrawTexture(val, (Texture)(object)Texture2D.whiteTexture);
			GUI.color = (Color)(flag ? colour : new Color(0.1f, 0.12f, 0.14f));
			GUI.DrawTexture(new Rect(((Rect)(ref val)).x + 4f, ((Rect)(ref val)).y + 4f, ((Rect)(ref val)).width - 8f, ((Rect)(ref val)).height - 8f), (Texture)(object)Texture2D.whiteTexture);
			GUI.color = color;
			if (GUI.Button(val, "", GUIStyle.none))
			{
				favouriteSlot = i;
				if (flag)
				{
					PreviewFavourite();
				}
			}
			GUI.color = new Color(0f, 0f, 0f, 0.8f);
			GUI.DrawTexture(new Rect(((Rect)(ref val)).x + 4f, ((Rect)(ref val)).y + 24f, ((Rect)(ref val)).width - 8f, 28f), (Texture)(object)Texture2D.whiteTexture);
			GUI.color = color;
			GUI.Label(new Rect(((Rect)(ref val)).x, ((Rect)(ref val)).y + 22f, ((Rect)(ref val)).width, 30f), i + 1 + (flag ? "" : " · Empty"), centered);
		}
		if (GUI.Button(new Rect(20f, 452f, 240f, 48f), (controller && favouritesMode) ? ("Y · Save in slot " + (favouriteSlot + 1)) : ("Save in slot " + (favouriteSlot + 1)), button))
		{
			SaveFavourite();
		}
		bool enabled = GUI.enabled;
		GUI.enabled = LightFavourites.TryGet(Player.m_localPlayer, favouriteSlot, out var _);
		if (GUI.Button(new Rect(20f, 510f, 240f, 48f), (controller && favouritesMode) ? "X · Clear favourite" : "Clear favourite", button))
		{
			RemoveFavourite();
		}
		GUI.enabled = enabled;
		GUI.Label(new Rect(20f, 584f, 240f, 150f), controller ? "In favourites:\nLeft stick / D-pad: slot\nA: preview · Y: save\nX: clear · B: return\nThen A applies to the light." : "Click a saved slot to preview its colour. Apply saves it on the light.\n\nSelect any slot, then Save to store or replace it.", small);
	}

	private void SetFromMouse(Vector2 position, Rect rect)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0003: 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_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_003d: 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)
		Vector2 val = position - ((Rect)(ref rect)).center;
		saturation = Mathf.Clamp01(((Vector2)(ref val)).magnitude / (((Rect)(ref rect)).width * 0.5f));
		if (((Vector2)(ref val)).sqrMagnitude > 0.001f)
		{
			hue = Mathf.Repeat(Mathf.Atan2(0f - val.y, val.x) / ((float)Math.PI * 2f), 1f);
		}
		Preview();
	}

	private void CreateWheel()
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Expected O, but got Unknown
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: 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_00b9: Unknown result type (might be due to invalid IL or missing references)
		wheel = new Texture2D(400, 400, (TextureFormat)4, false);
		((Object)wheel).name = "LightUpYourWorld_ColourWheel";
		((Texture)wheel).wrapMode = (TextureWrapMode)1;
		Color32[] array = (Color32[])(object)new Color32[160000];
		float num = 199.5f;
		for (int i = 0; i < 400; i++)
		{
			for (int j = 0; j < 400; j++)
			{
				float num2 = ((float)j - num) / num;
				float num3 = ((float)i - num) / num;
				float num4 = Mathf.Sqrt(num2 * num2 + num3 * num3);
				array[i * 400 + j] = (Color32)((num4 <= 1f) ? Color32.op_Implicit(Color.HSVToRGB(Mathf.Repeat(Mathf.Atan2(num3, num2) / ((float)Math.PI * 2f), 1f), num4, 1f)) : new Color32((byte)0, (byte)0, (byte)0, (byte)0));
			}
		}
		wheel.SetPixels32(array);
		wheel.Apply(false, true);
	}
}
[HarmonyPatch(typeof(Piece), "Awake")]
internal static class AttachLightPatch
{
	private static void Postfix(Piece __instance)
	{
		WorldLight.Attach(__instance);
	}
}
[HarmonyPatch(typeof(Player), "Interact")]
internal static class LightInteractionPatch
{
	private static bool Prefix(Player __instance, GameObject __0, bool __1)
	{
		LightUpYourWorldPlugin instance = LightUpYourWorldPlugin.Instance;
		if (!Object.op_Implicit((Object)(object)instance) || (Object)(object)__instance != (Object)(object)Player.m_localPlayer)
		{
			return true;
		}
		if (instance.BlocksGameplay)
		{
			return false;
		}
		if (!ZInput.GetButton("JoyLBumper"))
		{
			return true;
		}
		WorldLight worldLight = WorldLight.Find(__0);
		if (!Object.op_Implicit((Object)(object)worldLight))
		{
			return true;
		}
		if (!__1)
		{
			instance.Open(worldLight);
		}
		return false;
	}
}
[HarmonyPatch(typeof(Player), "TakeInput")]
internal static class PlayerInputPatch
{
	private static void Postfix(ref bool __result)
	{
		if (Object.op_Implicit((Object)(object)LightUpYourWorldPlugin.Instance) && LightUpYourWorldPlugin.Instance.BlocksGameplay)
		{
			__result = false;
		}
	}
}
[HarmonyPatch(typeof(PlayerController), "TakeInput")]
internal static class ControllerInputPatch
{
	private static void Postfix(ref bool __result)
	{
		if (Object.op_Implicit((Object)(object)LightUpYourWorldPlugin.Instance) && LightUpYourWorldPlugin.Instance.BlocksGameplay)
		{
			__result = false;
		}
	}
}
[HarmonyPatch(typeof(HotkeyBar), "Update")]
internal static class HotbarPatch
{
	private static bool Prefix()
	{
		if (Object.op_Implicit((Object)(object)LightUpYourWorldPlugin.Instance))
		{
			return !LightUpYourWorldPlugin.Instance.BlocksGameplay;
		}
		return true;
	}
}
[HarmonyPatch(typeof(GameCamera), "UpdateMouseCapture")]
internal static class CursorPatch
{
	private static void Postfix()
	{
		if (Object.op_Implicit((Object)(object)LightUpYourWorldPlugin.Instance))
		{
			LightUpYourWorldPlugin.Instance.KeepCursor();
		}
	}
}
[HarmonyPatch(typeof(Character), "Damage")]
internal static class DamagePatch
{
	private static void Prefix(Character __instance)
	{
		if ((Object)(object)__instance == (Object)(object)Player.m_localPlayer && Object.op_Implicit((Object)(object)LightUpYourWorldPlugin.Instance))
		{
			LightUpYourWorldPlugin.Instance.Close();
		}
	}
}
public sealed class WorldLight : MonoBehaviour
{
	private sealed class PieceKind
	{
		internal bool IsBoat;

		internal float NextProbe;

		internal bool Probed;
	}

	internal static readonly HashSet<WorldLight> Sources = new HashSet<WorldLight>();

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

	private readonly Dictionary<Light, Color> originals = new Dictionary<Light, Color>();

	private readonly FlameVisuals flames = new FlameVisuals();

	private int savedFlame;

	private int previewFlame;

	private int savedEffect;

	private int savedFlameEffect;

	private int previewEffect;

	private int previewFlameEffect;

	private bool flamePreview;

	private ZNetView view;

	private Piece piece;

	private int saved;

	private int applied = -1;

	private bool initialized;

	private bool preview;

	private int previewColour;

	private float nextRead;

	private float nextDiscovery;

	private float nextFullDiscovery;

	private int hierarchyCount = -1;

	private bool flameDiscoveryNeeded = true;

	private bool flamesRequested;

	private static readonly ConditionalWeakTable<Piece, PieceKind> Kinds = new ConditionalWeakTable<Piece, PieceKind>();

	private bool refreshRequested = true;

	private float nextFaultRetry;

	private bool faultReported;

	internal int SavedEffect
	{
		get
		{
			if (!Ready)
			{
				return 0;
			}
			return LightAnimation.Normalize(view.GetZDO().GetInt("LightUpYourWorld.Effect", 0));
		}
	}

	internal int SavedFlameEffect
	{
		get
		{
			if (!Ready)
			{
				return 0;
			}
			return LightAnimation.Normalize(view.GetZDO().GetInt("LightUpYourWorld.FlameEffect", 0));
		}
	}

	internal bool HasFlame => flames.Available;

	internal string VisualStatus
	{
		get
		{
			if (!flames.Available)
			{
				return "No editable visible effect found";
			}
			return "Fire, wisp and lamp emission";
		}
	}

	internal Color OriginalFlameColour => flames.OriginalColour(OriginalColour);

	internal int SavedFlame
	{
		get
		{
			if (!Ready)
			{
				return 0;
			}
			return LightColour.Normalize(view.GetZDO().GetInt("LightUpYourWorld.Flame", 0));
		}
	}

	internal bool Ready
	{
		get
		{
			if (initialized && Object.op_Implicit((Object)(object)view) && view.IsValid())
			{
				return originals.Count > 0;
			}
			return false;
		}
	}

	internal int Saved
	{
		get
		{
			if (!Ready)
			{
				return 0;
			}
			return LightColour.Normalize(view.GetZDO().GetInt("LightUpYourWorld.Colour", 0));
		}
	}

	internal string DisplayName
	{
		get
		{
			if (!Object.op_Implicit((Object)(object)piece))
			{
				return "Light";
			}
			return LightUpYourWorldPlugin.Localize(piece.m_name);
		}
	}

	internal Color OriginalColour
	{
		get
		{
			//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)
			//IL_0047: 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)
			foreach (KeyValuePair<Light, Color> original in originals)
			{
				if (Object.op_Implicit((Object)(object)original.Key))
				{
					return original.Value;
				}
			}
			return Color.white;
		}
	}

	private static bool IsSupportedPiece(Piece owner)
	{
		if (Object.op_Implicit((Object)(object)owner))
		{
			return !Kinds.GetValue(owner, (Piece piece) => new PieceKind
			{
				IsBoat = Object.op_Implicit((Object)(object)((Component)piece).GetComponentInChildren<Ship>(true))
			}).IsBoat;
		}
		return false;
	}

	internal static WorldLight Find(GameObject obj)
	{
		if (!Object.op_Implicit((Object)(object)obj))
		{
			return null;
		}
		Piece componentInParent = obj.GetComponentInParent<Piece>();
		if (!IsSupportedPiece(componentInParent))
		{
			return null;
		}
		ZNetView component = ((Component)componentInParent).GetComponent<ZNetView>();
		if (!Object.op_Implicit((Object)(object)component) || !component.IsValid())
		{
			return null;
		}
		WorldLight worldLight = ((Component)componentInParent).GetComponent<WorldLight>();
		if (!Object.op_Implicit((Object)(object)worldLight))
		{
			PieceKind value = Kinds.GetValue(componentInParent, (Piece p) => new PieceKind());
			if (value.Probed && Time.unscaledTime < value.NextProbe)
			{
				return null;
			}
			value.Probed = true;
			value.NextProbe = Time.unscaledTime + 2f;
			if (Object.op_Implicit((Object)(object)((Component)componentInParent).GetComponentInChildren<Light>(true)))
			{
				worldLight = ((Component)componentInParent).gameObject.AddComponent<WorldLight>();
			}
		}
		if (!Object.op_Implicit((Object)(object)worldLight) || !worldLight.Ready)
		{
			return null;
		}
		return worldLight;
	}

	internal static void Attach(Piece owner)
	{
		if (IsSupportedPiece(owner))
		{
			ZNetView component = ((Component)owner).GetComponent<ZNetView>();
			if (Object.op_Implicit((Object)(object)component) && component.IsValid() && !Object.op_Implicit((Object)(object)((Component)owner).GetComponent<WorldLight>()) && Object.op_Implicit((Object)(object)((Component)owner).GetComponentInChildren<Light>(true)))
			{
				((Component)owner).gameObject.AddComponent<WorldLight>();
			}
		}
	}

	private void OnEnable()
	{
		Sources.Add(this);
	}

	private void OnDisable()
	{
		Sources.Remove(this);
		EndPreview();
	}

	private void OnDestroy()
	{
		Sources.Remove(this);
		flames.Dispose();
	}

	private void Start()
	{
		piece = ((Component)this).GetComponent<Piece>();
		view = ((Component)this).GetComponent<ZNetView>();
		if (!IsSupportedPiece(piece) || !Object.op_Implicit((Object)(object)view) || !view.IsValid())
		{
			((Behaviour)this).enabled = false;
		}
		else
		{
			initialized = true;
		}
	}

	internal void RefreshLights()
	{
		if (initialized)
		{
			DiscoverLights();
			flames.Discover(this);
			flamesRequested = true;
			flameDiscoveryNeeded = false;
			hierarchyCount = ((Component)this).transform.hierarchyCount;
			refreshRequested = false;
			nextDiscovery = Time.unscaledTime + 2f;
			nextFullDiscovery = Time.unscaledTime + 5f;
		}
	}

	private void DiscoverLights()
	{
		//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
		deadLights.Clear();
		foreach (KeyValuePair<Light, Color> original in originals)
		{
			if (!Object.op_Implicit((Object)(object)original.Key))
			{
				deadLights.Add(original.Key);
			}
		}
		foreach (Light deadLight in deadLights)
		{
			originals.Remove(deadLight);
		}
		Light[] componentsInChildren = ((Component)this).GetComponentsInChildren<Light>(true);
		foreach (Light val in componentsInChildren)
		{
			if (Object.op_Implicit((Object)(object)val) && !((Object)(object)((Component)val).GetComponentInParent<Piece>() != (Object)(object)piece) && !originals.ContainsKey(val))
			{
				Light val2 = NativeVisuals.Find<Light>(this, (Component)(object)val);
				originals.Add(val, Object.op_Implicit((Object)(object)val2) ? val2.color : val.color);
				applied = -1;
			}
		}
	}

	private void OnTransformChildrenChanged()
	{
		refreshRequested = true;
	}

	internal void Tick()
	{
		if (Time.unscaledTime < nextFaultRetry)
		{
			return;
		}
		try
		{
			TickCore();
		}
		catch (Exception ex)
		{
			nextFaultRetry = Time.unscaledTime + 5f;
			if (!faultReported)
			{
				LightUpYourWorldPlugin.Warn("Light update on " + DisplayName + ": " + ex.GetBaseException().Message);
			}
			faultReported = true;
		}
	}

	private void TickCore()
	{
		//IL_01a9: 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_01ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0182: 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)
		if (!initialized || !Object.op_Implicit((Object)(object)view) || !view.IsValid())
		{
			return;
		}
		if (refreshRequested || Time.unscaledTime >= nextDiscovery)
		{
			int num = ((Component)this).transform.hierarchyCount;
			if (refreshRequested || num != hierarchyCount || ((saved != 0 || savedFlame != 0 || preview || flamePreview) && Time.unscaledTime >= nextFullDiscovery))
			{
				DiscoverLights();
				hierarchyCount = num;
				flameDiscoveryNeeded = true;
				nextFullDiscovery = Time.unscaledTime + 5f;
			}
			refreshRequested = false;
			nextDiscovery = Time.unscaledTime + 2f;
		}
		if (Time.unscaledTime >= nextRead)
		{
			savedFlame = SavedFlame;
			saved = Saved;
			savedEffect = SavedEffect;
			savedFlameEffect = SavedFlameEffect;
			nextRead = Time.unscaledTime + 0.25f;
		}
		int num2 = LightAnimation.Colour(preview ? previewColour : saved, preview ? previewEffect : savedEffect, LightAnimation.Clock);
		if (num2 == 0)
		{
			if (applied != 0)
			{
				foreach (KeyValuePair<Light, Color> original in originals)
				{
					if (Object.op_Implicit((Object)(object)original.Key))
					{
						original.Key.color = original.Value;
					}
				}
			}
		}
		else
		{
			Color val = Unpack(num2);
			foreach (KeyValuePair<Light, Color> original2 in originals)
			{
				if (Object.op_Implicit((Object)(object)original2.Key) && ((Component)original2.Key).gameObject.activeInHierarchy && original2.Key.color != val)
				{
					original2.Key.color = val;
				}
			}
		}
		applied = num2;
		if (flameDiscoveryNeeded && (flamesRequested || flamePreview || savedFlame != 0))
		{
			flames.Discover(this);
			flamesRequested = true;
			flameDiscoveryNeeded = false;
		}
		flames.Apply(LightAnimation.Colour(flamePreview ? previewFlame : savedFlame, flamePreview ? previewFlameEffect : savedFlameEffect, LightAnimation.Clock));
	}

	internal static int Pack(Color colour)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//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)
		//IL_0007: 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_0013: Unknown result type (might be due to invalid IL or missing references)
		Color32 val = Color32.op_Implicit(colour);
		return LightColour.Pack(val.r, val.g, val.b);
	}

	internal static Color Unpack(int colour)
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		return Color32.op_Implicit(new Color32(LightColour.Red(colour), LightColour.Green(colour), LightColour.Blue(colour), byte.MaxValue));
	}

	internal void Preview(int colour, int effect = 0)
	{
		previewEffect = LightAnimation.Normalize(effect);
		preview = true;
		previewColour = LightColour.Normalize(colour);
		Tick();
	}

	internal void PreviewFlame(int colour, int effect = 0)
	{
		previewFlameEffect = LightAnimation.Normalize(effect);
		flamePreview = true;
		previewFlame = LightColour.Normalize(colour);
		Tick();
	}

	internal void PreviewChannels(ColourChannels channels)
	{
		preview = channels.Light.Dirty;
		previewColour = channels.Light.Selected;
		previewEffect = channels.Light.SelectedEffect;
		flamePreview = channels.Source.Dirty;
		previewFlame = channels.Source.Selected;
		previewFlameEffect = channels.Source.SelectedEffect;
		nextRead = 0f;
		Tick();
	}

	internal bool CommitFlame(int colour, int effect = 0)
	{
		if (!HasFlame || !LightColour.IsValid(colour) || !CanEdit(Player.m_localPlayer, flash: true))
		{
			return false;
		}
		view.ClaimOwnership();
		if (!view.IsOwner())
		{
			return false;
		}
		view.GetZDO().Set("LightUpYourWorld.Flame", colour);
		view.GetZDO().Set("LightUpYourWorld.FlameEffect", (colour != 0) ? LightAnimation.Normalize(effect) : 0);
		flamePreview = false;
		nextRead = 0f;
		Tick();
		return true;
	}

	internal void EndPreview()
	{
		flamePreview = false;
		preview = false;
		nextRead = 0f;
		Tick();
	}

	internal bool CanEdit(Player player, bool flash)
	{
		//IL_001e: 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_0040: Unknown result type (might be due to invalid IL or missing references)
		if (Object.op_Implicit((Object)(object)player) && Ready && !((Character)player).IsDead() && Vector3.Distance(((Component)player).transform.position, ((Component)this).transform.position) <= 8f)
		{
			return PrivateArea.CheckAccess(((Component)this).transform.position, 0f, flash, false);
		}
		return false;
	}

	internal bool Commit(int colour, int effect = 0)
	{
		if (!LightColour.IsValid(colour) || !CanEdit(Player.m_localPlayer, flash: true))
		{
			return false;
		}
		view.ClaimOwnership();
		if (!view.IsOwner())
		{
			return false;
		}
		view.GetZDO().Set("LightUpYourWorld.Colour", colour);
		view.GetZDO().Set("LightUpYourWorld.Effect", (colour != 0) ? LightAnimation.Normalize(effect) : 0);
		saved = colour;
		preview = false;
		nextRead = 0f;
		Tick();
		return true;
	}

	internal void RestoreForUnload()
	{
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		flames.Dispose();
		foreach (KeyValuePair<Light, Color> original in originals)
		{
			if (Object.op_Implicit((Object)(object)original.Key))
			{
				original.Key.color = original.Value;
			}
		}
	}
}
internal sealed class FlameVisuals
{
	private sealed class Entry
	{
		internal Renderer renderer;

		internal int slot;

		internal int last;

		internal ParticleColours particles;

		internal bool particle;

		internal bool ownsParticles;

		internal bool warned;

		internal float nextAttempt;

		internal readonly Dictionary<string, Color> colours = new Dictionary<string, Color>();

		internal readonly Dictionary<string, Texture> textures = new Dictionary<string, Texture>();

		internal readonly Dictionary<string, Texture> neutral = new Dictionary<string, Texture>();

		internal readonly MaterialPropertyBlock working = new MaterialPropertyBlock();
	}

	private readonly List<Entry> entries = new List<Entry>();

	private readonly Dictionary<Renderer, Material[]> inspected = new Dictionary<Renderer, Material[]>();

	private readonly List<Renderer> dead = new List<Renderer>();

	private int applied = -1;

	private float nextApply;

	internal bool Available => entries.Count > 0;

	internal int Count => entries.Count;

	internal Color OriginalColour(Color fallback)
	{
		//IL_002d: 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_00c5: 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_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Unknown result type (might be due to invalid IL or missing references)
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: 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)
		foreach (Entry entry in entries)
		{
			if (entry.particles != null && entry.particles.TryOriginalColour(out var colour))
			{
				return colour;
			}
		}
		foreach (Entry entry2 in entries)
		{
			foreach (Color value in entry2.colours.Values)
			{
				if (Strength(value) > 0f)
				{
					return value;
				}
			}
		}
		return fallback;
	}

	internal static bool IsFlame(string name)
	{
		if (!VisualRules.Excluded(name))
		{
			return VisualRules.EffectMesh(name, "", "");
		}
		return false;
	}

	internal static bool IsFireMesh(string material, string shader)
	{
		return VisualRules.EffectMesh(material, shader, "");
	}

	private static float Strength(Color c)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		return Mathf.Max(c.r, Mathf.Max(c.g, c.b));
	}

	private static bool SameMaterials(Material[] a, Material[] b)
	{
		if (a.Length != b.Length)
		{
			return false;
		}
		for (int i = 0; i < a.Length; i++)
		{
			if ((Object)(object)a[i] != (Object)(object)b[i])
			{
				return false;
			}
		}
		return true;
	}

	private static void Release(Entry e)
	{
		foreach (Texture value in e.textures.Values)
		{
			if (e.neutral.Count > 0)
			{
				NeutralTextures.Release(value);
			}
		}
		e.neutral.Clear();
	}

	internal void Discover(WorldLight owner)
	{
		//IL_02d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_02dc: Expected O, but got Unknown
		//IL_036a: Unknown result type (might be due to invalid IL or missing references)
		//IL_032c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0321: Unknown result type (might be due to invalid IL or missing references)
		//IL_039e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0393: Unknown result type (might be due to invalid IL or missing references)
		dead.Clear();
		foreach (KeyValuePair<Renderer, Material[]> item in inspected)
		{
			if (!Object.op_Implicit((Object)(object)item.Key))
			{
				dead.Add(item.Key);
			}
		}
		foreach (Renderer item2 in dead)
		{
			inspected.Remove(item2);
		}
		for (int num = entries.Count - 1; num >= 0; num--)
		{
			if (!Object.op_Implicit((Object)(object)entries[num].renderer))
			{
				Release(entries[num]);
				entries.RemoveAt(num);
			}
		}
		Piece component = ((Component)owner).GetComponent<Piece>();
		Renderer[] componentsInChildren = ((Component)owner).GetComponentsInChildren<Renderer>(true);
		foreach (Renderer val in componentsInChildren)
		{
			if (!Object.op_Implicit((Object)(object)val) || (Object)(object)((Component)val).GetComponentInParent<Piece>() != (Object)(object)component)
			{
				continue;
			}
			Material[] sharedMaterials = val.sharedMaterials;
			if (inspected.TryGetValue(val, out var value) && SameMaterials(value, sharedMaterials))
			{
				continue;
			}
			for (int num2 = entries.Count - 1; num2 >= 0; num2--)
			{
				if ((Object)(object)entries[num2].renderer == (Object)(object)val)
				{
					RestoreEntry(entries[num2], restoreColours: false);
					Release(entries[num2]);
					entries.RemoveAt(num2);
				}
			}
			inspected[val] = sharedMaterials;
			bool flag = val is ParticleSystemRenderer;
			Renderer val2 = NativeVisuals.Find<Renderer>(owner, (Component)(object)val);
			ParticleColours particleColours = null;
			for (int j = 0; j < sharedMaterials.Length; j++)
			{
				Material val3 = sharedMaterials[j];
				if (!Object.op_Implicit((Object)(object)val3))
				{
					continue;
				}
				string text = (Object.op_Implicit((Object)(object)val3.shader) ? ((Object)val3.shader).name : "");
				if (VisualRules.Excluded(((Object)val).name + " " + ((Object)val3).name))
				{
					continue;
				}
				bool flag2 = flag || VisualRules.EffectMesh(((Object)val3).name, text, ((Object)val).name);
				Material val4 = ((Object.op_Implicit((Object)(object)val2) && j < val2.sharedMaterials.Length) ? val2.sharedMaterials[j] : val3);
				if (!Object.op_Implicit((Object)(object)val4) || !Object.op_Implicit((Object)(object)val4.shader) || ((Object)val4.shader).name != text)
				{
					val4 = val3;
				}
				Entry entry = new Entry
				{
					renderer = val,
					slot = j,
					particle = flag
				};
				MaterialPropertyBlock val5 = new MaterialPropertyBlock();
				val.GetPropertyBlock(val5, j);
				string[] effectKeys;
				if (flag2)
				{
					effectKeys = VisualRules.EffectKeys;
					foreach (string text2 in effectKeys)
					{
						if (val3.HasProperty(text2))
						{
							entry.colours[text2] = (val5.HasColor(text2) ? val5.GetColor(text2) : val4.GetColor(text2));
						}
					}
				}
				effectKeys = VisualRules.EmissionKeys;
				foreach (string text3 in effectKeys)
				{
					if (val3.HasProperty(text3) && (flag2 || Strength(val4.GetColor(text3)) > 0.0001f))
					{
						entry.colours[text3] = (val5.HasColor(text3) ? val5.GetColor(text3) : val4.GetColor(text3));
					}
				}
				if (flag && particleColours == null)
				{
					particleColours = new ParticleColours(((Component)val).GetComponent("ParticleSystem"), VisualRules.GradientMapped(text), Object.op_Implicit((Object)(object)val2) ? ((Component)val2).GetComponent("ParticleSystem") : null);
					entry.ownsParticles = true;
				}
				entry.particles = particleColours;
				if (entry.colours.Count == 0 && (entry.particles == null || !entry.particles.Available))
				{
					continue;
				}
				if (flag2 && !VisualRules.GradientMapped(text))
				{
					CaptureTexture(entry, val4, val5, "_MainTex");
					if (VisualRules.FlowingEffect(text))
					{
						effectKeys = VisualRules.FlowColourTextures;
						foreach (string key in effectKeys)
						{
							CaptureTexture(entry, val4, val5, key);
						}
					}
				}
				effectKeys = new string[3] { "_EmissionMap", "_EmissiveTex", "_EmissiveMap" };
				foreach (string key2 in effectKeys)
				{
					if (flag2 || entry.colours.Count > 0)
					{
						CaptureTexture(entry, val4, val5, key2);
					}
				}
				entries.Add(entry);
				applied = -1;
			}
		}
	}

	private static void CaptureTexture(Entry e, Material m, MaterialPropertyBlock block, string key)
	{
		if (m.HasProperty(key))
		{
			Texture val = (block.HasTexture(key) ? block.GetTexture(key) : m.GetTexture(key));
			if (Object.op_Implicit((Object)(object)val))
			{
				e.textures[key] = val;
			}
		}
	}

	private static void RestoreEntry(Entry e, bool restoreColours = true)
	{
		//IL_008c: 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)
		if (!Object.op_Implicit((Object)(object)e.renderer))
		{
			e.last = 0;
			return;
		}
		if (e.ownsParticles && e.particles != null)
		{
			e.particles.Apply(0);
		}
		if (Object.op_Implicit((Object)(object)e.renderer) && e.last != 0)
		{
			e.renderer.GetPropertyBlock(e.working, e.slot);
			if (restoreColours)
			{
				foreach (KeyValuePair<string, Color> colour in e.colours)
				{
					e.working.SetColor(colour.Key, colour.Value);
				}
			}
			else
			{
				foreach (KeyValuePair<string, Color> colour2 in e.colours)
				{
					Material[] sharedMaterials = e.renderer.sharedMaterials;
					if (e.slot < sharedMaterials.Length && Object.op_Implicit((Object)(object)sharedMaterials[e.slot]) && sharedMaterials[e.slot].HasProperty(colour2.Key))
					{
						e.working.SetColor(colour2.Key, sharedMaterials[e.slot].GetColor(colour2.Key));
					}
				}
			}
			foreach (KeyValuePair<string, Texture> texture in e.textures)
			{
				Material[] sharedMaterials2 = e.renderer.sharedMaterials;
				Texture val = (restoreColours ? texture.Value : ((e.slot < sharedMaterials2.Length && Object.op_Implicit((Object)(object)sharedMaterials2[e.slot]) && sharedMaterials2[e.slot].HasProperty(texture.Key)) ? sharedMaterials2[e.slot].GetTexture(texture.Key) : null));
				e.working.SetTexture(texture.Key, val);
			}
			if (e.slot < e.renderer.sharedMaterials.Length)
			{
				e.renderer.SetPropertyBlock(e.working, e.slot);
			}
		}
		e.last = 0;
	}

	internal void Apply(int packed)
	{
		//IL_00f5: 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_00fc: 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)
		//IL_012e: Unknown result type (might be due to invalid IL or missing references)
		//IL_012f: Unknown result type (might be due to invalid IL or missing references)
		bool flag = packed != applied;
		if (!flag && (packed == 0 || Time.unscaledTime < nextApply))
		{
			return;
		}
		nextApply = Time.unscaledTime + 0.1f;
		foreach (Entry entry in entries)
		{
			if (!Object.op_Implicit((Object)(object)entry.renderer) || Time.unscaledTime < entry.nextAttempt)
			{
				continue;
			}
			try
			{
				if (packed == 0)
				{
					RestoreEntry(entry);
				}
				else
				{
					if (!((Component)entry.renderer).gameObject.activeInHierarchy)
					{
						continue;
					}
					if (entry.ownsParticles && entry.particles != null)
					{
						entry.particles.Apply(packed, flag || entry.last != packed);
					}
					entry.renderer.GetPropertyBlock(entry.working, entry.slot);
					Color chosen = ((entry.particle && entry.particles != null && entry.particles.Available) ? Color.white : WorldLight.Unpack(packed));
					foreach (KeyValuePair<string, Color> colour in entry.colours)
					{
						entry.working.SetColor(colour.Key, ParticleColours.Recolour(colour.Value, chosen));
					}
					if (entry.neutral.Count == 0)
					{
						foreach (KeyValuePair<string, Texture> texture in entry.textures)
						{
							entry.neutral[texture.Key] = NeutralTextures.Acquire(texture.Value);
						}
					}
					foreach (KeyValuePair<string, Texture> item in entry.neutral)
					{
						entry.working.SetTexture(item.Key, item.Value);
					}
					entry.renderer.SetPropertyBlock(entry.working, entry.slot);
					entry.last = packed;
					continue;
				}
			}
			catch (Exception ex)
			{
				entry.nextAttempt = Time.unscaledTime + 5f;
				if (!entry.warned)
				{
					LightUpYourWorldPlugin.Warn("Visible light effect " + ((Object)entry.renderer).name + " could not be updated: " + ex.GetBaseException().Message);
				}
				entry.warned = true;
			}
		}
		applied = packed;
	}

	internal void Restore()
	{
		applied = -1;
		foreach (Entry entry in entries)
		{
			try
			{
				RestoreEntry(entry);
			}
			catch (Exception ex)
			{
				if (!entry.warned)
				{
					LightUpYourWorldPlugin.Warn("Light reset: " + ex.GetBaseException().Message);
				}
				entry.warned = true;
			}
			entry.nextAttempt = 0f;
		}
	}

	internal void Dispose()
	{
		Restore();
		foreach (Entry entry in entries)
		{
			Release(entry);
		}
		entries.Clear();
		inspected.Clear();
	}
}
internal static class VisualRules
{
	internal static readonly string[] EmissionKeys = new string[4] { "_EmissionColor", "_EmissiveColor", "_GlowColor", "_NoiseGlowColor" };

	internal static readonly string[] EffectKeys = new string[11]
	{
		"_Color", "_TintColor", "_BaseColor", "_ColorTop", "_ColorBottom", "_Color1", "_Color2", "_StartColor", "_EndColor", "_FlowColor",
		"_FoamColor"
	};

	internal static readonly string[] FlowColourTextures = new string[2] { "_FoamTex", "_FlowTexture" };

	internal static bool Excluded(string name)
	{
		string text = (name ?? "").ToLowerInvariant();
		if (!text.Contains("smoke") && !text.Contains("steam") && !text.Contains("dust") && !(text == "ash") && !text.StartsWith("ash_") && !text.Contains(" ashes"))
		{
			return text.Contains("mistarea");
		}
		return true;
	}

	internal static bool GradientMapped(string shader)
	{
		string text = (shader ?? "").ToLowerInvariant();
		if (text.Contains("gradient"))
		{
			return text.Contains("particle");
		}
		return false;
	}

	internal static bool FlowingEffect(string shader)
	{
		string text = (shader ?? "").ToLowerInvariant();
		switch (text)
		{
		default:
			return text == "custom/bonemass";
		case "custom/flow":
		case "custom/flowopaque":
		case "custom/blob":
			return true;
		}
	}

	internal static bool EffectMesh(string material, string shader, string renderer)
	{
		string text = (material ?? "").ToLowerInvariant();
		string text2 = (shader ?? "").ToLowerInvariant();
		string text3 = (text + " " + text2 + " " + renderer).ToLowerInvariant();
		if (Excluded(text3) || text2 == "custom/piece")
		{
			return false;
		}
		if (!FlowingEffect(text2) && (!text.Contains("flame") || text.Contains("flametal")) && !text.StartsWith("fire_") && !text2.Contains("fire") && !text2.Contains("flame") && !text3.Contains("demister_ball") && !text.Contains("yagluthsoul") && !text3.Contains("wisp") && !text3.Contains("halo") && !text3.Contains("glow"))
		{
			return text3.Contains("lightshaft");
		}
		return true;
	}
}
internal static class NativeVisuals
{
	internal static T Find<T>(WorldLight owner, Component live) where T : Component
	{
		ZNetView component = ((Component)owner).GetComponent<ZNetView>();
		if (!Object.op_Implicit((Object)(object)ZNetScene.instance) || !Object.op_Implicit((Object)(object)component) || !component.IsValid())
		{
			return default(T);
		}
		GameObject prefab = ZNetScene.instance.GetPrefab(component.GetZDO().GetPrefab());
		if (!Object.op_Implicit((Object)(object)prefab) || (Object)(object)prefab == (Object)(object)((Component)owner).gameObject)
		{
			return default(T);
		}
		List<Transform> list = new List<Transform>();
		Transform val = live.transform;
		while (Object.op_Implicit((Object)(object)val) && (Object)(object)val != (Object)(object)((Component)owner).transform)
		{
			list.Add(val);
			val = val.parent;
		}
		Transform val2 = prefab.transform;
		for (int num = list.Count - 1; num >= 0; num--)
		{
			int siblingIndex = list[num].GetSiblingIndex();
			if (siblingIndex >= val2.childCount)
			{
				return default(T);
			}
			val2 = val2.GetChild(siblingIndex);
			if (((Object)val2).name != ((Object)list[num]).name)
			{
				return default(T);
			}
		}
		return ((Component)val2).GetComponent<T>();
	}
}
internal static class NeutralTextures
{
	private sealed class Entry
	{
		internal Texture2D copy;

		internal int users;
	}

	private static readonly Dictionary<Texture, Entry> cache = new Dictionary<Texture, Entry>();

	internal static Texture Acquire(Texture original)
	{
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Expected O, but got Unknown
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: Unknown result type (might be due to invalid IL or missing references)
		//IL_0108: Unknown result type (might be due to invalid IL or missing references)
		//IL_0143: 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)
		if (!Object.op_Implicit((Object)(object)original) || !(original is Texture2D))
		{
			return original;
		}
		if (!cache.TryGetValue(original, out var value))
		{
			value = new Entry();
			cache.Add(original, value);
			RenderTexture val = null;
			RenderTexture active = RenderTexture.active;
			Texture2D val2 = null;
			try
			{
				int num = Math.Min(original.width, 1024);
				int num2 = Math.Min(original.height, 1024);
				val = RenderTexture.GetTemporary(num, num2, 0, (RenderTextureFormat)0, (RenderTextureReadWrite)2);
				Graphics.Blit(original, val);
				RenderTexture.active = val;
				val2 = new Texture2D(num, num2, (TextureFormat)4, true, false);
				val2.ReadPixels(new Rect(0f, 0f, (float)num, (float)num2), 0, 0);
				Color32[] pixels = val2.GetPixels32();
				for (int i = 0; i < pixels.Length; i++)
				{
					byte b = Math.Max(pixels[i].r, Math.Max(pixels[i].g, pixels[i].b));
					pixels[i] = new Color32(b, b, b, pixels[i].a);
				}
				val2.SetPixels32(pixels);
				val2.Apply(true, true);
				((Object)val2).name = "LightUpYourWorld neutral " + ((Object)original).name;
				((Texture)val2).wrapMode = original.wrapMode;
				((Texture)val2).filterMode = original.filterMode;
				((Texture)val2).anisoLevel = original.anisoLevel;
				value.copy = val2;
			}
			catch (Exception ex)
			{
				if (Object.op_Implicit((Object)(object)val2))
				{
					Object.Destroy((Object)(object)val2);
				}
				LightUpYourWorldPlugin.Warn("Could not neutralize light texture " + ((Object)original).name + ": " + ex.Message);
			}
			finally
			{
				RenderTexture.active = active;
				if (Object.op_Implicit((Object)(object)val))
				{
					RenderTexture.ReleaseTemporary(val);
				}
			}
		}
		value.users++;
		if (!Object.op_Implicit((Object)(object)value.copy))
		{
			return original;
		}
		return (Texture)(object)value.copy;
	}

	internal static void Release(Texture original)
	{
		if (Object.op_Implicit((Object)(object)original) && cache.TryGetValue(original, out var value) && --value.users <= 0)
		{
			if (Object.op_Implicit((Object)(object)value.copy))
			{
				Object.Destroy((Object)(object)value.copy);
			}
			cache.Remove(original);
		}
	}
}
internal sealed class ParticleColours
{
	private sealed class Setting
	{
		internal object module;

		internal object original;

		internal object mapped;

		internal PropertyInfo property;

		internal bool primary;
	}

	private sealed class CustomSetting
	{
		internal object stream;

		internal object original;

		internal object mapped;
	}

	private readonly List<Setting> settings = new List<Setting>();

	private readonly List<CustomSetting> customSettings = new List<CustomSetting>();

	private object customModule;

	private object baselineSystem;

	private MethodInfo setCustomColor;

	private MethodInfo getCustomColor;

	private object system;

	private MethodInfo getParticles;

	private MethodInfo setParticles;

	private Type particleType;

	private PropertyInfo particleColor;

	private bool tinted;

	private int mappedColour = -1;

	internal string CustomDescription { get; private set; } = "not requested";

	internal bool Available => settings.Count > 0;

	internal string CustomReadback()
	{
		List<string> list = new List<string>();
		foreach (CustomSetting customSetting in customSettings)
		{
			object obj = getCustomColor.Invoke(customModule, new object[1] { customSetting.stream });
			MethodInfo method = obj.GetType().GetMethod("Evaluate", new Type[2]
			{
				typeof(float),
				typeof(float)
			});
			list.Add(customSetting.stream?.ToString() + "=" + ((method == null) ? "unavailable" : method.Invoke(obj, new object[2] { 0.5f, 0.5f }).ToString()));
		}
		return string.Join(";", list);
	}

	private void CaptureCustomColours()
	{
		List<string> list = new List<string>();
		try
		{
			customModule = system.GetType().GetProperty("customData")?.GetValue(system, null);
			if (customModule == null)
			{
				CustomDescription = "customData unavailable";
				return;
			}
			Type type = customModule.GetType();
			object obj = baselineSystem.GetType().GetProperty("customData")?.GetValue(baselineSystem, null) ?? customModule;
			MethodInfo method = type.GetMethod("GetMode");
			getCustomColor = type.GetMethod("GetColor");
			setCustomColor = type.GetMethod("SetColor");
			if (method == null || getCustomColor == null || setCustomColor == null)
			{
				CustomDescription = "customData methods unavailable";
				return;
			}
			list.Add("enabled=" + type.GetProperty("enabled")?.GetValue(customModule, null));
			Type parameterType = method.GetParameters()[0].ParameterType;
			string[] array = new string[2] { "Custom1", "Custom2" };
			foreach (string text in array)
			{
				object obj2 = Enum.Parse(parameterType, text);
				string text2 = method.Invoke(obj, new object[1] { obj2 }).ToString();
				list.Add(text + "=" + text2);
				if (text2 == "Color")
				{
					customSettings.Add(new CustomSetting
					{
						stream = obj2,
						original = Snapshot(getCustomColor.Invoke(obj, new object[1] { obj2 }))
					});
				}
			}
			CustomDescription = string.Join(";", list);
		}
		catch (Exception ex)
		{
			CustomDescription = "capture failed: " + ex.GetBaseException().Message;
		}
	}

	internal bool TryOriginalColour(out Color colour)
	{
		//IL_00c7: 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_00cc: Unknown result type (might be due to invalid IL or missing references)
		object obj = ((customSettings.Count > 0) ? customSettings[0].original : settings.Find((Setting s) => s.primary)?.original);
		MethodInfo methodInfo = obj?.GetType().GetMethod("Evaluate", new Type[2]
		{
			typeof(float),
			typeof(float)
		});
		colour = (Color)((methodInfo == null) ? Color.white : ((Color)methodInfo.Invoke(obj, new object[2] { 0.5f, 0.5f })));
		return methodInfo != null;
	}

	internal ParticleColours(object particleSystem, bool gradientMapped = false, object nativeSystem = null)
	{
		if (particleSystem != null)
		{
			system = particleSystem;
			baselineSystem = nativeSystem ?? particleSystem;
			if (gradientMapped)
			{
				CaptureCustomColours();
			}
			Capture("main", "startColor", primary: true);
			Capture("colorOverLifetime", "color", primary: false);
			Capture("colorBySpeed", "color", primary: false);
			Capture("trails", "colorOverLifetime", primary: false);
			Capture("trails", "colorOverTrail", primary: false);
			Type type = system.GetType();
			particleType = type.GetNestedType("Particle", BindingFlags.Public);
			if (!(particleType == null))
			{
				Type type2 = particleType.MakeArrayType();
				getParticles = type.GetMethod("GetParticles", new Type[1] { type2 });
				setParticles = type.GetMethod("SetParticles", new Type[2]
				{
					type2,
					typeof(int)
				});
				particleColor = particleType.GetProperty("startColor");
			}
		}
	}

	private void Capture(string moduleName, string propertyName, bool primary)
	{
		PropertyInfo property = system.GetType().GetProperty(moduleName);
		if (!(property == null))
		{
			object value = property.GetValue(system, null);
			PropertyInfo property2 = value.GetType().GetProperty(propertyName);
			if (!(property2 == null) && property2.CanWrite)
			{
				object value2 = property.GetValue(baselineSystem, null);
				settings.Add(new Setting
				{
					module = value,
					property = property2,
					original = Snapshot(property2.GetValue(value2, null)),
					primary = primary
				});
			}
		}
	}

	internal static object Snapshot(object original)
	{
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Expected O, but got Unknown
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		object objectValue = RuntimeHelpers.GetObjectValue(original);
		string[] array = new string[3] { "gradient", "gradientMin", "gradientMax" };
		foreach (string name in array)
		{
			PropertyInfo property = objectValue.GetType().GetProperty(name);
			if (!(property == null) && property.CanWrite)
			{
				object? value = property.GetValue(original, null);
				Gradient val = (Gradient)((value is Gradient) ? value : null);
				if (val != null)
				{
					Gradient val2 = new Gradient();
					val2.SetKeys((GradientColorKey[])val.colorKeys.Clone(), (GradientAlphaKey[])val.alphaKeys.Clone());
					val2.mode = val.mode;
					property.SetValue(objectValue, val2, null);
				}
			}
		}
		return objectValue;
	}

	internal static Color Recolour(Color original, Color chosen)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: 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_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: 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)
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		float num = Mathf.Max(original.r, Mathf.Max(original.g, original.b));
		return new Color(chosen.r * num, chosen.g * num, chosen.b * num, original.a);
	}

	private static Gradient RecolourGradient(Gradient original, Color chosen)
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Expected O, but got Unknown
		//IL_002e: 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_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		if (original == null)
		{
			return null;
		}
		Gradient val = new Gradient();
		GradientColorKey[] array = (GradientColorKey[])original.colorKeys.Clone();
		for (int i = 0; i < array.Length; i++)
		{
			array[i].color = Recolour(array[i].color, chosen);
		}
		val.SetKeys(array, (GradientAlphaKey[])original.alphaKeys.Clone());
		val.mode = original.mode;
		return val;
	}

	internal static object Map(object original, Color chosen)
	{
		//IL_005f: 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_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: 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_00db: Expected O, but got Unknown
		object objectValue = RuntimeHelpers.GetObjectValue(original);
		Type type = objectValue.GetType();
		string[] array = new string[3] { "color", "colorMin", "colorMax" };
		foreach (string name in array)
		{
			PropertyInfo property = type.GetProperty(name);
			if (property != null && property.CanWrite)
			{
				property.SetValue(objectValue, Recolour((Color)property.GetValue(original, null), chosen), null);
			}
		}
		array = new string[3] { "gradient", "gradientMin", "gradientMax" };
		foreach (string name2 in array)
		{
			PropertyInfo property2 = type.GetProperty(name2);
			if (property2 != null && property2.CanWrite)
			{
				property2.SetValue(objectValue, RecolourGradient((Gradient)property2.GetValue(original, null), chosen), null);
			}
		}
		return objectValue;
	}

	internal void Apply(int packed, bool recolourLive = true)
	{
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_0355: Unknown result type (might be due to invalid IL or missing references)
		//IL_035a: Unknown result type (might be due to invalid IL or missing references)
		//IL_03c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_03c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_03be: Unknown result type (might be due to invalid IL or missing references)
		//IL_03cc: Unknown result type (might be due to invalid IL or missing references)
		//IL_03e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_03d9: Unknown result type (might be due to invalid IL or missing references)
		//IL_03db: Unknown result type (might be due to invalid IL or missing references)
		//IL_03dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_03e5: Unknown result type (might be due to invalid IL or missing references)
		if (system == null || (packed == 0 && !tinted))
		{
			return;
		}
		if (mappedColour != packed)
		{
			foreach (CustomSetting customSetting in customSettings)
			{
				customSetting.mapped = ((packed == 0) ? Snapshot(customSetting.original) : Map(customSetting.original, WorldLight.Unpack(packed)));
			}
			foreach (Setting setting2 in settings)
			{
				setting2.mapped = ((packed == 0) ? Snapshot(setting2.original) : Map(setting2.original, (setting2.primary && customSettings.Count == 0) ? WorldLight.Unpack(packed) : Color.white));
			}
			mappedColour = packed;
		}
		foreach (CustomSetting customSetting2 in customSettings)
		{
			setCustomColor.Invoke(customModule, new object[2] { customSetting2.stream, customSetting2.mapped });
		}
		foreach (Setting setting3 in settings)
		{
			setting3.property.SetValue(setting3.module, setting3.mapped, null);
		}
		tinted = packed != 0;
		if (packed == 0)
		{
			MethodInfo method = system.GetType().GetMethod("Clear", new Type[1] { typeof(bool) });
			if (method != null)
			{
				method.Invoke(system, new object[1] { false });
				return;
			}
		}
		if (!recolourLive || getParticles == null || setParticles == null || particleColor == null)
		{
			return;
		}
		PropertyInfo property = system.GetType().GetProperty("particleCount");
		int num = ((property != null) ? ((int)property.GetValue(system, null)) : 0);
		if (num > 0)
		{
			Array array = Array.CreateInstance(particleType, num);
			num = (int)getParticles.Invoke(system, new object[1] { array });
			Color chosen = ((packed == 0 || customSettings.Count > 0) ? Color.white : WorldLight.Unpack(packed));
			Setting setting = settings.Find((Setting s) => s.primary);
			MethodInfo methodInfo = setting?.original.GetType().GetMethod("Evaluate", new Type[2]
			{
				typeof(float),
				typeof(float)
			});
			PropertyInfo property2 = particleType.GetProperty("randomSeed");
			for (int num2 = 0; num2 < num; num2++)
			{
				object value = array.GetValue(num2);
				Color32 val = (Color32)particleColor.GetValue(value, null);
				float num3 = ((property2 == null) ? 0.5f : ((float)((uint)property2.GetValue(value, null) % 65536) / 65535f));
				Color val2 = (Color)((methodInfo == null) ? Color32.op_Implicit(val) : ((Color)methodInfo.Invoke(setting.original, new object[2] { num3, num3 })));
				particleColor.SetValue(value, Color32.op_Implicit((packed == 0) ? val2 : Recolour(val2, chosen)), null);
				array.SetValue(value, num2);
			}
			setParticles.Invoke(system, new object[2] { array, num });
		}
	}
}
internal static class PermanentFire
{
	internal const string FuelKey = "LightUpYourWorld.PermanentFuel";

	internal static bool Fueled(Fireplace fire)
	{
		if (!Object.op_Implicit((Object)(object)fire) || !Object.op_Implicit((Object)(object)((Component)fire).GetComponent<Piece>()) || !Object.op_Implicit((Object)(object)fire.m_fuelItem))
		{
			return false;
		}
		ZNetView component = ((Component)fire).GetComponent<ZNetView>();
		if (!Object.op_Implicit((Object)(object)component) || !component.IsValid())
		{
			return false;
		}
		ZDO zDO = component.GetZDO();
		if (!(zDO.GetFloat(ZDOVars.s_fuel, 0f) > 0f))
		{
			return zDO.GetFloat("LightUpYourWorld.PermanentFuel", 0f) > 0f;
		}
		return true;
	}
}
[HarmonyPatch(typeof(Fireplace), "UpdateFireplace")]
internal static class PreserveFireFuel
{
	private static void Prefix(Fireplace __instance, out bool __state)
	{
		__state = __instance.m_infiniteFuel;
		if (!PermanentFire.Fueled(__instance))
		{
			return;
		}
		__instance.m_infiniteFuel = true;
		ZNetView component = ((Component)__instance).GetComponent<ZNetView>();
		if (component.IsOwner())
		{
			ZDO zDO = component.GetZDO();
			float num = zDO.GetFloat("LightUpYourWorld.PermanentFuel", 0f);
			float num2 = zDO.GetFloat(ZDOVars.s_fuel, 0f);
			if (num <= 0f && num2 > 0f)
			{
				zDO.Set("LightUpYourWorld.PermanentFuel", num2);
			}
			else if (num > 0f && num2 <= 0f)
			{
				zDO.Set(ZDOVars.s_fuel, num);
			}
		}
	}

	private static void Finalizer(Fireplace __instance, bool __state)
	{
		__instance.m_infiniteFuel = __state;
	}
}
[HarmonyPatch(typeof(Fireplace), "IsBurning")]
internal static class KeepFueledFireBurning
{
	private static void Prefix(Fireplace __instance, out bool __state)
	{
		__state = __instance.m_infiniteFuel;
		if (PermanentFire.Fueled(__instance))
		{
			__instance.m_infiniteFuel = true;
		}
	}

	private static void Finalizer(Fireplace __instance, bool __state)
	{
		__instance.m_infiniteFuel = __state;
	}
}
internal static class LightColour
{
	internal const string SaveKey = "LightUpYourWorld.Colour";

	internal static int Pack(byte r, byte g, byte b)
	{
		return 0x1000000 | (r << 16) | (g << 8) | b;
	}

	internal static bool IsValid(int value)
	{
		if (value != 0)
		{
			return (value & -16777216) == 16777216;
		}
		return true;
	}

	internal static int Normalize(int value)
	{
		if (!IsValid(value))
		{
			return 0;
		}
		return value;
	}

	internal static byte Red(int value)
	{
		return (byte)(value >> 16);
	}

	internal static byte Green(int value)
	{
		return (byte)(value >> 8);
	}

	internal static byte Blue(int value)
	{
		return (byte)value;
	}
}
internal sealed class ColourSession
{
	internal int Saved { get; private set; }

	internal int Selected { get; private set; }

	internal int SavedEffect { get; private set; }

	internal int SelectedEffect { get; private set; }

	internal bool Dirty
	{
		get
		{
			if (Selected == Saved)
			{
				return SelectedEffect != SavedEffect;
			}
			return true;
		}
	}

	internal void Open(int saved, int effect = 0)
	{
		int saved2 = (Selected = LightColour.Normalize(saved));
		Saved = saved2;
		saved2 = (SelectedEffect = LightAnimation.Normalize(effect));
		SavedEffect = saved2;
	}

	internal void Preview(int value)
	{
		Selected = LightColour.Normalize(value);
	}

	internal void PreviewEffect(int effect)
	{
		SelectedEffect = LightAnimation.Normalize(effect);
	}

	internal void Applied()
	{
		Saved = Selected;
		SavedEffect = SelectedEffect;
	}

	internal void Cancel(int latestSaved, int effect = 0)
	{
		Open(latestSaved, effect);
	}
}
internal sealed class ColourChannels
{
	internal readonly ColourSession Light = new ColourSession();

	internal readonly ColourSession Source = new ColourSession();

	internal bool SourceSelected { get; private set; }

	internal ColourSession Current
	{
		get
		{
			if (!SourceSelected)
			{
				return Light;
			}
			return Source;
		}
	}

	internal bool AnyDirty
	{
		get
		{
			if (!Light.Dirty)
			{
				return Source.Dirty;
			}
			return true;
		}
	}

	internal void Open(int light, int lightEffect, int source, int sourceEffect)
	{
		Light.Open(light, lightEffect);
		Source.Open(source, sourceEffect);
		SourceSelected = false;
	}

	internal void Select(bool source)
	{
		SourceSelected = source;
	}

	internal bool Refresh(int light, int lightEffect, int source, int sourceEffect)
	{
		bool flag = Refresh(Light, light, lightEffect);
		return Refresh(Source, source, sourceEffect) || flag;
	}

	private static bool Refresh(ColourSession session, int colour, int effect)
	{
		if (session.Dirty || (session.Saved == colour && session.SavedEffect == effect))
		{
			return false;
		}
		session.Open(colour, effect);
		return true;
	}

	internal static bool RequestedSource(bool current, bool left, bool right, bool toggle)
	{
		if (!toggle)
		{
			if (left == right)
			{
				return current;
			}
			return right;
		}
		return !current;
	}
}
internal static class LightAnimation
{
	internal static double Clock
	{
		get
		{
			if (!Object.op_Implicit((Object)(object)ZNet.instance))
			{
				return Time.unscaledTime;
			}
			return ZNet.instance.GetTimeSeconds();
		}
	}

	internal static int Normalize(int mode)
	{
		if (mode < 0 || mode > 2)
		{
			return 0;
		}
		return mode;
	}

	internal static string Name(int mode)
	{
		return mode switch
		{
			2 => "Colour cycle", 
			1 => "Gentle pulse", 
			_ => "Steady", 
		};
	}

	internal static int Colour(int packed, int mode, double seconds)
	{
		//IL_000e: 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_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: 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_0077: 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)
		if (packed == 0 || Normalize(mode) == 0)
		{
			return packed;
		}
		Color val = WorldLight.Unpack(packed);
		seconds = Math.Floor(seconds * 8.0) / 8.0;
		if (mode == 1)
		{
			float num = (float)(0.825 + 0.175 * Math.Cos(seconds * Math.PI / 2.0));
			return WorldLight.Pack(new Color(val.r * num, val.g * num, val.b * num));
		}
		float num2 = default(float);
		float val2 = default(float);
		float num3 = default(float);
		Color.RGBToHSV(val, ref num2, ref val2, ref num3);
		return WorldLight.Pack(Color.HSVToRGB((float)(((double)num2 + seconds / 24.0) % 1.0), Math.Max(val2, 0.65f), num3));
	}
}
internal static class MenuLayout
{
	internal static float Scale(float width, float height)
	{
		return Math.Min(height / 1100f, width / 1900f);
	}
}
internal static class LightFavourites
{
	internal const int Slots = 10;

	private const string Prefix = "LightUpYourWorld.Favourite.";

	private static readonly FieldInfo CustomData = AccessTools.Field(typeof(Player), "m_customData");

	private static Dictionary<string, string> Data(Player player)
	{
		if (!Object.op_Implicit((Object)(object)player) || !(CustomData != null))
		{
			return null;
		}
		return CustomData.GetValue(player) as Dictionary<string, string>;
	}

	internal static bool TryGet(Player player, int slot, out Color colour)
	{
		//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)
		//IL_0094: 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)
		colour = Color.white;
		if (slot < 0 || slot >= 10)
		{
			return false;
		}
		Dictionary<string, string> dictionary = Data(player);
		if (dictionary == null || !dictionary.TryGetValue("LightUpYourWorld.Favourite." + slot, out var value) || value == null || value.Length != 6)
		{
			return false;
		}
		if (!int.TryParse(value, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var result))
		{
			return false;
		}
		colour = new Color((float)((result >> 16) & 0xFF) / 255f, (float)((result >> 8) & 0xFF) / 255f, (float)(result & 0xFF) / 255f, 1f);
		return true;
	}

	internal static void Set(Player player, int slot, Color colour)
	{
		//IL_0015: Unknown result type (might be due to invalid IL or missing references)
		//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)
		if (slot >= 0 && slot < 10)
		{
			Dictionary<string, string> dictionary = Data(player);
			if (dictionary != null)
			{
				Color32 val = Color32.op_Implicit(colour);
				dictionary["LightUpYourWorld.Favourite." + slot] = val.r.ToString("X2") + val.g.ToString("X2") + val.b.ToString("X2");
			}
		}
	}

	internal static void Remove(Player player, int slot)
	{
		Dictionary<string, string> dictionary = Data(player);
		if (slot >= 0 && slot < 10)
		{
			dictionary?.Remove("LightUpYourWorld.Favourite." + slot);
		}
	}

	internal static int FirstEmpty(Player player)
	{
		for (int i = 0; i < 10; i++)
		{
			if (!TryGet(player, i, out var _))
			{
				return i;
			}
		}
		return 0;
	}
}
[HarmonyPatch(typeof(Hud), "UpdateCrosshair")]
internal static class NativeLightPrompt
{
	private static WorldLight lastLight;

	private static string lastNative;

	private static string lastPrompt;

	private static string combined;

	private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
	{
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00aa: Expected O, but got Unknown
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d3: Expected O, but got Unknown
		List<CodeInstruction> list = new List<CodeInstruction>(instructions);
		int num = -1;
		int num2 = 0;
		for (int i = 0; i < list.Count; i++)
		{
			if (list[i].operand is MethodInfo { Name: "GetHoverText" } methodInfo && methodInfo.DeclaringType == typeof(Hoverable) && methodInfo.ReturnType == typeof(string) && methodInfo.GetParameters().Length == 0)
			{
				num = i;
				num2++;
			}
		}
		if (num2 != 1)
		{
			Debug.LogWarning((object)"[LightUpYourWorld] Unsupported hover method layout: prompt extension skipped. Colour-menu hotkey remains available.");
			return list;
		}
		list.Insert(num + 1, new CodeInstruction(OpCodes.Ldarg_1, (object)null));
		list.Insert(num + 2, new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(NativeLightPrompt), "Append", (Type[])null, (Type[])null)));
		return list;
	}

	private static string Append(string original, Player player)
	{
		LightUpYourWorldPlugin instance = LightUpYourWorldPlugin.Instance;
		if (string.IsNullOrEmpty(original) || !Object.op_Implicit((Object)(object)instance) || instance.IsOpen || !Object.op_Implicit((Object)(object)player) || (Object)(object)player != (Object)(object)Player.m_localPlayer || ((Character)player).InPlaceMode() || !LightUpYourWorldPlugin.CanTakeInput(player))
		{
			return original;
		}
		WorldLight worldLight = WorldLight.Find(((Humanoid)player).GetHoverObject());
		if (!Object.op_Implicit((Object)(object)worldLight))
		{
			return original;
		}
		string hoverPrompt = instance.HoverPrompt;
		if ((Object)(object)worldLight == (Object)(object)lastLight && original == lastNative && hoverPrompt == lastPrompt)
		{
			return combined;
		}
		lastLight = worldLight;
		lastNative = original;
		lastPrompt = hoverPrompt;
		combined = (original.Contains(hoverPrompt) ? original : (original.TrimEnd() + "\n" + hoverPrompt));
		return combined;
	}
}