Decompiled source of ShowDevotionItems v0.1.2

plugins/ShowDevotionItems/ShowDevotionItems.dll

Decompiled 2 weeks 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.Logging;
using Microsoft.CodeAnalysis;
using RoR2;
using RoR2.UI;
using TMPro;
using UnityEngine;
using UnityEngine.Events;

[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("ShowDevotionItems")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+150d21d929ef415d80ee07f98cda0ce78a737ae2")]
[assembly: AssemblyProduct("ShowDevotionItems")]
[assembly: AssemblyTitle("ShowDevotionItems")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.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 ShowDevotionItems
{
	internal class DevotionScoreboard : MonoBehaviour
	{
		private class LemurianRow
		{
			public ScoreboardStrip strip;

			public readonly List<ItemIndex> items = new List<ItemIndex>();

			public bool dirty = true;
		}

		private const float RefreshInterval = 0.25f;

		private ScoreboardController scoreboard;

		private readonly Dictionary<CharacterMaster, LemurianRow> rows = new Dictionary<CharacterMaster, LemurianRow>();

		private readonly Dictionary<CharacterMaster, List<CharacterMaster>> lemuriansByOwner = new Dictionary<CharacterMaster, List<CharacterMaster>>();

		private readonly List<ItemIndex> itemBuffer = new List<ItemIndex>();

		private readonly HashSet<ItemIndex> seenItems = new HashSet<ItemIndex>();

		private readonly List<CharacterMaster> staleOwners = new List<CharacterMaster>();

		private float refreshTimer;

		private void Awake()
		{
			scoreboard = ((Component)this).GetComponent<ScoreboardController>();
		}

		private void OnEnable()
		{
			foreach (LemurianRow value in rows.Values)
			{
				value.dirty = true;
			}
			refreshTimer = 0f;
		}

		private void OnDestroy()
		{
			ClearRows();
		}

		private void LateUpdate()
		{
			refreshTimer -= Time.unscaledDeltaTime;
			if (!(refreshTimer > 0f))
			{
				refreshTimer = 0.25f;
				Refresh();
			}
		}

		private static bool IsDevotionEnabled()
		{
			if (Object.op_Implicit((Object)(object)RunArtifactManager.instance) && Object.op_Implicit((Object)(object)Artifacts.Devotion))
			{
				return RunArtifactManager.instance.IsArtifactEnabled(Artifacts.Devotion);
			}
			return false;
		}

		private void Refresh()
		{
			if (!IsDevotionEnabled())
			{
				ClearRows();
				return;
			}
			CollectLemurians();
			foreach (ScoreboardStrip element in scoreboard.stripAllocator.elements)
			{
				CharacterMaster val = (Object.op_Implicit((Object)(object)element) ? element.master : null);
				if (Object.op_Implicit((Object)(object)val) && lemuriansByOwner.TryGetValue(val, out var value))
				{
					if (!rows.TryGetValue(val, out var value2) || !Object.op_Implicit((Object)(object)value2.strip))
					{
						value2 = new LemurianRow
						{
							strip = CreateStrip()
						};
						rows[val] = value2;
					}
					((Component)value2.strip).transform.SetSiblingIndex(((Component)element).transform.GetSiblingIndex() + 1);
					UpdateRow(value2, val, value);
				}
			}
			staleOwners.Clear();
			foreach (KeyValuePair<CharacterMaster, LemurianRow> row in rows)
			{
				if (!Object.op_Implicit((Object)(object)row.Key) || !lemuriansByOwner.ContainsKey(row.Key) || !HasPlayerStrip(row.Key))
				{
					staleOwners.Add(row.Key);
				}
			}
			foreach (CharacterMaster staleOwner in staleOwners)
			{
				DestroyRow(rows[staleOwner]);
				rows.Remove(staleOwner);
			}
		}

		private void CollectLemurians()
		{
			foreach (List<CharacterMaster> value2 in lemuriansByOwner.Values)
			{
				value2.Clear();
			}
			foreach (CharacterMaster readOnlyInstances in CharacterMaster.readOnlyInstancesList)
			{
				if (!Object.op_Implicit((Object)(object)readOnlyInstances) || !Object.op_Implicit((Object)(object)readOnlyInstances.inventory) || !Object.op_Implicit((Object)(object)((Component)readOnlyInstances).GetComponent<DevotedLemurianController>()))
				{
					continue;
				}
				CharacterMaster val = (Object.op_Implicit((Object)(object)readOnlyInstances.minionOwnership) ? readOnlyInstances.minionOwnership.ownerMaster : null);
				if (Object.op_Implicit((Object)(object)val))
				{
					if (!lemuriansByOwner.TryGetValue(val, out var value))
					{
						value = new List<CharacterMaster>();
						lemuriansByOwner[val] = value;
					}
					value.Add(readOnlyInstances);
				}
			}
			staleOwners.Clear();
			foreach (KeyValuePair<CharacterMaster, List<CharacterMaster>> item in lemuriansByOwner)
			{
				if (!Object.op_Implicit((Object)(object)item.Key) || item.Value.Count == 0)
				{
					staleOwners.Add(item.Key);
				}
			}
			foreach (CharacterMaster staleOwner in staleOwners)
			{
				lemuriansByOwner.Remove(staleOwner);
			}
		}

		private bool HasPlayerStrip(CharacterMaster owner)
		{
			foreach (ScoreboardStrip element in scoreboard.stripAllocator.elements)
			{
				if (Object.op_Implicit((Object)(object)element) && (Object)(object)element.master == (Object)(object)owner)
				{
					return true;
				}
			}
			return false;
		}

		private ScoreboardStrip CreateStrip()
		{
			ScoreboardStrip component = Object.Instantiate<GameObject>(scoreboard.stripPrefab, (Transform)(object)scoreboard.container).GetComponent<ScoreboardStrip>();
			component.itemInventoryDisplay.SetSubscribedInventory((Inventory)null);
			if (Object.op_Implicit((Object)(object)component.equipmentIcon))
			{
				component.equipmentIcon.SetTargetInventory((Inventory)null);
			}
			if (Object.op_Implicit((Object)(object)component.keyItemIcon))
			{
				component.keyItemIcon.targetInventory = null;
			}
			if (Object.op_Implicit((Object)(object)component.userAvatar))
			{
				((Component)component.userAvatar).gameObject.SetActive(false);
			}
			((TMP_Text)component.moneyText).text = string.Empty;
			return component;
		}

		private void UpdateRow(LemurianRow row, CharacterMaster owner, List<CharacterMaster> lemurians)
		{
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			ScoreboardStrip strip = row.strip;
			((TMP_Text)strip.nameLabel).text = $"{Util.GetBestMasterName(owner)}'s Lemurians ({lemurians.Count})";
			strip.classIcon.texture = FindPortrait(lemurians[0]);
			CollectItems(lemurians, itemBuffer);
			if (!row.dirty && SameItems(row.items, itemBuffer))
			{
				return;
			}
			row.dirty = false;
			row.items.Clear();
			row.items.AddRange(itemBuffer);
			int[] array = new int[ItemCatalog.itemCount];
			foreach (ItemIndex item in row.items)
			{
				array[item] = 1;
			}
			strip.itemInventoryDisplay.SetItems(row.items, array);
			((TMP_Text)strip.itemCountText).text = row.items.Count.ToString();
		}

		private void CollectItems(List<CharacterMaster> lemurians, List<ItemIndex> output)
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			output.Clear();
			seenItems.Clear();
			foreach (CharacterMaster lemurian in lemurians)
			{
				Inventory inventory = lemurian.inventory;
				foreach (ItemIndex item in inventory.itemAcquisitionOrder)
				{
					ItemDef itemDef = ItemCatalog.GetItemDef(item);
					if (!((Object)(object)itemDef == (Object)null) && !itemDef.hidden && !IsDeveloperItem(itemDef) && inventory.GetItemCountEffective(item) > 0 && seenItems.Add(item))
					{
						output.Add(item);
					}
				}
			}
		}

		private static bool SameItems(List<ItemIndex> a, List<ItemIndex> b)
		{
			//IL_0016: 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)
			if (a.Count != b.Count)
			{
				return false;
			}
			for (int i = 0; i < a.Count; i++)
			{
				if (a[i] != b[i])
				{
					return false;
				}
			}
			return true;
		}

		private static bool IsDeveloperItem(ItemDef itemDef)
		{
			if ((Object)(object)itemDef == (Object)(object)Items.BoostHp || (Object)(object)itemDef == (Object)(object)Items.BoostDamage)
			{
				return true;
			}
			return false;
		}

		private static Texture FindPortrait(CharacterMaster lemurian)
		{
			CharacterBody body = lemurian.GetBody();
			if (Object.op_Implicit((Object)(object)body))
			{
				return body.portraitIcon;
			}
			GameObject bodyPrefab = lemurian.bodyPrefab;
			CharacterBody val = (Object.op_Implicit((Object)(object)bodyPrefab) ? bodyPrefab.GetComponent<CharacterBody>() : null);
			if (!Object.op_Implicit((Object)(object)val))
			{
				return null;
			}
			return val.portraitIcon;
		}

		private void ClearRows()
		{
			foreach (LemurianRow value in rows.Values)
			{
				DestroyRow(value);
			}
			rows.Clear();
		}

		private static void DestroyRow(LemurianRow row)
		{
			if (Object.op_Implicit((Object)(object)row.strip))
			{
				Object.Destroy((Object)(object)((Component)row.strip).gameObject);
			}
		}
	}
	internal static class Log
	{
		private static ManualLogSource _logSource;

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

		private static string Format(object data, string file, int line)
		{
			string fileName = Path.GetFileName(file);
			return $"[{fileName}:{line}] {data}";
		}

		internal static void Debug(object data, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0)
		{
			_logSource.LogDebug((object)Format(data, file, line));
		}

		internal static void Error(object data, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0)
		{
			_logSource.LogError((object)Format(data, file, line));
		}

		internal static void Fatal(object data, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0)
		{
			_logSource.LogFatal((object)Format(data, file, line));
		}

		internal static void Info(object data, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0)
		{
			_logSource.LogInfo((object)Format(data, file, line));
		}

		internal static void Message(object data, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0)
		{
			_logSource.LogMessage((object)Format(data, file, line));
		}

		internal static void Warning(object data, [CallerFilePath] string file = "", [CallerLineNumber] int line = 0)
		{
			_logSource.LogWarning((object)Format(data, file, line));
		}
	}
	[BepInPlugin("Korthose.ShowDevotionItems", "ShowDevotionItems", "0.1.2")]
	public class ShowDevotionItems : BaseUnityPlugin
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static UnityAction <0>__OnScoreboardOpen;
		}

		public const string PluginGUID = "Korthose.ShowDevotionItems";

		public const string PluginAuthor = "Korthose";

		public const string PluginName = "ShowDevotionItems";

		public const string PluginVersion = "0.1.2";

		public void Awake()
		{
			//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_0026: Expected O, but got Unknown
			Log.Init(((BaseUnityPlugin)this).Logger);
			object obj = <>O.<0>__OnScoreboardOpen;
			if (obj == null)
			{
				UnityAction val = OnScoreboardOpen;
				<>O.<0>__OnScoreboardOpen = val;
				obj = (object)val;
			}
			ScoreboardController.onScoreboardOpen += (UnityAction)obj;
		}

		public void OnDestroy()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			object obj = <>O.<0>__OnScoreboardOpen;
			if (obj == null)
			{
				UnityAction val = OnScoreboardOpen;
				<>O.<0>__OnScoreboardOpen = val;
				obj = (object)val;
			}
			ScoreboardController.onScoreboardOpen -= (UnityAction)obj;
		}

		private static void OnScoreboardOpen()
		{
			ScoreboardController[] array = Object.FindObjectsOfType<ScoreboardController>();
			foreach (ScoreboardController val in array)
			{
				if (!Object.op_Implicit((Object)(object)((Component)val).GetComponent<DevotionScoreboard>()))
				{
					((Component)val).gameObject.AddComponent<DevotionScoreboard>();
				}
			}
		}
	}
}