Decompiled source of InspectTeamUpgrades v0.9.0

InspectTeamUpgrades.dll

Decompiled 20 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("jahnpm")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+8f28ab761f4cb74c45fdd8af7a0ec9a2a9d5c2de")]
[assembly: AssemblyProduct("InspectTeamUpgrades")]
[assembly: AssemblyTitle("InspectTeamUpgrades")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 InspectTeamUpgrades
{
	[BepInPlugin("jahnpm.InspectTeamUpgrades", "InspectTeamUpgrades", "0.9.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "jahnpm.InspectTeamUpgrades";

		public const string PluginName = "InspectTeamUpgrades";

		public const string PluginVersion = "0.9.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> EnableScrollWheel;

		internal static ConfigEntry<bool> InvertScrollWheel;

		internal static ConfigEntry<Key> NextPageKey;

		internal static ConfigEntry<Key> PrevPageKey;

		internal static ConfigEntry<bool> ResetPageOnClose;

		internal static ConfigEntry<int> TestPages;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Expected O, but got Unknown
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			EnableScrollWheel = ((BaseUnityPlugin)this).Config.Bind<bool>("Controls", "EnableScrollWheel", true, "Cycle player upgrade pages with the mouse scroll wheel while the map (Tab) is open.");
			InvertScrollWheel = ((BaseUnityPlugin)this).Config.Bind<bool>("Controls", "InvertScrollWheel", false, "Invert the mouse scroll wheel direction for cycling pages (default: scroll down = next page).");
			NextPageKey = ((BaseUnityPlugin)this).Config.Bind<Key>("Controls", "NextPageKey", (Key)62, "Key to show the next player's upgrades while the map (Tab) is open.");
			PrevPageKey = ((BaseUnityPlugin)this).Config.Bind<Key>("Controls", "PrevPageKey", (Key)61, "Key to show the previous player's upgrades while the map (Tab) is open.");
			ResetPageOnClose = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ResetPageOnClose", true, "Jump back to your own upgrades every time the map is closed.");
			TestPages = ((BaseUnityPlugin)this).Config.Bind<int>("Debug", "TestPages", 0, new ConfigDescription("Number of fake player pages to append, for testing the paging UI without other players. Set to 0 for normal play.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 10), Array.Empty<object>()));
			_harmony = new Harmony("jahnpm.InspectTeamUpgrades");
			_harmony.PatchAll();
			Log.LogInfo((object)"InspectTeamUpgrades v0.9.0 loaded.");
		}
	}
}
namespace InspectTeamUpgrades.Patches
{
	[HarmonyPatch(typeof(StatsUI))]
	internal static class StatsUIPatch
	{
		private struct Page
		{
			public string SteamId;

			public string Title;

			public Dictionary<string, int> FakeUpgrades;
		}

		private const int MaxNameLength = 16;

		private static readonly List<Page> Pages = new List<Page>();

		private static string _selectedSteamId;

		private static string _originalHeader;

		private static bool _wasVisible;

		private static readonly string[] TestNames = new string[3] { "TestPlayer", "AVeryLongTestPlayerName", "EmptyPlayer" };

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

		[HarmonyPostfix]
		[HarmonyPatch("Start")]
		private static void StartPostfix(StatsUI __instance)
		{
			_originalHeader = ((TMP_Text)__instance.upgradesHeader).text;
			_selectedSteamId = null;
		}

		[HarmonyPrefix]
		[HarmonyPatch("ShowStats")]
		private static void ShowStatsPrefix()
		{
			_selectedSteamId = null;
		}

		[HarmonyPostfix]
		[HarmonyPatch("Update")]
		private static void UpdatePostfix(StatsUI __instance)
		{
			//IL_00af: 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_00c5: Unknown result type (might be due to invalid IL or missing references)
			if (!(((SemiUI)__instance).showTimer > 0f))
			{
				if (_wasVisible && Plugin.ResetPageOnClose.Value)
				{
					_selectedSteamId = null;
				}
				_wasVisible = false;
				return;
			}
			_wasVisible = true;
			if (!SemiFunc.NoTextInputsActive())
			{
				return;
			}
			int num = 0;
			if (Plugin.EnableScrollWheel.Value)
			{
				Mouse current = Mouse.current;
				if (current != null)
				{
					float y = ((InputControl<Vector2>)(object)current.scroll).ReadValue().y;
					if (y > 0.1f)
					{
						num += (Plugin.InvertScrollWheel.Value ? 1 : (-1));
					}
					else if (y < -0.1f)
					{
						num += ((!Plugin.InvertScrollWheel.Value) ? 1 : (-1));
					}
				}
			}
			Keyboard current2 = Keyboard.current;
			if (current2 != null)
			{
				if (WasPressed(current2, Plugin.NextPageKey.Value))
				{
					num++;
				}
				if (WasPressed(current2, Plugin.PrevPageKey.Value))
				{
					num--;
				}
			}
			if (num != 0)
			{
				BuildPages();
				if (Pages.Count > 1)
				{
					int num2 = ((SelectedIndex() + num) % Pages.Count + Pages.Count) % Pages.Count;
					_selectedSteamId = ((num2 == 0) ? null : Pages[num2].SteamId);
					__instance.Fetch();
					((SemiUI)__instance).SemiUISpringShakeX(5f, 10f, 0.15f);
				}
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("Fetch")]
		private static bool FetchPrefix(StatsUI __instance)
		{
			if (_originalHeader == null)
			{
				_originalHeader = ((TMP_Text)__instance.upgradesHeader).text;
			}
			BuildPages();
			if (Pages.Count <= 1)
			{
				_selectedSteamId = null;
				((TMP_Text)__instance.upgradesHeader).text = _originalHeader;
				return true;
			}
			int num = SelectedIndex();
			Page page = Pages[num];
			StringBuilder stringBuilder = new StringBuilder();
			StringBuilder stringBuilder2 = new StringBuilder();
			foreach (KeyValuePair<string, int> item in from u in page.FakeUpgrades ?? StatsManager.instance.FetchPlayerUpgrades(page.SteamId)
				where u.Value > 0
				orderby StatsUI.GetUpgradeDisplayName(u.Key)
				select u)
			{
				stringBuilder.Append(StatsUI.GetUpgradeDisplayName(item.Key)).Append('\n');
				stringBuilder2.Append("<b>").Append(item.Value).Append("</b>\n");
			}
			if (stringBuilder.Length == 0)
			{
				stringBuilder.Append("<alpha=#88><i>No upgrades</i>");
			}
			((TMP_Text)__instance.Text).text = stringBuilder.ToString();
			((TMP_Text)__instance.textNumbers).text = stringBuilder2.ToString();
			((TMP_Text)__instance.upgradesHeader).text = $"{page.Title} <size=60%><noparse><</noparse> {num + 1}/{Pages.Count} <noparse>></noparse></size>";
			((Behaviour)__instance.upgradesHeader).enabled = true;
			__instance.scanlineObject.SetActive(true);
			return false;
		}

		private static bool WasPressed(Keyboard keyboard, Key key)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			if ((int)key != 0)
			{
				return ((ButtonControl)keyboard[key]).wasPressedThisFrame;
			}
			return false;
		}

		private static int SelectedIndex()
		{
			if (_selectedSteamId == null)
			{
				return 0;
			}
			int num = Pages.FindIndex((Page p) => p.SteamId == _selectedSteamId);
			if (num < 0)
			{
				_selectedSteamId = null;
				return 0;
			}
			return num;
		}

		private static void BuildPages()
		{
			Pages.Clear();
			string text = (Object.op_Implicit((Object)(object)PlayerController.instance) ? PlayerController.instance.playerSteamID : null);
			if (string.IsNullOrEmpty(text))
			{
				return;
			}
			Pages.Add(new Page
			{
				SteamId = text,
				Title = _originalHeader
			});
			List<PlayerAvatar> list = SemiFunc.PlayerGetList();
			if (list != null)
			{
				foreach (PlayerAvatar item in list)
				{
					if (Object.op_Implicit((Object)(object)item) && !string.IsNullOrEmpty(item.steamID) && !(item.steamID == text))
					{
						string text2 = item.playerName;
						if (string.IsNullOrEmpty(text2))
						{
							text2 = SemiFunc.PlayerGetName(item);
						}
						Pages.Add(new Page
						{
							SteamId = item.steamID,
							Title = FormatName(text2)
						});
					}
				}
			}
			AddTestPages();
		}

		private static void AddTestPages()
		{
			int value = Plugin.TestPages.Value;
			if (value <= 0 || !Object.op_Implicit((Object)(object)StatsManager.instance))
			{
				return;
			}
			for (int i = 0; i < value; i++)
			{
				if (i >= TestUpgrades.Count)
				{
					TestUpgrades.Add(MakeTestUpgrades(i));
				}
				string rawName = TestNames[i % TestNames.Length] + ((i >= TestNames.Length) ? (i + 1).ToString() : "");
				Pages.Add(new Page
				{
					SteamId = "test-page-" + i,
					Title = FormatName(rawName),
					FakeUpgrades = TestUpgrades[i]
				});
			}
		}

		private static Dictionary<string, int> MakeTestUpgrades(int pageIndex)
		{
			Dictionary<string, int> dictionary = new Dictionary<string, int>();
			if (pageIndex % TestNames.Length == 2)
			{
				return dictionary;
			}
			int num = 0;
			foreach (string key in StatsManager.instance.dictionaryOfDictionaries.Keys)
			{
				if (key.StartsWith("playerUpgrade"))
				{
					int value = ((pageIndex % TestNames.Length == 1) ? (1 + (num * 5 + pageIndex) % 6) : (((num * 7 + pageIndex * 3) % 4 == 0) ? (1 + num % 3) : 0));
					dictionary[key] = value;
					num++;
				}
			}
			return dictionary;
		}

		private static string FormatName(string rawName)
		{
			string text = (string.IsNullOrEmpty(rawName) ? "Player" : rawName);
			if (text.Length > 16)
			{
				text = text.Substring(0, 14) + "..";
			}
			return "<noparse>" + text.ToUpperInvariant().Replace("</noparse>", "") + "</noparse>";
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}