Decompiled source of SmartSkillRecovery v1.0.0

plugins\Oakwood.SmartSkillRecovery\Oakwood.SmartSkillRecovery.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[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("Oakwood.SmartSkillRecovery")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("Oakwood.SmartSkillRecovery")]
[assembly: AssemblyTitle("Oakwood.SmartSkillRecovery")]
[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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 Oakwood.SmartSkillRecovery
{
	[BepInPlugin("org.oakwood.smartskillrecovery", "SmartSkillRecovery", "1.0.0")]
	public sealed class SmartSkillRecoveryPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Skills), "RaiseSkill")]
		private static class RaiseSkillPatch
		{
			private static void Prefix(Skills __instance, SkillType skillType, ref float factor)
			{
				//IL_0035: Unknown result type (might be due to invalid IL or missing references)
				//IL_003d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0058: Unknown result type (might be due to invalid IL or missing references)
				//IL_0089: Unknown result type (might be due to invalid IL or missing references)
				//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
				//IL_00c8: Invalid comparison between Unknown and I4
				//IL_00f3: 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_0101: Invalid comparison between Unknown and I4
				if (!_enabled.Value || factor <= 0f)
				{
					return;
				}
				Player localPlayer = Player.m_localPlayer;
				if ((Object)(object)localPlayer == (Object)null || (Object)(object)((Character)localPlayer).GetSkills() != (Object)(object)__instance)
				{
					return;
				}
				float skillLevel = __instance.GetSkillLevel(skillType);
				UpdatePeakLevel(localPlayer, skillType, skillLevel);
				float num = 1f;
				if (_skillRecoveryBonus.Value > 0)
				{
					float peakLevel = GetPeakLevel(localPlayer, skillType);
					if (skillLevel < peakLevel)
					{
						num += (float)_skillRecoveryBonus.Value / 100f;
					}
				}
				if (_skillCatchUpBonus.Value > 0 && WeaponSkills.Contains(skillType))
				{
					float highestWeaponLevel = GetHighestWeaponLevel(__instance);
					if (skillLevel + 0.01f < highestWeaponLevel)
					{
						num += (float)_skillCatchUpBonus.Value / 100f;
					}
				}
				if (_swimmingExperienceBonus.Value > 0 && (int)skillType == 103)
				{
					num += (float)_swimmingExperienceBonus.Value / 100f;
				}
				if (_shieldAttackXpFactor.Value > 0 && WeaponSkills.Contains(skillType) && (int)skillType != 10)
				{
					foreach (StatusEffect statusEffect in ((Character)localPlayer).GetSEMan().GetStatusEffects())
					{
						SE_Shield val = (SE_Shield)(object)((statusEffect is SE_Shield) ? statusEffect : null);
						if (val != null && ShieldCasters.TryGetValue(val, out Player value) && (Object)(object)value != (Object)null && !((Character)value).IsDead())
						{
							float num2 = factor * ((float)_shieldAttackXpFactor.Value / 100f);
							if (num2 > 0f)
							{
								((Character)value).RaiseSkill((SkillType)10, num2);
							}
							break;
						}
					}
				}
				factor *= num;
			}

			private static void Postfix(Skills __instance, SkillType skillType)
			{
				//IL_002b: Unknown result type (might be due to invalid IL or missing references)
				//IL_002d: Unknown result type (might be due to invalid IL or missing references)
				if (_enabled.Value)
				{
					Player localPlayer = Player.m_localPlayer;
					if ((Object)(object)localPlayer != (Object)null && (Object)(object)((Character)localPlayer).GetSkills() == (Object)(object)__instance)
					{
						UpdatePeakLevel(localPlayer, skillType, __instance.GetSkillLevel(skillType));
					}
				}
			}
		}

		[HarmonyPatch(typeof(Skills), "LowerAllSkills")]
		private static class LowerAllSkillsPatch
		{
			private static readonly MethodInfo? GetSkillMethod = AccessTools.Method(typeof(Skills), "GetSkill", new Type[1] { typeof(SkillType) }, (Type[])null);

			private static void Prefix(Skills __instance, out (float level, float acc)? __state)
			{
				__state = null;
				if (_enabled.Value && _preventSwimmingSkillLoss.Value && !(GetSkillMethod == null))
				{
					object? obj = GetSkillMethod.Invoke(__instance, new object[1] { (object)(SkillType)103 });
					Skill val = (Skill)((obj is Skill) ? obj : null);
					if (val != null)
					{
						__state = (val.m_level, val.m_accumulator);
					}
				}
			}

			private static void Postfix(Skills __instance, (float level, float acc)? __state)
			{
				if (__state.HasValue && !(GetSkillMethod == null))
				{
					object? obj = GetSkillMethod.Invoke(__instance, new object[1] { (object)(SkillType)103 });
					Skill val = (Skill)((obj is Skill) ? obj : null);
					if (val != null)
					{
						val.m_level = __state.Value.level;
						val.m_accumulator = __state.Value.acc;
					}
				}
			}
		}

		[HarmonyPatch(typeof(Character), "Damage")]
		private static class CharacterDamagePatch
		{
			private static void Prefix(Character __instance, HitData hit)
			{
				if (!_enabled.Value || __instance.IsPlayer())
				{
					return;
				}
				Character attacker = hit.GetAttacker();
				Player val = (Player)(object)((attacker is Player) ? attacker : null);
				if (val == null || (Object)(object)val != (Object)(object)Player.m_localPlayer)
				{
					return;
				}
				BaseAI component = ((Component)__instance).GetComponent<BaseAI>();
				if (((Character)val).IsCrouching() && (!((Object)(object)component != (Object)null) || !component.IsAlerted()))
				{
					if (_sneakBonusDamage.Value > 0f)
					{
						float skillFactor = ((Character)val).GetSkillFactor((SkillType)101);
						float num = 1f + _sneakBonusDamage.Value / 100f * skillFactor;
						((DamageTypes)(ref hit.m_damage)).Modify(num);
					}
					if (_sneakBonusExperience.Value > 0f)
					{
						((Character)val).RaiseSkill((SkillType)101, _sneakBonusExperience.Value / 20f);
					}
				}
			}
		}

		[HarmonyPatch(typeof(Attack), "Start")]
		private static class AttackShieldContextPatch
		{
			private static void Prefix(Humanoid character)
			{
				Player val = (Player)(object)((character is Player) ? character : null);
				if (val != null)
				{
					_pendingShieldCaster = val;
				}
			}

			private static void Postfix()
			{
				_pendingShieldCaster = null;
			}
		}

		[HarmonyPatch(typeof(SE_Shield), "Setup")]
		private static class ShieldSetupPatch
		{
			private static void Postfix(SE_Shield __instance, Character character)
			{
				Player val = (Player)(((object)_pendingShieldCaster) ?? ((object)((character is Player) ? character : null)));
				if ((Object)(object)val != (Object)null)
				{
					ShieldCasters.Remove(__instance);
					ShieldCasters.Add(__instance, val);
				}
			}
		}

		[HarmonyPatch(typeof(SE_Shield), "IsDone")]
		private static class ShieldDonePatch
		{
			private static readonly FieldInfo? DamageField = AccessTools.Field(typeof(SE_Shield), "m_damage");

			private static readonly FieldInfo? TotalAbsorbField = AccessTools.Field(typeof(SE_Shield), "m_totalAbsorbDamage");

			private static void Postfix(SE_Shield __instance, bool __result)
			{
				if (_enabled.Value && _removeShieldExpiredXp.Value && __result)
				{
					float num = ((DamageField != null) ? ((float)DamageField.GetValue(__instance)) : 0f);
					float num2 = ((TotalAbsorbField != null) ? ((float)TotalAbsorbField.GetValue(__instance)) : 0f);
					if (num < num2)
					{
						__instance.m_levelUpSkillFactor = 0f;
					}
				}
			}
		}

		public const string PluginGuid = "org.oakwood.smartskillrecovery";

		public const string PluginName = "SmartSkillRecovery";

		public const string PluginVersion = "1.0.0";

		private const string PeakKeyPrefix = "SmartSkillRecovery_Peak_";

		private static readonly HashSet<SkillType> WeaponSkills = new HashSet<SkillType>
		{
			(SkillType)1,
			(SkillType)2,
			(SkillType)3,
			(SkillType)4,
			(SkillType)5,
			(SkillType)7,
			(SkillType)8,
			(SkillType)9,
			(SkillType)10,
			(SkillType)11,
			(SkillType)14
		};

		private static ConfigEntry<bool> _enabled = null;

		private static ConfigEntry<int> _skillRecoveryBonus = null;

		private static ConfigEntry<int> _skillCatchUpBonus = null;

		private static ConfigEntry<bool> _preventSwimmingSkillLoss = null;

		private static ConfigEntry<int> _swimmingExperienceBonus = null;

		private static ConfigEntry<int> _shieldAttackXpFactor = null;

		private static ConfigEntry<bool> _removeShieldExpiredXp = null;

		private static ConfigEntry<float> _sneakBonusDamage = null;

		private static ConfigEntry<float> _sneakBonusExperience = null;

		private static readonly ConditionalWeakTable<SE_Shield, Player> ShieldCasters = new ConditionalWeakTable<SE_Shield, Player>();

		private static Player? _pendingShieldCaster;

		private readonly Harmony _harmony = new Harmony("org.oakwood.smartskillrecovery");

		private void Awake()
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Expected O, but got Unknown
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Expected O, but got Unknown
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Expected O, but got Unknown
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Expected O, but got Unknown
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_016d: Expected O, but got Unknown
			//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01aa: Expected O, but got Unknown
			_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1 - General", "Enabled", true, "Enable SmartSkillRecovery.");
			_skillRecoveryBonus = ((BaseUnityPlugin)this).Config.Bind<int>("1 - General", "Skill recovery bonus", 100, new ConfigDescription("Bonus XP in percent skills gain until they reach their highest recorded level again.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 200), Array.Empty<object>()));
			_skillCatchUpBonus = ((BaseUnityPlugin)this).Config.Bind<int>("2 - Weapons", "Skill catch up bonus", 75, new ConfigDescription("Bonus XP in percent weapon skills gain until they have caught up to the player's highest weapon skill level.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 200), Array.Empty<object>()));
			_preventSwimmingSkillLoss = ((BaseUnityPlugin)this).Config.Bind<bool>("3 - Swimming", "Prevent swimming skill loss", true, "If on, the swimming skill will no longer lose XP on death.");
			_swimmingExperienceBonus = ((BaseUnityPlugin)this).Config.Bind<int>("3 - Swimming", "Swimming experience bonus", 115, new ConfigDescription("Bonus XP in percent the swimming skill gets.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 200), Array.Empty<object>()));
			_shieldAttackXpFactor = ((BaseUnityPlugin)this).Config.Bind<int>("4 - Blood Magic", "Shield attack XP factor", 44, new ConfigDescription("The caster of a shield gains this percentage of the weapon XP the shielded attacker gets.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), Array.Empty<object>()));
			_removeShieldExpiredXp = ((BaseUnityPlugin)this).Config.Bind<bool>("4 - Blood Magic", "Remove shield expired XP", true, "If on, the shielded player will not gain Blood Magic XP when the shield expires by timeout.");
			_sneakBonusDamage = ((BaseUnityPlugin)this).Config.Bind<float>("5 - Sneak", "Sneak bonus damage", 50f, new ConfigDescription("Bonus damage in percent unaware enemies take if attacked while sneaking at skill level 100.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 200f), Array.Empty<object>()));
			_sneakBonusExperience = ((BaseUnityPlugin)this).Config.Bind<float>("5 - Sneak", "Sneak bonus experience", 20f, new ConfigDescription("Bonus experience factor the sneak skill gains when attacking an unaware enemy while sneaking.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 200f), Array.Empty<object>()));
			_harmony.PatchAll(typeof(RaiseSkillPatch));
			_harmony.PatchAll(typeof(LowerAllSkillsPatch));
			_harmony.PatchAll(typeof(CharacterDamagePatch));
			_harmony.PatchAll(typeof(AttackShieldContextPatch));
			_harmony.PatchAll(typeof(ShieldSetupPatch));
			_harmony.PatchAll(typeof(ShieldDonePatch));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"SmartSkillRecovery v1.0.0 loaded.");
		}

		private void OnDestroy()
		{
			_harmony.UnpatchSelf();
		}

		private static float GetPeakLevel(Player player, SkillType skillType)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Expected I4, but got Unknown
			string key = "SmartSkillRecovery_Peak_" + (int)skillType;
			if (player.m_customData != null && player.m_customData.TryGetValue(key, out var value) && float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var result))
			{
				return result;
			}
			return 0f;
		}

		private static void UpdatePeakLevel(Player player, SkillType skillType, float currentLevel)
		{
			//IL_000a: 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_0022: Expected I4, but got Unknown
			if (player.m_customData != null)
			{
				float peakLevel = GetPeakLevel(player, skillType);
				if (currentLevel > peakLevel)
				{
					player.m_customData["SmartSkillRecovery_Peak_" + (int)skillType] = currentLevel.ToString(CultureInfo.InvariantCulture);
				}
			}
		}

		private static float GetHighestWeaponLevel(Skills skills)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			float num = 0f;
			foreach (SkillType weaponSkill in WeaponSkills)
			{
				float skillLevel = skills.GetSkillLevel(weaponSkill);
				if (skillLevel > num)
				{
					num = skillLevel;
				}
			}
			return num;
		}
	}
}