Decompiled source of BuildingPicker v1.0.3

plugins/BuildingPicker/BuildingPicker.dll

Decompiled 2 years ago
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BuildingPicker.Patches;
using EquinoxsModUtils;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("BuildingPicker")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BuildingPicker")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("6c779674-701c-44a6-9771-153119482786")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace BuildingPicker
{
	[BepInPlugin("com.equinox.BuildingPicker", "BuildingPicker", "1.0.3")]
	public class BuildingPickerPlugin : BaseUnityPlugin
	{
		private const string MyGUID = "com.equinox.BuildingPicker";

		private const string PluginName = "BuildingPicker";

		private const string VersionString = "1.0.3";

		public static string PickerShortcutKey = "Picker Shortcut";

		public static ConfigEntry<KeyboardShortcut> PickerShortcut;

		private static readonly Harmony Harmony = new Harmony("com.equinox.BuildingPicker");

		public static ManualLogSource Log = new ManualLogSource("BuildingPicker");

		private void Awake()
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: BuildingPicker, VersionString: 1.0.3 is loading...");
			Harmony.PatchAll();
			PickerShortcut = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("General", PickerShortcutKey, new KeyboardShortcut((KeyCode)325, Array.Empty<KeyCode>()), (ConfigDescription)null);
			Harmony.CreateAndPatchAll(typeof(PlayerInspectorPatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(ToolbarSlotUIPatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(ConstructableCursorUIPatch), (string)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: BuildingPicker, VersionString: 1.0.3 is loaded.");
			Log = ((BaseUnityPlugin)this).Logger;
		}

		private void Update()
		{
		}
	}
}
namespace BuildingPicker.Patches
{
	internal class ConstructableCursorUIPatch
	{
		[HarmonyPatch(typeof(ConstructableCursorUI), "Set")]
		[HarmonyPrefix]
		private static void fixCrash()
		{
			if (!((Object)(object)Player.instance.builder.CurrentBuilder == (Object)null) && !((Object)(object)Player.instance.builder.CurrentBuilderInfo == (Object)null) && Player.instance.builder.CurrentBuilderInfo.variationData.Count() <= Player.instance.builder.CurrentBuilder.currentVariationIndex)
			{
				Player.instance.builder.CurrentBuilder.currentVariationIndex = 0;
			}
		}
	}
	internal class PlayerInspectorPatch
	{
		[HarmonyPatch(typeof(PlayerInspector), "LateUpdate")]
		[HarmonyPostfix]
		public static void pickBuilding(PlayerInspector __instance)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Expected O, but got Unknown
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0065: Unknown result type (might be due to invalid IL or missing references)
			//IL_006a: Unknown result type (might be due to invalid IL or missing references)
			KeyboardShortcut value = BuildingPickerPlugin.PickerShortcut.Value;
			if (!((KeyboardShortcut)(ref value)).IsDown())
			{
				return;
			}
			Transform val = (Transform)ModUtils.GetPrivateField<PlayerInspector>("playerCamera", __instance);
			LayerMask val2 = (LayerMask)ModUtils.GetPrivateField<PlayerInspector>("collisionLayers", __instance);
			RaycastHit val3 = default(RaycastHit);
			if (Physics.Raycast(val.position, val.forward, ref val3, 100f, LayerMask.op_Implicit(val2)))
			{
				GenericMachineInstanceRef val4 = (GenericMachineInstanceRef)ModUtils.GetPrivateField<PlayerInspector>("_machineHit", __instance);
				if (((GenericMachineInstanceRef)(ref val4)).IsValid())
				{
					Player.instance.toolbar.SetResForCurrentToolbar(9, ((GenericMachineInstanceRef)(ref val4)).ResId, true);
					Player.instance.toolbar.Select(9, false, 0);
					Player.instance.toolbar.SetToolbarVariationIndex(0, 9, 0);
					MethodInfo method = typeof(PlayerToolbar).GetMethod("SelectHotBarSlot", BindingFlags.Instance | BindingFlags.NonPublic);
					if (method == null)
					{
						Debug.Log((object)"selectHotbarSlotInfo is null");
						return;
					}
					object[] parameters = new object[1] { 9 };
					method.Invoke(Player.instance.toolbar, parameters);
				}
			}
			else
			{
				Debug.Log((object)"Raycast did not hit");
			}
		}
	}
	[HarmonyPatch(typeof(Player))]
	internal class PlayerPatches
	{
		[HarmonyPatch("Awake")]
		[HarmonyPrefix]
		public static bool Awake_Prefix(Player __instance)
		{
			BuildingPickerPlugin.Log.LogInfo((object)"In Player Awake method Prefix.");
			return true;
		}

		[HarmonyPatch("Awake")]
		[HarmonyPostfix]
		public static void Awake_Postfix(Player __instance)
		{
			BuildingPickerPlugin.Log.LogInfo((object)"In Player Awake method Postfix.");
		}
	}
	internal class ToolbarSlotUIPatch
	{
		[HarmonyPatch(typeof(ToolbarSlotUI), "Refresh")]
		[HarmonyPrefix]
		private static void fixError(ToolbarSlotUI __instance)
		{
			FieldInfo field = typeof(ToolbarSlotUI).GetField("_setVariationIndex", BindingFlags.Instance | BindingFlags.NonPublic);
			if (!(field == null))
			{
				int num = (int)field.GetValue(__instance);
				ResourceInfo setResource = __instance.setResource;
				BuilderInfo val = (BuilderInfo)(object)((setResource is BuilderInfo) ? setResource : null);
				if (!((Object)(object)val == (Object)null) && val.variationData != null && num >= val.variationData.Length)
				{
					field.SetValue(__instance, 0);
				}
			}
		}
	}
}