Decompiled source of AlterTickrate v0.2.4

AlterTickrate.dll

Decompiled 2 years ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using AlterTickrate.Compat;
using AlterTickrate.Patches;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyFileVersion("0.2.4")]
[assembly: AssemblyInformationalVersion("0.2.4")]
[assembly: AssemblyProduct("AlterTickrate")]
[assembly: AssemblyTitle("AlterTickrate")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.2.4.0")]
[module: UnverifiableCode]
namespace AlterTickrate
{
	public static class Parameters
	{
		public static int PowerUpdatePeriod = 1;

		public static int FacilityUpdatePeriod = 1;

		public static int LabProduceUpdatePeriod = 1;

		public static int LabResearchUpdatePeriod = 1;

		public static int LabLiftUpdatePeriod = 1;

		public static int InserterUpdatePeriod = 1;

		public static int StorageUpdatePeriod = 1;

		public static int BeltUpdatePeriod = 1;

		public static float FacilitySpeedRate = 1f;

		public static float InserterSpeedRate = 1f;

		private const int MaxBeltUpdatePeriod = 2;

		public static void SetFacilityValues(int powerPeriod, int facilityPeriod)
		{
			PowerUpdatePeriod = powerPeriod;
			FacilityUpdatePeriod = facilityPeriod;
			FacilitySpeedRate = facilityPeriod;
		}

		public static void SetLabValues(int produceLabPeriod, int researchLabPeriod, int liftLabPeriod)
		{
			LabProduceUpdatePeriod = produceLabPeriod;
			LabResearchUpdatePeriod = researchLabPeriod;
			LabLiftUpdatePeriod = liftLabPeriod;
		}

		public static void SetBeltValues(int inserterPeriod, int storageUpdatePeriod, int beltUpdatePeriod)
		{
			InserterUpdatePeriod = inserterPeriod;
			InserterSpeedRate = inserterPeriod;
			StorageUpdatePeriod = storageUpdatePeriod;
			BeltUpdatePeriod = Math.Min(beltUpdatePeriod, 2);
		}
	}
	[BepInPlugin("starfi5h.plugin.AlterTickrate", "AlterTickrate", "0.2.4")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		public const string GUID = "starfi5h.plugin.AlterTickrate";

		public const string NAME = "AlterTickrate";

		public const string VERSION = "0.2.4";

		public static Plugin plugin;

		public static bool Enable;

		private Harmony harmony;

		private ConfigEntry<int> Period_PowerUpdate;

		private ConfigEntry<int> Period_FacilityUpdate;

		private ConfigEntry<int> Period_LabProduceUpdate;

		private ConfigEntry<int> Period_LabResearchUpdate;

		private ConfigEntry<int> Period_LabLiftUpdate;

		private ConfigEntry<int> Period_SorterUpdate;

		private ConfigEntry<int> Period_StorageUpdate;

		private ConfigEntry<int> Period_BeltUpdate;

		private ConfigEntry<bool> UI_SmoothProgress;

		public void LoadConfig()
		{
			Period_PowerUpdate = ((BaseUnityPlugin)this).Config.Bind<int>("Facility", "PowerSystem", 10, "Update power system every x ticks.\n每x帧更新一次电力系统");
			Period_FacilityUpdate = ((BaseUnityPlugin)this).Config.Bind<int>("Facility", "Facility", 10, "Update facilities every x ticks.\n每x帧更新一次生产设施");
			Period_LabProduceUpdate = ((BaseUnityPlugin)this).Config.Bind<int>("Lab", "Produce", 10, "Update producing lab every x ticks.\n每x帧更新一次生产模式的研究站");
			Period_LabResearchUpdate = ((BaseUnityPlugin)this).Config.Bind<int>("Lab", "Research", 10, "Update researching lab every x ticks.\n每x帧更新一次科研模式的研究站");
			Period_LabLiftUpdate = ((BaseUnityPlugin)this).Config.Bind<int>("Lab", "Lift", 5, "Transfer items in lab tower every x ticks.\n每x帧搬运研究站塔内的物料");
			Period_SorterUpdate = ((BaseUnityPlugin)this).Config.Bind<int>("Transport", "Sorter", 2, "Update sorters every x ticks.\n每x帧更新一次分拣器");
			Period_StorageUpdate = ((BaseUnityPlugin)this).Config.Bind<int>("Transport", "Storage", 2, "Update storage every x ticks.\n每x帧更新一次仓储");
			Period_BeltUpdate = ((BaseUnityPlugin)this).Config.Bind<int>("Transport", "Belt", 1, "Update belt every x ticks.(Max:2)\n每x帧更新一次传送带(最大:2)");
			UI_SmoothProgress = ((BaseUnityPlugin)this).Config.Bind<bool>("UI", "SmoothProgress", false, "Interpolates progress animation in UI.\n利用插植使進度動畫平滑");
		}

		public void SaveBeltConfig(int beltUpdate, int storageUpdate)
		{
			Period_BeltUpdate.Value = beltUpdate;
			Period_StorageUpdate.Value = storageUpdate;
		}

		public void SaveLabConfig(int labProduceUpdate, int labResearchUpdate, int labLiftUpdate)
		{
			Period_LabProduceUpdate.Value = labProduceUpdate;
			Period_LabResearchUpdate.Value = labResearchUpdate;
			Period_LabLiftUpdate.Value = labLiftUpdate;
		}

		public void SetEnable(bool enable)
		{
			if (enable)
			{
				Parameters.SetFacilityValues(Period_PowerUpdate.Value, Period_FacilityUpdate.Value);
				Parameters.SetLabValues(Period_LabProduceUpdate.Value, Period_LabResearchUpdate.Value, Period_LabLiftUpdate.Value);
				Parameters.SetBeltValues(Period_SorterUpdate.Value, Period_StorageUpdate.Value, Period_BeltUpdate.Value);
				Enable = true;
			}
			else
			{
				Parameters.SetFacilityValues(1, 1);
				Parameters.SetLabValues(1, 1, 1);
				Parameters.SetBeltValues(1, 1, 1);
				Enable = false;
			}
		}

		public void Awake()
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			plugin = this;
			Log.LogSource = ((BaseUnityPlugin)this).Logger;
			harmony = new Harmony("starfi5h.plugin.AlterTickrate");
			LoadConfig();
			if (ModCompatibility.Init(harmony))
			{
				SetEnable(enable: true);
				Log.Info($"Facility:({Parameters.PowerUpdatePeriod}, {Parameters.FacilityUpdatePeriod}) " + $"Lab:({Parameters.LabProduceUpdatePeriod}, {Parameters.LabResearchUpdatePeriod}, {Parameters.LabLiftUpdatePeriod}) " + $"Belt:({Parameters.InserterUpdatePeriod}, {Parameters.StorageUpdatePeriod}, {Parameters.BeltUpdatePeriod})");
				harmony.PatchAll(typeof(GameData_Patch));
				if (Parameters.PowerUpdatePeriod > 1)
				{
					harmony.PatchAll(typeof(PowerSystem_Patch));
					harmony.PatchAll(typeof(DysonReqPower_Patch));
				}
				if (Parameters.FacilityUpdatePeriod > 1)
				{
					harmony.PatchAll(typeof(Facility_Patch));
				}
				if (Parameters.LabProduceUpdatePeriod > 1)
				{
					harmony.PatchAll(typeof(LabProduce_Patch));
				}
				if (Parameters.LabResearchUpdatePeriod > 1)
				{
					harmony.PatchAll(typeof(LabResearch_Patch));
				}
				if (Parameters.LabLiftUpdatePeriod > 1)
				{
					harmony.PatchAll(typeof(LabLift_Patch));
				}
				if (Parameters.InserterUpdatePeriod > 1)
				{
					harmony.PatchAll(typeof(Inserter_Patch));
				}
				if (Parameters.BeltUpdatePeriod > 1)
				{
					harmony.PatchAll(typeof(CargoPath_Patch));
				}
				if (UI_SmoothProgress.Value)
				{
					harmony.PatchAll(typeof(UIProgress_Patch));
				}
				harmony.PatchAll(typeof(UIcontrol));
			}
		}
	}
	public static class Log
	{
		public static ManualLogSource LogSource;

		public static void Error(object obj)
		{
			LogSource.LogError(obj);
		}

		public static void Warn(object obj)
		{
			LogSource.LogWarning(obj);
		}

		public static void Info(object obj)
		{
			LogSource.LogInfo(obj);
		}

		public static void Debug(object obj)
		{
			LogSource.LogDebug(obj);
		}
	}
	internal class UIcontrol
	{
		private static GameObject activeButton;

		private static Text activeButtonText;

		private static Image activeButtonColor;

		[HarmonyPostfix]
		[HarmonyPatch(typeof(UIPerformancePanel), "_OnOpen")]
		public static void Init()
		{
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Expected O, but got Unknown
			if ((Object)(object)activeButton == (Object)null)
			{
				try
				{
					UIButton cpuActiveButton = UIRoot.instance.uiGame.statWindow.performancePanelUI.cpuActiveButton;
					activeButton = Object.Instantiate<GameObject>(((Component)cpuActiveButton).gameObject, ((Component)cpuActiveButton).transform.parent);
					((Object)activeButton).name = "AlterTickrate - ActiveButton";
					Transform transform = activeButton.transform;
					transform.localPosition += new Vector3(120f, 0f);
					Object.Destroy((Object)(object)activeButton.GetComponent<UIButton>());
					Button component = activeButton.GetComponent<Button>();
					((UnityEventBase)component.onClick).RemoveAllListeners();
					((UnityEvent)component.onClick).AddListener(new UnityAction(OnActiveButtonClick));
					Transform obj = activeButton.transform.Find("button-text");
					Object.Destroy((Object)(object)((Component)obj).GetComponent<Localizer>());
					activeButtonText = ((Component)obj).GetComponent<Text>();
					activeButtonColor = activeButton.GetComponent<Image>();
					activeButtonText.text = "AlterTickrate";
					SetButtonState();
					Log.Debug("UI component init");
				}
				catch
				{
					Log.Warn("UI component initial fail!");
				}
			}
		}

		public static void OnDestory()
		{
			if ((Object)(object)activeButton != (Object)null)
			{
				Object.Destroy((Object)(object)activeButton);
				activeButtonText = null;
				activeButtonColor = null;
			}
		}

		public static void OnActiveButtonClick()
		{
			Plugin.plugin.SetEnable(!Plugin.Enable);
			SetButtonState();
		}

		private static void SetButtonState()
		{
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			if (Plugin.Enable)
			{
				activeButtonText.text = "AlterTick - ON";
				((Graphic)activeButtonColor).color = new Color(0.3804f, 0.8431f, 1f, 0.7216f);
			}
			else
			{
				activeButtonText.text = "AlterTick - OFF";
				((Graphic)activeButtonColor).color = new Color(0.6196f, 0.6196f, 0.6196f, 0.7216f);
			}
		}
	}
}
namespace AlterTickrate.Patches
{
	public class CargoPath_Patch
	{
		[HarmonyTranspiler]
		[HarmonyPatch(typeof(CargoPath), "Update")]
		private static IEnumerable<CodeInstruction> Update_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: Expected O, but got Unknown
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Expected O, but got Unknown
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Expected O, but got Unknown
			//IL_0083: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Expected O, but got Unknown
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Expected O, but got Unknown
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Expected O, but got Unknown
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Expected O, but got Unknown
			//IL_00d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Expected O, but got Unknown
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Expected O, but got Unknown
			//IL_0113: Unknown result type (might be due to invalid IL or missing references)
			//IL_0119: Expected O, but got Unknown
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Expected O, but got Unknown
			try
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				val.MatchForward(true, (CodeMatch[])(object)new CodeMatch[9]
				{
					new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "chunks"), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdloc(i, (LocalBuilder)null)), (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldc_I4_3, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Mul, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldc_I4_2, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Add, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldelem_I4, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Stloc_S, (object)null, (string)null)
				}).Insert((CodeInstruction[])(object)new CodeInstruction[2]
				{
					new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(Parameters), "BeltUpdatePeriod")),
					new CodeInstruction(OpCodes.Mul, (object)null)
				});
				return val.InstructionEnumeration();
			}
			catch (Exception obj)
			{
				Log.Error("Transpiler CargoPath.Update failed");
				Log.Error(obj);
				return instructions;
			}
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(SpraycoaterComponent), "InternalUpdate")]
		private static IEnumerable<CodeInstruction> SpraycoaterComponent_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Expected O, but got Unknown
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Expected O, but got Unknown
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a0: Expected O, but got Unknown
			try
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				val.MatchForward(true, (CodeMatch[])(object)new CodeMatch[2]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdloc(i, (LocalBuilder)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "speed"), (string)null)
				}).Advance(1).Insert((CodeInstruction[])(object)new CodeInstruction[2]
				{
					new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(Parameters), "BeltUpdatePeriod")),
					new CodeInstruction(OpCodes.Mul, (object)null)
				});
				return val.InstructionEnumeration();
			}
			catch (Exception obj)
			{
				Log.Error("Transpiler SpraycoaterComponent.InternalUpdate failed");
				Log.Error(obj);
				return instructions;
			}
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(MonitorComponent), "InternalUpdate")]
		private static IEnumerable<CodeInstruction> MonitorComponent_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Expected O, but got Unknown
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Expected O, but got Unknown
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Expected O, but got Unknown
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Expected O, but got Unknown
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Expected O, but got Unknown
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Expected O, but got Unknown
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Expected O, but got Unknown
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_010c: Expected O, but got Unknown
			//IL_011a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Expected O, but got Unknown
			//IL_0142: Unknown result type (might be due to invalid IL or missing references)
			//IL_0148: Expected O, but got Unknown
			//IL_0156: Unknown result type (might be due to invalid IL or missing references)
			//IL_015c: Expected O, but got Unknown
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0170: Expected O, but got Unknown
			//IL_017e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0184: Expected O, but got Unknown
			//IL_019d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a3: Expected O, but got Unknown
			//IL_01be: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c4: Expected O, but got Unknown
			//IL_01cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d2: Expected O, but got Unknown
			//IL_01da: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e0: Expected O, but got Unknown
			try
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				val.MatchForward(true, (CodeMatch[])(object)new CodeMatch[6]
				{
					new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "targetCargoBytes"), (string)null),
					new CodeMatch((OpCode?)OpCodes.Conv_R8, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldc_R8, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Div, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Stloc_S, (object)null, (string)null)
				}).Advance(-1).Insert((CodeInstruction[])(object)new CodeInstruction[3]
				{
					new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(Parameters), "BeltUpdatePeriod")),
					new CodeInstruction(OpCodes.Conv_R8, (object)null),
					new CodeInstruction(OpCodes.Div, (object)null)
				});
				val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[6]
				{
					new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "cargoFlow"), (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldloc_0, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Add, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Stfld, (object)null, (string)null)
				}).Advance(3).Insert((CodeInstruction[])(object)new CodeInstruction[4]
				{
					new CodeInstruction(OpCodes.Ldloc_0, (object)null),
					new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(Parameters), "BeltUpdatePeriod")),
					new CodeInstruction(OpCodes.Div, (object)null),
					new CodeInstruction(OpCodes.Stloc_0, (object)null)
				});
				return val.InstructionEnumeration();
			}
			catch (Exception obj)
			{
				Log.Error("Transpiler MonitorComponent.InternalUpdate failed");
				Log.Error(obj);
				return instructions;
			}
		}
	}
	public class DysonReqPower_Patch
	{
		private static long[] energyReqs = new long[64];

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(DysonSphere), "BeforeGameTick")]
		private static IEnumerable<CodeInstruction> BeforeGameTick_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Expected O, but got Unknown
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Expected O, but got Unknown
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Expected O, but got Unknown
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bb: Expected O, but got Unknown
			try
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[4]
				{
					new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldc_I4_0, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Conv_I8, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Stfld, (object)AccessTools.Field(typeof(DysonSphere), "energyReqCurrentTick"), (string)null)
				}).Advance(1).RemoveInstructions(3)
					.Insert((CodeInstruction[])(object)new CodeInstruction[2]
					{
						new CodeInstruction(OpCodes.Ldarg_1, (object)null),
						new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(DysonReqPower_Patch), "UpdateEnergyReqCurrentTick", (Type[])null, (Type[])null))
					});
				return val.InstructionEnumeration();
			}
			catch (Exception obj)
			{
				Log.Error("Transpiler DysonSphere.BeforeGameTick failed");
				Log.Error(obj);
				return instructions;
			}
		}

		private static void UpdateEnergyReqCurrentTick(DysonSphere __instance, long times)
		{
			if (times % Parameters.PowerUpdatePeriod == 0L)
			{
				int index = __instance.starData.index;
				if (index >= energyReqs.Length)
				{
					energyReqs = new long[GameMain.galaxy.stars.Length];
				}
				__instance.energyReqCurrentTick = energyReqs[index];
				energyReqs[index] = 0L;
			}
		}

		private static bool RequestDysonSpherePower_Guard(PowerSystem __instance)
		{
			return (__instance.factory.index + (int)GameMain.gameTick) % Parameters.PowerUpdatePeriod == 0;
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(PowerSystem), "RequestDysonSpherePower")]
		private static IEnumerable<CodeInstruction> RequestDysonSpherePower_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator iLGenerator)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Expected O, but got Unknown
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Expected O, but got Unknown
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Expected O, but got Unknown
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0093: Expected O, but got Unknown
			//IL_009b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a1: Expected O, but got Unknown
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Expected O, but got Unknown
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Expected O, but got Unknown
			//IL_0129: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Expected O, but got Unknown
			try
			{
				CodeMatcher val = new CodeMatcher(instructions, iLGenerator);
				Label label = default(Label);
				val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
				{
					new CodeMatch((OpCode?)OpCodes.Stfld, (object)AccessTools.Field(typeof(PowerSystem), "dysonSphere"), (string)null)
				}).Advance(1).CreateLabel(ref label)
					.Insert((CodeInstruction[])(object)new CodeInstruction[4]
					{
						new CodeInstruction(OpCodes.Ldarg_0, (object)null),
						new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(DysonReqPower_Patch), "RequestDysonSpherePower_Guard", (Type[])null, (Type[])null)),
						new CodeInstruction(OpCodes.Brtrue_S, (object)label),
						new CodeInstruction(OpCodes.Ret, (object)null)
					});
				val.End().MatchBack(false, (CodeMatch[])(object)new CodeMatch[2]
				{
					new CodeMatch((OpCode?)OpCodes.Dup, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldfld, (object)AccessTools.Field(typeof(DysonSphere), "energyReqCurrentTick"), (string)null)
				}).RemoveInstructions(2)
					.Advance(1)
					.RemoveInstructions(2)
					.Insert((CodeInstruction[])(object)new CodeInstruction[1]
					{
						new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(DysonReqPower_Patch), "CollectEnergyReqs", (Type[])null, (Type[])null))
					});
				return val.InstructionEnumeration();
			}
			catch (Exception obj)
			{
				Log.Error("Transpiler PowerSystem.RequestDysonSpherePower failed");
				Log.Error(obj);
				return instructions;
			}
		}

		private static void CollectEnergyReqs(DysonSphere dysonSphere, long value)
		{
			if (dysonSphere.starData.index < energyReqs.Length)
			{
				energyReqs[dysonSphere.starData.index] += value;
			}
		}
	}
	public class Facility_Patch
	{
		[HarmonyPrefix]
		[HarmonyPriority(600)]
		[HarmonyPatch(typeof(MinerComponent), "InternalUpdate")]
		[HarmonyPatch(typeof(AssemblerComponent), "InternalUpdate")]
		[HarmonyPatch(typeof(EjectorComponent), "InternalUpdate")]
		[HarmonyPatch(typeof(SiloComponent), "InternalUpdate")]
		private static void FacilitySpeedModify(ref float power)
		{
			if (power >= 0.1f)
			{
				power *= Parameters.FacilitySpeedRate;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(SiloComponent), "InternalUpdate")]
		private static void AnimPowerCorrection(ref SiloComponent __instance, float power, AnimData[] animPool)
		{
			if (power >= 0.1f)
			{
				animPool[__instance.entityId].power = power / Parameters.FacilitySpeedRate;
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(FactorySystem), "GameTick", new Type[]
		{
			typeof(long),
			typeof(bool),
			typeof(int),
			typeof(int),
			typeof(int)
		})]
		private static bool GameTick(FactorySystem __instance, long time, int _usedThreadCnt, int _curThreadIdx, int _minimumMissionCnt)
		{
			//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c8: Invalid comparison between Unknown and I4
			//IL_025b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0261: Invalid comparison between Unknown and I4
			if ((__instance.factory.index + time) % Parameters.FacilityUpdatePeriod == 0L)
			{
				return true;
			}
			AnimData[] entityAnimPool = __instance.factory.entityAnimPool;
			int num = default(int);
			int num2 = default(int);
			if (WorkerThreadExecutor.CalculateMissionIndex(1, __instance.fractionatorCursor - 1, _usedThreadCnt, _curThreadIdx, _minimumMissionCnt, ref num, ref num2))
			{
				FactoryProductionStat obj = GameMain.statistics.production.factoryStatPool[__instance.factory.index];
				int[] productRegister = obj.productRegister;
				int[] consumeRegister = obj.consumeRegister;
				SignData[] entitySignPool = __instance.factory.entitySignPool;
				for (int i = num; i < num2; i++)
				{
					if (__instance.fractionatorPool[i].id == i)
					{
						int entityId = __instance.fractionatorPool[i].entityId;
						float power = entityAnimPool[entityId].power;
						uint state = ((FractionatorComponent)(ref __instance.fractionatorPool[i])).InternalUpdate(__instance.factory, power, entitySignPool, productRegister, consumeRegister);
						entityAnimPool[entityId].time = Mathf.Sqrt((float)__instance.fractionatorPool[i].fluidInputCount * 0.025f);
						entityAnimPool[entityId].state = state;
					}
				}
			}
			if ((__instance.factory.index + time) % Parameters.BeltUpdatePeriod == 0L && WorkerThreadExecutor.CalculateMissionIndex(1, __instance.minerCursor - 1, _usedThreadCnt, _curThreadIdx, _minimumMissionCnt, ref num, ref num2))
			{
				float miningSpeedScale = GameMain.data.history.miningSpeedScale;
				byte b = default(byte);
				for (int j = num; j < num2; j++)
				{
					if (__instance.minerPool[j].id != j)
					{
						continue;
					}
					ref MinerComponent reference = ref __instance.minerPool[j];
					if (reference.productCount > 0 && reference.insertTarget > 0 && reference.productId > 0)
					{
						int num3 = 4;
						if ((int)reference.type == 2)
						{
							num3 = (int)(36000000f / (float)reference.period * miningSpeedScale * (float)reference.veinCount - 0.01f) / 1800 + 1;
							num3 = ((num3 >= 4) ? 4 : ((num3 < 1) ? 1 : num3));
						}
						int num4 = ((reference.productCount < num3) ? reference.productCount : num3);
						int num5 = __instance.factory.InsertInto(reference.insertTarget, 0, reference.productId, (byte)num4, (byte)0, ref b);
						reference.productCount -= num5;
						if (reference.productCount == 0 && (int)reference.type == 2)
						{
							reference.productId = 0;
						}
					}
				}
			}
			if (__instance.factory == GameMain.localPlanet?.factory)
			{
				if (WorkerThreadExecutor.CalculateMissionIndex(1, __instance.minerCursor - 1, _usedThreadCnt, _curThreadIdx, _minimumMissionCnt, ref num, ref num2))
				{
					for (int k = num; k < num2; k++)
					{
						if (__instance.minerPool[k].id == k)
						{
							int entityId2 = __instance.minerPool[k].entityId;
							ref AnimData reference2 = ref entityAnimPool[entityId2];
							int num6 = (int)Mathf.Floor(reference2.time / 10f);
							reference2.time %= 10f;
							((AnimData)(ref reference2)).Step(reference2.state, 1f / 60f);
							if (num6 > 0)
							{
								reference2.time += num6 * 10;
							}
						}
					}
				}
				if (WorkerThreadExecutor.CalculateMissionIndex(1, __instance.assemblerCursor - 1, _usedThreadCnt, _curThreadIdx, _minimumMissionCnt, ref num, ref num2))
				{
					for (int l = num; l < num2; l++)
					{
						if (__instance.assemblerPool[l].id == l)
						{
							int entityId3 = __instance.assemblerPool[l].entityId;
							ref AnimData reference3 = ref entityAnimPool[entityId3];
							((AnimData)(ref reference3)).Step(reference3.state, 1f / 60f);
						}
					}
				}
				int tickOffset = (__instance.factory.index + (int)time) % Parameters.FacilityUpdatePeriod - Parameters.FacilityUpdatePeriod;
				if (WorkerThreadExecutor.CalculateMissionIndex(1, __instance.ejectorCursor - 1, _usedThreadCnt, _curThreadIdx, _minimumMissionCnt, ref num, ref num2))
				{
					float[] networkServes = __instance.factory.powerSystem.networkServes;
					PowerConsumerComponent[] consumerPool = __instance.factory.powerSystem.consumerPool;
					for (int m = num; m < num2; m++)
					{
						if (__instance.ejectorPool[m].id == m)
						{
							int entityId4 = __instance.ejectorPool[m].entityId;
							ref AnimData animData = ref entityAnimPool[entityId4];
							float power2 = networkServes[consumerPool[__instance.ejectorPool[m].pcId].networkId];
							AnimUpdate(ref __instance.ejectorPool[m], ref animData, tickOffset, power2);
						}
					}
				}
				if (WorkerThreadExecutor.CalculateMissionIndex(1, __instance.siloCursor - 1, _usedThreadCnt, _curThreadIdx, _minimumMissionCnt, ref num, ref num2))
				{
					for (int n = num; n < num2; n++)
					{
						if (__instance.siloPool[n].id == n)
						{
							int entityId5 = __instance.siloPool[n].entityId;
							ref AnimData animData2 = ref entityAnimPool[entityId5];
							AnimUpdate(ref __instance.siloPool[n], ref animData2, tickOffset);
						}
					}
				}
			}
			return false;
		}

		private static void AnimUpdate(ref EjectorComponent ejector, ref AnimData animData, int tickOffset, float power)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Invalid comparison between Unknown and I4
			if ((int)ejector.targetState != 1)
			{
				return;
			}
			if (ejector.fired)
			{
				animData.time += 1f / 60f;
				if (animData.time >= 11f)
				{
					ejector.fired = false;
					animData.time = 0f;
				}
			}
			else if (ejector.direction != 0)
			{
				float num = (float)Cargo.accTableMilli[((EjectorComponent)(ref ejector)).incLevel];
				int num2 = (int)(power * 10000f * (1f + num) + 0.1f) * tickOffset;
				if (ejector.boost)
				{
					num2 *= 10;
				}
				if (ejector.direction > 0)
				{
					float num3 = (float)(ejector.time + num2) / (float)ejector.chargeSpend;
					animData.time = Mathf.Min(num3, 1f);
				}
				else
				{
					float num4 = (float)(-(ejector.time - num2)) / (float)ejector.coldSpend;
					animData.time = Mathf.Min(num4, 0f);
				}
			}
		}

		private static void AnimUpdate(ref SiloComponent silo, ref AnimData animData, int tickOffset)
		{
			if (silo.direction != 0)
			{
				float power = animData.power;
				float num = (float)Cargo.accTableMilli[((SiloComponent)(ref silo)).incLevel];
				int num2 = (int)(power * 10000f * (1f + num) + 0.1f) * tickOffset;
				if (silo.boost)
				{
					num2 *= 10;
				}
				if (silo.direction > 0)
				{
					float num3 = (float)(silo.time + num2) / (float)silo.chargeSpend;
					animData.time = Mathf.Min(num3, 1f);
				}
				else
				{
					float num4 = (float)(-(silo.time - num2)) / (float)silo.coldSpend;
					animData.time = Mathf.Min(num4, 0f);
				}
			}
		}
	}
	public class GameData_Patch
	{
		private static PlanetFactory[] powerFactories = (PlanetFactory[])(object)new PlanetFactory[0];

		private static int powerFactoryCount;

		private static int researchLabFactoryCount;

		private static int liftLabFactoryCount;

		private static int inserterFactoryCount;

		private static int storageFacotryCount;

		private static int beltFactoryCount;

		private static int beltAddonFactoryCount;

		[HarmonyPrefix]
		[HarmonyPatch(typeof(GameData), "GameTick")]
		private static void GameTick_Prefix(GameData __instance, long time)
		{
			if (__instance.factories.Length != powerFactories.Length)
			{
				powerFactories = (PlanetFactory[])(object)new PlanetFactory[__instance.factories.Length];
			}
			PlanetFactory val = GameMain.localPlanet?.factory;
			powerFactoryCount = 0;
			researchLabFactoryCount = 0;
			for (int i = 0; i < __instance.factoryCount; i++)
			{
				if ((__instance.factories[i].index + (int)time) % Parameters.PowerUpdatePeriod == 0)
				{
					powerFactories[powerFactoryCount++] = __instance.factories[i];
				}
			}
			if (val != null && (val.index + (int)time) % Parameters.PowerUpdatePeriod != 0)
			{
				powerFactories[powerFactoryCount++] = val;
			}
			researchLabFactoryCount = ((time % Parameters.LabResearchUpdatePeriod == 0L) ? __instance.factoryCount : 0);
			liftLabFactoryCount = ((time % Parameters.LabLiftUpdatePeriod == 0L) ? __instance.factoryCount : 0);
			inserterFactoryCount = ((time % Parameters.InserterUpdatePeriod == 0L) ? __instance.factoryCount : 0);
			storageFacotryCount = ((time % Parameters.StorageUpdatePeriod == 0L) ? __instance.factoryCount : 0);
			beltFactoryCount = 0;
			beltAddonFactoryCount = 0;
			if (time % Parameters.BeltUpdatePeriod == 0L)
			{
				beltFactoryCount = __instance.factoryCount;
				beltAddonFactoryCount = __instance.factoryCount;
			}
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(GameData), "GameTick")]
		private static IEnumerable<CodeInstruction> GameTick_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Expected O, but got Unknown
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Expected O, but got Unknown
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c5: Expected O, but got Unknown
			//IL_0121: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Expected O, but got Unknown
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Expected O, but got Unknown
			//IL_0186: Unknown result type (might be due to invalid IL or missing references)
			//IL_018c: Expected O, but got Unknown
			//IL_0259: Unknown result type (might be due to invalid IL or missing references)
			//IL_025f: Expected O, but got Unknown
			//IL_01d7: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dd: Expected O, but got Unknown
			//IL_0281: Unknown result type (might be due to invalid IL or missing references)
			//IL_0287: Expected O, but got Unknown
			//IL_01ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0205: Expected O, but got Unknown
			//IL_0447: Unknown result type (might be due to invalid IL or missing references)
			//IL_044d: Expected O, but got Unknown
			//IL_02be: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c4: Expected O, but got Unknown
			//IL_046f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0475: Expected O, but got Unknown
			//IL_034e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0354: Expected O, but got Unknown
			//IL_02e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ec: Expected O, but got Unknown
			//IL_04ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_04b2: Expected O, but got Unknown
			//IL_0376: Unknown result type (might be due to invalid IL or missing references)
			//IL_037c: Expected O, but got Unknown
			//IL_04d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_04da: Expected O, but got Unknown
			//IL_03b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b9: Expected O, but got Unknown
			//IL_052a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0530: Expected O, but got Unknown
			//IL_03db: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e1: Expected O, but got Unknown
			//IL_0552: Unknown result type (might be due to invalid IL or missing references)
			//IL_0558: Expected O, but got Unknown
			try
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldc_I4_8), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "BeginSample"), (string)null)
				});
				int pos = val.Pos;
				val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldc_I4_S && (sbyte)i.operand == 12), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "BeginSample"), (string)null)
				});
				int pos2 = val.Pos;
				ReplaceFactories(val, pos, pos2, "powerFactories", "powerFactoryCount");
				if (Parameters.LabResearchUpdatePeriod > 1 || Parameters.LabLiftUpdatePeriod > 1)
				{
					val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
					{
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldc_I4_S && (sbyte)i.operand == 17), (string)null),
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "BeginSample"), (string)null)
					});
					pos = val.Pos;
					val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
					{
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "get_multithreadSystem"), (string)null)
					});
					pos2 = val.Pos;
					ReplaceFactories(val, pos, pos2, "", "researchLabFactoryCount");
					pos = pos2;
					val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
					{
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldc_I4_S && (sbyte)i.operand == 17), (string)null),
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "EndSample"), (string)null)
					});
					pos2 = val.Pos;
					ReplaceFactories(val, pos, pos2, "", "liftLabFactoryCount");
				}
				if (Parameters.InserterUpdatePeriod > 1)
				{
					val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
					{
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldc_I4_S && (sbyte)i.operand == 10), (string)null),
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "BeginSample"), (string)null)
					});
					pos = val.Pos;
					val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
					{
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldc_I4_S && (sbyte)i.operand == 10), (string)null),
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "EndSample"), (string)null)
					});
					pos2 = val.Pos;
					ReplaceFactories(val, pos, pos2, "", "inserterFactoryCount");
				}
				if (Parameters.StorageUpdatePeriod > 1)
				{
					val.Start();
					for (int j = 0; j < 3; j++)
					{
						val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
						{
							new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldc_I4_S && (sbyte)i.operand == 19), (string)null),
							new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "BeginSample"), (string)null)
						});
						pos = val.Pos;
						val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
						{
							new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldc_I4_S && (sbyte)i.operand == 19), (string)null),
							new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "EndSample"), (string)null)
						});
						pos2 = val.Pos;
						ReplaceFactories(val, pos, pos2, "", "storageFacotryCount");
					}
				}
				if (Parameters.BeltUpdatePeriod > 1)
				{
					val.Start();
					val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
					{
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldc_I4_S && (sbyte)i.operand == 9), (string)null),
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "BeginSample"), (string)null)
					});
					pos = val.Pos;
					val.MatchForward(true, (CodeMatch[])(object)new CodeMatch[2]
					{
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldc_I4_S && (sbyte)i.operand == 9), (string)null),
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "EndSample"), (string)null)
					});
					pos2 = val.Pos;
					ReplaceFactories(val, pos, pos2, "", "beltFactoryCount");
					pos = val.Pos;
					val.MatchForward(true, (CodeMatch[])(object)new CodeMatch[2]
					{
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldc_I4_S && (sbyte)i.operand == 19), (string)null),
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "BeginSample"), (string)null)
					});
					pos2 = val.Pos;
					ReplaceFactories(val, pos, pos2, "", "beltAddonFactoryCount");
				}
				return val.InstructionEnumeration();
			}
			catch
			{
				Log.Error("Transpiler GameData.GameTick failed");
				return instructions;
			}
		}

		public static void ReplaceFactories(CodeMatcher codeMatcher, int start, int end, string factoriesName, string factoryCountName)
		{
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Expected O, but got Unknown
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Expected O, but got Unknown
			if (factoriesName != "")
			{
				FieldInfo fieldInfo = AccessTools.Field(typeof(GameData_Patch), factoriesName);
				codeMatcher.Start().Advance(start);
				while (true)
				{
					codeMatcher.MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
					{
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "factories"), (string)null)
					});
					if (codeMatcher.IsInvalid || codeMatcher.Pos >= end)
					{
						break;
					}
					codeMatcher.Advance(-1).SetAndAdvance(OpCodes.Nop, (object)null).SetAndAdvance(OpCodes.Ldsfld, (object)fieldInfo);
				}
			}
			FieldInfo fieldInfo2 = AccessTools.Field(typeof(GameData_Patch), factoryCountName);
			codeMatcher.Start().Advance(start);
			while (true)
			{
				codeMatcher.MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "factoryCount"), (string)null)
				});
				if (codeMatcher.IsInvalid || codeMatcher.Pos >= end)
				{
					break;
				}
				codeMatcher.Advance(-1).SetAndAdvance(OpCodes.Nop, (object)null).SetAndAdvance(OpCodes.Ldsfld, (object)fieldInfo2);
			}
			codeMatcher.Start().Advance(end);
		}
	}
	public class Inserter_Patch
	{
		[HarmonyPrefix]
		[HarmonyPriority(600)]
		[HarmonyPatch(typeof(FactorySystem), "GameTickInserters", new Type[]
		{
			typeof(long),
			typeof(bool),
			typeof(int),
			typeof(int)
		})]
		private static void GameTickInserters_Prefix(ref long time)
		{
			time /= Parameters.InserterUpdatePeriod;
		}

		[HarmonyPrefix]
		[HarmonyPriority(600)]
		[HarmonyPatch(typeof(InserterComponent), "InternalUpdate")]
		[HarmonyPatch(typeof(InserterComponent), "InternalUpdateNoAnim")]
		private static void InternalUpdatePrefix(ref float power)
		{
			if (power >= 0.1f)
			{
				power *= Parameters.InserterSpeedRate;
			}
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(InserterComponent), "InternalUpdate")]
		[HarmonyPatch(typeof(InserterComponent), "InternalUpdateNoAnim")]
		private static IEnumerable<CodeInstruction> InputUpdate_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Expected O, but got Unknown
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Expected O, but got Unknown
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			try
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[4]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdloc(i, (LocalBuilder)null)), (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldc_I4_1, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Sub, (object)null, (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Stfld && ((FieldInfo)i.operand).Name == "idleTick"), (string)null)
				}).Repeat((Action<CodeMatcher>)delegate(CodeMatcher matcher)
				{
					matcher.Advance(1).SetAndAdvance(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(Parameters), "InserterUpdatePeriod"));
				}, (Action<string>)null);
				return val.InstructionEnumeration();
			}
			catch (Exception obj)
			{
				Log.Error("Transpiler InserterComponent.InternalUpdate failed");
				Log.Error(obj);
				return instructions;
			}
		}
	}
	public class LabLift_Patch
	{
		[HarmonyPrefix]
		[HarmonyPatch(typeof(LabComponent), "UpdateOutputToNext")]
		private static bool IncreaseLiftThroughput(ref LabComponent __instance, LabComponent[] labPool)
		{
			if (!Plugin.Enable)
			{
				return true;
			}
			ref LabComponent reference = ref labPool[__instance.nextLabId];
			if (reference.id == 0 || reference.id != __instance.nextLabId)
			{
				__instance.nextLabId = 0;
				return false;
			}
			if (reference.needs != null && __instance.recipeId == reference.recipeId && __instance.techId == reference.techId)
			{
				if (__instance.matrixServed != null && reference.matrixServed != null)
				{
					int[] obj = ((__instance.entityId > reference.entityId) ? __instance.matrixServed : reference.matrixServed);
					int[] array = ((__instance.entityId > reference.entityId) ? reference.matrixServed : __instance.matrixServed);
					lock (obj)
					{
						lock (array)
						{
							for (int i = 0; i < 6; i++)
							{
								if (__instance.matrixServed[i] >= 36000 && reference.needs[i] == 6001 + i)
								{
									int num = 3600 * (__instance.matrixServed[i] / 7200);
									int num2 = ((LabComponent)(ref __instance)).split_inc(ref __instance.matrixServed[i], ref __instance.matrixIncServed[i], num);
									reference.matrixIncServed[i] += num2;
									reference.matrixServed[i] += num;
									__instance.needs[i] = ((__instance.matrixServed[i] < 36000) ? (6001 + i) : 0);
									reference.needs[0] = ((reference.matrixServed[i] < 36000) ? (6001 + i) : 0);
								}
							}
						}
					}
				}
				else if (__instance.served != null && reference.served != null)
				{
					int[] obj2 = ((__instance.entityId > reference.entityId) ? __instance.served : reference.served);
					int[] array2 = ((__instance.entityId > reference.entityId) ? reference.served : __instance.served);
					lock (obj2)
					{
						lock (array2)
						{
							int num3 = __instance.served.Length;
							for (int j = 0; j < num3; j++)
							{
								if (__instance.needs[j] == 0 && reference.needs[j] == __instance.requires[j] && __instance.served[j] >= 1)
								{
									int num4 = __instance.served[j] / 2 + 1;
									int num5 = num4 * __instance.incServed[j] / __instance.served[j];
									__instance.served[j] -= num4;
									__instance.incServed[j] -= num5;
									reference.served[j] += num4;
									reference.incServed[j] += num5;
									__instance.needs[j] = ((__instance.served[j] < 4) ? __instance.requires[j] : 0);
									reference.needs[j] = ((reference.served[j] < 4) ? reference.requires[j] : 0);
								}
							}
						}
					}
				}
				if (__instance.produced != null && reference.produced != null)
				{
					int[] obj3 = ((__instance.entityId > reference.entityId) ? __instance.produced : reference.produced);
					int[] array3 = ((__instance.entityId > reference.entityId) ? reference.produced : __instance.produced);
					lock (obj3)
					{
						lock (array3)
						{
							if (__instance.produced[0] < 9 && reference.produced[0] > 0)
							{
								int num6 = 9 - __instance.produced[0];
								num6 = ((reference.produced[0] < num6) ? reference.produced[0] : num6);
								__instance.produced[0] += num6;
								reference.produced[0] -= num6;
							}
						}
					}
				}
			}
			return false;
		}
	}
	public class LabProduce_Patch
	{
		[HarmonyPrefix]
		[HarmonyPriority(600)]
		[HarmonyPatch(typeof(LabComponent), "InternalUpdateAssemble")]
		private static void FacilitySpeedModify(ref float power)
		{
			if (power >= 0.1f)
			{
				power *= Parameters.LabProduceUpdatePeriod;
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(FactorySystem), "GameTickLabProduceMode", new Type[]
		{
			typeof(long),
			typeof(bool),
			typeof(int),
			typeof(int),
			typeof(int)
		})]
		private static bool LocalAnim_Lab(FactorySystem __instance, int _usedThreadCnt, int _curThreadIdx, int _minimumMissionCnt)
		{
			if ((__instance.factory.index + GameMain.gameTick) % Parameters.LabProduceUpdatePeriod == 0L)
			{
				return true;
			}
			int num = default(int);
			int num2 = default(int);
			if (__instance.factory == GameMain.localPlanet?.factory && WorkerThreadExecutor.CalculateMissionIndex(1, __instance.labCursor - 1, _usedThreadCnt, _curThreadIdx, _minimumMissionCnt, ref num, ref num2))
			{
				AnimData[] entityAnimPool = __instance.factory.entityAnimPool;
				for (int i = num; i < num2; i++)
				{
					if (__instance.labPool[i].id == i)
					{
						ref AnimData reference = ref entityAnimPool[__instance.labPool[i].entityId];
						((AnimData)(ref reference)).Step01(reference.state, 1f / 60f);
					}
				}
			}
			return false;
		}
	}
	public class LabResearch_Patch
	{
		private static int techHashedThisFrame;

		[HarmonyPrefix]
		[HarmonyPriority(600)]
		[HarmonyPatch(typeof(LabComponent), "InternalUpdateResearch")]
		private static void ResearchSpeedModify(ref float speed)
		{
			speed *= Parameters.LabResearchUpdatePeriod;
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(GameStatData), "RecordTechHashed")]
		private static void RecordTechHashed(GameStatData __instance)
		{
			techHashedThisFrame = 0;
			for (int i = 0; i < Parameters.LabResearchUpdatePeriod; i++)
			{
				techHashedThisFrame += __instance.techHashedHistory[i];
			}
			techHashedThisFrame /= Parameters.LabResearchUpdatePeriod;
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(UITechNode), "UpdateInfoDynamic")]
		[HarmonyPatch(typeof(UITechTip), "UpdateInfoDynamic")]
		private static IEnumerable<CodeInstruction> UpdateInfoDynamic_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Expected O, but got Unknown
			try
			{
				return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdloc(i, (LocalBuilder)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "techHashedThisFrame"), (string)null)
				}).Repeat((Action<CodeMatcher>)delegate(CodeMatcher matcher)
				{
					matcher.SetAndAdvance(OpCodes.Nop, (object)null).SetAndAdvance(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(LabResearch_Patch), "techHashedThisFrame"));
				}, (Action<string>)null).InstructionEnumeration();
			}
			catch
			{
				Log.Warn("Transpiler UpdateInfoDynamic failed");
				return instructions;
			}
		}
	}
	public class PowerSystem_Patch
	{
		private const float DELTA_TIME = 1f / 60f;

		[HarmonyPrefix]
		[HarmonyPatch(typeof(PowerSystem), "GameTick")]
		private static bool GameTick(PowerSystem __instance, ref long time)
		{
			if (__instance.factory != GameMain.localPlanet?.factory || (__instance.factory.index + (int)time) % Parameters.PowerUpdatePeriod == 0)
			{
				time /= Parameters.PowerUpdatePeriod;
				return true;
			}
			EntityData[] entityPool = __instance.factory.entityPool;
			AnimData[] entityAnimPool = __instance.factory.entityAnimPool;
			PowerGeneratorComponent[] genPool = __instance.genPool;
			for (int i = 1; i < __instance.genCursor; i++)
			{
				if (genPool[i].id != i)
				{
					continue;
				}
				int entityId = genPool[i].entityId;
				ref AnimData reference = ref entityAnimPool[entityId];
				if (genPool[i].wind)
				{
					((AnimData)(ref reference)).Step2(reference.state, 1f / 60f, reference.power, 0.7f);
				}
				else if (genPool[i].gamma)
				{
					reference.time += 1f / 60f;
					if (reference.time > 1f)
					{
						reference.time -= 1f;
					}
				}
				else if (genPool[i].fuelMask > 1)
				{
					((AnimData)(ref reference)).Step2(reference.state, 1f / 60f, reference.power, 2f);
				}
				else if (genPool[i].geothermal)
				{
					((AnimData)(ref reference)).Step(reference.state, 1f / 60f, 2f, 0f);
				}
				else
				{
					((AnimData)(ref reference)).Step2(reference.state, 1f / 60f, reference.power, 1f);
				}
			}
			Mecha mecha = GameMain.mainPlayer.mecha;
			for (int j = 1; j < __instance.nodeCursor; j++)
			{
				if (__instance.nodePool[j].id != j)
				{
					continue;
				}
				int entityId2 = __instance.nodePool[j].entityId;
				int networkId = __instance.nodePool[j].networkId;
				ref AnimData reference2 = ref entityAnimPool[entityId2];
				if (__instance.nodePool[j].isCharger)
				{
					int num = __instance.nodePool[j].requiredEnergy - __instance.nodePool[j].idleEnergyPerTick;
					float num2 = __instance.networkServes[networkId];
					if (__instance.nodePool[j].coverRadius < 15f)
					{
						((AnimData)(ref reference2)).StepPoweredClamped(num2, 1f / 60f, (num <= 0) ? 1u : 2u);
					}
					else
					{
						((AnimData)(ref reference2)).StepPoweredClamped2(num2, 1f / 60f, (num <= 0) ? 1u : 2u);
					}
					if (num > 0 && entityAnimPool[entityId2].state == 2)
					{
						num = (int)(num2 * (float)num);
						mecha.coreEnergy += (double)num;
						mecha.MarkEnergyChange(2, (double)num);
						mecha.AddChargerDevice(entityId2);
						if (mecha.coreEnergy > mecha.coreEnergyCap)
						{
							mecha.coreEnergy = mecha.coreEnergyCap;
						}
					}
				}
				else if (entityPool[entityId2].powerGenId == 0 && entityPool[entityId2].powerAccId == 0 && entityPool[entityId2].powerExcId == 0)
				{
					((AnimData)(ref reference2)).Step2(reference2.state, 1f / 60f, reference2.power, 0.4f);
				}
			}
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(ProductionStatistics), "PrepareTick")]
		private static bool PrepareTick(ProductionStatistics __instance)
		{
			for (int i = 0; i < __instance.gameData.factoryCount; i++)
			{
				FactoryProductionStat val = __instance.factoryStatPool[i];
				Array.Clear(val.productRegister, 0, 12000);
				Array.Clear(val.consumeRegister, 0, 12000);
				if ((i + GameMain.gameTick) % Parameters.PowerUpdatePeriod == 0L)
				{
					val.powerGenRegister = 0L;
					val.powerConRegister = 0L;
					val.powerDisRegister = 0L;
					val.powerChaRegister = 0L;
				}
				val.hashRegister = 0L;
				__instance.factoryStatPool[i].itemChanged = false;
			}
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(PowerExchangerComponent), "StateUpdate")]
		private static bool StateUpdate(ref PowerExchangerComponent __instance)
		{
			if (__instance.state < __instance.targetState)
			{
				__instance.state += 0.00557f * (float)Parameters.PowerUpdatePeriod * 5f;
				if (__instance.state >= __instance.targetState)
				{
					__instance.state = __instance.targetState;
				}
			}
			else if (__instance.state > __instance.targetState)
			{
				__instance.state -= 0.00557f * (float)Parameters.PowerUpdatePeriod * 5f;
				if (__instance.state <= __instance.targetState)
				{
					__instance.state = __instance.targetState;
				}
			}
			return false;
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(PowerExchangerComponent), "InputUpdate")]
		private static IEnumerable<CodeInstruction> InputUpdate_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Expected O, but got Unknown
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Expected O, but got Unknown
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Expected O, but got Unknown
			//IL_00e6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Expected O, but got Unknown
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Expected O, but got Unknown
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0132: Expected O, but got Unknown
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Expected O, but got Unknown
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Expected O, but got Unknown
			//IL_0183: Unknown result type (might be due to invalid IL or missing references)
			//IL_0189: Expected O, but got Unknown
			//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Expected O, but got Unknown
			//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d9: Expected O, but got Unknown
			//IL_01fb: Unknown result type (might be due to invalid IL or missing references)
			//IL_0201: Expected O, but got Unknown
			//IL_020f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0215: Expected O, but got Unknown
			//IL_0237: Unknown result type (might be due to invalid IL or missing references)
			//IL_023d: Expected O, but got Unknown
			//IL_0269: Unknown result type (might be due to invalid IL or missing references)
			//IL_026f: Expected O, but got Unknown
			//IL_0277: Unknown result type (might be due to invalid IL or missing references)
			//IL_027d: Expected O, but got Unknown
			//IL_0285: Unknown result type (might be due to invalid IL or missing references)
			//IL_028b: Expected O, but got Unknown
			//IL_02a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_02aa: Expected O, but got Unknown
			//IL_02c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_02cb: Expected O, but got Unknown
			//IL_0307: Unknown result type (might be due to invalid IL or missing references)
			//IL_030d: Expected O, but got Unknown
			try
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[7]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdloc(i, (LocalBuilder)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdarg(i, (int?)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "maxPoolEnergy"), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdarg(i, (int?)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "currPoolEnergy"), (string)null),
					new CodeMatch((OpCode?)OpCodes.Sub, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Blt, (object)null, (string)null)
				}).Advance(1).Insert((CodeInstruction[])(object)new CodeInstruction[3]
				{
					new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(Parameters), "PowerUpdatePeriod")),
					new CodeInstruction(OpCodes.Conv_I8, (object)null),
					new CodeInstruction(OpCodes.Mul, (object)null)
				});
				val.End().MatchBack(false, (CodeMatch[])(object)new CodeMatch[6]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdarg(i, (int?)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdarg(i, (int?)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "currPoolEnergy"), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdloc(i, (LocalBuilder)null)), (string)null),
					new CodeMatch((OpCode?)OpCodes.Add, (object)null, (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Stfld && ((FieldInfo)i.operand).Name == "currPoolEnergy"), (string)null)
				}).Advance(4)
					.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[3]
					{
						new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(Parameters), "PowerUpdatePeriod")),
						new CodeInstruction(OpCodes.Conv_I8, (object)null),
						new CodeInstruction(OpCodes.Mul, (object)null)
					})
					.Advance(1)
					.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[3]
					{
						new CodeInstruction(OpCodes.Ldarg_0, (object)null),
						new CodeInstruction(OpCodes.Ldfld, (object)AccessTools.Field(typeof(PowerExchangerComponent), "maxPoolEnergy")),
						new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(Math), "Min", new Type[2]
						{
							typeof(long),
							typeof(long)
						}, (Type[])null))
					});
				return val.InstructionEnumeration();
			}
			catch (Exception obj)
			{
				Log.Error("Transpiler PowerExchangerComponent.InputUpdate failed");
				Log.Error(obj);
				return instructions;
			}
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(PowerExchangerComponent), "OutputUpdate")]
		private static IEnumerable<CodeInstruction> OutputUpdate_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Expected O, but got Unknown
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			//IL_0096: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Expected O, but got Unknown
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Expected O, but got Unknown
			//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dc: Expected O, but got Unknown
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Expected O, but got Unknown
			//IL_011f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Expected O, but got Unknown
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_014d: Expected O, but got Unknown
			//IL_016f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Expected O, but got Unknown
			//IL_0197: Unknown result type (might be due to invalid IL or missing references)
			//IL_019d: Expected O, but got Unknown
			//IL_01ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b1: Expected O, but got Unknown
			//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d9: Expected O, but got Unknown
			//IL_0205: Unknown result type (might be due to invalid IL or missing references)
			//IL_020b: Expected O, but got Unknown
			//IL_0213: Unknown result type (might be due to invalid IL or missing references)
			//IL_0219: Expected O, but got Unknown
			//IL_0221: Unknown result type (might be due to invalid IL or missing references)
			//IL_0227: Expected O, but got Unknown
			//IL_0240: Unknown result type (might be due to invalid IL or missing references)
			//IL_0246: Expected O, but got Unknown
			//IL_024e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0254: Expected O, but got Unknown
			//IL_0290: Unknown result type (might be due to invalid IL or missing references)
			//IL_0296: Expected O, but got Unknown
			try
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[4]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdloc(i, (LocalBuilder)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdarg(i, (int?)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "currPoolEnergy"), (string)null),
					new CodeMatch((OpCode?)OpCodes.Blt, (object)null, (string)null)
				}).Advance(1).Insert((CodeInstruction[])(object)new CodeInstruction[3]
				{
					new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(Parameters), "PowerUpdatePeriod")),
					new CodeInstruction(OpCodes.Conv_I8, (object)null),
					new CodeInstruction(OpCodes.Mul, (object)null)
				});
				val.End().MatchBack(false, (CodeMatch[])(object)new CodeMatch[6]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdarg(i, (int?)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdarg(i, (int?)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "currPoolEnergy"), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdloc(i, (LocalBuilder)null)), (string)null),
					new CodeMatch((OpCode?)OpCodes.Sub, (object)null, (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Stfld && ((FieldInfo)i.operand).Name == "currPoolEnergy"), (string)null)
				}).Advance(4)
					.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[3]
					{
						new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(Parameters), "FacilityUpdatePeriod")),
						new CodeInstruction(OpCodes.Conv_I8, (object)null),
						new CodeInstruction(OpCodes.Mul, (object)null)
					})
					.Advance(1)
					.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[3]
					{
						new CodeInstruction(OpCodes.Ldc_I4_0, (object)null),
						new CodeInstruction(OpCodes.Conv_I8, (object)null),
						new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(Math), "Max", new Type[2]
						{
							typeof(long),
							typeof(long)
						}, (Type[])null))
					});
				return val.InstructionEnumeration();
			}
			catch (Exception obj)
			{
				Log.Error("Transpiler PowerExchangerComponent.OutputUpdate failed");
				Log.Error(obj);
				return instructions;
			}
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(PowerGeneratorComponent), "GameTick_Gamma")]
		private static IEnumerable<CodeInstruction> GameTick_Gamma_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Expected O, but got Unknown
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Expected O, but got Unknown
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b0: Expected O, but got Unknown
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Expected O, but got Unknown
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Expected O, but got Unknown
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_010a: Expected O, but got Unknown
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: Expected O, but got Unknown
			//IL_0148: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Expected O, but got Unknown
			//IL_0170: Unknown result type (might be due to invalid IL or missing references)
			//IL_0176: Expected O, but got Unknown
			//IL_0198: Unknown result type (might be due to invalid IL or missing references)
			//IL_019e: Expected O, but got Unknown
			//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b2: Expected O, but got Unknown
			//IL_01c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c6: Expected O, but got Unknown
			//IL_01e8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ee: Expected O, but got Unknown
			//IL_0242: Unknown result type (might be due to invalid IL or missing references)
			//IL_0248: Expected O, but got Unknown
			//IL_026a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0270: Expected O, but got Unknown
			//IL_0292: Unknown result type (might be due to invalid IL or missing references)
			//IL_0298: Expected O, but got Unknown
			//IL_02ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c0: Expected O, but got Unknown
			//IL_02e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02e8: Expected O, but got Unknown
			//IL_02f6: Unknown result type (might be due to invalid IL or missing references)
			//IL_02fc: Expected O, but got Unknown
			//IL_0328: Unknown result type (might be due to invalid IL or missing references)
			//IL_032e: Expected O, but got Unknown
			//IL_0336: Unknown result type (might be due to invalid IL or missing references)
			//IL_033c: Expected O, but got Unknown
			//IL_0344: Unknown result type (might be due to invalid IL or missing references)
			//IL_034a: Expected O, but got Unknown
			try
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[6]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdarg(i, (int?)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "catalystIncPoint"), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdarg(i, (int?)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "catalystPoint"), (string)null),
					new CodeMatch((OpCode?)OpCodes.Div, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Stloc_S, (object)null, (string)null)
				}).Advance(2).InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[2]
				{
					new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(Parameters), "FacilityUpdatePeriod")),
					new CodeInstruction(OpCodes.Mul, (object)null)
				});
				val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[6]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdarg(i, (int?)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdarg(i, (int?)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "catalystPoint"), (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldc_I4_1, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Sub, (object)null, (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Stfld && ((FieldInfo)i.operand).Name == "catalystPoint"), (string)null)
				}).Advance(3).SetAndAdvance(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(Parameters), "PowerUpdatePeriod"));
				val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[6]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdarg(i, (int?)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdarg(i, (int?)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "productCount"), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => CodeInstructionExtensions.IsLdarg(i, (int?)null)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "capacityCurrentTick"), (string)null),
					new CodeMatch((OpCode?)OpCodes.Conv_R8, (object)null, (string)null)
				}).Advance(6).Insert((CodeInstruction[])(object)new CodeInstruction[3]
				{
					new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(Parameters), "PowerUpdatePeriod")),
					new CodeInstruction(OpCodes.Conv_R8, (object)null),
					new CodeInstruction(OpCodes.Mul, (object)null)
				});
				return val.InstructionEnumeration();
			}
			catch (Exception obj)
			{
				Log.Error("Transpiler PowerGeneratorComponent.GameTick_Gamma failed");
				Log.Error(obj);
				return instructions;
			}
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(PowerGeneratorComponent), "GenEnergyByFuel")]
		private static IEnumerable<CodeInstruction> GenEnergyByFuel_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Expected O, but got Unknown
			//IL_0046: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Expected O, but got Unknown
			//IL_005a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Expected O, but got Unknown
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Expected O, but got Unknown
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Expected O, but got Unknown
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Expected O, but got Unknown
			//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Expected O, but got Unknown
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Expected O, but got Unknown
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d8: Expected O, but got Unknown
			try
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[4]
				{
					new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "fuelEnergy"), (string)null),
					new CodeMatch((OpCode?)OpCodes.Ldloc_0, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Blt, (object)null, (string)null)
				}).Insert((CodeInstruction[])(object)new CodeInstruction[5]
				{
					new CodeInstruction(OpCodes.Ldloc_0, (object)null),
					new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(Parameters), "PowerUpdatePeriod")),
					new CodeInstruction(OpCodes.Conv_I8, (object)null),
					new CodeInstruction(OpCodes.Mul, (object)null),
					new CodeInstruction(OpCodes.Stloc_0, (object)null)
				});
				return val.InstructionEnumeration();
			}
			catch (Exception obj)
			{
				Log.Error("Transpiler PowerGeneratorComponent.GenEnergyByFuel failed");
				Log.Error(obj);
				return instructions;
			}
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(PowerSystem), "GameTick")]
		private static IEnumerable<CodeInstruction> PowerSystemGameTick_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Expected O, but got Unknown
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Expected O, but got Unknown
			//IL_0076: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Expected O, but got Unknown
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Expected O, but got Unknown
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Expected O, but got Unknown
			try
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				val.End().MatchBack(false, (CodeMatch[])(object)new CodeMatch[2]
				{
					new CodeMatch((OpCode?)OpCodes.Add, (object)null, (string)null),
					new CodeMatch((OpCode?)OpCodes.Stfld, (object)AccessTools.Field(typeof(FactoryProductionStat), "energyConsumption"), (string)null)
				}).Insert((CodeInstruction[])(object)new CodeInstruction[3]
				{
					new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(Parameters), "PowerUpdatePeriod")),
					new CodeInstruction(OpCodes.Conv_I8, (object)null),
					new CodeInstruction(OpCodes.Mul, (object)null)
				});
				return val.InstructionEnumeration();
			}
			catch (Exception obj)
			{
				Log.Error("Transpiler PowerSystem.GameTick failed");
				Log.Error(obj);
				return instructions;
			}
		}
	}
	public class UIProgress_Patch
	{
		private static int factoryIndex;

		private static float progress0;

		private static float progress1;

		private static float extraProgress0;

		private static float extraProgress1;

		private static int lastTick;

		private static int lastExtraTick;

		public static float GetFillAmount(float progress)
		{
			if (Parameters.FacilityUpdatePeriod == 1)
			{
				return progress;
			}
			int num = (factoryIndex + (int)GameMain.gameTick) % Parameters.FacilityUpdatePeriod;
			if (num < lastTick)
			{
				progress0 = progress1;
				progress1 = progress;
				if (progress1 < progress0)
				{
					progress0 -= 1f;
				}
			}
			lastTick = num;
			return Mathf.Clamp01(Mathf.Lerp(progress0, progress1, (float)num / (float)Parameters.FacilityUpdatePeriod));
		}

		public static float GetExtraFillAmount(float progress)
		{
			if (Parameters.FacilityUpdatePeriod == 1)
			{
				return progress;
			}
			int num = (factoryIndex + (int)GameMain.gameTick) % Parameters.FacilityUpdatePeriod;
			if (num < lastExtraTick)
			{
				extraProgress0 = extraProgress1;
				extraProgress1 = progress;
				if (extraProgress1 < extraProgress0)
				{
					extraProgress0 -= 1f;
				}
			}
			lastExtraTick = num;
			return Mathf.Clamp01(Mathf.Lerp(extraProgress0, extraProgress1, (float)num / (float)Parameters.FacilityUpdatePeriod));
		}

		public static float GetCoolDown(float progress)
		{
			if (Parameters.FacilityUpdatePeriod == 1)
			{
				return progress;
			}
			int num = (factoryIndex + (int)GameMain.gameTick) % Parameters.FacilityUpdatePeriod;
			if (num < lastTick)
			{
				progress0 = progress1;
				progress1 = Mathf.Clamp01(progress);
			}
			lastTick = num;
			return Mathf.Lerp(progress0, progress1, (float)num / (float)Parameters.FacilityUpdatePeriod);
		}

		public static float GetFuelAmount(float progress)
		{
			if (Parameters.PowerUpdatePeriod == 1)
			{
				return progress;
			}
			int num = (factoryIndex + (int)GameMain.gameTick) % Parameters.PowerUpdatePeriod;
			if (num < lastTick)
			{
				progress0 = progress1;
				progress1 = progress;
				if (progress1 > progress0)
				{
					progress0 = progress1;
				}
			}
			lastTick = num;
			return Mathf.Clamp01(Mathf.Lerp(progress0, progress1, (float)num / (float)Parameters.PowerUpdatePeriod));
		}

		public static float GetExtraFuelAmount(float progress)
		{
			if (Parameters.PowerUpdatePeriod == 1)
			{
				return progress;
			}
			int num = (factoryIndex + (int)GameMain.gameTick) % Parameters.PowerUpdatePeriod;
			if (num < lastExtraTick)
			{
				extraProgress0 = extraProgress1;
				extraProgress1 = progress;
				if (extraProgress1 > extraProgress0)
				{
					extraProgress0 = extraProgress1;
				}
			}
			lastExtraTick = num;
			return Mathf.Clamp01(Mathf.Lerp(extraProgress0, extraProgress1, (float)num / (float)Parameters.PowerUpdatePeriod));
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(UIAssemblerWindow), "_OnUpdate")]
		[HarmonyPatch(typeof(UILabWindow), "_OnUpdate")]
		[HarmonyPatch(typeof(UIEjectorWindow), "_OnUpdate")]
		[HarmonyPatch(typeof(UISiloWindow), "_OnUpdate")]
		private static IEnumerable<CodeInstruction> OnUpdate_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0137: Unknown result type (might be due to invalid IL or missing references)
			//IL_013d: Expected O, but got Unknown
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: Expected O, but got Unknown
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Expected O, but got Unknown
			//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Expected O, but got Unknown
			try
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				if (val.InstructionAt(2).opcode == OpCodes.Call)
				{
					string name = ((MethodInfo)val.InstructionAt(2).operand).Name;
					if (name == "get_ejectorId" || name == "get_siloId")
					{
						val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
						{
							new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "Clamp01"), (string)null)
						}).SetAndAdvance(OpCodes.Call, (object)AccessTools.Method(typeof(UIProgress_Patch), "GetCoolDown", (Type[])null, (Type[])null)).MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
						{
							new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "Clamp01"), (string)null)
						})
							.SetAndAdvance(OpCodes.Call, (object)AccessTools.Method(typeof(UIProgress_Patch), "GetCoolDown", (Type[])null, (Type[])null));
						return val.InstructionEnumeration();
					}
				}
				val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "Clamp01"), (string)null)
				}).SetAndAdvance(OpCodes.Call, (object)AccessTools.Method(typeof(UIProgress_Patch), "GetFillAmount", (Type[])null, (Type[])null)).MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && ((MethodInfo)i.operand).Name == "Clamp01"), (string)null)
				});
				if (val.IsValid)
				{
					val.SetAndAdvance(OpCodes.Call, (object)AccessTools.Method(typeof(UIProgress_Patch), "GetExtraFillAmount", (Type[])null, (Type[])null));
				}
				return val.InstructionEnumeration();
			}
			catch (Exception obj)
			{
				Log.Error("Transpiler OnUpdate_Transpiler failed");
				Log.Error(obj);
				return instructions;
			}
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(UIPowerGeneratorWindow), "_OnUpdate")]
		private static IEnumerable<CodeInstruction> PowerOnUpdate_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Expected O, but got Unknown
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Expected O, but got Unknown
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Expected O, but got Unknown
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Expected O, but got Unknown
			try
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				val.End().MatchBack(false, (CodeMatch[])(object)new CodeMatch[1]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Callvirt && ((MethodInfo)i.operand).Name == "set_fillAmount"), (string)null)
				}).Insert((CodeInstruction[])(object)new CodeInstruction[1]
				{
					new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(UIProgress_Patch), "GetExtraFuelAmount", (Type[])null, (Type[])null))
				})
					.MatchBack(false, (CodeMatch[])(object)new CodeMatch[1]
					{
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Callvirt && ((MethodInfo)i.operand).Name == "set_fillAmount"), (string)null)
					})
					.Insert((CodeInstruction[])(object)new CodeInstruction[1]
					{
						new CodeInstruction(OpCodes.Call, (object)AccessTools.Method(typeof(UIProgress_Patch), "GetFuelAmount", (Type[])null, (Type[])null))
					});
				return val.InstructionEnumeration();
			}
			catch (Exception obj)
			{
				Log.Error("Transpiler PowerOnUpdate_Transpiler failed");
				Log.Error(obj);
				return instructions;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(UIAssemblerWindow), "OnServingBoxChange")]
		private static void OnServingBoxChange(UIAssemblerWindow __instance)
		{
			if (__instance.assemblerId != 0 && __instance.factory != null)
			{
				factoryIndex = __instance.factory.index;
				ref AssemblerComponent reference = ref __instance.factorySystem.assemblerPool[__instance.assemblerId];
				progress0 = (progress1 = Mathf.Clamp01((float)reference.time / (float)reference.timeSpend));
				extraProgress0 = (extraProgress1 = Mathf.Clamp01((float)reference.extraTime / (float)reference.extraTimeSpend));
				lastTick = (lastExtraTick = 0);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(UILabWindow), "OnLabIdChange")]
		private static void OnLabIdChange(UILabWindow __instance)
		{
			if (__instance.labId != 0 && __instance.factory != null)
			{
				factoryIndex = __instance.factory.index;
				ref LabComponent reference = ref __instance.factorySystem.labPool[__instance.labId];
				progress0 = (progress1 = Mathf.Clamp01((float)reference.time / (float)reference.timeSpend));
				extraProgress0 = (extraProgress1 = Mathf.Clamp01((float)reference.extraTime / (float)reference.extraTimeSpend));
				lastTick = (lastExtraTick = 0);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(UIEjectorWindow), "OnServingBoxChange")]
		private static void OnServingBoxChange(UIEjectorWindow __instance)
		{
			if (__instance.ejectorId != 0 && __instance.factory != null)
			{
				factoryIndex = __instance.factory.index;
				ref EjectorComponent reference = ref __instance.factorySystem.ejectorPool[__instance.ejectorId];
				progress0 = (progress1 = ((reference.direction > 0) ? Mathf.Clamp01((float)reference.time / (float)reference.chargeSpend) : Mathf.Clamp01((float)reference.time / (float)reference.coldSpend)));
				lastTick = (lastExtraTick = 0);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(UISiloWindow), "OnServingBoxChange")]
		private static void OnServingBoxChange(UISiloWindow __instance)
		{
			if (__instance.siloId != 0 && __instance.factory != null)
			{
				factoryIndex = __instance.factory.index;
				ref SiloComponent reference = ref __instance.factorySystem.siloPool[__instance.siloId];
				progress0 = (progress1 = ((reference.direction > 0) ? Mathf.Clamp01((float)reference.time / (float)reference.chargeSpend) : Mathf.Clamp01((float)reference.time / (float)reference.coldSpend)));
				lastTick = (lastExtraTick = 0);
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(UIPowerGeneratorWindow), "OnGeneratorIdChange")]
		private static void OnGeneratorIdChange(UIPowerGeneratorWindow __instance)
		{
			if (__instance.generatorId != 0 && __instance.factory != null)
			{
				factoryIndex = __instance.factory.index;
				ref PowerGeneratorComponent reference = ref __instance.powerSystem.genPool[__instance.generatorId];
				ItemProto val = ((ProtoSet<ItemProto>)(object)LDB.items).Select((int)reference.curFuelId);
				if (val != null)
				{
					progress0 = (progress1 = reference.fuelEnergy / val.HeatValue);
					extraProgress0 = (extraProgress1 = 0f);
					lastTick = (lastExtraTick = 0);
				}
			}
		}
	}
}
namespace AlterTickrate.Compat
{
	public class ModCompatibility
	{
		public static class Incompat
		{
			private static string message = "";

			public static bool Check(Harmony harmony)
			{
				Dictionary<string, PluginInfo> pluginInfos = Chainloader.PluginInfos;
				if (pluginInfos.ContainsKey("com.starfi5h.plugin.SampleAndHoldSim"))
				{
					message += "\nSampleAndHoldSim";
				}
				if (pluginInfos.ContainsKey("dev.raptor.dsp.Blackbox"))
				{
					message += "\nBlackbox";
				}
				if (pluginInfos.ContainsKey("org.LoShin.GenesisBook") || pluginInfos.ContainsKey("org.kremnev8.plugin.BetterMachines") || pluginInfos.ContainsKey("top.awbugl.DSP.BeltSpeedEnhancement") || pluginInfos.ContainsKey("shisang_MoreReasonableCargoTraffic"))
				{
					Plugin.plugin.SaveBeltConfig(1, 1);
					Log.Debug("Belt altering mods detect: belt parameters are now disabled");
				}
				if (pluginInfos.ContainsKey("org.soardev.labopt"))
				{
					Plugin.plugin.SaveLabConfig(1, 1, 1);
					Log.Debug("Labopt compatibility - OK (lab parameters disabled)");
				}
				harmony.PatchAll(typeof(Incompat));
				if (message != "")
				{
					message = "AlterTickrate is not compatible with following mods 与以下不兼容:" + message;
					return false;
				}
				return true;
			}

			[HarmonyPostfix]
			[HarmonyPatch(typeof(VFPreload), "InvokeOnLoadWorkEnded")]
			public static void OnGameLoaded()
			{
				if (message != "")
				{
					UIMessageBox.Show("AlterTickrate", message, StringTranslate.Translate("确定"), 3);
				}
				else
				{
					if (!Chainloader.PluginInfos.ContainsKey("me.xiaoye97.plugin.Dyson.LDBTool"))
					{
						return;
					}
					Type type = AccessTools.TypeByName("xiaoye97.LDBTool");
					if (!(type != null))
					{
						return;
					}
					Log.Debug("Checking LDBTool proto...");
					List<List<Proto>> obj = (List<List<Proto>>)AccessTools.Field(type, "TotalDict").GetValue(null);
					bool flag = false;
					foreach (List<Proto> item in obj)
					{
						foreach (Proto item2 in item)
						{
							if (item2 is ItemProto || item2 is RecipeProto)
							{
								flag = true;
								break;
							}
						}
					}
					if (flag)
					{
						message = "Proto is modified! Please use SampleAndHoldSim for better compatibility.\n侦测到物品和配方修改,可能无法兼容.请改用SampleAndHoldSim";
						Log.Warn(message);
						UIMessageBox.Show("AlterTickrate", message, StringTranslate.Translate("确定"), 3);
					}
				}
			}
		}

		public static class DSPOptimizations
		{
			public const string GUID = "com.Selsion.DSPOptimizations";

			public static void Init(Harmony harmony)
			{
				//IL_0052: Unknown result type (might be due to invalid IL or missing references)
				//IL_0060: Expected O, but got Unknown
				try
				{
					if (Chainloader.PluginInfos.TryGetValue("com.Selsion.DSPOptimizations", out var value))
					{
						Type type = ((object)value.Instance).GetType().Assembly.GetType("DSPOptimizations.StationStorageOpt");
						harmony.Patch((MethodBase)AccessTools.Method(type, "RunStorageLogic", (Type[])null, (Type[])null), new HarmonyMethod(typeof(DSPOptimizations).GetMethod("RunStorageLogic_Prefix")), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
						Log.Debug("DSPOptimizations compatibility - OK");
					}
				}
				catch (Exception obj)
				{
					Log.Warn("DSPOptimizations compatibility failed! Last working version: 1.1.14");
					Log.Warn(obj);
				}
			}

			public static bool RunStorageLogic_Prefix()
			{
				return GameMain.gameTick % Parameters.StorageUpdatePeriod == 0;
			}
		}

		public static bool Init(Harmony harmony)
		{
			if (!Incompat.Check(harmony))
			{
				return false;
			}
			DSPOptimizations.Init(harmony);
			return true;
		}
	}
}