Decompiled source of ProductSorter v1.3.0

ProductSorter.dll

Decompiled a month 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 HarmonyLib;
using Il2CppScheduleOne;
using Il2CppScheduleOne.ItemFramework;
using Il2CppScheduleOne.Product;
using Il2CppScheduleOne.StationFramework;
using Il2CppScheduleOne.UI.Phone.ProductManagerApp;
using Il2CppSystem.Collections;
using Il2CppSystem.Collections.Generic;
using MelonLoader;
using ProductSorter;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(global::ProductSorter.ProductSorter), "ProductSorter", "1.3", "Pewoh", null)]
[assembly: MelonColor]
[assembly: MelonGame("TVGS", "Schedule I")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("ProductSorter")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("ProductSorter")]
[assembly: AssemblyTitle("ProductSorter")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace ProductSorter;

public static class BuildInfo
{
	public const string Name = "ProductSorter";

	public const string Description = "ProductSorter is a simple and intuitive mod that adds sorting options to the Product Manager interface. Quickly sort your products by price & addictiveness";

	public const string Author = "Pewoh";

	public const string Company = null;

	public const string Version = "1.3";

	public const string DownloadLink = null;
}
public class ProductSorter : MelonMod
{
	[HarmonyPatch(typeof(ProductManager), "Awake")]
	public static class ProductManager_Awake_Patch
	{
		public static void Postfix(ProductManager __instance)
		{
			productManager = __instance;
			MelonLogger.Msg("ProductManager reference obtained");
		}
	}

	[HarmonyPatch(typeof(ProductManagerApp), "Awake")]
	public static class ProductManagerApp_Awake_Patch
	{
		public static void Prefix()
		{
			ResetState();
		}
	}

	[HarmonyPatch(typeof(ProductManagerApp), "SetOpen")]
	public static class ProductManagerApp_SetOpen_Patch
	{
		public static bool Prefix(bool open, ProductManagerApp __instance)
		{
			if ((Object)(object)__instance == (Object)null || ((Object)((Component)__instance).gameObject).name != "ProductManagerApp")
			{
				return true;
			}
			if (open)
			{
				currentProductManagerApp = __instance;
				CleanupUI();
				CaptureOriginalOrder();
				AddUiButton();
				AddWeedFilterDropdown();
				AddSearchField();
			}
			else
			{
				RestoreOriginalOrder();
				CleanupUI();
				ResetState();
			}
			return true;
		}
	}

	private static ProductManagerApp currentProductManagerApp = null;

	private static GameObject sortContainer = null;

	private static GameObject weedFilterContainer = null;

	private static string currentSortType = null;

	private static string currentWeedFilter = null;

	private static Dictionary<string, Dictionary<string, int>> originalOrderByCategory = new Dictionary<string, Dictionary<string, int>>();

	private static GameObject searchContainer = null;

	private static string currentSearchText = null;

	private static InputField searchInputField = null;

	private static readonly string[] weedTypes = new string[5] { "All Types", "OG Kush", "Sour Diesel", "Green Crack", "Granddaddy Purple" };

	private static ProductManager productManager;

	private static bool shortcutsDisabled = false;

	private static bool wasFocused = false;

	private static bool hasLoggedUpdateError = false;

	private static bool isTyping = false;

	private static Image searchFieldBackground = null;

	private static Text searchFieldPlaceholder = null;

	private static float originalBorderWidth = 1f;

	public override void OnInitializeMelon()
	{
		MelonLogger.Msg("ProductSorter initialized");
		productManager = Object.FindObjectOfType<ProductManager>();
		if (!((Object)(object)productManager != (Object)null))
		{
		}
	}

	private static Transform FindChildByPath(Transform parent, string path)
	{
		Transform val = parent;
		string[] array = path.Split('/');
		foreach (string text in array)
		{
			val = ((val != null) ? val.Find(text) : null);
			if ((Object)(object)val == (Object)null)
			{
				return null;
			}
		}
		return val;
	}

	private static Transform FindNamedChild(Transform parent, string name)
	{
		for (int i = 0; i < parent.childCount; i++)
		{
			Transform child = parent.GetChild(i);
			if (((Object)child).name == name)
			{
				return child;
			}
		}
		return null;
	}

	private static void CleanupUI()
	{
		if ((Object)(object)sortContainer != (Object)null)
		{
			Object.Destroy((Object)(object)sortContainer);
			sortContainer = null;
		}
		if ((Object)(object)weedFilterContainer != (Object)null)
		{
			Object.Destroy((Object)(object)weedFilterContainer);
			weedFilterContainer = null;
		}
		if ((Object)(object)searchContainer != (Object)null)
		{
			Object.Destroy((Object)(object)searchContainer);
			searchContainer = null;
			searchInputField = null;
		}
	}

	private static void ResetState()
	{
		currentSortType = null;
		currentWeedFilter = null;
		currentSearchText = null;
		currentProductManagerApp = null;
		searchInputField = null;
		originalOrderByCategory.Clear();
	}

	private static void CaptureOriginalOrder()
	{
		if ((Object)(object)currentProductManagerApp == (Object)null)
		{
			return;
		}
		originalOrderByCategory.Clear();
		Transform val = FindChildByPath(((Component)currentProductManagerApp).transform, "Container/Scroll View/Viewport/Content");
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		for (int i = 0; i < val.childCount; i++)
		{
			Transform child = val.GetChild(i);
			if ((Object)(object)child == (Object)null)
			{
				continue;
			}
			Transform val2 = FindNamedChild(child, "Entries");
			if ((Object)(object)val2 == (Object)null)
			{
				continue;
			}
			Dictionary<string, int> dictionary = new Dictionary<string, int>();
			for (int j = 0; j < val2.childCount; j++)
			{
				Transform child2 = val2.GetChild(j);
				ProductEntry val3 = ((child2 != null) ? ((Component)child2).GetComponent<ProductEntry>() : null);
				if ((Object)(object)val3 != (Object)null)
				{
					dictionary[((ItemDefinition)val3.Definition).Name] = j;
				}
			}
			if (dictionary.Count > 0)
			{
				originalOrderByCategory[((Object)child).name] = dictionary;
			}
		}
	}

	private static void RestoreOriginalOrder()
	{
		//IL_01e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ea: Expected O, but got Unknown
		if ((Object)(object)currentProductManagerApp == (Object)null || originalOrderByCategory.Count == 0)
		{
			return;
		}
		Transform val = FindChildByPath(((Component)currentProductManagerApp).transform, "Container/Scroll View/Viewport/Content");
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		for (int i = 0; i < val.childCount; i++)
		{
			Transform child = val.GetChild(i);
			if ((Object)(object)child == (Object)null)
			{
				continue;
			}
			string name = ((Object)child).name;
			if (!originalOrderByCategory.ContainsKey(name))
			{
				continue;
			}
			Transform val2 = FindNamedChild(child, "Entries");
			if ((Object)(object)val2 == (Object)null)
			{
				continue;
			}
			Dictionary<string, int> dictionary = originalOrderByCategory[name];
			Dictionary<int, Transform> dictionary2 = new Dictionary<int, Transform>();
			for (int j = 0; j < val2.childCount; j++)
			{
				Transform child2 = val2.GetChild(j);
				ProductEntry val3 = ((child2 != null) ? ((Component)child2).GetComponent<ProductEntry>() : null);
				if ((Object)(object)val3 != (Object)null && dictionary.ContainsKey(((ItemDefinition)val3.Definition).Name))
				{
					dictionary2[dictionary[((ItemDefinition)val3.Definition).Name]] = child2;
				}
			}
			for (int k = 0; k < dictionary2.Count; k++)
			{
				if (dictionary2.TryGetValue(k, out var value))
				{
					value.SetSiblingIndex(k);
				}
			}
			for (int l = 0; l < val2.childCount; l++)
			{
				((Component)val2.GetChild(l)).gameObject.SetActive(true);
			}
		}
		IEnumerator enumerator = val.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				Transform val4 = (Transform)enumerator.Current;
				Transform val5 = val4.Find("None");
				if ((Object)(object)val5 != (Object)null)
				{
					((Component)val5).gameObject.SetActive(false);
				}
			}
		}
		finally
		{
			if (enumerator is IDisposable disposable)
			{
				disposable.Dispose();
			}
		}
		RectTransform component = ((Component)val).GetComponent<RectTransform>();
		if (!((Object)(object)component != (Object)null))
		{
			return;
		}
		try
		{
			LayoutRebuilder.ForceRebuildLayoutImmediate(component);
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Error rebuilding layout: " + ex.Message);
		}
	}

	private static GameObject CreateUIObject(string name, Transform parent, Vector2 size, Vector2 anchorMin, Vector2 anchorMax, Vector2 pivot, Vector2 position)
	{
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Expected O, but got Unknown
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: 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)
		GameObject val = new GameObject(name);
		val.transform.SetParent(parent, false);
		RectTransform val2 = val.AddComponent<RectTransform>();
		val2.sizeDelta = size;
		val2.anchorMin = anchorMin;
		val2.anchorMax = anchorMax;
		val2.pivot = pivot;
		val2.anchoredPosition = position;
		return val;
	}

	private static Text CreateTextComponent(GameObject obj, string content, int fontSize, TextAnchor alignment, Color color)
	{
		//IL_002a: 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)
		Text val = obj.AddComponent<Text>();
		val.text = content;
		val.font = Resources.GetBuiltinResource<Font>("Arial.ttf");
		val.fontSize = fontSize;
		val.alignment = alignment;
		((Graphic)val).color = color;
		return val;
	}

	public static void AddUiButton()
	{
		//IL_004e: 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_006c: 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_008a: 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)
		//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00df: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ee: 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)
		//IL_0126: Unknown result type (might be due to invalid IL or missing references)
		//IL_0135: Unknown result type (might be due to invalid IL or missing references)
		//IL_0144: Unknown result type (might be due to invalid IL or missing references)
		//IL_0153: 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_0182: Unknown result type (might be due to invalid IL or missing references)
		//IL_01cd: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_0219: Unknown result type (might be due to invalid IL or missing references)
		//IL_0228: Unknown result type (might be due to invalid IL or missing references)
		//IL_0237: Unknown result type (might be due to invalid IL or missing references)
		//IL_0246: Unknown result type (might be due to invalid IL or missing references)
		//IL_0255: Unknown result type (might be due to invalid IL or missing references)
		//IL_0277: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ee: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)currentProductManagerApp == (Object)null)
		{
			return;
		}
		Transform val = FindChildByPath(((Component)currentProductManagerApp).transform, "Container/Topbar");
		if (!((Object)(object)val == (Object)null))
		{
			sortContainer = CreateUIObject("SortContainer", val, new Vector2(260f, 50f), new Vector2(1f, 0.5f), new Vector2(1f, 0.5f), new Vector2(1f, 0.5f), new Vector2(-30f, 0f));
			GameObject obj = CreateUIObject("SortByLabel", sortContainer.transform, new Vector2(80f, 50f), new Vector2(0f, 0.5f), new Vector2(0f, 0.5f), new Vector2(0f, 0.5f), new Vector2(0f, 0f));
			CreateTextComponent(obj, "Sort by:", 18, (TextAnchor)3, Color.white);
			GameObject val2 = CreateUIObject("PriceButton", sortContainer.transform, new Vector2(80f, 40f), new Vector2(0f, 0.5f), new Vector2(0f, 0.5f), new Vector2(0f, 0.5f), new Vector2(90f, 0f));
			((Graphic)val2.AddComponent<Image>()).color = new Color(0.2f, 0.2f, 0.2f);
			((UnityEvent)val2.AddComponent<Button>().onClick).AddListener(UnityAction.op_Implicit((Action)delegate
			{
				OnSortClick("price");
			}));
			GameObject obj2 = CreateUIObject("PriceText", val2.transform, Vector2.zero, Vector2.zero, Vector2.one, Vector2.zero, Vector2.zero);
			CreateTextComponent(obj2, "Price", 16, (TextAnchor)4, Color.white);
			GameObject val3 = CreateUIObject("AddictButton", sortContainer.transform, new Vector2(100f, 40f), new Vector2(0f, 0.5f), new Vector2(0f, 0.5f), new Vector2(0f, 0.5f), new Vector2(180f, 0f));
			((Graphic)val3.AddComponent<Image>()).color = new Color(0.2f, 0.2f, 0.2f);
			((UnityEvent)val3.AddComponent<Button>().onClick).AddListener(UnityAction.op_Implicit((Action)delegate
			{
				OnSortClick("addict");
			}));
			GameObject obj3 = CreateUIObject("AddictText", val3.transform, Vector2.zero, Vector2.zero, Vector2.one, Vector2.zero, Vector2.zero);
			CreateTextComponent(obj3, "Addiction", 16, (TextAnchor)4, Color.white);
		}
	}

	private static void AddWeedFilterDropdown()
	{
		//IL_0094: 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_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0136: Unknown result type (might be due to invalid IL or missing references)
		//IL_014d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0164: Unknown result type (might be due to invalid IL or missing references)
		//IL_017b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0192: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01dd: Expected O, but got Unknown
		if ((Object)(object)currentProductManagerApp == (Object)null)
		{
			return;
		}
		GameObject val = GameObject.Find("DeliveryApp");
		Dropdown val2 = null;
		if ((Object)(object)val != (Object)null)
		{
			Transform val3 = FindChildByPath(val.transform, "Container/Scroll View/Viewport/Content/Dan's Hardware/Contents/Panel/Destination/Dropdown (Legacy)");
			if ((Object)(object)val3 != (Object)null)
			{
				val2 = ((Component)val3).GetComponent<Dropdown>();
			}
		}
		Transform val4 = FindChildByPath(((Component)currentProductManagerApp).transform, "Container/Topbar");
		if (!((Object)(object)val4 == (Object)null))
		{
			GameObject obj = CreateUIObject("BaseWeedLabel", val4, new Vector2(80f, 40f), new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), new Vector2(-340f, 0f));
			CreateTextComponent(obj, "Weed:", 18, (TextAnchor)5, Color.white);
			GameObject val5 = Object.Instantiate<GameObject>(((Component)val2).gameObject);
			((Object)val5).name = "WeedTypeDropdown";
			val5.transform.SetParent(val4, false);
			Dropdown component = val5.GetComponent<Dropdown>();
			RectTransform component2 = val5.GetComponent<RectTransform>();
			component2.anchorMin = new Vector2(0.5f, 0.5f);
			component2.anchorMax = new Vector2(0.5f, 0.5f);
			component2.pivot = new Vector2(0.5f, 0.5f);
			component2.sizeDelta = new Vector2(220f, 40f);
			component2.anchoredPosition = new Vector2(-170f, 0f);
			LayoutRebuilder.ForceRebuildLayoutImmediate(component2);
			component.ClearOptions();
			component.ClearOptions();
			string[] array = weedTypes;
			foreach (string text in array)
			{
				component.options.Add(new OptionData(text));
			}
			component.RefreshShownValue();
			((UnityEvent<int>)(object)component.onValueChanged).AddListener(UnityAction<int>.op_Implicit((Action<int>)OnWeedFilterChanged));
			component.value = 0;
			currentWeedFilter = null;
			weedFilterContainer = val5;
		}
	}

	private static void OnWeedFilterChanged(int index)
	{
		if (!((Object)(object)currentProductManagerApp == (Object)null))
		{
			currentWeedFilter = ((index == 0) ? null : weedTypes[index]);
			ApplyFilters();
		}
	}

	private static void AddSearchField()
	{
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_006a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e1: 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_00ff: 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_0113: 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_01aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_01af: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_024e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0253: Unknown result type (might be due to invalid IL or missing references)
		//IL_0258: Unknown result type (might be due to invalid IL or missing references)
		//IL_0267: Unknown result type (might be due to invalid IL or missing references)
		//IL_026c: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_0300: Unknown result type (might be due to invalid IL or missing references)
		//IL_030f: Unknown result type (might be due to invalid IL or missing references)
		//IL_031e: Unknown result type (might be due to invalid IL or missing references)
		//IL_032d: Unknown result type (might be due to invalid IL or missing references)
		//IL_033c: 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)
		//IL_0228: Unknown result type (might be due to invalid IL or missing references)
		//IL_0374: Unknown result type (might be due to invalid IL or missing references)
		//IL_02d0: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_03ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_03c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_03c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_03ea: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if ((Object)(object)currentProductManagerApp == (Object)null)
			{
				return;
			}
			Transform val = FindChildByPath(((Component)currentProductManagerApp).transform, "Container/Topbar");
			if ((Object)(object)val == (Object)null)
			{
				MelonLogger.Error("Topbar not found for search field");
				return;
			}
			searchContainer = CreateUIObject("SearchContainer", val, new Vector2(150f, 40f), new Vector2(1f, 0.5f), new Vector2(1f, 0.5f), new Vector2(1f, 0.5f), new Vector2(-400f, 0f));
			if ((Object)(object)searchContainer == (Object)null)
			{
				MelonLogger.Error("Failed to create search container");
				return;
			}
			GameObject val2 = CreateUIObject("SearchField", searchContainer.transform, new Vector2(150f, 40f), new Vector2(0f, 0.5f), new Vector2(1f, 0.5f), new Vector2(0.5f, 0.5f), Vector2.zero);
			if ((Object)(object)val2 == (Object)null)
			{
				MelonLogger.Error("Failed to create search field");
				return;
			}
			Image val3 = val2.AddComponent<Image>();
			if ((Object)(object)val3 != (Object)null)
			{
				((Graphic)val3).color = new Color(1f, 1f, 1f, 0.9f);
			}
			searchInputField = val2.AddComponent<InputField>();
			if ((Object)(object)searchInputField == (Object)null)
			{
				MelonLogger.Error("Failed to add InputField component");
				return;
			}
			GameObject val4 = CreateUIObject("Placeholder", val2.transform, Vector2.zero, Vector2.zero, Vector2.one, new Vector2(0f, 0.5f), Vector2.zero);
			if ((Object)(object)val4 != (Object)null)
			{
				Text val5 = CreateTextComponent(val4, "Search by name...", 20, (TextAnchor)3, Color.gray);
				if ((Object)(object)val5 != (Object)null)
				{
					RectTransform component = val4.GetComponent<RectTransform>();
					if ((Object)(object)component != (Object)null)
					{
						component.offsetMin = new Vector2(10f, 0f);
					}
					searchInputField.placeholder = (Graphic)(object)val5;
				}
			}
			GameObject val6 = CreateUIObject("Text", val2.transform, Vector2.zero, Vector2.zero, Vector2.one, new Vector2(0f, 0.5f), Vector2.zero);
			if ((Object)(object)val6 != (Object)null)
			{
				Text val7 = CreateTextComponent(val6, "", 20, (TextAnchor)3, Color.black);
				if ((Object)(object)val7 != (Object)null)
				{
					RectTransform component2 = val6.GetComponent<RectTransform>();
					if ((Object)(object)component2 != (Object)null)
					{
						component2.offsetMin = new Vector2(10f, 0f);
					}
					searchInputField.textComponent = val7;
				}
			}
			GameObject val8 = CreateUIObject("ClearButton", val2.transform, new Vector2(30f, 30f), new Vector2(1f, 0.5f), new Vector2(1f, 0.5f), new Vector2(1f, 0.5f), new Vector2(-10f, 0f));
			if ((Object)(object)val8 != (Object)null)
			{
				Image val9 = val8.AddComponent<Image>();
				if ((Object)(object)val9 != (Object)null)
				{
					((Graphic)val9).color = Color.black;
				}
				Button val10 = val8.AddComponent<Button>();
				if ((Object)(object)val10 != (Object)null)
				{
					GameObject val11 = CreateUIObject("ClearText", val8.transform, Vector2.zero, Vector2.zero, Vector2.one, new Vector2(0.5f, 0.5f), Vector2.zero);
					if ((Object)(object)val11 != (Object)null)
					{
						Text val12 = CreateTextComponent(val11, "X", 24, (TextAnchor)4, Color.white);
						if ((Object)(object)val12 != (Object)null)
						{
							val12.fontStyle = (FontStyle)1;
						}
					}
					((UnityEvent)val10.onClick).AddListener(UnityAction.op_Implicit((Action)delegate
					{
						if ((Object)(object)searchInputField != (Object)null)
						{
							searchInputField.text = "";
							currentSearchText = "";
							ApplyFilters();
						}
					}));
				}
			}
			if ((Object)(object)searchInputField != (Object)null)
			{
				((UnityEvent<string>)(object)searchInputField.onValueChanged).AddListener(UnityAction<string>.op_Implicit((Action<string>)OnSearchTextChanged));
				((UnityEvent<string>)(object)searchInputField.onEndEdit).AddListener(UnityAction<string>.op_Implicit((Action<string>)delegate
				{
					OnSearchFieldUnfocused();
				}));
			}
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Error creating search field: " + ex.Message + "\n" + ex.StackTrace);
		}
	}

	public override void OnUpdate()
	{
		try
		{
			if ((Object)(object)searchInputField != (Object)null)
			{
				EventSystem current = EventSystem.current;
				bool flag = (Object)(object)current != (Object)null && (Object)(object)current.currentSelectedGameObject != (Object)null && (Object)(object)current.currentSelectedGameObject == (Object)(object)((Component)searchInputField).gameObject;
				if (!wasFocused && flag)
				{
					OnSearchFieldFocused();
				}
				else if (wasFocused && !flag)
				{
					OnSearchFieldUnfocused();
				}
				if (flag && shortcutsDisabled)
				{
					GameInput.IsTyping = true;
				}
				wasFocused = flag;
			}
		}
		catch (Exception ex)
		{
			if (!hasLoggedUpdateError)
			{
				MelonLogger.Error("Error in OnUpdate: " + ex.Message);
				hasLoggedUpdateError = true;
			}
		}
	}

	private static void OnSearchTextChanged(string value)
	{
		currentSearchText = value.ToLower();
		ApplyFilters();
	}

	private static void OnSearchFieldFocused()
	{
		//IL_010f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: 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)
		DisableGameShortcuts();
		if (!((Object)(object)searchInputField != (Object)null))
		{
			return;
		}
		if ((Object)(object)searchFieldBackground == (Object)null)
		{
			searchFieldBackground = ((Component)searchInputField).GetComponent<Image>();
		}
		if ((Object)(object)searchFieldPlaceholder == (Object)null && (Object)(object)searchInputField.placeholder != (Object)null)
		{
			Graphic placeholder = searchInputField.placeholder;
			searchFieldPlaceholder = (Text)(object)((placeholder is Text) ? placeholder : null);
		}
		if ((Object)(object)searchFieldBackground != (Object)null)
		{
			Outline val = ((Component)searchInputField).gameObject.GetComponent<Outline>();
			if ((Object)(object)val == (Object)null)
			{
				val = ((Component)searchInputField).gameObject.AddComponent<Outline>();
			}
			if ((Object)(object)val != (Object)null)
			{
				((Shadow)val).effectColor = Color.blue;
				((Shadow)val).effectDistance = new Vector2(2f, 2f);
				((Behaviour)val).enabled = true;
			}
			((Graphic)searchFieldBackground).color = new Color(0.9f, 0.9f, 1f, 1f);
		}
		if ((Object)(object)searchFieldPlaceholder != (Object)null)
		{
			((Behaviour)searchFieldPlaceholder).enabled = false;
		}
	}

	private static void OnSearchFieldUnfocused()
	{
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		RestoreGameShortcuts();
		if (!((Object)(object)searchInputField != (Object)null))
		{
			return;
		}
		if ((Object)(object)searchFieldBackground != (Object)null)
		{
			Outline component = ((Component)searchInputField).gameObject.GetComponent<Outline>();
			if ((Object)(object)component != (Object)null)
			{
				((Behaviour)component).enabled = false;
			}
			((Graphic)searchFieldBackground).color = new Color(1f, 1f, 1f, 0.9f);
		}
		if ((Object)(object)searchFieldPlaceholder != (Object)null && string.IsNullOrEmpty(searchInputField.text))
		{
			((Behaviour)searchFieldPlaceholder).enabled = true;
		}
	}

	private static void DisableGameShortcuts()
	{
		if (shortcutsDisabled)
		{
			return;
		}
		try
		{
			GameInput.IsTyping = true;
			shortcutsDisabled = true;
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Failed to disable game shortcuts: " + ex.Message);
		}
	}

	private static void RestoreGameShortcuts()
	{
		if (!shortcutsDisabled)
		{
			return;
		}
		try
		{
			GameInput.IsTyping = false;
			shortcutsDisabled = false;
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Failed to restore game shortcuts: " + ex.Message);
			shortcutsDisabled = false;
		}
	}

	private static void ApplyFilters()
	{
		if ((Object)(object)currentProductManagerApp == (Object)null)
		{
			return;
		}
		Transform val = FindChildByPath(((Component)currentProductManagerApp).transform, "Container/Scroll View/Viewport/Content");
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		Transform val2 = val.Find("Outline");
		if ((Object)(object)val2 != (Object)null)
		{
			((Component)val2).gameObject.SetActive(false);
		}
		for (int i = 0; i < val.childCount; i++)
		{
			Transform child = val.GetChild(i);
			if ((Object)(object)child == (Object)null || ((Object)child).name == "Outline")
			{
				continue;
			}
			Transform val3 = FindNamedChild(child, "Entries");
			if ((Object)(object)val3 == (Object)null)
			{
				continue;
			}
			bool flag = false;
			for (int j = 0; j < val3.childCount; j++)
			{
				Transform child2 = val3.GetChild(j);
				ProductEntry val4 = ((child2 != null) ? ((Component)child2).GetComponent<ProductEntry>() : null);
				if (!((Object)(object)val4 == (Object)null))
				{
					bool flag2 = string.IsNullOrEmpty(currentWeedFilter) || FindBaseWeedTypeRecursive(val4.Definition, currentWeedFilter);
					bool flag3 = string.IsNullOrEmpty(currentSearchText) || ((ItemDefinition)val4.Definition).Name.ToLower().Contains(currentSearchText);
					bool flag4 = flag2 && flag3;
					((Component)child2).gameObject.SetActive(flag4);
					if (flag4)
					{
						flag = true;
					}
				}
			}
			Transform val5 = child.Find("None");
			if ((Object)(object)val5 != (Object)null)
			{
				((Component)val5).gameObject.SetActive(!flag && (!string.IsNullOrEmpty(currentWeedFilter) || !string.IsNullOrEmpty(currentSearchText)));
			}
		}
		RectTransform component = ((Component)val).GetComponent<RectTransform>();
		if (!((Object)(object)component != (Object)null))
		{
			return;
		}
		try
		{
			LayoutRebuilder.ForceRebuildLayoutImmediate(component);
		}
		catch (Exception ex)
		{
			MelonLogger.Error("Error rebuilding layout: " + ex.Message);
		}
	}

	private static void ApplyFilter(string weedType)
	{
		currentWeedFilter = weedType;
		ApplyFilters();
	}

	private static bool FindBaseWeedTypeRecursive(ProductDefinition definition, string weedType, HashSet<string> visitedProductIds = null, int depth = 0)
	{
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ef: Invalid comparison between Unknown and I4
		if (depth > 10 || (Object)(object)definition == (Object)null)
		{
			return false;
		}
		if (string.IsNullOrEmpty(weedType))
		{
			return true;
		}
		if (visitedProductIds == null)
		{
			visitedProductIds = new HashSet<string>();
		}
		if (visitedProductIds.Contains(((ItemDefinition)definition).ID))
		{
			return false;
		}
		visitedProductIds.Add(((ItemDefinition)definition).ID);
		if (((ItemDefinition)definition).Name.Contains(weedType))
		{
			return true;
		}
		if (definition.Recipes == null || definition.Recipes.Count == 0)
		{
			return false;
		}
		Enumerator<StationRecipe> enumerator = definition.Recipes.GetEnumerator();
		while (enumerator.MoveNext())
		{
			StationRecipe current = enumerator.Current;
			Enumerator<IngredientQuantity> enumerator2 = current.Ingredients.GetEnumerator();
			while (enumerator2.MoveNext())
			{
				IngredientQuantity current2 = enumerator2.Current;
				ItemDefinition item = current2.Item;
				if (item == null || (int)item.Category > 0)
				{
					continue;
				}
				if (current2.Item.Name.Contains(weedType))
				{
					return true;
				}
				try
				{
					if (!((Object)(object)productManager != (Object)null))
					{
						continue;
					}
					ProductDefinition val = null;
					Enumerator<ProductDefinition> enumerator3 = productManager.AllProducts.GetEnumerator();
					while (enumerator3.MoveNext())
					{
						ProductDefinition current3 = enumerator3.Current;
						if (((ItemDefinition)current3).ID == current2.Item.ID)
						{
							val = current3;
							break;
						}
					}
					if ((Object)(object)val != (Object)null && FindBaseWeedTypeRecursive(val, weedType, visitedProductIds, depth + 1))
					{
						return true;
					}
				}
				catch (Exception ex)
				{
					MelonLogger.Error("Error in recipe checking: " + ex.Message);
				}
			}
		}
		return false;
	}

	public static void OnSortClick(string sortType)
	{
		if (currentSortType == sortType)
		{
			RestoreOriginalOrder();
			currentSortType = null;
			if ((Object)(object)weedFilterContainer != (Object)null)
			{
				Dropdown component = weedFilterContainer.GetComponent<Dropdown>();
				if ((Object)(object)component != (Object)null)
				{
					component.value = 0;
					component.RefreshShownValue();
					currentWeedFilter = null;
				}
			}
			ResetButtonTextStyles();
		}
		else
		{
			if ((Object)(object)currentProductManagerApp == (Object)null)
			{
				return;
			}
			Transform val = FindChildByPath(((Component)currentProductManagerApp).transform, "Container/Scroll View/Viewport/Content");
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			for (int i = 0; i < val.childCount; i++)
			{
				Transform child = val.GetChild(i);
				if ((Object)(object)child == (Object)null)
				{
					continue;
				}
				Transform val2 = FindNamedChild(child, "Entries");
				if ((Object)(object)val2 == (Object)null)
				{
					continue;
				}
				List<Transform> list = new List<Transform>();
				for (int j = 0; j < val2.childCount; j++)
				{
					Transform child2 = val2.GetChild(j);
					if (!((Object)(object)child2 == (Object)null))
					{
						ProductEntry component2 = ((Component)child2).GetComponent<ProductEntry>();
						if ((Object)(object)component2 != (Object)null)
						{
							list.Add(child2);
						}
					}
				}
				if (list.Count == 0)
				{
					continue;
				}
				IOrderedEnumerable<Transform> orderedEnumerable = ((!(sortType == "price")) ? list.OrderByDescending(delegate(Transform x)
				{
					ProductEntry component4 = ((Component)x).GetComponent<ProductEntry>();
					return (component4 != null) ? component4.Definition.GetAddictiveness() : 0f;
				}) : list.OrderByDescending(delegate(Transform x)
				{
					ProductEntry component3 = ((Component)x).GetComponent<ProductEntry>();
					return (component3 != null) ? component3.Definition.MarketValue : 0f;
				}));
				foreach (Transform item in orderedEnumerable)
				{
					item.SetAsLastSibling();
				}
			}
			currentSortType = sortType;
			UpdateButtonTextStyles();
		}
	}

	private static void UpdateButtonTextStyles()
	{
		if ((Object)(object)sortContainer == (Object)null)
		{
			return;
		}
		ResetButtonTextStyles();
		string text = ((currentSortType == "price") ? "PriceButton" : "AddictButton");
		Transform val = sortContainer.transform.Find(text);
		if ((Object)(object)val != (Object)null)
		{
			Transform val2 = val.Find((currentSortType == "price") ? "PriceText" : "AddictText");
			Text val3 = ((val2 != null) ? ((Component)val2).GetComponent<Text>() : null);
			if ((Object)(object)val3 != (Object)null)
			{
				val3.fontStyle = (FontStyle)1;
				val3.fontSize += 3;
			}
		}
	}

	private static void ResetButtonTextStyles()
	{
		if ((Object)(object)sortContainer == (Object)null)
		{
			return;
		}
		Transform val = sortContainer.transform.Find("PriceButton");
		if ((Object)(object)val != (Object)null)
		{
			Transform obj = val.Find("PriceText");
			Text val2 = ((obj != null) ? ((Component)obj).GetComponent<Text>() : null);
			if ((Object)(object)val2 != (Object)null)
			{
				val2.fontStyle = (FontStyle)0;
				val2.fontSize = 16;
			}
		}
		Transform val3 = sortContainer.transform.Find("AddictButton");
		if ((Object)(object)val3 != (Object)null)
		{
			Transform obj2 = val3.Find("AddictText");
			Text val4 = ((obj2 != null) ? ((Component)obj2).GetComponent<Text>() : null);
			if ((Object)(object)val4 != (Object)null)
			{
				val4.fontStyle = (FontStyle)0;
				val4.fontSize = 16;
			}
		}
	}
}