Decompiled source of MoreResearchQueue v2.1.0

MoreResearchQueue.dll

Decompiled 2 days ago
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: CompilationRelaxations(8)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace MoreResearchQueue;

[BepInProcess("DSPGAME.exe")]
[BepInPlugin("com.dsp.moreResearchQueue", "MoreResearchQueue", "2.1.0")]
public class Plugin : BaseUnityPlugin
{
	public const string GUID = "com.dsp.moreResearchQueue";

	public const string NAME = "MoreResearchQueue";

	public const string VERSION = "2.1.0";

	public static ConfigEntry<int> MaxQueueLength;

	public static ConfigEntry<KeyboardShortcut> ToggleKey;

	internal static bool showUI;

	internal static Rect winRect = new Rect(80f, 80f, 340f, 200f);

	internal static string textValue = "32";

	internal static string status = "";

	internal static float sliderValue = 32f;

	internal void Awake()
	{
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Expected O, but got Unknown
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: Expected O, but got Unknown
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		MaxQueueLength = ((BaseUnityPlugin)this).Config.Bind<int>("General", "MaxQueueLength", 32, new ConfigDescription("Research queue length (2-255). Default 32.\n研究队列长度(2-255),默认32。", (AcceptableValueBase)(object)new AcceptableValueRange<int>(2, 255), new object[0]));
		ToggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("General", "ToggleKey", new KeyboardShortcut((KeyCode)289, (KeyCode[])(object)new KeyCode[0]), "Hotkey to toggle the queue settings window.\n开关队列设置窗口的快捷键。");
		textValue = MaxQueueLength.Value.ToString();
		sliderValue = Mathf.Clamp(MaxQueueLength.Value, 2, 255);
		try
		{
			Harmony val = new Harmony("com.dsp.moreResearchQueue");
			val.PatchAll(typeof(HistoryPatches));
			Log("v2.1.0 loaded. MaxQueueLength = " + MaxQueueLength.Value + ", hotkey = " + ToggleKey.Value);
		}
		catch (Exception ex)
		{
			Log("PATCH FAILED: " + ex);
		}
	}

	internal static void Log(object msg)
	{
		Logger.CreateLogSource("MoreResearchQueue").LogInfo((object)msg.ToString());
	}

	internal void Update()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			KeyboardShortcut value = ToggleKey.Value;
			if (((KeyboardShortcut)(ref value)).IsDown())
			{
				showUI = !showUI;
			}
		}
		catch (Exception)
		{
		}
	}

	internal void OnGUI()
	{
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0019: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Expected O, but got Unknown
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		if (showUI)
		{
			winRect = GUI.Window(19794, winRect, new WindowFunction(DrawWindow), "MoreResearchQueue v2.1.0");
		}
	}

	private void DrawWindow(int id)
	{
		//IL_00db: Unknown result type (might be due to invalid IL or missing references)
		GUILayout.Space(4f);
		GUILayout.Label("Queue length / 队列长度: " + Mathf.RoundToInt(sliderValue), (GUILayoutOption[])(object)new GUILayoutOption[0]);
		sliderValue = GUILayout.HorizontalSlider(sliderValue, 2f, 255f, (GUILayoutOption[])(object)new GUILayoutOption[0]);
		GUILayout.BeginHorizontal((GUILayoutOption[])(object)new GUILayoutOption[0]);
		GUILayout.Label("Value:", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(46f) });
		textValue = GUILayout.TextField(textValue, (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Width(70f) });
		if (GUILayout.Button("Apply / 应用", (GUILayoutOption[])(object)new GUILayoutOption[1] { GUILayout.Height(22f) }))
		{
			ApplyFromUI();
		}
		GUILayout.EndHorizontal();
		GUILayout.Space(4f);
		GUILayout.Label(string.Concat("Hotkey: ", ToggleKey.Value, "  (toggle window / 开关窗口)"), (GUILayoutOption[])(object)new GUILayoutOption[0]);
		GUILayout.Label("Takes effect immediately, queued techs are kept.\n立即生效,保留已排队的科技。\nReopen the research UI to refresh visible slots.\n重新打开研究界面刷新显示格子。", (GUILayoutOption[])(object)new GUILayoutOption[0]);
		if (!string.IsNullOrEmpty(status))
		{
			GUILayout.Label(status, (GUILayoutOption[])(object)new GUILayoutOption[0]);
		}
		GUI.DragWindow();
	}

	private void ApplyFromUI()
	{
		if (!int.TryParse(textValue.Trim(), out var result))
		{
			result = Mathf.RoundToInt(sliderValue);
		}
		result = Mathf.Clamp(result, 2, 255);
		sliderValue = result;
		textValue = result.ToString();
		MaxQueueLength.Value = result;
		bool flag = HistoryPatches.ResizeQueue(result);
		status = (flag ? ("Applied: " + result) : "No game loaded yet (enter a game first).");
		Log("Queue length set to " + result + ", applied=" + flag);
	}
}
[HarmonyPatch]
internal static class HistoryPatches
{
	[HarmonyPatch(typeof(GameHistoryData), "Import")]
	[HarmonyPatch(typeof(GameHistoryData), "SetForNewGame")]
	[HarmonyPostfix]
	public static void AfterHistoryInit(GameHistoryData __instance)
	{
		Resize(__instance, Mathf.Clamp(Plugin.MaxQueueLength.Value, 2, 255));
	}

	[HarmonyPatch(typeof(GameHistoryData), "MaxTechQueueCount")]
	[HarmonyPostfix]
	public static void OverrideMaxTechQueueCount(GameHistoryData __instance, ref int __result)
	{
		int num = Mathf.Clamp(Plugin.MaxQueueLength.Value, 2, 255);
		if (__result < num)
		{
			__result = num;
		}
		if (__instance.techQueue != null && __instance.techQueue.Length < num)
		{
			Resize(__instance, num);
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(UIResearchQueue), "_OnInit")]
	public static void BeforeQueueUIInit()
	{
		if (GameMain.data != null && GameMain.data.history != null)
		{
			Resize(GameMain.data.history, Mathf.Clamp(Plugin.MaxQueueLength.Value, 2, 255));
		}
	}

	public static bool ResizeQueue(int cap)
	{
		cap = Mathf.Clamp(cap, 2, 255);
		if (GameMain.data == null || GameMain.data.history == null)
		{
			return false;
		}
		Resize(GameMain.data.history, cap);
		return true;
	}

	public static void Resize(GameHistoryData history, int cap)
	{
		try
		{
			if (history.techQueue != null && history.techQueue.Length != cap)
			{
				int[] array = new int[cap];
				int i;
				for (i = 0; i < history.techQueue.Length && history.techQueue[i] != 0; i++)
				{
				}
				int num = Mathf.Min(i, cap - 1);
				Array.Copy(history.techQueue, array, num);
				for (int j = num; j < cap; j++)
				{
					array[j] = 0;
				}
				history.techQueue = array;
				Plugin.Log("techQueue resized to " + cap);
			}
		}
		catch (Exception ex)
		{
			Plugin.Log("Resize failed: " + ex);
		}
	}
}