Decompiled source of GK2QueueCount v0.3.1

BepInEx/plugins/GK2QueueCount/GK2QueueCount.dll

Decompiled a day ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("GK2QueueCount")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+e41c369d8500f1c1abddc1babc2b00989dc329ac")]
[assembly: AssemblyProduct("GK2QueueCount")]
[assembly: AssemblyTitle("GK2QueueCount")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 GK2QueueCount
{
	[BepInPlugin("de.w00dst0ckOo.gk2.queuecount", "GK2 Queue Count", "0.3.0")]
	public class Plugin : BaseUnityPlugin
	{
		private Harmony _harmony;

		private void Awake()
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected O, but got Unknown
			((BaseUnityPlugin)this).Logger.LogInfo((object)"GK2 Queue Count 0.3.0 loading...");
			_harmony = new Harmony("de.w00dst0ckOo.gk2.queuecount");
			_harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"GK2 Queue Count loaded.");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
}
namespace GK2QueueCount.Patches
{
	[HarmonyPatch(typeof(UICraftHintWidget))]
	internal static class CraftHintPatch
	{
		private sealed class WidgetState
		{
			public UIItemCell ResultItem;

			public Transform Icon;

			public TMP_Text Label;

			public int LastCount;

			public bool HasCount;
		}

		private const string QueueLabelName = "GK2QueueCount_QueueLabel";

		private static readonly FieldRef<UICraftHintWidget, UICraftHintWidgetData> ReadData = AccessTools.FieldRefAccess<UICraftHintWidget, UICraftHintWidgetData>("data");

		private static readonly FieldRef<UICraftHintWidget, UIItemCell> ReadResultItem = AccessTools.FieldRefAccess<UICraftHintWidget, UIItemCell>("craftResultItem");

		private static readonly FieldRef<UIItemCell, TextMeshProUGUI> ReadCountLabel = AccessTools.FieldRefAccess<UIItemCell, TextMeshProUGUI>("countLabel");

		private static readonly ConditionalWeakTable<UICraftHintWidget, WidgetState> States = new ConditionalWeakTable<UICraftHintWidget, WidgetState>();

		[HarmonyPostfix]
		[HarmonyPatch("RedrawCraftHint", new Type[] { })]
		private static void RedrawCraftHintPostfix(UICraftHintWidget __instance)
		{
			if ((Object)(object)__instance == (Object)null)
			{
				return;
			}
			UICraftHintWidgetData val = ReadData.Invoke(__instance);
			object obj;
			if (val == null)
			{
				obj = null;
			}
			else
			{
				CraftComponent craftComponent = val.CraftComponent;
				obj = ((craftComponent != null) ? craftComponent.CurrentCraftElement : null);
			}
			if (obj == null)
			{
				return;
			}
			UIItemCell val2 = ReadResultItem.Invoke(__instance);
			if ((Object)(object)val2 == (Object)null)
			{
				return;
			}
			TMP_Text val3 = (TMP_Text)(object)ReadCountLabel.Invoke(val2);
			if ((Object)(object)val3 == (Object)null || (Object)(object)val2.Icon == (Object)null)
			{
				return;
			}
			Transform transform = ((Component)val2.Icon).transform;
			if (!States.TryGetValue(__instance, out var value))
			{
				value = new WidgetState();
				States.Add(__instance, value);
			}
			if ((Object)(object)value.ResultItem != (Object)(object)val2 || (Object)(object)value.Icon != (Object)(object)transform || (Object)(object)value.Label == (Object)null || (Object)(object)value.Label.transform.parent != (Object)(object)transform)
			{
				value.ResultItem = val2;
				value.Icon = transform;
				value.Label = GetOrCreateQueueLabel(val2, val3);
				value.HasCount = false;
			}
			TMP_Text label = value.Label;
			int num = 0;
			CraftElementBase currentCraftElement = val.CraftComponent.CurrentCraftElement;
			foreach (CraftElementBase item in val.CraftComponent.CraftElementsQueue)
			{
				if (item != null && item.CraftId == currentCraftElement.CraftId)
				{
					num += item.Count;
				}
			}
			if (!value.HasCount || value.LastCount != num)
			{
				label.text = $"×{num}";
				value.LastCount = num;
				value.HasCount = true;
			}
			bool flag = num > 0;
			if (((Component)label).gameObject.activeSelf != flag)
			{
				((Component)label).gameObject.SetActive(flag);
			}
		}

		private static TMP_Text GetOrCreateQueueLabel(UIItemCell craftResultItem, TMP_Text originalCountLabel)
		{
			//IL_0058: 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_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0111: 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_013a: Unknown result type (might be due to invalid IL or missing references)
			Transform transform = ((Component)craftResultItem.Icon).transform;
			Transform val = transform.Find("GK2QueueCount_QueueLabel");
			if ((Object)(object)val != (Object)null)
			{
				TMP_Text component = ((Component)val).GetComponent<TMP_Text>();
				if ((Object)(object)component != (Object)null)
				{
					return component;
				}
			}
			GameObject val2 = new GameObject("GK2QueueCount_QueueLabel", new Type[2]
			{
				typeof(RectTransform),
				typeof(TextMeshProUGUI)
			});
			val2.transform.SetParent(transform, false);
			val2.transform.SetAsLastSibling();
			TextMeshProUGUI component2 = val2.GetComponent<TextMeshProUGUI>();
			((TMP_Text)component2).font = originalCountLabel.font;
			((TMP_Text)component2).fontSharedMaterial = originalCountLabel.fontSharedMaterial;
			((TMP_Text)component2).fontSize = originalCountLabel.fontSize;
			((TMP_Text)component2).fontStyle = originalCountLabel.fontStyle;
			((Graphic)component2).color = ((Graphic)originalCountLabel).color;
			((Graphic)component2).raycastTarget = false;
			((TMP_Text)component2).textWrappingMode = (TextWrappingModes)0;
			((TMP_Text)component2).overflowMode = (TextOverflowModes)0;
			((TMP_Text)component2).alignment = (TextAlignmentOptions)257;
			RectTransform rectTransform = ((TMP_Text)component2).rectTransform;
			rectTransform.anchorMin = new Vector2(0f, 1f);
			rectTransform.anchorMax = new Vector2(0f, 1f);
			rectTransform.pivot = new Vector2(0f, 1f);
			rectTransform.anchoredPosition = new Vector2(5f, -5f);
			rectTransform.sizeDelta = new Vector2(50f, 22f);
			return (TMP_Text)(object)component2;
		}
	}
}