Decompiled source of Deathcount v0.0.1

plugins\Deathcount.dll

Decompiled 15 hours 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;
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.1")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.1.0")]
namespace Deathcount;

[BepInPlugin("de.sirskunkalot.Deathcount", "Deathcount", "0.0.1")]
[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.1";

	private static ConfigEntry<bool> EnabledConfig;

	private static ConfigEntry<float> PosXConfig;

	private static ConfigEntry<float> PosYConfig;

	private static ConfigEntry<int> FontSizeConfig;

	private static ConfigEntry<KeyCode> ToggleStatisticsUIConfig;

	private static ButtonConfig ToggleStatisticsUIButton;

	private static GameObject DeathcountUI;

	private static GameObject StatisticsUI;

	private void Awake()
	{
		//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0104: Unknown result type (might be due to invalid IL or missing references)
		//IL_010f: Unknown result type (might be due to invalid IL or missing references)
		//IL_011a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0126: 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;
			}
		};
		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)
	{
		//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_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: 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;
		}
	}

	private static void CreateDeathcountUI()
	{
		//IL_008c: 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_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
		if (GUIManager.Instance == null)
		{
			Logger.LogError((object)"GUIManager instance is null");
		}
		else if (!Object.op_Implicit((Object)(object)GUIManager.CustomGUIFront))
		{
			Logger.LogError((object)"GUIManager CustomGUI is null");
		}
		else if (Object.op_Implicit((Object)(object)DeathcountUI))
		{
			Logger.LogError((object)"DeathcountUI is not null");
		}
		else if (Object.op_Implicit((Object)(object)Game.instance))
		{
			DeathcountUI = GUIManager.Instance.CreateText($"Deaths: {Game.instance.GetPlayerProfile().m_playerStats[(PlayerStatType)0]}", GUIManager.CustomGUIBack.transform, new Vector2(0.5f, 1f), new Vector2(0.5f, 1f), new Vector2(PosXConfig.Value, PosYConfig.Value), GUIManager.Instance.NorseBold, FontSizeConfig.Value, GUIManager.Instance.ValheimOrange, true, Color.black, 100f, 50f, false);
			ContentSizeFitter obj = DeathcountUI.AddComponent<ContentSizeFitter>();
			obj.verticalFit = (FitMode)2;
			obj.horizontalFit = (FitMode)0;
			Text component = DeathcountUI.GetComponent<Text>();
			component.horizontalOverflow = (HorizontalWrapMode)1;
			component.verticalOverflow = (VerticalWrapMode)0;
			DeathcountUI.AddComponent<DragWindowCntrl>();
			DeathcountUI.SetActive(true);
		}
	}

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

	private static void DestroyDeathcountUI()
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002b: 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)
		if (!Object.op_Implicit((Object)(object)DeathcountUI))
		{
			Logger.LogError((object)"DeathcountUI is null");
			return;
		}
		PosXConfig.Value = ((RectTransform)DeathcountUI.transform).anchoredPosition.x;
		PosYConfig.Value = ((RectTransform)DeathcountUI.transform).anchoredPosition.y;
		DeathcountUI.SetActive(false);
		Object.Destroy((Object)(object)DeathcountUI);
	}

	private static void CreateStatisticsUI()
	{
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: 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_00c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: 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_0139: Unknown result type (might be due to invalid IL or missing references)
		//IL_013e: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a8: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01dc: 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_0260: Unknown result type (might be due to invalid IL or missing references)
		//IL_0265: Unknown result type (might be due to invalid IL or missing references)
		//IL_0270: Unknown result type (might be due to invalid IL or missing references)
		//IL_027c: Unknown result type (might be due to invalid IL or missing references)
		//IL_029c: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d6: Unknown result type (might be due to invalid IL or missing references)
		if (GUIManager.Instance == null)
		{
			Logger.LogError((object)"GUIManager instance is null");
		}
		else if (!Object.op_Implicit((Object)(object)GUIManager.CustomGUIFront))
		{
			Logger.LogError((object)"GUIManager CustomGUI is null");
		}
		else if (Object.op_Implicit((Object)(object)StatisticsUI))
		{
			Logger.LogError((object)"StatisticsUI is not null");
		}
		else
		{
			if (!Object.op_Implicit((Object)(object)Game.instance))
			{
				return;
			}
			StatisticsUI = GUIManager.Instance.CreateWoodpanel(GUIManager.CustomGUIFront.transform, new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(0f, 0f), 450f, 500f, false);
			StatisticsUI.SetActive(false);
			GUIManager.Instance.CreateText("Death Statistics", StatisticsUI.transform, new Vector2(0.5f, 1f), new Vector2(0.5f, 1f), new Vector2(45f, -45f), GUIManager.Instance.AveriaSerifBold, 30, GUIManager.Instance.ValheimOrange, true, Color.black, 300f, 40f, false);
			Transform obj = GUIManager.Instance.CreateScrollView(StatisticsUI.transform, false, true, 10f, 5f, GUIManager.Instance.ValheimScrollbarHandleColorBlock, Color.black, 300f, 350f).transform.Find("Scroll View/Viewport/Content");
			RectTransform val = (RectTransform)(object)((obj is RectTransform) ? obj : null);
			PlayerProfile playerProfile = Game.instance.GetPlayerProfile();
			if (playerProfile != null)
			{
				GUIManager.Instance.CreateText($"Deaths: {playerProfile.m_playerStats[(PlayerStatType)0]}", ((Component)val).transform, new Vector2(0.5f, 1f), new Vector2(0.5f, 1f), new Vector2(0f, 0f), GUIManager.Instance.AveriaSerif, 20, GUIManager.Instance.ValheimOrange, true, Color.black, 300f, 40f, false);
				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)
				{
					GUIManager.Instance.CreateText($"{item}: {playerProfile.m_playerStats[item]}", ((Component)val).transform, new Vector2(0.5f, 1f), new Vector2(0.5f, 1f), new Vector2(0f, 0f), GUIManager.Instance.AveriaSerif, 20, GUIManager.Instance.ValheimOrange, true, Color.black, 300f, 40f, false);
				}
			}
			StatisticsUI.SetActive(true);
			GUIManager.BlockInput(true);
		}
	}

	private static void DestroyStatisticsUI()
	{
		if (!Object.op_Implicit((Object)(object)StatisticsUI))
		{
			Logger.LogError((object)"StatisticsUI is null");
			return;
		}
		GUIManager.BlockInput(false);
		StatisticsUI.SetActive(false);
		Object.Destroy((Object)(object)StatisticsUI);
	}
}