using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using ComfyLib;
using HarmonyLib;
using UnityEngine;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("LicenseToSkill")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("LicenseToSkill")]
[assembly: AssemblyCopyright("Copyright © 2021")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("e3243d22-4307-4008-ba36-9f326008cde5")]
[assembly: AssemblyFileVersion("1.2.1")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.1.0")]
[module: UnverifiableCode]
namespace LicenseToSkill
{
[BepInPlugin("redseiko.valheim.comfytools.licensetoskill", "LicenseToSkill", "1.2.1")]
public class LicenseToSkill : BaseUnityPlugin
{
public const string PluginGUID = "redseiko.valheim.comfytools.licensetoskill";
public const string PluginName = "LicenseToSkill";
public const string PluginVersion = "1.2.1";
private Harmony _harmony;
public const float DefaultHardDeathCooldown = 600f;
public void Awake()
{
PluginConfig.BindConfig(((BaseUnityPlugin)this).Config);
_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "redseiko.valheim.comfytools.licensetoskill");
}
public void OnDestroy()
{
Harmony harmony = _harmony;
if (harmony != null)
{
harmony.UnpatchSelf();
}
}
public static void SetHardDeathCoolDown()
{
if (Object.op_Implicit((Object)(object)Player.m_localPlayer))
{
UpdateHardDeathCooldownTimer();
if (PluginConfig.IsModEnabled.Value)
{
Player.m_localPlayer.m_hardDeathCooldown = GetConfigHardDeathCooldown();
}
else
{
Player.m_localPlayer.m_hardDeathCooldown = 600f;
}
}
}
private static void UpdateHardDeathCooldownTimer()
{
if (Object.op_Implicit((Object)(object)Player.m_localPlayer) && ((Character)Player.m_localPlayer).m_seman != null)
{
StatusEffect val = ((Character)Player.m_localPlayer).m_seman.m_statusEffects.Where((StatusEffect x) => x.NameHash() == Player.s_statusEffectSoftDeath).FirstOrDefault();
if (!((Object)(object)val == (Object)null))
{
val.m_ttl = GetConfigHardDeathCooldown() - Player.m_localPlayer.m_timeSinceDeath;
}
}
}
public static float GetConfigHardDeathCooldown()
{
return PluginConfig.HardDeathCooldownOverride.Value * 60f;
}
}
[HarmonyPatch(typeof(Player))]
internal static class PlayerPatch
{
[HarmonyPostfix]
[HarmonyPatch("OnSpawned")]
private static void SetupAwakePostfix(ref Player __instance)
{
if (PluginConfig.IsModEnabled.Value && !((Object)(object)__instance != (Object)(object)Player.m_localPlayer))
{
__instance.m_hardDeathCooldown = LicenseToSkill.GetConfigHardDeathCooldown();
}
}
[HarmonyPostfix]
[HarmonyPatch("HardDeath")]
private static void HardDeathPostfix(ref Player __instance, ref bool __result)
{
if (PluginConfig.IsModEnabled.Value && !((Object)(object)__instance != (Object)(object)Player.m_localPlayer))
{
__result = __instance.m_timeSinceDeath > LicenseToSkill.GetConfigHardDeathCooldown();
}
}
}
[HarmonyPatch(typeof(SEMan))]
internal static class SEManPatch
{
[HarmonyPostfix]
[HarmonyPatch("AddStatusEffect", new Type[]
{
typeof(int),
typeof(bool),
typeof(int),
typeof(float)
})]
private static void AddStatusEffectPostfix(ref SEMan __instance, ref StatusEffect __result, int nameHash, bool resetTime)
{
if (PluginConfig.IsModEnabled.Value && Object.op_Implicit((Object)(object)__result) && !((Object)(object)__instance.m_character != (Object)(object)Player.m_localPlayer) && nameHash == Player.s_statusEffectSoftDeath)
{
__result.m_ttl = LicenseToSkill.GetConfigHardDeathCooldown();
}
}
}
[HarmonyPatch(typeof(Skills))]
internal static class SkillsPatch
{
[HarmonyPrefix]
[HarmonyPatch("LowerAllSkills")]
private static void LowerAllSkillsPrefix(ref Skills __instance, ref float factor)
{
if (PluginConfig.IsModEnabled.Value && (Object)(object)__instance.m_player == (Object)(object)Player.m_localPlayer)
{
factor = PluginConfig.SkillLossPercentOverride.Value * 0.01f;
}
}
}
public class PluginConfig
{
public static ConfigEntry<bool> IsModEnabled { get; private set; }
public static ConfigEntry<float> HardDeathCooldownOverride { get; private set; }
public static ConfigEntry<float> SkillLossPercentOverride { get; private set; }
public static void BindConfig(ConfigFile config)
{
//IL_0046: Unknown result type (might be due to invalid IL or missing references)
//IL_0050: Expected O, but got Unknown
//IL_007f: Unknown result type (might be due to invalid IL or missing references)
//IL_0089: Expected O, but got Unknown
IsModEnabled = config.Bind<bool>("_Global", "isModEnabled", true, "Globally enable or disable this mod.");
HardDeathCooldownOverride = config.Bind<float>("OnDeath", "hardDeathCooldownOverride", 20f, new ConfigDescription("Duration (in minutes) of the 'no skill loss' status effect after death.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(10f, 20f), Array.Empty<object>()));
SkillLossPercentOverride = config.Bind<float>("OnDeath", "skillLossPercentOverride", 1f, new ConfigDescription("Percentage of the skill's current level to lose on death.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 5f), Array.Empty<object>()));
IsModEnabled.OnSettingChanged<bool>(LicenseToSkill.SetHardDeathCoolDown);
HardDeathCooldownOverride.OnSettingChanged<float>(LicenseToSkill.SetHardDeathCoolDown);
}
}
}
namespace LicenseToSkill.Patches
{
[HarmonyPatch(typeof(StatusEffect))]
internal static class StatusEffectPatch
{
[HarmonyPrefix]
[HarmonyPatch("GetIconText")]
public static bool GetIconTextPrefix(ref StatusEffect __instance, ref string __result)
{
if (PluginConfig.IsModEnabled.Value || __instance.NameHash() != Player.s_statusEffectSoftDeath)
{
return true;
}
__result = "";
return false;
}
}
}
namespace ComfyLib
{
public static class ConfigFileExtensions
{
internal sealed class ConfigurationManagerAttributes
{
public Action<ConfigEntryBase> CustomDrawer;
public bool? Browsable;
public bool? HideDefaultButton;
public int? Order;
}
private static readonly Dictionary<string, int> _sectionToSettingOrder = new Dictionary<string, int>();
private static int GetSettingOrder(string section)
{
if (!_sectionToSettingOrder.TryGetValue(section, out var value))
{
value = 0;
}
_sectionToSettingOrder[section] = value - 1;
return value;
}
public static ConfigEntry<T> BindInOrder<T>(this ConfigFile config, string section, string key, T defaultValue, string description, AcceptableValueBase acceptableValues)
{
//IL_0028: Unknown result type (might be due to invalid IL or missing references)
//IL_0032: Expected O, but got Unknown
return config.Bind<T>(section, key, defaultValue, new ConfigDescription(description, acceptableValues, new object[1]
{
new ConfigurationManagerAttributes
{
Order = GetSettingOrder(section)
}
}));
}
public static ConfigEntry<T> BindInOrder<T>(this ConfigFile config, string section, string key, T defaultValue, string description, Action<ConfigEntryBase> customDrawer = null, bool browsable = true, bool hideDefaultButton = false, bool hideSettingName = false)
{
//IL_0049: Unknown result type (might be due to invalid IL or missing references)
//IL_0053: Expected O, but got Unknown
return config.Bind<T>(section, key, defaultValue, new ConfigDescription(description, (AcceptableValueBase)null, new object[1]
{
new ConfigurationManagerAttributes
{
Browsable = browsable,
CustomDrawer = customDrawer,
HideDefaultButton = hideDefaultButton,
Order = GetSettingOrder(section)
}
}));
}
public static void OnSettingChanged<T>(this ConfigEntry<T> configEntry, Action settingChangedHandler)
{
configEntry.SettingChanged += delegate
{
settingChangedHandler();
};
}
public static void OnSettingChanged<T>(this ConfigEntry<T> configEntry, Action<T> settingChangedHandler)
{
configEntry.SettingChanged += delegate(object _, EventArgs eventArgs)
{
//IL_0007: Unknown result type (might be due to invalid IL or missing references)
settingChangedHandler((T)((SettingChangedEventArgs)eventArgs).ChangedSetting.BoxedValue);
};
}
public static void OnSettingChanged<T>(this ConfigEntry<T> configEntry, Action<ConfigEntry<T>> settingChangedHandler)
{
configEntry.SettingChanged += delegate(object _, EventArgs eventArgs)
{
//IL_0007: Unknown result type (might be due to invalid IL or missing references)
settingChangedHandler((ConfigEntry<T>)((SettingChangedEventArgs)eventArgs).ChangedSetting.BoxedValue);
};
}
}
}