Decompiled source of UnlocksSearch v1.0.0

UnlocksSearch.dll

Decompiled 15 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using Assets.Scripts.Saves___Serialization.Progression.Achievements;
using BepInEx;
using BepInEx.Core.Logging.Interpolation;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;
using HarmonyLib;
using Il2CppInterop.Runtime.InteropTypes;
using Il2CppInterop.Runtime.InteropTypes.Arrays;
using TMPro;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.SceneManagement;
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: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("UnlocksSearch")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Adds a search bar to the Unlocks screen to filter on keywords for unlocks")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("UnlocksSearch")]
[assembly: AssemblyTitle("UnlocksSearch")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace UnlocksSearch;

internal static class Constants
{
	public const string MODNAME = "UnlocksSearch";

	public const string AUTHOR = "ZeusesNeckMeat";

	public const string GUID = "ZeusesNeckMeat_UnlocksSearch";

	public const string VERSION = "1.0.0";

	public const string INPUT_FIELD_NAME = "UnlocksSearchInput";

	public const string INPUT_FIELD_PLACEHOLDER_TEXT = "Search...";
}
[HarmonyPatch(typeof(TMP_InputField), "OnEnable")]
internal class Patches
{
	[HarmonyPostfix]
	public static void TMP_InputField_OnEnable_Postfix(TMP_InputField __instance)
	{
		if (!(((Object)((Component)__instance).gameObject).name != "UnlocksSearchInput"))
		{
			UnlocksSearch.Logger.LogDebug((object)"TMP_InputField.OnEnable called for search input field. Setting placeholder text.");
			__instance.text = string.Empty;
			Graphic placeholder = __instance.placeholder;
			TextMeshProUGUI val = (TextMeshProUGUI)(object)((placeholder is TextMeshProUGUI) ? placeholder : null);
			if ((Object)(object)val != (Object)null)
			{
				((TMP_Text)val).text = "Search...";
			}
			if (((Component)__instance).gameObject.activeSelf)
			{
				__instance.SendOnSubmit();
			}
		}
	}
}
[HarmonyPatch(typeof(MyButtonTabs), "Select")]
internal class MyButtonTabs_Select_Patch
{
	[HarmonyPostfix]
	public static void MyButtonTabs_Select_Postfix(MyButtonTabs __instance)
	{
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_007a: Expected O, but got Unknown
		if ((Object)(object)__instance == (Object)null || ((Il2CppObjectBase)__instance).WasCollected || (Object)(object)((Component)__instance).gameObject == (Object)null || ((Il2CppObjectBase)((Component)__instance).gameObject).WasCollected)
		{
			return;
		}
		Window activeWindow = WindowManager.activeWindow;
		if (((Object)((Component)activeWindow).gameObject).name != "W_Unlocks")
		{
			return;
		}
		ManualLogSource logger = UnlocksSearch.Logger;
		bool flag = default(bool);
		BepInExDebugLogInterpolatedStringHandler val = new BepInExDebugLogInterpolatedStringHandler(36, 1, ref flag);
		if (flag)
		{
			((BepInExLogInterpolatedStringHandler)val).AppendLiteral("MyButtonTabs.Select called for tab: ");
			((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(((Object)((Component)__instance).gameObject).name);
		}
		logger.LogDebug(val);
		Transform obj = ((Component)activeWindow).transform.Find("Header/Header/UnlocksSearchInput");
		TMP_InputField val2 = ((obj != null) ? ((Component)obj).GetComponent<TMP_InputField>() : null);
		if (((Object)((Component)__instance).gameObject).name != "B_Items")
		{
			if (val2 != null)
			{
				GameObject gameObject = ((Component)val2).gameObject;
				if (gameObject != null)
				{
					gameObject.SetActive(false);
				}
			}
			return;
		}
		UnlocksSearch.Logger.LogDebug((object)"B_Items tab selected. Activating search input field.");
		if (val2 != null)
		{
			GameObject gameObject2 = ((Component)val2).gameObject;
			if (gameObject2 != null)
			{
				gameObject2.SetActive(true);
			}
		}
	}
}
internal static class SearchInputFactory
{
	private const string _PARENT_PATH = "Header/Header";

	private const string _REFERENCE_TITLE_PATH = "T_Title";

	private const string _UNLOCK_CONTAINERS_PATH = "WindowLayers/Content/ScrollRect/Content/";

	private static GameObject _inputFieldPrefab;

	public static void CreateSearchInput(Transform unlocksTransform)
	{
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_0069: Expected O, but got Unknown
		//IL_0119: Unknown result type (might be due to invalid IL or missing references)
		//IL_0123: Expected O, but got Unknown
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0161: Unknown result type (might be due to invalid IL or missing references)
		//IL_016d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0179: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c3: Expected O, but got Unknown
		//IL_01e6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f3: 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_0230: 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_00de: Expected O, but got Unknown
		if ((Object)(object)_inputFieldPrefab != (Object)null && !((Il2CppObjectBase)_inputFieldPrefab).WasCollected)
		{
			UnlocksSearch.Logger.LogInfo((object)"Search input already exists and is valid. Skipping creation.");
			return;
		}
		Transform val = unlocksTransform.Find("Header/Header");
		bool flag = default(bool);
		if (!Object.op_Implicit((Object)(object)val))
		{
			ManualLogSource logger = UnlocksSearch.Logger;
			BepInExErrorLogInterpolatedStringHandler val2 = new BepInExErrorLogInterpolatedStringHandler(74, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("Failed to find parent header transform at path '");
				((BepInExLogInterpolatedStringHandler)val2).AppendFormatted<string>("Header/Header");
				((BepInExLogInterpolatedStringHandler)val2).AppendLiteral("' under unlocks transform.");
			}
			logger.LogError(val2);
			return;
		}
		Transform obj = val.Find("T_Title");
		TextMeshProUGUI val3 = ((obj != null) ? ((Component)obj).GetComponent<TextMeshProUGUI>() : null);
		if (!Object.op_Implicit((Object)(object)val3))
		{
			ManualLogSource logger2 = UnlocksSearch.Logger;
			BepInExInfoLogInterpolatedStringHandler val4 = new BepInExInfoLogInterpolatedStringHandler(65, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val4).AppendLiteral("Failed to find reference text at path '");
				((BepInExLogInterpolatedStringHandler)val4).AppendFormatted<string>("T_Title");
				((BepInExLogInterpolatedStringHandler)val4).AppendLiteral("' under unlocks transform.");
			}
			logger2.LogInfo(val4);
		}
		_inputFieldPrefab = new GameObject("UnlocksSearchInput");
		_inputFieldPrefab.transform.SetParent(val, false);
		RectTransform val5 = _inputFieldPrefab.AddComponent<RectTransform>();
		val5.anchorMin = new Vector2(0.4f, 0.25f);
		val5.anchorMax = new Vector2(0.7f, 0.75f);
		val5.anchoredPosition = Vector2.zero;
		val5.sizeDelta = Vector2.zero;
		Image val6 = _inputFieldPrefab.AddComponent<Image>();
		((Graphic)val6).color = new Color(0.1f, 0.1f, 0.1f, 0.9f);
		val6.type = (Type)1;
		GameObject val7 = new GameObject("Border");
		val7.transform.SetParent(_inputFieldPrefab.transform, false);
		RectTransform val8 = val7.AddComponent<RectTransform>();
		val8.anchorMin = Vector2.zero;
		val8.anchorMax = Vector2.one;
		val8.sizeDelta = new Vector2(2f, 2f);
		((Graphic)val7.AddComponent<Image>()).color = new Color(0.3f, 0.3f, 0.3f, 0.8f);
		val7.transform.SetAsFirstSibling();
		TMP_InputField val9 = _inputFieldPrefab.AddComponent<TMP_InputField>();
		((Object)val9).SetName("UnlocksSearchInput");
		val9.textComponent = (TMP_Text)(object)CreateInputText(_inputFieldPrefab.transform, val3);
		val9.placeholder = (Graphic)(object)CreatePlaceholder(_inputFieldPrefab.transform, val3);
		val9.text = "";
		val9.characterLimit = 50;
		((UnityEvent<string>)(object)val9.onSubmit).AddListener(UnityAction<string>.op_Implicit((Action<string>)delegate(string input)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Expected O, but got Unknown
			ManualLogSource logger3 = UnlocksSearch.Logger;
			bool flag2 = default(bool);
			BepInExInfoLogInterpolatedStringHandler val10 = new BepInExInfoLogInterpolatedStringHandler(26, 1, ref flag2);
			if (flag2)
			{
				((BepInExLogInterpolatedStringHandler)val10).AppendLiteral("Search input submitted: '");
				((BepInExLogInterpolatedStringHandler)val10).AppendFormatted<string>(input);
				((BepInExLogInterpolatedStringHandler)val10).AppendLiteral("'");
			}
			logger3.LogInfo(val10);
			FilterItems(input);
		}));
	}

	private static void FilterItems(string searchTerm)
	{
		//IL_0228: Unknown result type (might be due to invalid IL or missing references)
		//IL_022f: Expected O, but got Unknown
		//IL_0147: Unknown result type (might be due to invalid IL or missing references)
		//IL_014e: Expected O, but got Unknown
		searchTerm = searchTerm?.Trim();
		Transform val = ((Component)WindowManager.activeWindow).transform.Find("WindowLayers/Content/ScrollRect/Content/");
		Il2CppArrayBase<UnlockContainer> componentsInChildren = ((Component)val).GetComponentsInChildren<UnlockContainer>(true);
		if (string.IsNullOrEmpty(searchTerm))
		{
			for (int i = 0; i < componentsInChildren.Count; i++)
			{
				UnlockContainer val2 = componentsInChildren[i];
				if (!((Object)(object)val2 == (Object)null) && !((Il2CppObjectBase)val2).WasCollected && !((Object)(object)((Component)val2).gameObject == (Object)null) && !((Il2CppObjectBase)((Component)val2).gameObject).WasCollected && !((Object)(object)val2.unlockable == (Object)null) && !((Il2CppObjectBase)val2.unlockable).WasCollected)
				{
					((Component)val2).gameObject.SetActive(true);
				}
			}
		}
		int num = 0;
		int num2 = 0;
		bool flag = default(bool);
		for (int j = 0; j < componentsInChildren.Count; j++)
		{
			UnlockContainer val3 = componentsInChildren[j];
			if ((Object)(object)val3 == (Object)null || ((Il2CppObjectBase)val3).WasCollected || (Object)(object)val3.unlockable == (Object)null || ((Il2CppObjectBase)val3.unlockable).WasCollected)
			{
				continue;
			}
			ItemData val4 = ((Il2CppObjectBase)val3.unlockable).TryCast<ItemData>();
			if ((Object)(object)val4 == (Object)null)
			{
				ManualLogSource logger = UnlocksSearch.Logger;
				BepInExWarningLogInterpolatedStringHandler val5 = new BepInExWarningLogInterpolatedStringHandler(56, 1, ref flag);
				if (flag)
				{
					((BepInExLogInterpolatedStringHandler)val5).AppendLiteral("Unlockable in container '");
					((BepInExLogInterpolatedStringHandler)val5).AppendFormatted<string>(((Object)((Component)val3).gameObject).name);
					((BepInExLogInterpolatedStringHandler)val5).AppendLiteral("' is not an ItemData. Skipping.");
				}
				logger.LogWarning(val5);
				continue;
			}
			string name = ((UnlockableBase)val4).GetName();
			if (name == null || !name.Contains(searchTerm, StringComparison.OrdinalIgnoreCase))
			{
				string shortDescription = val4.GetShortDescription();
				if (shortDescription == null || !shortDescription.Contains(searchTerm, StringComparison.OrdinalIgnoreCase))
				{
					string description = ((UnlockableBase)val4).GetDescription();
					if (description == null || !description.Contains(searchTerm, StringComparison.OrdinalIgnoreCase))
					{
						((Component)val3).gameObject.SetActive(false);
						num2++;
						continue;
					}
				}
			}
			((Component)val3).gameObject.SetActive(true);
			num++;
		}
		ManualLogSource logger2 = UnlocksSearch.Logger;
		BepInExInfoLogInterpolatedStringHandler val6 = new BepInExInfoLogInterpolatedStringHandler(66, 3, ref flag);
		if (flag)
		{
			((BepInExLogInterpolatedStringHandler)val6).AppendLiteral("Total item count: ");
			((BepInExLogInterpolatedStringHandler)val6).AppendFormatted<int>(componentsInChildren.Count);
			((BepInExLogInterpolatedStringHandler)val6).AppendLiteral(" | Total matching search: ");
			((BepInExLogInterpolatedStringHandler)val6).AppendFormatted<int>(num);
			((BepInExLogInterpolatedStringHandler)val6).AppendLiteral(" | Total Deactivated: ");
			((BepInExLogInterpolatedStringHandler)val6).AppendFormatted<int>(num2);
		}
		logger2.LogInfo(val6);
	}

	private static TextMeshProUGUI CreateInputText(Transform parent, TextMeshProUGUI referenceText)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		//IL_002c: 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_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: 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_00cf: Unknown result type (might be due to invalid IL or missing references)
		GameObject val = new GameObject("Text");
		val.transform.SetParent(parent, false);
		RectTransform val2 = val.AddComponent<RectTransform>();
		val2.anchorMin = new Vector2(0f, 0f);
		val2.anchorMax = new Vector2(0.97f, 1f);
		val2.sizeDelta = Vector2.zero;
		TextMeshProUGUI val3 = val.AddComponent<TextMeshProUGUI>();
		if (Object.op_Implicit((Object)(object)referenceText))
		{
			((TMP_Text)val3).font = ((TMP_Text)referenceText).font;
			((TMP_Text)val3).fontSize = 24f;
			((Graphic)val3).color = ((Graphic)referenceText).color;
		}
		else
		{
			((TMP_Text)val3).fontSize = 24f;
			((Graphic)val3).color = Color.white;
		}
		((TMP_Text)val3).alignment = (TextAlignmentOptions)4097;
		((TMP_Text)val3).margin = new Vector4(10f, 0f, 10f, 0f);
		return val3;
	}

	private static TextMeshProUGUI CreatePlaceholder(Transform parent, TextMeshProUGUI referenceText)
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		//IL_002c: 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_004e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: 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)
		GameObject val = new GameObject("Placeholder");
		val.transform.SetParent(parent, false);
		RectTransform val2 = val.AddComponent<RectTransform>();
		val2.anchorMin = new Vector2(0f, 0f);
		val2.anchorMax = new Vector2(0.97f, 1f);
		val2.sizeDelta = Vector2.zero;
		TextMeshProUGUI val3 = val.AddComponent<TextMeshProUGUI>();
		((TMP_Text)val3).text = "Search...";
		if (Object.op_Implicit((Object)(object)referenceText))
		{
			((TMP_Text)val3).font = ((TMP_Text)referenceText).font;
			((TMP_Text)val3).fontSize = 24f;
		}
		else
		{
			((TMP_Text)val3).fontSize = 24f;
		}
		((TMP_Text)val3).alignment = (TextAlignmentOptions)4097;
		((Graphic)val3).color = new Color(0.5f, 0.5f, 0.5f, 0.5f);
		((TMP_Text)val3).fontStyle = (FontStyles)2;
		((TMP_Text)val3).margin = new Vector4(10f, 0f, 10f, 0f);
		return val3;
	}
}
[BepInPlugin("ZeusesNeckMeat_UnlocksSearch", "UnlocksSearch", "1.0.0")]
public class UnlocksSearch : BasePlugin
{
	private const string _OBJECT_NAME_UI = "UI";

	private const string _UNLOCKS_TRANSFORM_PATH = "Tabs/W_Unlocks";

	private static ManualLogSource _logger;

	public static ManualLogSource Logger => _logger;

	public UnlocksSearch()
	{
		_logger = ((BasePlugin)this).Log;
	}

	public override void Load()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		Harmony val = new Harmony("ZeusesNeckMeat_UnlocksSearch");
		val.PatchAll();
		SceneManager.sceneLoaded += UnityAction<Scene, LoadSceneMode>.op_Implicit((Action<Scene, LoadSceneMode>)SceneManager_sceneLoaded);
	}

	private void SceneManager_sceneLoaded(Scene scene, LoadSceneMode mode)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Expected O, but got Unknown
		//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Expected O, but got Unknown
		//IL_0139: Unknown result type (might be due to invalid IL or missing references)
		//IL_0140: Expected O, but got Unknown
		ManualLogSource logger = _logger;
		bool flag = default(bool);
		BepInExInfoLogInterpolatedStringHandler val = new BepInExInfoLogInterpolatedStringHandler(14, 1, ref flag);
		if (flag)
		{
			((BepInExLogInterpolatedStringHandler)val).AppendLiteral("Scene loaded: ");
			((BepInExLogInterpolatedStringHandler)val).AppendFormatted<string>(((Scene)(ref scene)).name);
		}
		logger.LogInfo(val);
		if (((Scene)(ref scene)).name != "MainMenu")
		{
			return;
		}
		Il2CppReferenceArray<GameObject> rootGameObjects = ((Scene)(ref scene)).GetRootGameObjects();
		if (rootGameObjects == null || ((Il2CppArrayBase<GameObject>)(object)rootGameObjects).Count == 0)
		{
			_logger.LogError((object)"Failed to get root game objects.");
			return;
		}
		GameObject val2 = ((IEnumerable<GameObject>)rootGameObjects).FirstOrDefault((Func<GameObject, bool>)((GameObject obj) => string.Equals(((Object)obj).name, "UI", StringComparison.OrdinalIgnoreCase)));
		if ((Object)(object)val2 == (Object)null)
		{
			ManualLogSource logger2 = _logger;
			BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(45, 1, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("Failed to find root game object with name '");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>("UI");
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("'.");
			}
			logger2.LogError(val3);
			return;
		}
		Transform val4 = val2.transform.Find("Tabs/W_Unlocks");
		if (!Object.op_Implicit((Object)(object)val4))
		{
			ManualLogSource logger3 = _logger;
			BepInExErrorLogInterpolatedStringHandler val3 = new BepInExErrorLogInterpolatedStringHandler(45, 2, ref flag);
			if (flag)
			{
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("Failed to find transform at path '");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>("Tabs/W_Unlocks");
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("' under '");
				((BepInExLogInterpolatedStringHandler)val3).AppendFormatted<string>("UI");
				((BepInExLogInterpolatedStringHandler)val3).AppendLiteral("'.");
			}
			logger3.LogError(val3);
		}
		else
		{
			SearchInputFactory.CreateSearchInput(val4);
		}
	}
}