Decompiled source of Strazers PlayerHEX v1.0.1

PlayerHEX.dll

Decompiled 2 months ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Mirror;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInPlugin("strazer.playerhex", "Strazer's Player HEX", "1.0.3")]
public class PlayerHEXPlugin : BaseUnityPlugin
{
	public const string GUID = "strazer.playerhex";

	public const string NAME = "Strazer's Player HEX";

	public const string VERSION = "1.0.3";

	public static ConfigEntry<bool> Enabled;

	public static ConfigEntry<string> PlayerColourHex;

	public static ConfigEntry<string> WindowToggleKey;

	public static ConfigEntry<string> AutoSyncKey;

	public static ConfigEntry<bool> AutoSyncOnPlayerJoin;

	private void Awake()
	{
		//IL_00be: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c4: Expected O, but got Unknown
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d6: Expected O, but got Unknown
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Enable or disable the mod.");
		PlayerColourHex = ((BaseUnityPlugin)this).Config.Bind<string>("General", "PlayerColourHex", "#FFFFFF", "HEX color for the player's forklift and character. Example: #FFFFFF, #FF0000.");
		WindowToggleKey = ((BaseUnityPlugin)this).Config.Bind<string>("General", "WindowToggleKey", "F8", "Key to toggle the colour picker window. Valid: any KeyCode name.");
		AutoSyncKey = ((BaseUnityPlugin)this).Config.Bind<string>("General", "AutoSyncKey", "F7", "Key to manually resync your saved HEX colour to all players. Valid: any KeyCode name.");
		AutoSyncOnPlayerJoin = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AutoSyncOnPlayerJoin", true, "Automatically resync colours when a player joins.");
		PlayerColorManager_UpdateRenderers_Patch.Initialize();
		RegisterNetworkSerializers();
		Harmony val = new Harmony("strazer.playerhex");
		val.PatchAll();
		GameObject val2 = new GameObject("PlayerHEX_UI");
		Object.DontDestroyOnLoad((Object)(object)val2);
		((Object)val2).hideFlags = (HideFlags)61;
		val2.AddComponent<ColorPickerUI>();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Strazer's Player HEX loaded");
	}

	private static void RegisterNetworkSerializers()
	{
		Type typeFromHandle = typeof(Writer<NetMsgCustomPlayerColor>);
		Type typeFromHandle2 = typeof(Reader<NetMsgCustomPlayerColor>);
		Type typeFromHandle3 = typeof(Writer<NetMsgResyncRequest>);
		Type typeFromHandle4 = typeof(Reader<NetMsgResyncRequest>);
		FieldInfo field = typeFromHandle.GetField("write", BindingFlags.Static | BindingFlags.Public);
		FieldInfo field2 = typeFromHandle2.GetField("read", BindingFlags.Static | BindingFlags.Public);
		FieldInfo field3 = typeFromHandle3.GetField("write", BindingFlags.Static | BindingFlags.Public);
		FieldInfo field4 = typeFromHandle4.GetField("read", BindingFlags.Static | BindingFlags.Public);
		field.SetValue(null, new Action<NetworkWriter, NetMsgCustomPlayerColor>(NetMsgCustomPlayerColorSerializer.WriteNetMsgCustomPlayerColor));
		field2.SetValue(null, new Func<NetworkReader, NetMsgCustomPlayerColor>(NetMsgCustomPlayerColorSerializer.ReadNetMsgCustomPlayerColor));
		field3.SetValue(null, new Action<NetworkWriter, NetMsgResyncRequest>(NetMsgCustomPlayerColorSerializer.WriteNetMsgResyncRequest));
		field4.SetValue(null, new Func<NetworkReader, NetMsgResyncRequest>(NetMsgCustomPlayerColorSerializer.ReadNetMsgResyncRequest));
		Debug.Log((object)"[PlayerHEX] Network message serializers registered");
	}
}
[HarmonyPatch(typeof(PlayerColorManager), "UpdateRenderers")]
public static class PlayerColorManager_UpdateRenderers_Patch
{
	private static FieldInfo _vehicleRendersField;

	private static FieldInfo _playerRendersField;

	private static FieldInfo _networkField;

	private static readonly int MainColorId = Shader.PropertyToID("_MainColor");

	private static bool _initialized;

	private static Dictionary<uint, int> _traceCallCount = new Dictionary<uint, int>();

	private static Dictionary<uint, bool> _traceLastFound = new Dictionary<uint, bool>();

	public static void Initialize()
	{
		if (!_initialized)
		{
			_vehicleRendersField = AccessTools.Field(typeof(PlayerColorManager), "vehicleRendersToUpdate");
			_playerRendersField = AccessTools.Field(typeof(PlayerColorManager), "playerRendersToUpdate");
			_networkField = AccessTools.Field(typeof(PlayerColorManager), "playerColorManagerNetwork");
			_initialized = true;
		}
	}

	[HarmonyPostfix]
	public static void Postfix(PlayerColorManager __instance)
	{
		//IL_0059: 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_0272: Unknown result type (might be due to invalid IL or missing references)
		if (!PlayerHEXPlugin.Enabled.Value)
		{
			return;
		}
		object? value = _networkField.GetValue(__instance);
		PlayerColorManagerNetwork val = (PlayerColorManagerNetwork)((value is PlayerColorManagerNetwork) ? value : null);
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		uint netId = ((NetworkBehaviour)val).netId;
		if (CustomColorManager.TryGetColor(netId, out var color))
		{
			OverrideRenderers(__instance, Color32.op_Implicit(color));
			_traceCallCount.TryGetValue(netId, out var value2);
			_traceLastFound.TryGetValue(netId, out var value3);
			if (value2 < 5 || !value3)
			{
				List<Renderer> list = _vehicleRendersField.GetValue(__instance) as List<Renderer>;
				List<Renderer> list2 = _playerRendersField.GetValue(__instance) as List<Renderer>;
				object[] array = new object[12]
				{
					"[PlayerHEX TRACE] RENDER FOUND netId=", netId, " colour=#", null, null, null, null, null, null, null,
					null, null
				};
				object[] array2 = array;
				byte r = color.r;
				array2[3] = r.ToString("X2");
				object[] array3 = array;
				r = color.g;
				array3[4] = r.ToString("X2");
				object[] array4 = array;
				r = color.b;
				array4[5] = r.ToString("X2");
				array[6] = " playerRenders=";
				array[7] = list2?.Count ?? 0;
				array[8] = " vehicleRenders=";
				array[9] = list?.Count ?? 0;
				array[10] = " callCount=";
				array[11] = value2;
				Debug.Log((object)string.Concat(array));
			}
			_traceCallCount[netId] = value2 + 1;
			_traceLastFound[netId] = true;
		}
		else
		{
			_traceCallCount.TryGetValue(netId, out var value4);
			if (value4 < 5 && ((NetworkBehaviour)val).isLocalPlayer)
			{
				Debug.Log((object)("[PlayerHEX TRACE] RENDER LOCAL netId=" + netId + " callCount=" + value4));
			}
			if (value4 < 5)
			{
				_traceCallCount[netId] = value4 + 1;
			}
			_traceLastFound[netId] = false;
			if (((NetworkBehaviour)val).isLocalPlayer && HexColorUtil.TryParse(PlayerHEXPlugin.PlayerColourHex.Value, out var color2))
			{
				OverrideRenderers(__instance, color2);
			}
		}
	}

	private static void OverrideRenderers(PlayerColorManager instance, Color color)
	{
		//IL_004f: 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)
		if (_vehicleRendersField.GetValue(instance) is List<Renderer> { Count: >0 } list)
		{
			for (int i = 0; i < list.Count; i++)
			{
				Renderer val = list[i];
				if ((Object)(object)val != (Object)null)
				{
					MaterialUtil.SetPropertyBlockColor(val, MainColorId, color);
				}
			}
		}
		if (!(_playerRendersField.GetValue(instance) is List<Renderer> { Count: >0 } list2))
		{
			return;
		}
		for (int i = 0; i < list2.Count; i++)
		{
			Renderer val = list2[i];
			if ((Object)(object)val != (Object)null)
			{
				MaterialUtil.SetPropertyBlockColor(val, MainColorId, color);
			}
		}
	}
}
public static class HexColorUtil
{
	public static bool TryParse(string hex, out Color color)
	{
		//IL_0002: 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)
		color = Color.white;
		if (string.IsNullOrEmpty(hex))
		{
			return false;
		}
		hex = hex.Trim();
		if (!hex.StartsWith("#"))
		{
			hex = "#" + hex;
		}
		return ColorUtility.TryParseHtmlString(hex, ref color);
	}

	public static string ToHex(Color color)
	{
		int num = Mathf.RoundToInt(Mathf.Clamp01(color.r) * 255f);
		int num2 = Mathf.RoundToInt(Mathf.Clamp01(color.g) * 255f);
		int num3 = Mathf.RoundToInt(Mathf.Clamp01(color.b) * 255f);
		return "#" + num.ToString("X2") + num2.ToString("X2") + num3.ToString("X2");
	}

	public static void ColorToHSV(Color color, out float h, out float s, out float v)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		Color.RGBToHSV(color, ref h, ref s, ref v);
	}

	public static Color HSVToColor(float h, float s, float v)
	{
		//IL_0004: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: 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 Color.HSVToRGB(h, s, v);
	}
}
public class ColorPickerUI : MonoBehaviour
{
	private const int WindowId = 1346913624;

	private const float Deg2Rad = (float)Math.PI / 180f;

	private const float Rad2Deg = 57.29578f;

	private bool _showWindow;

	private Rect _windowRect = new Rect(20f, 20f, 300f, 520f);

	private Texture2D _colorWheelTex;

	private bool _wheelDragging;

	private float _hue = 0f;

	private float _saturation = 0f;

	private float _value = 1f;

	private int _red = 255;

	private int _green = 255;

	private int _blue = 255;

	private string _hexText = "FFFFFF";

	private Color _previewColor = Color.white;

	private string _hexFieldText = "FFFFFF";

	private KeyCode _toggleKey = (KeyCode)289;

	private KeyCode _resyncKey = (KeyCode)288;

	private void Awake()
	{
		_colorWheelTex = GenerateColorWheel(256);
		LoadFromConfig();
		_hexFieldText = _hexText;
		ParseToggleKey();
	}

	private void OnDestroy()
	{
		if ((Object)(object)_colorWheelTex != (Object)null)
		{
			Object.Destroy((Object)(object)_colorWheelTex);
		}
	}

	private void ParseToggleKey()
	{
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		string value = PlayerHEXPlugin.WindowToggleKey.Value;
		if (!Enum.TryParse<KeyCode>(value, ignoreCase: true, out _toggleKey))
		{
			_toggleKey = (KeyCode)289;
		}
		string value2 = PlayerHEXPlugin.AutoSyncKey.Value;
		if (!Enum.TryParse<KeyCode>(value2, ignoreCase: true, out _resyncKey))
		{
			_resyncKey = (KeyCode)288;
		}
	}

	private void LoadFromConfig()
	{
		//IL_0028: 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_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: 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_00da: Unknown result type (might be due to invalid IL or missing references)
		string value = PlayerHEXPlugin.PlayerColourHex.Value;
		if (HexColorUtil.TryParse(value, out var color))
		{
			_previewColor = color;
		}
		else
		{
			_previewColor = Color.white;
		}
		_hue = 0f;
		_saturation = 0f;
		_value = 1f;
		Color.RGBToHSV(_previewColor, ref _hue, ref _saturation, ref _value);
		_hue *= 360f;
		_red = Mathf.RoundToInt(_previewColor.r * 255f);
		_green = Mathf.RoundToInt(_previewColor.g * 255f);
		_blue = Mathf.RoundToInt(_previewColor.b * 255f);
		_hexText = HexColorUtil.ToHex(_previewColor).TrimStart(new char[1] { '#' });
	}

	private void OnGUI()
	{
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Invalid comparison between Unknown and I4
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: 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_008e: Invalid comparison between Unknown and I4
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		//IL_011c: Expected O, but got Unknown
		//IL_0117: Unknown result type (might be due to invalid IL or missing references)
		//IL_011c: Unknown result type (might be due to invalid IL or missing references)
		if (!GameUtil.isLobby && (GameUtil.isRun || GameUtil.isGym || GameUtil.isTutorial))
		{
			_showWindow = false;
			return;
		}
		Event current = Event.current;
		if ((int)current.type == 4 && current.keyCode == _resyncKey && !current.control && !current.shift && !current.alt)
		{
			CustomColorManager.SendMySavedColor();
			current.Use();
			return;
		}
		if ((int)current.type == 4 && current.keyCode == _toggleKey && !current.control && !current.shift && !current.alt)
		{
			_showWindow = !_showWindow;
			if (_showWindow)
			{
				LoadFromConfig();
			}
			current.Use();
		}
		if (_showWindow)
		{
			_windowRect = GUILayout.Window(1346913624, _windowRect, new WindowFunction(DrawWindow), "Strazer's Player HEX", (GUILayoutOption[])(object)new GUILayoutOption[0]);
		}
	}

	private void DrawWindow(int id)
	{
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		GUILayout.Space(5f);
		DrawColorWheel();
		DrawValueSlider();
		DrawRGBSliders();
		DrawHexField();
		DrawButtons();
		GUILayout.Space(5f);
		GUI.DragWindow(new Rect(0f, 0f, ((Rect)(ref _windowRect)).width, 25f));
	}

	private void DrawColorWheel()
	{
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_010e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0246: Unknown result type (might be due to invalid IL or missing references)
		//IL_024c: Invalid comparison between Unknown and I4
		//IL_0127: Unknown result type (might be due to invalid IL or missing references)
		//IL_0162: Unknown result type (might be due to invalid IL or missing references)
		//IL_0158: Unknown result type (might be due to invalid IL or missing references)
		//IL_015e: Invalid comparison between Unknown and I4
		//IL_018a: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0222: Unknown result type (might be due to invalid IL or missing references)
		//IL_0227: Unknown result type (might be due to invalid IL or missing references)
		GUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[0]);
		GUILayout.FlexibleSpace();
		Rect rect = GUILayoutUtility.GetRect(200f, 200f, (GUILayoutOption[])(object)new GUILayoutOption[2]
		{
			GUILayout.MaxWidth(200f),
			GUILayout.MaxHeight(200f)
		});
		GUI.DrawTexture(rect, (Texture)(object)_colorWheelTex);
		float num = ((Rect)(ref rect)).x + ((Rect)(ref rect)).width * 0.5f;
		float num2 = ((Rect)(ref rect)).y + ((Rect)(ref rect)).height * 0.5f;
		float num3 = num + Mathf.Cos(_hue * ((float)Math.PI / 180f)) * (_saturation * ((Rect)(ref rect)).width * 0.45f);
		float num4 = num2 - Mathf.Sin(_hue * ((float)Math.PI / 180f)) * (_saturation * ((Rect)(ref rect)).height * 0.45f);
		Rect val = default(Rect);
		((Rect)(ref val))..ctor(num3 - 4f, num4 - 4f, 8f, 8f);
		GUI.Box(val, GUIContent.none);
		Event current = Event.current;
		if (((Rect)(ref rect)).Contains(current.mousePosition))
		{
			if ((int)current.type == 0 && current.button == 0)
			{
				_wheelDragging = true;
			}
			if ((_wheelDragging && (int)current.type == 3) || ((int)current.type == 0 && current.button == 0))
			{
				float num5 = (current.mousePosition.x - num) / (((Rect)(ref rect)).width * 0.45f);
				float num6 = (current.mousePosition.y - num2) / (((Rect)(ref rect)).height * 0.45f);
				float saturation = Mathf.Clamp01(Mathf.Sqrt(num5 * num5 + num6 * num6));
				float hue = (Mathf.Atan2(0f - num6, num5) * 57.29578f + 360f) % 360f;
				_hue = hue;
				_saturation = saturation;
				_previewColor = Color.HSVToRGB(_hue / 360f, _saturation, _value);
				SyncRGBNumbersFromColor();
				SyncHexFromColor();
				current.Use();
			}
		}
		if ((int)current.type == 1 && current.button == 0)
		{
			_wheelDragging = false;
		}
		GUILayout.FlexibleSpace();
		GUILayout.EndHorizontal();
	}

	private void DrawValueSlider()
	{
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		GUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[0]);
		GUILayout.Label("V:", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(20f) });
		float num = GUILayout.HorizontalSlider(_value, 0f, 1f, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(true) });
		if (!Mathf.Approximately(num, _value))
		{
			_value = num;
			_previewColor = Color.HSVToRGB(_hue / 360f, _saturation, _value);
			SyncRGBNumbersFromColor();
			SyncHexFromColor();
		}
		GUILayout.Label(_value.ToString("F2"), (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(35f) });
		GUILayout.EndHorizontal();
	}

	private void DrawRGBSliders()
	{
		DrawSingleRGBSlider("R:", ref _red);
		DrawSingleRGBSlider("G:", ref _green);
		DrawSingleRGBSlider("B:", ref _blue);
	}

	private void DrawSingleRGBSlider(string label, ref int value)
	{
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		GUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[0]);
		GUILayout.Label(label, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(20f) });
		float num = value;
		float num2 = GUILayout.HorizontalSlider(num, 0f, 255f, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.ExpandWidth(true) });
		int num3 = Mathf.RoundToInt(num2);
		if (num3 != value)
		{
			value = num3;
			_previewColor = new Color((float)_red / 255f, (float)_green / 255f, (float)_blue / 255f);
			SyncHSVFromColor();
			SyncHexFromColor();
		}
		GUILayout.Label(value.ToString(), (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(30f) });
		GUILayout.EndHorizontal();
	}

	private void DrawHexField()
	{
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f4: Invalid comparison between Unknown and I4
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0174: Unknown result type (might be due to invalid IL or missing references)
		//IL_0175: Unknown result type (might be due to invalid IL or missing references)
		//IL_017a: Unknown result type (might be due to invalid IL or missing references)
		//IL_017d: 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_0194: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: Unknown result type (might be due to invalid IL or missing references)
		GUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[0]);
		GUILayout.Label("HEX:", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(35f) });
		GUI.SetNextControlName("HexInput");
		string text = GUILayout.TextField(_hexFieldText, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(80f) });
		if (text != _hexFieldText)
		{
			_hexFieldText = text;
			if (HexColorUtil.TryParse(_hexFieldText, out var color))
			{
				_previewColor = color;
				SyncRGBNumbersFromColor();
				SyncHSVFromColor();
				_hexText = HexColorUtil.ToHex(_previewColor).TrimStart(new char[1] { '#' });
				_hexFieldText = _hexText;
			}
		}
		if (Event.current.isKey && (int)Event.current.keyCode == 13 && GUI.GetNameOfFocusedControl() == "HexInput" && HexColorUtil.TryParse(_hexFieldText, out var color2))
		{
			_hexFieldText = HexColorUtil.ToHex(color2).TrimStart(new char[1] { '#' });
		}
		Rect rect = GUILayoutUtility.GetRect(40f, 20f, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(40f) });
		Color backgroundColor = GUI.backgroundColor;
		GUI.backgroundColor = _previewColor;
		GUI.Box(rect, GUIContent.none);
		GUI.backgroundColor = backgroundColor;
		GUILayout.EndHorizontal();
	}

	private void DrawButtons()
	{
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_015a: Unknown result type (might be due to invalid IL or missing references)
		//IL_015b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0161: Unknown result type (might be due to invalid IL or missing references)
		GUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[0]);
		if (GUILayout.Button("Apply", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(25f) }))
		{
			string text = HexColorUtil.ToHex(_previewColor);
			PlayerHEXPlugin.PlayerColourHex.Value = text;
			_hexText = text.TrimStart(new char[1] { '#' });
			_hexFieldText = _hexText;
			CustomColorManager.SendMySavedColor();
		}
		if (GUILayout.Button("Reset", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(25f) }))
		{
			LoadFromConfig();
			_hexFieldText = _hexText;
		}
		GUILayout.EndHorizontal();
		GUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[0]);
		if (GUILayout.Button("Copy HEX", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(25f) }))
		{
			GUIUtility.systemCopyBuffer = "#" + _hexText;
		}
		if (GUILayout.Button("Paste HEX", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(25f) }))
		{
			string systemCopyBuffer = GUIUtility.systemCopyBuffer;
			if (!string.IsNullOrEmpty(systemCopyBuffer) && HexColorUtil.TryParse(systemCopyBuffer, out var color))
			{
				_previewColor = color;
				_hexText = HexColorUtil.ToHex(color).TrimStart(new char[1] { '#' });
				_hexFieldText = _hexText;
				SyncRGBNumbersFromColor();
				SyncHSVFromColor();
				GUI.FocusControl("HexInput");
			}
		}
		GUILayout.EndHorizontal();
		GUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[0]);
		if (GUILayout.Button("Use Default", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(25f) }))
		{
			CustomColorManager.SendClearColor();
		}
		GUILayout.EndHorizontal();
	}

	private void SyncRGBNumbersFromColor()
	{
		_red = Mathf.RoundToInt(_previewColor.r * 255f);
		_green = Mathf.RoundToInt(_previewColor.g * 255f);
		_blue = Mathf.RoundToInt(_previewColor.b * 255f);
	}

	private void SyncHSVFromColor()
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		float num = default(float);
		float saturation = default(float);
		float value = default(float);
		Color.RGBToHSV(_previewColor, ref num, ref saturation, ref value);
		_hue = num * 360f;
		_saturation = saturation;
		_value = value;
	}

	private void SyncHexFromColor()
	{
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		_hexText = HexColorUtil.ToHex(_previewColor).TrimStart(new char[1] { '#' });
		_hexFieldText = _hexText;
	}

	private static Texture2D GenerateColorWheel(int size)
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Expected O, but got Unknown
		//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00da: 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_00ad: Unknown result type (might be due to invalid IL or missing references)
		Texture2D val = new Texture2D(size, size, (TextureFormat)3, false);
		((Texture)val).wrapMode = (TextureWrapMode)1;
		float num = (float)size * 0.5f;
		float num2 = num;
		Color[] array = (Color[])(object)new Color[size * size];
		for (int i = 0; i < size; i++)
		{
			for (int j = 0; j < size; j++)
			{
				float num3 = (float)j - num;
				float num4 = (float)i - num;
				float num5 = Mathf.Sqrt(num3 * num3 + num4 * num4);
				float num6 = Mathf.Clamp01(num5 / num2);
				float num7 = (Mathf.Atan2(num4, num3) * 57.29578f + 360f) % 360f;
				if (num5 <= num2)
				{
					ref Color reference = ref array[i * size + j];
					reference = Color.HSVToRGB(num7 / 360f, num6, 1f);
				}
				else
				{
					ref Color reference2 = ref array[i * size + j];
					reference2 = new Color(0.15f, 0.15f, 0.15f, 1f);
				}
			}
		}
		val.SetPixels(array);
		val.Apply();
		return val;
	}
}
public struct NetMsgCustomPlayerColor : NetworkMessage
{
	public uint netId;

	public byte r;

	public byte g;

	public byte b;

	public bool hasCustomColor;
}
[StructLayout(LayoutKind.Sequential, Size = 1)]
public struct NetMsgResyncRequest : NetworkMessage
{
}
public static class NetMsgCustomPlayerColorSerializer
{
	public static void WriteNetMsgCustomPlayerColor(NetworkWriter writer, NetMsgCustomPlayerColor msg)
	{
		NetworkWriterExtensions.WriteUInt(writer, msg.netId);
		writer.WriteByte(msg.r);
		writer.WriteByte(msg.g);
		writer.WriteByte(msg.b);
		NetworkWriterExtensions.WriteBool(writer, msg.hasCustomColor);
	}

	public static NetMsgCustomPlayerColor ReadNetMsgCustomPlayerColor(NetworkReader reader)
	{
		return new NetMsgCustomPlayerColor
		{
			netId = NetworkReaderExtensions.ReadUInt(reader),
			r = reader.ReadByte(),
			g = reader.ReadByte(),
			b = reader.ReadByte(),
			hasCustomColor = NetworkReaderExtensions.ReadBool(reader)
		};
	}

	public static void WriteNetMsgResyncRequest(NetworkWriter writer, NetMsgResyncRequest msg)
	{
	}

	public static NetMsgResyncRequest ReadNetMsgResyncRequest(NetworkReader reader)
	{
		return default(NetMsgResyncRequest);
	}
}
public static class CustomColorManager
{
	public static readonly Dictionary<uint, Color32> PlayerColors = new Dictionary<uint, Color32>();

	public static readonly Dictionary<int, Color32> ServerColors = new Dictionary<int, Color32>();

	public static void SetColor(uint netId, Color32 color)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		PlayerColors[netId] = color;
	}

	public static void RemoveColor(uint netId)
	{
		PlayerColors.Remove(netId);
	}

	public static bool TryGetColor(uint netId, out Color32 color)
	{
		return PlayerColors.TryGetValue(netId, out color);
	}

	public static void SendMySavedColor()
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		if (HexColorUtil.TryParse(PlayerHEXPlugin.PlayerColourHex.Value, out var color))
		{
			Color32 color2 = Color32.op_Implicit(color);
			NetworkIdentity localPlayer = NetworkClient.localPlayer;
			if (!((Object)(object)localPlayer == (Object)null))
			{
				uint netId = localPlayer.netId;
				SetColor(netId, color2);
				NetMsgCustomPlayerColor netMsgCustomPlayerColor = new NetMsgCustomPlayerColor
				{
					netId = netId,
					r = color2.r,
					g = color2.g,
					b = color2.b,
					hasCustomColor = true
				};
				NetworkClient.Send<NetMsgCustomPlayerColor>(netMsgCustomPlayerColor, 0);
			}
		}
	}

	public static void SendClearColor()
	{
		NetworkIdentity localPlayer = NetworkClient.localPlayer;
		if (!((Object)(object)localPlayer == (Object)null))
		{
			uint netId = localPlayer.netId;
			RemoveColor(netId);
			NetMsgCustomPlayerColor netMsgCustomPlayerColor = new NetMsgCustomPlayerColor
			{
				netId = netId,
				r = 0,
				g = 0,
				b = 0,
				hasCustomColor = false
			};
			NetworkClient.Send<NetMsgCustomPlayerColor>(netMsgCustomPlayerColor, 0);
		}
	}

	public static void HandleServerMessage(NetworkConnectionToClient sender, NetMsgCustomPlayerColor msg)
	{
		//IL_01f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_0201: Unknown result type (might be due to invalid IL or missing references)
		int connectionId = sender.connectionId;
		bool active = NetworkServer.active;
		Debug.Log((object)("[PlayerHEX TRACE] SERVER MSG RX connId=" + connectionId + " msg.netId=" + msg.netId + " hasCustomColor=" + msg.hasCustomColor + " msg.r=" + msg.r + " g=" + msg.g + " b=" + msg.b + " sender.identity=" + (((Object)(object)((NetworkConnection)sender).identity != (Object)null) ? ((NetworkConnection)sender).identity.netId.ToString() : "NULL") + " ServerColors.Count=" + ServerColors.Count));
		uint num = (((Object)(object)((NetworkConnection)sender).identity != (Object)null) ? ((NetworkConnection)sender).identity.netId : 0u);
		if (num == 0)
		{
			Debug.Log((object)("[PlayerHEX TRACE] SERVER MSG DROPPED sender identity is NULL connId=" + connectionId));
			return;
		}
		if (num != msg.netId)
		{
			Debug.Log((object)("[PlayerHEX TRACE] SERVER MSG DROPPED netId mismatch sender=" + num + " msg=" + msg.netId));
			return;
		}
		int count = ServerColors.Count;
		if (msg.hasCustomColor)
		{
			Color32 value = default(Color32);
			((Color32)(ref value))..ctor(msg.r, msg.g, msg.b, byte.MaxValue);
			PlayerColors[num] = value;
			ServerColors[connectionId] = value;
			Debug.Log((object)("[PlayerHEX TRACE] SERVER COLOR SET connId=" + connectionId + " colour=#" + value.r.ToString("X2") + value.g.ToString("X2") + value.b.ToString("X2") + " countBefore=" + count + " countAfter=" + ServerColors.Count));
		}
		else
		{
			PlayerColors.Remove(num);
			ServerColors.Remove(connectionId);
			Debug.Log((object)("[PlayerHEX TRACE] SERVER COLOR REMOVED connId=" + connectionId + " countBefore=" + count + " countAfter=" + ServerColors.Count));
		}
		NetworkServer.SendToAll<NetMsgCustomPlayerColor>(msg, 0, false);
	}

	public static void HandleClientMessage(NetMsgCustomPlayerColor msg)
	{
		//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
		Debug.Log((object)("[PlayerHEX TRACE] CLIENT RX netId=" + msg.netId + " hasCustomColor=" + msg.hasCustomColor + " r=" + msg.r + " g=" + msg.g + " b=" + msg.b));
		if (msg.hasCustomColor)
		{
			Color32 value = default(Color32);
			((Color32)(ref value))..ctor(msg.r, msg.g, msg.b, byte.MaxValue);
			PlayerColors[msg.netId] = value;
			Debug.Log((object)("[PlayerHEX TRACE] DICT SET netId=" + msg.netId + " colour=#" + value.r.ToString("X2") + value.g.ToString("X2") + value.b.ToString("X2")));
		}
		else
		{
			PlayerColors.Remove(msg.netId);
		}
	}

	public static void HandleResyncRequest(NetMsgResyncRequest msg)
	{
		Debug.Log((object)"[PlayerHEX TRACE] RESYNC REQUEST received");
		SendMySavedColor();
	}
}
[HarmonyPatch(typeof(AggroNetworkManager), "OnStartClient")]
public static class AggroNetworkManager_OnStartClient_Patch
{
	[HarmonyPostfix]
	public static void Postfix(AggroNetworkManager __instance)
	{
		NetworkClient.RegisterHandler<NetMsgCustomPlayerColor>((Action<NetMsgCustomPlayerColor>)CustomColorManager.HandleClientMessage, false);
		NetworkClient.RegisterHandler<NetMsgResyncRequest>((Action<NetMsgResyncRequest>)CustomColorManager.HandleResyncRequest, false);
		((MonoBehaviour)__instance).StartCoroutine(AutoSendOnJoin());
	}

	private static IEnumerator AutoSendOnJoin()
	{
		yield return (object)new WaitForSeconds(0.5f);
		CustomColorManager.SendMySavedColor();
	}
}
[HarmonyPatch(typeof(AggroNetworkManager), "OnStartServer")]
public static class AggroNetworkManager_OnStartServer_Patch
{
	[HarmonyPostfix]
	public static void Postfix()
	{
		NetworkServer.RegisterHandler<NetMsgCustomPlayerColor>((Action<NetworkConnectionToClient, NetMsgCustomPlayerColor>)CustomColorManager.HandleServerMessage, false);
	}
}
[HarmonyPatch(typeof(AggroNetworkManager), "OnDestroy")]
public static class AggroNetworkManager_OnDestroy_Patch
{
	[HarmonyPostfix]
	public static void Postfix()
	{
		NetworkClient.UnregisterHandler<NetMsgCustomPlayerColor>();
		NetworkClient.UnregisterHandler<NetMsgResyncRequest>();
		NetworkServer.UnregisterHandler<NetMsgCustomPlayerColor>();
	}
}
[HarmonyPatch(typeof(AggroNetworkManager), "SpawnPlayers")]
public static class AggroNetworkManager_SpawnPlayers_Patch
{
	[HarmonyPostfix]
	public static void Postfix()
	{
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		if (!NetworkServer.active)
		{
			return;
		}
		foreach (KeyValuePair<int, NetworkConnectionToClient> connection in NetworkServer.connections)
		{
			BroadcastConnectionColor(connection.Key, connection.Value);
		}
		NetworkConnectionToClient localConnection = (NetworkConnectionToClient)(object)NetworkServer.localConnection;
		if (localConnection != null && (Object)(object)((NetworkConnection)localConnection).identity != (Object)null)
		{
			int connectionId = localConnection.connectionId;
			if (CustomColorManager.ServerColors.TryGetValue(connectionId, out var value))
			{
				uint netId = ((NetworkConnection)localConnection).identity.netId;
				CustomColorManager.SetColor(netId, value);
				NetMsgCustomPlayerColor netMsgCustomPlayerColor = new NetMsgCustomPlayerColor
				{
					netId = netId,
					r = value.r,
					g = value.g,
					b = value.b,
					hasCustomColor = true
				};
				NetworkServer.SendToAll<NetMsgCustomPlayerColor>(netMsgCustomPlayerColor, 0, false);
			}
		}
	}

	private static void BroadcastConnectionColor(int connId, NetworkConnectionToClient conn)
	{
		if (CustomColorManager.ServerColors.TryGetValue(connId, out var value) && !((Object)(object)((NetworkConnection)conn).identity == (Object)null))
		{
			uint netId = ((NetworkConnection)conn).identity.netId;
			NetMsgCustomPlayerColor netMsgCustomPlayerColor = new NetMsgCustomPlayerColor
			{
				netId = netId,
				r = value.r,
				g = value.g,
				b = value.b,
				hasCustomColor = true
			};
			NetworkServer.SendToAll<NetMsgCustomPlayerColor>(netMsgCustomPlayerColor, 0, false);
		}
	}
}
[HarmonyPatch(typeof(AggroNetworkManager), "OnServerConnect")]
public static class AggroNetworkManager_OnServerConnect_Patch
{
	[HarmonyPostfix]
	public static void Postfix(AggroNetworkManager __instance, NetworkConnectionToClient conn)
	{
		if (NetworkServer.active && !(conn is LocalConnectionToClient) && PlayerHEXPlugin.AutoSyncOnPlayerJoin.Value)
		{
			Debug.Log((object)("[PlayerHEX TRACE] SERVER CONNECT connId=" + conn.connectionId + " ServerColors.Count=" + CustomColorManager.ServerColors.Count));
			((MonoBehaviour)__instance).StartCoroutine(RequestResyncFromAll());
		}
	}

	private static IEnumerator RequestResyncFromAll()
	{
		yield return (object)new WaitForSeconds(0.5f);
		Debug.Log((object)"[PlayerHEX TRACE] RESYNC BROADCAST");
		NetworkServer.SendToAll<NetMsgResyncRequest>(default(NetMsgResyncRequest), 0, false);
	}
}