Decompiled source of Inventory Sorter v1.0.0

plugins\SulfurInventorySorter.dll

Decompiled 6 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using PerfectRandom.Sulfur.Core;
using PerfectRandom.Sulfur.Core.Items;
using PerfectRandom.Sulfur.Core.UI;
using PerfectRandom.Sulfur.Core.UI.Inventory;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("SulfurInventorySorter")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SulfurInventorySorter")]
[assembly: AssemblyTitle("SulfurInventorySorter")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace SulfurInventorySorter
{
	[BepInPlugin("local.sulfur.inventorysorter", "SULFUR Inventory Sorter", "1.0.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "local.sulfur.inventorysorter";

		public const string PluginName = "SULFUR Inventory Sorter";

		public const string PluginVersion = "1.0.0";

		internal static ManualLogSource Log;

		private Button? _backpackButton;

		private Button? _otherButton;

		private InventoryUI? _boundInventoryUi;

		private float _nextRefresh;

		private static Sprite? _sortIcon;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Log.LogInfo((object)"SULFUR Inventory Sorter 1.0.0 loaded.");
		}

		private void Update()
		{
			UIManager instance = StaticInstance<UIManager>.Instance;
			InventoryUI val = ((instance != null) ? instance.InventoryUI : null);
			if (!((Object)(object)val == (Object)null))
			{
				if ((Object)(object)_boundInventoryUi != (Object)(object)val || (Object)(object)_backpackButton == (Object)null)
				{
					BindInventoryUi(val);
				}
				if (Time.unscaledTime >= _nextRefresh)
				{
					_nextRefresh = Time.unscaledTime + 0.25f;
					RefreshButtonState(val);
				}
			}
		}

		private void BindInventoryUi(InventoryUI inventoryUi)
		{
			_boundInventoryUi = inventoryUi;
			if ((Object)(object)_backpackButton == (Object)null && (Object)(object)inventoryUi.InventoryPanelLeft != (Object)null)
			{
				_backpackButton = CreateButton(inventoryUi.InventoryPanelLeft, delegate
				{
					SortGrid(inventoryUi.PlayerBackpackGrid);
				});
			}
			if ((Object)(object)_otherButton == (Object)null && (Object)(object)inventoryUi.InventoryPanelRight != (Object)null)
			{
				_otherButton = CreateButton(inventoryUi.InventoryPanelRight, delegate
				{
					SortGrid(GetSecondaryOpenGrid(inventoryUi));
				});
			}
		}

		private static Button CreateButton(RectTransform parent, Action onClick)
		{
			//IL_004c: 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_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: 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_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b9: 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_00d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0107: Expected O, but got Unknown
			//IL_0108: 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_0110: 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_0128: 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_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0151: Unknown result type (might be due to invalid IL or missing references)
			//IL_0166: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Unknown result type (might be due to invalid IL or missing references)
			//IL_018f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0199: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_01bd: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ed: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fc: 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_0217: Unknown result type (might be due to invalid IL or missing references)
			//IL_022c: Unknown result type (might be due to invalid IL or missing references)
			//IL_023b: Unknown result type (might be due to invalid IL or missing references)
			//IL_024a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0256: Unknown result type (might be due to invalid IL or missing references)
			//IL_026b: 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_0289: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_02dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f7: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("InventorySorterButton", new Type[4]
			{
				typeof(RectTransform),
				typeof(CanvasRenderer),
				typeof(Image),
				typeof(Button)
			});
			val.transform.SetParent((Transform)(object)parent, false);
			val.transform.SetAsLastSibling();
			RectTransform component = val.GetComponent<RectTransform>();
			component.anchorMin = new Vector2(1f, 1f);
			component.anchorMax = new Vector2(1f, 1f);
			component.pivot = new Vector2(0f, 1f);
			component.anchoredPosition = new Vector2(10f, 0f);
			component.sizeDelta = new Vector2(42f, 42f);
			Image component2 = val.GetComponent<Image>();
			((Graphic)component2).color = SortButtonHover.NormalBackground;
			Button component3 = val.GetComponent<Button>();
			((UnityEvent)component3.onClick).AddListener((UnityAction)delegate
			{
				onClick();
			});
			ColorBlock colors = ((Selectable)component3).colors;
			((ColorBlock)(ref colors)).normalColor = Color.white;
			((ColorBlock)(ref colors)).highlightedColor = Color.white;
			((ColorBlock)(ref colors)).pressedColor = Color.white;
			((ColorBlock)(ref colors)).disabledColor = Color.white;
			((ColorBlock)(ref colors)).colorMultiplier = 1f;
			((Selectable)component3).colors = colors;
			Image val2 = CreateStretchImage(val.transform, "Icon", new Vector2(7f, 7f), new Vector2(-7f, -7f));
			val2.sprite = GetSortIcon();
			((Graphic)val2).color = SortButtonHover.NormalYellow;
			Image val3 = CreateBorder(val.transform, "BorderTop", new Vector2(0f, 1f), new Vector2(1f, 1f), new Vector2(0f, 1f));
			Image val4 = CreateBorder(val.transform, "BorderRight", new Vector2(1f, 0f), new Vector2(1f, 1f), new Vector2(1f, 1f));
			Image val5 = CreateBorder(val.transform, "BorderBottom", new Vector2(0f, 0f), new Vector2(1f, 0f), new Vector2(0f, 0f));
			Image val6 = CreateBorder(val.transform, "BorderLeft", new Vector2(0f, 0f), new Vector2(0f, 1f), new Vector2(0f, 1f));
			((Graphic)val3).rectTransform.sizeDelta = new Vector2(0f, 2f);
			((Graphic)val4).rectTransform.sizeDelta = new Vector2(2f, 0f);
			((Graphic)val5).rectTransform.sizeDelta = new Vector2(0f, 2f);
			((Graphic)val6).rectTransform.sizeDelta = new Vector2(2f, 0f);
			SortButtonHover sortButtonHover = val.AddComponent<SortButtonHover>();
			sortButtonHover.Background = component2;
			sortButtonHover.Icon = val2;
			sortButtonHover.Border = (Image[])(object)new Image[4] { val3, val4, val5, val6 };
			return component3;
		}

		private static Image CreateStretchImage(Transform parent, string name, Vector2 offsetMin, Vector2 offsetMax)
		{
			//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_0047: 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_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject(name, new Type[3]
			{
				typeof(RectTransform),
				typeof(CanvasRenderer),
				typeof(Image)
			});
			val.transform.SetParent(parent, false);
			RectTransform component = val.GetComponent<RectTransform>();
			component.anchorMin = Vector2.zero;
			component.anchorMax = Vector2.one;
			component.offsetMin = offsetMin;
			component.offsetMax = offsetMax;
			return val.GetComponent<Image>();
		}

		private static Image CreateBorder(Transform parent, string name, Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot)
		{
			//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_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject(name, new Type[3]
			{
				typeof(RectTransform),
				typeof(CanvasRenderer),
				typeof(Image)
			});
			val.transform.SetParent(parent, false);
			RectTransform component = val.GetComponent<RectTransform>();
			component.anchorMin = anchorMin;
			component.anchorMax = anchorMax;
			component.pivot = pivot;
			component.anchoredPosition = Vector2.zero;
			Image component2 = val.GetComponent<Image>();
			((Graphic)component2).color = SortButtonHover.NormalYellow;
			return component2;
		}

		private static Sprite GetSortIcon()
		{
			//IL_0019: 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_0037: 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_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: 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)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Expected O, but got Unknown
			//IL_0079: 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_0085: 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_009e: Expected O, but got Unknown
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Expected O, but got Unknown
			if ((Object)(object)_sortIcon != (Object)null)
			{
				return _sortIcon;
			}
			Texture2D val = new Texture2D(96, 96, (TextureFormat)4, false);
			Color element = new Color(0f, 0f, 0f, 0f);
			Color normalYellow = SortButtonHover.NormalYellow;
			Color[] pixels = Enumerable.Repeat<Color>(element, 9216).ToArray();
			val.SetPixels(pixels);
			DrawArrow(val, normalYellow, new Vector2(32f, 22f), new Vector2(32f, 74f));
			DrawArrow(val, normalYellow, new Vector2(64f, 74f), new Vector2(64f, 22f));
			val.Apply(false, true);
			_sortIcon = Sprite.Create(val, new Rect(0f, 0f, 96f, 96f), new Vector2(0.5f, 0.5f), 96f);
			((Object)_sortIcon).name = "InventorySorterIcon";
			return _sortIcon;
		}

		private static void DrawArrow(Texture2D texture, Color color, Vector2 start, Vector2 end)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0004: 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_000d: 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_0015: 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_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: 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_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: 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_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: 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_0052: 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_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			Vector2 val = end - start;
			Vector2 normalized = ((Vector2)(ref val)).normalized;
			Vector2 val2 = default(Vector2);
			((Vector2)(ref val2))..ctor(0f - normalized.y, normalized.x);
			Vector2 val3 = end - normalized * 18f;
			DrawLine(texture, color, start, val3, 5);
			FillTriangle(texture, color, end, val3 + val2 * 11f, val3 - val2 * 11f);
		}

		private static void DrawLine(Texture2D texture, Color color, Vector2 start, Vector2 end, int thickness)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: 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)
			Vector2 val = end - start;
			int num = Mathf.CeilToInt(((Vector2)(ref val)).magnitude);
			for (int i = 0; i <= num; i++)
			{
				Vector2 val2 = Vector2.Lerp(start, end, (float)i / (float)num);
				FillCircle(texture, color, Mathf.RoundToInt(val2.x), Mathf.RoundToInt(val2.y), thickness);
			}
		}

		private static void FillCircle(Texture2D texture, Color color, int centerX, int centerY, int radius)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			for (int i = -radius; i <= radius; i++)
			{
				for (int j = -radius; j <= radius; j++)
				{
					if (j * j + i * i <= radius * radius)
					{
						SetPixel(texture, color, centerX + j, centerY + i);
					}
				}
			}
		}

		private static void FillTriangle(Texture2D texture, Color color, Vector2 a, Vector2 b, Vector2 c)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: 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_004c: 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_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: 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_00b5: Unknown result type (might be due to invalid IL or missing references)
			int num = Mathf.FloorToInt(Mathf.Min(a.x, Mathf.Min(b.x, c.x)));
			int num2 = Mathf.CeilToInt(Mathf.Max(a.x, Mathf.Max(b.x, c.x)));
			int num3 = Mathf.FloorToInt(Mathf.Min(a.y, Mathf.Min(b.y, c.y)));
			int num4 = Mathf.CeilToInt(Mathf.Max(a.y, Mathf.Max(b.y, c.y)));
			for (int i = num3; i <= num4; i++)
			{
				for (int j = num; j <= num2; j++)
				{
					if (PointInTriangle(new Vector2((float)j + 0.5f, (float)i + 0.5f), a, b, c))
					{
						SetPixel(texture, color, j, i);
					}
				}
			}
		}

		private static bool PointInTriangle(Vector2 p, Vector2 a, Vector2 b, Vector2 c)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_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)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: 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_0013: Unknown result type (might be due to invalid IL or missing references)
			float num = Sign(p, a, b);
			float num2 = Sign(p, b, c);
			float num3 = Sign(p, c, a);
			bool flag = num < 0f || num2 < 0f || num3 < 0f;
			bool flag2 = num > 0f || num2 > 0f || num3 > 0f;
			return !(flag && flag2);
		}

		private static float Sign(Vector2 p1, Vector2 p2, Vector2 p3)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: 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)
			//IL_0013: 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_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: 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)
			return (p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y);
		}

		private static void SetPixel(Texture2D texture, Color color, int x, int y)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			if (x >= 0 && x < ((Texture)texture).width && y >= 0 && y < ((Texture)texture).height)
			{
				texture.SetPixel(x, y, color);
			}
		}

		private void RefreshButtonState(InventoryUI inventoryUi)
		{
			bool isOpened = inventoryUi.IsOpened;
			SetButtonVisible("backpack", isOpened, inventoryUi.PlayerBackpackGrid, DockSide.Left);
			SetButtonVisible("secondary", isOpened, GetSecondaryOpenGrid(inventoryUi), DockSide.Right);
		}

		private void SetButtonVisible(string buttonKind, bool inventoryOpen, ItemGrid? grid, DockSide dockSide)
		{
			Button val = ((buttonKind == "backpack") ? _backpackButton : _otherButton);
			if (!((Object)(object)val == (Object)null))
			{
				bool flag = inventoryOpen && (Object)(object)grid != (Object)null;
				((Component)val).gameObject.SetActive(flag);
				if (flag)
				{
					DockButtonToGrid(val, grid, dockSide);
					((Selectable)val).interactable = (Object)(object)grid.draggingItem == (Object)null;
					((Component)val).GetComponent<SortButtonHover>()?.Refresh();
				}
			}
		}

		private static void DockButtonToGrid(Button button, ItemGrid grid, DockSide dockSide)
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: 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_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e6: 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_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			RectTransform component = ((Component)button).GetComponent<RectTransform>();
			RectTransform rectTransform = grid.RectTransform;
			if (!((Object)(object)rectTransform == (Object)null))
			{
				if ((Object)(object)((Transform)component).parent != (Object)(object)rectTransform)
				{
					((Transform)component).SetParent((Transform)(object)rectTransform, false);
					component.sizeDelta = new Vector2(42f, 42f);
				}
				if (dockSide == DockSide.Left)
				{
					component.anchorMin = new Vector2(0f, 1f);
					component.anchorMax = new Vector2(0f, 1f);
					component.pivot = new Vector2(1f, 1f);
					component.anchoredPosition = new Vector2(-10f, 0f);
				}
				else
				{
					component.anchorMin = new Vector2(1f, 1f);
					component.anchorMax = new Vector2(1f, 1f);
					component.pivot = new Vector2(0f, 1f);
					component.anchoredPosition = new Vector2(10f, 0f);
				}
				((Component)button).transform.SetAsLastSibling();
			}
		}

		private static ItemGrid? GetSecondaryOpenGrid(InventoryUI inventoryUi)
		{
			if (inventoryUi.InStashUI)
			{
				return inventoryUi.StashInventoryItemGrid;
			}
			if (!inventoryUi.InShopUI)
			{
				ServiceStation currentServiceStation = inventoryUi.CurrentServiceStation;
				if (currentServiceStation == null || !currentServiceStation.IsSellableServiceStation)
				{
					if ((Object)(object)inventoryUi.CurrentServiceStation != (Object)null)
					{
						return inventoryUi.ServiceStationGrid;
					}
					return null;
				}
			}
			return null;
		}

		private static void SortGrid(ItemGrid? grid)
		{
			//IL_0236: Unknown result type (might be due to invalid IL or missing references)
			//IL_023b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0291: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)grid == (Object)null)
			{
				Log.LogDebug((object)"Sort skipped: no grid.");
				return;
			}
			if ((Object)(object)grid.draggingItem != (Object)null)
			{
				Log.LogDebug((object)"Sort skipped: an item is being dragged.");
				return;
			}
			List<InventoryItem> list = (from item in grid.AllItemsInGridOnly().OfType<InventoryItem>()
				where (Object)(object)item != (Object)null
				select item).ToList();
			if (list.Count <= 1)
			{
				return;
			}
			Dictionary<InventoryItem, Vector2Int> originalPositions = list.ToDictionary((InventoryItem item) => item, (InventoryItem item) => item.gridPosition);
			List<InventoryItem> list2 = list.OrderBy(GetSortGroup).ThenBy((InventoryItem item) => (int)item.itemDefinition.slotType).ThenByDescending(delegate(InventoryItem item)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				Vector2Int inventorySize = item.InventorySize;
				return ((Vector2Int)(ref inventorySize)).y;
			})
				.ThenByDescending(delegate(InventoryItem item)
				{
					//IL_0001: Unknown result type (might be due to invalid IL or missing references)
					//IL_0006: Unknown result type (might be due to invalid IL or missing references)
					Vector2Int inventorySize = item.InventorySize;
					return ((Vector2Int)(ref inventorySize)).x;
				})
				.ThenBy<InventoryItem, string>((InventoryItem item) => item.itemDefinition.displayName, StringComparer.OrdinalIgnoreCase)
				.ThenBy<InventoryItem, string>((InventoryItem item) => ((object)Unsafe.As<ItemId, ItemId>(ref item.itemDefinition.id)/*cast due to .constrained prefix*/).ToString(), StringComparer.Ordinal)
				.ToList();
			try
			{
				foreach (InventoryItem item in list)
				{
					item.UnregisterAtItemGrid();
				}
				List<InventoryItem> list3 = new List<InventoryItem>();
				Dictionary<InventoryItem, Vector2Int> dictionary = TryCreateBandedLayout(grid, list2);
				if (dictionary == null)
				{
					dictionary = TryCreateCompactLayout(grid, list2);
					if (dictionary != null)
					{
						Log.LogWarning((object)"Grouped layout did not fit; used compact sorted layout.");
					}
				}
				if (dictionary == null)
				{
					RollBack(grid, list3, list, originalPositions);
					Log.LogWarning((object)"Sort failed: could not create any valid layout, restored original positions.");
					return;
				}
				foreach (InventoryItem item2 in list2)
				{
					Vector2Int val = dictionary[item2];
					item2.SetContainingGrid(grid);
					item2.SetToNewGridPosition(((Vector2Int)(ref val)).x, ((Vector2Int)(ref val)).y);
					list3.Add(item2);
				}
				Log.LogInfo((object)$"Sorted {list2.Count} items in {grid.gridType}.");
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"Sort failed: {arg}");
				TryRestore(list, originalPositions);
			}
		}

		private static Dictionary<InventoryItem, Vector2Int>? TryCreateBandedLayout(ItemGrid grid, List<InventoryItem> sortedItems)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: 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_009e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			Dictionary<InventoryItem, Vector2Int> dictionary = new Dictionary<InventoryItem, Vector2Int>();
			List<InventoryItem> list = new List<InventoryItem>();
			Vector2Int cursor = Vector2Int.zero;
			int num = 0;
			int num2 = -1;
			foreach (InventoryItem sortedItem in sortedItems)
			{
				int sortGroup = GetSortGroup(sortedItem);
				if (num2 >= 0 && sortGroup != num2 && ((Vector2Int)(ref cursor)).x != 0)
				{
					((Vector2Int)(ref cursor)).x = 0;
					((Vector2Int)(ref cursor)).y = ((Vector2Int)(ref cursor)).y + Math.Max(num, 1);
					num = 0;
				}
				Vector2Int nextCursor;
				int nextRowHeight;
				Vector2Int? val = FindNextBandedPosition(grid, sortedItem, cursor, dictionary, out nextCursor, out nextRowHeight, num);
				if (!val.HasValue)
				{
					list.Add(sortedItem);
					continue;
				}
				dictionary[sortedItem] = val.Value;
				cursor = nextCursor;
				num = nextRowHeight;
				num2 = sortGroup;
			}
			foreach (InventoryItem item in list)
			{
				Vector2Int? val2 = FindAnyPosition(grid, item, dictionary);
				if (!val2.HasValue)
				{
					return null;
				}
				dictionary[item] = val2.Value;
			}
			return dictionary;
		}

		private static Dictionary<InventoryItem, Vector2Int>? TryCreateCompactLayout(ItemGrid grid, List<InventoryItem> sortedItems)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			Dictionary<InventoryItem, Vector2Int> dictionary = new Dictionary<InventoryItem, Vector2Int>();
			foreach (InventoryItem sortedItem in sortedItems)
			{
				Vector2Int? val = FindAnyPosition(grid, sortedItem, dictionary);
				if (!val.HasValue)
				{
					return null;
				}
				dictionary[sortedItem] = val.Value;
			}
			return dictionary;
		}

		private static Vector2Int? FindAnyPosition(ItemGrid grid, InventoryItem item, Dictionary<InventoryItem, Vector2Int> layout)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//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)
			//IL_003d: 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_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			Vector2Int inventorySize = item.InventorySize;
			int num = 0;
			Vector2Int val = default(Vector2Int);
			while (true)
			{
				int num2 = num + ((Vector2Int)(ref inventorySize)).y;
				Vector2Int gridSize = grid.gridSize;
				if (num2 > ((Vector2Int)(ref gridSize)).y)
				{
					break;
				}
				int num3 = 0;
				while (true)
				{
					int num4 = num3 + ((Vector2Int)(ref inventorySize)).x;
					gridSize = grid.gridSize;
					if (num4 > ((Vector2Int)(ref gridSize)).x)
					{
						break;
					}
					((Vector2Int)(ref val))..ctor(num3, num);
					if (CanPlaceInLayout(val, inventorySize, grid.gridSize, layout))
					{
						return val;
					}
					num3++;
				}
				num++;
			}
			return null;
		}

		private static Vector2Int? FindNextBandedPosition(ItemGrid grid, InventoryItem item, Vector2Int cursor, Dictionary<InventoryItem, Vector2Int> layout, out Vector2Int nextCursor, out int nextRowHeight, int rowHeight)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: 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_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: 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)
			Vector2Int inventorySize = item.InventorySize;
			int num = ((Vector2Int)(ref cursor)).x;
			int num2 = ((Vector2Int)(ref cursor)).y;
			Vector2Int val = default(Vector2Int);
			while (true)
			{
				int num3 = num2 + ((Vector2Int)(ref inventorySize)).y;
				Vector2Int gridSize = grid.gridSize;
				if (num3 > ((Vector2Int)(ref gridSize)).y)
				{
					break;
				}
				int num4 = num + ((Vector2Int)(ref inventorySize)).x;
				gridSize = grid.gridSize;
				if (num4 > ((Vector2Int)(ref gridSize)).x)
				{
					num = 0;
					num2 += Math.Max(rowHeight, 1);
					rowHeight = 0;
					continue;
				}
				((Vector2Int)(ref val))..ctor(num, num2);
				if (CanPlaceInLayout(val, inventorySize, grid.gridSize, layout))
				{
					nextCursor = new Vector2Int(((Vector2Int)(ref val)).x + ((Vector2Int)(ref inventorySize)).x, ((Vector2Int)(ref val)).y);
					nextRowHeight = Math.Max(rowHeight, ((Vector2Int)(ref inventorySize)).y);
					return val;
				}
				num++;
			}
			nextCursor = cursor;
			nextRowHeight = rowHeight;
			return null;
		}

		private static bool CanPlaceInLayout(Vector2Int position, Vector2Int size, Vector2Int gridSize, Dictionary<InventoryItem, Vector2Int> layout)
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: 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_0070: 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)
			if (((Vector2Int)(ref position)).x < 0 || ((Vector2Int)(ref position)).y < 0 || ((Vector2Int)(ref position)).x + ((Vector2Int)(ref size)).x > ((Vector2Int)(ref gridSize)).x || ((Vector2Int)(ref position)).y + ((Vector2Int)(ref size)).y > ((Vector2Int)(ref gridSize)).y)
			{
				return false;
			}
			RectInt val = default(RectInt);
			((RectInt)(ref val))..ctor(position, size);
			RectInt val2 = default(RectInt);
			foreach (KeyValuePair<InventoryItem, Vector2Int> item in layout)
			{
				((RectInt)(ref val2))..ctor(item.Value, item.Key.InventorySize);
				if (((RectInt)(ref val)).Overlaps(val2))
				{
					return false;
				}
			}
			return true;
		}

		private static int GetSortGroup(InventoryItem item)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Invalid comparison between Unknown and I4
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Invalid comparison between Unknown and I4
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Invalid comparison between Unknown and I4
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Invalid comparison between Unknown and I4
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Invalid comparison between Unknown and I4
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Invalid comparison between Unknown and I4
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Invalid comparison between Unknown and I4
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Invalid comparison between Unknown and I4
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Invalid comparison between Unknown and I4
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Invalid comparison between Unknown and I4
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Invalid comparison between Unknown and I4
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Invalid comparison between Unknown and I4
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Invalid comparison between Unknown and I4
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Expected I4, but got Unknown
			ItemDefinition itemDefinition = item.itemDefinition;
			if ((int)itemDefinition.slotType == 7 || (int)itemDefinition.slotType == 8 || (int)itemDefinition.ItemType == 2)
			{
				return 0;
			}
			if ((int)itemDefinition.ItemType == 1 || (int)itemDefinition.slotType != 0)
			{
				return 10;
			}
			if ((int)itemDefinition.ItemType == 4)
			{
				return 20;
			}
			if ((int)itemDefinition.ItemType == 5)
			{
				return 30;
			}
			if ((int)itemDefinition.ItemType == 6 || (int)itemDefinition.ItemType == 9)
			{
				return 40;
			}
			if ((int)itemDefinition.ItemType == 7)
			{
				return 50;
			}
			if ((int)itemDefinition.ItemType == 3)
			{
				return 60;
			}
			if ((int)itemDefinition.ItemType == 8)
			{
				return 70;
			}
			if ((int)itemDefinition.ItemType == 10)
			{
				return 80;
			}
			if ((int)itemDefinition.ItemType == 11)
			{
				return 90;
			}
			return 100 + itemDefinition.ItemType;
		}

		private static void RollBack(ItemGrid grid, List<InventoryItem> placedItems, List<InventoryItem> allItems, Dictionary<InventoryItem, Vector2Int> originalPositions)
		{
			foreach (InventoryItem placedItem in placedItems)
			{
				placedItem.UnregisterAtItemGrid();
			}
			TryRestore(allItems, originalPositions);
		}

		private static void TryRestore(List<InventoryItem> items, Dictionary<InventoryItem, Vector2Int> originalPositions)
		{
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			foreach (InventoryItem item in items.OrderBy(delegate(InventoryItem item)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				//IL_000c: Unknown result type (might be due to invalid IL or missing references)
				Vector2Int val2 = originalPositions[item];
				return ((Vector2Int)(ref val2)).y;
			}).ThenBy(delegate(InventoryItem item)
			{
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				//IL_000c: Unknown result type (might be due to invalid IL or missing references)
				Vector2Int val2 = originalPositions[item];
				return ((Vector2Int)(ref val2)).x;
			}))
			{
				Vector2Int val = originalPositions[item];
				item.SetToNewGridPosition(((Vector2Int)(ref val)).x, ((Vector2Int)(ref val)).y);
			}
		}
	}
	public enum DockSide
	{
		Left,
		Right
	}
	public sealed class SortButtonHover : MonoBehaviour, IPointerEnterHandler, IEventSystemHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
	{
		public static readonly Color NormalYellow = new Color(1f, 0.66f, 0.16f, 1f);

		public static readonly Color HoverYellow = new Color(1f, 0.82f, 0.32f, 1f);

		public static readonly Color NormalBackground = new Color(0.02f, 0.02f, 0.02f, 0.42f);

		public static readonly Color HoverBackground = new Color(0.06f, 0.04f, 0.01f, 0.72f);

		public static readonly Color DisabledYellow = new Color(1f, 0.66f, 0.16f, 0.35f);

		private RectTransform? _rectTransform;

		private bool _hovered;

		public Image? Background { get; set; }

		public Image? Icon { get; set; }

		public Image[] Border { get; set; } = Array.Empty<Image>();

		private void Awake()
		{
			_rectTransform = ((Component)this).GetComponent<RectTransform>();
			Refresh();
		}

		private void OnEnable()
		{
			_hovered = false;
			Refresh();
		}

		public void OnPointerEnter(PointerEventData eventData)
		{
			_hovered = true;
			Refresh();
		}

		public void OnPointerExit(PointerEventData eventData)
		{
			_hovered = false;
			Refresh();
		}

		public void OnPointerDown(PointerEventData eventData)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)_rectTransform != (Object)null)
			{
				((Transform)_rectTransform).localScale = new Vector3(0.96f, 0.96f, 1f);
			}
		}

		public void OnPointerUp(PointerEventData eventData)
		{
			Refresh();
		}

		public void Refresh()
		{
			//IL_001d: 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_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: 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_0058: 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_00a1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			Button component = ((Component)this).GetComponent<Button>();
			bool flag = (Object)(object)component == (Object)null || ((Selectable)component).interactable;
			Color color = ((!flag) ? DisabledYellow : (_hovered ? HoverYellow : NormalYellow));
			if ((Object)(object)Background != (Object)null)
			{
				((Graphic)Background).color = ((flag && _hovered) ? HoverBackground : NormalBackground);
			}
			if ((Object)(object)Icon != (Object)null)
			{
				((Graphic)Icon).color = color;
			}
			Image[] border = Border;
			foreach (Image val in border)
			{
				if ((Object)(object)val != (Object)null)
				{
					((Graphic)val).color = color;
				}
			}
			if ((Object)(object)_rectTransform != (Object)null)
			{
				((Transform)_rectTransform).localScale = (Vector3)((flag && _hovered) ? new Vector3(1.08f, 1.08f, 1f) : Vector3.one);
			}
		}
	}
}