Decompiled source of SimpleItemDrawer v1.1.0

SimpleItemDrawer.dll

Decompiled 3 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("SimpleItemDrawer")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("SimpleItemDrawer")]
[assembly: AssemblyTitle("SimpleItemDrawer")]
[assembly: AssemblyVersion("1.1.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace SimpleItemDrawer
{
	internal static class DrawerPrefab
	{
		internal const string DisplayName = "ItemDrawerDisplay";

		internal const string IconName = "Icon";

		internal const string HaloName = "Halo";

		internal const string CountName = "Count";

		internal const string FrameGroupName = "Frame";

		private const float FrameWidth = 0.1f;

		private const float DividerHeight = 0.08f;

		private const float RecessDepth = 0.12f;

		private const float PanelFront = 0.38f;

		private const float BodyGap = 0.05f;

		private const float RimInset = 0.04f;

		private const float Inner = 0.8f;

		private const string PreloadCharacters = "0123456789,";

		private const float CountGlowBoost = 1.1f;

		private static Mesh cube;

		private static Mesh marbleBlock;

		private static Vector3 marbleCenter;

		private static Material marble;

		private static Material darkMarble;

		private static Material iron;

		private static Sprite halo;

		private static Sprite edgeGlowSprite;

		private static TextMeshProUGUI signText;

		private const float IconEdgeGlow = 0.8f;

		private static TMP_FontAsset countFont;

		private static Material countMaterial;

		private const float CountFontSize = 30f;

		private const float CountSizeFloor = 0.7f;

		internal static string SlotName(int slot)
		{
			return "Slot" + slot;
		}

		internal static void Build(GameObject prefab, int slots)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			//IL_0038: 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_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: 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_00da: 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_00f0: 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_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: 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)
			//IL_0159: Unknown result type (might be due to invalid IL or missing references)
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0190: Unknown result type (might be due to invalid IL or missing references)
			//IL_0195: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_020a: 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_0216: Unknown result type (might be due to invalid IL or missing references)
			//IL_023d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0247: Unknown result type (might be due to invalid IL or missing references)
			LoadParts();
			StripChestVisuals(prefab, out var visualLayer, out var colliderLayer);
			GameObject val = new GameObject("New")
			{
				layer = visualLayer
			};
			val.transform.SetParent(prefab.transform, false);
			GameObject val2 = new GameObject("Frame")
			{
				layer = visualLayer
			};
			val2.transform.SetParent(val.transform, false);
			Transform transform = val2.transform;
			float num = 0.32999998f;
			float num2 = num + 0.5f;
			float num3 = 0.46f - num;
			float num4 = 0.46f - num3 / 2f;
			Part(transform, marbleBlock, marble, new Vector3(0f, 0.5f, num - num2 / 2f), new Vector3(1f, 1f, num2), marbleCenter);
			Part(transform, marbleBlock, marble, new Vector3(0f, 0.95f, num4), new Vector3(1f, 0.1f, num3), marbleCenter);
			Part(transform, marbleBlock, marble, new Vector3(0f, 0.05f, num4), new Vector3(1f, 0.1f, num3), marbleCenter);
			Part(transform, marbleBlock, marble, new Vector3(-0.45f, 0.5f, num4), new Vector3(0.1f, 0.8f, num3), marbleCenter);
			Part(transform, marbleBlock, marble, new Vector3(0.45f, 0.5f, num4), new Vector3(0.1f, 0.8f, num3), marbleCenter);
			AddIronTrim(transform);
			if (slots == 2)
			{
				AddDivider(transform, num);
			}
			float num5 = 0.38f - num;
			Part(val.transform, cube, darkMarble, new Vector3(0f, 0.5f, num + num5 / 2f), new Vector3(0.8f, 0.8f, num5));
			GameObject val3 = new GameObject("collider")
			{
				layer = colliderLayer
			};
			val3.transform.SetParent(prefab.transform, false);
			BoxCollider obj = val3.AddComponent<BoxCollider>();
			obj.center = new Vector3(0f, 0.5f, 0f);
			obj.size = Vector3.one;
			AddSnapPoints(prefab.transform);
			AddDisplay(prefab.transform, slots);
			WearNTear component = prefab.GetComponent<WearNTear>();
			if ((Object)(object)component != (Object)null)
			{
				component.m_new = val;
				component.m_worn = val;
				component.m_broken = val;
				component.m_wet = null;
				component.m_fragmentRoots = null;
				component.m_supports = true;
			}
		}

		internal static Sprite RenderIcon(GameObject prefab, int slots)
		{
			//IL_003d: 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_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_005d: Expected O, but got Unknown
			//IL_0072: 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_00de: 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_00ff: 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)
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_013f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_0169: Unknown result type (might be due to invalid IL or missing references)
			//IL_0170: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_019f: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cb: Unknown result type (might be due to invalid IL or missing references)
			Transform val = prefab.transform.Find("New");
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
			GameObject val2 = Object.Instantiate<GameObject>(((Component)val).gameObject);
			val2.transform.position = new Vector3(0f, -10000f, 0f);
			Material val3 = new Material(darkMarble)
			{
				color = Color.black
			};
			val3.SetColor("_EmissionColor", new Color(0.72f, 0.42f, 1f) * 0.8f);
			float num = 0.38750002f;
			float num2 = 0.386f;
			float height;
			float[] array = CompartmentCenters(slots, out height);
			foreach (float num3 in array)
			{
				float num4 = 0.5f + num3;
				float num5 = height / 2f - 0.0125f;
				Part(val2.transform, cube, val3, new Vector3(0f, num4 + num5, num2), new Vector3(0.8f, 0.025f, 0.01f));
				Part(val2.transform, cube, val3, new Vector3(0f, num4 - num5, num2), new Vector3(0.8f, 0.025f, 0.01f));
				Part(val2.transform, cube, val3, new Vector3(0f - num, num4, num2), new Vector3(0.025f, height, 0.01f));
				Part(val2.transform, cube, val3, new Vector3(num, num4, num2), new Vector3(0.025f, height, 0.01f));
			}
			try
			{
				return RenderManager.Instance.Render(val2, RenderManager.IsometricRotation);
			}
			finally
			{
				Object.DestroyImmediate((Object)(object)val2);
				Object.Destroy((Object)(object)val3);
			}
		}

		internal static void PrepareCountText(TMP_Text text)
		{
			//IL_003f: 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_0054: Expected O, but got Unknown
			if ((Object)(object)countMaterial == (Object)null)
			{
				TMP_Text val = InventoryAmountText();
				if ((Object)(object)val == (Object)null)
				{
					return;
				}
				countFont = val.font;
				countFont.TryAddCharacters("0123456789,", false);
				countMaterial = new Material(val.fontSharedMaterial)
				{
					name = "ItemDrawer_count"
				};
				ApplyCountColor();
			}
			if ((Object)(object)text.font != (Object)(object)countFont)
			{
				text.font = countFont;
				text.fontSharedMaterial = countMaterial;
			}
			SyncCountAtlas();
		}

		internal static void ApplyCountColor()
		{
			//IL_000e: 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_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_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)
			if (!((Object)(object)countMaterial == (Object)null))
			{
				Color countTextColor = Plugin.CountTextColor;
				countMaterial.SetColor("_FaceColor", new Color(countTextColor.r * 1.1f, countTextColor.g * 1.1f, countTextColor.b * 1.1f, countTextColor.a));
			}
		}

		internal static void SyncCountAtlas()
		{
			if (!((Object)(object)countMaterial == (Object)null))
			{
				Texture atlasTexture = (Texture)(object)countFont.atlasTexture;
				if ((Object)(object)countMaterial.mainTexture != (Object)(object)atlasTexture)
				{
					countMaterial.mainTexture = atlasTexture;
				}
			}
		}

		internal static void FitCounts(TMP_Text[] texts)
		{
			float[] array = new float[texts.Length];
			float num = float.MaxValue;
			for (int i = 0; i < texts.Length; i++)
			{
				TMP_Text val = texts[i];
				if (!((Object)(object)val == (Object)null) && !string.IsNullOrEmpty(val.text))
				{
					val.fontSize = 30f;
					val.enableAutoSizing = true;
					val.ForceMeshUpdate(false, false);
					array[i] = val.fontSize;
					num = Mathf.Min(num, array[i]);
				}
			}
			for (int j = 0; j < texts.Length; j++)
			{
				if (!(array[j] <= 0f))
				{
					texts[j].enableAutoSizing = false;
					texts[j].fontSize = ((num >= array[j] * 0.7f) ? num : array[j]);
				}
			}
		}

		private static TMP_Text InventoryAmountText()
		{
			InventoryGui instance = InventoryGui.instance;
			if ((Object)(object)instance == (Object)null || (Object)(object)instance.m_playerGrid == (Object)null || (Object)(object)instance.m_playerGrid.m_elementPrefab == (Object)null)
			{
				return null;
			}
			Transform val = instance.m_playerGrid.m_elementPrefab.transform.Find("amount");
			if (!((Object)(object)val != (Object)null))
			{
				return null;
			}
			return ((Component)val).GetComponent<TMP_Text>();
		}

		private static void LoadParts()
		{
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0076: 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)
			if (!((Object)(object)cube != (Object)null))
			{
				cube = ((Component)Find("wood_beam_1", "New")).GetComponent<MeshFilter>().sharedMesh;
				iron = ((Renderer)((Component)Find("iron_wall_1x1", "New/default")).GetComponent<MeshRenderer>()).sharedMaterial;
				Transform obj = Find("blackmarble_1x1", "new/stone_low");
				marbleBlock = ((Component)obj).GetComponent<MeshFilter>().sharedMesh;
				Bounds bounds = marbleBlock.bounds;
				marbleCenter = ((Bounds)(ref bounds)).center;
				Material sharedMaterial = ((Renderer)((Component)obj).GetComponent<MeshRenderer>()).sharedMaterial;
				marble = Tinted(sharedMaterial, "marble", 1f, 2f);
				darkMarble = Tinted(sharedMaterial, "darkMarble", 0.18f, 3f);
				signText = PrefabManager.Instance.GetPrefab("sign").GetComponent<Sign>().m_textWidget;
				halo = CreateHaloSprite();
				edgeGlowSprite = CreateEdgeGlowSprite();
			}
		}

		private static Sprite CreateEdgeGlowSprite()
		{
			//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_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			//IL_010d: 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_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(128, 128, (TextureFormat)4, false)
			{
				wrapMode = (TextureWrapMode)1,
				filterMode = (FilterMode)1
			};
			Color[] array = (Color[])(object)new Color[16384];
			for (int i = 0; i < 128; i++)
			{
				for (int j = 0; j < 128; j++)
				{
					float num = ((float)j + 0.5f) / 128f;
					float num2 = ((float)i + 0.5f) / 128f;
					float num3 = Mathf.Exp((0f - Mathf.Min(num, 1f - num)) / 0.06f);
					float num4 = Mathf.Exp((0f - Mathf.Min(num2, 1f - num2)) / 0.06f);
					array[i * 128 + j] = new Color(1f, 1f, 1f, 1f - (1f - num3) * (1f - num4));
				}
			}
			val.SetPixels(array);
			val.Apply();
			return Sprite.Create(val, new Rect(0f, 0f, 128f, 128f), new Vector2(0.5f, 0.5f));
		}

		private static Transform Find(string prefab, string path)
		{
			return PrefabManager.Instance.GetPrefab(prefab).transform.Find(path);
		}

		private static Material Tinted(Material source, string name, float brightness, float tiling)
		{
			//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_0017: 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_001f: 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_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			return new Material(source)
			{
				name = "ItemDrawer_" + name,
				color = source.color * brightness,
				mainTextureScale = new Vector2(tiling, tiling)
			};
		}

		private static Sprite CreateHaloSprite()
		{
			//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_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: 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_0058: 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_00b2: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(128, 128, (TextureFormat)4, false)
			{
				wrapMode = (TextureWrapMode)1,
				filterMode = (FilterMode)1
			};
			Color[] array = (Color[])(object)new Color[16384];
			for (int i = 0; i < 128; i++)
			{
				for (int j = 0; j < 128; j++)
				{
					float num = Vector2.Distance(new Vector2((float)j + 0.5f, (float)i + 0.5f), new Vector2(64f, 64f)) / 64f;
					array[i * 128 + j] = new Color(1f, 1f, 1f, (num >= 1f) ? 0f : (Mathf.Exp((0f - num) * num * 4f) * (1f - num)));
				}
			}
			val.SetPixels(array);
			val.Apply();
			return Sprite.Create(val, new Rect(0f, 0f, 128f, 128f), new Vector2(0.5f, 0.5f));
		}

		private static void StripChestVisuals(GameObject prefab, out int visualLayer, out int colliderLayer)
		{
			colliderLayer = (visualLayer = LayerMask.NameToLayer("piece"));
			Renderer componentInChildren = prefab.GetComponentInChildren<Renderer>(true);
			if ((Object)(object)componentInChildren != (Object)null)
			{
				visualLayer = ((Component)componentInChildren).gameObject.layer;
			}
			Collider componentInChildren2 = prefab.GetComponentInChildren<Collider>(true);
			if ((Object)(object)componentInChildren2 != (Object)null)
			{
				colliderLayer = ((Component)componentInChildren2).gameObject.layer;
			}
			Collider[] components = prefab.GetComponents<Collider>();
			for (int i = 0; i < components.Length; i++)
			{
				Object.DestroyImmediate((Object)(object)components[i]);
			}
			LODGroup[] components2 = prefab.GetComponents<LODGroup>();
			for (int i = 0; i < components2.Length; i++)
			{
				Object.DestroyImmediate((Object)(object)components2[i]);
			}
			for (int num = prefab.transform.childCount - 1; num >= 0; num--)
			{
				GameObject gameObject = ((Component)prefab.transform.GetChild(num)).gameObject;
				if ((Object)(object)gameObject.GetComponentInChildren<Renderer>(true) != (Object)null || (Object)(object)gameObject.GetComponentInChildren<Collider>(true) != (Object)null)
				{
					Object.DestroyImmediate((Object)(object)gameObject);
				}
			}
			Container component = prefab.GetComponent<Container>();
			component.m_open = null;
			component.m_closed = null;
		}

		private static void AddIronTrim(Transform v)
		{
			//IL_0024: 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_003f: 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_0063: 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_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: 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_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: 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_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
			float num = 0.47750002f;
			float num2 = 0.415f;
			Part(v, cube, iron, new Vector3(0f, 0.5f + num2, num), new Vector3(0.86f, 0.03f, 0.054999977f));
			Part(v, cube, iron, new Vector3(0f, 0.5f - num2, num), new Vector3(0.86f, 0.03f, 0.054999977f));
			Part(v, cube, iron, new Vector3(0f - num2, 0.5f, num), new Vector3(0.03f, 0.8f, 0.054999977f));
			Part(v, cube, iron, new Vector3(num2, 0.5f, num), new Vector3(0.03f, 0.8f, 0.054999977f));
		}

		private static void AddDivider(Transform f, float bodyFront)
		{
			//IL_002a: 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_003f: Unknown result type (might be due to invalid IL or missing references)
			float num = 0.495f - bodyFront;
			Part(f, marbleBlock, marble, new Vector3(0f, 0.5f, 0.495f - num / 2f), new Vector3(0.8f, 0.08f, num), marbleCenter);
		}

		private static float[] CompartmentCenters(int slots, out float height)
		{
			if (slots == 1)
			{
				height = 0.8f;
				return new float[1];
			}
			height = 0.36f;
			float num = (0.08f + height) / 2f;
			return new float[2]
			{
				num,
				0f - num
			};
		}

		private static void AddSnapPoints(Transform root)
		{
			//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_00b9: 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)
			float[] array = new float[2] { 0f, 1f };
			foreach (float num in array)
			{
				float[] array2 = new float[2] { 0.5f, -0.5f };
				foreach (float num2 in array2)
				{
					float[] array3 = new float[2] { -0.5f, 0.5f };
					foreach (float num3 in array3)
					{
						GameObject val = new GameObject(((num > 0f) ? "Top" : "Bottom") + ((num2 > 0f) ? " front" : " back") + ((num3 < 0f) ? " left" : " right"))
						{
							tag = "snappoint"
						};
						val.transform.SetParent(root, false);
						val.transform.localPosition = new Vector3(num3, num, num2);
					}
				}
			}
		}

		private static void AddDisplay(Transform root, int slots)
		{
			//IL_0028: 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)
			//IL_0035: Expected O, but got Unknown
			//IL_0048: 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_0058: 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_0072: 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_008c: 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_00a0: 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_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Expected O, but got Unknown
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_010e: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: 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)
			int layer = ((Component)signText).gameObject.layer;
			GameObject val = new GameObject("ItemDrawerDisplay", new Type[1] { typeof(RectTransform) })
			{
				layer = layer
			};
			val.transform.SetParent(root, false);
			RectTransform val2 = (RectTransform)val.transform;
			val2.sizeDelta = new Vector2(100f, 100f);
			((Transform)val2).localPosition = new Vector3(0f, 0.5f, 0.39499998f);
			((Transform)val2).localRotation = Quaternion.Euler(0f, 180f, 0f);
			((Transform)val2).localScale = Vector3.one * 0.008f;
			val.AddComponent<Canvas>().renderMode = (RenderMode)2;
			float height;
			float[] array = CompartmentCenters(slots, out height);
			for (int i = 0; i < slots; i++)
			{
				GameObject val3 = new GameObject(SlotName(i), new Type[1] { typeof(RectTransform) })
				{
					layer = layer
				};
				val3.transform.SetParent(val.transform, false);
				RectTransform val4 = (RectTransform)val3.transform;
				val4.sizeDelta = new Vector2(100f, height * 125f);
				val4.anchoredPosition = new Vector2(0f, array[i] * 125f);
				if (slots == 1)
				{
					AddSingleSlot(val3.transform);
				}
				else
				{
					AddStackedSlot(val3.transform, height * 125f);
				}
			}
		}

		private static void AddSingleSlot(Transform slot)
		{
			//IL_000b: 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_003f: 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)
			AddEdgeGlow(slot, new Vector2(100f, 100f));
			AddHalo(slot, 76f, new Vector2(0f, 14f));
			AddIcon(slot, 52f, new Vector2(0f, 14f));
			AddCount(slot, 88f, new Vector2(0f, -30f));
		}

		private static void AddStackedSlot(Transform slot, float height)
		{
			//IL_0007: 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_0041: 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)
			AddEdgeGlow(slot, new Vector2(100f, height));
			AddHalo(slot, Mathf.Min(50f, height), new Vector2(-28f, 0f));
			AddIcon(slot, 34f, new Vector2(-28f, 0f));
			AddCount(slot, 48f, new Vector2(20f, 0f));
		}

		private static void AddEdgeGlow(Transform parent, Vector2 size)
		{
			//IL_0006: 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_0031: Unknown result type (might be due to invalid IL or missing references)
			Image obj = AddImage(parent, "EdgeGlow", size, Vector2.zero);
			obj.sprite = edgeGlowSprite;
			((Graphic)obj).color = new Color(0.72f, 0.42f, 1f, 0.09f);
			((Behaviour)obj).enabled = true;
		}

		private static void AddHalo(Transform parent, float size, Vector2 position)
		{
			//IL_0008: 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_0032: Unknown result type (might be due to invalid IL or missing references)
			Image obj = AddImage(parent, "Halo", new Vector2(size, size), position);
			obj.sprite = halo;
			((Graphic)obj).color = new Color(1f, 0.93f, 0.8f, 0.08f);
		}

		private static void AddIcon(Transform parent, float size, Vector2 position)
		{
			//IL_0008: 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)
			AddImage(parent, "Icon", new Vector2(size, size), position).preserveAspect = true;
		}

		private static void AddCount(Transform parent, float width, Vector2 position)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: 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_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: 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_004d: 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_00b4: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("Count", new Type[1] { typeof(RectTransform) })
			{
				layer = ((Component)parent).gameObject.layer
			};
			val.transform.SetParent(parent, false);
			RectTransform val2 = (RectTransform)val.transform;
			val2.sizeDelta = new Vector2(width, 60f);
			val2.anchoredPosition = position;
			TextMeshProUGUI obj = val.AddComponent<TextMeshProUGUI>();
			((TMP_Text)obj).font = ((TMP_Text)signText).font;
			((TMP_Text)obj).fontSize = 30f;
			((TMP_Text)obj).fontSizeMin = 6f;
			((TMP_Text)obj).fontSizeMax = 30f;
			((TMP_Text)obj).fontStyle = (FontStyles)1;
			((TMP_Text)obj).alignment = (TextAlignmentOptions)514;
			((TMP_Text)obj).textWrappingMode = (TextWrappingModes)0;
			((TMP_Text)obj).overflowMode = (TextOverflowModes)0;
			((Graphic)obj).color = Color.white;
			((TMP_Text)obj).text = "";
		}

		private static Image AddImage(Transform parent, string name, Vector2 size, Vector2 position)
		{
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: 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_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_0043: 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)
			GameObject val = new GameObject(name, new Type[1] { typeof(RectTransform) })
			{
				layer = ((Component)parent).gameObject.layer
			};
			val.transform.SetParent(parent, false);
			RectTransform val2 = (RectTransform)val.transform;
			val2.sizeDelta = size;
			val2.anchoredPosition = position;
			Image obj = val.AddComponent<Image>();
			((Behaviour)obj).enabled = false;
			return obj;
		}

		private static void Part(Transform parent, Mesh mesh, Material mat, Vector3 center, Vector3 size, Vector3 pivot = default(Vector3))
		{
			//IL_0025: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: 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_0048: 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_005b: 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_005e: 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_0065: 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)
			GameObject val = new GameObject("part", new Type[2]
			{
				typeof(MeshFilter),
				typeof(MeshRenderer)
			})
			{
				layer = ((Component)parent).gameObject.layer
			};
			val.transform.SetParent(parent, false);
			val.transform.localScale = size;
			val.transform.localPosition = center - Vector3.Scale(pivot, size);
			val.GetComponent<MeshFilter>().sharedMesh = mesh;
			((Renderer)val.GetComponent<MeshRenderer>()).sharedMaterial = mat;
		}
	}
	public class ItemDrawer : MonoBehaviour
	{
		private static readonly string[] TypeKeys = new string[2] { "SimpleItemDrawer_type", "SimpleItemDrawer_type2" };

		private const string ClearTypeRpc = "SimpleItemDrawer_ClearType";

		private const string ClearTypeResultRpc = "SimpleItemDrawer_ClearTypeResult";

		private static readonly ConditionalWeakTable<Inventory, ItemDrawer> byInventory = new ConditionalWeakTable<Inventory, ItemDrawer>();

		private static readonly Dictionary<string, Sprite> iconByType = new Dictionary<string, Sprite>();

		public int slotCount = 1;

		private Container container;

		private ZNetView nview;

		private Image[] icons;

		private Image[] halos;

		private TMP_Text[] countTexts;

		private static readonly int ColorId = Shader.PropertyToID("_Color");

		private static readonly int EmissionId = Shader.PropertyToID("_EmissionColor");

		private static readonly Func<WearNTear, float> supportColorValue = CreateSupportColorValue();

		private GameObject frameGroup;

		private Inventory Inventory => container.GetInventory();

		internal bool CanClearType
		{
			get
			{
				for (int i = 0; i < slotCount; i++)
				{
					if (IsClearable(i))
					{
						return true;
					}
				}
				return false;
			}
		}

		internal string ClearTypeHint
		{
			get
			{
				if (slotCount != 1)
				{
					return "$itemdrawer_clear_types";
				}
				return "$itemdrawer_clear_type";
			}
		}

		private string NothingToClearMessage
		{
			get
			{
				if (slotCount != 1)
				{
					return "$itemdrawer_msg_no_empty_type";
				}
				return "$itemdrawer_msg_not_empty";
			}
		}

		internal bool HasItems
		{
			get
			{
				if ((Object)(object)container != (Object)null)
				{
					return Inventory.NrOfItems() > 0;
				}
				return false;
			}
		}

		internal static ItemDrawer Get(Inventory inventory)
		{
			if (inventory == null || !byInventory.TryGetValue(inventory, out var value))
			{
				return null;
			}
			return value;
		}

		internal void Init(Container owner)
		{
			if ((Object)(object)container != (Object)null)
			{
				return;
			}
			container = owner;
			nview = ((Component)this).GetComponent<ZNetView>();
			Inventory inventory = owner.GetInventory();
			byInventory.Add(inventory, this);
			nview.Register("SimpleItemDrawer_ClearType", (Action<long>)RPC_ClearType);
			nview.Register<bool>("SimpleItemDrawer_ClearTypeResult", (Action<long, bool>)RPC_ClearTypeResult);
			icons = (Image[])(object)new Image[slotCount];
			halos = (Image[])(object)new Image[slotCount];
			countTexts = (TMP_Text[])(object)new TMP_Text[slotCount];
			for (int i = 0; i < slotCount; i++)
			{
				Transform val = ((Component)this).transform.Find("ItemDrawerDisplay/" + DrawerPrefab.SlotName(i));
				if (!((Object)(object)val == (Object)null))
				{
					Image[] array = icons;
					int num = i;
					Transform obj = val.Find("Icon");
					array[num] = ((obj != null) ? ((Component)obj).GetComponent<Image>() : null);
					Image[] array2 = halos;
					int num2 = i;
					Transform obj2 = val.Find("Halo");
					array2[num2] = ((obj2 != null) ? ((Component)obj2).GetComponent<Image>() : null);
					TMP_Text[] array3 = countTexts;
					int num3 = i;
					Transform obj3 = val.Find("Count");
					array3[num3] = ((obj3 != null) ? ((Component)obj3).GetComponent<TMP_Text>() : null);
				}
			}
			inventory.m_onChanged = (Action)Delegate.Combine(inventory.m_onChanged, new Action(RefreshDisplay));
			RefreshDisplay();
		}

		private static Func<WearNTear, float> CreateSupportColorValue()
		{
			MethodInfo methodInfo = AccessTools.Method(typeof(WearNTear), "GetSupportColorValue", (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				return null;
			}
			try
			{
				return AccessTools.MethodDelegate<Func<WearNTear, float>>(methodInfo, (object)null, true);
			}
			catch (Exception)
			{
				return null;
			}
		}

		internal bool Highlight(WearNTear wnt)
		{
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: 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_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d5: 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)
			if ((Object)(object)frameGroup == (Object)null)
			{
				Transform val = ((Component)this).transform.Find("New/Frame");
				if ((Object)(object)val == (Object)null || (Object)(object)MaterialMan.instance == (Object)null)
				{
					return false;
				}
				frameGroup = ((Component)val).gameObject;
			}
			Color val2 = default(Color);
			((Color)(ref val2))..ctor(0.6f, 0.8f, 1f);
			float num = ((supportColorValue != null) ? supportColorValue(wnt) : (-1f));
			if (num >= 0f)
			{
				float num2 = default(float);
				float num3 = default(float);
				float num4 = default(float);
				Color.RGBToHSV(Color.Lerp(new Color(1f, 0f, 0f), new Color(0f, 1f, 0f), num), ref num2, ref num3, ref num4);
				val2 = Color.HSVToRGB(num2, Mathf.Lerp(1f, 0.5f, num), Mathf.Lerp(1.2f, 0.9f, num));
			}
			MaterialMan.instance.SetValue<Color>(frameGroup, EmissionId, val2 * 0.4f, false);
			MaterialMan.instance.SetValue<Color>(frameGroup, ColorId, val2, false);
			((MonoBehaviour)this).CancelInvoke("ResetHighlight");
			((MonoBehaviour)this).Invoke("ResetHighlight", 0.2f);
			return true;
		}

		private void ResetHighlight()
		{
			if (!((Object)(object)frameGroup == (Object)null) && !((Object)(object)MaterialMan.instance == (Object)null))
			{
				MaterialMan.instance.ResetValue(frameGroup, ColorId);
				MaterialMan.instance.ResetValue(frameGroup, EmissionId);
			}
		}

		private void Start()
		{
			RefreshDisplay();
		}

		private void RefreshDisplay()
		{
			if (countTexts == null)
			{
				return;
			}
			string[] array = Types();
			for (int i = 0; i < slotCount; i++)
			{
				Sprite val = ((array[i] == null) ? null : GetIcon(array[i]));
				if ((Object)(object)icons[i] != (Object)null)
				{
					icons[i].sprite = val;
					((Behaviour)icons[i]).enabled = (Object)(object)val != (Object)null;
				}
				if ((Object)(object)halos[i] != (Object)null)
				{
					((Behaviour)halos[i]).enabled = (Object)(object)val != (Object)null;
				}
				if (!((Object)(object)countTexts[i] == (Object)null))
				{
					DrawerPrefab.PrepareCountText(countTexts[i]);
					countTexts[i].text = ((array[i] == null) ? "" : CountItems(array[i]).ToString("N0", CultureInfo.InvariantCulture));
				}
			}
			DrawerPrefab.FitCounts(countTexts);
		}

		private Sprite GetIcon(string type)
		{
			foreach (ItemData allItem in Inventory.GetAllItems())
			{
				if (allItem.m_shared.m_name == type)
				{
					return allItem.GetIcon();
				}
			}
			if (iconByType.TryGetValue(type, out var value))
			{
				return value;
			}
			Sprite val = null;
			if ((Object)(object)ObjectDB.instance != (Object)null)
			{
				foreach (GameObject item in ObjectDB.instance.m_items)
				{
					ItemDrop val2 = (((Object)(object)item != (Object)null) ? item.GetComponent<ItemDrop>() : null);
					if ((Object)(object)val2 != (Object)null && val2.m_itemData.m_shared.m_name == type)
					{
						val = val2.m_itemData.GetIcon();
						break;
					}
				}
			}
			iconByType[type] = val;
			return val;
		}

		private string StoredType(int slot)
		{
			if (!((Object)(object)nview != (Object)null) || !nview.IsValid())
			{
				return "";
			}
			return nview.GetZDO().GetString(TypeKeys[slot], "");
		}

		internal string[] Types()
		{
			string[] array = new string[slotCount];
			bool flag = false;
			for (int i = 0; i < slotCount; i++)
			{
				string text = StoredType(i);
				if (string.IsNullOrEmpty(text))
				{
					flag = true;
				}
				else
				{
					array[i] = text;
				}
			}
			if (!flag)
			{
				return array;
			}
			foreach (ItemData allItem in Inventory.GetAllItems())
			{
				string name = allItem.m_shared.m_name;
				if (Array.IndexOf(array, name) < 0)
				{
					int num = Array.IndexOf(array, null);
					if (num < 0)
					{
						break;
					}
					array[num] = name;
				}
			}
			return array;
		}

		internal bool Accepts(ItemData item)
		{
			if (item.m_shared.m_maxStackSize <= 1)
			{
				return false;
			}
			string[] array = Types();
			if (Array.IndexOf(array, item.m_shared.m_name) < 0)
			{
				return Array.IndexOf(array, null) >= 0;
			}
			return true;
		}

		internal void ShowRejection(ItemData item)
		{
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)localPlayer == (Object)null))
			{
				string text;
				if (item.m_shared.m_maxStackSize <= 1)
				{
					text = "$itemdrawer_msg_not_stackable";
				}
				else
				{
					string[] array = Types();
					Localization instance = Localization.instance;
					text = ((slotCount == 1) ? instance.Localize("$itemdrawer_msg_wrong_type", new string[1] { instance.Localize(array[0]) }) : instance.Localize("$itemdrawer_msg_wrong_types", new string[2]
					{
						instance.Localize(array[0]),
						instance.Localize(array[1])
					}));
				}
				((Character)localPlayer).Message((MessageType)2, text, 0, (Sprite)null, false);
			}
		}

		private int CountItems(string type)
		{
			int num = 0;
			foreach (ItemData allItem in Inventory.GetAllItems())
			{
				if (allItem.m_shared.m_name == type)
				{
					num += allItem.m_stack;
				}
			}
			return num;
		}

		internal bool IsLabeledFor(string sharedName)
		{
			for (int i = 0; i < slotCount; i++)
			{
				if (StoredType(i) == sharedName)
				{
					return true;
				}
			}
			return false;
		}

		private bool IsClearable(int slot)
		{
			string text = StoredType(slot);
			if (!string.IsNullOrEmpty(text))
			{
				return CountItems(text) == 0;
			}
			return false;
		}

		internal void StoreTypesFromContents()
		{
			if (!nview.IsOwner())
			{
				return;
			}
			string[] array = Types();
			for (int i = 0; i < slotCount; i++)
			{
				if (array[i] != null && string.IsNullOrEmpty(StoredType(i)))
				{
					nview.GetZDO().Set(TypeKeys[i], array[i]);
				}
			}
		}

		internal void RequestClearType(Player player)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			if (container.m_checkGuardStone && !PrivateArea.CheckAccess(((Component)this).transform.position, 0f, true, false))
			{
				return;
			}
			if (!CanClearType)
			{
				if (Inventory.NrOfItems() > 0)
				{
					((Character)player).Message((MessageType)2, NothingToClearMessage, 0, (Sprite)null, false);
				}
			}
			else
			{
				nview.InvokeRPC("SimpleItemDrawer_ClearType", Array.Empty<object>());
			}
		}

		private void RPC_ClearType(long sender)
		{
			if (!nview.IsOwner())
			{
				return;
			}
			bool flag = false;
			for (int i = 0; i < slotCount; i++)
			{
				if (IsClearable(i))
				{
					nview.GetZDO().Set(TypeKeys[i], "");
					flag = true;
				}
			}
			if (flag)
			{
				RefreshDisplay();
			}
			nview.InvokeRPC(sender, "SimpleItemDrawer_ClearTypeResult", new object[1] { flag });
		}

		private void RPC_ClearTypeResult(long sender, bool cleared)
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer != (Object)null)
			{
				((Character)localPlayer).Message((MessageType)2, cleared ? "$itemdrawer_msg_type_cleared" : NothingToClearMessage, 0, (Sprite)null, false);
			}
		}
	}
	[HarmonyPatch(typeof(Container), "Awake")]
	internal static class ContainerAwakePatch
	{
		private static void Postfix(Container __instance)
		{
			ItemDrawer component = ((Component)__instance).GetComponent<ItemDrawer>();
			if ((Object)(object)component != (Object)null && __instance.GetInventory() != null)
			{
				component.Init(__instance);
			}
		}
	}
	[HarmonyPatch(typeof(Container), "Save")]
	internal static class ContainerSavePatch
	{
		private static void Prefix(Container __instance)
		{
			((Component)__instance).GetComponent<ItemDrawer>()?.StoreTypesFromContents();
		}
	}
	[HarmonyPatch(typeof(Piece), "CanBeRemoved")]
	internal static class PieceCanBeRemovedPatch
	{
		private static void Postfix(Piece __instance, ref bool __result)
		{
			if (!__result)
			{
				return;
			}
			ItemDrawer component = ((Component)__instance).GetComponent<ItemDrawer>();
			if (!((Object)(object)component == (Object)null) && component.HasItems)
			{
				__result = false;
				if (PlayerRemovePiecePatch.Active)
				{
					PlayerRemovePiecePatch.DrawerRefused = true;
				}
			}
		}
	}
	[HarmonyPatch(typeof(Player), "RemovePiece")]
	internal static class PlayerRemovePiecePatch
	{
		internal static bool Active;

		internal static bool DrawerRefused;

		private static void Prefix()
		{
			Active = true;
			DrawerRefused = false;
		}

		private static void Finalizer()
		{
			Active = false;
			DrawerRefused = false;
		}
	}
	[HarmonyPatch(typeof(Player), "Message")]
	internal static class PlayerMessagePatch
	{
		private static void Prefix(ref string msg)
		{
			if (PlayerRemovePiecePatch.DrawerRefused && msg == "$msg_cantremovenow")
			{
				msg = "$itemdrawer_msg_not_empty";
			}
		}
	}
	[HarmonyPatch(typeof(WearNTear), "Highlight")]
	internal static class WearNTearHighlightPatch
	{
		private static bool Prefix(WearNTear __instance)
		{
			ItemDrawer component = ((Component)__instance).GetComponent<ItemDrawer>();
			if (!((Object)(object)component == (Object)null))
			{
				return !component.Highlight(__instance);
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(Container), "GetHoverText")]
	internal static class ContainerHoverTextPatch
	{
		[HarmonyPriority(0)]
		private static void Postfix(Container __instance, ref string __result)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			ItemDrawer component = ((Component)__instance).GetComponent<ItemDrawer>();
			if (!((Object)(object)component == (Object)null) && component.CanClearType && (!__instance.m_checkGuardStone || PrivateArea.CheckAccess(((Component)__instance).transform.position, 0f, false, false)))
			{
				__result += Localization.instance.Localize("\n[<color=yellow><b>" + Plugin.ClearTypeKeyLabel + "</b></color>] " + component.ClearTypeHint);
			}
		}
	}
	[HarmonyPatch]
	internal static class InventoryAddItemPatch
	{
		private static IEnumerable<MethodBase> TargetMethods()
		{
			yield return AccessTools.Method(typeof(Inventory), "AddItem", new Type[1] { typeof(ItemData) }, (Type[])null);
			yield return AccessTools.Method(typeof(Inventory), "AddItem", new Type[2]
			{
				typeof(ItemData),
				typeof(Vector2i)
			}, (Type[])null);
			yield return AccessTools.Method(typeof(Inventory), "AddItem", new Type[5]
			{
				typeof(ItemData),
				typeof(int),
				typeof(int),
				typeof(int),
				typeof(bool)
			}, (Type[])null);
		}

		private static bool Prefix(Inventory __instance, ItemData item, ref bool __result)
		{
			ItemDrawer itemDrawer = ItemDrawer.Get(__instance);
			if ((Object)(object)itemDrawer == (Object)null || itemDrawer.Accepts(item))
			{
				return true;
			}
			__result = false;
			return false;
		}
	}
	[HarmonyPatch(typeof(Inventory), "CanAddItem", new Type[]
	{
		typeof(ItemData),
		typeof(int)
	})]
	internal static class InventoryCanAddItemPatch
	{
		private static void Postfix(Inventory __instance, ItemData item, ref bool __result)
		{
			if (__result)
			{
				ItemDrawer itemDrawer = ItemDrawer.Get(__instance);
				if ((Object)(object)itemDrawer != (Object)null && !itemDrawer.Accepts(item))
				{
					__result = false;
				}
			}
		}
	}
	[HarmonyPatch(typeof(Inventory), "HaveItem", new Type[]
	{
		typeof(string),
		typeof(bool)
	})]
	internal static class InventoryHaveItemPatch
	{
		private static void Postfix(Inventory __instance, string name, ref bool __result)
		{
			if (!__result)
			{
				ItemDrawer itemDrawer = ItemDrawer.Get(__instance);
				if ((Object)(object)itemDrawer != (Object)null && itemDrawer.IsLabeledFor(name))
				{
					__result = true;
				}
			}
		}
	}
	[HarmonyPatch(typeof(Inventory), "ContainsItemByName")]
	internal static class InventoryContainsItemByNamePatch
	{
		private static void Postfix(Inventory __instance, string name, ref bool __result)
		{
			if (!__result)
			{
				ItemDrawer itemDrawer = ItemDrawer.Get(__instance);
				if ((Object)(object)itemDrawer != (Object)null && itemDrawer.IsLabeledFor(name))
				{
					__result = true;
				}
			}
		}
	}
	[HarmonyPatch(typeof(Inventory), "MoveItemToThis", new Type[]
	{
		typeof(Inventory),
		typeof(ItemData)
	})]
	internal static class InventoryMoveItemToThisPatch
	{
		private static bool Prefix(Inventory __instance, ItemData item)
		{
			ItemDrawer itemDrawer = ItemDrawer.Get(__instance);
			if ((Object)(object)itemDrawer == (Object)null || itemDrawer.Accepts(item))
			{
				return true;
			}
			itemDrawer.ShowRejection(item);
			return false;
		}
	}
	[HarmonyPatch(typeof(InventoryGrid), "DropItem")]
	internal static class InventoryGridDropItemPatch
	{
		private static bool Prefix(InventoryGrid __instance, Inventory fromInventory, ItemData item, int amount, Vector2i pos, ref bool __result)
		{
			//IL_0008: 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)
			Inventory inventory = __instance.GetInventory();
			ItemData itemAt = inventory.GetItemAt(pos.x, pos.y);
			if (itemAt == item)
			{
				return true;
			}
			ItemDrawer itemDrawer = ItemDrawer.Get(inventory);
			if ((Object)(object)itemDrawer != (Object)null && !itemDrawer.Accepts(item))
			{
				itemDrawer.ShowRejection(item);
				__result = false;
				return false;
			}
			bool num = itemAt != null && item.m_stack == amount && (itemAt.m_shared.m_name != item.m_shared.m_name || (item.m_shared.m_maxQuality > 1 && itemAt.m_quality != item.m_quality) || itemAt.m_shared.m_maxStackSize == 1);
			ItemDrawer itemDrawer2 = ItemDrawer.Get(fromInventory);
			if (num && (Object)(object)itemDrawer2 != (Object)null && !itemDrawer2.Accepts(itemAt))
			{
				itemDrawer2.ShowRejection(itemAt);
				__result = false;
				return false;
			}
			return true;
		}
	}
	[BepInPlugin("Iteravio.SimpleItemDrawer", "SimpleItemDrawer", "1.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "Iteravio.SimpleItemDrawer";

		public const string PluginName = "SimpleItemDrawer";

		public const string PluginVersion = "1.1.0";

		internal const string PiecePrefabName = "piece_itemdrawer";

		internal const string DoublePiecePrefabName = "piece_itemdrawer_double";

		private const int GridWidth = 8;

		private const int GridHeight = 25;

		internal static ManualLogSource Log;

		private static ConfigEntry<KeyboardShortcut> clearTypeKey;

		private static ConfigEntry<Color> countTextColor;

		internal static Color CountTextColor => countTextColor.Value;

		internal unsafe static string ClearTypeKeyLabel
		{
			get
			{
				//IL_0005: 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_000d: Unknown result type (might be due to invalid IL or missing references)
				//IL_0014: Invalid comparison between Unknown and I4
				//IL_001b: 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)
				KeyboardShortcut value = clearTypeKey.Value;
				if ((int)((KeyboardShortcut)(ref value)).MainKey != 47)
				{
					value = clearTypeKey.Value;
					return ((object)(*(KeyboardShortcut*)(&value))/*cast due to .constrained prefix*/).ToString();
				}
				return "/";
			}
		}

		private void Awake()
		{
			//IL_0022: 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_00ad: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			clearTypeKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Controls", "ClearItemTypeKey", new KeyboardShortcut((KeyCode)47, Array.Empty<KeyCode>()), "Clears the item type of the empty drawer you're looking at. [Not synced with Server]");
			countTextColor = ((BaseUnityPlugin)this).Config.Bind<Color>("Display", "CountTextColor", new Color(1f, 0.729f, 0.251f), "Color of the item count on drawer fronts. It glows slightly at night. [Not synced with Server]");
			countTextColor.SettingChanged += delegate
			{
				DrawerPrefab.ApplyCountColor();
			};
			AddTranslations();
			PrefabManager.OnVanillaPrefabsAvailable += AddDrawerPieces;
			new Harmony("Iteravio.SimpleItemDrawer").PatchAll();
		}

		private void Update()
		{
			//IL_000a: 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)
			DrawerPrefab.SyncCountAtlas();
			KeyboardShortcut value = clearTypeKey.Value;
			if (!((KeyboardShortcut)(ref value)).IsDown())
			{
				return;
			}
			Player localPlayer = Player.m_localPlayer;
			if (!((Object)(object)localPlayer == (Object)null) && !IsTyping())
			{
				GameObject hoverObject = ((Humanoid)localPlayer).GetHoverObject();
				ItemDrawer itemDrawer = (((Object)(object)hoverObject != (Object)null) ? hoverObject.GetComponentInParent<ItemDrawer>() : null);
				if ((Object)(object)itemDrawer != (Object)null)
				{
					itemDrawer.RequestClearType(localPlayer);
				}
			}
		}

		private static bool IsTyping()
		{
			if (!InventoryGui.IsVisible() && !Console.IsVisible() && !TextInput.IsVisible() && !Menu.IsVisible())
			{
				if ((Object)(object)Chat.instance != (Object)null)
				{
					return Chat.instance.HasFocus();
				}
				return false;
			}
			return true;
		}

		private static void AddTranslations()
		{
			CustomLocalization localization = LocalizationManager.Instance.GetLocalization();
			string text = "English";
			localization.AddTranslation(ref text, new Dictionary<string, string>
			{
				{ "piece_itemdrawer", "Item Drawer" },
				{ "piece_itemdrawer_description", "Holds up to 200 stacks of a single stackable item." },
				{ "piece_itemdrawer_double", "Double Item Drawer" },
				{ "piece_itemdrawer_double_description", "Holds up to 200 stacks of up to two stackable items." },
				{ "itemdrawer_clear_type", "Clear Drawer's item type" },
				{ "itemdrawer_clear_types", "Clear Drawer's unused item types" },
				{ "itemdrawer_msg_wrong_type", "This Drawer can only hold $1" },
				{ "itemdrawer_msg_wrong_types", "This Drawer can only hold $1 and $2" },
				{ "itemdrawer_msg_not_stackable", "Drawers can only hold stackable items" },
				{ "itemdrawer_msg_not_empty", "Empty the Drawer first" },
				{ "itemdrawer_msg_no_empty_type", "Empty a compartment first" },
				{ "itemdrawer_msg_type_cleared", "Drawer item type cleared" }
			});
		}

		private void AddDrawerPieces()
		{
			PrefabManager.OnVanillaPrefabsAvailable -= AddDrawerPieces;
			AddDrawerPiece("piece_itemdrawer", "$piece_itemdrawer", 1);
			AddDrawerPiece("piece_itemdrawer_double", "$piece_itemdrawer_double", 2);
		}

		private static void AddDrawerPiece(string prefabName, string nameKey, int slots)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Expected O, but got Unknown
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Expected O, but got Unknown
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Expected O, but got Unknown
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Expected O, but got Unknown
			PieceConfig val = new PieceConfig();
			val.Name = nameKey;
			val.Description = nameKey + "_description";
			val.PieceTable = PieceTables.Hammer;
			val.Category = PieceCategories.Furniture;
			val.CraftingStation = CraftingStations.Workbench;
			val.Requirements = (RequirementConfig[])(object)new RequirementConfig[2]
			{
				new RequirementConfig("Stone", 25, 0, true),
				new RequirementConfig("GreydwarfEye", 5, 0, true)
			};
			PieceConfig val2 = val;
			CustomPiece val3 = new CustomPiece(prefabName, "piece_chest_wood", val2);
			Container component = val3.PiecePrefab.GetComponent<Container>();
			component.m_name = nameKey;
			component.m_width = 8;
			component.m_height = 25;
			DrawerPrefab.Build(val3.PiecePrefab, slots);
			val3.PiecePrefab.AddComponent<ItemDrawer>().slotCount = slots;
			Sprite val4 = DrawerPrefab.RenderIcon(val3.PiecePrefab, slots);
			if ((Object)(object)val4 != (Object)null)
			{
				val3.Piece.m_icon = val4;
			}
			PieceManager.Instance.AddPiece(val3);
		}
	}
}