Decompiled source of SkillPeak v1.0.1

SkillPeak.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("SkillPeak")]
[assembly: AssemblyConfiguration("release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+d1a3be93a4fe3fa71a7bd725644530beef47ad88")]
[assembly: AssemblyProduct("SkillPeak")]
[assembly: AssemblyTitle("SkillPeak")]
[assembly: AssemblyVersion("1.0.0.0")]
[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 SkillPeak
{
	[BepInPlugin("net.gooop.valheim.skillpeak", "SkillPeak", "1.0.0")]
	public class SkillPeakPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "net.gooop.valheim.skillpeak";

		public const string PluginName = "SkillPeak";

		public const string PluginVersion = "1.0.0";

		internal static ConfigEntry<bool> ConfigEnabled;

		internal static readonly Dictionary<SkillType, float> MaxLevels = new Dictionary<SkillType, float>();

		private void Awake()
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			ConfigEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Show a tick mark on each skill bar for the highest level that skill has ever reached.");
			new Harmony("net.gooop.valheim.skillpeak").PatchAll(Assembly.GetExecutingAssembly());
		}

		internal static void RecordMax(SkillType type, float level)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			if (!MaxLevels.TryGetValue(type, out var value) || level > value)
			{
				MaxLevels[type] = level;
			}
		}

		internal static Transform FindChildRecursive(Transform parent, string name)
		{
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected O, but got Unknown
			if (((Object)parent).name == name)
			{
				return parent;
			}
			foreach (Transform item in parent)
			{
				Transform val = FindChildRecursive(item, name);
				if ((Object)(object)val != (Object)null)
				{
					return val;
				}
			}
			return null;
		}
	}
	[HarmonyPatch(typeof(Skill), "Raise")]
	public static class PatchSkillRaise
	{
		private static void Postfix(Skill __instance)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			SkillPeakPlugin.RecordMax(__instance.m_info.m_skill, __instance.m_level);
		}
	}
	[HarmonyPatch(typeof(SkillsDialog), "Setup")]
	public static class PatchSkillsDialogSetup
	{
		private const string TickName = "SkillPeak_Tick";

		private static readonly Color TickColor = new Color(1f, 0.93f, 0.6f, 0.95f);

		private const float TickWidth = 2f;

		private const float TickOverflow = 4f;

		private static readonly FieldRef<SkillsDialog, List<GameObject>> ElementsField = AccessTools.FieldRefAccess<SkillsDialog, List<GameObject>>("m_elements");

		private static void Postfix(SkillsDialog __instance, Player player)
		{
			//IL_0042: 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_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			if (!SkillPeakPlugin.ConfigEnabled.Value || (Object)(object)player == (Object)null)
			{
				return;
			}
			List<GameObject> list = ElementsField.Invoke(__instance);
			List<Skill> skillList = ((Character)player).GetSkills().GetSkillList();
			for (int i = 0; i < skillList.Count && i < list.Count; i++)
			{
				SkillType skill = skillList[i].m_info.m_skill;
				SkillPeakPlugin.RecordMax(skill, skillList[i].m_level);
				if (SkillPeakPlugin.MaxLevels.TryGetValue(skill, out var value) && !(value <= 0f))
				{
					GameObject val = list[i];
					Transform val2 = SkillPeakPlugin.FindChildRecursive(val.transform, "currentlevel");
					RectTransform val3 = (((Object)(object)val2 == (Object)null) ? null : ((Component)val2).GetComponent<RectTransform>());
					if (!((Object)(object)val3 == (Object)null))
					{
						PositionTick(val.transform, val3, Mathf.Clamp01(value / 100f));
					}
				}
			}
		}

		private static void PositionTick(Transform elementTransform, RectTransform barRect, float fraction)
		{
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: 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)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Expected O, but got Unknown
			//IL_006b: 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_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Expected O, but got Unknown
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_012e: Unknown result type (might be due to invalid IL or missing references)
			Transform val = SkillPeakPlugin.FindChildRecursive(elementTransform, "SkillPeak_Tick");
			RectTransform val2;
			if ((Object)(object)val != (Object)null)
			{
				val2 = (RectTransform)val;
			}
			else
			{
				GameObject val3 = new GameObject("SkillPeak_Tick", new Type[2]
				{
					typeof(RectTransform),
					typeof(Image)
				});
				val3.transform.SetParent(((Transform)barRect).parent, false);
				val2 = (RectTransform)val3.transform;
				val2.anchorMin = barRect.anchorMin;
				val2.anchorMax = barRect.anchorMax;
				val2.pivot = new Vector2(0f, 0.5f);
				Image component = val3.GetComponent<Image>();
				((Graphic)component).color = TickColor;
				((Graphic)component).raycastTarget = false;
			}
			((Transform)val2).SetAsLastSibling();
			Rect rect = barRect.rect;
			float width = ((Rect)(ref rect)).width;
			rect = barRect.rect;
			float height = ((Rect)(ref rect)).height;
			float num = barRect.anchoredPosition.x - barRect.pivot.x * width;
			float num2 = barRect.anchoredPosition.y + (0.5f - barRect.pivot.y) * height;
			val2.sizeDelta = new Vector2(2f, height + 4f);
			val2.anchoredPosition = new Vector2(num + fraction * width, num2);
		}
	}
	[HarmonyPatch(typeof(Player), "Save")]
	public static class PatchPlayerSave
	{
		private const string SaveKey = "SkillPeak_MaxLevels";

		private const int SaveVersion = 1;

		private static void Prefix(Player __instance)
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Expected O, but got Unknown
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Expected I4, but got Unknown
			if ((Object)(object)__instance == (Object)null || (Object)(object)Player.m_localPlayer != (Object)(object)__instance)
			{
				return;
			}
			ZPackage val = new ZPackage();
			val.Write(1);
			val.Write(SkillPeakPlugin.MaxLevels.Count);
			foreach (KeyValuePair<SkillType, float> maxLevel in SkillPeakPlugin.MaxLevels)
			{
				val.Write((int)maxLevel.Key);
				val.Write(maxLevel.Value);
			}
			__instance.m_customData["SkillPeak_MaxLevels"] = val.GetBase64();
		}
	}
	[HarmonyPatch(typeof(Player), "OnSpawned")]
	public static class PatchPlayerOnSpawned
	{
		private const string SaveKey = "SkillPeak_MaxLevels";

		private const int SaveVersion = 1;

		internal static bool Loaded;

		private static void Postfix(Player __instance)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			//IL_0092: 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_005d: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)__instance == (Object)null || (Object)(object)Player.m_localPlayer != (Object)(object)__instance || Loaded)
			{
				return;
			}
			if (__instance.m_customData.TryGetValue("SkillPeak_MaxLevels", out var value))
			{
				ZPackage val = new ZPackage(value);
				if (val.ReadInt() == 1)
				{
					int num = val.ReadInt();
					for (int i = 0; i < num; i++)
					{
						SkillType type = (SkillType)val.ReadInt();
						float level = val.ReadSingle();
						SkillPeakPlugin.RecordMax(type, level);
					}
				}
			}
			foreach (Skill skill in ((Character)__instance).GetSkills().GetSkillList())
			{
				SkillPeakPlugin.RecordMax(skill.m_info.m_skill, skill.m_level);
			}
			Loaded = true;
		}
	}
	[HarmonyPatch(typeof(Game), "Logout")]
	public static class PatchGameLogout
	{
		private static void Postfix()
		{
			PatchPlayerOnSpawned.Loaded = false;
			SkillPeakPlugin.MaxLevels.Clear();
		}
	}
}