Decompiled source of Search On Production v0.2.0

BepInEx/plugins/SearchOnProduction/SearchOnProduction.dll

Decompiled 15 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
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("SearchOnProduction")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.2.0.0")]
[assembly: AssemblyInformationalVersion("0.2.0")]
[assembly: AssemblyProduct("SearchOnProduction")]
[assembly: AssemblyTitle("SearchOnProduction")]
[assembly: AssemblyVersion("0.2.0.0")]
namespace OFDTECH.SearchOnProduction;

[BepInPlugin("oftech.valheim.searchonproduction", "Search On Production", "0.2.0")]
public sealed class SearchOnProductionPlugin : BaseUnityPlugin
{
	public const string PluginGuid = "oftech.valheim.searchonproduction";

	public const string PluginName = "Search On Production";

	public const string PluginVersion = "0.2.0";

	private Harmony _harmony;

	internal static SearchOnProductionPlugin Instance { get; private set; }

	internal static ManualLogSource Log { get; private set; }

	private void Awake()
	{
		//IL_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Expected O, but got Unknown
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		_harmony = new Harmony("oftech.valheim.searchonproduction");
		_harmony.PatchAll(Assembly.GetExecutingAssembly());
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Search On Production 0.2.0 loaded.");
	}

	private void Update()
	{
		SearchOverlay.HandleKeyboardInput();
	}

	private void OnGUI()
	{
		SearchOverlay.Draw();
	}
}
[HarmonyPatch(typeof(InventoryGui), "Show")]
internal static class InventoryGui_Show_SearchOnProduction_Patch
{
	[HarmonyPrefix]
	[HarmonyPriority(800)]
	private static void Prefix(InventoryGui __instance)
	{
		SearchOverlay.Reset(__instance);
		SearchToolbarLayout.SetFallback(__instance, useFallback: false);
	}
}
[HarmonyPatch(typeof(InventoryGui), "Hide")]
internal static class InventoryGui_Hide_SearchOnProduction_Patch
{
	[HarmonyPrefix]
	[HarmonyPriority(800)]
	private static void Prefix(InventoryGui __instance)
	{
		SearchOverlay.Reset(__instance);
		SearchToolbarLayout.SetFallback(__instance, useFallback: false);
	}
}
[HarmonyPatch(typeof(InventoryGui), "UpdateRecipeList")]
internal static class InventoryGui_UpdateRecipeList_SearchOnProduction_Patch
{
	[HarmonyPrefix]
	[HarmonyPriority(800)]
	private static void Prefix(InventoryGui __instance, ref List<Recipe> recipes)
	{
		SearchToolbarLayout.SetFallback(__instance, useFallback: false);
		SearchOverlay.FilterRecipes(ref recipes);
	}

	[HarmonyPostfix]
	[HarmonyPriority(0)]
	private static void Postfix(InventoryGui __instance)
	{
		SearchToolbarLayout.SetFallback(__instance, SearchOverlay.UsesFallbackRow);
	}
}
[HarmonyPatch(typeof(Chat), "HasFocus")]
internal static class Chat_HasFocus_SearchOnProduction_Patch
{
	[HarmonyPostfix]
	private static void Postfix(ref bool __result)
	{
		if (SearchOverlay.HasKeyboardFocus)
		{
			__result = true;
		}
	}
}
internal static class SearchOverlay
{
	private const string SearchControlName = "SearchOnProduction.SearchField";

	private const int MaximumSearchLength = 128;

	private const float ButtonWidth = 24f;

	private const float HorizontalGap = 4f;

	private const float PanelMargin = 5f;

	private static string _query = string.Empty;

	private static bool _isOpen;

	private static bool _hasKeyboardFocus;

	private static bool _focusNextDraw;

	private static bool _clearFocusOnNextDraw;

	private static int _blockFocusUntilFrame = int.MinValue;

	private static bool _refreshInProgress;

	private static bool _warnedRefreshFailure;

	private static MethodInfo _updateCraftingPanelMethod;

	private static Texture2D _searchIcon;

	private static GUIStyle _buttonStyle;

	private static GUIStyle _fieldStyle;

	private static int _buttonFontSize = -1;

	private static int _fieldFontSize = -1;

	private static bool _usesFallbackRow;

	private static readonly List<Selectable> TabSelectables = new List<Selectable>();

	internal static bool UsesFallbackRow => _usesFallbackRow;

	internal static bool HasActiveCraftingStation
	{
		get
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer != (Object)null)
			{
				return (Object)(object)localPlayer.GetCurrentCraftingStation() != (Object)null;
			}
			return false;
		}
	}

	internal static bool HasKeyboardFocus
	{
		get
		{
			if (!_hasKeyboardFocus)
			{
				return Time.frameCount <= _blockFocusUntilFrame;
			}
			return true;
		}
	}

	internal static void Reset(InventoryGui gui)
	{
		if (_hasKeyboardFocus)
		{
			_clearFocusOnNextDraw = true;
		}
		_query = string.Empty;
		_isOpen = false;
		_hasKeyboardFocus = false;
		_focusNextDraw = false;
		_usesFallbackRow = false;
		_blockFocusUntilFrame = int.MinValue;
	}

	internal static void HandleKeyboardInput()
	{
		if (_hasKeyboardFocus && (Input.GetKeyDown((KeyCode)13) || Input.GetKeyDown((KeyCode)271) || Input.GetKeyDown((KeyCode)27)))
		{
			ReleaseKeyboardFocus();
		}
	}

	internal static void FilterRecipes(ref List<Recipe> recipes)
	{
		string text = _query.Trim();
		if (string.IsNullOrWhiteSpace(text) || recipes == null || !HasActiveCraftingStation)
		{
			return;
		}
		string[] array = Tokenize(text);
		if (array.Length == 0)
		{
			return;
		}
		List<Recipe> list = new List<Recipe>();
		List<Recipe> list2 = new List<Recipe>(recipes.Count);
		for (int i = 0; i < recipes.Count; i++)
		{
			Recipe val = recipes[i];
			List<string> productNames = GetProductNames(val);
			if (array.Length > 1 && IsExactProductMatch(productNames, array))
			{
				list.Add(val);
			}
			else if (AllWordsMatch(productNames, array))
			{
				list2.Add(val);
			}
		}
		recipes = ((array.Length > 1 && list.Count > 0) ? list : list2);
	}

	internal static void Draw()
	{
		//IL_0102: 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_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		//IL_0184: Unknown result type (might be due to invalid IL or missing references)
		//IL_017c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ea: Invalid comparison between Unknown and I4
		//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fc: Invalid comparison between Unknown and I4
		//IL_0200: Unknown result type (might be due to invalid IL or missing references)
		//IL_020a: Invalid comparison between Unknown and I4
		//IL_020e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0215: Invalid comparison between Unknown and I4
		InventoryGui instance = InventoryGui.instance;
		if ((Object)(object)instance == (Object)null || !InventoryGui.IsVisible())
		{
			ClearFocusIfRequested();
			return;
		}
		if (!HasActiveCraftingStation || (Object)(object)instance.m_recipeListRoot == (Object)null || (Object)(object)instance.m_crafting == (Object)null || !((Component)instance.m_crafting).gameObject.activeInHierarchy)
		{
			_usesFallbackRow = false;
			SearchToolbarLayout.SetFallback(instance, useFallback: false);
			ClearFocusIfRequested();
			return;
		}
		if (!TryGetToolbarRects(instance, out var buttonRect, out var fieldRect, out var scale))
		{
			_usesFallbackRow = false;
			SearchToolbarLayout.SetFallback(instance, useFallback: false);
			ClearFocusIfRequested();
			return;
		}
		SearchToolbarLayout.SetFallback(instance, _usesFallbackRow);
		EnsureStyles(scale);
		int depth = GUI.depth;
		GUI.depth = -100;
		try
		{
			Event current = Event.current;
			bool flag = false;
			ClearFocusIfRequested();
			if (current != null && (int)current.type == 0 && !((Rect)(ref buttonRect)).Contains(current.mousePosition) && (!_isOpen || !((Rect)(ref fieldRect)).Contains(current.mousePosition)) && _hasKeyboardFocus)
			{
				ReleaseKeyboardFocus();
				flag = true;
			}
			ClearFocusIfRequested();
			bool isOpen = _isOpen;
			if (GUI.Button(buttonRect, GUIContent.none, _buttonStyle))
			{
				if (isOpen)
				{
					CloseSearch(instance);
				}
				else
				{
					_isOpen = true;
					_focusNextDraw = true;
				}
			}
			ClearFocusIfRequested();
			if (_isOpen)
			{
				GUI.SetNextControlName("SearchOnProduction.SearchField");
				string text = GUI.TextField(fieldRect, _query, 128, _fieldStyle);
				if (!string.Equals(text, _query, StringComparison.Ordinal))
				{
					_query = text;
					RefreshRecipeList(instance);
				}
			}
			if (_isOpen)
			{
				DrawCloseMark(buttonRect);
			}
			else
			{
				DrawSearchIcon(buttonRect, scale);
			}
			if (_focusNextDraw && _isOpen)
			{
				GUI.FocusControl("SearchOnProduction.SearchField");
				_focusNextDraw = false;
				_hasKeyboardFocus = true;
				_clearFocusOnNextDraw = false;
			}
			else if (!_isOpen)
			{
				_hasKeyboardFocus = false;
			}
			else if (!flag)
			{
				_hasKeyboardFocus = GUI.GetNameOfFocusedControl() == "SearchOnProduction.SearchField";
			}
			if (current != null && (int)current.type == 4 && _hasKeyboardFocus && ((int)current.keyCode == 13 || (int)current.keyCode == 271 || (int)current.keyCode == 27))
			{
				ReleaseKeyboardFocus();
				current.Use();
			}
		}
		finally
		{
			GUI.depth = depth;
		}
	}

	private static void ClearFocusIfRequested()
	{
		if (_clearFocusOnNextDraw)
		{
			GUI.FocusControl((string)null);
			_clearFocusOnNextDraw = false;
		}
	}

	private static void ReleaseKeyboardFocus()
	{
		_hasKeyboardFocus = false;
		_clearFocusOnNextDraw = true;
		_blockFocusUntilFrame = Time.frameCount + 1;
	}

	private static void CloseSearch(InventoryGui gui)
	{
		bool num = !string.IsNullOrEmpty(_query);
		_query = string.Empty;
		_isOpen = false;
		_focusNextDraw = false;
		_hasKeyboardFocus = false;
		_clearFocusOnNextDraw = true;
		_blockFocusUntilFrame = Time.frameCount + 1;
		if (num)
		{
			RefreshRecipeList(gui);
		}
	}

	private static List<string> GetProductNames(Recipe recipe)
	{
		List<string> list = new List<string>(3);
		if ((Object)(object)recipe == (Object)null || (Object)(object)recipe.m_item == (Object)null)
		{
			return list;
		}
		if (!string.IsNullOrEmpty(((Object)recipe.m_item).name))
		{
			list.Add(((Object)recipe.m_item).name);
		}
		ItemData itemData = recipe.m_item.m_itemData;
		if (itemData == null || itemData.m_shared == null)
		{
			return list;
		}
		string name = itemData.m_shared.m_name;
		if (!string.IsNullOrEmpty(name))
		{
			list.Add(name);
		}
		Localization instance = Localization.instance;
		if (instance != null && !string.IsNullOrEmpty(name))
		{
			string text = instance.Localize(name);
			if (!string.IsNullOrEmpty(text))
			{
				list.Add(text);
			}
		}
		return list;
	}

	private static bool IsExactProductMatch(List<string> productNames, string[] queryWords)
	{
		for (int i = 0; i < productNames.Count; i++)
		{
			string[] array = Tokenize(productNames[i]);
			if (array.Length == queryWords.Length && WordsEqual(array, queryWords))
			{
				return true;
			}
		}
		return false;
	}

	private static bool AllWordsMatch(List<string> productNames, string[] queryWords)
	{
		List<string> list = new List<string>();
		for (int i = 0; i < productNames.Count; i++)
		{
			string[] array = Tokenize(productNames[i]);
			for (int j = 0; j < array.Length; j++)
			{
				list.Add(array[j]);
			}
		}
		for (int k = 0; k < queryWords.Length; k++)
		{
			bool flag = false;
			for (int l = 0; l < list.Count; l++)
			{
				if (list[l].StartsWith(queryWords[k], StringComparison.CurrentCultureIgnoreCase))
				{
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				return false;
			}
		}
		return true;
	}

	private static bool WordsEqual(string[] left, string[] right)
	{
		for (int i = 0; i < left.Length; i++)
		{
			if (!string.Equals(left[i], right[i], StringComparison.CurrentCultureIgnoreCase))
			{
				return false;
			}
		}
		return true;
	}

	private static string[] Tokenize(string text)
	{
		if (string.IsNullOrWhiteSpace(text))
		{
			return new string[0];
		}
		List<string> list = new List<string>();
		StringBuilder stringBuilder = new StringBuilder();
		char c = '\0';
		foreach (char c2 in text)
		{
			if (!char.IsLetterOrDigit(c2))
			{
				AddCurrentWord(list, stringBuilder);
				c = c2;
				continue;
			}
			if (char.IsUpper(c2) && stringBuilder.Length > 0 && (char.IsLower(c) || char.IsDigit(c)))
			{
				AddCurrentWord(list, stringBuilder);
			}
			stringBuilder.Append(char.ToLowerInvariant(c2));
			c = c2;
		}
		AddCurrentWord(list, stringBuilder);
		return list.ToArray();
	}

	private static void AddCurrentWord(List<string> words, StringBuilder currentWord)
	{
		if (currentWord.Length > 0)
		{
			words.Add(currentWord.ToString());
			currentWord.Length = 0;
		}
	}

	private static Rect GetScreenRect(RectTransform rectTransform, InventoryGui gui)
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: 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_0075: 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_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: 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_00aa: 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_00cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_0102: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)rectTransform == (Object)null)
		{
			return new Rect(0f, 0f, 0f, 0f);
		}
		Canvas val = (((Object)(object)gui.m_crafting != (Object)null) ? ((Component)gui.m_crafting).GetComponentInParent<Canvas>() : null);
		Camera obj = (((Object)(object)val != (Object)null && (int)val.renderMode != 0) ? val.worldCamera : null);
		Vector3[] array = (Vector3[])(object)new Vector3[4];
		rectTransform.GetWorldCorners(array);
		Vector2 val2 = RectTransformUtility.WorldToScreenPoint(obj, array[0]);
		Vector2 val3 = RectTransformUtility.WorldToScreenPoint(obj, array[1]);
		Vector2 val4 = RectTransformUtility.WorldToScreenPoint(obj, array[2]);
		float num = Mathf.Min(val2.x, val4.x);
		float num2 = Mathf.Max(val2.x, val4.x);
		float num3 = Mathf.Min((float)Screen.height - val3.y, (float)Screen.height - val4.y);
		float num4 = Mathf.Max((float)Screen.height - val2.y, (float)Screen.height - val4.y);
		return Rect.MinMaxRect(num, num3, num2, num4);
	}

	private static float GetUiScale(RectTransform area, InventoryGui gui, Rect screenRect)
	{
		//IL_0044: 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_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		Canvas val = (((Object)(object)gui.m_crafting != (Object)null) ? ((Component)gui.m_crafting).GetComponentInParent<Canvas>() : null);
		if ((Object)(object)val != (Object)null && val.scaleFactor > 0f)
		{
			return val.scaleFactor;
		}
		if ((Object)(object)area != (Object)null)
		{
			Rect rect = area.rect;
			if (((Rect)(ref rect)).width > 0f)
			{
				float width = ((Rect)(ref screenRect)).width;
				rect = area.rect;
				return Mathf.Max(0.5f, width / ((Rect)(ref rect)).width);
			}
		}
		return 1f;
	}

	private static void EnsureStyles(float scale)
	{
		//IL_0045: Unknown result type (might be due to invalid IL or missing references)
		//IL_004f: Expected O, but got Unknown
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Expected O, but got Unknown
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a1: Expected O, but got Unknown
		//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e0: Expected O, but got Unknown
		int num = Mathf.Clamp(Mathf.RoundToInt(18f * scale), 12, 28);
		int num2 = Mathf.Clamp(Mathf.RoundToInt(14f * scale), 11, 22);
		if (_buttonStyle == null || num != _buttonFontSize)
		{
			_buttonStyle = new GUIStyle(GUI.skin.button);
			_buttonStyle.alignment = (TextAnchor)4;
			_buttonStyle.fontSize = num;
			_buttonStyle.padding = new RectOffset(1, 1, 1, 1);
			_buttonFontSize = num;
		}
		if (_fieldStyle == null || num2 != _fieldFontSize)
		{
			_fieldStyle = new GUIStyle(GUI.skin.textField);
			_fieldStyle.alignment = (TextAnchor)3;
			_fieldStyle.fontSize = num2;
			_fieldStyle.padding = new RectOffset(Mathf.RoundToInt(7f * scale), Mathf.RoundToInt(4f * scale), 1, 1);
			_fieldFontSize = num2;
		}
		if ((Object)(object)_searchIcon == (Object)null)
		{
			_searchIcon = CreateSearchIcon();
		}
	}

	private static void DrawSearchIcon(Rect buttonRect, float scale)
	{
		//IL_000a: 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_0032: Unknown result type (might be due to invalid IL or missing references)
		float num = 18f * scale;
		GUI.DrawTexture(new Rect(((Rect)(ref buttonRect)).center.x - num * 0.5f, ((Rect)(ref buttonRect)).center.y - num * 0.5f, num, num), (Texture)(object)_searchIcon, (ScaleMode)2, true);
	}

	private static void DrawCloseMark(Rect buttonRect)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		GUI.Label(buttonRect, "×", _buttonStyle);
	}

	private static bool TryGetToolbarRects(InventoryGui gui, out Rect buttonRect, out Rect fieldRect, out float scale)
	{
		//IL_0015: 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_0034: 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_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: 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_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f4: 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_014a: Unknown result type (might be due to invalid IL or missing references)
		//IL_01de: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0204: Unknown result type (might be due to invalid IL or missing references)
		//IL_022a: Unknown result type (might be due to invalid IL or missing references)
		//IL_022f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0199: Unknown result type (might be due to invalid IL or missing references)
		//IL_019e: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d5: Unknown result type (might be due to invalid IL or missing references)
		buttonRect = new Rect(0f, 0f, 0f, 0f);
		fieldRect = new Rect(0f, 0f, 0f, 0f);
		scale = 1f;
		RectTransform crafting = gui.m_crafting;
		Button val = (((Object)(object)gui.m_tabCraft != (Object)null && ((Component)gui.m_tabCraft).gameObject.activeInHierarchy) ? gui.m_tabCraft : gui.m_tabUpgrade);
		RectTransform val2 = (RectTransform)(((Object)(object)val != (Object)null) ? /*isinst with value type is only supported in some contexts*/: null);
		if ((Object)(object)crafting == (Object)null || (Object)(object)val2 == (Object)null || !((Component)val2).gameObject.activeInHierarchy)
		{
			return false;
		}
		Rect screenRect = GetScreenRect(crafting, gui);
		Rect screenRect2 = GetScreenRect(val2, gui);
		if (((Rect)(ref screenRect)).width <= 0f || ((Rect)(ref screenRect2)).width <= 0f || ((Rect)(ref screenRect2)).height <= 0f)
		{
			return false;
		}
		scale = GetUiScale(crafting, gui, screenRect);
		float num = 4f * scale;
		float num2 = 5f * scale;
		float num3 = Mathf.Min(24f * scale, ((Rect)(ref screenRect2)).height);
		float num4 = Mathf.Min(26f * scale, ((Rect)(ref screenRect2)).height);
		float num5 = ((Rect)(ref screenRect)).xMax - num2;
		float num6 = FindLastTabRight(gui, screenRect2, screenRect, scale);
		_usesFallbackRow = num5 - num6 - (num3 + num * 2f) < 72f * scale;
		if (_usesFallbackRow)
		{
			float num7 = ((Rect)(ref screenRect2)).yMax + 2f * scale;
			buttonRect = new Rect(num5 - num3, num7, num3, num4);
			float num8 = ((Rect)(ref screenRect)).xMin + num2;
			float num9 = ((Rect)(ref buttonRect)).xMin - num;
			fieldRect = new Rect(num8, num7, Mathf.Max(0f, num9 - num8), num4);
		}
		else
		{
			float num10 = ((Rect)(ref screenRect2)).center.y - num4 * 0.5f;
			buttonRect = new Rect(num6 + num, num10, num3, num4);
			float num11 = ((Rect)(ref buttonRect)).xMax + num;
			fieldRect = new Rect(num11, num10, Mathf.Max(0f, num5 - num11), num4);
		}
		if (((Rect)(ref buttonRect)).width > 0f)
		{
			return ((Rect)(ref fieldRect)).width > 0f;
		}
		return false;
	}

	private static float FindLastTabRight(InventoryGui gui, Rect referenceTabRect, Rect panelRect, float scale)
	{
		//IL_0094: Unknown result type (might be due to invalid IL or missing references)
		//IL_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bc: 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)
		float num = ((Rect)(ref referenceTabRect)).xMax;
		float num2 = Mathf.Max(((Rect)(ref referenceTabRect)).height * 0.45f, 5f * scale);
		TabSelectables.Clear();
		((Component)gui.m_crafting).GetComponentsInChildren<Selectable>(true, TabSelectables);
		for (int i = 0; i < TabSelectables.Count; i++)
		{
			Selectable val = TabSelectables[i];
			if ((Object)(object)val == (Object)null || !((Behaviour)val).isActiveAndEnabled || !((Component)val).gameObject.activeInHierarchy)
			{
				continue;
			}
			Transform transform = ((Component)val).transform;
			RectTransform val2 = (RectTransform)(object)((transform is RectTransform) ? transform : null);
			if (!((Object)(object)val2 == (Object)null))
			{
				Rect screenRect = GetScreenRect(val2, gui);
				if (!(((Rect)(ref screenRect)).width <= 0f) && !(((Rect)(ref screenRect)).height <= 0f) && !(Mathf.Abs(((Rect)(ref screenRect)).center.y - ((Rect)(ref referenceTabRect)).center.y) > num2) && !(((Rect)(ref screenRect)).width > ((Rect)(ref panelRect)).width * 0.4f) && !(((Rect)(ref screenRect)).xMin < ((Rect)(ref panelRect)).xMin - 2f * scale) && !(((Rect)(ref screenRect)).xMax > ((Rect)(ref panelRect)).xMax + 2f * scale))
				{
					num = Mathf.Max(num, ((Rect)(ref screenRect)).xMax);
				}
			}
		}
		return num;
	}

	private static Texture2D CreateSearchIcon()
	{
		//IL_0043: 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_0144: Unknown result type (might be due to invalid IL or missing references)
		//IL_0149: Unknown result type (might be due to invalid IL or missing references)
		//IL_0154: Unknown result type (might be due to invalid IL or missing references)
		//IL_015b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0162: 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_0172: Expected O, but got Unknown
		//IL_009f: 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_00c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cb: 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_00d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d9: 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_00ef: 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_00f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_011a: 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)
		Color32 val = default(Color32);
		((Color32)(ref val))..ctor(byte.MaxValue, byte.MaxValue, byte.MaxValue, (byte)0);
		Color32 val2 = default(Color32);
		((Color32)(ref val2))..ctor(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue);
		Color32[] array = (Color32[])(object)new Color32[1024];
		for (int i = 0; i < array.Length; i++)
		{
			array[i] = val;
		}
		Vector2 val4 = default(Vector2);
		for (int j = 0; j < 32; j++)
		{
			for (int k = 0; k < 32; k++)
			{
				float num = (float)k - 12f;
				float num2 = (float)j - 13f;
				bool num3 = Mathf.Abs(Mathf.Sqrt(num * num + num2 * num2) - 8f) <= 1.7f;
				Vector2 val3 = new Vector2((float)k, (float)j);
				((Vector2)(ref val4))..ctor(18.5f, 19.5f);
				Vector2 val5 = new Vector2(27.5f, 28.5f) - val4;
				float sqrMagnitude = ((Vector2)(ref val5)).sqrMagnitude;
				float num4 = Mathf.Clamp01(Vector2.Dot(val3 - val4, val5) / sqrMagnitude);
				float num5 = Vector2.Distance(val3, val4 + val5 * num4);
				if (num3 || num5 <= 1.8f)
				{
					array[j * 32 + k] = val2;
				}
			}
		}
		Texture2D val6 = new Texture2D(32, 32, (TextureFormat)4, false)
		{
			name = "SearchOnProduction.SearchIcon",
			filterMode = (FilterMode)1,
			wrapMode = (TextureWrapMode)1
		};
		val6.SetPixels32(array);
		val6.Apply(false, true);
		return val6;
	}

	private static void RefreshRecipeList(InventoryGui gui)
	{
		if ((Object)(object)gui == (Object)null || _refreshInProgress)
		{
			return;
		}
		if (_updateCraftingPanelMethod == null)
		{
			_updateCraftingPanelMethod = AccessTools.Method(typeof(InventoryGui), "UpdateCraftingPanel", new Type[1] { typeof(bool) }, (Type[])null);
		}
		if (_updateCraftingPanelMethod == null)
		{
			WarnRefreshFailure("InventoryGui.UpdateCraftingPanel(Boolean) was not found.");
			return;
		}
		try
		{
			_refreshInProgress = true;
			_updateCraftingPanelMethod.Invoke(gui, new object[1] { false });
		}
		catch (Exception ex)
		{
			WarnRefreshFailure(ex.GetBaseException().Message);
		}
		finally
		{
			_refreshInProgress = false;
		}
	}

	private static void WarnRefreshFailure(string reason)
	{
		if (!_warnedRefreshFailure)
		{
			_warnedRefreshFailure = true;
			if ((Object)(object)SearchOnProductionPlugin.Instance != (Object)null)
			{
				SearchOnProductionPlugin.Log.LogWarning((object)("Could not refresh the crafting list: " + reason));
			}
		}
	}
}
internal static class SearchToolbarLayout
{
	private const float ToolbarHeight = 30f;

	private static readonly HashSet<InventoryGui> ExpandedInventories = new HashSet<InventoryGui>();

	internal static void SetFallback(InventoryGui gui, bool useFallback)
	{
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_0098: 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_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: 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_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)gui == (Object)null || (Object)(object)gui.m_recipeListRoot == (Object)null)
		{
			return;
		}
		if (useFallback)
		{
			if (!ExpandedInventories.Add(gui))
			{
				return;
			}
		}
		else if (!ExpandedInventories.Remove(gui))
		{
			return;
		}
		RectTransform recipeListRoot = gui.m_recipeListRoot;
		for (int i = 0; i < ((Transform)recipeListRoot).childCount; i++)
		{
			Transform child = ((Transform)recipeListRoot).GetChild(i);
			RectTransform val = (RectTransform)(object)((child is RectTransform) ? child : null);
			if ((Object)(object)val != (Object)null)
			{
				val.anchoredPosition += new Vector2(0f, useFallback ? (-30f) : 30f);
			}
		}
		Vector2 sizeDelta = recipeListRoot.sizeDelta;
		float num = (useFallback ? 30f : (-30f));
		recipeListRoot.sizeDelta = new Vector2(sizeDelta.x, sizeDelta.y + num);
	}
}