Decompiled source of GupRankings v1.0.1

plugins/GupRankings/GupRankings.dll

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using GupRankings.RankingDisplayHooks;
using GupRankings.SortStatsNetMessage;
using Microsoft.CodeAnalysis;
using MonoMod.RuntimeDetour;
using R2API.Networking;
using R2API.Networking.Interfaces;
using R2API.Utils;
using RiskOfOptions;
using RiskOfOptions.OptionConfigs;
using RiskOfOptions.Options;
using RoR2;
using RoR2.Stats;
using RoR2.UI;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("GupRankings")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+0c5fd7b39a8c46c699ccea21e75d0e5d1c936869")]
[assembly: AssemblyProduct("GupRankings")]
[assembly: AssemblyTitle("GupRankings")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[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 GupRankings
{
	public enum StatRankings
	{
		TotalDamageDealt,
		TotalMinionDamageDealt,
		HighestDamageDealt,
		TotalGoldCollected,
		TotalGoldSpent,
		LeastDamageTaken,
		TotalKills,
		TotalEliteKills
	}
	[BepInPlugin("KarmaReplicant.GupRankings", "GupRankings", "1.0.0")]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public class BasePlugin : BaseUnityPlugin
	{
		public const string PluginGUID = "KarmaReplicant.GupRankings";

		public const string PluginAuthor = "KarmaReplicant";

		public const string PluginName = "GupRankings";

		public const string PluginVersion = "1.0.0";

		internal RankingDisplay rankDisplay;

		public ConfigEntry<StatRankings> statEnum;

		public ConfigEntry<Color> headerColor;

		public ConfigEntry<Color> firstColor;

		public ConfigEntry<Color> secondColor;

		public ConfigEntry<Color> thirdColor;

		public ConfigEntry<float> fontSize;

		public ConfigEntry<float> userNameLength;

		public byte[] imgData;

		public Sprite logo;

		public static BasePlugin instance;

		public void Awake()
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Expected O, but got Unknown
			//IL_00ef: 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)
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_010f: Expected O, but got Unknown
			//IL_010a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0114: Expected O, but got Unknown
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: 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)
			//IL_015f: Expected O, but got Unknown
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Expected O, but got Unknown
			//IL_0184: Unknown result type (might be due to invalid IL or missing references)
			//IL_019e: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a8: Expected O, but got Unknown
			//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ec: Expected O, but got Unknown
			//IL_020c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0226: Unknown result type (might be due to invalid IL or missing references)
			//IL_0230: Expected O, but got Unknown
			//IL_0250: Unknown result type (might be due to invalid IL or missing references)
			//IL_026a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0274: Expected O, but got Unknown
			string directoryName = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location);
			imgData = File.ReadAllBytes(directoryName + "/icon.png");
			Texture2D val = new Texture2D(256, 256);
			ImageConversion.LoadImage(val, imgData);
			logo = Sprite.Create(val, new Rect(0f, 0f, (float)((Texture)val).width, (float)((Texture)val).height), new Vector2(0f, 0f));
			instance = this;
			rankDisplay = new RankingDisplay();
			NetworkingAPI.RegisterMessageType<SortStats>();
			statEnum = ((BaseUnityPlugin)this).Config.Bind<StatRankings>("Options", "Leaderboard Stat", StatRankings.TotalDamageDealt, "Select the statistic that the leaderboard will be based on. Whatever the host has it set to will be displayed in game.");
			ModSettingsManager.AddOption((BaseOption)new ChoiceOption((ConfigEntryBase)(object)statEnum));
			fontSize = ((BaseUnityPlugin)this).Config.Bind<float>("Options", "Font Size", 0f, "Select the font size for the leaderboard text. Setting it to 0 will set it to the default size of the panel. The percentage equals the size.");
			ModSettingsManager.AddOption((BaseOption)new SliderOption(fontSize, new SliderConfig
			{
				min = 0f,
				max = 64f
			}));
			userNameLength = ((BaseUnityPlugin)this).Config.Bind<float>("Options", "Username Length", 8f, "Select the maximum amount of characters of player's usernames that will be shown to help make everything fit. Setting it to 1 will have the full name displayed regardless. The percentage equals the size.");
			ModSettingsManager.AddOption((BaseOption)new SliderOption(userNameLength, new SliderConfig
			{
				min = 1f,
				max = 32f
			}));
			headerColor = ((BaseUnityPlugin)this).Config.Bind<Color>("Options", "Header Color", new Color(1f, 44f / 85f, 0f), "Select the header color of the leaderboard.");
			ModSettingsManager.AddOption((BaseOption)new ColorOption(headerColor));
			firstColor = ((BaseUnityPlugin)this).Config.Bind<Color>("Options", "First Color", new Color(0.8509804f, 0.72156864f, 0.02745098f), "Select the first place color of the leaderboard.");
			ModSettingsManager.AddOption((BaseOption)new ColorOption(firstColor));
			secondColor = ((BaseUnityPlugin)this).Config.Bind<Color>("Options", "Second Color", new Color(64f / 85f, 64f / 85f, 64f / 85f), "Select the second place color of the leaderboard.");
			ModSettingsManager.AddOption((BaseOption)new ColorOption(secondColor));
			thirdColor = ((BaseUnityPlugin)this).Config.Bind<Color>("Options", "Third Color", new Color(0.7490196f, 0.47058824f, 19f / 85f), "Select the third place color of the leaderboard.");
			ModSettingsManager.AddOption((BaseOption)new ColorOption(thirdColor));
			ModSettingsManager.SetModIcon(logo);
			Log.Init(((BaseUnityPlugin)this).Logger);
		}
	}
	internal static class Log
	{
		private static ManualLogSource _logSource;

		internal static void Init(ManualLogSource logSource)
		{
			_logSource = logSource;
		}

		internal static void Debug(object data)
		{
			_logSource.LogDebug(data);
		}

		internal static void Error(object data)
		{
			_logSource.LogError(data);
		}

		internal static void Fatal(object data)
		{
			_logSource.LogFatal(data);
		}

		internal static void Info(object data)
		{
			_logSource.LogInfo(data);
		}

		internal static void Message(object data)
		{
			_logSource.LogMessage(data);
		}

		internal static void Warning(object data)
		{
			_logSource.LogWarning(data);
		}
	}
}
namespace GupRankings.SortStatsNetMessage
{
	public class SortStats : INetMessage, ISerializableObject
	{
		private NetworkInstanceId netId;

		private string rawStats;

		private StatRankings selectedStat;

		public static string statLabel = "none";

		private List<KeyValuePair<string, ulong>> statsData;

		public static string sortedStatDisplay;

		private bool changed;

		public SortStats()
		{
			rawStats = "";
			statsData = new List<KeyValuePair<string, ulong>>();
			sortedStatDisplay = "<color=#ff8300>Leaderboard (" + statLabel + "):</color>\nIF YOU SEE THIS THERE IS A PROBLEM!!!\nPLEASE REPORT IT TO THE GITHUB ISSUES PAGE!";
			changed = false;
		}

		public SortStats(NetworkInstanceId netId, string stats, StatRankings selectedStat, string statName)
		{
			//IL_0007: 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)
			this.netId = netId;
			rawStats = stats;
			this.selectedStat = selectedStat;
			statLabel = statName;
		}

		public void Deserialize(NetworkReader reader)
		{
			//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)
			netId = reader.ReadNetworkId();
			string text = reader.ReadString();
			if (!text.Equals(rawStats))
			{
				rawStats = text;
				changed = true;
			}
			else
			{
				changed = false;
			}
			selectedStat = (StatRankings)reader.ReadInt32();
			statLabel = reader.ReadString();
		}

		public void OnReceived()
		{
			if (changed)
			{
				statsData.Clear();
				string[] array = rawStats.Split('\n');
				for (int i = 0; i < array.Length; i++)
				{
					string[] array2 = array[i].Split('\t');
					statsData.Add(new KeyValuePair<string, ulong>(array2[0], ulong.Parse(array2[1])));
				}
				if (selectedStat == StatRankings.LeastDamageTaken)
				{
					statsData.Sort(PlayerStatsComparisonGreater);
				}
				else
				{
					statsData.Sort(PlayerStatsComparisonLess);
				}
				SetDisplayString(statsData);
			}
		}

		public void Serialize(NetworkWriter writer)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			writer.Write(netId);
			writer.Write(rawStats);
			writer.Write((int)selectedStat);
			writer.Write(statLabel);
		}

		private static void SetDisplayString(List<KeyValuePair<string, ulong>> data)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: 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)
			//IL_0029: 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_003e: 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_004e: 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_0064: 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)
			//IL_012f: 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)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			Color32 color = Color32.op_Implicit(BasePlugin.instance.headerColor.Value);
			Color32 color2 = Color32.op_Implicit(BasePlugin.instance.firstColor.Value);
			Color32 color3 = Color32.op_Implicit(BasePlugin.instance.secondColor.Value);
			Color32 color4 = Color32.op_Implicit(BasePlugin.instance.thirdColor.Value);
			sortedStatDisplay = "<color=#" + ConvertColorToHexString(color) + ">" + statLabel + "</color>\n<color=#" + ConvertColorToHexString(color) + ">Leaderboard:</color>\n";
			int num = 1;
			foreach (KeyValuePair<string, ulong> datum in data)
			{
				string text = datum.Key;
				if ((int)BasePlugin.instance.userNameLength.Value != 1 && datum.Key.Length > (int)BasePlugin.instance.userNameLength.Value)
				{
					text = text.Substring(0, (int)BasePlugin.instance.userNameLength.Value);
				}
				switch (num)
				{
				case 1:
					sortedStatDisplay += $"<color=#{ConvertColorToHexString(color2)}>1. {text}: {datum.Value}</color>\n";
					break;
				case 2:
					sortedStatDisplay += $"<color=#{ConvertColorToHexString(color3)}>2. {text}: {datum.Value}</color>\n";
					break;
				case 3:
					sortedStatDisplay += $"<color=#{ConvertColorToHexString(color4)}>3. {text}: {datum.Value}</color>\n";
					break;
				default:
					sortedStatDisplay += $"{num}. {text}: {datum.Value}\n";
					break;
				}
				num++;
			}
		}

		public static void HostSync(string s, StatRankings selectedStat, string statName)
		{
			statLabel = statName;
			List<KeyValuePair<string, ulong>> list = new List<KeyValuePair<string, ulong>>();
			string[] array = s.Split('\n');
			for (int i = 0; i < array.Length; i++)
			{
				string[] array2 = array[i].Split('\t');
				list.Add(new KeyValuePair<string, ulong>(array2[0], ulong.Parse(array2[1])));
			}
			if (selectedStat == StatRankings.LeastDamageTaken)
			{
				list.Sort(PlayerStatsComparisonGreater);
			}
			else
			{
				list.Sort(PlayerStatsComparisonLess);
			}
			SetDisplayString(list);
		}

		private static int PlayerStatsComparisonGreater(KeyValuePair<string, ulong> x, KeyValuePair<string, ulong> y)
		{
			return Convert.ToInt32(x.Value > y.Value);
		}

		private static int PlayerStatsComparisonLess(KeyValuePair<string, ulong> x, KeyValuePair<string, ulong> y)
		{
			return Convert.ToInt32(x.Value < y.Value);
		}

		private static string ConvertColorToHexString(Color32 color)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			string[] array = BitConverter.ToString(new byte[3] { color.r, color.g, color.b }).Split('-');
			string text = "";
			string[] array2 = array;
			foreach (string text2 in array2)
			{
				text += text2;
			}
			return text;
		}
	}
}
namespace GupRankings.RankingDisplayHooks
{
	public class RankingDisplay
	{
		private VerticalLayoutGroup layoutGroup;

		private LayoutElement layoutElement;

		private static Hook updateHook;

		private TextMeshProUGUI text;

		private GameObject textObj;

		private Transform track;

		public Dictionary<StatRankings, StatDef> StatsMap;

		private float originalFontSize;

		public RankingDisplay()
		{
			StatsMap = new Dictionary<StatRankings, StatDef>
			{
				{
					StatRankings.TotalDamageDealt,
					StatDef.totalDamageDealt
				},
				{
					StatRankings.TotalMinionDamageDealt,
					StatDef.totalMinionDamageDealt
				},
				{
					StatRankings.HighestDamageDealt,
					StatDef.highestDamageDealt
				},
				{
					StatRankings.TotalGoldCollected,
					StatDef.goldCollected
				},
				{
					StatRankings.TotalGoldSpent,
					StatDef.totalGoldPurchases
				},
				{
					StatRankings.LeastDamageTaken,
					StatDef.totalDamageTaken
				},
				{
					StatRankings.TotalKills,
					StatDef.totalKills
				},
				{
					StatRankings.TotalEliteKills,
					StatDef.totalEliteKills
				}
			};
			Hooks();
		}

		public void Hooks()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			MethodInfo? method = typeof(ObjectivePanelController).GetMethod("Update", BindingFlags.Instance | BindingFlags.NonPublic);
			MethodInfo method2 = typeof(RankingDisplay).GetMethod("Update", BindingFlags.Instance | BindingFlags.NonPublic);
			updateHook = new Hook((MethodBase)method, method2, (object)this);
		}

		private void Update(Action<ObjectivePanelController> orig, ObjectivePanelController self)
		{
			rankDisplay();
			orig(self);
		}

		public void rankDisplay()
		{
			//IL_022a: 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_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			VerticalLayoutGroup[] componentsInChildren = Run.instance.uiInstances[0].GetComponentsInChildren<VerticalLayoutGroup>();
			foreach (VerticalLayoutGroup val in componentsInChildren)
			{
				if (((Object)((Component)val).gameObject).name == "RightInfoBar" && (Object)(object)textObj == (Object)null)
				{
					Transform val2 = ((Component)val).transform.Find("ObjectivePanel");
					GameObject val3 = Object.Instantiate<GameObject>(((Component)val2).gameObject);
					val3.transform.parent = ((Component)val2.parent).transform;
					((Object)val3).name = "RankingDisplay";
					RectTransform component = val3.GetComponent<RectTransform>();
					layoutGroup = val3.GetComponent<VerticalLayoutGroup>();
					this.text = val3.GetComponentInChildren<TextMeshProUGUI>();
					layoutElement = val3.GetComponentInChildren<LayoutElement>();
					((Transform)component).localPosition = Vector3.zero;
					((Transform)component).localEulerAngles = Vector3.zero;
					((Transform)component).localScale = Vector3.one;
					((Behaviour)layoutGroup).enabled = false;
					((Behaviour)layoutGroup).enabled = true;
					if ((Object)(object)textObj == (Object)null)
					{
						((TMP_Text)this.text).alignment = (TextAlignmentOptions)257;
						((Graphic)this.text).color = Color.white;
						textObj = ((Component)this.text).gameObject;
						track = val3.transform;
						originalFontSize = ((TMP_Text)this.text).fontSize;
					}
				}
			}
			if (NetworkServer.active)
			{
				string text = "";
				foreach (PlayerCharacterMasterController instance in PlayerCharacterMasterController.instances)
				{
					try
					{
						StatSheet currentStats = instance.master.playerStatsComponent.currentStats;
						text = text + instance.GetDisplayName() + "\t" + currentStats.GetStatValueString(StatsMap[BasePlugin.instance.statEnum.Value]) + "\n";
					}
					catch
					{
						Debug.Log((object)(instance.GetDisplayName() + " has thrown an exception..."));
					}
				}
				NetMessageExtensions.Send((INetMessage)(object)new SortStats(((NetworkBehaviour)LocalUserManager.GetFirstLocalUser().currentNetworkUser).netId, text.Substring(0, text.Length - 1), BasePlugin.instance.statEnum.Value, ((ConfigEntryBase)BasePlugin.instance.statEnum).BoxedValue.ToString()), (NetworkDestination)1);
				SortStats.HostSync(text.Substring(0, text.Length - 1), BasePlugin.instance.statEnum.Value, ((ConfigEntryBase)BasePlugin.instance.statEnum).BoxedValue.ToString());
			}
			((TMP_Text)this.text).fontSize = ((BasePlugin.instance.fontSize.Value == 0f) ? originalFontSize : BasePlugin.instance.fontSize.Value);
			if (Object.op_Implicit((Object)(object)this.text))
			{
				layoutElement.preferredHeight = ((TMP_Text)this.text).fontSize * (float)SortStats.sortedStatDisplay.Split('\n').Length;
				((TMP_Text)this.text).SetText(SortStats.sortedStatDisplay, true);
			}
			if (!Object.op_Implicit((Object)(object)textObj))
			{
				textObj.SetActive(true);
			}
		}
	}
}