Decompiled source of SkillInjector1point0fork v1.2.0

plugins/SkillInjector.dll

Decompiled a month ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("0.0.0.0")]
namespace Pipakin.SkillInjectorMod;

[BepInPlugin("com.pipakin.SkillInjectorMod", "SkillInjectorMod", "1.2.0")]
public class SkillInjector : BaseUnityPlugin
{
	private class SkillInfo
	{
		public SkillDef Def;

		public string Name;

		public SkillType Template;
	}

	[HarmonyPatch(typeof(Skills), "GetSkillDef")]
	private static class SkillInjectionPatch
	{
		private static void Postfix(SkillType type, ref SkillDef __result, List<SkillDef> ___m_skills)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			if (__result == null)
			{
				SkillDef skillDef = GetSkillDef(type, ___m_skills);
				if (skillDef != null)
				{
					___m_skills.Add(skillDef);
					__result = skillDef;
				}
			}
		}
	}

	[HarmonyPatch(typeof(Skills), "IsSkillValid")]
	private static class SkillValidPatch
	{
		private static void Postfix(SkillType type, ref bool __result)
		{
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Expected I4, but got Unknown
			if (!__result && Defs.ContainsKey((int)type))
			{
				__result = true;
			}
		}
	}

	[HarmonyPatch(typeof(Skills), "CheatRaiseSkill")]
	private static class CheatRaiseSkillPatch
	{
		private static bool Prefix(string name, float value, Skills __instance)
		{
			if (string.IsNullOrEmpty(name))
			{
				return true;
			}
			string text = name.ToLowerInvariant();
			foreach (KeyValuePair<int, SkillInfo> def in Defs)
			{
				if (!(def.Value.Name.ToLowerInvariant() != text))
				{
					object? obj = AccessTools.Method(typeof(Skills), "GetSkill", new Type[1] { typeof(SkillType) }, (Type[])null)?.Invoke(__instance, new object[1] { (object)(SkillType)def.Key });
					Skill val = (Skill)((obj is Skill) ? obj : null);
					if (val == null)
					{
						return false;
					}
					val.m_level += value;
					val.m_level = Mathf.Clamp(val.m_level, 0f, 100f);
					Player localPlayer = Player.m_localPlayer;
					if (localPlayer != null)
					{
						((Character)localPlayer).Message((MessageType)1, "Skill increased " + def.Value.Name + ": " + (int)val.m_level, 0, val.m_info?.m_icon, false);
					}
					Console instance = Console.instance;
					if (instance != null)
					{
						instance.Print("Skill " + def.Value.Name + " = " + val.m_level);
					}
					return false;
				}
			}
			return true;
		}
	}

	[HarmonyPatch(typeof(Skills), "CheatResetSkill")]
	private static class CheatResetSkillPatch
	{
		private static bool Prefix(string name, Skills __instance)
		{
			if (string.IsNullOrEmpty(name))
			{
				return true;
			}
			string text = name.ToLowerInvariant();
			foreach (KeyValuePair<int, SkillInfo> def in Defs)
			{
				if (!(def.Value.Name.ToLowerInvariant() != text))
				{
					__instance.ResetSkill((SkillType)def.Key);
					return false;
				}
			}
			return true;
		}
	}

	public const string PluginGuid = "com.pipakin.SkillInjectorMod";

	public const string PluginVersion = "1.2.0";

	private static readonly Dictionary<int, SkillInfo> Defs = new Dictionary<int, SkillInfo>();

	private Harmony _harmony;

	private void Awake()
	{
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Expected O, but got Unknown
		((BaseUnityPlugin)this).Config.Bind<int>("General", "NexusID", 341, "Nexus mod ID (original pipakin SkillInjector).");
		_harmony = new Harmony("com.pipakin.SkillInjectorMod");
		_harmony.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"SkillInjector 1.2.0 (Valheim 1.0) loaded. Registered skills wait for dependent mods.");
	}

	public static void RegisterNewSkill(int id, string name, string description, float increment, Sprite icon, SkillType template = (SkillType)0)
	{
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: 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_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Expected O, but got Unknown
		//IL_006b: 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)
		if (Defs.ContainsKey(id))
		{
			throw new Exception("Id " + id + " already in use by skill $skill_" + id);
		}
		Defs[id] = new SkillInfo
		{
			Def = new SkillDef
			{
				m_description = description,
				m_icon = icon,
				m_increseStep = increment,
				m_skill = (SkillType)id
			},
			Name = name,
			Template = template
		};
		TryAddLoc(id, name);
	}

	private static void TryAddLoc(int id, string name)
	{
		try
		{
			Type type = AccessTools.TypeByName("Localization");
			if (!(type == null))
			{
				object obj = AccessTools.Property(type, "instance")?.GetValue(null);
				if (obj != null)
				{
					AccessTools.Method(type, "AddWord", new Type[2]
					{
						typeof(string),
						typeof(string)
					}, (Type[])null)?.Invoke(obj, new object[2]
					{
						"skill_" + id,
						name
					});
				}
			}
		}
		catch
		{
		}
	}

	public static SkillDef GetSkillDef(SkillType type, List<SkillDef> skills = null)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0002: Expected I4, but got Unknown
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0036: Unknown result type (might be due to invalid IL or missing references)
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		int num = (int)type;
		if (!Defs.TryGetValue(num, out var value))
		{
			return null;
		}
		if ((int)value.Template != 0 && skills != null)
		{
			foreach (SkillDef skill in skills)
			{
				if (skill != null && skill.m_skill == value.Template)
				{
					if (value.Def.m_description == null)
					{
						value.Def.m_description = skill.m_description;
					}
					if ((Object)(object)value.Def.m_icon == (Object)null)
					{
						value.Def.m_icon = skill.m_icon;
					}
				}
			}
		}
		TryAddLoc(num, value.Name);
		return value.Def;
	}
}