Decompiled source of Strazers PlayerHEX v1.0.0

PlayerHEX.dll

Decompiled 5 hours ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
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.0")]
public class PlayerHEXPlugin : BaseUnityPlugin
{
	public const string GUID = "strazer.playerhex";

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

	public const string VERSION = "1.0.0";

	public static ConfigEntry<bool> Enabled;

	public static ConfigEntry<string> PlayerColourHex;

	public static ConfigEntry<string> WindowToggleKey;

	private void Awake()
	{
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Expected O, but got Unknown
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_008c: 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 local player's forklift. Example values: #FFFFFF, #FF0000, #00FF00, #0000FF.");
		WindowToggleKey = ((BaseUnityPlugin)this).Config.Bind<string>("General", "WindowToggleKey", "F8", "Key used to toggle the colour picker window on and off. Valid: any KeyCode name (e.g. F8, F1, BackQuote).");
		PlayerColorManager_UpdateRenderers_Patch.Initialize();
		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");
	}
}
[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;

	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_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_012c: Unknown result type (might be due to invalid IL or missing references)
		if (!PlayerHEXPlugin.Enabled.Value)
		{
			return;
		}
		string value = PlayerHEXPlugin.PlayerColourHex.Value;
		if (!HexColorUtil.TryParse(value, out var color))
		{
			return;
		}
		object? value2 = _networkField.GetValue(__instance);
		PlayerColorManagerNetwork val = (PlayerColorManagerNetwork)((value2 is PlayerColorManagerNetwork) ? value2 : null);
		if ((Object)(object)val == (Object)null || !((NetworkBehaviour)val).isLocalPlayer)
		{
			return;
		}
		if (_vehicleRendersField.GetValue(__instance) is List<Renderer> { Count: >0 } list)
		{
			for (int i = 0; i < list.Count; i++)
			{
				Renderer val2 = list[i];
				if ((Object)(object)val2 != (Object)null)
				{
					MaterialUtil.SetPropertyBlockColor(val2, MainColorId, color);
				}
			}
		}
		if (!(_playerRendersField.GetValue(__instance) is List<Renderer> { Count: >0 } list2))
		{
			return;
		}
		for (int i = 0; i < list2.Count; i++)
		{
			Renderer val2 = list2[i];
			if ((Object)(object)val2 != (Object)null)
			{
				MaterialUtil.SetPropertyBlockColor(val2, 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 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)
		string value = PlayerHEXPlugin.WindowToggleKey.Value;
		if (!Enum.TryParse<KeyCode>(value, ignoreCase: true, out _toggleKey))
		{
			_toggleKey = (KeyCode)289;
		}
	}

	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_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_000e: Invalid comparison between Unknown and I4
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0087: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Expected O, but got Unknown
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		Event current = Event.current;
		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_0154: Unknown result type (might be due to invalid IL or missing references)
		//IL_0155: 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)
		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;
		}
		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();
	}

	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;
	}
}