Decompiled source of EnhancedPlayerPanel v1.0.0

plugins/EnhancedPlayerPanel.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using PurrNet;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("EnhancedPlayerPanel")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("EnhancedPlayerPanel")]
[assembly: AssemblyTitle("EnhancedPlayerPanel")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace Shared
{
	public static class AccessTools
	{
		public static Type TypeByName(string name)
		{
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			foreach (Assembly assembly in assemblies)
			{
				Type type = assembly.GetType(name);
				if (type != null)
				{
					return type;
				}
			}
			return null;
		}

		public static MethodInfo Method(Type type, string name)
		{
			return type?.GetMethod(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
		}

		public static FieldInfo Field(Type type, string name)
		{
			return type?.GetField(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
		}

		public static PropertyInfo Property(Type type, string name)
		{
			return type?.GetProperty(name, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
		}
	}
	public static class FuzzyMatcher
	{
		public static float CalculateScore(string target, string query)
		{
			if (string.IsNullOrEmpty(query))
			{
				return 0f;
			}
			if (string.IsNullOrEmpty(target))
			{
				return 0f;
			}
			string text = target.ToLowerInvariant();
			string text2 = query.ToLowerInvariant();
			if (text.StartsWith(text2))
			{
				return 2f;
			}
			if (IsAcronymMatch(text, text2))
			{
				return 1.5f;
			}
			if (text.Contains(text2))
			{
				return 0.5f;
			}
			return 0f;
		}

		private static bool IsAcronymMatch(string target, string query)
		{
			if (query.Length == 0)
			{
				return false;
			}
			if (target.Length == 0)
			{
				return false;
			}
			if (target[0] != query[0])
			{
				return false;
			}
			int i = 0;
			int num = 0;
			for (; i < target.Length; i++)
			{
				if (num >= query.Length)
				{
					break;
				}
				if (target[i] == query[num])
				{
					num++;
				}
			}
			return num == query.Length;
		}

		public static IEnumerable<T> Filter<T>(IEnumerable<T> items, Func<T, string> selector, string query)
		{
			if (string.IsNullOrEmpty(query))
			{
				return items;
			}
			return from item in items
				select new
				{
					Item = item,
					Score = CalculateScore(selector(item), query)
				} into x
				where x.Score > 0f
				orderby x.Score descending, selector(x.Item)
				select x.Item;
		}
	}
}
namespace EnhancedPlayerPanel
{
	[BepInPlugin("com.on-together-mods.enhancedplayerpanel", "EnhancedPlayerPanel", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(PlayerItemController), "Init")]
		private static class PlayerItemInitPatch
		{
			private static void Postfix(PlayerItemController __instance, string playerSteamId, string playerName, bool isSelf)
			{
				//IL_0071: Unknown result type (might be due to invalid IL or missing references)
				//IL_0080: Unknown result type (might be due to invalid IL or missing references)
				//IL_0085: Unknown result type (might be due to invalid IL or missing references)
				if ((Object)(object)__instance == (Object)null || isSelf)
				{
					return;
				}
				Transform val = ((Component)__instance).transform.Find("Group_Buttons");
				if (!((Object)(object)val == (Object)null) && !((Object)(object)val.Find("Button_Ping") != (Object)null))
				{
					RectTransform component = ((Component)val).GetComponent<RectTransform>();
					component.anchoredPosition += new Vector2(-135f, 0f);
					Transform referenceButton = val.Find("Button_Report");
					CreateButton(val, "Button_Ping", _pingIcon, referenceButton, 0, delegate
					{
						ClosePlayerPanel();
						MentionPlayer(playerName);
					});
					CreateButton(val, "Button_IdCard", _idCardIcon, referenceButton, 1, delegate
					{
						ClosePlayerPanel();
						OpenIdCard(playerSteamId);
					});
				}
			}
		}

		private static Harmony _harmony;

		private static Sprite _pingIcon;

		private static Sprite _idCardIcon;

		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			_harmony = new Harmony("com.on-together-mods.enhancedplayerpanel");
			_harmony.PatchAll();
			_pingIcon = LoadEmbeddedSprite("PingIcon.png");
			_idCardIcon = LoadEmbeddedSprite("IDCardIcon.png");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"EnhancedPlayerPanel loaded");
		}

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

		private static Sprite LoadEmbeddedSprite(string resourceName)
		{
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Expected O, but got Unknown
			//IL_00cb: 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)
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			using Stream stream = executingAssembly.GetManifestResourceStream("EnhancedPlayerPanel." + resourceName);
			if (stream == null)
			{
				return null;
			}
			byte[] array = new byte[stream.Length];
			int num = 0;
			int num2 = array.Length;
			while (num2 > 0)
			{
				int num3 = stream.Read(array, num, num2);
				if (num3 <= 0)
				{
					break;
				}
				num += num3;
				num2 -= num3;
			}
			if (num != array.Length)
			{
				return null;
			}
			Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
			if (!ImageConversion.LoadImage(val, array))
			{
				return null;
			}
			return Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0.5f, 0.5f));
		}

		private static void CreateButton(Transform parent, string name, Sprite icon, Transform referenceButton, int siblingIndex, Action onClick)
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Expected O, but got Unknown
			GameObject val = new GameObject(name, new Type[3]
			{
				typeof(RectTransform),
				typeof(Image),
				typeof(Button)
			});
			val.transform.SetParent(parent, false);
			val.transform.SetSiblingIndex(siblingIndex);
			if ((Object)(object)referenceButton != (Object)null)
			{
				RectTransform component = ((Component)referenceButton).GetComponent<RectTransform>();
				RectTransform component2 = val.GetComponent<RectTransform>();
				component2.sizeDelta = component.sizeDelta;
				LayoutElement component3 = ((Component)referenceButton).GetComponent<LayoutElement>();
				if ((Object)(object)component3 != (Object)null)
				{
					LayoutElement val2 = val.AddComponent<LayoutElement>();
					val2.preferredWidth = component3.preferredWidth;
					val2.preferredHeight = component3.preferredHeight;
					val2.minWidth = component3.minWidth;
					val2.minHeight = component3.minHeight;
					val2.flexibleWidth = component3.flexibleWidth;
					val2.flexibleHeight = component3.flexibleHeight;
				}
			}
			Image component4 = val.GetComponent<Image>();
			((Graphic)component4).color = Color.white;
			component4.preserveAspect = true;
			if ((Object)(object)icon != (Object)null)
			{
				component4.sprite = icon;
			}
			Button component5 = val.GetComponent<Button>();
			((UnityEvent)component5.onClick).AddListener((UnityAction)delegate
			{
				onClick();
				SFXManager i = MonoSingleton<SFXManager>.I;
				if ((Object)(object)i != (Object)null)
				{
					i.PlayUIClick();
				}
			});
		}

		private static void ClosePlayerPanel()
		{
			PlayerPanelController i = NetworkSingleton<PlayerPanelController>.I;
			if ((Object)(object)i != (Object)null)
			{
				i.ButtonClosePanel();
			}
		}

		private static void OpenIdCard(string steamId)
		{
			TemporaryPhoto i = MonoSingleton<TemporaryPhoto>.I;
			PlayerPanelController i2 = NetworkSingleton<PlayerPanelController>.I;
			if ((Object)(object)i == (Object)null || (Object)(object)i2 == (Object)null)
			{
				return;
			}
			int num = i2.PlayerSteamIDs.IndexOf(steamId);
			if (num >= 0 && num < i2.PlayerTransforms.Count)
			{
				NetworkTransform val = i2.PlayerTransforms[num];
				if (!((Object)(object)val == (Object)null))
				{
					i.ButtonOpenIdCheck(val);
				}
			}
		}

		private static void MentionPlayer(string playerName)
		{
			UIManager i = MonoSingleton<UIManager>.I;
			if (!((Object)(object)i == (Object)null) && !((Object)(object)i.MessageInput == (Object)null))
			{
				if (i.IsMessagePanelHidden)
				{
					i.ButtonHide();
				}
				if (!i.IsMessagePanelActivated)
				{
					i.OsSelectMessageInput();
				}
				string text = "@" + playerName + " ";
				if (string.IsNullOrWhiteSpace(i.MessageInput.text))
				{
					i.MessageInput.text = text;
				}
				else
				{
					string text2 = (i.MessageInput.text.EndsWith(" ", StringComparison.Ordinal) ? "" : " ");
					TMP_InputField messageInput = i.MessageInput;
					messageInput.text = messageInput.text + text2 + text;
				}
				((Selectable)i.MessageInput).Select();
				i.MessageInput.ActivateInputField();
				i.MessageInput.caretPosition = i.MessageInput.text.Length;
			}
		}
	}
}