Decompiled source of BuildingQOLs v1.0.2

BuildingQOLs.dll

Decompiled 8 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
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.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.6", FrameworkDisplayName = ".NET Framework 4.6")]
[assembly: AssemblyCompany("BuildingQOLs")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("Some building related QOL options.")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2")]
[assembly: AssemblyProduct("BuildingQOLs")]
[assembly: AssemblyTitle("BuildingQOLs")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.2.0")]
[module: UnverifiableCode]
namespace BuildingQOLs;

[BepInPlugin("com.Trol1face.dsp.BuildingQOLs", "BuildingQOLs", "1.0.2")]
public class BuildingQOLs : BaseUnityPlugin
{
	private static class Patch
	{
		[HarmonyTranspiler]
		[HarmonyPatch(typeof(BuildTool_Path), "ConfirmOperation")]
		public static IEnumerable<CodeInstruction> BuildTool_Path_ConfirmOperation_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator ilgen)
		{
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected O, but got Unknown
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Expected O, but got Unknown
			//IL_02ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0305: Expected O, but got Unknown
			//IL_00fe: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: 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_0133: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Expected O, but got Unknown
			//IL_0154: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Expected O, but got Unknown
			//IL_0175: Unknown result type (might be due to invalid IL or missing references)
			//IL_017b: Expected O, but got Unknown
			//IL_0189: Unknown result type (might be due to invalid IL or missing references)
			//IL_018f: 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_01b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01be: 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_01db: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e1: Expected O, but got Unknown
			//IL_01fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0203: Expected O, but got Unknown
			//IL_020c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0212: 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_0327: Unknown result type (might be due to invalid IL or missing references)
			//IL_032d: Expected O, but got Unknown
			//IL_0267: Unknown result type (might be due to invalid IL or missing references)
			//IL_026d: Expected O, but got Unknown
			//IL_034f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0355: Expected O, but got Unknown
			//IL_0377: Unknown result type (might be due to invalid IL or missing references)
			//IL_037d: Expected O, but got Unknown
			//IL_03a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_03a8: Expected O, but got Unknown
			//IL_03b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_03b7: Expected O, but got Unknown
			//IL_03c0: Unknown result type (might be due to invalid IL or missing references)
			//IL_03c6: Expected O, but got Unknown
			if (holdReleaseBeltsBuilding.Value || autoTakeBeltsAltitude.Value)
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				if (holdReleaseBeltsBuilding.Value)
				{
					Label label = ilgen.DefineLabel();
					Label label2 = ilgen.DefineLabel();
					List<Label> list = new List<Label>();
					list.Add(label);
					List<Label> list2 = new List<Label>();
					list2.Add(label2);
					FieldInfo insertAnchor = typeof(InputValue).GetField("onDown");
					val.Start();
					val.MatchForward(true, (CodeMatch[])(object)new CodeMatch[1]
					{
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld && i.operand is FieldInfo fieldInfo && fieldInfo == insertAnchor), (string)null)
					});
					if (val.Pos != 0)
					{
						val.Advance(-1);
						val.RemoveInstructions(3);
						val.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[13]
						{
							new CodeInstruction(OpCodes.Call, (object)typeof(VFInput).GetMethod("get__buildConfirm")),
							new CodeInstruction(OpCodes.Ldfld, (object)typeof(InputValue).GetField("onDown")),
							new CodeInstruction(OpCodes.Brtrue, (object)label2),
							new CodeInstruction(OpCodes.Call, (object)typeof(VFInput).GetMethod("get__buildConfirm")),
							new CodeInstruction(OpCodes.Ldfld, (object)typeof(InputValue).GetField("onUp")),
							new CodeInstruction(OpCodes.Brfalse, (object)label),
							new CodeInstruction(OpCodes.Ldarg_0, (object)null),
							new CodeInstruction(OpCodes.Ldfld, (object)typeof(BuildTool_Path).GetField("waitForConfirm")),
							new CodeInstruction(OpCodes.Brfalse, (object)label),
							new CodeInstruction(OpCodes.Ldarg_0, (object)null),
							new CodeInstruction(OpCodes.Ldfld, (object)typeof(BuildTool_Path).GetField("pathPointCount")),
							new CodeInstruction(OpCodes.Ldc_I4_1, (object)null),
							new CodeInstruction(OpCodes.Ble, (object)label)
						});
						val.AddLabels((IEnumerable<Label>)list2);
						val.End();
						val.MatchBack(true, (CodeMatch[])(object)new CodeMatch[1]
						{
							new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldc_I4_0), (string)null)
						});
						val.AddLabels((IEnumerable<Label>)list);
					}
				}
				if (autoTakeBeltsAltitude.Value)
				{
					MethodInfo anchor = typeof(BuildTool).GetMethod("GetObjectPose");
					MethodInfo method = typeof(BuildingQOLs).GetMethod("TakeOnCursorBeltAltitude");
					FieldInfo field = typeof(BuildTool_Path).GetField("altitude");
					val.Start();
					val.MatchForward(true, (CodeMatch[])(object)new CodeMatch[4]
					{
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && i.operand is MethodInfo methodInfo && methodInfo == anchor), (string)null),
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld), (string)null),
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Stfld), (string)null),
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Br), (string)null)
					});
					if (val.Pos != 0)
					{
						val.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[3]
						{
							new CodeInstruction(OpCodes.Ldarg_0, (object)null),
							new CodeInstruction(OpCodes.Call, (object)method),
							new CodeInstruction(OpCodes.Stfld, (object)field)
						});
					}
				}
				return val.InstructionEnumeration();
			}
			return instructions;
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(BuildTool_Inserter), "ConfirmOperation")]
		public static IEnumerable<CodeInstruction> BuildTool_Inserter_ConfirmOperation_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			if (holdReleaseSortersBuilding.Value)
			{
				FieldInfo field = typeof(InputValue).GetField("onDown");
				FieldInfo field2 = typeof(InputValue).GetField("onUp");
				int num = -1;
				List<CodeInstruction> list = new List<CodeInstruction>(instructions);
				for (int i = 0; i < list.Count; i++)
				{
					if (list[i].opcode == OpCodes.Ldfld && list[i].operand is FieldInfo fieldInfo && fieldInfo == field)
					{
						num = i;
						break;
					}
				}
				if (num > -1)
				{
					list[num].operand = field2;
				}
				return list.AsEnumerable();
			}
			return instructions;
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(BuildTool_Path), "CreatePrebuilds")]
		public static IEnumerable<CodeInstruction> BuildTool_Path_CreatePrebuilds_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator ilgen)
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Expected O, but got Unknown
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: 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_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Expected O, but got Unknown
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Expected O, but got Unknown
			//IL_0152: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Expected O, but got Unknown
			//IL_017a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0180: Expected O, but got Unknown
			//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a8: Expected O, but got Unknown
			//IL_01ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d0: Expected O, but got Unknown
			//IL_01df: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e5: Expected O, but got Unknown
			if (disableBeltProlongation.Value)
			{
				Label label = ilgen.DefineLabel();
				List<Label> list = new List<Label>();
				CodeInstruction val = new CodeInstruction(OpCodes.Br, (object)label);
				CodeMatcher val2 = new CodeMatcher(instructions, (ILGenerator)null);
				MethodInfo anchor = typeof(BuildTool).GetMethod("get_buildPreviews");
				list.Add(label);
				val2.MatchForward(false, (CodeMatch[])(object)new CodeMatch[4]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldarg_0), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldarg_0), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && i.operand is MethodInfo methodInfo2 && methodInfo2 == anchor), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call), (string)null)
				});
				if (val2.Pos != -1)
				{
					val2.AddLabels((IEnumerable<Label>)list);
					val2.Start();
					val2.MatchForward(false, (CodeMatch[])(object)new CodeMatch[5]
					{
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ble), (string)null),
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldc_I4_1), (string)null),
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Stloc_S), (string)null),
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldarg_0), (string)null),
						new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call && i.operand is MethodInfo methodInfo && methodInfo == anchor), (string)null)
					});
					if (val2.Pos != 0)
					{
						val2.Advance(1);
						val2.Insert((CodeInstruction[])(object)new CodeInstruction[1] { val });
						return val2.InstructionEnumeration();
					}
				}
			}
			return instructions;
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(BuildTool_Path), "DeterminePreviews")]
		public static object BuildTool_Path_DeterminePreviews_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator ilgen)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: 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_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Expected O, but got Unknown
			if (altitudeValueInCursorText.Value)
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				MethodInfo method = typeof(BuildingQOLs).GetMethod("CursorText_DeterminePreviews");
				val.MatchForward(true, (CodeMatch[])(object)new CodeMatch[1]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldstr && (string)i.operand == "选择起始位置"), (string)null)
				});
				if (val.Pos != -1)
				{
					val.RemoveInstructions(2);
					val.Insert((CodeInstruction[])(object)new CodeInstruction[1]
					{
						new CodeInstruction(OpCodes.Call, (object)method)
					});
				}
				return val.InstructionEnumeration();
			}
			return instructions;
		}

		[HarmonyTranspiler]
		[HarmonyPatch(typeof(BuildTool_Path), "CheckBuildConditions")]
		public static object BuildTool_Path_CheckBuildConditions_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator ilgen)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: 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_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_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Expected O, but got Unknown
			//IL_010f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: 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_015f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Expected O, but got Unknown
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			//IL_018d: Expected O, but got Unknown
			//IL_01a2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a8: Expected O, but got Unknown
			//IL_01d8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01de: 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
			if (altitudeValueInCursorText.Value)
			{
				CodeMatcher val = new CodeMatcher(instructions, (ILGenerator)null);
				MethodInfo method = typeof(BuildingQOLs).GetMethod("CursorText_CheckBuildConditions_ConditionOK");
				MethodInfo method2 = typeof(BuildingQOLs).GetMethod("CursorText_CheckBuildConditions_ConditionNotOK");
				val.MatchForward(true, (CodeMatch[])(object)new CodeMatch[8]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldc_I4_M1), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Stfld), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldarg_0), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Call), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldfld), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldloc_S), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Callvirt && i.operand is MethodInfo methodInfo && methodInfo == typeof(BuildPreview).GetMethod("get_conditionText")), (string)null),
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Stfld && i.operand is FieldInfo fieldInfo && fieldInfo == typeof(BuildModel).GetField("cursorText")), (string)null)
				});
				val.Insert((CodeInstruction[])(object)new CodeInstruction[1]
				{
					new CodeInstruction(OpCodes.Call, (object)method2)
				});
				val.MatchForward(true, (CodeMatch[])(object)new CodeMatch[1]
				{
					new CodeMatch((Func<CodeInstruction, bool>)((CodeInstruction i) => i.opcode == OpCodes.Ldstr && (string)i.operand == "点击鼠标建造"), (string)null)
				});
				val.RemoveInstructions(8);
				val.Insert((CodeInstruction[])(object)new CodeInstruction[1]
				{
					new CodeInstruction(OpCodes.Call, (object)method)
				});
				return val.InstructionEnumeration();
			}
			return instructions;
		}
	}

	public const string __NAME__ = "BuildingQOLs";

	public const string __GUID__ = "com.Trol1face.dsp.BuildingQOLs";

	public static ConfigEntry<bool> holdReleaseBeltsBuilding;

	public static ConfigEntry<bool> holdReleaseSortersBuilding;

	public static ConfigEntry<bool> disableBeltProlongation;

	public static ConfigEntry<bool> altitudeValueInCursorText;

	public static ConfigEntry<bool> shortVerOfAltitudeAndLength;

	public static ConfigEntry<bool> autoTakeBeltsAltitude;

	private void Awake()
	{
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		holdReleaseBeltsBuilding = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "holdReleaseBeltsBuilding", true, "Enable 1 click building for Belts");
		holdReleaseSortersBuilding = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "holdReleaseSortersBuilding", true, "Enable 1 click building for Sorters");
		disableBeltProlongation = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "disableBeltProlongation", true, "If set on TRUE ending belt on ground will not start another belt in the end of builded one. In vanilla if you build end a belt into nothing, end of the belt becomes a new start and you continue to build it or cancel with RMB. This feature disables that");
		altitudeValueInCursorText = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "AltitudeValueInCursorText", true, "There will be a text near cursor representing current belt's altitude (instead of tips about clicking to build)");
		shortVerOfAltitudeAndLength = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ShortVerOfAltitudeAndLength", false, "Enable this in addition to previous config to change form from *Altitude: n/Length: n* to short version *A: n| L: n");
		autoTakeBeltsAltitude = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "autoTakeBeltsAltitude", true, "If you start a belt in another belt your current altitude will change to this belt's altitude automaticly");
		new Harmony("com.Trol1face.dsp.BuildingQOLs").PatchAll(typeof(Patch));
	}

	public static int ObjectAltitude(Vector3 pos)
	{
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		PlanetAuxData planetAux = GameMain.mainPlayer.controller.actionBuild.planetAux;
		if (planetAux == null)
		{
			return 0;
		}
		Vector3 val = planetAux.Snap(pos, true);
		float num = Vector3.Distance(pos, val);
		return (int)Math.Round(num / 1.3333333f);
	}

	public static int TakeOnCursorBeltAltitude()
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		BuildTool_Path pathTool = GameMain.mainPlayer.controller.actionBuild.pathTool;
		int result = pathTool.altitude;
		if (((BuildTool)pathTool).ObjectIsBelt(pathTool.castObjectId))
		{
			result = ObjectAltitude(pathTool.castObjectPos);
		}
		return result;
	}

	public static string CursorText_DeterminePreviews()
	{
		BuildTool_Path pathTool = GameMain.mainPlayer.controller.actionBuild.pathTool;
		string text = (pathTool.altitude + 1).ToString();
		if (shortVerOfAltitudeAndLength.Value)
		{
			return "A: " + text + " | L: 0";
		}
		return "Altitude: " + text + Environment.NewLine + "Length: 0";
	}

	public static string CursorText_CheckBuildConditions_ConditionOK()
	{
		BuildTool_Path pathTool = GameMain.mainPlayer.controller.actionBuild.pathTool;
		string text = (pathTool.altitude + 1).ToString();
		string text2 = pathTool.pathPointCount.ToString();
		if (shortVerOfAltitudeAndLength.Value)
		{
			return "A: " + text + " | L: " + text2;
		}
		return "Altitude: " + text + Environment.NewLine + "Length: " + text2;
	}

	public static string CursorText_CheckBuildConditions_ConditionNotOK(string problem)
	{
		BuildTool_Path pathTool = GameMain.mainPlayer.controller.actionBuild.pathTool;
		string text = (pathTool.altitude + 1).ToString();
		string text2 = pathTool.pathPointCount.ToString();
		if (shortVerOfAltitudeAndLength.Value)
		{
			return "A: " + text + " | L: " + text2 + Environment.NewLine + problem;
		}
		return "Altitude: " + text + Environment.NewLine + "Length: " + text2 + Environment.NewLine + problem;
	}
}