Decompiled source of GeosUITweaks v0.1.0

GeosUITweaks.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
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.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("GeosUITweaks")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Small changes to the game's interface")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+a2afa3d962886bb25a72d1d0ae45167e1d694804")]
[assembly: AssemblyProduct("GeosUITweaks")]
[assembly: AssemblyTitle("GeosUITweaks")]
[assembly: AssemblyVersion("0.1.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 GeosUITweaks
{
	[BepInPlugin("com.geostyx.dsp.geosuitweaks", "GeosUITweaks", "0.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ConfigEntry<bool> ShowWarningPlanets;

		private Harmony? _harmony;

		private void Awake()
		{
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Expected O, but got Unknown
			ShowWarningPlanets = ((BaseUnityPlugin)this).Config.Bind<bool>("Warnings", "ShowWarningPlanets", true, "Hovering over a warning lists the planets it's on.");
			_harmony = new Harmony("com.geostyx.dsp.geosuitweaks");
			try
			{
				_harmony.PatchAll(typeof(WarningPlanetsPatch));
			}
			catch
			{
				_harmony.UnpatchSelf();
				_harmony = null;
				throw;
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"GeosUITweaks 0.1.0 loaded");
		}

		private void OnDestroy()
		{
			Harmony? harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
			_harmony = null;
		}
	}
	[HarmonyPatch(typeof(UIWarningWindow), "_OnLateUpdate")]
	internal static class WarningPlanetsPatch
	{
		private const int DashboardSignalId = 518;

		private const int LocationSignalId = 519;

		private const int MarkerSignalId = 2401;

		private const int MaxPlanets = 10;

		private const int RefreshFrames = 30;

		private static readonly FieldRef<UIWarningWindow, List<UIWarningSignalEntry>> SignalEntries = AccessTools.FieldRefAccess<UIWarningWindow, List<UIWarningSignalEntry>>("signalEntries");

		private static readonly FieldRef<UIWarningWindow, List<UIWarningItemEntry>> ItemEntries = AccessTools.FieldRefAccess<UIWarningWindow, List<UIWarningItemEntry>>("itemEntries");

		private static readonly Dictionary<int, int> Counts = new Dictionary<int, int>();

		private static readonly List<int> Astros = new List<int>();

		private static readonly Comparison<int> ByCountThenAstro = delegate(int a, int b)
		{
			int num = Counts[b].CompareTo(Counts[a]);
			return (num == 0) ? a.CompareTo(b) : num;
		};

		private static readonly StringBuilder Text = new StringBuilder();

		private static int _cachedSignalId;

		private static int _cachedDetailId;

		private static int _cachedFrame = int.MinValue;

		private static string _cachedText = "";

		[HarmonyPostfix]
		private static void Postfix(UIWarningWindow __instance)
		{
			//IL_01af: Unknown result type (might be due to invalid IL or missing references)
			bool value = Plugin.ShowWarningPlanets.Value;
			WarningSystem warningSystem = __instance.warningSystem;
			if (warningSystem == null)
			{
				return;
			}
			int num = ((__instance.selectedSignalId > 0) ? __instance.selectedSignalId : __instance.hoverSignalId);
			bool flag = value;
			if (flag)
			{
				bool flag2 = ((num == 0 || (uint)(num - 518) <= 1u || num == 2401) ? true : false);
				flag = !flag2;
			}
			bool flag3 = flag;
			List<UIWarningItemEntry> list = ItemEntries.Invoke(__instance);
			for (int i = 0; i < list.Count; i++)
			{
				UIButton button = list[i].button;
				ref TipSettings tips = ref button.tips;
				if (flag3 && i < __instance.itemCount && button._isPointerEnter)
				{
					string text = PlanetsText(warningSystem, num, list[i].detailId1);
					string text2 = ((text.Length > 0) ? SignalName(list[i].detailId1) : "");
					if (tips.tipText != text || tips.tipTitle != text2)
					{
						tips.tipTitle = text2;
						tips.tipText = text;
						button.UpdateTip();
					}
				}
				else
				{
					tips.tipText = null;
				}
			}
			List<UIWarningSignalEntry> list2 = SignalEntries.Invoke(__instance);
			int hoverIndex = __instance.hoverIndex;
			int hoverSignalId = __instance.hoverSignalId;
			if (value && hoverIndex >= 0 && hoverIndex < list2.Count && hoverSignalId != 518 && hoverSignalId != 519)
			{
				string text3 = PlanetsText(warningSystem, hoverSignalId, 0);
				if (text3.Length != 0)
				{
					__instance.hoverTip.SetTip(true, SignalName(hoverSignalId), text3, 6, new Vector2(10f, -6f), 0, (Transform)(object)list2[hoverIndex].rectTrans, (string)null, (string)null);
					((Component)__instance.hoverTip).gameObject.SetActive(true);
				}
			}
		}

		private static string SignalName(int signalId)
		{
			object obj2;
			if (signalId >= 1000)
			{
				ItemProto obj = ((ProtoSet<ItemProto>)(object)LDB.items).Select(signalId);
				obj2 = ((obj != null) ? ((Proto)obj).name : null);
			}
			else
			{
				SignalProto obj3 = ((ProtoSet<SignalProto>)(object)LDB.signals).Select(signalId);
				obj2 = ((obj3 != null) ? ((Proto)obj3).name : null);
			}
			if (obj2 == null)
			{
				obj2 = "";
			}
			return (string)obj2;
		}

		private static string PlanetsText(WarningSystem warningSystem, int signalId, int detailId)
		{
			if (signalId == _cachedSignalId && detailId == _cachedDetailId && Time.frameCount - _cachedFrame < 30)
			{
				return _cachedText;
			}
			Counts.Clear();
			Astros.Clear();
			WarningData[] warningPool = warningSystem.warningPool;
			int num = Math.Min(warningSystem.warningCursor, warningPool.Length);
			for (int i = 1; i < num; i++)
			{
				ref WarningData reference = ref warningPool[i];
				if (reference.id == i && reference.state > 0 && reference.signalId == signalId && reference.astroId > 0 && (detailId <= 0 || reference.detailId1 == detailId))
				{
					if (Counts.TryGetValue(reference.astroId, out var value))
					{
						Counts[reference.astroId] = value + 1;
						continue;
					}
					Counts[reference.astroId] = 1;
					Astros.Add(reference.astroId);
				}
			}
			Astros.Sort(ByCountThenAstro);
			GalaxyData galaxy = warningSystem.gameData.galaxy;
			Text.Length = 0;
			int num2 = Math.Min(Astros.Count, 10);
			for (int j = 0; j < num2; j++)
			{
				int num3 = Astros[j];
				PlanetData obj = galaxy.PlanetByAstroId(num3);
				string value2 = ((obj != null) ? obj.displayName : null) ?? num3.ToString();
				if (Text.Length > 0)
				{
					Text.Append('\n');
				}
				Text.Append(value2);
				if (Astros.Count > 1)
				{
					Text.Append(" (").Append(Counts[num3]).Append(')');
				}
			}
			if (Astros.Count > num2)
			{
				Text.Append("\n… +").Append(Astros.Count - num2);
			}
			_cachedSignalId = signalId;
			_cachedDetailId = detailId;
			_cachedFrame = Time.frameCount;
			_cachedText = Text.ToString();
			return _cachedText;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "com.geostyx.dsp.geosuitweaks";

		public const string PLUGIN_NAME = "GeosUITweaks";

		public const string PLUGIN_VERSION = "0.1.0";
	}
}