Decompiled source of TechTreeNoRecenter v1.0.0

plugins/TechTreeNoRecenter.dll

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

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("TechTreeNoRecenter")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+ae2ec4ab07888185701bf94f463f98dc4d3e4fa3")]
[assembly: AssemblyProduct("TechTreeNoRecenter")]
[assembly: AssemblyTitle("TechTreeNoRecenter")]
[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 TechTreeNoRecenter
{
	[BepInPlugin("com.github.jiesiluo.techtreenorecenter", "Tech Tree No Recenter", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "com.github.jiesiluo.techtreenorecenter";

		public const string Version = "1.0.0";

		internal static ConfigEntry<bool> KeepCardOnScreen;

		internal static ConfigEntry<float> MarginTop;

		internal static ConfigEntry<float> MarginBottom;

		internal static ConfigEntry<float> MarginSides;

		private void Awake()
		{
			KeepCardOnScreen = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "KeepCardOnScreen", true, "After clicking a technology, scroll just far enough that its opened card is fully on screen.");
			MarginTop = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MarginTop", 100f, "Space kept free at the top of the screen (the tech tree's title bar), in UI units.");
			MarginBottom = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MarginBottom", 30f, "Space kept free at the bottom of the screen, in UI units.");
			MarginSides = ((BaseUnityPlugin)this).Config.Bind<float>("General", "MarginSides", 20f, "Space kept free at the left and right edges, in UI units.");
			Harmony.CreateAndPatchAll(typeof(TechNodePatches), "com.github.jiesiluo.techtreenorecenter");
		}

		private void LateUpdate()
		{
			CardKeeper.Update();
		}
	}
	internal static class TechNodePatches
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(UITechNode), "OnPointerDown")]
		private static void AfterClick(UITechNode __instance)
		{
			UITechTree tree = __instance.tree;
			if (!((Object)(object)tree == (Object)null) && !((Object)(object)tree.selected != (Object)(object)__instance))
			{
				if ((Object)(object)tree.centerViewNode == (Object)(object)__instance)
				{
					tree.centerViewNode = null;
				}
				CardKeeper.Begin(tree, __instance);
			}
		}
	}
	public static class CardKeeper
	{
		private const float Seconds = 1.5f;

		private static readonly FieldRef<UITechTree, Vector2> ScrollPos0 = AccessTools.FieldRefAccess<UITechTree, Vector2>("scrollPos0");

		private static readonly FieldRef<UITechTree, Vector2> ScrollPos1 = AccessTools.FieldRefAccess<UITechTree, Vector2>("scrollPos1");

		private static readonly FieldRef<UITechTree, float> ViewScale = AccessTools.FieldRefAccess<UITechTree, float>("viewScale");

		private static UITechTree tree;

		private static UITechNode node;

		private static float until;

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

		public static bool Active => (Object)(object)tree != (Object)null;

		internal static void Begin(UITechTree t, UITechNode n)
		{
			if (Plugin.KeepCardOnScreen.Value)
			{
				tree = t;
				node = n;
				until = Time.unscaledTime + 1.5f;
			}
		}

		private static void End()
		{
			tree = null;
			node = null;
		}

		internal static void Update()
		{
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: 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_0101: 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_0107: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: 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_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)tree == (Object)null)
			{
				return;
			}
			if (!((ManualBehaviour)tree).active || (Object)(object)tree.selected != (Object)(object)node || tree.isDragging || (Object)(object)tree.centerViewNode != (Object)null || Time.unscaledTime > until)
			{
				End();
				return;
			}
			Vector2 val = Overflow(tree, node);
			if (!(((Vector2)(ref val)).sqrMagnitude < 0.25f))
			{
				float num = Mathf.Max(ViewScale.Invoke(tree), 0.05f);
				Vector2 val2 = val * (1f - Mathf.Exp(-15f * Time.unscaledDeltaTime)) / num;
				if (tree.page == 1)
				{
					ref Vector2 reference = ref ScrollPos1.Invoke(tree);
					reference += val2;
				}
				else
				{
					ref Vector2 reference2 = ref ScrollPos0.Invoke(tree);
					reference2 += val2;
				}
				tree.SetPanelTrans();
			}
		}

		public static Vector2 Overflow(UITechTree t, UITechNode n)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
			Rect val = CardRect(t, n);
			Rect val2 = ViewRect(t);
			float num = 0f;
			float num2 = 0f;
			if (((Rect)(ref val)).xMax > ((Rect)(ref val2)).xMax)
			{
				num = ((Rect)(ref val2)).xMax - ((Rect)(ref val)).xMax;
			}
			if (((Rect)(ref val)).xMin + num < ((Rect)(ref val2)).xMin)
			{
				num = ((Rect)(ref val2)).xMin - ((Rect)(ref val)).xMin;
			}
			if (((Rect)(ref val)).yMin < ((Rect)(ref val2)).yMin)
			{
				num2 = ((Rect)(ref val2)).yMin - ((Rect)(ref val)).yMin;
			}
			if (((Rect)(ref val)).yMax + num2 > ((Rect)(ref val2)).yMax)
			{
				num2 = ((Rect)(ref val2)).yMax - ((Rect)(ref val)).yMax;
			}
			return new Vector2(num, num2);
		}

		public static RectTransform Space(UITechTree t)
		{
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Expected O, but got Unknown
			return (RectTransform)((Transform)t.graphGroup0).parent;
		}

		public static Rect CardRect(UITechTree t, UITechNode n)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			return Bounds(Space(t), n.panelRect);
		}

		public static Rect ViewRect(UITechTree t)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			Rect val = Bounds(Space(t), (RectTransform)((Component)t).transform);
			float value = Plugin.MarginSides.Value;
			return Rect.MinMaxRect(((Rect)(ref val)).xMin + value, ((Rect)(ref val)).yMin + Plugin.MarginBottom.Value, ((Rect)(ref val)).xMax - value, ((Rect)(ref val)).yMax - Plugin.MarginTop.Value);
		}

		private static Rect Bounds(RectTransform space, RectTransform rect)
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			rect.GetWorldCorners(Corners);
			Vector2 val = default(Vector2);
			((Vector2)(ref val))..ctor(float.MaxValue, float.MaxValue);
			Vector2 val2 = default(Vector2);
			((Vector2)(ref val2))..ctor(float.MinValue, float.MinValue);
			Vector3[] corners = Corners;
			foreach (Vector3 val3 in corners)
			{
				Vector2 val4 = Vector2.op_Implicit(((Transform)space).InverseTransformPoint(val3));
				val = Vector2.Min(val, val4);
				val2 = Vector2.Max(val2, val4);
			}
			return Rect.MinMaxRect(val.x, val.y, val2.x, val2.y);
		}
	}
}