Decompiled source of Deathcount v0.0.4

plugins\Deathcount.dll

Decompiled 3 weeks ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Jotunn.Configs;
using Jotunn.GUI;
using Jotunn.Managers;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Deathcount")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Deathcount")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("43d898ca-cdb6-4e14-8f06-391374411eb5")]
[assembly: AssemblyFileVersion("0.0.4")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.4.0")]
namespace Deathcount;

[BepInPlugin("de.sirskunkalot.Deathcount", "Deathcount", "0.0.4")]
[BepInDependency(/*Could not decode attribute arguments.*/)]
internal class Deathcount : BaseUnityPlugin
{
	public const string PluginGUID = "de.sirskunkalot.Deathcount";

	public const string PluginName = "Deathcount";

	public const string PluginVersion = "0.0.4";

	private static ConfigEntry<bool> EnabledConfig;

	private static ConfigEntry<float> PosXConfig;

	private static ConfigEntry<float> PosYConfig;

	private static ConfigEntry<int> FontSizeConfig;

	private static ConfigEntry<Color> FontColorConfig;

	private static ConfigEntry<Color> FontOutlineColorConfig;

	private static ConfigEntry<KeyCode> ToggleStatisticsUIConfig;

	private static ButtonConfig ToggleStatisticsUIButton;

	private static GameObject DeathcountUI;

	private static GameObject StatisticsUI;

	private void Awake()
	{
		//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_013d: 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_01a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c5: Expected O, but got Unknown
		EnabledConfig = ((BaseUnityPlugin)this).Config.Bind<bool>("Deathcount", "Enabled", true, "Deathcount is enabled and will show total deaths on screen upon entering a world");
		EnabledConfig.SettingChanged += delegate
		{
			if (EnabledConfig.Value)
			{
				if (!Object.op_Implicit((Object)(object)DeathcountUI))
				{
					CreateDeathcountUI();
				}
			}
			else
			{
				if (Object.op_Implicit((Object)(object)DeathcountUI))
				{
					DestroyDeathcountUI();
				}
				if (Object.op_Implicit((Object)(object)StatisticsUI))
				{
					DestroyStatisticsUI();
				}
			}
		};
		PosXConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Deathcount", "X Position", 0f, "X Position of the Deathcount display. Screen position will be saved automatically on logout when the display was dragged.");
		PosYConfig = ((BaseUnityPlugin)this).Config.Bind<float>("Deathcount", "Y Position", -45f, "Y Position of the Deathcount display. Screen position will be saved automatically on logout when the display was dragged.");
		FontSizeConfig = ((BaseUnityPlugin)this).Config.Bind<int>("Deathcount", "Font size", 30, "Size of the Deathcount display font.");
		FontSizeConfig.SettingChanged += delegate
		{
			if (Object.op_Implicit((Object)(object)DeathcountUI))
			{
				DeathcountUI.GetComponent<Text>().fontSize = FontSizeConfig.Value;
			}
		};
		FontColorConfig = ((BaseUnityPlugin)this).Config.Bind<Color>("Deathcount", "Font color", GUIManager.Instance.ValheimOrange, "Font color of the Deathcount display.");
		FontColorConfig.SettingChanged += delegate
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)DeathcountUI))
			{
				((Graphic)DeathcountUI.GetComponent<Text>()).color = FontColorConfig.Value;
			}
		};
		FontOutlineColorConfig = ((BaseUnityPlugin)this).Config.Bind<Color>("Deathcount", "Font outline color", Color.black, "Font outline color of the Deathcount display.");
		FontOutlineColorConfig.SettingChanged += delegate
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			if (Object.op_Implicit((Object)(object)DeathcountUI))
			{
				((Shadow)DeathcountUI.GetComponent<Outline>()).effectColor = FontOutlineColorConfig.Value;
			}
		};
		ToggleStatisticsUIConfig = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("Statistics", "Statistics UI Key", (KeyCode)291, "Key to show/hide the death statistics UI");
		ToggleStatisticsUIButton = new ButtonConfig
		{
			Name = "StatisticsUIKey",
			Config = ToggleStatisticsUIConfig,
			ActiveInCustomGUI = true
		};
		InputManager.Instance.AddButton("de.sirskunkalot.Deathcount", ToggleStatisticsUIButton);
		GUIManager.OnCustomGUIAvailable += delegate
		{
			if (EnabledConfig.Value)
			{
				CreateDeathcountUI();
			}
		};
		Harmony.CreateAndPatchAll(typeof(Deathcount), "de.sirskunkalot.Deathcount");
	}

	private void Update()
	{
		if (ZInput.instance != null && ZInput.GetButtonDown(ToggleStatisticsUIButton.Name))
		{
			if (!Object.op_Implicit((Object)(object)StatisticsUI))
			{
				CreateStatisticsUI();
			}
			else
			{
				DestroyStatisticsUI();
			}
		}
	}

	[HarmonyPatch(typeof(Player), "OnDeath")]
	[HarmonyPostfix]
	private static void PostfixPlayerOnDeath(Player __instance)
	{
		if ((Object)(object)__instance == (Object)(object)Player.m_localPlayer && Object.op_Implicit((Object)(object)DeathcountUI))
		{
			UpdateDeathcountUI();
		}
	}

	[HarmonyPatch(typeof(Game), "Shutdown")]
	[HarmonyPostfix]
	private static void PostfixGameShutdown(Game __instance)
	{
		if (Object.op_Implicit((Object)(object)DeathcountUI))
		{
			DestroyDeathcountUI();
		}
		if (Object.op_Implicit((Object)(object)StatisticsUI))
		{
			DestroyStatisticsUI();
		}
	}

	private static void CreateDeathcountUI()
	{
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: 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_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		if (GUIManager.Instance != null && Object.op_Implicit((Object)(object)GUIManager.CustomGUIBack) && !Object.op_Implicit((Object)(object)DeathcountUI) && Object.op_Implicit((Object)(object)Game.instance))
		{
			PlayerProfile playerProfile = Game.instance.GetPlayerProfile();
			if (playerProfile != null)
			{
				Transform val = GUIManager.CustomGUIBack.transform.parent.Find("PixelFix/IngameGui/HUD/hudroot");
				DeathcountUI = GUIManager.Instance.CreateText($"Deaths: {playerProfile.m_playerStats[(PlayerStatType)0]}", Object.op_Implicit((Object)(object)val) ? val : GUIManager.CustomGUIBack.transform, new Vector2(0.5f, 1f), new Vector2(0.5f, 1f), new Vector2(PosXConfig.Value, PosYConfig.Value), GUIManager.Instance.NorseBold, FontSizeConfig.Value, FontColorConfig.Value, true, FontOutlineColorConfig.Value, 100f, 50f, false);
				ContentSizeFitter obj = DeathcountUI.AddComponent<ContentSizeFitter>();
				obj.horizontalFit = (FitMode)2;
				obj.verticalFit = (FitMode)2;
				Text component = DeathcountUI.GetComponent<Text>();
				component.horizontalOverflow = (HorizontalWrapMode)1;
				component.verticalOverflow = (VerticalWrapMode)1;
				DeathcountUI.AddComponent<DragWindowCntrl>();
				DeathcountUI.SetActive(true);
			}
		}
	}

	private static void UpdateDeathcountUI()
	{
		if (Object.op_Implicit((Object)(object)DeathcountUI) && Object.op_Implicit((Object)(object)Game.instance))
		{
			PlayerProfile playerProfile = Game.instance.GetPlayerProfile();
			if (playerProfile != null)
			{
				DeathcountUI.GetComponent<Text>().text = $"Deaths: {playerProfile.m_playerStats[(PlayerStatType)0]}";
			}
		}
	}

	private static void DestroyDeathcountUI()
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		if (Object.op_Implicit((Object)(object)DeathcountUI))
		{
			PosXConfig.Value = ((RectTransform)DeathcountUI.transform).anchoredPosition.x;
			PosYConfig.Value = ((RectTransform)DeathcountUI.transform).anchoredPosition.y;
			DeathcountUI.SetActive(false);
			Object.Destroy((Object)(object)DeathcountUI);
			DeathcountUI = null;
		}
	}

	private static void CreateStatisticsUI()
	{
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Expected O, but got Unknown
		//IL_0081: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: 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_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0155: Expected O, but got Unknown
		//IL_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_022e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0233: Unknown result type (might be due to invalid IL or missing references)
		//IL_0239: Unknown result type (might be due to invalid IL or missing references)
		//IL_0245: Unknown result type (might be due to invalid IL or missing references)
		//IL_0262: Unknown result type (might be due to invalid IL or missing references)
		if (GUIManager.Instance == null || !Object.op_Implicit((Object)(object)GUIManager.CustomGUIFront) || Object.op_Implicit((Object)(object)StatisticsUI) || !Object.op_Implicit((Object)(object)Game.instance))
		{
			return;
		}
		PlayerProfile playerProfile = Game.instance.GetPlayerProfile();
		if (playerProfile == null)
		{
			return;
		}
		StatisticsUI = new GameObject("StatisticsUI", new Type[1] { typeof(RectTransform) });
		StatisticsUI.transform.SetParent(GUIManager.CustomGUIFront.transform, false);
		RectTransform val = (RectTransform)StatisticsUI.transform;
		val.anchorMin = new Vector2(0f, 0.5f);
		val.anchorMax = new Vector2(0f, 0.5f);
		val.pivot = new Vector2(0f, 0.5f);
		val.anchoredPosition = new Vector2(30f, 0f);
		Image obj = StatisticsUI.AddComponent<Image>();
		obj.type = (Type)1;
		((Graphic)obj).material = Cache.GetPrefab<Material>("litpanel");
		((Graphic)obj).color = Color.white;
		obj.sprite = GUIManager.Instance.GetSprite("woodpanel_trophys");
		VerticalLayoutGroup obj2 = StatisticsUI.AddComponent<VerticalLayoutGroup>();
		((HorizontalOrVerticalLayoutGroup)obj2).childForceExpandWidth = true;
		((HorizontalOrVerticalLayoutGroup)obj2).childForceExpandHeight = false;
		((LayoutGroup)obj2).childAlignment = (TextAnchor)0;
		((HorizontalOrVerticalLayoutGroup)obj2).spacing = 4f;
		((LayoutGroup)obj2).padding = new RectOffset(25, 25, 25, 25);
		ContentSizeFitter obj3 = StatisticsUI.AddComponent<ContentSizeFitter>();
		obj3.horizontalFit = (FitMode)2;
		obj3.verticalFit = (FitMode)2;
		AddStatText("Death Statistics", 30, GUIManager.Instance.NorseBold, (TextAnchor)4, GUIManager.Instance.ValheimOrange);
		AddSpacer(10f);
		AddStatText($"Deaths: {playerProfile.m_playerStats[(PlayerStatType)0]}", 18, GUIManager.Instance.AveriaSerif, (TextAnchor)3, Color.white);
		foreach (PlayerStatType item in from PlayerStatType e in Enum.GetValues(typeof(PlayerStatType))
			where ((object)(PlayerStatType)(ref e)).ToString().StartsWith("DeathB") || ((object)(PlayerStatType)(ref e)).ToString().StartsWith("Tombstone")
			orderby ((object)(PlayerStatType)(ref e)).ToString()
			select e)
		{
			AddStatText($"{item}: {playerProfile.m_playerStats[item]}", 18, GUIManager.Instance.AveriaSerif, (TextAnchor)3, Color.white);
		}
		StatisticsUI.SetActive(true);
		static void AddSpacer(float height)
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			GameObject val2 = new GameObject("Spacer", new Type[2]
			{
				typeof(RectTransform),
				typeof(LayoutElement)
			});
			val2.transform.SetParent(StatisticsUI.transform, false);
			val2.GetComponent<LayoutElement>().preferredHeight = height;
		}
		static void AddStatText(string text, int fontSize, Font font, TextAnchor alignment, Color color)
		{
			//IL_001a: 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_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			GUIManager.Instance.CreateText(text, StatisticsUI.transform, new Vector2(0f, 0.5f), new Vector2(0f, 0.5f), new Vector2(0f, 0f), font, fontSize, color, true, Color.black, 300f, 30f, false).GetComponent<Text>().alignment = alignment;
		}
	}

	private static void DestroyStatisticsUI()
	{
		if (Object.op_Implicit((Object)(object)StatisticsUI))
		{
			StatisticsUI.SetActive(false);
			Object.Destroy((Object)(object)StatisticsUI);
			StatisticsUI = null;
		}
	}
}