Decompiled source of MechaDronesTweaks v1.1.4

MechaDronesTweaks.dll

Decompiled 9 months 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 BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("MechaDronesTweaks")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("DSP MOD - MechaDronesTweaks")]
[assembly: AssemblyFileVersion("1.1.4.0")]
[assembly: AssemblyInformationalVersion("1.1.4+e2d83ee3cba46207f73d983cb0fd76c6dd66618b")]
[assembly: AssemblyProduct("MechaDronesTweaks")]
[assembly: AssemblyTitle("MechaDronesTweaks")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.4.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace MechaDronesTweaks
{
	internal class FastDronesRemover
	{
		public const string FastDronesGuid = "com.dkoppstein.plugin.DSP.FastDrones";

		private const string FastDronesVersion = "0.0.5";

		public static bool Run(Harmony harmony)
		{
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Expected O, but got Unknown
			if (!Chainloader.PluginInfos.TryGetValue("com.dkoppstein.plugin.DSP.FastDrones", out var value) || value.Metadata.Version.ToString() != "0.0.5")
			{
				return false;
			}
			Type type = ((object)value.Instance).GetType().Assembly.GetType("com.dkoppstein.plugin.DSP.FastDrones.FastDronesPlugin");
			harmony.Patch((MethodBase)AccessTools.Method(type, "Start", (Type[])null, (Type[])null), new HarmonyMethod(typeof(FastDronesRemover).GetMethod("PatchFastDronesStart")), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			return true;
		}

		public static bool PatchFastDronesStart()
		{
			return false;
		}
	}
	[BepInPlugin("org.soardev.mechadronestweaks", "MechaDronesTweaks", "1.1.4")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class MechaDronesTweaksPlugin : BaseUnityPlugin
	{
		public static readonly ManualLogSource Logger = Logger.CreateLogSource("MechaDronesTweaks");

		private readonly Harmony _harmony = new Harmony("org.soardev.mechadronestweaks");

		public MechaDronesTweaksPlugin()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			try
			{
				if (FastDronesRemover.Run(_harmony))
				{
					Logger.LogInfo((object)"Unpatch FastDrones - OK");
				}
			}
			catch (Exception arg)
			{
				Logger.LogWarning((object)$"Failed to unpatch FastDrones: {arg}");
			}
		}

		public void Awake()
		{
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Expected O, but got Unknown
			//IL_00eb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Expected O, but got Unknown
			//IL_012d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0137: Expected O, but got Unknown
			//IL_016f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0179: Expected O, but got Unknown
			MechaDronesTweaks.UseFixedSpeed = ((BaseUnityPlugin)this).Config.Bind<bool>("MechaDrones", "UseFixedSpeed", MechaDronesTweaks.UseFixedSpeed, "Use fixed speed for mecha drones").Value;
			MechaDronesTweaks.SkipStage1 = ((BaseUnityPlugin)this).Config.Bind<bool>("MechaDrones", "SkipStage1", MechaDronesTweaks.SkipStage1, "Skip 1st stage of working mecha drones (flying away from mecha in ~1/3 speed for several frames)").Value;
			MechaDronesTweaks.RemoveSpeedLimitForStage1 = ((BaseUnityPlugin)this).Config.Bind<bool>("MechaDrones", "RemoveSpeedLimitForStage1", MechaDronesTweaks.RemoveSpeedLimitForStage1, "Remove speed limit for 1st stage (has a speed limit @ ~10m/s originally)").Value;
			MechaDronesTweaks.FixedSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("MechaDrones", "FixedSpeed", MechaDronesTweaks.FixedSpeed, new ConfigDescription("Fixed speed for mecha drones, working only when UseFixedSpeed is enabled", (AcceptableValueBase)(object)new AcceptableValueRange<float>(6f, 1000f), Array.Empty<object>())).Value;
			MechaDronesTweaks.SpeedMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("MechaDrones", "SpeedMultiplier", MechaDronesTweaks.SpeedMultiplier, new ConfigDescription("Speed multiplier for mecha drones, working only when UseFixedSpeed is disabled", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 10f), Array.Empty<object>())).Value;
			MechaDronesTweaks.EnergyMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("MechaDrones", "EnergyMultiplier", MechaDronesTweaks.EnergyMultiplier, new ConfigDescription("Energy consumption multiplier for mecha drones", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>())).Value;
			MechaDronesTweaks.EnergyMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>("MechaDrones", "EnergyMultiplier", MechaDronesTweaks.EnergyMultiplier, new ConfigDescription("Energy consumption multiplier for mecha drones", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>())).Value;
			_harmony.PatchAll(typeof(MechaDronesTweaks));
		}
	}
	public static class MechaDronesTweaks
	{
		public static bool UseFixedSpeed;

		public static bool SkipStage1;

		public static bool RemoveSpeedLimitForStage1 = true;

		public static float FixedSpeed = 300f;

		public static float SpeedMultiplier = 4f;

		public static float EnergyMultiplier = 0.1f;

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(ConstructionSystem), "UpdateDrones")]
		[HarmonyPatch(typeof(UIMechaWindow), "UpdateProps")]
		[HarmonyPatch(typeof(UITechTree), "RefreshDataValueText")]
		private static IEnumerable<CodeInstruction> UITechTreeRefreshDataValueText_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
		{
			//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
			CodeMatcher val = new CodeMatcher(instructions, generator);
			val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
			{
				new CodeMatch((OpCode?)OpCodes.Ldfld, (object)AccessTools.Field(typeof(GameHistoryData), "constructionDroneSpeed"), (string)null)
			});
			if (UseFixedSpeed)
			{
				val.Repeat((Action<CodeMatcher>)delegate(CodeMatcher m)
				{
					//IL_0015: Unknown result type (might be due to invalid IL or missing references)
					//IL_001b: Expected O, but got Unknown
					//IL_002c: Unknown result type (might be due to invalid IL or missing references)
					//IL_0032: Expected O, but got Unknown
					m.Advance(1).InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[2]
					{
						new CodeInstruction(OpCodes.Pop, (object)null),
						new CodeInstruction(OpCodes.Ldc_R4, (object)FixedSpeed)
					});
				}, (Action<string>)null);
			}
			else
			{
				val.Repeat((Action<CodeMatcher>)delegate(CodeMatcher m)
				{
					//IL_001e: Unknown result type (might be due to invalid IL or missing references)
					//IL_0024: Expected O, but got Unknown
					//IL_002c: Unknown result type (might be due to invalid IL or missing references)
					//IL_0032: Expected O, but got Unknown
					m.Advance(1).InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[2]
					{
						new CodeInstruction(OpCodes.Ldc_R4, (object)SpeedMultiplier),
						new CodeInstruction(OpCodes.Mul, (object)null)
					});
				}, (Action<string>)null);
			}
			return val.InstructionEnumeration();
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(ConstructionModuleComponent), "EjectBaseDrone")]
		[HarmonyPatch(typeof(ConstructionModuleComponent), "EjectMechaDrone")]
		private static IEnumerable<CodeInstruction> MechaUpdateTargets_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Expected O, but got Unknown
			CodeMatcher val = new CodeMatcher(instructions, generator);
			if (!SkipStage1)
			{
				return val.InstructionEnumeration();
			}
			val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
			{
				new CodeMatch((OpCode?)OpCodes.Ldc_I4_1, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Stfld, (object)AccessTools.Field(typeof(DroneComponent), "stage"), (string)null)
			}).Operand = 2;
			return val.InstructionEnumeration();
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(ConstructionSystem), "UpdateDrones")]
		private static IEnumerable<CodeInstruction> MechaUpdateDrones_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Expected O, but got Unknown
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: 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_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Expected O, but got Unknown
			CodeMatcher val = new CodeMatcher(instructions, generator);
			if (EnergyMultiplier >= 1f)
			{
				return val.InstructionEnumeration();
			}
			val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
			{
				new CodeMatch((OpCode?)OpCodes.Ldfld, (object)AccessTools.Field(typeof(ModeConfig), "droneEnergyPerMeter"), (string)null)
			}).Advance(1).Insert((CodeInstruction[])(object)new CodeInstruction[2]
			{
				new CodeInstruction(OpCodes.Ldc_R8, (object)(double)EnergyMultiplier),
				new CodeInstruction(OpCodes.Mul, (object)null)
			});
			return val.InstructionEnumeration();
		}

		[HarmonyTranspiler]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		private static IEnumerable<CodeInstruction> MechaDroneUpdate_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: 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_00d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Expected O, but got Unknown
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0105: Expected O, but got Unknown
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Expected O, but got Unknown
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: Expected O, but got Unknown
			//IL_0161: Unknown result type (might be due to invalid IL or missing references)
			//IL_0167: Expected O, but got Unknown
			CodeMatcher val = new CodeMatcher(instructions, generator);
			if (RemoveSpeedLimitForStage1)
			{
				val.MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction instr) => instr.opcode == OpCodes.Ldc_R4 && CodeInstructionExtensions.OperandIs(instr, (object)1f)), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction instr) => instr.opcode == OpCodes.Ldc_R4 && CodeInstructionExtensions.OperandIs(instr, (object)3f)), (string)null)
				});
				val.Advance(1).Operand = 10000f;
			}
			if (!UseFixedSpeed && Math.Abs(SpeedMultiplier - 1f) < 0.01f)
			{
				return val.InstructionEnumeration();
			}
			val.Start().MatchForward(false, (CodeMatch[])(object)new CodeMatch[3]
			{
				new CodeMatch((OpCode?)OpCodes.Ldarg_0, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Ldc_R4, (object)null, (string)null),
				new CodeMatch((OpCode?)OpCodes.Stfld, (object)AccessTools.Field(typeof(DroneComponent), "progress"), (string)null)
			});
			val.Repeat((Action<CodeMatcher>)delegate(CodeMatcher m)
			{
				if (CodeInstructionExtensions.OperandIs(m.InstructionAt(1), (object)0f))
				{
					m.InstructionAt(3).labels = m.Labels;
					m.RemoveInstructions(3);
				}
				else if (CodeInstructionExtensions.OperandIs(m.InstructionAt(1), (object)1f))
				{
					m.Advance(1).Operand = 0f;
					m.Advance(2);
				}
			}, (Action<string>)null);
			val.Start().MatchForward(false, (CodeMatch[])(object)new CodeMatch[1]
			{
				new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction instr) => instr.opcode == OpCodes.Ldc_R4 && CodeInstructionExtensions.OperandIs(instr, (object)0.5f)), (string)null)
			});
			val.Repeat((Action<CodeMatcher>)delegate(CodeMatcher m)
			{
				if (UseFixedSpeed)
				{
					if (FixedSpeed > 75f)
					{
						m.Operand = 0.5f * FixedSpeed / 75f;
					}
				}
				else
				{
					m.Operand = 0.5f * SpeedMultiplier;
				}
			}, (Action<string>)null);
			return val.InstructionEnumeration();
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "org.soardev.mechadronestweaks";

		public const string PLUGIN_NAME = "MechaDronesTweaks";

		public const string PLUGIN_VERSION = "1.1.4";
	}
}