Please disclose if any significant portion of your mod was created using AI tools by adding the 'AI Generated' category. Failing to do so may result in the mod being removed from Thunderstore.
Decompiled source of Kratyr Tweaks v0.2.0
BepInEx/plugins/KratyrTweaks.dll
Decompiled a day agousing 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 BepInEx.Logging; using HarmonyLib; using KratyrTweaks.Core; using KratyrTweaks.Features; using Microsoft.CodeAnalysis; 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: AssemblyCompany("KratyrTweaks")] [assembly: AssemblyConfiguration("Release")] [assembly: AssemblyFileVersion("0.2.0.0")] [assembly: AssemblyInformationalVersion("0.2.0")] [assembly: AssemblyProduct("KratyrTweaks")] [assembly: AssemblyTitle("KratyrTweaks")] [assembly: AssemblyVersion("0.2.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 KratyrTweaks.Features { internal static class HudVisibility { private static readonly int Visible = Animator.StringToHash("Visible"); internal static int Install(Harmony harmony, ManualLogSource log) { Type type = AccessTools.TypeByName("Hud"); if (type == null) { log.LogWarning((object)"Valheim Hud type not found; HUD tweaks disabled."); return 0; } int num = 0; if (InstallOne(harmony, log, type, "UpdateStamina", "m_staminaAnimator", "StaminaPostfix")) { num++; } if (InstallOne(harmony, log, type, "UpdateEitr", "m_eitrAnimator", "EitrPostfix")) { num++; } return num; } private static bool InstallOne(Harmony harmony, ManualLogSource log, Type hud, string methodName, string animatorName, string postfixName) { //IL_00a9: Unknown result type (might be due to invalid IL or missing references) //IL_00ae: Unknown result type (might be due to invalid IL or missing references) //IL_00bd: Expected O, but got Unknown try { MethodInfo methodInfo = AccessTools.DeclaredMethod(hud, methodName, new Type[2] { typeof(Player), typeof(float) }, (Type[])null); FieldInfo fieldInfo = AccessTools.Field(hud, animatorName); if (methodInfo == null || fieldInfo == null || fieldInfo.FieldType != typeof(Animator)) { log.LogWarning((object)("HUD tweak skipped: " + methodName + " or " + animatorName + " unavailable.")); return false; } MethodInfo methodInfo2 = AccessTools.Method(typeof(HudVisibility), postfixName, (Type[])null, (Type[])null); harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(methodInfo2) { priority = 0 }, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); return true; } catch (Exception arg) { log.LogWarning((object)$"HUD tweak skipped: {methodName}: {arg}"); return false; } } private static void StaminaPostfix(Animator ___m_staminaAnimator) { if (Plugin.AlwaysShowStamina.Value) { KeepVisible(___m_staminaAnimator); } } private static void EitrPostfix(Player __0, Animator ___m_eitrAnimator) { if (Plugin.AlwaysShowEitr.Value && Object.op_Implicit((Object)(object)__0) && ((Character)__0).GetMaxEitr() > 0f) { KeepVisible(___m_eitrAnimator); } } private static void KeepVisible(Animator animator) { if ((Object)(object)animator != (Object)null && ((Behaviour)animator).isActiveAndEnabled && (Object)(object)animator.runtimeAnimatorController != (Object)null) { animator.SetBool(Visible, true); } } } internal static class TombstoneEquip { private enum Slot { None, Head, Chest, Legs, Cape, Utility, Trinket, Weapon, Shield } private sealed class DeathEquipment { internal long Owner; internal HashSet<ItemData> Items; } private sealed class Recovery { internal Player Player; internal Inventory Inventory; internal HashSet<ItemData> AlreadyHeld; internal string GraveMarker; } private const string Marker = "KratyrTweaks.TombstoneEquipment"; private static readonly int MarkerKey = StringExtensionMethods.GetStableHashCode("KratyrTweaks.TombstoneEquipment"); private static Action<Container> saveContainer; [ThreadStatic] private static DeathEquipment dying; internal static bool Install(Harmony harmony, ManualLogSource log) { MethodInfo methodInfo = AccessTools.DeclaredMethod(typeof(Player), "OnDeath", Type.EmptyTypes, (Type[])null); MethodInfo methodInfo2 = AccessTools.DeclaredMethod(typeof(TombStone), "Setup", new Type[2] { typeof(string), typeof(long) }, (Type[])null); MethodInfo methodInfo3 = AccessTools.DeclaredMethod(typeof(Container), "RPC_TakeAllResponse", new Type[2] { typeof(long), typeof(bool) }, (Type[])null); MethodInfo methodInfo4 = AccessTools.DeclaredMethod(typeof(InventoryGui), "OnTakeAll", Type.EmptyTypes, (Type[])null); MethodInfo methodInfo5 = AccessTools.DeclaredMethod(typeof(Container), "Save", Type.EmptyTypes, (Type[])null); if (methodInfo == null || methodInfo2 == null || methodInfo3 == null || methodInfo4 == null || methodInfo5 == null) { log.LogWarning((object)"Tombstone auto-equip disabled: a required Valheim method is unavailable."); return false; } try { saveContainer = AccessTools.MethodDelegate<Action<Container>>(methodInfo5, (object)null, true); HarmonyMethod val = Hook("CaptureEquipment"); val.priority = 800; harmony.Patch((MethodBase)methodInfo, val, (HarmonyMethod)null, (HarmonyMethod)null, Hook("EndDeath"), (HarmonyMethod)null); harmony.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, Hook("MarkGrave"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); harmony.Patch((MethodBase)methodInfo3, Hook("BeginQuickPickup"), Hook("FinishPickup"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); harmony.Patch((MethodBase)methodInfo4, Hook("BeginManualPickup"), Hook("FinishPickup"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); return true; } catch (Exception ex) { log.LogWarning((object)("Tombstone auto-equip unavailable: " + ex.Message)); return false; } } private static HarmonyMethod Hook(string name) { //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_0012: Expected O, but got Unknown return new HarmonyMethod(typeof(TombstoneEquip), name, (Type[])null); } private static void CaptureEquipment(Player __instance, out DeathEquipment __state) { __state = dying; dying = null; if ((Object)(object)__instance != (Object)(object)Player.m_localPlayer || (TombstonePolicy.Effective & EquipPolicy.Enabled) == 0) { return; } HashSet<ItemData> hashSet = new HashSet<ItemData>(); foreach (ItemData allItem in ((Humanoid)__instance).GetInventory().GetAllItems()) { if (GetSlot(allItem) != Slot.None && allItem.m_shared.m_maxStackSize == 1 && ((Humanoid)__instance).IsItemEquiped(allItem)) { hashSet.Add(allItem); } } if (hashSet.Count != 0) { dying = new DeathEquipment { Owner = __instance.GetPlayerID(), Items = hashSet }; } } private static void EndDeath(DeathEquipment __state) { dying = __state; } private static void MarkGrave(long __1, Container ___m_container, ZNetView ___m_nview) { if (dying == null || __1 != dying.Owner || !Object.op_Implicit((Object)(object)___m_container) || !Object.op_Implicit((Object)(object)___m_nview) || !___m_nview.IsValid() || !___m_nview.IsOwner()) { return; } string text = null; foreach (ItemData allItem in ___m_container.GetInventory().GetAllItems()) { if (dying.Items.Contains(allItem)) { if (text == null) { text = Guid.NewGuid().ToString("N"); } allItem.m_customData["KratyrTweaks.TombstoneEquipment"] = text; } } if (text != null) { ___m_nview.GetZDO().Set(MarkerKey, text); saveContainer(___m_container); } } private static void BeginQuickPickup(Container __instance, bool __1, out Recovery __state) { __state = (__1 ? BeginPickup(__instance, manual: false) : null); } private static void BeginManualPickup(Container ___m_currentContainer, out Recovery __state) { __state = BeginPickup(___m_currentContainer, manual: true); } private static Recovery BeginPickup(Container container, bool manual) { EquipPolicy policy = TombstonePolicy.Effective; Player localPlayer = Player.m_localPlayer; if ((policy & EquipPolicy.Enabled) == 0 || (manual && (policy & EquipPolicy.ManualTakeAll) == 0) || !Object.op_Implicit((Object)(object)localPlayer) || ((Character)localPlayer).IsDead() || ((Character)localPlayer).IsTeleporting() || !Object.op_Implicit((Object)(object)container) || !Object.op_Implicit((Object)(object)((Component)container).GetComponent<TombStone>())) { return null; } ZNetView component = ((Component)container).GetComponent<ZNetView>(); if (!Object.op_Implicit((Object)(object)component) || !component.IsValid()) { return null; } ZDO zDO = component.GetZDO(); if (zDO.GetLong(ZDOVars.s_owner, 0L) != localPlayer.GetPlayerID()) { return null; } string marker = zDO.GetString(MarkerKey, ""); if (!container.GetInventory().GetAllItems().Exists((ItemData item) => Marked(item, marker) || ((policy & EquipPolicy.CarryWeightGear) != EquipPolicy.Off && AddsCarryWeight(item)))) { return null; } Inventory inventory = ((Humanoid)localPlayer).GetInventory(); return new Recovery { Player = localPlayer, Inventory = inventory, GraveMarker = marker, AlreadyHeld = new HashSet<ItemData>(inventory.GetAllItems()) }; } private static bool Marked(ItemData item, string marker) { if (!string.IsNullOrEmpty(marker) && item.m_customData.TryGetValue("KratyrTweaks.TombstoneEquipment", out var value)) { return value == marker; } return false; } private static bool AddsCarryWeight(ItemData item) { if (item.m_shared.m_maxStackSize == 1) { StatusEffect equipStatusEffect = item.m_shared.m_equipStatusEffect; SE_Stats val = (SE_Stats)(object)((equipStatusEffect is SE_Stats) ? equipStatusEffect : null); if (val != null) { return val.m_addMaxCarryWeight > 0f; } } return false; } private static void FinishPickup(Recovery __state) { if (__state == null || (Object)(object)__state.Player != (Object)(object)Player.m_localPlayer) { return; } Player player = __state.Player; Inventory inventory = __state.Inventory; EquipPolicy effective = TombstonePolicy.Effective; List<(ItemData, Slot, bool)> list = new List<(ItemData, Slot, bool)>(); HashSet<Slot> hashSet = new HashSet<Slot>(); foreach (ItemData allItem in inventory.GetAllItems()) { Slot slot = GetSlot(allItem); if (!__state.AlreadyHeld.Contains(allItem)) { bool flag = Marked(allItem, __state.GraveMarker); if (flag) { allItem.m_customData.Remove("KratyrTweaks.TombstoneEquipment"); } if (slot != Slot.None && ShouldEquip(allItem, slot, flag, effective)) { list.Add((allItem, slot, flag)); } } if (((Humanoid)player).IsItemEquiped(allItem)) { hashSet.Add(slot); } } if ((effective & EquipPolicy.Enabled) == 0 || ((Character)player).IsDead()) { return; } bool flag2 = !hashSet.Contains(Slot.Weapon) && !hashSet.Contains(Slot.Shield); list.Sort(((ItemData Item, Slot Slot, bool Worn) a, (ItemData Item, Slot Slot, bool Worn) b) => (a.Worn != b.Worn) ? ((!a.Worn) ? 1 : (-1)) : a.Slot.CompareTo(b.Slot)); foreach (var (val, slot2, _) in list) { if (!hashSet.Contains(slot2) && inventory.ContainsItem(val) && ((slot2 != Slot.Weapon && slot2 != Slot.Shield) || flag2) && ((Humanoid)player).EquipItem(val, false)) { hashSet.Add(slot2); } } } private static bool ShouldEquip(ItemData item, Slot slot, bool worn, EquipPolicy policy) { if (slot == Slot.Weapon || slot == Slot.Shield) { if (worn) { return (policy & EquipPolicy.HeldGear) != 0; } return false; } if (!worn || (policy & EquipPolicy.WornGear) == 0) { if ((policy & EquipPolicy.CarryWeightGear) != EquipPolicy.Off) { return AddsCarryWeight(item); } return false; } return true; } private static Slot GetSlot(ItemData item) { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000b: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_000e: Unknown result type (might be due to invalid IL or missing references) //IL_006c: Expected I4, but got Unknown ItemType itemType = item.m_shared.m_itemType; switch (itemType - 3) { case 3: return Slot.Head; case 4: return Slot.Chest; case 8: return Slot.Legs; case 14: return Slot.Cape; case 15: return Slot.Utility; case 21: return Slot.Trinket; case 2: return Slot.Shield; case 0: case 1: case 11: case 12: case 16: case 19: return Slot.Weapon; default: return Slot.None; } } } [Flags] internal enum EquipPolicy { Off = 0, Enabled = 1, WornGear = 2, HeldGear = 4, ManualTakeAll = 8, CarryWeightGear = 0x10, All = 0x1F } internal static class TombstonePolicy { private const string RequestRpc = "KratyrTweaks_TombstonePolicyRequest_v1"; private const string PolicyRpc = "KratyrTweaks_TombstonePolicy_v1"; private static ConfigEntry<bool> enabled; private static ConfigEntry<bool> wornGear; private static ConfigEntry<bool> heldGear; private static ConfigEntry<bool> manualTakeAll; private static ConfigEntry<bool> carryWeightGear; private static readonly HashSet<long> subscribers = new HashSet<long>(); private static ZRoutedRpc router; private static ZNet session; private static EquipPolicy remote; private static long requestedHost; private static volatile bool settingsChanged; private static bool available; internal static EquipPolicy Effective { get { ZNet instance = ZNet.instance; if (!available || !Object.op_Implicit((Object)(object)instance) || (Object)(object)instance != (Object)(object)session) { return EquipPolicy.Off; } if (instance.IsServer()) { return HostPolicy; } if (requestedHost == 0L || instance.GetServerPeer()?.m_uid != requestedHost) { return EquipPolicy.Off; } return remote; } } private static EquipPolicy HostPolicy { get { if (enabled.Value) { return (EquipPolicy)(1 | (wornGear.Value ? 2 : 0) | (heldGear.Value ? 4 : 0) | (manualTakeAll.Value ? 8 : 0) | (carryWeightGear.Value ? 16 : 0)); } return EquipPolicy.Off; } } internal static void Install(ConfigFile config, Harmony harmony, ManualLogSource log) { enabled = config.Bind<bool>("Tombstone Auto Equip", "Enabled", false, "Host: restore previously equipped gear on recovery of your own tombstone."); wornGear = config.Bind<bool>("Tombstone Auto Equip", "RestoreWornGear", true, "Host: restore armor, capes, utility items and trinkets."); heldGear = config.Bind<bool>("Tombstone Auto Equip", "RestoreHeldGear", false, "Host: restore previously equipped weapons, shields and tools."); carryWeightGear = config.Bind<bool>("Tombstone Auto Equip", "EquipCarryWeightGear", true, "Host: equip recovered carry-weight gear even when it was not worn at death."); manualTakeAll = config.Bind<bool>("Tombstone Auto Equip", "IncludeManualTakeAll", true, "Host: apply the same policy to the Take All button in an open tombstone."); MethodInfo methodInfo = AccessTools.DeclaredMethod(typeof(ZNet), "Awake", Type.EmptyTypes, (Type[])null); MethodInfo methodInfo2 = AccessTools.DeclaredMethod(typeof(ZNet), "RPC_PeerInfo", new Type[2] { typeof(ZRpc), typeof(ZPackage) }, (Type[])null); MethodInfo methodInfo3 = AccessTools.DeclaredMethod(typeof(ZNet), "OnDestroy", Type.EmptyTypes, (Type[])null); if (methodInfo == null || methodInfo2 == null || methodInfo3 == null) { log.LogWarning((object)"Tombstone auto-equip disabled: network lifecycle methods unavailable."); return; } try { harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, Hook("NetworkAwake"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); harmony.Patch((MethodBase)methodInfo2, (HarmonyMethod)null, Hook("PeerReady"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); harmony.Patch((MethodBase)methodInfo3, Hook("NetworkDestroy"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null); enabled.SettingChanged += Changed; wornGear.SettingChanged += Changed; heldGear.SettingChanged += Changed; manualTakeAll.SettingChanged += Changed; carryWeightGear.SettingChanged += Changed; available = true; log.LogInfo((object)"Tombstone auto-equip available. Configure it under [Tombstone Auto Equip] on the host."); } catch (Exception ex) { log.LogWarning((object)("Tombstone auto-equip policy unavailable: " + ex.Message)); } } private static HarmonyMethod Hook(string name) { //IL_000c: Unknown result type (might be due to invalid IL or missing references) //IL_0012: Expected O, but got Unknown return new HarmonyMethod(typeof(TombstonePolicy), name, (Type[])null); } private static void Changed(object sender, EventArgs args) { settingsChanged = true; } private static void NetworkAwake(ZNet __instance) { if (available && ZRoutedRpc.instance != null && router != ZRoutedRpc.instance) { session = __instance; router = ZRoutedRpc.instance; router.Register("KratyrTweaks_TombstonePolicyRequest_v1", (Action<long>)ReceiveRequest); router.Register<int>("KratyrTweaks_TombstonePolicy_v1", (Action<long, int>)ReceivePolicy); } } private static void PeerReady(ZNet __instance) { if (!available || __instance.IsServer()) { return; } ZNetPeer serverPeer = __instance.GetServerPeer(); if (serverPeer != null && serverPeer.IsReady() && serverPeer.m_uid != 0L && serverPeer.m_uid != requestedHost) { NetworkAwake(__instance); if (router != null && !((Object)(object)session != (Object)(object)__instance)) { remote = EquipPolicy.Off; requestedHost = serverPeer.m_uid; router.InvokeRoutedRPC(requestedHost, "KratyrTweaks_TombstonePolicyRequest_v1", Array.Empty<object>()); } } } private static void ReceiveRequest(long sender) { ZNet instance = ZNet.instance; if (available && Object.op_Implicit((Object)(object)instance) && !((Object)(object)instance != (Object)(object)session) && instance.IsServer()) { ZNetPeer peer = instance.GetPeer(sender); if (peer != null && peer.IsReady()) { subscribers.Add(sender); router.InvokeRoutedRPC(sender, "KratyrTweaks_TombstonePolicy_v1", new object[1] { (int)HostPolicy }); } } } private static void ReceivePolicy(long sender, int flags) { ZNet instance = ZNet.instance; if (available && Object.op_Implicit((Object)(object)instance) && !((Object)(object)instance != (Object)(object)session) && !instance.IsServer() && sender == requestedHost) { ZNetPeer serverPeer = instance.GetServerPeer(); if (serverPeer != null && serverPeer.m_uid == sender && (flags & -32) == 0) { remote = (EquipPolicy)flags; } } } internal static void FlushSettingsChange() { if (!settingsChanged) { return; } settingsChanged = false; ZNet net = ZNet.instance; if (!available || !Object.op_Implicit((Object)(object)net) || (Object)(object)net != (Object)(object)session || !net.IsServer() || router == null) { return; } subscribers.RemoveWhere(delegate(long id) { ZNetPeer peer = net.GetPeer(id); return peer == null || !peer.IsReady(); }); int hostPolicy = (int)HostPolicy; foreach (long subscriber in subscribers) { router.InvokeRoutedRPC(subscriber, "KratyrTweaks_TombstonePolicy_v1", new object[1] { hostPolicy }); } } private static void NetworkDestroy(ZNet __instance) { if (session == __instance) { Reset(); } } internal static void Reset() { session = null; router = null; requestedHost = 0L; remote = EquipPolicy.Off; subscribers.Clear(); } } } namespace KratyrTweaks.Core { [BepInPlugin("com.kratyr.tweaks", "Kratyr Tweaks", "0.2.0")] public sealed class Plugin : BaseUnityPlugin { public const string PluginGuid = "com.kratyr.tweaks"; public const string PluginName = "Kratyr Tweaks"; public const string PluginVersion = "0.2.0"; private Harmony harmony; internal static ConfigEntry<bool> AlwaysShowStamina { get; private set; } internal static ConfigEntry<bool> AlwaysShowEitr { get; private set; } private void Awake() { //IL_0046: Unknown result type (might be due to invalid IL or missing references) //IL_0050: Expected O, but got Unknown AlwaysShowStamina = ((BaseUnityPlugin)this).Config.Bind<bool>("HUD", "AlwaysShowStamina", true, "Keep the vanilla stamina bar visible, even when full. Disable for normal visibility."); AlwaysShowEitr = ((BaseUnityPlugin)this).Config.Bind<bool>("HUD", "AlwaysShowEitr", true, "Keep the vanilla Eitr bar visible when available. Disable for normal visibility."); harmony = new Harmony("com.kratyr.tweaks"); int num = HudVisibility.Install(harmony, ((BaseUnityPlugin)this).Logger); ((BaseUnityPlugin)this).Logger.LogInfo((object)string.Format("{0} {1}: {2}/2 HUD patches installed.", "Kratyr Tweaks", "0.2.0", num)); if (TombstoneEquip.Install(harmony, ((BaseUnityPlugin)this).Logger)) { TombstonePolicy.Install(((BaseUnityPlugin)this).Config, harmony, ((BaseUnityPlugin)this).Logger); } } private void Update() { TombstonePolicy.FlushSettingsChange(); } private void OnDestroy() { TombstonePolicy.Reset(); if (harmony != null) { harmony.UnpatchSelf(); harmony = null; } } } }