Decompiled source of EquipmentSheet v2.0.7

plugins\EquipmentSheet\EquipmentSheet.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Equipment Sheet")]
[assembly: AssemblyFileVersion("2.0.7")]
[assembly: AssemblyCompany("R4V9N1")]
[assembly: AssemblyDescription("Created by R4V9N1")]
[assembly: AssemblyProduct("Equipment Sheet")]
[assembly: AssemblyCopyright("Created by R4V9N1")]
[assembly: AssemblyMetadata("Creator", "Created by R4V9N1")]
[assembly: AssemblyVersion("2.0.7.0")]
namespace EquipmentSheet;

internal static class EquipmentSheetNativeSkin
{
	private static bool _applied;

	internal static void Apply(InventoryGui gui)
	{
		//IL_008c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0143: Unknown result type (might be due to invalid IL or missing references)
		//IL_0159: Unknown result type (might be due to invalid IL or missing references)
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0185: Unknown result type (might be due to invalid IL or missing references)
		//IL_019b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f6: Unknown result type (might be due to invalid IL or missing references)
		//IL_020b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0220: Unknown result type (might be due to invalid IL or missing references)
		//IL_0235: Unknown result type (might be due to invalid IL or missing references)
		//IL_0249: Unknown result type (might be due to invalid IL or missing references)
		//IL_027a: Unknown result type (might be due to invalid IL or missing references)
		//IL_041a: Unknown result type (might be due to invalid IL or missing references)
		//IL_041f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0426: Unknown result type (might be due to invalid IL or missing references)
		//IL_042d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0434: Unknown result type (might be due to invalid IL or missing references)
		//IL_0441: Unknown result type (might be due to invalid IL or missing references)
		//IL_0452: Expected O, but got Unknown
		//IL_0452: Unknown result type (might be due to invalid IL or missing references)
		//IL_0463: Unknown result type (might be due to invalid IL or missing references)
		//IL_0478: Unknown result type (might be due to invalid IL or missing references)
		//IL_048d: Unknown result type (might be due to invalid IL or missing references)
		//IL_04a2: Unknown result type (might be due to invalid IL or missing references)
		//IL_04b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_04c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_051f: Unknown result type (might be due to invalid IL or missing references)
		//IL_052f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0550: Unknown result type (might be due to invalid IL or missing references)
		//IL_0564: Unknown result type (might be due to invalid IL or missing references)
		//IL_0592: Unknown result type (might be due to invalid IL or missing references)
		//IL_05a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_05b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_05c7: Unknown result type (might be due to invalid IL or missing references)
		//IL_034b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0394: Unknown result type (might be due to invalid IL or missing references)
		//IL_03aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_062e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0633: Unknown result type (might be due to invalid IL or missing references)
		//IL_063a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0641: Unknown result type (might be due to invalid IL or missing references)
		//IL_0648: Unknown result type (might be due to invalid IL or missing references)
		//IL_065b: Unknown result type (might be due to invalid IL or missing references)
		//IL_066c: Expected O, but got Unknown
		//IL_066c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0673: Unknown result type (might be due to invalid IL or missing references)
		//IL_067e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0689: Unknown result type (might be due to invalid IL or missing references)
		//IL_0693: Unknown result type (might be due to invalid IL or missing references)
		//IL_069d: Unknown result type (might be due to invalid IL or missing references)
		//IL_06f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0709: Unknown result type (might be due to invalid IL or missing references)
		//IL_072a: Unknown result type (might be due to invalid IL or missing references)
		//IL_073e: Unknown result type (might be due to invalid IL or missing references)
		if (_applied || (Object)(object)gui == (Object)null || (Object)(object)gui.m_player == (Object)null)
		{
			return;
		}
		TMP_FontAsset val = FindAnyNativeFont(gui);
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		Transform val2 = ((Component)gui).transform.Find("root/EquipmentSheetPanel");
		if ((Object)(object)val2 == (Object)null)
		{
			Debug.LogWarning((object)"[EquipmentSheet] Native skin: EquipmentSheetPanel not found.");
			return;
		}
		_applied = true;
		SetLayerRecursively(((Component)val2).gameObject, ((Component)gui).gameObject.layer);
		RectTransform val3 = (RectTransform)(object)((val2 is RectTransform) ? val2 : null);
		if ((Object)(object)val3 != (Object)null)
		{
			val3.sizeDelta = new Vector2(224f, 264f);
		}
		Image component = ((Component)val2).GetComponent<Image>();
		Transform val4 = ((Transform)gui.m_player).Find("Bkg");
		if ((Object)(object)component != (Object)null && (Object)(object)val4 != (Object)null)
		{
			Image component2 = ((Component)val4).GetComponent<Image>();
			if ((Object)(object)component2 != (Object)null)
			{
				CopyImageStyle(component2, component);
				((Graphic)component).color = Color.white;
				((Graphic)component).raycastTarget = true;
				Debug.Log((object)("[EquipmentSheet] Native frame: " + ((Object)component2.sprite).name));
			}
		}
		Transform val5 = val2.Find("EquipmentSheetGridRoot");
		if ((Object)(object)val5 != (Object)null)
		{
			RectTransform val6 = (RectTransform)(object)((val5 is RectTransform) ? val5 : null);
			if ((Object)(object)val6 != (Object)null)
			{
				val6.anchorMin = new Vector2(0f, 1f);
				val6.anchorMax = new Vector2(0f, 1f);
				val6.pivot = new Vector2(0f, 1f);
				val6.anchoredPosition = new Vector2(12f, -44f);
				val6.sizeDelta = new Vector2(200f, 200f);
			}
		}
		Transform val7 = ((Transform)gui.m_player).Find("sunken");
		if ((Object)(object)val7 != (Object)null)
		{
			Image component3 = ((Component)val7).GetComponent<Image>();
			if ((Object)(object)component3 != (Object)null)
			{
				GameObject obj = CreateImage("EquipmentSheet_NativeInset", val2);
				RectTransform component4 = obj.GetComponent<RectTransform>();
				component4.anchorMin = new Vector2(0f, 1f);
				component4.anchorMax = new Vector2(0f, 1f);
				component4.pivot = new Vector2(0f, 1f);
				component4.anchoredPosition = new Vector2(8f, -38f);
				component4.sizeDelta = new Vector2(208f, 218f);
				Image component5 = obj.GetComponent<Image>();
				CopyImageStyle(component3, component5);
				((Graphic)component5).color = new Color(1f, 1f, 1f, 0.72f);
				((Graphic)component5).raycastTarget = false;
				obj.transform.SetAsFirstSibling();
			}
		}
		Image val8 = null;
		if ((Object)(object)gui.m_playerGrid != (Object)null && (Object)(object)gui.m_playerGrid.m_elementPrefab != (Object)null)
		{
			val8 = gui.m_playerGrid.m_elementPrefab.GetComponent<Image>();
		}
		if ((Object)(object)val8 != (Object)null)
		{
			Image[] componentsInChildren = ((Component)val2).GetComponentsInChildren<Image>(true);
			foreach (Image val9 in componentsInChildren)
			{
				if (!((Object)(object)val9 == (Object)null) && ((Object)val9).name.StartsWith("EquipmentSheet_", StringComparison.Ordinal) && ((Object)val9).name.EndsWith("_Frame", StringComparison.Ordinal))
				{
					CopyImageStyle(val8, val9);
					((Graphic)val9).color = new Color(0.31f, 0.23f, 0.215f, 1f);
					((Graphic)val9).raycastTarget = false;
					Outline val10 = ((Component)val9).GetComponent<Outline>();
					if ((Object)(object)val10 == (Object)null)
					{
						val10 = ((Component)val9).gameObject.AddComponent<Outline>();
					}
					((Shadow)val10).effectColor = new Color(0.115f, 0.065f, 0.04f, 0.98f);
					((Shadow)val10).effectDistance = new Vector2(2f, -2f);
					((Shadow)val10).useGraphicAlpha = false;
				}
			}
			Debug.Log((object)("[EquipmentSheet] Native slots: " + ((Object)val8.sprite).name));
		}
		TMP_FontAsset val11 = FindFont(gui, "Valheim-Norsebold");
		TMP_FontAsset val12 = FindFont(gui, "Valheim-AveriaSansLibre");
		GameObject val13 = new GameObject("EquipmentSheet_NativeTitle", new Type[1] { typeof(RectTransform) });
		val13.SetActive(false);
		val13.AddComponent<CanvasRenderer>();
		val13.AddComponent<TextMeshProUGUI>();
		val13.transform.SetParent(val2, false);
		SetLayerRecursively(val13, ((Component)gui).gameObject.layer);
		RectTransform component6 = val13.GetComponent<RectTransform>();
		component6.anchorMin = new Vector2(0f, 1f);
		component6.anchorMax = new Vector2(0f, 1f);
		component6.pivot = new Vector2(0.5f, 0.5f);
		component6.anchoredPosition = new Vector2(112f, -19f);
		component6.sizeDelta = new Vector2(190f, 30f);
		TextMeshProUGUI component7 = val13.GetComponent<TextMeshProUGUI>();
		((TMP_Text)component7).text = "EQUIPMENT";
		((TMP_Text)component7).font = (((Object)(object)val11 != (Object)null) ? val11 : (((Object)(object)val12 != (Object)null) ? val12 : val));
		((TMP_Text)component7).fontSize = 25f;
		((TMP_Text)component7).alignment = (TextAlignmentOptions)514;
		((Graphic)component7).color = new Color(1f, 0.718f, 0.36f, 1f);
		((Graphic)component7).raycastTarget = false;
		val13.SetActive(true);
		Shadow obj2 = val13.AddComponent<Shadow>();
		obj2.effectColor = new Color(0f, 0f, 0f, 0.78f);
		obj2.effectDistance = new Vector2(2f, -2f);
		Image val14 = FindImageBySprite(gui, "panel_separator");
		if ((Object)(object)val14 != (Object)null)
		{
			CreateSeparator(val2, val14, new Vector2(112f, -36f), new Vector2(188f, 2f));
			CreateSeparator(val2, val14, new Vector2(112f, -178f), new Vector2(188f, 2f));
		}
		Text[] componentsInChildren2 = ((Component)val2).GetComponentsInChildren<Text>(true);
		foreach (Text val15 in componentsInChildren2)
		{
			if (!((Object)(object)val15 == (Object)null) && ((Object)val15).name.EndsWith("_Label", StringComparison.Ordinal))
			{
				string text = val15.text;
				GameObject val16 = new GameObject("NativeLabel", new Type[1] { typeof(RectTransform) });
				val16.SetActive(false);
				val16.AddComponent<CanvasRenderer>();
				val16.AddComponent<TextMeshProUGUI>();
				val16.transform.SetParent(((Component)val15).transform, false);
				SetLayerRecursively(val16, ((Component)gui).gameObject.layer);
				RectTransform component8 = val16.GetComponent<RectTransform>();
				component8.anchorMin = Vector2.zero;
				component8.anchorMax = Vector2.one;
				component8.offsetMin = Vector2.zero;
				component8.offsetMax = Vector2.zero;
				TextMeshProUGUI component9 = val16.GetComponent<TextMeshProUGUI>();
				((TMP_Text)component9).text = text;
				((TMP_Text)component9).font = (((Object)(object)val12 != (Object)null) ? val12 : (((Object)(object)val11 != (Object)null) ? val11 : val));
				((TMP_Text)component9).fontSize = 11f;
				((TMP_Text)component9).alignment = (TextAlignmentOptions)514;
				((Graphic)component9).color = new Color(0.86f, 0.72f, 0.48f, 0.82f);
				((Graphic)component9).raycastTarget = false;
				val16.SetActive(true);
				Shadow obj3 = val16.AddComponent<Shadow>();
				obj3.effectColor = new Color(0f, 0f, 0f, 0.8f);
				obj3.effectDistance = new Vector2(1f, -1f);
				((Behaviour)val15).enabled = false;
			}
		}
		Debug.Log((object)"[EquipmentSheet] Native Valheim skin applied.");
	}

	private static void CreateSeparator(Transform parent, Image source, Vector2 position, Vector2 size)
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: 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_006b: Unknown result type (might be due to invalid IL or missing references)
		GameObject obj = CreateImage("EquipmentSheet_NativeSeparator", parent);
		RectTransform component = obj.GetComponent<RectTransform>();
		component.anchorMin = new Vector2(0f, 1f);
		component.anchorMax = new Vector2(0f, 1f);
		component.pivot = new Vector2(0.5f, 0.5f);
		component.anchoredPosition = position;
		component.sizeDelta = size;
		Image component2 = obj.GetComponent<Image>();
		CopyImageStyle(source, component2);
		((Graphic)component2).color = Color.white;
		((Graphic)component2).raycastTarget = false;
	}

	private static GameObject CreateImage(string name, Transform parent)
	{
		//IL_002e: 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)
		//IL_0041: Expected O, but got Unknown
		GameObject val = new GameObject(name, new Type[3]
		{
			typeof(RectTransform),
			typeof(CanvasRenderer),
			typeof(Image)
		});
		val.transform.SetParent(parent, false);
		return val;
	}

	private static void CopyImageStyle(Image source, Image destination)
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		destination.sprite = source.sprite;
		destination.overrideSprite = source.overrideSprite;
		destination.type = source.type;
		destination.preserveAspect = source.preserveAspect;
		destination.fillCenter = source.fillCenter;
		((Graphic)destination).material = ((Graphic)source).material;
		destination.pixelsPerUnitMultiplier = source.pixelsPerUnitMultiplier;
	}

	private static TMP_FontAsset FindAnyNativeFont(InventoryGui gui)
	{
		TMP_Text[] componentsInChildren = ((Component)gui).GetComponentsInChildren<TMP_Text>(true);
		foreach (TMP_Text val in componentsInChildren)
		{
			if ((Object)(object)val != (Object)null && (Object)(object)val.font != (Object)null)
			{
				return val.font;
			}
		}
		return null;
	}

	private static TMP_FontAsset FindFont(InventoryGui gui, string fontName)
	{
		TMP_Text[] componentsInChildren = ((Component)gui).GetComponentsInChildren<TMP_Text>(true);
		for (int i = 0; i < componentsInChildren.Length; i++)
		{
			if (!((Object)(object)componentsInChildren[i] == (Object)null) && !((Object)(object)componentsInChildren[i].font == (Object)null) && string.Equals(((Object)componentsInChildren[i].font).name, fontName, StringComparison.Ordinal))
			{
				return componentsInChildren[i].font;
			}
		}
		for (int j = 0; j < componentsInChildren.Length; j++)
		{
			if ((Object)(object)componentsInChildren[j] != (Object)null && (Object)(object)componentsInChildren[j].font != (Object)null)
			{
				return componentsInChildren[j].font;
			}
		}
		return null;
	}

	private static Image FindImageBySprite(InventoryGui gui, string spriteName)
	{
		Image[] componentsInChildren = ((Component)gui).GetComponentsInChildren<Image>(true);
		foreach (Image val in componentsInChildren)
		{
			if (!((Object)(object)val == (Object)null) && !((Object)(object)val.sprite == (Object)null) && string.Equals(((Object)val.sprite).name, spriteName, StringComparison.Ordinal))
			{
				return val;
			}
		}
		return null;
	}

	private static void SetLayerRecursively(GameObject root, int layer)
	{
		root.layer = layer;
		Transform transform = root.transform;
		for (int i = 0; i < transform.childCount; i++)
		{
			SetLayerRecursively(((Component)transform.GetChild(i)).gameObject, layer);
		}
	}
}
[BepInPlugin("r4v9n1.equipmentsheet", "Equipment Sheet", "2.0.7")]
public sealed class EquipmentSheetPlugin : BaseUnityPlugin
{
	private enum SlotKind
	{
		Equipment,
		Food
	}

	private sealed class SlotDef
	{
		public readonly string Label;

		public readonly ItemType Type;

		public readonly SlotKind Kind;

		public SlotDef(string label, ItemType type)
		{
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			Label = label;
			Type = type;
			Kind = SlotKind.Equipment;
		}

		public SlotDef(string label)
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			Label = label;
			Type = (ItemType)0;
			Kind = SlotKind.Food;
		}
	}

	private sealed class SheetUpgradeTransaction
	{
		public Player Player;

		public Inventory PlayerInventory;

		public ItemData OriginalItem;

		public Vector2i SheetPosition;

		public bool OriginalRemoved;

		public bool ReplacementAttempted;

		public ItemData ReplacementItem;
	}

	public const string PluginGuid = "r4v9n1.equipmentsheet";

	public const string PluginName = "Equipment Sheet";

	public const string PluginVersion = "2.0.7";

	public const string CreatorCredit = "Created by R4V9N1";

	private const string CustomDataKey = "is.codex.valheim.equipmentsheet.inventory.v1";

	private const string ProtectionSnapshotKey = "is.codex.valheim.equipmentsheet.protection-snapshot.v1";

	private const string PendingTransferKey = "is.codex.valheim.equipmentsheet.pending-transfer.v1";

	private const string DeathSnapshotKey = "is.codex.valheim.equipmentsheet.death-snapshot.v2";

	private const string GraveSlotMetadataKey = "is.codex.valheim.equipmentsheet.grave-slot.v2";

	private const int SheetColumns = 3;

	private const int SheetRows = 3;

	private const int EquipmentSlotCount = 6;

	private static readonly SlotDef[] Slots = new SlotDef[9]
	{
		new SlotDef("Helm", (ItemType)6),
		new SlotDef("Chest", (ItemType)7),
		new SlotDef("Legs", (ItemType)11),
		new SlotDef("Trinket", (ItemType)24),
		new SlotDef("Back", (ItemType)17),
		new SlotDef("Belt", (ItemType)18),
		new SlotDef("Food 1"),
		new SlotDef("Food 2"),
		new SlotDef("Food 3")
	};

	private static ConfigEntry<bool> _enabled;

	private static ConfigEntry<float> _panelGap;

	private static ConfigEntry<float> _panelExtraRightOffset;

	private static ManualLogSource _log;

	private static RectTransform _panel;

	private static CanvasGroup _panelCanvasGroup;

	private static CanvasGroup _nativeInventoryCanvasGroup;

	private static InventoryGrid _grid;

	private static Inventory _equipmentInventory;

	private static Image[] _slotFrames;

	private static Image[] _slotIcons;

	private static Text[] _slotLabels;

	private static Text[] _slotAmounts;

	private static Text[] _slotQualities;

	private static Image[] _slotDurabilityBacks;

	private static Image[] _slotDurabilityBars;

	private static UIInputHandler[] _slotHitboxes;

	private static UITooltip[] _slotTooltips;

	private static Vector2 _cellSize = new Vector2(50f, 50f);

	private static Vector2[] _slotPositions;

	private static FieldInfo _customDataField;

	private static FieldInfo _onChangedField;

	private static FieldInfo _inventoryItemsField;

	private static FieldInfo _dragGoField;

	private static FieldInfo _dragItemField;

	private static FieldInfo _dragInventoryField;

	private static FieldInfo _dragAmountField;

	private static FieldInfo _itemEquippedField;

	private static FieldInfo _itemCustomDataField;

	private static FieldInfo _itemCrafterIdField;

	private static FieldInfo _itemCrafterNameField;

	private static FieldInfo _inventoryWidthField;

	private static FieldInfo _inventoryHeightField;

	private static FieldInfo _gridWidthField;

	private static FieldInfo _gridHeightField;

	private static FieldInfo _humanoidChestField;

	private static FieldInfo _humanoidLegField;

	private static FieldInfo _humanoidHelmetField;

	private static FieldInfo _humanoidShoulderField;

	private static FieldInfo _humanoidUtilityField;

	private static FieldInfo _humanoidTrinketField;

	private static FieldInfo _currentContainerField;

	private static FieldInfo _craftUpgradeItemField;

	private static MethodInfo _inventoryChangedMethod;

	private static MethodInfo _setupDragItemMethod;

	private static MethodInfo _setupEquipmentMethod;

	private static MethodInfo _createItemTooltipMethod;

	private static Player _loadedPlayer;

	private static bool _loadingInventory;

	private static bool _loadedInventorySaveSafe = true;

	private static readonly Dictionary<Inventory, int> _knownTombstoneInventories = new Dictionary<Inventory, int>();

	private static int _upgradeRecipeScanDepth;

	private static SheetUpgradeTransaction _sheetUpgradeTransaction;

	private static readonly List<ItemData> PendingAutoRouteItems = new List<ItemData>();

	private static ItemData _manualSheetDragItem;

	private static bool _restoringMarkedGraveItems;

	private static float _lastWrongTypeMessageTime = float.NegativeInfinity;

	private Harmony _harmony;

	private void Awake()
	{
		//IL_0393: Unknown result type (might be due to invalid IL or missing references)
		//IL_039d: Expected O, but got Unknown
		_log = ((BaseUnityPlugin)this).Logger;
		_enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Show the equipment sheet panel next to the player inventory.");
		_panelGap = ((BaseUnityPlugin)this).Config.Bind<float>("General", "PanelGap", 20f, "Horizontal gap (UI units) between the vanilla inventory grid and the equipment panel.");
		_panelExtraRightOffset = ((BaseUnityPlugin)this).Config.Bind<float>("General", "PanelExtraRightOffset", 60f, "Extra horizontal offset added after PanelGap. Useful when another UI overlaps the equipment panel.");
		_customDataField = AccessTools.Field(typeof(Player), "m_customData");
		_onChangedField = AccessTools.Field(typeof(Inventory), "m_onChanged");
		_inventoryItemsField = AccessTools.Field(typeof(Inventory), "m_inventory");
		_dragGoField = AccessTools.Field(typeof(InventoryGui), "m_dragGo");
		_dragItemField = AccessTools.Field(typeof(InventoryGui), "m_dragItem");
		_dragInventoryField = AccessTools.Field(typeof(InventoryGui), "m_dragInventory");
		_dragAmountField = AccessTools.Field(typeof(InventoryGui), "m_dragAmount");
		_itemEquippedField = AccessTools.Field(typeof(ItemData), "m_equipped");
		_itemCustomDataField = AccessTools.Field(typeof(ItemData), "m_customData");
		_itemCrafterIdField = AccessTools.Field(typeof(ItemData), "m_crafterID");
		_itemCrafterNameField = AccessTools.Field(typeof(ItemData), "m_crafterName");
		_inventoryWidthField = AccessTools.Field(typeof(Inventory), "m_width");
		_inventoryHeightField = AccessTools.Field(typeof(Inventory), "m_height");
		_gridWidthField = AccessTools.Field(typeof(InventoryGrid), "m_width");
		_gridHeightField = AccessTools.Field(typeof(InventoryGrid), "m_height");
		_humanoidChestField = AccessTools.Field(typeof(Humanoid), "m_chestItem");
		_humanoidLegField = AccessTools.Field(typeof(Humanoid), "m_legItem");
		_humanoidHelmetField = AccessTools.Field(typeof(Humanoid), "m_helmetItem");
		_humanoidShoulderField = AccessTools.Field(typeof(Humanoid), "m_shoulderItem");
		_humanoidUtilityField = AccessTools.Field(typeof(Humanoid), "m_utilityItem");
		_humanoidTrinketField = AccessTools.Field(typeof(Humanoid), "m_trinketItem");
		_currentContainerField = AccessTools.Field(typeof(InventoryGui), "m_currentContainer");
		_craftUpgradeItemField = AccessTools.Field(typeof(InventoryGui), "m_craftUpgradeItem");
		_inventoryChangedMethod = AccessTools.Method(typeof(Inventory), "Changed", new Type[2]
		{
			typeof(bool),
			typeof(bool)
		}, (Type[])null);
		_setupDragItemMethod = AccessTools.Method(typeof(InventoryGui), "SetupDragItem", new Type[3]
		{
			typeof(ItemData),
			typeof(Inventory),
			typeof(int)
		}, (Type[])null);
		_setupEquipmentMethod = AccessTools.Method(typeof(Humanoid), "SetupEquipment", (Type[])null, (Type[])null);
		_createItemTooltipMethod = AccessTools.Method(typeof(InventoryGrid), "CreateItemTooltip", new Type[2]
		{
			typeof(ItemData),
			typeof(UITooltip)
		}, (Type[])null);
		EnsureEquipmentInventory();
		_harmony = new Harmony("r4v9n1.equipmentsheet");
		_harmony.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Equipment Sheet 2.0.7 loaded.");
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Created by R4V9N1.");
	}

	private void OnDestroy()
	{
		if (_harmony != null)
		{
			_harmony.UnpatchSelf();
			_harmony = null;
		}
	}

	private void Update()
	{
		//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
		InventoryGui instance = InventoryGui.instance;
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer != (Object)null && (Object)(object)localPlayer != (Object)(object)_loadedPlayer)
		{
			ResetEphemeralCharacterState();
			LoadEquipmentInventory(localPlayer);
			_loadedPlayer = localPlayer;
		}
		if (_manualSheetDragItem != null && ((Object)(object)instance == (Object)null || _dragItemField == null || _dragItemField.GetValue(instance) != _manualSheetDragItem))
		{
			_manualSheetDragItem = null;
		}
		ProcessPendingAutoRouteItems(localPlayer);
		RestoreMarkedGraveItemsFromPlayerBag(localPlayer);
		if (!((Object)(object)_panel == (Object)null) && !((Object)(object)instance == (Object)null) && !((Object)(object)localPlayer == (Object)null))
		{
			bool flag = _enabled != null && _enabled.Value && (Object)(object)instance.m_player != (Object)null && ((Component)instance.m_player).gameObject.activeInHierarchy;
			if ((Object)(object)_panelCanvasGroup != (Object)null)
			{
				float num = (((Object)(object)_nativeInventoryCanvasGroup != (Object)null) ? _nativeInventoryCanvasGroup.alpha : 1f);
				_panelCanvasGroup.alpha = Mathf.Clamp01(num);
				_panelCanvasGroup.interactable = flag && ((Object)(object)_nativeInventoryCanvasGroup == (Object)null || _nativeInventoryCanvasGroup.interactable);
				_panelCanvasGroup.blocksRaycasts = flag && ((Object)(object)_nativeInventoryCanvasGroup == (Object)null || _nativeInventoryCanvasGroup.blocksRaycasts);
			}
			RectTransform player = instance.m_player;
			if ((Object)(object)player != (Object)null)
			{
				float num2 = ((_panelGap == null) ? 20f : _panelGap.Value) + ((_panelExtraRightOffset == null) ? 60f : _panelExtraRightOffset.Value);
				RectTransform panel = _panel;
				Vector2 anchoredPosition = player.anchoredPosition;
				Rect rect = player.rect;
				panel.anchoredPosition = anchoredPosition + new Vector2(((Rect)(ref rect)).width + num2, 0f);
			}
			if (((Component)_panel).gameObject.activeSelf != flag)
			{
				((Component)_panel).gameObject.SetActive(flag);
			}
			if (flag)
			{
				EquipmentSheetNativeSkin.Apply(instance);
				UpdateEquipmentGrid(instance, localPlayer);
			}
		}
	}

	internal static void SetupPanel(InventoryGui gui)
	{
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Expected O, but got Unknown
		//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0109: Unknown result type (might be due to invalid IL or missing references)
		//IL_011c: Unknown result type (might be due to invalid IL or missing references)
		//IL_012f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0134: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bf: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01da: Unknown result type (might be due to invalid IL or missing references)
		//IL_0264: Unknown result type (might be due to invalid IL or missing references)
		//IL_0226: Unknown result type (might be due to invalid IL or missing references)
		//IL_0234: Unknown result type (might be due to invalid IL or missing references)
		//IL_0286: Unknown result type (might be due to invalid IL or missing references)
		//IL_028b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0298: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_0326: Unknown result type (might be due to invalid IL or missing references)
		//IL_0541: Unknown result type (might be due to invalid IL or missing references)
		//IL_0546: Unknown result type (might be due to invalid IL or missing references)
		//IL_057a: Unknown result type (might be due to invalid IL or missing references)
		//IL_057f: Unknown result type (might be due to invalid IL or missing references)
		//IL_058d: Unknown result type (might be due to invalid IL or missing references)
		//IL_059e: Unknown result type (might be due to invalid IL or missing references)
		//IL_05b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_05c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_05da: Unknown result type (might be due to invalid IL or missing references)
		//IL_05e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_060b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0656: Unknown result type (might be due to invalid IL or missing references)
		//IL_065b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0669: Unknown result type (might be due to invalid IL or missing references)
		//IL_067a: Unknown result type (might be due to invalid IL or missing references)
		//IL_068f: Unknown result type (might be due to invalid IL or missing references)
		//IL_06a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_06b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_06c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_06ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_06d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_06e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_06e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_06fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_074e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0753: Unknown result type (might be due to invalid IL or missing references)
		//IL_0761: Unknown result type (might be due to invalid IL or missing references)
		//IL_076c: Unknown result type (might be due to invalid IL or missing references)
		//IL_077d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0792: Unknown result type (might be due to invalid IL or missing references)
		//IL_07a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_07b9: Unknown result type (might be due to invalid IL or missing references)
		//IL_07c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_07cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0819: Unknown result type (might be due to invalid IL or missing references)
		//IL_0855: Unknown result type (might be due to invalid IL or missing references)
		//IL_0869: Unknown result type (might be due to invalid IL or missing references)
		//IL_08ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_08c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_08cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0905: Unknown result type (might be due to invalid IL or missing references)
		//IL_091f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0924: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)_panel != (Object)null) && !((Object)(object)gui == (Object)null) && !((Object)(object)gui.m_player == (Object)null) && !((Object)(object)gui.m_playerGrid == (Object)null) && !((Object)(object)gui.m_playerGrid.m_elementPrefab == (Object)null))
		{
			EnsureEquipmentInventory();
			RectTransform player = gui.m_player;
			GameObject val = new GameObject("EquipmentSheetPanel", new Type[1] { typeof(RectTransform) });
			val.transform.SetParent(((Transform)player).parent, false);
			val.transform.SetSiblingIndex(((Transform)player).GetSiblingIndex() + 1);
			RectTransform component = val.GetComponent<RectTransform>();
			component.anchorMin = player.anchorMin;
			component.anchorMax = player.anchorMax;
			component.pivot = player.pivot;
			RectTransform component2 = gui.m_playerGrid.m_elementPrefab.GetComponent<RectTransform>();
			if ((Object)(object)component2 != (Object)null && component2.sizeDelta.x > 0f && component2.sizeDelta.y > 0f)
			{
				_cellSize = component2.sizeDelta;
			}
			float num = 3f * _cellSize.x + 8f + 24f;
			float num2 = 3f * _cellSize.y + 8f + 24f;
			float num3 = ((_panelGap == null) ? 20f : _panelGap.Value) + ((_panelExtraRightOffset == null) ? 60f : _panelExtraRightOffset.Value);
			component.sizeDelta = new Vector2(num, num2);
			Vector2 anchoredPosition = player.anchoredPosition;
			Rect rect = player.rect;
			component.anchoredPosition = anchoredPosition + new Vector2(((Rect)(ref rect)).width + num3, 0f);
			Image val2 = val.AddComponent<Image>();
			_panelCanvasGroup = val.AddComponent<CanvasGroup>();
			_nativeInventoryCanvasGroup = ((Component)player).GetComponent<CanvasGroup>();
			Image component3 = ((Component)player).GetComponent<Image>();
			if ((Object)(object)component3 != (Object)null)
			{
				val2.sprite = component3.sprite;
				val2.type = component3.type;
				((Graphic)val2).color = ((Graphic)component3).color;
				((Graphic)val2).material = ((Graphic)component3).material;
			}
			else
			{
				((Graphic)val2).color = new Color(0f, 0f, 0f, 0.6f);
			}
			GameObject val3 = new GameObject("EquipmentSheetGridRoot", new Type[1] { typeof(RectTransform) });
			val3.transform.SetParent((Transform)(object)component, false);
			RectTransform component4 = val3.GetComponent<RectTransform>();
			component4.anchorMin = new Vector2(0f, 1f);
			component4.anchorMax = new Vector2(0f, 1f);
			component4.pivot = new Vector2(0f, 1f);
			component4.anchoredPosition = new Vector2(12f, -12f);
			component4.sizeDelta = new Vector2(3f * _cellSize.x + 8f, 3f * _cellSize.y + 8f);
			_grid = val3.AddComponent<InventoryGrid>();
			_grid.m_elementPrefab = gui.m_playerGrid.m_elementPrefab;
			_grid.m_gridRoot = component4;
			if (_gridWidthField != null)
			{
				_gridWidthField.SetValue(_grid, 3);
			}
			if (_gridHeightField != null)
			{
				_gridHeightField.SetValue(_grid, 3);
			}
			_grid.m_elementSpace = 4f;
			_grid.m_tooltipAnchor = gui.m_playerGrid.m_tooltipAnchor;
			_grid.m_onSelected = delegate(InventoryGrid grid, ItemData item, Vector2i pos, Modifier mod)
			{
				//IL_0008: Unknown result type (might be due to invalid IL or missing references)
				//IL_0009: Unknown result type (might be due to invalid IL or missing references)
				HandleEquipmentGridSelected(gui, grid, item, pos, mod);
			};
			_grid.m_onRightClick = delegate(InventoryGrid grid, ItemData item, Vector2i pos)
			{
				//IL_0008: Unknown result type (might be due to invalid IL or missing references)
				HandleEquipmentGridSelected(gui, grid, item, pos, (Modifier)0);
			};
			_grid.OnMoveToLowerInventoryGrid = gui.m_playerGrid.OnMoveToLowerInventoryGrid;
			_grid.OnMoveToUpperInventoryGrid = gui.m_playerGrid.OnMoveToUpperInventoryGrid;
			_grid.m_uiGroup = gui.m_playerGrid.m_uiGroup;
			_slotPositions = (Vector2[])(object)new Vector2[Slots.Length];
			_slotFrames = (Image[])(object)new Image[Slots.Length];
			_slotIcons = (Image[])(object)new Image[Slots.Length];
			_slotLabels = (Text[])(object)new Text[Slots.Length];
			_slotAmounts = (Text[])(object)new Text[Slots.Length];
			_slotQualities = (Text[])(object)new Text[Slots.Length];
			_slotDurabilityBacks = (Image[])(object)new Image[Slots.Length];
			_slotDurabilityBars = (Image[])(object)new Image[Slots.Length];
			_slotHitboxes = (UIInputHandler[])(object)new UIInputHandler[Slots.Length];
			_slotTooltips = (UITooltip[])(object)new UITooltip[Slots.Length];
			Font builtinResource = Resources.GetBuiltinResource<Font>("Arial.ttf");
			for (int num4 = 0; num4 < Slots.Length; num4++)
			{
				int num5 = num4 % 3;
				int num6 = num4 / 3;
				_slotPositions[num4] = new Vector2((float)num5 * (_cellSize.x + 4f), (float)(-num6) * (_cellSize.y + 4f));
				GameObject val4 = new GameObject("EquipmentSheet_" + Slots[num4].Label + "_Frame", new Type[1] { typeof(RectTransform) });
				val4.transform.SetParent((Transform)(object)component4, false);
				RectTransform component5 = val4.GetComponent<RectTransform>();
				component5.anchorMin = new Vector2(0f, 1f);
				component5.anchorMax = new Vector2(0f, 1f);
				component5.pivot = new Vector2(0f, 1f);
				component5.anchoredPosition = _slotPositions[num4];
				component5.sizeDelta = _cellSize;
				Image val5 = val4.AddComponent<Image>();
				((Graphic)val5).color = new Color(0f, 0f, 0f, 0.45f);
				((Graphic)val5).raycastTarget = false;
				_slotFrames[num4] = val5;
				GameObject val6 = new GameObject("EquipmentSheet_" + Slots[num4].Label + "_Icon", new Type[1] { typeof(RectTransform) });
				val6.transform.SetParent((Transform)(object)component4, false);
				RectTransform component6 = val6.GetComponent<RectTransform>();
				component6.anchorMin = new Vector2(0f, 1f);
				component6.anchorMax = new Vector2(0f, 1f);
				component6.pivot = new Vector2(0f, 1f);
				component6.anchoredPosition = _slotPositions[num4] + new Vector2(4f, -4f);
				component6.sizeDelta = _cellSize - new Vector2(8f, 8f);
				Image val7 = val6.AddComponent<Image>();
				((Graphic)val7).color = Color.white;
				val7.preserveAspect = true;
				((Graphic)val7).raycastTarget = false;
				_slotIcons[num4] = val7;
				GameObject val8 = new GameObject("EquipmentSheet_" + Slots[num4].Label + "_Label", new Type[1] { typeof(RectTransform) });
				val8.transform.SetParent((Transform)(object)component4, false);
				val8.transform.SetAsLastSibling();
				RectTransform component7 = val8.GetComponent<RectTransform>();
				component7.anchorMin = new Vector2(0f, 1f);
				component7.anchorMax = new Vector2(0f, 1f);
				component7.pivot = new Vector2(0f, 1f);
				component7.anchoredPosition = _slotPositions[num4];
				component7.sizeDelta = _cellSize;
				Text val9 = val8.AddComponent<Text>();
				val9.text = Slots[num4].Label;
				val9.font = builtinResource;
				val9.fontSize = 12;
				val9.alignment = (TextAnchor)4;
				((Graphic)val9).color = new Color(1f, 1f, 1f, 0.95f);
				((Graphic)val9).raycastTarget = false;
				val9.horizontalOverflow = (HorizontalWrapMode)0;
				val9.verticalOverflow = (VerticalWrapMode)1;
				Outline obj = val8.AddComponent<Outline>();
				((Shadow)obj).effectColor = new Color(0f, 0f, 0f, 0.85f);
				((Shadow)obj).effectDistance = new Vector2(1f, -1f);
				_slotLabels[num4] = val9;
				_slotAmounts[num4] = CreateSlotCornerText(component4, builtinResource, "EquipmentSheet_" + Slots[num4].Label + "_Amount", _slotPositions[num4] + new Vector2(2f, 0f - _cellSize.y + 16f), (TextAnchor)3);
				_slotQualities[num4] = CreateSlotCornerText(component4, builtinResource, "EquipmentSheet_" + Slots[num4].Label + "_Quality", _slotPositions[num4] + new Vector2(_cellSize.x - 18f, -2f), (TextAnchor)5);
				CreateSlotDurabilityBar(component4, num4);
			}
			CreateSlotHitboxes(component4);
			_panel = component;
			val.SetActive(false);
			if (_log != null)
			{
				_log.LogInfo((object)"Equipment Sheet: created persistent equipment inventory panel.");
			}
		}
	}

	private static void EnsureEquipmentInventory()
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Expected O, but got Unknown
		if (_equipmentInventory == null)
		{
			_equipmentInventory = new Inventory("Equipment Sheet", (Sprite)null, 3, 3);
			HookEquipmentInventoryChanged();
		}
	}

	private static void HookEquipmentInventoryChanged()
	{
		if (_equipmentInventory == null || _onChangedField == null)
		{
			return;
		}
		Action action = _onChangedField.GetValue(_equipmentInventory) as Action;
		Action action2 = ValidateEquipmentInventory;
		bool flag = false;
		if (action != null)
		{
			Delegate[] invocationList = action.GetInvocationList();
			for (int i = 0; i < invocationList.Length; i++)
			{
				if (invocationList[i] == action2)
				{
					flag = true;
					break;
				}
			}
		}
		if (!flag)
		{
			_onChangedField.SetValue(_equipmentInventory, (Action)Delegate.Combine(action, action2));
		}
	}

	private static void UpdateEquipmentGrid(InventoryGui gui, Player player)
	{
		if (!((Object)(object)_grid == (Object)null) && _equipmentInventory != null)
		{
			UpdateSlotVisuals();
		}
	}

	private static Text CreateSlotCornerText(RectTransform parent, Font font, string name, Vector2 position, TextAnchor anchor)
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Expected O, but got Unknown
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: 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_007d: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject(name, new Type[1] { typeof(RectTransform) });
		val.transform.SetParent((Transform)(object)parent, false);
		RectTransform component = val.GetComponent<RectTransform>();
		component.anchorMin = new Vector2(0f, 1f);
		component.anchorMax = new Vector2(0f, 1f);
		component.pivot = new Vector2(0f, 1f);
		component.anchoredPosition = position;
		component.sizeDelta = new Vector2(16f, 14f);
		Text obj = val.AddComponent<Text>();
		obj.font = font;
		obj.fontSize = 10;
		obj.alignment = anchor;
		((Graphic)obj).color = Color.white;
		((Graphic)obj).raycastTarget = false;
		Outline obj2 = val.AddComponent<Outline>();
		((Shadow)obj2).effectColor = new Color(0f, 0f, 0f, 0.85f);
		((Shadow)obj2).effectDistance = new Vector2(1f, -1f);
		return obj;
	}

	private static void CreateSlotDurabilityBar(RectTransform parent, int slot)
	{
		//IL_002e: 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)
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0067: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Unknown result type (might be due to invalid IL or missing references)
		//IL_008d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a8: 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_00cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_0145: Unknown result type (might be due to invalid IL or missing references)
		//IL_014a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0157: Unknown result type (might be due to invalid IL or missing references)
		//IL_0168: Unknown result type (might be due to invalid IL or missing references)
		//IL_017d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0192: Unknown result type (might be due to invalid IL or missing references)
		//IL_019e: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ba: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject("EquipmentSheet_" + Slots[slot].Label + "_DurabilityBack", new Type[1] { typeof(RectTransform) });
		val.transform.SetParent((Transform)(object)parent, false);
		RectTransform component = val.GetComponent<RectTransform>();
		component.anchorMin = new Vector2(0f, 1f);
		component.anchorMax = new Vector2(0f, 1f);
		component.pivot = new Vector2(0f, 1f);
		component.anchoredPosition = _slotPositions[slot] + new Vector2(4f, 0f - _cellSize.y + 7f);
		component.sizeDelta = new Vector2(_cellSize.x - 8f, 4f);
		Image val2 = val.AddComponent<Image>();
		((Graphic)val2).color = new Color(0f, 0f, 0f, 0.8f);
		((Graphic)val2).raycastTarget = false;
		((Component)val2).gameObject.SetActive(false);
		_slotDurabilityBacks[slot] = val2;
		GameObject val3 = new GameObject("EquipmentSheet_" + Slots[slot].Label + "_DurabilityBar", new Type[1] { typeof(RectTransform) });
		val3.transform.SetParent((Transform)(object)parent, false);
		RectTransform component2 = val3.GetComponent<RectTransform>();
		component2.anchorMin = new Vector2(0f, 1f);
		component2.anchorMax = new Vector2(0f, 1f);
		component2.pivot = new Vector2(0f, 1f);
		component2.anchoredPosition = component.anchoredPosition;
		component2.sizeDelta = component.sizeDelta;
		Image val4 = val3.AddComponent<Image>();
		((Graphic)val4).color = Color.green;
		((Graphic)val4).raycastTarget = false;
		((Component)val4).gameObject.SetActive(false);
		_slotDurabilityBars[slot] = val4;
	}

	private static void UpdateSlotVisuals()
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0260: Unknown result type (might be due to invalid IL or missing references)
		if (_slotLabels == null)
		{
			return;
		}
		Vector2i val = default(Vector2i);
		for (int i = 0; i < _slotLabels.Length; i++)
		{
			if ((Object)(object)_slotLabels[i] == (Object)null)
			{
				continue;
			}
			((Vector2i)(ref val))..ctor(i % 3, i / 3);
			ItemData val2 = ((_equipmentInventory == null) ? null : _equipmentInventory.GetItemAt(val.x, val.y));
			bool flag = val2 != null && val2.m_shared != null;
			_slotLabels[i].text = Slots[i].Label;
			((Component)_slotLabels[i]).gameObject.SetActive(!flag);
			((Component)_slotLabels[i]).transform.SetAsLastSibling();
			if (_slotIcons != null && i < _slotIcons.Length && (Object)(object)_slotIcons[i] != (Object)null)
			{
				_slotIcons[i].sprite = (flag ? val2.GetIcon() : null);
				((Component)_slotIcons[i]).gameObject.SetActive(flag);
				((Component)_slotIcons[i]).transform.SetAsLastSibling();
			}
			if (_slotAmounts != null && i < _slotAmounts.Length && (Object)(object)_slotAmounts[i] != (Object)null)
			{
				_slotAmounts[i].text = ((flag && val2.m_stack > 1) ? val2.m_stack.ToString() : "");
				((Component)_slotAmounts[i]).gameObject.SetActive(flag && val2.m_stack > 1);
				((Component)_slotAmounts[i]).transform.SetAsLastSibling();
			}
			if (_slotQualities != null && i < _slotQualities.Length && (Object)(object)_slotQualities[i] != (Object)null)
			{
				_slotQualities[i].text = ((flag && val2.m_quality > 1) ? val2.m_quality.ToString() : "");
				((Component)_slotQualities[i]).gameObject.SetActive(flag && val2.m_quality > 1);
				((Component)_slotQualities[i]).transform.SetAsLastSibling();
			}
			UpdateSlotDurability(i, val2, flag);
			if (_slotTooltips != null && i < _slotTooltips.Length && (Object)(object)_slotTooltips[i] != (Object)null)
			{
				if (flag && (Object)(object)_grid != (Object)null)
				{
					SetSlotTooltip(val2, _slotTooltips[i]);
				}
				else
				{
					_slotTooltips[i].Set("", "", ((Object)(object)_grid == (Object)null) ? null : _grid.m_tooltipAnchor, Vector2.zero);
				}
			}
			if (_slotHitboxes != null && i < _slotHitboxes.Length && (Object)(object)_slotHitboxes[i] != (Object)null)
			{
				((Component)_slotHitboxes[i]).transform.SetAsLastSibling();
			}
		}
	}

	private static void UpdateSlotDurability(int slot, ItemData item, bool hasItem)
	{
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_0147: Unknown result type (might be due to invalid IL or missing references)
		//IL_0160: Unknown result type (might be due to invalid IL or missing references)
		//IL_0172: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		//IL_0120: Unknown result type (might be due to invalid IL or missing references)
		//IL_012c: Unknown result type (might be due to invalid IL or missing references)
		bool flag = hasItem && item.m_shared != null && item.m_shared.m_useDurability && item.GetMaxDurability() > 0f;
		if (_slotDurabilityBacks != null && slot < _slotDurabilityBacks.Length && (Object)(object)_slotDurabilityBacks[slot] != (Object)null)
		{
			((Component)_slotDurabilityBacks[slot]).gameObject.SetActive(flag);
			((Component)_slotDurabilityBacks[slot]).transform.SetAsLastSibling();
		}
		if (_slotDurabilityBars != null && slot < _slotDurabilityBars.Length && !((Object)(object)_slotDurabilityBars[slot] == (Object)null))
		{
			Image val = _slotDurabilityBars[slot];
			((Component)val).gameObject.SetActive(flag);
			((Component)val).transform.SetAsLastSibling();
			if (flag)
			{
				float num = Mathf.Clamp01(item.m_durability / item.GetMaxDurability());
				((Graphic)val).rectTransform.sizeDelta = new Vector2((_cellSize.x - 8f) * num, 4f);
				((Graphic)val).color = ((num > 0.5f) ? Color.Lerp(new Color(1f, 0.75f, 0.1f, 1f), new Color(0.1f, 0.85f, 0.15f, 1f), (num - 0.5f) * 2f) : Color.Lerp(new Color(0.85f, 0.1f, 0.05f, 1f), new Color(1f, 0.75f, 0.1f, 1f), num * 2f));
			}
		}
	}

	private static void CreateSlotHitboxes(RectTransform gridRoot)
	{
		//IL_0053: 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_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0081: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)gridRoot == (Object)null || _slotPositions == null)
		{
			return;
		}
		for (int i = 0; i < Slots.Length; i++)
		{
			int slotIndex = i;
			GameObject val = new GameObject("EquipmentSheet_" + Slots[i].Label + "_Hitbox", new Type[1] { typeof(RectTransform) });
			val.transform.SetParent((Transform)(object)gridRoot, false);
			val.transform.SetAsLastSibling();
			RectTransform component = val.GetComponent<RectTransform>();
			component.anchorMin = new Vector2(0f, 1f);
			component.anchorMax = new Vector2(0f, 1f);
			component.pivot = new Vector2(0f, 1f);
			component.anchoredPosition = _slotPositions[i];
			component.sizeDelta = _cellSize;
			Image obj = val.AddComponent<Image>();
			((Graphic)obj).color = new Color(0f, 0f, 0f, 0f);
			((Graphic)obj).raycastTarget = true;
			UIInputHandler val2 = val.AddComponent<UIInputHandler>();
			val2.m_onLeftDown = delegate
			{
				HandleSheetSlotClicked(slotIndex, rightClick: false);
			};
			val2.m_onRightDown = delegate
			{
				HandleSheetSlotClicked(slotIndex, rightClick: true);
			};
			_slotHitboxes[i] = val2;
			UITooltip val3 = val.AddComponent<UITooltip>();
			ConfigureSlotTooltip(val3);
			_slotTooltips[i] = val3;
		}
	}

	private static void ConfigureSlotTooltip(UITooltip tooltip)
	{
		if (!((Object)(object)tooltip == (Object)null))
		{
			InventoryGui instance = InventoryGui.instance;
			UITooltip val = (((Object)(object)instance != (Object)null && (Object)(object)instance.m_playerGrid != (Object)null && (Object)(object)instance.m_playerGrid.m_elementPrefab != (Object)null) ? instance.m_playerGrid.m_elementPrefab.GetComponentInChildren<UITooltip>(true) : null);
			if ((Object)(object)val != (Object)null)
			{
				tooltip.m_tooltipPrefab = val.m_tooltipPrefab;
				tooltip.m_gamepadFocusObject = val.m_gamepadFocusObject;
			}
		}
	}

	private static void SetSlotTooltip(ItemData item, UITooltip tooltip)
	{
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		if (item != null && !((Object)(object)tooltip == (Object)null))
		{
			if (_createItemTooltipMethod != null && (Object)(object)_grid != (Object)null)
			{
				_createItemTooltipMethod.Invoke(_grid, new object[2] { item, tooltip });
			}
			else
			{
				string text = ((item.m_shared == null) ? "" : item.m_shared.m_name);
				tooltip.Set(text, item.GetTooltip(-1), ((Object)(object)_grid == (Object)null) ? null : _grid.m_tooltipAnchor, Vector2.zero);
			}
		}
	}

	private static void HandleSheetSlotClicked(int slotIndex, bool rightClick)
	{
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		if (slotIndex < 0 || slotIndex >= Slots.Length || _equipmentInventory == null)
		{
			return;
		}
		InventoryGui instance = InventoryGui.instance;
		if (!((Object)(object)instance == (Object)null))
		{
			Vector2i val = default(Vector2i);
			((Vector2i)(ref val))..ctor(slotIndex % 3, slotIndex / 3);
			ItemData itemAt = _equipmentInventory.GetItemAt(val.x, val.y);
			if (IsFoodSlot(slotIndex) && itemAt != null && rightClick && !HasActiveDrag(instance))
			{
				TryConsumeFoodSlot(slotIndex, itemAt);
			}
			else if (IsEquipmentSlot(slotIndex) && itemAt != null && rightClick && !HasActiveDrag(instance))
			{
				MoveEquipmentItemToPlayerBag(itemAt);
			}
			else
			{
				HandleEquipmentGridSelected(instance, _grid, itemAt, val, (Modifier)0);
			}
		}
	}

	private static bool HasActiveDrag(InventoryGui gui)
	{
		if ((Object)(object)gui != (Object)null && _dragItemField != null)
		{
			return _dragItemField.GetValue(gui) is ItemData;
		}
		return false;
	}

	private static void TryConsumeFoodSlot(int slotIndex, ItemData item)
	{
		Player localPlayer = Player.m_localPlayer;
		if (!((Object)(object)localPlayer == (Object)null) && _equipmentInventory != null && IsFoodSlot(slotIndex) && MatchesSlot(item, slotIndex))
		{
			((Humanoid)localPlayer).UseItem(_equipmentInventory, item, true);
			RefreshEquipmentInventory();
			SaveEquipmentInventory(localPlayer);
		}
	}

	private static void ValidateEquipmentInventory()
	{
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		if (_loadingInventory || _equipmentInventory == null || HasProtectionSnapshot(Player.m_localPlayer))
		{
			return;
		}
		bool flag = false;
		List<ItemData> list = new List<ItemData>(_equipmentInventory.GetAllItems());
		for (int i = 0; i < list.Count; i++)
		{
			ItemData val = list[i];
			if (val != null && val.m_shared != null)
			{
				int slotIndex = GetSlotIndex(val.m_gridPos);
				if (slotIndex >= 0 && MatchesSlot(val, slotIndex))
				{
					SetEquippedFlag(val, IsEquipmentSlot(slotIndex));
					continue;
				}
				MoveItemBackToPlayerBag(val);
				flag = true;
			}
		}
		if (flag)
		{
			RefreshEquipmentInventory();
			ShowWrongItemTypeMessage();
		}
		SyncCharacterEquipment(Player.m_localPlayer);
		SaveEquipmentInventory(Player.m_localPlayer);
	}

	internal static void QueueAutoRouteEquippedItem(Humanoid humanoid, ItemData item, bool equipped)
	{
		Player val = (Player)(object)((humanoid is Player) ? humanoid : null);
		if (equipped && !((Object)(object)val == (Object)null) && !((Object)(object)val != (Object)(object)Player.m_localPlayer) && item != null && !HasProtectionSnapshot(val))
		{
			Inventory inventory = ((Humanoid)val).GetInventory();
			if (item == _manualSheetDragItem && inventory != null && inventory.ContainsItem(item))
			{
				_manualSheetDragItem = null;
				PendingAutoRouteItems.Remove(item);
				((Humanoid)val).UnequipItem(item, false);
			}
			else if (!((Object)(object)val != (Object)(object)_loadedPlayer) && _enabled != null && _enabled.Value && GetEquipmentSlotForItem(item) >= 0 && inventory != null && inventory.ContainsItem(item) && ((Humanoid)val).IsItemEquiped(item) && !PendingAutoRouteItems.Contains(item))
			{
				PendingAutoRouteItems.Add(item);
			}
		}
	}

	private static void ProcessPendingAutoRouteItems(Player player)
	{
		if (HasProtectionSnapshot(player))
		{
			PendingAutoRouteItems.Clear();
		}
		else if (PendingAutoRouteItems.Count != 0)
		{
			List<ItemData> list = new List<ItemData>(PendingAutoRouteItems);
			PendingAutoRouteItems.Clear();
			for (int i = 0; i < list.Count; i++)
			{
				MoveEquippedBagItemToSheet(player, list[i]);
			}
		}
	}

	private static void MoveEquippedBagItemToSheet(Player player, ItemData item)
	{
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_006f: 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_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00da: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: 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_00c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_010d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0114: Unknown result type (might be due to invalid IL or missing references)
		//IL_0135: Unknown result type (might be due to invalid IL or missing references)
		//IL_0137: Unknown result type (might be due to invalid IL or missing references)
		//IL_0159: Unknown result type (might be due to invalid IL or missing references)
		//IL_015b: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)player == (Object)null || item == null || _equipmentInventory == null || HasProtectionSnapshot(player) || !((Humanoid)player).IsItemEquiped(item))
		{
			return;
		}
		int equipmentSlotForItem = GetEquipmentSlotForItem(item);
		Inventory inventory = ((Humanoid)player).GetInventory();
		List<ItemData> inventoryItems = GetInventoryItems(inventory);
		List<ItemData> inventoryItems2 = GetInventoryItems(_equipmentInventory);
		if (equipmentSlotForItem < 0 || inventory == null || inventoryItems == null || inventoryItems2 == null || !inventoryItems.Contains(item))
		{
			return;
		}
		Vector2i val = default(Vector2i);
		((Vector2i)(ref val))..ctor(equipmentSlotForItem % 3, equipmentSlotForItem / 3);
		Vector2i gridPos = item.m_gridPos;
		if (inventory.GetItemAt(gridPos.x, gridPos.y) != item)
		{
			return;
		}
		ItemData itemAt = _equipmentInventory.GetItemAt(val.x, val.y);
		if (itemAt == item)
		{
			return;
		}
		inventoryItems.Remove(item);
		if (itemAt != null)
		{
			inventoryItems2.Remove(itemAt);
			itemAt.m_gridPos = gridPos;
			SetEquippedFlag(itemAt, equipped: false);
			inventoryItems.Add(itemAt);
		}
		item.m_gridPos = val;
		SetEquippedFlag(item, equipped: true);
		inventoryItems2.Add(item);
		if (_equipmentInventory.GetItemAt(val.x, val.y) != item || (itemAt != null && inventory.GetItemAt(gridPos.x, gridPos.y) != itemAt))
		{
			inventoryItems2.Remove(item);
			item.m_gridPos = gridPos;
			SetEquippedFlag(item, equipped: true);
			inventoryItems.Add(item);
			if (itemAt != null)
			{
				inventoryItems.Remove(itemAt);
				itemAt.m_gridPos = val;
				SetEquippedFlag(itemAt, equipped: true);
				inventoryItems2.Add(itemAt);
			}
		}
		else
		{
			RefreshEquipmentInventory();
			if (_inventoryChangedMethod != null)
			{
				NotifyInventoryChanged(inventory);
			}
			SaveEquipmentInventory(player);
		}
	}

	private static void MoveEquipmentItemToPlayerBag(ItemData item)
	{
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: 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_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e1: 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)
		if (HasProtectionSnapshot(Player.m_localPlayer))
		{
			return;
		}
		Player localPlayer = Player.m_localPlayer;
		Inventory val = (((Object)(object)localPlayer == (Object)null) ? null : ((Humanoid)localPlayer).GetInventory());
		List<ItemData> inventoryItems = GetInventoryItems(val);
		List<ItemData> inventoryItems2 = GetInventoryItems(_equipmentInventory);
		if ((Object)(object)localPlayer == (Object)null || item == null || val == null || inventoryItems == null || inventoryItems2 == null || !inventoryItems2.Contains(item))
		{
			return;
		}
		Vector2i val2 = FindFirstEmptyPosition(val);
		if (val2.x < 0)
		{
			((Character)localPlayer).Message((MessageType)2, "$inventory_full", 0, (Sprite)null, false);
			return;
		}
		Vector2i gridPos = item.m_gridPos;
		SavePendingTransfer(localPlayer, item);
		((Humanoid)localPlayer).UnequipItem(item, false);
		inventoryItems2.Remove(item);
		item.m_gridPos = val2;
		SetEquippedFlag(item, equipped: false);
		inventoryItems.Add(item);
		if (val.GetItemAt(val2.x, val2.y) != item || _equipmentInventory.ContainsItem(item))
		{
			inventoryItems.Remove(item);
			item.m_gridPos = gridPos;
			SetEquippedFlag(item, equipped: true);
			inventoryItems2.Add(item);
			SyncCharacterEquipment(localPlayer);
			ClearPendingTransfer(localPlayer);
		}
		else
		{
			RefreshEquipmentInventory();
			if (_inventoryChangedMethod != null)
			{
				NotifyInventoryChanged(val);
			}
			SaveEquipmentInventory(localPlayer);
			ClearPendingTransfer(localPlayer);
		}
	}

	private static Vector2i FindFirstEmptyPosition(Inventory inventory)
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_0045: 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 (inventory == null)
		{
			return new Vector2i(-1, -1);
		}
		int width = inventory.GetWidth();
		int height = inventory.GetHeight();
		for (int i = 0; i < height; i++)
		{
			for (int j = 0; j < width; j++)
			{
				if (inventory.GetItemAt(j, i) == null)
				{
					return new Vector2i(j, i);
				}
			}
		}
		return new Vector2i(-1, -1);
	}

	private static int GetEquipmentSlotForItem(ItemData item)
	{
		for (int i = 0; i < 6; i++)
		{
			if (MatchesSlot(item, i))
			{
				return i;
			}
		}
		return -1;
	}

	private static FieldInfo GetHumanoidFieldForType(ItemType type)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0003: Invalid comparison between Unknown and I4
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		//IL_0017: Invalid comparison between Unknown and I4
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Invalid comparison between Unknown and I4
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Invalid comparison between Unknown and I4
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Invalid comparison between Unknown and I4
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Invalid comparison between Unknown and I4
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Invalid comparison between Unknown and I4
		if ((int)type <= 11)
		{
			if ((int)type == 6)
			{
				return _humanoidHelmetField;
			}
			if ((int)type == 7)
			{
				return _humanoidChestField;
			}
			if ((int)type == 11)
			{
				return _humanoidLegField;
			}
		}
		else
		{
			if ((int)type == 17)
			{
				return _humanoidShoulderField;
			}
			if ((int)type == 18)
			{
				return _humanoidUtilityField;
			}
			if ((int)type == 24)
			{
				return _humanoidTrinketField;
			}
		}
		return null;
	}

	private static void SyncCharacterEquipment(Player player)
	{
		//IL_002e: 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_005d: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)player == (Object)null || _equipmentInventory == null || _setupEquipmentMethod == null)
		{
			return;
		}
		bool flag = false;
		Vector2i val = default(Vector2i);
		for (int i = 0; i < 6; i++)
		{
			FieldInfo humanoidFieldForType = GetHumanoidFieldForType(Slots[i].Type);
			if (humanoidFieldForType == null)
			{
				continue;
			}
			((Vector2i)(ref val))..ctor(i % 3, i / 3);
			ItemData val2 = _equipmentInventory.GetItemAt(val.x, val.y);
			if (val2 != null && (val2.m_shared == null || !MatchesSlot(val2, i)))
			{
				val2 = null;
			}
			object? value = humanoidFieldForType.GetValue(player);
			ItemData val3 = (ItemData)((value is ItemData) ? value : null);
			if (val3 == val2)
			{
				continue;
			}
			bool flag2 = val3 != null && _equipmentInventory.ContainsItem(val3);
			if (val2 != null || flag2)
			{
				if (val3 != null)
				{
					((Humanoid)player).UnequipItem(val3, false);
				}
				if (val2 != null)
				{
					humanoidFieldForType.SetValue(player, val2);
					SetEquippedFlag(val2, equipped: true);
				}
				flag = true;
			}
		}
		if (flag)
		{
			_setupEquipmentMethod.Invoke(player, null);
		}
	}

	internal static bool IsEquipmentGrid(InventoryGrid grid)
	{
		if ((Object)(object)grid != (Object)null && (Object)(object)_grid != (Object)null)
		{
			return (Object)(object)grid == (Object)(object)_grid;
		}
		return false;
	}

	internal static bool BeginContainerUpdateGuard(InventoryGui gui)
	{
		if ((Object)(object)gui == (Object)null || _dragInventoryField == null || _currentContainerField == null)
		{
			return false;
		}
		object? value = _dragInventoryField.GetValue(gui);
		Inventory val = (Inventory)((value is Inventory) ? value : null);
		if (val == null || val != _equipmentInventory)
		{
			return false;
		}
		object? value2 = _currentContainerField.GetValue(gui);
		if ((Object)((value2 is Container) ? value2 : null) != (Object)null)
		{
			return false;
		}
		Player localPlayer = Player.m_localPlayer;
		if ((Object)(object)localPlayer == (Object)null)
		{
			return false;
		}
		_dragInventoryField.SetValue(gui, ((Humanoid)localPlayer).GetInventory());
		return true;
	}

	internal static void EndContainerUpdateGuard(InventoryGui gui, bool didSwap)
	{
		if (didSwap && !((Object)(object)gui == (Object)null) && !(_dragInventoryField == null))
		{
			_dragInventoryField.SetValue(gui, _equipmentInventory);
		}
	}

	internal static bool TryDropIntoEquipmentSlot(Inventory fromInventory, ItemData item, int amount, Vector2i pos)
	{
		//IL_001c: 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_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: 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_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ab: 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)
		//IL_0105: Unknown result type (might be due to invalid IL or missing references)
		//IL_0110: Unknown result type (might be due to invalid IL or missing references)
		//IL_0111: Unknown result type (might be due to invalid IL or missing references)
		//IL_0129: Unknown result type (might be due to invalid IL or missing references)
		//IL_012f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0146: Unknown result type (might be due to invalid IL or missing references)
		//IL_0148: Unknown result type (might be due to invalid IL or missing references)
		if (_equipmentInventory == null || fromInventory == null || item == null || item.m_shared == null)
		{
			return false;
		}
		if (_equipmentInventory.GetItemAt(pos.x, pos.y) == item)
		{
			return true;
		}
		int slotIndex = GetSlotIndex(pos);
		if (slotIndex < 0 || !MatchesSlot(item, slotIndex))
		{
			ShowWrongItemTypeMessage();
			return false;
		}
		if (IsFoodSlot(slotIndex))
		{
			return TryDropIntoFoodSlot(fromInventory, item, amount, pos);
		}
		if (_equipmentInventory.GetItemAt(pos.x, pos.y) != null)
		{
			return false;
		}
		if (Mathf.Clamp((amount <= 0) ? item.m_stack : amount, 1, item.m_stack) != item.m_stack)
		{
			ShowWrongItemTypeMessage();
			return false;
		}
		if (fromInventory == _equipmentInventory)
		{
			item.m_gridPos = pos;
			SetEquippedFlag(item, equipped: true);
			RefreshEquipmentInventory();
			SaveEquipmentInventory(Player.m_localPlayer);
			return true;
		}
		Player localPlayer = Player.m_localPlayer;
		SavePendingTransfer(localPlayer, item, pos);
		List<ItemData> inventoryItems = GetInventoryItems(fromInventory);
		List<ItemData> inventoryItems2 = GetInventoryItems(_equipmentInventory);
		if (inventoryItems == null || inventoryItems2 == null || !inventoryItems.Contains(item))
		{
			ClearPendingTransfer(localPlayer);
			return false;
		}
		Vector2i gridPos = item.m_gridPos;
		inventoryItems.Remove(item);
		item.m_gridPos = pos;
		SetEquippedFlag(item, equipped: true);
		inventoryItems2.Add(item);
		if (_equipmentInventory.GetItemAt(pos.x, pos.y) != item)
		{
			inventoryItems2.Remove(item);
			item.m_gridPos = gridPos;
			SetEquippedFlag(item, equipped: false);
			inventoryItems.Add(item);
			RestorePendingTransfer(localPlayer);
			return false;
		}
		RefreshEquipmentInventory();
		if (_inventoryChangedMethod != null)
		{
			NotifyInventoryChanged(fromInventory);
		}
		SaveEquipmentInventory(Player.m_localPlayer);
		ClearPendingTransfer(localPlayer);
		return true;
	}

	private static bool TryDropIntoFoodSlot(Inventory fromInventory, ItemData item, int amount, Vector2i pos)
	{
		//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_006f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: 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)
		if (_equipmentInventory == null || fromInventory == null || item == null || !IsFoodItem(item))
		{
			return false;
		}
		int num = Mathf.Clamp((amount <= 0) ? item.m_stack : amount, 1, item.m_stack);
		ItemData itemAt = _equipmentInventory.GetItemAt(pos.x, pos.y);
		if (itemAt == item)
		{
			return true;
		}
		if (fromInventory == _equipmentInventory)
		{
			return TryMoveFoodInsideSheet(item, num, itemAt, pos);
		}
		int stack = item.m_stack;
		bool flag = _equipmentInventory.MoveItemToThis(fromInventory, item, num, pos.x, pos.y);
		if (!(item.m_stack != stack || flag))
		{
			return false;
		}
		SetEquippedFlag(item, equipped: false);
		RefreshEquipmentInventory();
		if (_inventoryChangedMethod != null)
		{
			NotifyInventoryChanged(fromInventory);
		}
		SaveEquipmentInventory(Player.m_localPlayer);
		return true;
	}

	private static bool TryMoveFoodInsideSheet(ItemData item, int amount, ItemData target, Vector2i pos)
	{
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		List<ItemData> inventoryItems = GetInventoryItems(_equipmentInventory);
		if (inventoryItems == null || item == null || !inventoryItems.Contains(item))
		{
			return false;
		}
		if (target == null)
		{
			item.m_gridPos = pos;
			SetEquippedFlag(item, equipped: false);
			RefreshEquipmentInventory();
			SaveEquipmentInventory(Player.m_localPlayer);
			return true;
		}
		if (!CanStackFood(target, item))
		{
			return false;
		}
		int num = target.m_shared.m_maxStackSize - target.m_stack;
		if (num <= 0)
		{
			return false;
		}
		int num2 = Mathf.Min(Mathf.Clamp(amount, 1, item.m_stack), num);
		target.m_stack += num2;
		item.m_stack -= num2;
		SetEquippedFlag(target, equipped: false);
		SetEquippedFlag(item, equipped: false);
		if (item.m_stack <= 0)
		{
			inventoryItems.Remove(item);
		}
		RefreshEquipmentInventory();
		SaveEquipmentInventory(Player.m_localPlayer);
		return true;
	}

	private static bool CanStackFood(ItemData target, ItemData item)
	{
		if (target == null || item == null || target.m_shared == null || item.m_shared == null)
		{
			return false;
		}
		if (target.m_shared.m_name == item.m_shared.m_name && target.m_quality == item.m_quality && target.m_worldLevel == item.m_worldLevel && target.m_variant == item.m_variant && Math.Abs(target.m_durability - item.m_durability) <= 0.001f && ItemCustomDataEqual(target, item))
		{
			return target.m_shared.m_maxStackSize > 1;
		}
		return false;
	}

	private static bool TryInsertExact(Inventory target, ItemData item, Vector2i position, bool notifyChanged = true)
	{
		//IL_0013: 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)
		//IL_0025: 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)
		//IL_0042: 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_0077: 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)
		if (target == null || item == null || _inventoryItemsField == null || position.x < 0 || position.y < 0 || position.x >= target.GetWidth() || position.y >= target.GetHeight() || target.GetItemAt(position.x, position.y) != null)
		{
			return false;
		}
		if (!(_inventoryItemsField.GetValue(target) is List<ItemData> list) || list.Contains(item))
		{
			return false;
		}
		item.m_gridPos = position;
		list.Add(item);
		if (notifyChanged)
		{
			NotifyInventoryChanged(target);
		}
		return true;
	}

	private static void HandleEquipmentGridSelected(InventoryGui gui, InventoryGrid grid, ItemData item, Vector2i pos, Modifier mod)
	{
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		if (!IsEquipmentGrid(grid) || (Object)(object)gui == (Object)null)
		{
			return;
		}
		ItemData val = (ItemData)((_dragItemField == null) ? null : /*isinst with value type is only supported in some contexts*/);
		Inventory fromInventory = (Inventory)((_dragInventoryField == null) ? null : /*isinst with value type is only supported in some contexts*/);
		int amount = ((!(_dragAmountField == null)) ? ((int)_dragAmountField.GetValue(gui)) : 0);
		if (val != null)
		{
			if (TryDropIntoEquipmentSlot(fromInventory, val, amount, pos))
			{
				ClearDragItem(gui);
			}
		}
		else if (item != null && _setupDragItemMethod != null)
		{
			SetEquippedFlag(item, equipped: false);
			_manualSheetDragItem = item;
			_setupDragItemMethod.Invoke(gui, new object[3] { item, _equipmentInventory, item.m_stack });
		}
	}

	private static void ClearDragItem(InventoryGui gui)
	{
		if (!((Object)(object)gui == (Object)null))
		{
			ItemData val = (ItemData)((_dragItemField == null) ? null : /*isinst with value type is only supported in some contexts*/);
			if (_manualSheetDragItem == val)
			{
				_manualSheetDragItem = null;
			}
			GameObject val2 = (GameObject)((_dragGoField == null) ? null : /*isinst with value type is only supported in some contexts*/);
			if ((Object)(object)val2 != (Object)null)
			{
				Object.Destroy((Object)(object)val2);
			}
			if (_dragGoField != null)
			{
				_dragGoField.SetValue(gui, null);
			}
			if (_dragItemField != null)
			{
				_dragItemField.SetValue(gui, null);
			}
			if (_dragInventoryField != null)
			{
				_dragInventoryField.SetValue(gui, null);
			}
			if (_dragAmountField != null)
			{
				_dragAmountField.SetValue(gui, 0);
			}
		}
	}

	private static int GetSlotIndex(Vector2i pos)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: 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)
		if (pos.x < 0 || pos.x >= 3 || pos.y < 0 || pos.y >= 3)
		{
			return -1;
		}
		int num = pos.y * 3 + pos.x;
		if (num < 0 || num >= Slots.Length)
		{
			return -1;
		}
		return num;
	}

	private static bool IsEquipmentSlot(int slot)
	{
		if (slot >= 0 && slot < 6 && slot < Slots.Length)
		{
			return Slots[slot].Kind == SlotKind.Equipment;
		}
		return false;
	}

	private static bool IsFoodSlot(int slot)
	{
		if (slot >= 6 && slot < Slots.Length)
		{
			return Slots[slot].Kind == SlotKind.Food;
		}
		return false;
	}

	private static bool MatchesSlot(ItemData item, int slot)
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		if (item == null || item.m_shared == null || slot < 0 || slot >= Slots.Length)
		{
			return false;
		}
		if (IsFoodSlot(slot))
		{
			return IsFoodItem(item);
		}
		if (!IsEquipmentSlot(slot))
		{
			return false;
		}
		ItemType itemType = item.m_shared.m_itemType;
		if (IsExplicitlyExcludedEquipmentType(itemType))
		{
			return false;
		}
		return itemType == Slots[slot].Type;
	}

	private static bool IsFoodItem(ItemData item)
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Invalid comparison between Unknown and I4
		if (item == null || item.m_shared == null)
		{
			return false;
		}
		if ((int)item.m_shared.m_itemType == 2)
		{
			if (!(item.m_shared.m_food > 0f) && !(item.m_shared.m_foodStamina > 0f) && !(item.m_shared.m_foodEitr > 0f))
			{
				return (Object)(object)item.m_shared.m_consumeStatusEffect != (Object)null;
			}
			return true;
		}
		return false;
	}

	private unsafe static bool IsExplicitlyExcludedEquipmentType(ItemType type)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0002: Invalid comparison between Unknown and I4
		//IL_0004: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Invalid comparison between Unknown and I4
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Invalid comparison between Unknown and I4
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Invalid comparison between Unknown and I4
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		//IL_0015: Invalid comparison between Unknown and I4
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Invalid comparison between Unknown and I4
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Invalid comparison between Unknown and I4
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Invalid comparison between Unknown and I4
		if ((int)type == 4 || (int)type == 3 || (int)type == 14 || (int)type == 22 || (int)type == 20 || (int)type == 5 || (int)type == 19 || (int)type == 15)
		{
			return true;
		}
		string text = ((object)(*(ItemType*)(&type))/*cast due to .constrained prefix*/).ToString();
		if (text.IndexOf("Bow", StringComparison.OrdinalIgnoreCase) < 0)
		{
			return text.IndexOf("Ranged", StringComparison.OrdinalIgnoreCase) >= 0;
		}
		return true;
	}

	private static void MoveItemBackToPlayerBag(ItemData item)
	{
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		Player localPlayer = Player.m_localPlayer;
		Inventory val = (((Object)(object)localPlayer == (Object)null) ? null : ((Humanoid)localPlayer).GetInventory());
		if (HasProtectionSnapshot(localPlayer) || item == null || val == null || _equipmentInventory == null || !_equipmentInventory.ContainsItem(item))
		{
			return;
		}
		SetEquippedFlag(item, equipped: false);
		if (!val.AddItem(item))
		{
			int slotIndex = GetSlotIndex(item.m_gridPos);
			SetEquippedFlag(item, slotIndex >= 0 && IsEquipmentSlot(slotIndex) && MatchesSlot(item, slotIndex));
			if (_log != null)
			{
				_log.LogWarning((object)("Equipment Sheet: invalid item '" + GetItemDisplayName(item) + "' is in an equipment slot, but the player bag is full."));
			}
		}
		else
		{
			_equipmentInventory.RemoveItem(item);
			if (_log != null)
			{
				_log.LogInfo((object)("Equipment Sheet: moved invalid item '" + GetItemDisplayName(item) + "' back to the player bag."));
			}
		}
	}

	internal static void SaveEquipmentInventory(Player player)
	{
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Expected O, but got Unknown
		if ((Object)(object)player == (Object)null || (Object)(object)player != (Object)(object)_loadedPlayer || !_loadedInventorySaveSafe || _equipmentInventory == null)
		{
			if ((Object)(object)player != (Object)null && !_loadedInventorySaveSafe)
			{
				ManualLogSource log = _log;
				if (log != null)
				{
					log.LogWarning((object)"Equipment Sheet: skipped save because the character sheet failed validation; durable recovery data was preserved.");
				}
			}
		}
		else
		{
			Dictionary<string, string> customData = GetCustomData(player);
			if (customData != null)
			{
				ZPackage val = new ZPackage();
				_equipmentInventory.Save(val);
				customData["is.codex.valheim.equipmentsheet.inventory.v1"] = val.GetBase64();
			}
		}
	}

	internal static void CacheSheetForProtection(Player player)
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Expected O, but got Unknown
		if ((Object)(object)player == (Object)null || _equipmentInventory == null)
		{
			return;
		}
		Dictionary<string, string> customData = GetCustomData(player);
		if (customData != null)
		{
			ZPackage val = new ZPackage();
			_equipmentInventory.Save(val);
			customData["is.codex.valheim.equipmentsheet.protection-snapshot.v1"] = val.GetBase64();
			SaveEquipmentInventory(player);
			ManualLogSource log = _log;
			if (log != null)
			{
				log.LogInfo((object)"Equipment Sheet: cached dedicated slots when external death protection was activated.");
			}
		}
	}

	internal static bool TryConsumeNamedItem(string sharedName, Player player)
	{
		if (_equipmentInventory == null || string.IsNullOrEmpty(sharedName))
		{
			return false;
		}
		List<ItemData> allItems = _equipmentInventory.GetAllItems();
		for (int i = 0; i < allItems.Count; i++)
		{
			ItemData val = allItems[i];
			if (val != null && val.m_shared != null && string.Equals(val.m_shared.m_name, sharedName, StringComparison.Ordinal))
			{
				bool num = _equipmentInventory.RemoveOneItem(val);
				if (num)
				{
					RefreshEquipmentInventory();
					SaveEquipmentInventory(player);
				}
				return num;
			}
		}
		return false;
	}

	private static void SavePendingTransfer(Player player, ItemData item)
	{
		if (item != null)
		{
			SavePendingTransfer(player, new List<ItemData> { item });
		}
	}

	private static void SavePendingTransfer(Player player, ItemData item, Vector2i destination)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		if (item != null)
		{
			ItemData val = item.Clone();
			val.m_gridPos = destination;
			SavePendingTransfer(player, new List<ItemData> { val });
		}
	}

	private static void SavePendingTransfer(Player player, List<ItemData> items)
	{
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Expected O, but got Unknown
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Expected O, but got Unknown
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: 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)
		if ((Object)(object)player == (Object)null || items == null || items.Count == 0)
		{
			return;
		}
		Dictionary<string, string> customData = GetCustomData(player);
		if (customData == null)
		{
			return;
		}
		Inventory val = new Inventory("EquipmentSheetPending", (Sprite)null, 3, 3);
		for (int i = 0; i < items.Count; i++)
		{
			ItemData val2 = items[i];
			if (val2 == null)
			{
				continue;
			}
			ItemData val3 = val2.Clone();
			Vector2i gridPos = val2.m_gridPos;
			if (!val.AddItem(val3, gridPos))
			{
				ManualLogSource log = _log;
				if (log != null)
				{
					log.LogWarning((object)("Equipment Sheet: failed to cache item " + val2.m_shared?.m_name));
				}
			}
		}
		ZPackage val4 = new ZPackage();
		val.Save(val4);
		customData["is.codex.valheim.equipmentsheet.pending-transfer.v1"] = val4.GetBase64();
	}

	private static void ClearPendingTransfer(Player player)
	{
		GetCustomData(player)?.Remove("is.codex.valheim.equipmentsheet.pending-transfer.v1");
	}

	private static void RestorePendingTransfer(Player player)
	{
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Expected O, but got Unknown
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Expected O, but got Unknown
		//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_011e: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)player == (Object)null)
		{
			return;
		}
		Dictionary<string, string> customData = GetCustomData(player);
		if (customData == null || !customData.TryGetValue("is.codex.valheim.equipmentsheet.pending-transfer.v1", out var value) || string.IsNullOrEmpty(value))
		{
			return;
		}
		Inventory inventory = ((Humanoid)player).GetInventory();
		if (inventory == null)
		{
			return;
		}
		try
		{
			Inventory val = new Inventory("EquipmentSheetPending", (Sprite)null, 1, 1);
			ZPackage val2 = new ZPackage();
			val2.Load(Convert.FromBase64String(value));
			val.Load(val2);
			List<ItemData> list = new List<ItemData>(val.GetAllItems());
			List<ItemData> list2 = new List<ItemData>();
			for (int i = 0; i < list.Count; i++)
			{
				ItemData val3 = list[i];
				if (val3 == null)
				{
					continue;
				}
				ItemData val4 = FindRecoveredItem(inventory, val3);
				if (val4 == null)
				{
					continue;
				}
				int slotIndex = GetSlotIndex(val3.m_gridPos);
				if (slotIndex >= 0 && slotIndex < 6 && IsEquipmentSlot(slotIndex) && MatchesSlot(val3, slotIndex) && _equipmentInventory != null && _equipmentInventory.GetItemAt(val3.m_gridPos.x, val3.m_gridPos.y) == null)
				{
					ItemData val5 = val4.Clone();
					SetEquippedFlag(val5, equipped: true);
					if (TryInsertExact(_equipmentInventory, val5, val3.m_gridPos) && inventory.RemoveItem(val4))
					{
						list2.Add(val3);
						continue;
					}
					_equipmentInventory.RemoveItem(val5);
					SetEquippedFlag(val4, equipped: false);
				}
				else if (slotIndex >= 0 && IsFoodSlot(slotIndex) && _equipmentInventory != null && _equipmentInventory.GetItemAt(val3.m_gridPos.x, val3.m_gridPos.y) == null)
				{
					ItemData val6 = val4.Clone();
					SetEquippedFlag(val6, equipped: false);
					if (TryInsertExact(_equipmentInventory, val6, val3.m_gridPos) && inventory.RemoveItem(val4))
					{
						list2.Add(val3);
					}
					else
					{
						_equipmentInventory.RemoveItem(val6);
					}
				}
			}
			if (list2.Count > 0)
			{
				for (int j = 0; j < list2.Count; j++)
				{
					val.RemoveItem(list2[j]);
				}
			}
			if (val.NrOfItems() == 0)
			{
				ClearPendingTransfer(player);
			}
			MarkEquipmentItemsEquipped();
			RefreshEquipmentInventory();
			SyncCharacterEquipment(player);
			if (_inventoryChangedMethod != null)
			{
				NotifyInventoryChanged(inventory);
			}
			if (_log != null)
			{
				_log.LogInfo((object)("Equipment Sheet: restored " + list2.Count + " recovered item(s) to their saved sheet slots."));
			}
		}
		catch (Exception ex)
		{
			if (_log != null)
			{
				_log.LogWarning((object)("Equipment Sheet: failed to restore pending transfer: " + ex.Message));
			}
		}
	}

	private static void ResetEphemeralCharacterState()
	{
		PendingAutoRouteItems.Clear();
		_manualSheetDragItem = null;
		_knownTombstoneInventories.Clear();
		_restoringMarkedGraveItems = false;
		_upgradeRecipeScanDepth = 0;
		_sheetUpgradeTransaction = null;
	}

	internal static void LoadEquipmentInventory(Player player)
	{
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Expected O, but got Unknown
		//IL_0057: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_0069: Expected O, but got Unknown
		EnsureEquipmentInventory();
		Dictionary<string, string> customData = GetCustomData(player);
		if (customData == null || !customData.TryGetValue("is.codex.valheim.equipmentsheet.inventory.v1", out var value) || string.IsNullOrEmpty(value))
		{
			ClearEquipmentInventory(player, clearSavedData: false);
			_loadedInventorySaveSafe = true;
			return;
		}
		try
		{
			_loadingInventory = true;
			byte[] array = Convert.FromBase64String(value);
			ZPackage val = new ZPackage();
			val.Load(array);
			new Inventory("Equipment Sheet Load Validation", (Sprite)null, 3, 3).Load(val);
			ZPackage val2 = new ZPackage();
			val2.Load(array);
			List<ItemData> oldItems = new List<ItemData>(_equipmentInventory.GetAllItems());
			_equipmentInventory.RemoveAll();
			ClearCharacterEquipmentReferences(player, oldItems);
			_equipmentInventory.Load(val2);
			MarkEquipmentItemsEquipped();
			RefreshEquipmentInventory();
			_loadedInventorySaveSafe = true;
			if (_log != null)
			{
				_log.LogInfo((object)"Equipment Sheet: atomically loaded character-specific persistent equipment inventory.");
			}
		}
		catch (Exception ex)
		{
			ClearEquipmentInventory(player, clearSavedData: false);
			_loadedInventorySaveSafe = false;
			if (_log != null)
			{
				_log.LogWarning((object)("Equipment Sheet: failed to load persistent equipment inventory; cleared runtime sheet to prevent cross-character state leakage: " + ex.Message));
			}
		}
		finally
		{
			_loadingInventory = false;
		}
		SyncCharacterEquipment(player);
	}

	internal static void SnapshotSheetForDeath(Player player)
	{
		if (HasExternalDeathProtection(player))
		{
			ManualLogSource log = _log;
			if (log != null)
			{
				log.LogInfo((object)"Equipment Sheet: preserved sheet slots because active death protection is enabled.");
			}
			ClearProtectionSnapshot(player);
		}
		else if (!((Object)(object)player == (Object)null) && _equipmentInventory != null && _equipmentInventory.NrOfItems() != 0)
		{
			SaveDeathSnapshot(player);
			ManualLogSource log2 = _log;
			if (log2 != null)
			{
				log2.LogInfo((object)"Equipment Sheet: prepared transactional death snapshot for dedicated grave slots.");
			}
		}
	}

	internal static bool HasExternalDeathProtection(Player player)
	{
		if ((Object)(object)player == (Object)null)
		{
			return false;
		}
		if (HasProtectionSnapshot(player))
		{
			return true;
		}
		try
		{
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			for (int i = 0; i < assemblies.Length; i++)
			{
				Type type = assemblies[i].GetType("BrennivinProtection.BrennivinPlugin", throwOnError: false);
				if (!(type == null))
				{
					MethodInfo method = type.GetMethod("IsProtectionBuffActive", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
					if (method == null || method.ReturnType != typeof(bool))
					{
						return false;
					}
					return (bool)method.Invoke(null, new object[1] { player });
				}
			}
		}
		catch (Exception ex)
		{
			ManualLogSource log = _log;
			if (log != null)
			{
				log.LogWarning((object)("Equipment Sheet: external death-protection check failed: " + ex.Message));
			}
		}
		return false;
	}

	private static bool HasProtectionSnapshot(Player player)
	{
		return GetCustomData(player)?.ContainsKey("is.codex.valheim.equipmentsheet.protection-snapshot.v1") ?? false;
	}

	internal static void ClearProtectionSnapshot(Player player)
	{
		GetCustomData(player)?.Remove("is.codex.valheim.equipmentsheet.protection-snapshot.v1");
	}

	private static void SaveDeathSnapshot(Player player)
	{
		//IL_001c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Expected O, but got Unknown
		if (!((Object)(object)player == (Object)null) && _equipmentInventory != null)
		{
			Dictionary<string, string> customData = GetCustomData(player);
			if (customData != null)
			{
				ZPackage val = new ZPackage();
				_equipmentInventory.Save(val);
				customData["is.codex.valheim.equipmentsheet.death-snapshot.v2"] = val.GetBase64();
			}
		}
	}

	private static void ClearDeathSnapshot(Player player)
	{
		GetCustomData(player)?.Remove("is.codex.valheim.equipmentsheet.death-snapshot.v2");
	}

	private static bool TryLoadDeathSnapshot(Player player, out Inventory pending)
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: Expected O, but got Unknown
		//IL_0040: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Expected O, but got Unknown
		pending = null;
		if ((Object)(object)player == (Object)null)
		{
			return false;
		}
		Dictionary<string, string> customData = GetCustomData(player);
		if (customData == null || !customData.TryGetValue("is.codex.valheim.equipmentsheet.death-snapshot.v2", out var value) || string.IsNullOrEmpty(value))
		{
			return false;
		}
		try
		{
			Inventory val = new Inventory("EquipmentSheetDeathSnapshot", (Sprite)null, 3, 3);
			ZPackage val2 = new ZPackage();
			val2.Load(Convert.FromBase64String(value));
			val.Load(val2);
			pending = val;
			return true;
		}
		catch (Exception ex)
		{
			ManualLogSource log = _log;
			if (log != null)
			{
				log.LogWarning((object)("Equipment Sheet: failed to load death snapshot: " + ex.Message));
			}
			return false;
		}
	}

	internal static void PrepareTombstoneInventory(Inventory grave, Inventory original)
	{
		Player localPlayer = Player.m_localPlayer;
		if (grave != null && original != null && !((Object)(object)localPlayer == (Object)null) && original == ((Humanoid)localPlayer).GetInventory() && TryLoadDeathSnapshot(localPlayer, out var pending) && pending.NrOfItems() != 0 && !(_inventoryWidthField == null) && !(_inventoryHeightField == null))
		{
			int width = original.GetWidth();
			int height = original.GetHeight();
			_inventoryWidthField.SetValue(grave, width + 3);
			_inventoryHeightField.SetValue(grave, Math.Max(height, 3));
			_knownTombstoneInventories[grave] = width;
			ManualLogSource log = _log;
			if (log != null)
			{
				log.LogInfo((object)"Equipment Sheet: reserved a dedicated 3x3 tombstone region before vanilla inventory transfer.");
			}
		}
	}

	private static Vector2i GetGraveSlotPosition(int bagWidth, Vector2i sheetPosition)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: 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)
		return new Vector2i(bagWidth + sheetPosition.x, sheetPosition.y);
	}

	private static Dictionary<string, string> GetOrCreateItemCustomData(ItemData item)
	{
		if (item == null || _itemCustomDataField == null)
		{
			return null;
		}
		Dictionary<string, string> dictionary = _itemCustomDataField.GetValue(item) as Dictionary<string, string>;
		if (dictionary == null)
		{
			dictionary = new Dictionary<string, string>();
			_itemCustomDataField.SetValue(item, dictionary);
		}
		return dictionary;
	}

	private static void SetGraveSlotMetadata(ItemData item, Vector2i sheetPosition)
	{
		Dictionary<string, string> orCreateItemCustomData = GetOrCreateItemCustomData(item);
		if (orCreateItemCustomData != null)
		{
			orCreateItemCustomData["is.codex.valheim.equipmentsheet.grave-slot.v2"] = sheetPosition.x + "," + sheetPosition.y;
		}
	}

	private static bool TryGetGraveSlotMetadata(ItemData item, out Vector2i sheetPosition)
	{
		//IL_0003: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		sheetPosition = new Vector2i(-1, -1);
		if (item == null || _itemCustomDataField == null)
		{
			return false;
		}
		if (!(_itemCustomDataField.GetValue(item) is Dictionary<string, string> dictionary) || !dictionary.TryGetValue("is.codex.valheim.equipmentsheet.grave-slot.v2", out var value) || string.IsNullOrEmpty(value))
		{
			return false;
		}
		string[] array = value.Split(new char[1] { ',' });
		if (array.Length != 2 || !int.TryParse(array[0], out var result) || !int.TryParse(array[1], out var result2) || result < 0 || result >= 3 || result2 < 0 |