Decompiled source of ExtendedTechQueue v1.0.2

Extended.Tech.Queue.v.1.0.1.dll

Decompiled 11 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[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("Loki Moon Labs")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("Extended Tech Queue")]
[assembly: AssemblyTitle("ExtendedTechQueue")]
[assembly: AssemblyVersion("1.0.1.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 ExtendedTechQueue
{
	internal static class ModInfo
	{
		internal const string Guid = "com.lokimoonlabs.extendedtechqueue";

		internal const string Name = "Extended Tech Queue";

		internal const string Version = "1.0.1";

		internal const string HarmonyId = "com.lokimoonlabs.extendedtechqueue.patches";
	}
	[BepInPlugin("com.lokimoonlabs.extendedtechqueue", "Extended Tech Queue", "1.0.1")]
	[BepInProcess("DSPGAME.exe")]
	public sealed class Plugin : BaseUnityPlugin
	{
		internal static ConfigEntry<int> TechQueueLength;

		private Harmony harmony;

		private void Awake()
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Expected O, but got Unknown
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: Expected O, but got Unknown
			TechQueueLength = ((BaseUnityPlugin)this).Config.Bind<int>("General", "TechQueueLength", 100, new ConfigDescription("Maximum research queue length. Vanilla is 8. Applied when a save is loaded or a new game is initialized.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(8, 1000), Array.Empty<object>()));
			harmony = new Harmony("com.lokimoonlabs.extendedtechqueue.patches");
			harmony.PatchAll(typeof(TechQueuePatch));
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Extended Tech Queue v1.0.1 loaded. Queue length: " + TechQueueLength.Value));
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
			harmony = null;
		}
	}
	[HarmonyPatch]
	internal static class TechQueuePatch
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameHistoryData), "MaxTechQueueCount")]
		private static void AfterMaxTechQueueCount(GameHistoryData __instance, ref int __result)
		{
			if (__instance?.techQueue != null)
			{
				__result = __instance.techQueue.Length;
			}
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(GameHistoryData), "Import")]
		private static IEnumerable<CodeInstruction> PreserveImportedQueue(IEnumerable<CodeInstruction> instructions)
		{
			//IL_015b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Expected O, but got Unknown
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			FieldInfo fieldInfo = AccessTools.Field(typeof(GameHistoryData), "techQueue");
			MethodInfo methodInfo = AccessTools.Method(typeof(BinaryReader), "ReadInt32", Type.EmptyTypes, (Type[])null);
			MethodInfo methodInfo2 = AccessTools.Method(typeof(Math), "Max", new Type[2]
			{
				typeof(int),
				typeof(int)
			}, (Type[])null);
			int num = 0;
			for (int i = 4; i + 1 < list.Count; i++)
			{
				if (!(list[i].opcode != OpCodes.Newarr) && object.Equals(list[i].operand, typeof(int)) && CodeInstructionExtensions.StoresField(list[i + 1], fieldInfo) && CodeInstructionExtensions.Calls(list[i - 4], methodInfo))
				{
					CodeInstruction val = LoadStoredLocal(list[i - 3]);
					val.labels.AddRange(list[i].labels);
					list[i].labels.Clear();
					val.blocks.AddRange(list[i].blocks);
					list[i].blocks.Clear();
					list.Insert(i, val);
					list.Insert(i + 1, new CodeInstruction(OpCodes.Call, (object)methodInfo2));
					num++;
					i += 2;
				}
			}
			if (num != 1)
			{
				throw new InvalidOperationException("Extended Tech Queue could not locate the saved research queue allocation in GameHistoryData.Import.");
			}
			return list;
		}

		private static CodeInstruction LoadStoredLocal(CodeInstruction store)
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001e: Expected O, but got Unknown
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Expected O, but got Unknown
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005a: Expected O, but got Unknown
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Expected O, but got Unknown
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009b: Expected O, but got Unknown
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Expected O, but got Unknown
			if (store.opcode == OpCodes.Stloc_0)
			{
				return new CodeInstruction(OpCodes.Ldloc_0, (object)null);
			}
			if (store.opcode == OpCodes.Stloc_1)
			{
				return new CodeInstruction(OpCodes.Ldloc_1, (object)null);
			}
			if (store.opcode == OpCodes.Stloc_2)
			{
				return new CodeInstruction(OpCodes.Ldloc_2, (object)null);
			}
			if (store.opcode == OpCodes.Stloc_3)
			{
				return new CodeInstruction(OpCodes.Ldloc_3, (object)null);
			}
			if (store.opcode == OpCodes.Stloc_S)
			{
				return new CodeInstruction(OpCodes.Ldloc_S, store.operand);
			}
			if (store.opcode == OpCodes.Stloc)
			{
				return new CodeInstruction(OpCodes.Ldloc, store.operand);
			}
			throw new InvalidOperationException("Extended Tech Queue could not identify the saved research queue length local.");
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameHistoryData), "SetForNewGame")]
		[HarmonyPatch(typeof(GameHistoryData), "Import")]
		private static void ResizeTechQueue(GameHistoryData __instance)
		{
			if (__instance?.techQueue == null || Plugin.TechQueueLength == null)
			{
				return;
			}
			int val = 0;
			for (int i = 0; i < __instance.techQueue.Length; i++)
			{
				if (__instance.techQueue[i] != 0)
				{
					val = i + 1;
				}
			}
			int num = Math.Max(8, Math.Max(Plugin.TechQueueLength.Value, val));
			if (__instance.techQueue.Length != num)
			{
				int[] array = new int[num];
				Array.Copy(__instance.techQueue, array, Math.Min(__instance.techQueue.Length, array.Length));
				__instance.techQueue = array;
			}
		}
	}
}