Decompiled source of UITweaks v1.0.0

UITweaks.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("UITweaks")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace UITweaks;

[BepInPlugin("starfi5h.plugin.UITweaks", "UITweaks", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "starfi5h.plugin.UITweaks";

	public const string NAME = "UITweaks";

	public const string VERSION = "1.0.0";

	public static ManualLogSource Log;

	private static Plugin plugin;

	private static Harmony harmony;

	public void Awake()
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		plugin = this;
		harmony = new Harmony("starfi5h.plugin.UITweaks");
		harmony.PatchAll(typeof(TechTree_Tweaks));
		harmony.PatchAll(typeof(Station_Tweaks));
		harmony.PatchAll(typeof(UILayout_Tweaks));
		UILayout_Tweaks.OnAwake(((BaseUnityPlugin)this).Config);
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(UIOptionWindow), "OnApplyClick")]
	public static void OnApplyButtonClick()
	{
		((BaseUnityPlugin)plugin).Config.Reload();
	}
}
public class Station_Tweaks
{
	[HarmonyPostfix]
	[HarmonyPatch(typeof(UIStationStorage), "_OnOpen")]
	internal static void UIStationStorage_OnOpen(UIStationStorage __instance)
	{
		if ((Object)(object)((Component)__instance.localSdButton).gameObject.GetComponent<ButtonClickHandler>() == (Object)null)
		{
			ButtonClickHandler buttonClickHandler = ((Component)__instance.localSdButton).gameObject.AddComponent<ButtonClickHandler>();
			buttonClickHandler.OnRightClick = delegate
			{
				__instance.poppedRemote = false;
				__instance.OnOptionButton1Click();
			};
			buttonClickHandler.OnMiddleClick = delegate
			{
				__instance.poppedRemote = false;
				__instance.OnOptionButton0Click();
			};
		}
		if ((Object)(object)((Component)__instance.remoteSdButton).gameObject.GetComponent<ButtonClickHandler>() == (Object)null)
		{
			ButtonClickHandler buttonClickHandler2 = ((Component)__instance.remoteSdButton).gameObject.AddComponent<ButtonClickHandler>();
			buttonClickHandler2.OnRightClick = delegate
			{
				__instance.poppedRemote = true;
				__instance.OnOptionButton1Click();
			};
			buttonClickHandler2.OnMiddleClick = delegate
			{
				__instance.poppedRemote = true;
				__instance.OnOptionButton0Click();
			};
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(UIControlPanelStationStorage), "_OnOpen")]
	internal static void UIControlPanelStationStorage_OnOpen(UIControlPanelStationStorage __instance)
	{
		if ((Object)(object)((Component)__instance.localSdButton).gameObject.GetComponent<ButtonClickHandler>() == (Object)null)
		{
			ButtonClickHandler buttonClickHandler = ((Component)__instance.localSdButton).gameObject.AddComponent<ButtonClickHandler>();
			buttonClickHandler.OnRightClick = delegate
			{
				__instance.poppedRemote = false;
				__instance.OnOptionButton1Click();
			};
			buttonClickHandler.OnMiddleClick = delegate
			{
				__instance.poppedRemote = false;
				__instance.OnOptionButton0Click();
			};
		}
		if ((Object)(object)((Component)__instance.remoteSdButton).gameObject.GetComponent<ButtonClickHandler>() == (Object)null)
		{
			ButtonClickHandler buttonClickHandler2 = ((Component)__instance.remoteSdButton).gameObject.AddComponent<ButtonClickHandler>();
			buttonClickHandler2.OnRightClick = delegate
			{
				__instance.poppedRemote = true;
				__instance.OnOptionButton1Click();
			};
			buttonClickHandler2.OnMiddleClick = delegate
			{
				__instance.poppedRemote = true;
				__instance.OnOptionButton0Click();
			};
		}
	}
}
public class ButtonClickHandler : MonoBehaviour, IPointerClickHandler, IEventSystemHandler
{
	public Action OnRightClick { get; set; }

	public Action OnMiddleClick { get; set; }

	public void OnPointerClick(PointerEventData eventData)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: Invalid comparison between Unknown and I4
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Invalid comparison between Unknown and I4
		if ((int)eventData.button == 1)
		{
			OnRightClick?.Invoke();
		}
		else if ((int)eventData.button == 2)
		{
			OnMiddleClick?.Invoke();
		}
	}
}
public class TechTree_Tweaks
{
	private static UITechNode currentSelectNode;

	private static UIButton locateBtn;

	[HarmonyPostfix]
	[HarmonyPatch(typeof(UITechTree), "_OnInit")]
	internal static void Init()
	{
		foreach (UITechNode value in UIRoot.instance.uiGame.techTree.nodes.Values)
		{
			AddCostPreview(value);
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(UITechNode), "DeterminePrerequisiteSuffice")]
	public static void SkipMetadataRequirement(ref bool __result)
	{
		__result = true;
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(UITechTree), "_OnLateUpdate")]
	public static void UpdateNaviBtn(UITechTree __instance)
	{
		//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			if ((Object)(object)currentSelectNode == (Object)(object)__instance.selected)
			{
				return;
			}
			UITechNode node = currentSelectNode;
			currentSelectNode = __instance.selected;
			SetCostPreviewActive(node, active: true);
			SetCostPreviewActive(currentSelectNode, active: false);
			if ((Object)(object)currentSelectNode == (Object)null)
			{
				UIButton obj = locateBtn;
				if (obj != null)
				{
					((Component)obj).gameObject.SetActive(false);
				}
				return;
			}
			if ((Object)(object)locateBtn == (Object)null)
			{
				AddBtn(((Component)currentSelectNode).gameObject.transform);
			}
			if (GameMain.history.ImplicitPreTechRequired(((Proto)(currentSelectNode.techProto?)).ID ?? 0) != 0)
			{
				((Component)locateBtn).transform.SetParent(((Component)currentSelectNode).transform);
				((Component)locateBtn).transform.localPosition = new Vector3(286f, -218f, 0f);
				((Component)locateBtn).gameObject.SetActive(true);
			}
			else
			{
				((Component)locateBtn).gameObject.SetActive(false);
			}
		}
		catch (Exception ex)
		{
			Debug.Log((object)ex);
		}
	}

	private static void AddBtn(Transform parent)
	{
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		GameObject obj = Object.Instantiate<GameObject>(((Component)UIRoot.instance.uiGame.researchQueue.pauseButton).gameObject, parent);
		((Object)obj).name = "ReorderTechQueue_Navi";
		obj.transform.localScale = new Vector3(0.33f, 0.33f, 0f);
		obj.transform.localPosition = new Vector3(286f, -218f, 0f);
		Transform obj2 = obj.transform.Find("icon");
		Image val = ((obj2 != null) ? ((Component)obj2).GetComponent<Image>() : null);
		if ((Object)(object)val != (Object)null)
		{
			UIStarmap starmap = UIRoot.instance.uiGame.starmap;
			Transform obj3 = ((Component)starmap.cursorFunctionButton3).transform.Find("icon");
			object sprite;
			if (obj3 == null)
			{
				sprite = null;
			}
			else
			{
				Image component = ((Component)obj3).GetComponent<Image>();
				sprite = ((component != null) ? component.sprite : null);
			}
			val.sprite = (Sprite)sprite;
		}
		locateBtn = obj.GetComponent<UIButton>();
		locateBtn.tips.tipTitle = "Locate";
		locateBtn.tips.tipText = "Navigate to the required tech";
		locateBtn.onClick += OnLocateButtonClick;
	}

	private static void OnLocateButtonClick(int obj)
	{
		if (!((Object)(object)currentSelectNode == (Object)null))
		{
			int num = GameMain.history.ImplicitPreTechRequired(((Proto)(currentSelectNode.techProto?)).ID ?? 0);
			if (num != 0)
			{
				UIRoot.instance.uiGame.techTree.SelectTech(num);
			}
		}
	}

	private static void AddCostPreview(UITechNode node)
	{
		//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)node == (Object)null)
		{
			return;
		}
		Transform obj = ((Component)node).gameObject.transform.Find("icon");
		GameObject val = ((obj != null) ? ((Component)obj).gameObject : null);
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		int num = Math.Min(node.techProto.itemArray.Length, 6);
		int num2 = 0;
		for (int num3 = num - 1; num3 >= 0; num3--)
		{
			if (node.techProto.itemArray[num3] != null)
			{
				GameObject obj2 = Object.Instantiate<GameObject>(val, ((Component)node).gameObject.transform);
				((Object)obj2).name = "CostPreviewIcon-" + num3;
				obj2.transform.localPosition = new Vector3((float)(190 - num2++ * 15), -124f, 0f);
				obj2.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);
				((Component)obj2.transform).GetComponent<Image>().sprite = node.techProto.itemArray[num3].iconSprite;
			}
		}
	}

	private static void SetCostPreviewActive(UITechNode node, bool active)
	{
		if ((Object)(object)node == (Object)null)
		{
			return;
		}
		for (int i = 0; i < 6; i++)
		{
			Transform obj = ((Component)node).gameObject.transform.Find("CostPreviewIcon-" + i);
			if (obj != null)
			{
				((Component)obj).gameObject.SetActive(active);
			}
		}
	}

	private static void RemoveCostPreview(UITechNode node)
	{
		if (!((Object)(object)node == (Object)null))
		{
			for (int i = 0; i < 6; i++)
			{
				Transform obj = ((Component)node).gameObject.transform.Find("CostPreviewIcon-" + i);
				Object.Destroy((Object)(object)((obj != null) ? ((Component)obj).gameObject : null));
			}
		}
	}
}
public class UILayout_Tweaks
{
	private static ConfigEntry<bool> enableOverwrite;

	private static ConfigEntry<int> customLayoutHeightEntry;

	private static int customLayoutHeight;

	public static void OnAwake(ConfigFile config)
	{
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: Expected O, but got Unknown
		enableOverwrite = config.Bind<bool>("UI Layout", "Enable Overwrite", false, "Enable overwite to the UI layout height setting");
		customLayoutHeightEntry = config.Bind<int>("UI Layout", "UI Layout Height", 900, new ConfigDescription("Lower value gets larger layout", (AcceptableValueBase)(object)new AcceptableValueRange<int>(480, 900), Array.Empty<object>()));
		customLayoutHeightEntry.SettingChanged += OnConfigChanged;
	}

	public static void OnDestroy(ConfigFile _)
	{
		customLayoutHeightEntry.SettingChanged -= OnConfigChanged;
	}

	public static void OnConfigChanged(object sender, EventArgs e)
	{
		if (enableOverwrite.Value)
		{
			Plugin.Log.LogDebug((object)$"uiLayoutHeight changed: {UICanvasScalerHandler.uiLayoutHeight} => {customLayoutHeightEntry.Value}");
			UICanvasScalerHandler.uiLayoutHeight = customLayoutHeightEntry.Value;
			DSPGame.globalOption.uiLayoutHeight = customLayoutHeightEntry.Value;
		}
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(UIOptionWindow), "CollectUILayoutHeights")]
	public static void CollectUILayoutHeights_Postfix(UIOptionWindow __instance)
	{
		if (enableOverwrite.Value)
		{
			List<int> uiLayoutHeights = __instance.uiLayoutHeights;
			if (customLayoutHeight != 0 && customLayoutHeight != customLayoutHeightEntry.Value)
			{
				uiLayoutHeights.Remove(customLayoutHeight);
			}
			customLayoutHeight = customLayoutHeightEntry.Value;
			if (!uiLayoutHeights.Contains(customLayoutHeight))
			{
				uiLayoutHeights.Add(customLayoutHeight);
				uiLayoutHeights.Sort();
			}
		}
	}
}