Decompiled source of Megingjord Overhaul v1.1.0

MegingjordOverhaul.dll

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

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace MegingjordOverhaul
{
	public static class BeltInventoryManager
	{
		private const string DataKey = "MegingjordOverhaul_BeltInventory";

		private const string MegingjordPrefab = "BeltStrength";

		private const int BeltWidth = 8;

		private const int BeltHeight = 1;

		private static Inventory _beltInventory;

		private static bool _beltEquipped;

		private static readonly FieldInfo TrinketItemField = AccessTools.Field(typeof(Humanoid), "m_trinketItem");

		private static readonly FieldInfo UtilityItemField = AccessTools.Field(typeof(Humanoid), "m_utilityItem");

		private static readonly FieldInfo InventoryItemsField = AccessTools.Field(typeof(Inventory), "m_inventory");

		private static readonly MethodInfo InventoryChangedMethod = AccessTools.Method(typeof(Inventory), "Changed", (Type[])null, (Type[])null);

		public static Inventory GetBeltInventory()
		{
			return _beltInventory;
		}

		public static bool IsBeltEquipped()
		{
			return _beltEquipped;
		}

		public static bool IsMegingjord(ItemData item)
		{
			if (item == null)
			{
				return false;
			}
			if ((Object)(object)item.m_dropPrefab != (Object)null && Utils.GetPrefabName(item.m_dropPrefab) == "BeltStrength")
			{
				return true;
			}
			return item.m_shared?.m_name == "$item_beltstrength";
		}

		public static bool HasMegingjordEquipped(Humanoid humanoid)
		{
			object? obj = TrinketItemField?.GetValue(humanoid);
			ItemData val = (ItemData)((obj is ItemData) ? obj : null);
			if (val != null && IsMegingjord(val))
			{
				return true;
			}
			object? obj2 = UtilityItemField?.GetValue(humanoid);
			ItemData val2 = (ItemData)((obj2 is ItemData) ? obj2 : null);
			if (val2 != null && IsMegingjord(val2))
			{
				return true;
			}
			return false;
		}

		public static void OnMegingjordEquipped(Player player)
		{
			if (!_beltEquipped)
			{
				_beltEquipped = true;
				EnsureBeltInventory();
				LoadBeltData(player);
				BeltInventoryUI.Show();
				Plugin.Log.LogInfo((object)"Megingjord equipped — belt inventory active.");
			}
		}

		public static void OnMegingjordUnequipped(Player player)
		{
			if (_beltEquipped)
			{
				_beltEquipped = false;
				DropBeltItems(player);
				SaveBeltData(player);
				BeltInventoryUI.Hide();
				Plugin.Log.LogInfo((object)"Megingjord unequipped — belt inventory hidden.");
			}
		}

		public static void EnsureBeltInventory()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Expected O, but got Unknown
			if (_beltInventory == null)
			{
				_beltInventory = new Inventory("Belt", (Sprite)null, 8, 1);
			}
		}

		private static void DropBeltItems(Player player)
		{
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: 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_0045: 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_004f: 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_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			if (_beltInventory == null)
			{
				return;
			}
			foreach (ItemData item in new List<ItemData>(_beltInventory.GetAllItems()))
			{
				Vector3 val = ((Component)player).transform.position + ((Component)player).transform.forward + Vector3.up;
				ItemDrop val2 = ItemDrop.DropItem(item, item.m_stack, val, ((Component)player).transform.rotation);
				if ((Object)(object)val2 != (Object)null)
				{
					val2.OnPlayerDrop();
					Rigidbody component = ((Component)val2).GetComponent<Rigidbody>();
					if ((Object)(object)component != (Object)null)
					{
						component.linearVelocity = (((Component)player).transform.forward + Vector3.up) * 5f;
					}
					_beltInventory.RemoveItem(item);
				}
				else
				{
					Plugin.Log.LogWarning((object)("Failed to drop belt item: " + item.m_shared.m_name));
				}
			}
		}

		public static void SaveBeltData(Player player)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Expected O, but got Unknown
			if (_beltInventory == null || (Object)(object)player == (Object)null)
			{
				return;
			}
			try
			{
				ZPackage val = new ZPackage();
				_beltInventory.Save(val);
				player.m_customData["MegingjordOverhaul_BeltInventory"] = val.GetBase64();
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Failed to save belt inventory: {arg}");
			}
		}

		public static void LoadBeltData(Player player)
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			if (_beltInventory == null || (Object)(object)player == (Object)null)
			{
				return;
			}
			try
			{
				if (player.m_customData.TryGetValue("MegingjordOverhaul_BeltInventory", out var value) && !string.IsNullOrEmpty(value))
				{
					ZPackage val = new ZPackage(value);
					_beltInventory.Load(val);
					Plugin.Log.LogInfo((object)"Belt inventory data loaded.");
				}
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Failed to load belt inventory: {arg}");
			}
		}

		public static void OnPlayerDeath(Player player)
		{
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			if (_beltInventory == null)
			{
				return;
			}
			Inventory inventory = ((Humanoid)player).GetInventory();
			if (InventoryItemsField?.GetValue(inventory) is List<ItemData> list)
			{
				int height = inventory.GetHeight();
				foreach (ItemData allItem in _beltInventory.GetAllItems())
				{
					allItem.m_gridPos = new Vector2i(allItem.m_gridPos.x, height);
					list.Add(allItem);
				}
			}
			_beltInventory.RemoveAll();
			SaveBeltData(player);
		}

		public static void SwapHotbar(Player player)
		{
			if (_beltInventory == null || (Object)(object)player == (Object)null)
			{
				return;
			}
			Inventory inventory = ((Humanoid)player).GetInventory();
			List<ItemData> list = InventoryItemsField?.GetValue(inventory) as List<ItemData>;
			List<ItemData> list2 = InventoryItemsField?.GetValue(_beltInventory) as List<ItemData>;
			if (list == null || list2 == null)
			{
				return;
			}
			List<ItemData> list3 = new List<ItemData>();
			List<ItemData> list4 = new List<ItemData>();
			for (int i = 0; i < 8; i++)
			{
				ItemData itemAt = inventory.GetItemAt(i, 0);
				ItemData itemAt2 = _beltInventory.GetItemAt(i, 0);
				if (itemAt != null && ((Humanoid)player).IsItemEquiped(itemAt))
				{
					((Humanoid)player).UnequipItem(itemAt, false);
				}
				if (itemAt != null)
				{
					list.Remove(itemAt);
					list3.Add(itemAt);
				}
				if (itemAt2 != null)
				{
					list2.Remove(itemAt2);
					list4.Add(itemAt2);
				}
			}
			foreach (ItemData item in list3)
			{
				list2.Add(item);
			}
			foreach (ItemData item2 in list4)
			{
				list.Add(item2);
			}
			InventoryChangedMethod?.Invoke(inventory, null);
			InventoryChangedMethod?.Invoke(_beltInventory, null);
		}

		public static void Reset()
		{
			_beltInventory = null;
			_beltEquipped = false;
			BeltInventoryUI.Destroy();
		}
	}
	public static class BeltInventoryUI
	{
		private static GameObject _beltGridObject;

		private static InventoryGrid _beltGrid;

		private static bool _isVisible;

		private static bool _created;

		private static readonly FieldInfo DragGoField = AccessTools.Field(typeof(InventoryGui), "m_dragGo");

		private static readonly FieldInfo DragItemField = AccessTools.Field(typeof(InventoryGui), "m_dragItem");

		private static readonly FieldInfo DragInventoryField = AccessTools.Field(typeof(InventoryGui), "m_dragInventory");

		private static readonly FieldInfo DragAmountField = AccessTools.Field(typeof(InventoryGui), "m_dragAmount");

		private static readonly FieldInfo CurrentContainerField = AccessTools.Field(typeof(InventoryGui), "m_currentContainer");

		private static int _dragStartFrame = -1;

		private static readonly MethodInfo SetupDragItemMethod = AccessTools.Method(typeof(InventoryGui), "SetupDragItem", new Type[3]
		{
			typeof(ItemData),
			typeof(Inventory),
			typeof(int)
		}, (Type[])null);

		public static InventoryGrid GetBeltGrid()
		{
			return _beltGrid;
		}

		public static bool IsGridVisible()
		{
			return _isVisible;
		}

		private static GameObject GetDragGo(InventoryGui gui)
		{
			object? obj = DragGoField?.GetValue(gui);
			return (GameObject)((obj is GameObject) ? obj : null);
		}

		private static ItemData GetDragItem(InventoryGui gui)
		{
			object? obj = DragItemField?.GetValue(gui);
			return (ItemData)((obj is ItemData) ? obj : null);
		}

		private static Inventory GetDragInventory(InventoryGui gui)
		{
			object? obj = DragInventoryField?.GetValue(gui);
			return (Inventory)((obj is Inventory) ? obj : null);
		}

		private static int GetDragAmount(InventoryGui gui)
		{
			return (int)(DragAmountField?.GetValue(gui) ?? ((object)0));
		}

		private static void SetDragAmount(InventoryGui gui, int amount)
		{
			DragAmountField?.SetValue(gui, amount);
		}

		private static Container GetCurrentContainer(InventoryGui gui)
		{
			object? obj = CurrentContainerField?.GetValue(gui);
			return (Container)((obj is Container) ? obj : null);
		}

		public static void CreateBeltGrid()
		{
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Expected O, but got Unknown
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			//IL_019d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b3: 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_01d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_020f: Unknown result type (might be due to invalid IL or missing references)
			//IL_021a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0225: Unknown result type (might be due to invalid IL or missing references)
			//IL_022f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0304: Unknown result type (might be due to invalid IL or missing references)
			//IL_0323: Unknown result type (might be due to invalid IL or missing references)
			//IL_032f: Unknown result type (might be due to invalid IL or missing references)
			//IL_033b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0347: Unknown result type (might be due to invalid IL or missing references)
			//IL_0267: Unknown result type (might be due to invalid IL or missing references)
			//IL_0272: Unknown result type (might be due to invalid IL or missing references)
			//IL_027d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0287: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_0404: Unknown result type (might be due to invalid IL or missing references)
			//IL_0409: Unknown result type (might be due to invalid IL or missing references)
			//IL_0419: Unknown result type (might be due to invalid IL or missing references)
			//IL_041e: Unknown result type (might be due to invalid IL or missing references)
			//IL_02d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e2: Unknown result type (might be due to invalid IL or missing references)
			if (_created)
			{
				return;
			}
			InventoryGui instance = InventoryGui.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			InventoryGrid playerGrid = instance.m_playerGrid;
			if ((Object)(object)playerGrid == (Object)null || (Object)(object)playerGrid.m_elementPrefab == (Object)null)
			{
				Plugin.Log.LogWarning((object)"CreateBeltGrid: playerGrid or elementPrefab is null");
				return;
			}
			RectTransform player = instance.m_player;
			Transform val = ((Transform)player).Find("Bkg");
			Transform val2 = ((Transform)player).Find("Darken");
			Plugin.Log.LogInfo((object)("CreateBeltGrid: prefab=" + ((Object)playerGrid.m_elementPrefab).name + ", " + $"space={playerGrid.m_elementSpace}, " + "Bkg=" + (((Object)(object)val != (Object)null) ? "found" : "MISSING") + ", Darken=" + (((Object)(object)val2 != (Object)null) ? "found" : "MISSING")));
			float elementSpace = playerGrid.m_elementSpace;
			float num = 18f;
			float num2 = 8f * elementSpace + num * 2f;
			float num3 = 1f * elementSpace + num * 2f;
			_beltGridObject = new GameObject("BeltInventoryPanel", new Type[1] { typeof(RectTransform) });
			_beltGridObject.layer = ((Component)player).gameObject.layer;
			_beltGridObject.transform.SetParent((Transform)(object)player, false);
			RectTransform component = _beltGridObject.GetComponent<RectTransform>();
			component.anchorMin = new Vector2(0.5f, 0f);
			component.anchorMax = new Vector2(0.5f, 0f);
			component.pivot = new Vector2(0.5f, 1f);
			component.sizeDelta = new Vector2(num2, num3);
			component.anchoredPosition = new Vector2(0f, -10f);
			if ((Object)(object)val2 != (Object)null)
			{
				RectTransform obj = Object.Instantiate<RectTransform>(((Component)val2).GetComponent<RectTransform>(), _beltGridObject.transform);
				((Object)obj).name = "Darken";
				obj.anchorMin = Vector2.zero;
				obj.anchorMax = Vector2.one;
				obj.offsetMin = Vector2.zero;
				obj.offsetMax = Vector2.zero;
			}
			if ((Object)(object)val != (Object)null)
			{
				Transform obj2 = Object.Instantiate<Transform>(val, _beltGridObject.transform);
				((Object)obj2).name = "Bkg";
				RectTransform component2 = ((Component)obj2).GetComponent<RectTransform>();
				component2.anchorMin = Vector2.zero;
				component2.anchorMax = Vector2.one;
				component2.offsetMin = Vector2.zero;
				component2.offsetMax = Vector2.zero;
				Image component3 = ((Component)val).GetComponent<Image>();
				Image component4 = ((Component)obj2).GetComponent<Image>();
				if ((Object)(object)component3 != (Object)null && (Object)(object)component4 != (Object)null)
				{
					component4.sprite = component3.sprite;
					component4.overrideSprite = component3.overrideSprite;
					((Graphic)component4).color = ((Graphic)component3).color;
					component4.type = component3.type;
					((Graphic)component4).material = ((Graphic)component3).material;
				}
			}
			GameObject val3 = new GameObject("Grid");
			val3.transform.SetParent(_beltGridObject.transform, false);
			RectTransform val4 = val3.AddComponent<RectTransform>();
			val4.anchorMin = Vector2.zero;
			val4.anchorMax = Vector2.one;
			val4.offsetMin = Vector2.zero;
			val4.offsetMax = Vector2.zero;
			_beltGrid = _beltGridObject.AddComponent<InventoryGrid>();
			_beltGrid.m_elementPrefab = playerGrid.m_elementPrefab;
			_beltGrid.m_elementSpace = elementSpace;
			_beltGrid.m_gridRoot = val4;
			_beltGrid.m_uiGroup = playerGrid.m_uiGroup;
			_beltGrid.m_onSelected = OnBeltItemSelected;
			_beltGrid.m_onRightClick = OnBeltItemRightClicked;
			ManualLogSource log = Plugin.Log;
			string text = $"Belt grid created. sizeDelta={component.sizeDelta}, ";
			Rect rect = component.rect;
			object arg = ((Rect)(ref rect)).width;
			rect = component.rect;
			log.LogInfo((object)(text + $"rect=({arg},{((Rect)(ref rect)).height})"));
			_beltGridObject.SetActive(false);
			_isVisible = false;
			_created = true;
		}

		public static void UpdateBeltGrid(Player player)
		{
			if ((Object)(object)_beltGrid == (Object)null || !_isVisible)
			{
				return;
			}
			Inventory beltInventory = BeltInventoryManager.GetBeltInventory();
			if (beltInventory == null)
			{
				return;
			}
			InventoryGui instance = InventoryGui.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			try
			{
				_beltGrid.UpdateInventory(beltInventory, player, GetDragItem(instance));
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"UpdateBeltGrid failed: {arg}");
			}
		}

		public static void Show()
		{
			if (!_created)
			{
				CreateBeltGrid();
			}
			BeltInventoryManager.EnsureBeltInventory();
			if ((Object)(object)_beltGridObject != (Object)null)
			{
				_beltGridObject.SetActive(true);
				_isVisible = true;
			}
		}

		public static void Hide()
		{
			_isVisible = false;
			if ((Object)(object)_beltGridObject != (Object)null)
			{
				_beltGridObject.SetActive(false);
			}
		}

		public static void Destroy()
		{
			if ((Object)(object)_beltGridObject != (Object)null)
			{
				Object.Destroy((Object)(object)_beltGridObject);
				_beltGridObject = null;
				_beltGrid = null;
			}
			_isVisible = false;
			_created = false;
		}

		private static void OnBeltItemSelected(InventoryGrid grid, ItemData item, Vector2i pos, Modifier mod)
		{
			//IL_0162: Unknown result type (might be due to invalid IL or missing references)
			//IL_0164: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Expected I4, but got Unknown
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_0215: Unknown result type (might be due to invalid IL or missing references)
			//IL_021a: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			InventoryGui instance = InventoryGui.instance;
			if ((Object)(object)instance == (Object)null)
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null || ((Character)localPlayer).IsTeleporting())
			{
				return;
			}
			Inventory beltInventory = BeltInventoryManager.GetBeltInventory();
			if (beltInventory == null)
			{
				return;
			}
			GameObject dragGo = GetDragGo(instance);
			ItemData dragItem = GetDragItem(instance);
			Inventory dragInventory = GetDragInventory(instance);
			int dragAmount = GetDragAmount(instance);
			if ((Object)(object)dragGo != (Object)null)
			{
				if (Time.frameCount == _dragStartFrame)
				{
					return;
				}
				instance.m_moveItemEffects.Create(((Component)instance).transform.position, Quaternion.identity, (Transform)null, 1f, -1);
				bool flag = ((Humanoid)localPlayer).IsItemEquiped(dragItem);
				bool flag2 = item != null && ((Humanoid)localPlayer).IsItemEquiped(item);
				if ((dragItem.m_shared.m_questItem || (item != null && item.m_shared.m_questItem)) && dragInventory != beltInventory)
				{
					return;
				}
				if (!dragInventory.ContainsItem(dragItem))
				{
					CallSetupDragItem(instance, null, null, 1);
					return;
				}
				((Humanoid)localPlayer).RemoveEquipAction(item);
				((Humanoid)localPlayer).RemoveEquipAction(dragItem);
				((Humanoid)localPlayer).UnequipItem(dragItem, false);
				((Humanoid)localPlayer).UnequipItem(item, false);
				bool num = grid.DropItem(dragInventory, dragItem, dragAmount, pos);
				if (dragItem.m_stack < dragAmount)
				{
					SetDragAmount(instance, dragItem.m_stack);
				}
				if (flag && ((Humanoid)localPlayer).GetInventory().ContainsItem(dragItem))
				{
					((Humanoid)localPlayer).EquipItem(dragItem, false);
				}
				if (flag2 && ((Humanoid)localPlayer).GetInventory().ContainsItem(item))
				{
					((Humanoid)localPlayer).EquipItem(item, false);
				}
				if (num)
				{
					CallSetupDragItem(instance, null, null, 1);
				}
			}
			else
			{
				if (item == null)
				{
					return;
				}
				switch (mod - 1)
				{
				case 1:
					if (!item.m_shared.m_questItem)
					{
						Container currentContainer = GetCurrentContainer(instance);
						if ((Object)(object)currentContainer != (Object)null)
						{
							((Humanoid)localPlayer).RemoveEquipAction(item);
							((Humanoid)localPlayer).UnequipItem(item, true);
							currentContainer.GetInventory().MoveItemToThis(beltInventory, item);
						}
						else
						{
							((Humanoid)localPlayer).RemoveEquipAction(item);
							((Humanoid)localPlayer).UnequipItem(item, true);
							((Humanoid)localPlayer).GetInventory().MoveItemToThis(beltInventory, item);
						}
						instance.m_moveItemEffects.Create(((Component)instance).transform.position, Quaternion.identity, (Transform)null, 1f, -1);
					}
					return;
				case 2:
					((Humanoid)localPlayer).DropItem(beltInventory, item, item.m_stack);
					instance.m_moveItemEffects.Create(((Component)instance).transform.position, Quaternion.identity, (Transform)null, 1f, -1);
					return;
				case 0:
					if (item.m_stack > 1)
					{
						CallShowSplitDialog(instance, item, beltInventory);
						return;
					}
					break;
				}
				if (SetupDragItemMethod != null)
				{
					_dragStartFrame = Time.frameCount;
					SetupDragItemMethod.Invoke(instance, new object[3] { item, beltInventory, item.m_stack });
				}
			}
		}

		private static void OnBeltItemRightClicked(InventoryGrid grid, ItemData item, Vector2i pos)
		{
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Invalid comparison between Unknown and I4
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null || ((Character)localPlayer).IsTeleporting() || item == null)
			{
				return;
			}
			Inventory beltInventory = BeltInventoryManager.GetBeltInventory();
			if (beltInventory == null)
			{
				return;
			}
			if ((int)item.m_shared.m_itemType == 2)
			{
				((Humanoid)localPlayer).UseItem(beltInventory, item, false);
			}
			else if (item.IsEquipable())
			{
				Inventory inventory = ((Humanoid)localPlayer).GetInventory();
				inventory.MoveItemToThis(beltInventory, item);
				if (inventory.ContainsItem(item))
				{
					((Humanoid)localPlayer).EquipItem(item, true);
				}
			}
			else
			{
				((Humanoid)localPlayer).UseItem(beltInventory, item, false);
			}
		}

		private static void CallSetupDragItem(InventoryGui gui, ItemData item, Inventory inventory, int amount)
		{
			object? obj = DragGoField?.GetValue(gui);
			GameObject val = (GameObject)((obj is GameObject) ? obj : null);
			if ((Object)(object)val != (Object)null)
			{
				Object.Destroy((Object)(object)val);
				DragGoField.SetValue(gui, null);
				DragItemField?.SetValue(gui, null);
				DragInventoryField?.SetValue(gui, null);
				DragAmountField?.SetValue(gui, 1);
			}
		}

		private static void CallShowSplitDialog(InventoryGui gui, ItemData item, Inventory inventory)
		{
			Traverse.Create((object)gui).Method("ShowSplitDialog", new object[2] { item, inventory }).GetValue();
		}
	}
	[BepInPlugin("com.shadow.megingjordoverhaul", "Megingjord Overhaul", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.shadow.megingjordoverhaul";

		public const string PluginName = "Megingjord Overhaul";

		public const string PluginVersion = "1.0.0";

		public static ManualLogSource Log;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			_harmony = new Harmony("com.shadow.megingjordoverhaul");
			_harmony.PatchAll(Assembly.GetExecutingAssembly());
			Log.LogInfo((object)"Megingjord Overhaul v1.0.0 loaded.");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
}
namespace MegingjordOverhaul.Patches
{
	[HarmonyPatch(typeof(Humanoid), "EquipItem")]
	internal static class Humanoid_EquipItem_Patch
	{
		internal static bool SwappingMegingjord;

		private static void Prefix(Humanoid __instance, ItemData item)
		{
			SwappingMegingjord = false;
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer) && BeltInventoryManager.IsMegingjord(item) && BeltInventoryManager.HasMegingjordEquipped(__instance))
			{
				SwappingMegingjord = true;
			}
		}

		private static void Postfix(Humanoid __instance, ItemData item, bool __result)
		{
			SwappingMegingjord = false;
			if (__result && !((Object)(object)__instance != (Object)(object)Player.m_localPlayer) && BeltInventoryManager.IsMegingjord(item))
			{
				BeltInventoryManager.OnMegingjordEquipped((Player)(object)((__instance is Player) ? __instance : null));
			}
		}
	}
	[HarmonyPatch(typeof(Humanoid), "UnequipItem")]
	internal static class Humanoid_UnequipItem_Patch
	{
		private static void Prefix(Humanoid __instance, ItemData item, ref ItemData __state)
		{
			__state = null;
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer) && item != null && BeltInventoryManager.IsMegingjord(item))
			{
				__state = item;
			}
		}

		private static void Postfix(Humanoid __instance, ItemData __state)
		{
			if (__state != null && !Humanoid_EquipItem_Patch.SwappingMegingjord)
			{
				BeltInventoryManager.OnMegingjordUnequipped((Player)(object)((__instance is Player) ? __instance : null));
			}
		}
	}
	[HarmonyPatch(typeof(Humanoid), "IsTeleportable")]
	internal static class Humanoid_IsTeleportable_Patch
	{
		private static void Postfix(Humanoid __instance, ref bool __result)
		{
			if (__result && !((Object)(object)__instance != (Object)(object)Player.m_localPlayer))
			{
				Inventory beltInventory = BeltInventoryManager.GetBeltInventory();
				if (beltInventory != null && !beltInventory.IsTeleportable())
				{
					__result = false;
				}
			}
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "Awake")]
	internal static class InventoryGui_Awake_Patch
	{
		private static void Postfix()
		{
			BeltInventoryUI.CreateBeltGrid();
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "UpdateInventory")]
	internal static class InventoryGui_UpdateInventory_Patch
	{
		private static void Postfix(Player player)
		{
			BeltInventoryUI.UpdateBeltGrid(player);
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "Show")]
	internal static class InventoryGui_Show_Patch
	{
		private static void Postfix()
		{
			if (BeltInventoryManager.IsBeltEquipped())
			{
				BeltInventoryUI.Show();
			}
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "Hide")]
	internal static class InventoryGui_Hide_Patch
	{
		private static void Postfix()
		{
			BeltInventoryUI.Hide();
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "UpdateContainer")]
	internal static class InventoryGui_UpdateContainer_Flag
	{
		internal static bool InUpdateContainer;

		private static void Prefix()
		{
			InUpdateContainer = true;
		}

		private static void Postfix()
		{
			InUpdateContainer = false;
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "SetupDragItem")]
	internal static class InventoryGui_SetupDragItem_ProtectBelt
	{
		private static readonly FieldInfo DragInventoryField = AccessTools.Field(typeof(InventoryGui), "m_dragInventory");

		private static bool Prefix(InventoryGui __instance, ItemData item)
		{
			if (!InventoryGui_UpdateContainer_Flag.InUpdateContainer)
			{
				return true;
			}
			if (item != null)
			{
				return true;
			}
			object? obj = DragInventoryField?.GetValue(__instance);
			Inventory val = (Inventory)((obj is Inventory) ? obj : null);
			if (val != null && val == BeltInventoryManager.GetBeltInventory())
			{
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(InventoryGui), "OnSelectedItem")]
	internal static class InventoryGui_OnSelectedItem_BeltCtrlClick
	{
		private static readonly FieldInfo DragGoField = AccessTools.Field(typeof(InventoryGui), "m_dragGo");

		private static readonly FieldInfo CurrentContainerField = AccessTools.Field(typeof(InventoryGui), "m_currentContainer");

		private static bool Prefix(InventoryGui __instance, InventoryGrid grid, ItemData item, Vector2i pos, Modifier mod)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Invalid comparison between Unknown and I4
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			if ((int)mod != 2)
			{
				return true;
			}
			if (item == null || item.m_shared.m_questItem)
			{
				return true;
			}
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null || ((Character)localPlayer).IsTeleporting())
			{
				return true;
			}
			Inventory beltInventory = BeltInventoryManager.GetBeltInventory();
			if (beltInventory == null || !BeltInventoryManager.IsBeltEquipped())
			{
				return true;
			}
			object? obj = DragGoField?.GetValue(__instance);
			if ((Object)((obj is GameObject) ? obj : null) != (Object)null)
			{
				return true;
			}
			if ((Object)(object)grid != (Object)(object)__instance.m_playerGrid)
			{
				return true;
			}
			object? obj2 = CurrentContainerField?.GetValue(__instance);
			if ((Object)((obj2 is Container) ? obj2 : null) != (Object)null)
			{
				return true;
			}
			((Humanoid)localPlayer).RemoveEquipAction(item);
			((Humanoid)localPlayer).UnequipItem(item, false);
			beltInventory.MoveItemToThis(((Humanoid)localPlayer).GetInventory(), item);
			__instance.m_moveItemEffects.Create(((Component)__instance).transform.position, Quaternion.identity, (Transform)null, 1f, -1);
			return false;
		}
	}
	[HarmonyPatch(typeof(Player), "Update")]
	internal static class Player_Update_HotbarSwap
	{
		private static void Postfix(Player __instance)
		{
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer))
			{
				bool flag = BeltInventoryManager.HasMegingjordEquipped((Humanoid)(object)__instance);
				bool flag2 = BeltInventoryManager.IsBeltEquipped();
				if (flag && !flag2)
				{
					BeltInventoryManager.OnMegingjordEquipped(__instance);
				}
				else if (!flag && flag2)
				{
					BeltInventoryManager.OnMegingjordUnequipped(__instance);
				}
				if (BeltInventoryManager.IsBeltEquipped() && !InventoryGui.IsVisible() && ZInput.GetKeyDown((KeyCode)308, true))
				{
					BeltInventoryManager.SwapHotbar(__instance);
				}
			}
		}
	}
	[HarmonyPatch(typeof(Player), "Save")]
	internal static class Player_Save_Patch
	{
		private static void Prefix(Player __instance)
		{
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer))
			{
				BeltInventoryManager.SaveBeltData(__instance);
			}
		}
	}
	[HarmonyPatch(typeof(Player), "Load")]
	internal static class Player_Load_Patch
	{
		private static void Postfix(Player __instance)
		{
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer) && BeltInventoryManager.HasMegingjordEquipped((Humanoid)(object)__instance))
			{
				BeltInventoryManager.OnMegingjordEquipped(__instance);
			}
		}
	}
	[HarmonyPatch(typeof(Player), "OnDeath")]
	internal static class Player_OnDeath_Patch
	{
		private static void Prefix(Player __instance)
		{
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer))
			{
				BeltInventoryManager.OnPlayerDeath(__instance);
			}
		}
	}
	[HarmonyPatch(typeof(Player), "OnDestroy")]
	internal static class Player_OnDestroy_Patch
	{
		private static void Prefix(Player __instance)
		{
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer))
			{
				BeltInventoryManager.SaveBeltData(__instance);
				BeltInventoryManager.Reset();
			}
		}
	}
}