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 Keychain v1.0.2
Keychain.dll
Decompiled 3 days agousing System; using System.Collections.Generic; using System.Reflection; using System.Runtime.CompilerServices; using AzuEPI; using BepInEx; using HarmonyLib; using TMPro; using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; [assembly: CompilationRelaxations(8)] [assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)] [assembly: AssemblyVersion("0.0.0.0")] namespace ValheimKeychain; [BepInDependency("Azumatt.AzuExtendedPlayerInventory", "2.4.14")] [BepInPlugin("YeffMods.Keychain", "Keychain", "1.0.2")] public sealed class KeychainPlugin : BaseUnityPlugin { [HarmonyPatch(typeof(ObjectDB), "UpdateRegisters")] private static class ItemDatabasePatch { [HarmonyPriority(0)] private static void Postfix(ObjectDB __instance) { LoadKeyPrefabs(__instance); } } internal const string Id = "YeffMods.Keychain"; internal const string InventoryMod = "Azumatt.AzuExtendedPlayerInventory"; internal static readonly KeySlot[] Slots = new KeySlot[3] { new KeySlot("Swamp", "CryptKey"), new KeySlot("Sealbreaker", "DvergrKey"), new KeySlot("Intricate", "BloodGoldKey") }; private Harmony harmony; private void Awake() { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_0010: Expected O, but got Unknown //IL_0031: Unknown result type (might be due to invalid IL or missing references) //IL_003b: Expected O, but got Unknown //IL_0042: Unknown result type (might be due to invalid IL or missing references) //IL_004c: Expected O, but got Unknown harmony = new Harmony("YeffMods.Keychain"); harmony.PatchAll(typeof(KeychainPlugin).Assembly); API.SlotAdded += new SlotAddedHandler(RefreshSlotIndices); API.SlotRemoved += new SlotRemovedHandler(RefreshSlotIndices); KeySlot[] slots = Slots; foreach (KeySlot keySlot in slots) { if (!API.AddSlot(keySlot.Id, (Func<Player, ItemData>)keySlot.GetEquipped, (Func<ItemData, bool>)keySlot.Accepts, -1)) { ((BaseUnityPlugin)this).Logger.LogError((object)("Could not register the inventory slot for " + keySlot.Prefab)); } } RefreshSlotIndices(null); if (Object.op_Implicit((Object)(object)ObjectDB.instance)) { LoadKeyPrefabs(ObjectDB.instance); } } private void OnDestroy() { //IL_0007: Unknown result type (might be due to invalid IL or missing references) //IL_0011: Expected O, but got Unknown //IL_0018: Unknown result type (might be due to invalid IL or missing references) //IL_0022: Expected O, but got Unknown API.SlotAdded -= new SlotAddedHandler(RefreshSlotIndices); API.SlotRemoved -= new SlotRemovedHandler(RefreshSlotIndices); if (harmony != null) { harmony.UnpatchSelf(); } } private static void RefreshSlotIndices(string changedSlot) { KeySlot[] slots = Slots; int num = default(int); foreach (KeySlot keySlot in slots) { keySlot.Index = (API.TryGetSlotIndexByName(keySlot.Id, ref num, true) ? num : (-1)); } } private static void LoadKeyPrefabs(ObjectDB database) { KeySlot[] slots = Slots; foreach (KeySlot keySlot in slots) { GameObject itemPrefab = database.GetItemPrefab(keySlot.Prefab); ItemDrop val = (Object.op_Implicit((Object)(object)itemPrefab) ? itemPrefab.GetComponent<ItemDrop>() : null); keySlot.Shared = (Object.op_Implicit((Object)(object)val) ? val.m_itemData.m_shared : null); } SharedData shared = Slots[2].Shared; if (shared != null && shared.m_maxStackSize < 3) { shared.m_maxStackSize = 3; } } } internal sealed class KeySlot { internal readonly string Id; internal readonly string Prefab; internal SharedData Shared; internal int Index = -1; internal KeySlot(string id, string prefab) { Id = "YeffKeychain_" + id; Prefab = prefab; } internal bool Accepts(ItemData item) { if (item == null || item.m_shared == null) { return false; } if (Shared != null) { if (object.ReferenceEquals(item.m_shared, Shared)) { return true; } if (item.m_shared.m_name != Shared.m_name) { return false; } } if (Object.op_Implicit((Object)(object)item.m_dropPrefab)) { return ((Object)item.m_dropPrefab).name == Prefab; } return false; } internal ItemData GetEquipped(Player player) { if (!Object.op_Implicit((Object)(object)player) || Shared == null) { return null; } Inventory inventory = ((Humanoid)player).GetInventory(); if (inventory == null) { return null; } List<ItemData> allItems = inventory.GetAllItems(); foreach (ItemData item in allItems) { if (item.m_equipped && Accepts(item)) { return item; } } return null; } } internal sealed class KeychainDrawer { [HarmonyPatch(typeof(InventoryGui), "UpdateInventory")] private static class UpdateInventoryPatch { [HarmonyAfter(new string[] { "Azumatt.AzuExtendedPlayerInventory" })] private static void Postfix(InventoryGui __instance, Player player) { UpdateDrawer(__instance, player); } } [HarmonyPatch(typeof(InventoryGui), "Hide")] private static class HideInventoryPatch { private static void Postfix(InventoryGui __instance) { if (current != null && (Object)(object)current.gui == (Object)(object)__instance && current.expanded) { current.SetExpanded(value: false); } } } [HarmonyPatch(typeof(InventoryGui), "OnDestroy")] private static class DestroyInventoryPatch { private static void Postfix(InventoryGui __instance) { if (current != null && (Object)(object)current.gui == (Object)(object)__instance) { current = null; } } } [HarmonyPatch(typeof(InventoryGrid), "GetGamepadSelectedElement")] private static class GamepadSelectionPatch { private static void Postfix(InventoryGrid __instance, ref RectTransform __result) { //IL_0024: Unknown result type (might be due to invalid IL or missing references) //IL_0029: Unknown result type (might be due to invalid IL or missing references) if (current != null && (Object)(object)current.grid == (Object)(object)__instance && Object.op_Implicit((Object)(object)__result) && __result.anchoredPosition == HiddenPosition) { __result = null; } } } private sealed class Cell { private InventoryElement element; private RectTransform rect; private CanvasGroup visibility; private GameObject bindingLabel; private Image emptyIcon; private SharedData knownKey; private Vector2 lastPosition; internal Vector2 NativePosition { get; private set; } internal bool IsKnown => knownKey != null; internal void Bind(InventoryElement target, SharedData key) { //IL_008a: Unknown result type (might be due to invalid IL or missing references) //IL_0090: Unknown result type (might be due to invalid IL or missing references) //IL_0022: Unknown result type (might be due to invalid IL or missing references) //IL_002c: Expected O, but got Unknown //IL_00a3: Unknown result type (might be due to invalid IL or missing references) //IL_006c: Unknown result type (might be due to invalid IL or missing references) //IL_0078: Unknown result type (might be due to invalid IL or missing references) //IL_007d: Unknown result type (might be due to invalid IL or missing references) if ((Object)(object)target != (Object)(object)element) { Release(); element = target; rect = (RectTransform)((Component)target).transform; visibility = ((Component)target).gameObject.AddComponent<CanvasGroup>(); Transform val = ((Component)target).transform.Find("binding"); bindingLabel = (Object.op_Implicit((Object)(object)val) ? ((Component)val).gameObject : null); NativePosition = rect.anchoredPosition; lastPosition = NativePosition; } else if (rect.anchoredPosition != lastPosition) { NativePosition = rect.anchoredPosition; } knownKey = key; } internal void Update(Vector2 position, bool visible, RectTransform tooltipAnchor) { //IL_0014: Unknown result type (might be due to invalid IL or missing references) //IL_0019: Unknown result type (might be due to invalid IL or missing references) //IL_002e: Unknown result type (might be due to invalid IL or missing references) //IL_002f: Unknown result type (might be due to invalid IL or missing references) //IL_0027: Unknown result type (might be due to invalid IL or missing references) //IL_010c: Unknown result type (might be due to invalid IL or missing references) //IL_01be: Unknown result type (might be due to invalid IL or missing references) if (!Object.op_Implicit((Object)(object)element)) { return; } if (rect.anchoredPosition != position) { rect.anchoredPosition = position; } lastPosition = position; SetVisible(visible); if (Object.op_Implicit((Object)(object)bindingLabel) && bindingLabel.activeSelf) { bindingLabel.SetActive(false); } Sprite val = ((visible && !element.m_used && knownKey.m_icons != null && knownKey.m_icons.Length > 0) ? knownKey.m_icons[0] : null); if (Object.op_Implicit((Object)(object)val) && !Object.op_Implicit((Object)(object)emptyIcon)) { emptyIcon = Object.Instantiate<Image>(element.m_icon, ((Component)element.m_icon).transform.parent); ((Object)emptyIcon).name = "Empty key"; ((Graphic)emptyIcon).color = new Color(1f, 1f, 1f, 0.32f); ((Graphic)emptyIcon).raycastTarget = false; ((Behaviour)emptyIcon).enabled = true; } if (Object.op_Implicit((Object)(object)emptyIcon)) { if ((Object)(object)emptyIcon.sprite != (Object)(object)val) { emptyIcon.sprite = val; } if (((Component)emptyIcon).gameObject.activeSelf != Object.op_Implicit((Object)(object)val)) { ((Component)emptyIcon).gameObject.SetActive(Object.op_Implicit((Object)(object)val)); } } if (Object.op_Implicit((Object)(object)val) && Object.op_Implicit((Object)(object)element.m_tooltip)) { element.m_tooltip.Set(knownKey.m_name, string.Empty, tooltipAnchor, Vector2.zero); } } private void SetVisible(bool value) { if (visibility.blocksRaycasts != value) { visibility.alpha = (value ? 1f : 0f); visibility.blocksRaycasts = value; visibility.interactable = value; } } internal void Release() { //IL_001a: 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_0033: Unknown result type (might be due to invalid IL or missing references) if (Object.op_Implicit((Object)(object)visibility)) { SetVisible(value: true); if (rect.anchoredPosition == lastPosition) { rect.anchoredPosition = NativePosition; } Object.Destroy((Object)(object)visibility); } if (Object.op_Implicit((Object)(object)emptyIcon)) { Object.Destroy((Object)(object)((Component)emptyIcon).gameObject); } element = null; visibility = null; emptyIcon = null; knownKey = null; } } private static readonly FieldInfo GridElements = AccessTools.Field(typeof(InventoryGrid), "m_elements"); private static readonly Func<InventoryGrid, Vector2i> GridSelection = AccessTools.MethodDelegate<Func<InventoryGrid, Vector2i>>(AccessTools.PropertyGetter(typeof(InventoryGrid), "SelectionGridPosition"), (object)null, true); private static readonly Vector2 HiddenPosition = new Vector2(0f, -10000f); private static KeychainDrawer current; private readonly InventoryGui gui; private readonly InventoryGrid grid; private readonly List<InventoryElement> elements; private readonly Button button; private readonly RectTransform header; private readonly TMP_Text label; private readonly Cell[] cells = new Cell[3] { new Cell(), new Cell(), new Cell() }; private bool expanded; private KeychainDrawer(InventoryGui gui, List<InventoryElement> elements) { //IL_0084: Unknown result type (might be due to invalid IL or missing references) //IL_008e: Expected O, but got Unknown //IL_00a0: Unknown result type (might be due to invalid IL or missing references) //IL_00aa: Expected O, but got Unknown //IL_00b2: Unknown result type (might be due to invalid IL or missing references) //IL_00c0: Unknown result type (might be due to invalid IL or missing references) //IL_00d2: Unknown result type (might be due to invalid IL or missing references) //IL_00dc: Expected O, but got Unknown this.gui = gui; grid = gui.m_playerGrid; this.elements = elements; button = Object.Instantiate<Button>(gui.m_takeAllButton, (Transform)(object)gui.m_player); ((Component)button).gameObject.SetActive(false); ((Object)button).name = "Keychain"; button.onClick = new ButtonClickedEvent(); ((UnityEvent)button.onClick).AddListener(new UnityAction(Toggle)); Button obj = button; Navigation navigation = default(Navigation); ((Navigation)(ref navigation)).mode = (Mode)0; ((Selectable)obj).navigation = navigation; header = (RectTransform)((Component)button).transform; label = ((Component)button).GetComponentInChildren<TMP_Text>(true); SetExpanded(value: false); } private static void UpdateDrawer(InventoryGui instance, Player player) { if (!Object.op_Implicit((Object)(object)player) || !Object.op_Implicit((Object)(object)instance.m_takeAllButton) || !Object.op_Implicit((Object)(object)instance.m_playerGrid)) { return; } Inventory inventory = ((Humanoid)player).GetInventory(); KeySlot keySlot = KeychainPlugin.Slots[0]; if (keySlot.Index < 0 || inventory == null) { RemoveDrawer(instance); return; } bool flag = current == null || (Object)(object)current.gui != (Object)(object)instance || (Object)(object)current.grid != (Object)(object)instance.m_playerGrid || !Object.op_Implicit((Object)(object)current.button); List<InventoryElement> list = (flag ? ((List<InventoryElement>)GridElements.GetValue(instance.m_playerGrid)) : current.elements); int slotGridLinearIndex = API.GetSlotGridLinearIndex(inventory, keySlot.Index); if (slotGridLinearIndex < 0 || slotGridLinearIndex >= list.Count || !Object.op_Implicit((Object)(object)list[slotGridLinearIndex])) { RemoveDrawer(instance); return; } if (flag) { RemoveDrawer(instance); current = new KeychainDrawer(instance, list); } current.UpdateCells(player, inventory, list[slotGridLinearIndex]); } private static void RemoveDrawer(InventoryGui instance) { if (current != null && !((Object)(object)current.gui != (Object)(object)instance)) { Cell[] array = current.cells; foreach (Cell cell in array) { cell.Release(); } if (Object.op_Implicit((Object)(object)current.button)) { Object.Destroy((Object)(object)((Component)current.button).gameObject); } current = null; } } private void UpdateCells(Player player, Inventory inventory, InventoryElement first) { //IL_0006: Unknown result type (might be due to invalid IL or missing references) //IL_000c: Expected O, but got Unknown //IL_002b: Unknown result type (might be due to invalid IL or missing references) //IL_0042: 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_013f: Unknown result type (might be due to invalid IL or missing references) //IL_0144: Unknown result type (might be due to invalid IL or missing references) //IL_0158: Unknown result type (might be due to invalid IL or missing references) //IL_01bb: Unknown result type (might be due to invalid IL or missing references) //IL_01bd: Unknown result type (might be due to invalid IL or missing references) //IL_01cd: Unknown result type (might be due to invalid IL or missing references) //IL_01d2: Unknown result type (might be due to invalid IL or missing references) //IL_01b4: Unknown result type (might be due to invalid IL or missing references) //IL_01d7: Unknown result type (might be due to invalid IL or missing references) //IL_01db: Unknown result type (might be due to invalid IL or missing references) //IL_00fd: Unknown result type (might be due to invalid IL or missing references) //IL_0100: Unknown result type (might be due to invalid IL or missing references) RectTransform firstRect = (RectTransform)((Component)first).transform; Vector2i val = (Vector2i)((!expanded && gui.ActiveGroup == 1 && ZInput.IsGamepadActive()) ? GridSelection(grid) : new Vector2i(-1, -1)); bool flag = false; for (int i = 0; i < cells.Length; i++) { KeySlot keySlot = KeychainPlugin.Slots[i]; int num = ((keySlot.Index < 0) ? (-1) : API.GetSlotGridLinearIndex(inventory, keySlot.Index)); if (num < 0 || num >= elements.Count || !Object.op_Implicit((Object)(object)elements[num])) { cells[i].Release(); continue; } InventoryElement val2 = elements[num]; bool flag2 = keySlot.Shared != null && player.IsKnownMaterial(keySlot.Shared.m_name); cells[i].Bind(val2, flag2 ? keySlot.Shared : null); flag = flag || flag2; if (flag2 && val == val2.Position) { SetExpanded(value: true); } } if (!flag && expanded) { SetExpanded(value: false); } Vector2 nativePosition = cells[0].NativePosition; float elementSpace = grid.m_elementSpace; if (flag) { PositionHeader(firstRect, nativePosition, elementSpace); } if (((Component)button).gameObject.activeSelf != flag) { ((Component)button).gameObject.SetActive(flag); } int num2 = 0; Cell[] array = cells; foreach (Cell cell in array) { bool flag3 = expanded && cell.IsKnown; Vector2 position = (flag3 ? (nativePosition - Vector2.up * ((float)num2++ * elementSpace)) : HiddenPosition); cell.Update(position, flag3, grid.m_tooltipAnchor); } } private void PositionHeader(RectTransform firstRect, Vector2 origin, float spacing) { //IL_008b: Unknown result type (might be due to invalid IL or missing references) //IL_009c: Unknown result type (might be due to invalid IL or missing references) //IL_00ad: Unknown result type (might be due to invalid IL or missing references) //IL_00d3: Unknown result type (might be due to invalid IL or missing references) //IL_00e3: Unknown result type (might be due to invalid IL or missing references) //IL_00e4: Unknown result type (might be due to invalid IL or missing references) //IL_00ee: Unknown result type (might be due to invalid IL or missing references) //IL_00f3: Unknown result type (might be due to invalid IL or missing references) //IL_001e: Unknown result type (might be due to invalid IL or missing references) //IL_0024: 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) //IL_004e: Unknown result type (might be due to invalid IL or missing references) //IL_0053: Unknown result type (might be due to invalid IL or missing references) //IL_0054: Unknown result type (might be due to invalid IL or missing references) //IL_005e: Unknown result type (might be due to invalid IL or missing references) //IL_0063: Unknown result type (might be due to invalid IL or missing references) if ((Object)(object)((Transform)header).parent != (Object)(object)((Transform)firstRect).parent || header.anchorMin != firstRect.anchorMin || header.anchorMax != firstRect.anchorMax || header.anchoredPosition != origin + Vector2.up * 48f) { ((Transform)header).SetParent(((Transform)firstRect).parent, false); header.anchorMin = firstRect.anchorMin; header.anchorMax = firstRect.anchorMax; header.pivot = firstRect.pivot; header.sizeDelta = new Vector2(Mathf.Max(spacing * 1.55f, 106f), 26f); header.anchoredPosition = origin + Vector2.up * 48f; ((Transform)header).SetAsLastSibling(); } } private void Toggle() { SetExpanded(!expanded); } private void SetExpanded(bool value) { expanded = value; if (Object.op_Implicit((Object)(object)label)) { label.text = (value ? "Keychain ▲" : "Keychain ▼"); } } } internal static class KeychainSaveLayout { private struct Layout { internal int Width; internal int First; internal int End; internal int KeyCount; internal static bool TryRead(Inventory inventory, out Layout layout) { layout = default(Layout); KeySlot[] slots = KeychainPlugin.Slots; int index = slots[0].Index; if (inventory == null || index < 0) { return false; } for (int i = 1; i < slots.Length; i++) { if (slots[i].Index != index + i) { return false; } } layout.Width = inventory.GetWidth(); layout.First = API.GetSlotGridLinearIndex(inventory, index); layout.End = layout.First + API.GetSlotCount() - index; layout.KeyCount = slots.Length; if (layout.Width > 0 && layout.First >= 0 && layout.End <= layout.Width * inventory.GetHeight()) { return layout.End - layout.First > layout.KeyCount; } return false; } internal bool TryTranslate(Vector2i position, bool saving, out Vector2i mapped) { //IL_0001: Unknown result type (might be due to invalid IL or missing references) //IL_0002: Unknown result type (might be due to invalid IL or missing references) //IL_009e: Unknown result type (might be due to invalid IL or missing references) //IL_00a3: Unknown result type (might be due to invalid IL or missing references) mapped = position; if (position.x < 0 || position.x >= Width || position.y < 0) { return false; } int num = position.x + position.y * Width; if (num < First || num >= End) { return false; } int num2 = End - First; int num3 = (saving ? (num2 - KeyCount) : KeyCount); int num4 = First + (num - First + num3) % num2; mapped = new Vector2i(num4 % Width, num4 / Width); return true; } } [HarmonyPatch(typeof(Player), "Save")] private static class SavePlayer { private struct SavedPosition { internal ItemData Item; internal Vector2i Position; } private static void Prefix(Player __instance, out List<SavedPosition> __state) { //IL_002f: 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) //IL_0072: Unknown result type (might be due to invalid IL or missing references) //IL_007f: Unknown result type (might be due to invalid IL or missing references) //IL_0080: Unknown result type (might be due to invalid IL or missing references) __state = null; Inventory inventory = ((Humanoid)__instance).GetInventory(); if (!Layout.TryRead(inventory, out var layout)) { return; } foreach (ItemData allItem in inventory.GetAllItems()) { if (layout.TryTranslate(allItem.m_gridPos, saving: true, out var mapped)) { if (__state == null) { __state = new List<SavedPosition>(layout.End - layout.First); } __state.Add(new SavedPosition { Item = allItem, Position = allItem.m_gridPos }); allItem.m_gridPos = mapped; } } } private static void Finalizer(List<SavedPosition> __state) { //IL_001e: Unknown result type (might be due to invalid IL or missing references) //IL_0023: Unknown result type (might be due to invalid IL or missing references) if (__state == null) { return; } foreach (SavedPosition item in __state) { item.Item.m_gridPos = item.Position; } } } [HarmonyPatch(typeof(Player), "Load")] private static class LoadPlayer { private static void Prefix(Player __instance, out Inventory __state) { __state = loadingInventory; loadingInventory = ((Humanoid)__instance).GetInventory(); } private static void Finalizer(Inventory __state) { loadingInventory = __state; } } [HarmonyPatch(typeof(Inventory), "Load", new Type[] { typeof(ZPackage) })] private static class LoadInventory { [HarmonyBefore(new string[] { "Azumatt.AzuExtendedPlayerInventory" })] private static void Postfix(Inventory __instance) { //IL_002c: 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) //IL_003d: Unknown result type (might be due to invalid IL or missing references) if (__instance != loadingInventory || !Layout.TryRead(__instance, out var layout)) { return; } foreach (ItemData allItem in __instance.GetAllItems()) { if (layout.TryTranslate(allItem.m_gridPos, saving: false, out var mapped)) { allItem.m_gridPos = mapped; } } } } [ThreadStatic] private static Inventory loadingInventory; }