using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ReorderTechQueue")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ReorderTechQueue")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("20bde779-7d7b-4372-abc0-29162f9145fb")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.1.0")]
[module: UnverifiableCode]
namespace ReorderTechQueue;
[BepInPlugin("com.starfi5h.plugin.ReorderTechQueue", "ReorderTechQueue", "1.2.0")]
public class ReorderTechQueuePlugin : BaseUnityPlugin
{
private Harmony harmony;
public static ConfigEntry<int> TechQueueLength;
public void Awake()
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_0010: Expected O, but got Unknown
harmony = new Harmony("com.starfi5h.plugin.ReorderTechQueue");
harmony.PatchAll(typeof(ReorderTechQueue));
TechQueueLength = ((BaseUnityPlugin)this).Config.Bind<int>("General", "TechQueueLength", 8, "Length of reserach queue.\n研究佇列的长度");
harmony.PatchAll(typeof(UITechNodePatch));
}
}
internal class ReorderTechQueue
{
[HarmonyPostfix]
[HarmonyPatch(typeof(GameHistoryData), "Import")]
[HarmonyPatch(typeof(GameHistoryData), "SetForNewGame")]
public static void ChangeTechQueueLength(GameHistoryData __instance)
{
int[] array = new int[ReorderTechQueuePlugin.TechQueueLength.Value];
int length = Math.Min(__instance.techQueue.Length, array.Length);
Array.Copy(__instance.techQueue, array, length);
__instance.techQueue = array;
}
[HarmonyPostfix]
[HarmonyPatch(typeof(UIResearchQueue), "_OnInit")]
public static void OnInit(UIResearchQueue __instance)
{
AddUIReorderNode(__instance.nodes);
}
[HarmonyPostfix]
[HarmonyPatch(typeof(UITechNode), "DeterminePrerequisiteSuffice")]
public static void SkipMetadataRequirement(ref bool __result)
{
__result = true;
}
public static void AddUIReorderNode(UIResearchQueueNode[] nodes)
{
for (int i = 0; i < nodes.Length; i++)
{
((Component)nodes[i]).gameObject.AddComponent<UIReorderNode>().Index = i;
}
}
public static void RemoveUIReorderNode(UIResearchQueueNode[] nodes)
{
for (int i = 0; i < nodes.Length; i++)
{
Object.Destroy((Object)(object)((Component)nodes[i]).gameObject.GetComponent<UIReorderNode>());
}
}
}
internal class UITechNodePatch
{
private static UITechNode currentSelectNode;
private static UIButton locateBtn;
[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_00c0: Unknown result type (might be due to invalid IL or missing references)
try
{
if ((Object)(object)currentSelectNode == (Object)(object)__instance.selected)
{
return;
}
currentSelectNode = __instance.selected;
if ((Object)(object)__instance.selected == (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)
{
}
}
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);
}
}
}
}
public class UIReorderNode : ManualBehaviour, IPointerDownHandler, IEventSystemHandler, IPointerUpHandler, IPointerEnterHandler
{
private static int currIndex = -1;
public int Index { get; set; }
public void OnPointerDown(PointerEventData pointerEventData)
{
currIndex = Index;
}
public void OnPointerUp(PointerEventData pointerEventData)
{
currIndex = -1;
}
public void OnPointerEnter(PointerEventData eventData)
{
if (currIndex != -1)
{
currIndex = ReorderQueue(currIndex, Index);
}
}
private static int ReorderQueue(int oldIndex, int newIndex)
{
int num = ((oldIndex < newIndex) ? oldIndex : newIndex);
int[] array = new int[GameMain.data.history.techQueueLength - num];
Array.Copy(GameMain.data.history.techQueue, num, array, 0, array.Length);
int num2 = array[oldIndex - num];
array[oldIndex - num] = array[newIndex - num];
array[newIndex - num] = num2;
for (int num3 = GameMain.data.history.techQueueLength - 1; num3 >= num; num3--)
{
GameMain.data.history.RemoveTechInQueue(num3);
}
for (int i = 0; i < array.Length; i++)
{
GameMain.data.history.EnqueueTech(array[i]);
}
if (GameMain.data.history.techQueue[newIndex] != num2)
{
return -1;
}
return newIndex;
}
}