Decompiled source of TrophyPercent v0.0.2

ValheimTrophyPercent.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(/*Could not decode attribute arguments.*/)]
[assembly: TargetFramework(".NETCoreApp,Version=v10.0", FrameworkDisplayName = ".NET 10.0")]
[assembly: AssemblyCompany("ValheimTrophyPercent")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+630ed9ab9c1ef99885b9720fb97178414104baca")]
[assembly: AssemblyProduct("ValheimTrophyPercent")]
[assembly: AssemblyTitle("ValheimTrophyPercent")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace ValheimTrophyPercent;

public static class ProgressList
{
	[HarmonyPatch(typeof(Hud), "Awake")]
	public static class Hud_Awake_Patch
	{
		public static void Postfix(Hud __instance)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Expected O, but got Unknown
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: 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)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)__instance.m_rootObject == (Object)null))
			{
				PinnedPanel = new GameObject("PinnedTrophiesPanel");
				PinnedPanel.transform.SetParent(__instance.m_rootObject.transform, false);
				Container = PinnedPanel.AddComponent<RectTransform>();
				Container.anchorMin = new Vector2(1f, 1f);
				Container.anchorMax = new Vector2(1f, 1f);
				Container.pivot = new Vector2(1f, 1f);
				Container.anchoredPosition = new Vector2(-20f, -250f);
				Container.sizeDelta = new Vector2(200f, 400f);
				VerticalLayoutGroup val = PinnedPanel.AddComponent<VerticalLayoutGroup>();
				((LayoutGroup)val).childAlignment = (TextAnchor)2;
				((HorizontalOrVerticalLayoutGroup)val).spacing = 5f;
				UpdatePinnedUI();
			}
		}
	}

	private static GameObject PinnedPanel;

	private static RectTransform Container;

	public static void UpdatePinnedUI()
	{
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Expected O, but got Unknown
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)PinnedPanel == (Object)null || (Object)(object)ObjectDB.instance == (Object)null)
		{
			return;
		}
		foreach (Transform item in PinnedPanel.transform)
		{
			Transform val = item;
			Object.Destroy((Object)(object)((Component)val).gameObject);
		}
		Enumerator<string> enumerator2 = TrophyManager.PinnedTrophies.GetEnumerator();
		try
		{
			while (enumerator2.MoveNext())
			{
				string current = enumerator2.Current;
				GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(current);
				if (!((Object)(object)itemPrefab == (Object)null))
				{
					ItemDrop component = itemPrefab.GetComponent<ItemDrop>();
					string name = Localization.instance.Localize(component.m_itemData.m_shared.m_name);
					Sprite icon = component.m_itemData.GetIcon();
					CreateRow(name, icon);
				}
			}
		}
		finally
		{
			((global::System.IDisposable)enumerator2/*cast due to .constrained prefix*/).Dispose();
		}
	}

	private static void CreateRow(string name, Sprite icon)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: Expected O, but got Unknown
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: 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_00e1: Expected O, but got Unknown
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject("PinnedRow");
		val.transform.SetParent(PinnedPanel.transform, false);
		RectTransform val2 = val.AddComponent<RectTransform>();
		val2.sizeDelta = new Vector2(200f, 30f);
		HorizontalLayoutGroup val3 = val.AddComponent<HorizontalLayoutGroup>();
		((HorizontalOrVerticalLayoutGroup)val3).spacing = 10f;
		((LayoutGroup)val3).childAlignment = (TextAnchor)5;
		((HorizontalOrVerticalLayoutGroup)val3).childControlWidth = false;
		GameObject val4 = new GameObject("Text");
		val4.transform.SetParent(val.transform, false);
		TMP_Text val5 = (TMP_Text)(object)val4.AddComponent<TextMeshProUGUI>();
		val5.text = name;
		val5.fontSize = 16f;
		((Graphic)val5).color = Color.white;
		val5.alignment = (TextAlignmentOptions)516;
		((Component)val5).GetComponent<RectTransform>().sizeDelta = new Vector2(150f, 30f);
		GameObject val6 = new GameObject("Icon");
		val6.transform.SetParent(val.transform, false);
		Image val7 = val6.AddComponent<Image>();
		val7.sprite = icon;
		val6.GetComponent<RectTransform>().sizeDelta = new Vector2(25f, 25f);
	}
}
[HarmonyPatch(typeof(TextsDialog), "UpdateTrophyList")]
public static class TextsDialog_UpdateTrophyList_Patch
{
	public static void Postfix(TextsDialog __instance, ref List<GameObject> ___m_trophyList, RectTransform ___m_trophieListRoot, GameObject ___m_trophieElementPrefab, float ___m_trophieListSpace)
	{
		//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)
		//IL_0117: Unknown result type (might be due to invalid IL or missing references)
		//IL_0125: Unknown result type (might be due to invalid IL or missing references)
		//IL_0192: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)Player.m_localPlayer == (Object)null)
		{
			return;
		}
		List<string> trophies = Player.m_localPlayer.GetTrophies();
		int totalTrophies = TrophyManager.GetTotalTrophies();
		AddProgressText(___m_trophieListRoot, trophies.Count, totalTrophies);
		for (int i = 0; i < trophies.Count; i++)
		{
			GameObject uiElement = ___m_trophyList[i];
			string trophyPrefabName = trophies[i];
			AddPinButtonToElement(uiElement, trophyPrefabName);
		}
		List<string> killedButMissingTrophies = TrophyManager.GetKilledButMissingTrophies();
		float num = 0f;
		Enumerator<string> enumerator = killedButMissingTrophies.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				string current = enumerator.Current;
				GameObject itemPrefab = ObjectDB.instance.GetItemPrefab(current);
				if (!((Object)(object)itemPrefab == (Object)null))
				{
					ItemDrop component = itemPrefab.GetComponent<ItemDrop>();
					GameObject val = Object.Instantiate<GameObject>(___m_trophieElementPrefab, (Transform)(object)___m_trophieListRoot);
					val.SetActive(true);
					Transform transform = val.transform;
					RectTransform val2 = (RectTransform)(object)((transform is RectTransform) ? transform : null);
					val2.anchoredPosition = new Vector2((float)((Vector2Int)(ref component.m_itemData.m_shared.m_trophyPos)).x * ___m_trophieListSpace, (float)((Vector2Int)(ref component.m_itemData.m_shared.m_trophyPos)).y * (0f - ___m_trophieListSpace));
					num = Mathf.Min(num, val2.anchoredPosition.y - ___m_trophieListSpace);
					string text = Localization.instance.Localize(component.m_itemData.m_shared.m_name);
					Image component2 = ((Component)((Transform)val2).Find("icon_bkg/icon")).GetComponent<Image>();
					component2.sprite = component.m_itemData.GetIcon();
					((Graphic)component2).color = new Color(0.2f, 0.2f, 0.2f, 0.8f);
					((Component)((Transform)val2).Find("name")).GetComponent<TMP_Text>().text = text + " <color=red>(Missing)</color>";
					((Component)((Transform)val2).Find("description")).GetComponent<TMP_Text>().text = "You have slain this beast, but the trophy eludes you...";
					___m_trophyList.Add(val);
					AddPinButtonToElement(val, current);
				}
			}
		}
		finally
		{
			((global::System.IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
		}
		float num2 = Mathf.Max(0f, 0f - num);
		___m_trophieListRoot.SetSizeWithCurrentAnchors((Axis)1, num2 + 100f);
	}

	private static void AddProgressText(RectTransform root, int collected, int total)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject("ProgressText");
		val.transform.SetParent(((Transform)root).parent, false);
		RectTransform val2 = val.AddComponent<RectTransform>();
		val2.anchoredPosition = new Vector2(20f, -20f);
		val2.anchorMin = new Vector2(0f, 1f);
		val2.anchorMax = new Vector2(0f, 1f);
		TMP_Text val3 = (TMP_Text)(object)val.AddComponent<TextMeshProUGUI>();
		val3.text = $"<color=orange>Trophies:</color> {collected} / {total}";
		val3.fontSize = 24f;
	}

	private static void AddPinButtonToElement(GameObject uiElement, string trophyPrefabName)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Expected O, but got Unknown
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: 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_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: Expected O, but got Unknown
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ec: Expected O, but got Unknown
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject("PinButton");
		val.transform.SetParent(uiElement.transform, false);
		RectTransform val2 = val.AddComponent<RectTransform>();
		val2.anchorMin = new Vector2(1f, 1f);
		val2.anchorMax = new Vector2(1f, 1f);
		val2.anchoredPosition = new Vector2(-15f, -15f);
		val2.sizeDelta = new Vector2(30f, 30f);
		Image img = val.AddComponent<Image>();
		((Graphic)img).color = (TrophyManager.PinnedTrophies.Contains(trophyPrefabName) ? Color.green : Color.gray);
		Button val3 = val.AddComponent<Button>();
		((UnityEvent)val3.onClick).AddListener((UnityAction)delegate
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			TrophyManager.TogglePin(trophyPrefabName);
			((Graphic)img).color = (TrophyManager.PinnedTrophies.Contains(trophyPrefabName) ? Color.green : Color.gray);
		});
		GameObject val4 = new GameObject("Text");
		val4.transform.SetParent(val.transform, false);
		TMP_Text val5 = (TMP_Text)(object)val4.AddComponent<TextMeshProUGUI>();
		val5.text = "*";
		((Graphic)val5).color = Color.white;
		val5.alignment = (TextAlignmentOptions)514;
	}
}
public static class TrophyManager
{
	public static HashSet<string> PinnedTrophies = new HashSet<string>();

	public static int TotalTrophiesInGame = -1;

	public static void TogglePin(string trophyPrefabName)
	{
		if (PinnedTrophies.Contains(trophyPrefabName))
		{
			PinnedTrophies.Remove(trophyPrefabName);
		}
		else
		{
			PinnedTrophies.Add(trophyPrefabName);
		}
		ProgressList.UpdatePinnedUI();
	}

	public static int GetTotalTrophies()
	{
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Invalid comparison between Unknown and I4
		if (TotalTrophiesInGame != -1)
		{
			return TotalTrophiesInGame;
		}
		TotalTrophiesInGame = 0;
		Enumerator<GameObject> enumerator = ObjectDB.instance.m_items.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				GameObject current = enumerator.Current;
				ItemDrop component = current.GetComponent<ItemDrop>();
				if ((Object)(object)component != (Object)null && (int)component.m_itemData.m_shared.m_itemType == 13)
				{
					TotalTrophiesInGame++;
				}
			}
		}
		finally
		{
			((global::System.IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
		}
		return TotalTrophiesInGame;
	}

	public static List<string> GetKilledButMissingTrophies()
	{
		//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0103: 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_0067: 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_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: Unknown result type (might be due to invalid IL or missing references)
		//IL_0157: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a8: Invalid comparison between Unknown and I4
		List<string> val = new List<string>();
		List<string> trophies = Player.m_localPlayer.GetTrophies();
		PlayerProfile playerProfile = Game.instance.GetPlayerProfile();
		HashSet<string> val2 = new HashSet<string>();
		for (int i = 0; i < 10; i++)
		{
			for (int j = 0; j < 5; j++)
			{
				if (playerProfile.m_playerStats[i].m_enemyStats[j] == null)
				{
					continue;
				}
				Enumerator<string, float> enumerator = playerProfile.m_playerStats[i].m_enemyStats[j].GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						KeyValuePair<string, float> current = enumerator.Current;
						if (current.Value > 0f)
						{
							val2.Add(current.Key);
						}
					}
				}
				finally
				{
					((global::System.IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
				}
			}
		}
		if ((Object)(object)ZNetScene.instance != (Object)null)
		{
			Enumerator<GameObject> enumerator2 = ZNetScene.instance.m_prefabs.GetEnumerator();
			try
			{
				while (enumerator2.MoveNext())
				{
					GameObject current2 = enumerator2.Current;
					string prefabName = Utils.GetPrefabName(current2);
					if (!val2.Contains(prefabName))
					{
						continue;
					}
					CharacterDrop component = current2.GetComponent<CharacterDrop>();
					if (!((Object)(object)component != (Object)null))
					{
						continue;
					}
					Enumerator<Drop> enumerator3 = component.m_drops.GetEnumerator();
					try
					{
						while (enumerator3.MoveNext())
						{
							Drop current3 = enumerator3.Current;
							if ((Object)(object)current3.m_prefab == (Object)null)
							{
								continue;
							}
							ItemDrop component2 = current3.m_prefab.GetComponent<ItemDrop>();
							if ((Object)(object)component2 != (Object)null && (int)component2.m_itemData.m_shared.m_itemType == 13)
							{
								string name = ((Object)current3.m_prefab).name;
								if (!trophies.Contains(name) && !val.Contains(name))
								{
									val.Add(name);
								}
							}
						}
					}
					finally
					{
						((global::System.IDisposable)enumerator3/*cast due to .constrained prefix*/).Dispose();
					}
				}
			}
			finally
			{
				((global::System.IDisposable)enumerator2/*cast due to .constrained prefix*/).Dispose();
			}
		}
		return val;
	}
}