Decompiled source of Ragnavik UI v1.0.2

plugins/RagnavikUI/RagnavikUI.dll

Decompiled 3 days ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.Events;
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: AssemblyVersion("0.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace RagnavikUI
{
	[BepInPlugin("lostkode.ragnavik.ui", "Ragnavik UI", "1.0.2")]
	public sealed class RagnavikUIPlugin : BaseUnityPlugin
	{
		private static ManualLogSource? uiLog;

		private static RectTransform? trackedInventory;

		private static RectTransform? armor;

		private static RectTransform? coin;

		private static RectTransform? weight;

		private static RectTransform? trash;

		private static readonly Vector3[] originalScales = (Vector3[])(object)new Vector3[4];

		private static float columnCenterX;

		private static bool loggedMissing;

		private static bool routingTrashClick;

		private void Awake()
		{
			uiLog = ((BaseUnityPlugin)this).Logger;
			Harmony.CreateAndPatchAll(typeof(RagnavikUIPlugin), (string)null);
		}

		[HarmonyPatch(typeof(InventoryGui), "Update")]
		[HarmonyPostfix]
		[HarmonyPriority(0)]
		private static void AfterInventoryUpdate(InventoryGui __instance)
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0188: Unknown result type (might be due to invalid IL or missing references)
			//IL_018d: Unknown result type (might be due to invalid IL or missing references)
			//IL_019d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_023a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0249: 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_0267: Unknown result type (might be due to invalid IL or missing references)
			//IL_02a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_02dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_02f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_03ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_03bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_03cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_03dc: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e1: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0334: Unknown result type (might be due to invalid IL or missing references)
			//IL_0339: Unknown result type (might be due to invalid IL or missing references)
			//IL_0343: Unknown result type (might be due to invalid IL or missing references)
			//IL_0352: Unknown result type (might be due to invalid IL or missing references)
			//IL_0362: Unknown result type (might be due to invalid IL or missing references)
			//IL_0367: Unknown result type (might be due to invalid IL or missing references)
			//IL_036c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0373: Unknown result type (might be due to invalid IL or missing references)
			//IL_0378: Unknown result type (might be due to invalid IL or missing references)
			//IL_037a: Unknown result type (might be due to invalid IL or missing references)
			RectTransform component = ((Component)__instance.m_player).GetComponent<RectTransform>();
			if ((Object)(object)component == (Object)null || !((Component)component).gameObject.activeInHierarchy)
			{
				return;
			}
			Rect rect = component.rect;
			if (((Rect)(ref rect)).height <= 0f)
			{
				return;
			}
			if ((Object)(object)trackedInventory != (Object)(object)component || (Object)(object)armor == (Object)null || (Object)(object)coin == (Object)null || (Object)(object)weight == (Object)null || (Object)(object)trash == (Object)null)
			{
				trackedInventory = component;
				armor = FindPanel(component, __instance, "Armor");
				coin = FindPanel(component, __instance, "CoinPocketUI");
				weight = FindPanel(component, __instance, "Weight");
				trash = FindPanel(component, __instance, "Trash");
				if ((Object)(object)armor == (Object)null || (Object)(object)coin == (Object)null || (Object)(object)weight == (Object)null || (Object)(object)trash == (Object)null)
				{
					if (!loggedMissing)
					{
						ManualLogSource? obj = uiLog;
						if (obj != null)
						{
							obj.LogWarning((object)("Four-row layout waiting for panels: " + $"Armor={(Object)(object)armor != (Object)null}, CoinPocketUI={(Object)(object)coin != (Object)null}, " + $"Weight={(Object)(object)weight != (Object)null}, Trash={(Object)(object)trash != (Object)null}"));
						}
						loggedMissing = true;
					}
					return;
				}
				loggedMissing = false;
				originalScales[0] = ((Transform)armor).localScale;
				originalScales[1] = ((Transform)coin).localScale;
				originalScales[2] = ((Transform)weight).localScale;
				originalScales[3] = ((Transform)trash).localScale;
			}
			RectTransform[] array = (RectTransform[])(object)new RectTransform[4] { armor, coin, weight, trash };
			rect = component.rect;
			float num = (((Rect)(ref rect)).height - 16f) / 4f;
			if (num <= 0f)
			{
				return;
			}
			float num2 = 0f;
			float num3 = 0f;
			for (int i = 0; i < array.Length; i++)
			{
				((Transform)array[i]).localScale = originalScales[i];
				Bounds val = PanelBounds(component, array[i]);
				num2 = Mathf.Max(num2, ((Bounds)(ref val)).size.y);
				num3 = Mathf.Max(num3, ((Bounds)(ref val)).size.x);
			}
			float num4 = Mathf.Min(1f, num / Mathf.Max(1f, num2 + 10f));
			rect = component.rect;
			columnCenterX = ((Rect)(ref rect)).xMax + 4f + num3 * num4 * 0.5f;
			for (int j = 0; j < array.Length; j++)
			{
				((Transform)array[j]).localScale = originalScales[j] * num4;
				rect = component.rect;
				float num5 = ((Rect)(ref rect)).yMax - 8f - ((float)j + 0.5f) * num;
				if (j == 2)
				{
					num5 -= 8f;
				}
				if (j == 3)
				{
					num5 -= 16f;
				}
				Bounds val2 = PanelBounds(component, array[j]);
				Vector3 val3 = ((Transform)component).TransformVector(new Vector3(columnCenterX - ((Bounds)(ref val2)).center.x, num5 - ((Bounds)(ref val2)).center.y, 0f));
				RectTransform obj2 = array[j];
				((Transform)obj2).position = ((Transform)obj2).position + val3;
			}
			Transform obj3 = ((Transform)trash).Find("ButtonCanvas");
			RectTransform val4 = (RectTransform)(object)((obj3 is RectTransform) ? obj3 : null);
			if ((Object)(object)val4 != (Object)null)
			{
				Bounds val5 = PanelBounds(component, trash);
				((Transform)val4).position = ((Transform)component).TransformPoint(((Bounds)(ref val5)).center);
				rect = trash.rect;
				val4.sizeDelta = ((Rect)(ref rect)).size;
			}
		}

		[HarmonyPatch(typeof(InventoryGui), "UpdateItemDrag")]
		[HarmonyPrefix]
		[HarmonyPriority(800)]
		private static void BeforeItemDragUpdate(InventoryGui __instance)
		{
			//IL_0066: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			if (routingTrashClick || (Object)(object)trash == (Object)null || !((Component)trash).gameObject.activeInHierarchy || !Input.GetMouseButtonDown(0) || AccessTools.Field(typeof(InventoryGui), "m_dragItem")?.GetValue(__instance) == null)
			{
				return;
			}
			Canvas componentInParent = ((Component)trash).GetComponentInParent<Canvas>();
			Camera val = (((Object)(object)componentInParent != (Object)null && (int)componentInParent.renderMode != 0) ? componentInParent.worldCamera : null);
			if (!RectTransformUtility.RectangleContainsScreenPoint(trash, Vector2.op_Implicit(Input.mousePosition), val))
			{
				return;
			}
			Transform obj = ((Transform)trash).Find("ButtonCanvas");
			Button val2 = ((obj != null) ? ((Component)obj).GetComponent<Button>() : null);
			if ((Object)(object)val2 == (Object)null)
			{
				return;
			}
			routingTrashClick = true;
			try
			{
				ManualLogSource? obj2 = uiLog;
				if (obj2 != null)
				{
					obj2.LogInfo((object)"Routing held-item click through TrashItems button");
				}
				((UnityEvent)val2.onClick).Invoke();
			}
			finally
			{
				routingTrashClick = false;
			}
		}

		private static RectTransform? FindPanel(RectTransform playerInventory, InventoryGui gui, string panelName)
		{
			RectTransform[] componentsInChildren = ((Component)playerInventory).GetComponentsInChildren<RectTransform>(true);
			foreach (RectTransform val in componentsInChildren)
			{
				if (((Object)val).name == panelName)
				{
					return val;
				}
			}
			componentsInChildren = ((Component)gui).GetComponentsInChildren<RectTransform>(true);
			foreach (RectTransform val2 in componentsInChildren)
			{
				if (((Object)val2).name == panelName)
				{
					return val2;
				}
			}
			return null;
		}

		private static Bounds PanelBounds(RectTransform inventory, RectTransform panel)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: 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)
			Vector3[] array = (Vector3[])(object)new Vector3[4];
			panel.GetWorldCorners(array);
			Bounds result = default(Bounds);
			((Bounds)(ref result))..ctor(((Transform)inventory).InverseTransformPoint(array[0]), Vector3.zero);
			for (int i = 1; i < array.Length; i++)
			{
				((Bounds)(ref result)).Encapsulate(((Transform)inventory).InverseTransformPoint(array[i]));
			}
			return result;
		}
	}
}