Decompiled source of AugaPatch v0.3.1

AugaPatch.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
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: AssemblyTitle("AugaPatch")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AugaPatch")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("ebd61d21-3e45-467e-992e-8c95ef9be373")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace AugaPatch;

[BepInPlugin("Nakia.augapatch", "AugaPatch", "0.3.1")]
public class Plugin : BaseUnityPlugin
{
	private Harmony _harmony;

	internal static ConfigEntry<int> VisibleRows;

	private bool _scrollInitialized = false;

	private float _scrollInitAt = -1f;

	private ScrollRect _initializedScroll = null;

	private readonly Dictionary<int, GameObject> _tabOriginalPrefabs = new Dictionary<int, GameObject>();

	private readonly Dictionary<int, Component> _tabPrefabOwners = new Dictionary<int, Component>();

	private float _nextPrefabScan;

	private float _nextPrefabErrorLog;

	private bool _prefabConfiguredLogged;

	private void Awake()
	{
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Expected O, but got Unknown
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Expected O, but got Unknown
		VisibleRows = ((BaseUnityPlugin)this).Config.Bind<int>("Inventory", "VisibleRows", 4, new ConfigDescription("Number of visible inventory rows.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(4, 9), Array.Empty<object>()));
		((BaseUnityPlugin)this).Logger.LogInfo((object)("[AugaPatch CONFIG] VisibleRows=" + VisibleRows.Value));
		((BaseUnityPlugin)this).Logger.LogInfo((object)"[AugaPatch] Plugin loaded successfully!");
		_harmony = new Harmony("Nakia.augapatch");
		_harmony.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"[AugaPatch] Harmony patches applied!");
	}

	private void Update()
	{
		try
		{
			ConfigureTabTooltipPrefabs();
		}
		catch (Exception ex)
		{
			if (Time.unscaledTime >= _nextPrefabErrorLog)
			{
				_nextPrefabErrorLog = Time.unscaledTime + 5f;
				((BaseUnityPlugin)this).Logger.LogWarning((object)("[AUGA TAB PREFAB] Error=" + ex));
			}
		}
	}

	private void LateUpdate()
	{
		InventoryGui instance = InventoryGui.instance;
		if ((Object)(object)instance == (Object)null || (Object)(object)instance.m_playerGrid == (Object)null || (Object)(object)instance.m_player == (Object)null || (Object)(object)Player.m_localPlayer == (Object)null)
		{
			_scrollInitialized = false;
			_scrollInitializedReset();
			return;
		}
		Transform val = ((Component)instance.m_playerGrid).transform.Find("Main");
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		ScrollRect component = ((Component)val).GetComponent<ScrollRect>();
		if ((Object)(object)component == (Object)null || (Object)(object)component.content == (Object)null)
		{
			return;
		}
		if ((Object)(object)_initializedScroll != (Object)(object)component)
		{
			_initializedScroll = component;
			_scrollInitialized = false;
			_scrollInitAt = -1f;
		}
		if (_scrollInitialized)
		{
			return;
		}
		if (!((Component)instance).gameObject.activeInHierarchy || !((Component)instance.m_player).gameObject.activeInHierarchy)
		{
			_scrollInitAt = -1f;
			return;
		}
		if (_scrollInitAt < 0f)
		{
			_scrollInitAt = Time.unscaledTime;
			return;
		}
		_scrollInitialized = true;
		((Behaviour)component).enabled = true;
		component.vertical = true;
		component.horizontal = false;
		Transform val2 = ((Transform)instance.m_player).Find("PlayerScroll");
		if ((Object)(object)val2 != (Object)null)
		{
			((Component)val2).gameObject.SetActive(true);
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)("[AUGA SCROLL] Initialized ScrollRect id=" + ((Object)component).GetInstanceID()));
	}

	private void _scrollInitializedReset()
	{
		_initializedScroll = null;
		_scrollInitAt = -1f;
	}

	private void ConfigureTabTooltipPrefabs()
	{
		if (Time.unscaledTime < _nextPrefabScan)
		{
			return;
		}
		_nextPrefabScan = Time.unscaledTime + 0.25f;
		InventoryGui instance = InventoryGui.instance;
		if ((Object)(object)instance == (Object)null || !((Component)instance).gameObject.activeInHierarchy)
		{
			return;
		}
		Type type = null;
		Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
		foreach (Assembly assembly in assemblies)
		{
			type = assembly.GetType("UITooltip", throwOnError: false);
			if (type != null)
			{
				break;
			}
		}
		if (type == null)
		{
			return;
		}
		FieldInfo field = type.GetField("m_tooltipPrefab", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		if (field == null)
		{
			return;
		}
		Component[] componentsInChildren = ((Component)instance).GetComponentsInChildren<Component>(true);
		GameObject val = null;
		Component[] array = componentsInChildren;
		foreach (Component val2 in array)
		{
			if (!((Object)(object)val2 == (Object)null) && type.IsInstanceOfType(val2))
			{
				object? value = field.GetValue(val2);
				GameObject val3 = (GameObject)((value is GameObject) ? value : null);
				if ((Object)(object)val3 != (Object)null && ((Object)val3).name == "InventoryTooltip")
				{
					val = val3;
					break;
				}
			}
		}
		if ((Object)(object)val == (Object)null)
		{
			if (!_prefabConfiguredLogged)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)"[AUGA TAB PREFAB v0.3.0] InventoryTooltip source not found; leaving tabs unchanged");
				_prefabConfiguredLogged = true;
			}
			return;
		}
		int num = 0;
		Component[] array2 = componentsInChildren;
		foreach (Component val4 in array2)
		{
			if (!((Object)(object)val4 == (Object)null) && type.IsInstanceOfType(val4) && IsInventoryTopTab(((Object)val4.gameObject).name))
			{
				int instanceID = ((Object)val4).GetInstanceID();
				object? value2 = field.GetValue(val4);
				GameObject val5 = (GameObject)((value2 is GameObject) ? value2 : null);
				if (!_tabOriginalPrefabs.ContainsKey(instanceID))
				{
					_tabOriginalPrefabs.Add(instanceID, val5);
					_tabPrefabOwners.Add(instanceID, val4);
				}
				if ((Object)(object)val5 != (Object)(object)val)
				{
					field.SetValue(val4, val);
					num++;
				}
			}
		}
	}

	private void RestoreTabTooltipPrefabs()
	{
		foreach (KeyValuePair<int, Component> tabPrefabOwner in _tabPrefabOwners)
		{
			Component value = tabPrefabOwner.Value;
			if (!((Object)(object)value == (Object)null))
			{
				FieldInfo field = ((object)value).GetType().GetField("m_tooltipPrefab", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
				if (field != null && _tabOriginalPrefabs.TryGetValue(tabPrefabOwner.Key, out var value2))
				{
					field.SetValue(value, value2);
				}
			}
		}
		_tabOriginalPrefabs.Clear();
		_tabPrefabOwners.Clear();
	}

	private static bool IsInventoryTopTab(string name)
	{
		int result;
		switch (name)
		{
		default:
			result = ((name == "TabButton_PVP") ? 1 : 0);
			break;
		case "TabButton_PlayerPanel":
		case "TabButton_Crafting":
		case "TabButton_Skills":
		case "TabButton_MessageLog":
			result = 1;
			break;
		}
		return (byte)result != 0;
	}

	private void OnDestroy()
	{
		RestoreTabTooltipPrefabs();
		if (_harmony != null)
		{
			_harmony.UnpatchSelf();
		}
	}
}
[HarmonyPatch(typeof(InventoryGui), "SetInventorySize")]
public static class InventorySizePatch
{
	[HarmonyPrefix]
	private static void Prefix(ref int rows)
	{
		int num = ((Plugin.VisibleRows != null) ? Plugin.VisibleRows.Value : 4);
		if (rows > num)
		{
			rows = num;
		}
	}
}