Decompiled source of TrainingLevel v1.0.0

BepInEx/plugins/TrainingLevel/TrainingLevel.dll

Decompiled 5 hours ago
using System;
using System.Collections;
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 TMPro;
using UnityEngine;

[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("TrainingLevel")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+11fde89c7cc8ad9a9b4a9d9be327658002dfaae9")]
[assembly: AssemblyProduct("TrainingLevel")]
[assembly: AssemblyTitle("TrainingLevel")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace TrainingLevel;

[BepInPlugin("local.valheim.traininglevel", "Training Level", "1.0.0")]
public sealed class Plugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(EnemyHud), "UpdateHuds")]
	private static class NamePatch
	{
		[HarmonyPostfix]
		private static void Postfix(IDictionary ___m_huds)
		{
			if (___m_huds == null || HudName == null)
			{
				return;
			}
			foreach (DictionaryEntry ___m_hud in ___m_huds)
			{
				object key = ___m_hud.Key;
				Player val = (Player)((key is Player) ? key : null);
				if (val == null || (Object)(object)val == (Object)null)
				{
					continue;
				}
				ZNetView component = ((Component)val).GetComponent<ZNetView>();
				ZDO val2 = ((component != null) ? component.GetZDO() : null);
				float num = ((val2 != null) ? val2.GetFloat("TrainingLevel_level", -1f) : (-1f));
				if (!(num < 0f))
				{
					object? value = HudName.GetValue(___m_hud.Value);
					TMP_Text val3 = (TMP_Text)((value is TMP_Text) ? value : null);
					if (val3 != null && !((Object)(object)val3 == (Object)null))
					{
						val3.text = ((Character)val).GetHoverName() + "  <color=#E9C887>Lv " + LevelRules.Shown(num) + "</color>";
					}
				}
			}
		}
	}

	[Serializable]
	[CompilerGenerated]
	private sealed class <>c
	{
		public static readonly <>c <>9 = new <>c();

		public static ConsoleEvent <>9__6_0;

		internal void <Awake>b__6_0(ConsoleEventArgs args)
		{
			args.Context.AddString(Describe());
		}
	}

	private const string LevelKey = "TrainingLevel_level";

	private const string SocialLevelKey = "SkillsReworked_Level";

	private const float PublishInterval = 2f;

	private static int skillCount;

	private float nextPublish;

	private ConfigEntry<bool> showInSocial;

	private static readonly FieldInfo HudName = AccessTools.Field(AccessTools.Inner(typeof(EnemyHud), "HudData"), "m_name");

	private void Awake()
	{
		//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)
		//IL_0044: 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_004d: Invalid comparison between Unknown and I4
		//IL_007b: 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)
		//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Expected O, but got Unknown
		showInSocial = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShowInSocialSystem", true, "Show your training level as the Level in M2Valheim's SocialSystem (friends list, party window and party HUD).");
		foreach (SkillType value in Enum.GetValues(typeof(SkillType)))
		{
			if ((int)value != 0 && (int)value != 999)
			{
				skillCount++;
			}
		}
		new Harmony("local.valheim.traininglevel").PatchAll(typeof(Plugin).Assembly);
		object obj = <>c.<>9__6_0;
		if (obj == null)
		{
			ConsoleEvent val2 = delegate(ConsoleEventArgs args)
			{
				args.Context.AddString(Describe());
			};
			<>c.<>9__6_0 = val2;
			obj = (object)val2;
		}
		new ConsoleCommand("traininglevel", "show your training level and the experience behind it", (ConsoleEvent)obj, false, false, false, false, false, false, (ConsoleOptionsFetcher)null, false, false, false);
		((BaseUnityPlugin)this).Logger.LogInfo((object)$"Players show their training level, counting {skillCount} skills.");
	}

	private void Update()
	{
		if (Time.time < nextPublish)
		{
			return;
		}
		nextPublish = Time.time + 2f;
		Player localPlayer = Player.m_localPlayer;
		object obj;
		if (!((Object)(object)localPlayer != (Object)null))
		{
			obj = null;
		}
		else
		{
			ZNetView component = ((Component)localPlayer).GetComponent<ZNetView>();
			obj = ((component != null) ? component.GetZDO() : null);
		}
		ZDO val = (ZDO)obj;
		if (val != null && ((Component)localPlayer).GetComponent<ZNetView>().IsOwner())
		{
			float num = (float)CurrentLevel(localPlayer);
			if (Math.Abs(val.GetFloat("TrainingLevel_level", -1f) - num) > 0.001f)
			{
				val.Set("TrainingLevel_level", num);
			}
			int num2 = (showInSocial.Value ? LevelRules.Shown(num) : 0);
			if (val.GetInt("SkillsReworked_Level", 0) != num2)
			{
				val.Set("SkillsReworked_Level", num2);
			}
		}
	}

	private static IEnumerable<float> VanillaLevels(Player player)
	{
		Skills skills = ((Character)player).GetSkills();
		if ((Object)(object)skills == (Object)null)
		{
			yield break;
		}
		foreach (Skill skill2 in skills.GetSkillList())
		{
			SkillType skill = skill2.m_info.m_skill;
			if ((int)skill != 0 && (int)skill != 999 && Enum.IsDefined(typeof(SkillType), skill))
			{
				yield return skill2.m_level;
			}
		}
	}

	private static double CurrentLevel(Player player)
	{
		return LevelRules.Level(VanillaLevels(player), skillCount);
	}

	private static string Describe()
	{
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null)
		{
			return "Spawn in a world first.";
		}
		double num = LevelRules.TotalExperience(VanillaLevels(localPlayer));
		double num2 = LevelRules.MaxExperience(skillCount);
		return $"Training level {CurrentLevel(localPlayer):0.0}: {num:N0} of {num2:N0} experience across {skillCount} skills ({100.0 * num / num2:0.0}%).";
	}
}
internal static class LevelRules
{
	public const float MaxSkill = 100f;

	public static double StepCost(double level)
	{
		return Math.Pow(Math.Floor(level + 1.0), 1.5) * 0.5 + 0.5;
	}

	public static double ExperienceAt(double level)
	{
		if (double.IsNaN(level) || level <= 0.0)
		{
			return 0.0;
		}
		level = Math.Min(level, 100.0);
		double num = Math.Floor(level);
		double num2 = 0.0;
		for (int i = 1; (double)i <= num; i++)
		{
			num2 += Math.Pow(i, 1.5) * 0.5 + 0.5;
		}
		return num2 + (level - num) * StepCost(num);
	}

	public static double MaxExperience(int skillCount)
	{
		return (double)skillCount * ExperienceAt(100.0);
	}

	public static double Level(IEnumerable<float> skillLevels, int skillCount)
	{
		if (skillCount <= 0 || skillLevels == null)
		{
			return 0.0;
		}
		double num = 0.0;
		foreach (float skillLevel in skillLevels)
		{
			num += ExperienceAt(skillLevel);
		}
		return Math.Min(100.0, 100.0 * Math.Sqrt(num / MaxExperience(skillCount)));
	}

	public static double TotalExperience(IEnumerable<float> skillLevels)
	{
		double num = 0.0;
		if (skillLevels != null)
		{
			foreach (float skillLevel in skillLevels)
			{
				num += ExperienceAt(skillLevel);
			}
		}
		return num;
	}

	public static int Shown(double level)
	{
		if (!double.IsNaN(level) && !(level <= 0.0))
		{
			return (int)Math.Floor(level + 1E-09);
		}
		return 0;
	}
}