Decompiled source of BetterUtilitySlot v1.0.6

BepInEx/plugins/BetterUtilitySlot/ZaboomafooW-BetterUtilitySlot-v1.0.6.dll

Decompiled a month 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 BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("BetterUtilitySlot")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.6.0")]
[assembly: AssemblyInformationalVersion("1.0.6")]
[assembly: AssemblyProduct("BetterUtilitySlot")]
[assembly: AssemblyTitle("BetterUtilitySlot")]
[assembly: AssemblyVersion("1.0.6.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace BetterUtilitySlot
{
	[BepInPlugin("ZaboomafooW.BetterUtilitySlot", "BetterUtilitySlot", "1.0.6")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "ZaboomafooW.BetterUtilitySlot";

		public const string PluginName = "BetterUtilitySlot";

		public const string PluginVersion = "1.0.6";

		private Harmony _harmony;

		internal static ManualLogSource ModLogger { get; private set; }

		private void Awake()
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_002a: Expected O, but got Unknown
			ModLogger = ((BaseUnityPlugin)this).Logger;
			ModLogger.LogInfo((object)"[BetterUtilitySlot] Loaded v1.0.6");
			_harmony = new Harmony("ZaboomafooW.BetterUtilitySlot");
			try
			{
				_harmony.PatchAll();
			}
			catch (Exception arg)
			{
				_harmony.UnpatchSelf();
				_harmony = null;
				ModLogger.LogError((object)string.Format("[{0}] Failed to apply verified v81 patches; mod disabled: {1}", "BetterUtilitySlot", arg));
				return;
			}
			ModLogger.LogInfo((object)"[BetterUtilitySlot] Hook succeeded: v81 utility-slot eligibility and utility-item consumption patches attached.");
		}

		private void OnApplicationQuit()
		{
			if (_harmony != null)
			{
				_harmony.UnpatchSelf();
				_harmony = null;
			}
			ModLogger = null;
		}
	}
}
namespace BetterUtilitySlot.Patches
{
	[HarmonyPatch(typeof(PlayerControllerB), "DestroyItemInSlot")]
	internal static class UtilitySlotDestroyPatch
	{
		private const int UtilitySlot = 50;

		private static bool _loggedUtilityConsumption;

		[HarmonyPrefix]
		private static void Prefix(PlayerControllerB __instance, int itemSlot, out float __state)
		{
			__state = float.NaN;
			if (itemSlot == 50 && __instance.currentItemSlot != 50 && !((Object)(object)__instance.ItemOnlySlot == (Object)null) && !((Object)(object)__instance.ItemOnlySlot.itemProperties == (Object)null))
			{
				__state = __instance.ItemOnlySlot.itemProperties.weight - 1f;
			}
		}

		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			FieldInfo field;
			MethodInfo method;
			try
			{
				field = typeof(HUDManager).GetField("itemSlotIcons", BindingFlags.Instance | BindingFlags.Public);
				method = typeof(UtilitySlotDestroyPatch).GetMethod("GetSlotIcon", BindingFlags.Static | BindingFlags.NonPublic);
			}
			catch (Exception arg)
			{
				ManualLogSource modLogger = Plugin.ModLogger;
				if (modLogger != null)
				{
					modLogger.LogError((object)string.Format("[{0}] Failed to resolve verified v81 DestroyItemInSlot patch members: {1}", "BetterUtilitySlot", arg));
				}
				throw;
			}
			if (field == null || method == null)
			{
				string text = ((field == null) ? "itemSlotIcons" : "GetSlotIcon");
				InvalidOperationException ex = new InvalidOperationException("[BetterUtilitySlot] Verified v81 patch member was unavailable: " + text + ".");
				ManualLogSource modLogger2 = Plugin.ModLogger;
				if (modLogger2 != null)
				{
					modLogger2.LogError((object)ex.Message);
				}
				throw ex;
			}
			List<CodeInstruction> list = new List<CodeInstruction>(instructions);
			int num = 0;
			for (int i = 0; i <= list.Count - 3; i++)
			{
				if (CodeInstructionExtensions.LoadsField(list[i], field, false) && !(list[i + 1].opcode != OpCodes.Ldarg_1) && !(list[i + 2].opcode != OpCodes.Ldelem_Ref))
				{
					list[i].opcode = OpCodes.Ldarg_1;
					list[i].operand = null;
					list[i + 1].opcode = OpCodes.Call;
					list[i + 1].operand = method;
					list.RemoveAt(i + 2);
					num++;
					break;
				}
			}
			if (num != 1)
			{
				throw new InvalidOperationException(string.Format("[{0}] Expected exactly one v81 DestroyItemInSlot HUD icon access, found {1}.", "BetterUtilitySlot", num));
			}
			return list;
		}

		[HarmonyPostfix]
		private static void Postfix(PlayerControllerB __instance, int itemSlot, float __state)
		{
			if (itemSlot != 50)
			{
				return;
			}
			if (!float.IsNaN(__state))
			{
				float num = __instance.carryWeight - __state;
				if (num < 1f)
				{
					num = 1f;
				}
				else if (num > 10f)
				{
					num = 10f;
				}
				__instance.carryWeight = num;
				if ((Object)(object)GameNetworkManager.Instance != (Object)null && (Object)(object)GameNetworkManager.Instance.localPlayerController == (Object)(object)__instance && (Object)(object)StartOfRound.Instance != (Object)null)
				{
					StartOfRound.Instance.SendChangedWeightEvent();
				}
			}
			if ((Object)(object)GameNetworkManager.Instance == (Object)null || (Object)(object)GameNetworkManager.Instance.localPlayerController != (Object)(object)__instance || (Object)(object)HUDManager.Instance == (Object)null || (Object)(object)HUDManager.Instance.itemOnlySlotIcon == (Object)null)
			{
				return;
			}
			((Behaviour)HUDManager.Instance.itemOnlySlotIcon).enabled = false;
			if (!_loggedUtilityConsumption)
			{
				_loggedUtilityConsumption = true;
				ManualLogSource modLogger = Plugin.ModLogger;
				if (modLogger != null)
				{
					modLogger.LogInfo((object)string.Format("[{0}] Feature executed: utility-slot item consumption cleared slot {1}, its HUD icon, and carried weight.", "BetterUtilitySlot", 50));
				}
			}
		}

		private static Image GetSlotIcon(HUDManager hudManager, int itemSlot)
		{
			if (itemSlot == 50)
			{
				return hudManager.itemOnlySlotIcon;
			}
			return hudManager.itemSlotIcons[itemSlot];
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB), "FirstEmptyItemSlot")]
	internal static class UtilitySlotEligibilityPatch
	{
		private const int UtilitySlot = 50;

		private static bool _loggedEligibility;

		[HarmonyPostfix]
		private static void Postfix(PlayerControllerB __instance, GrabbableObject attemptingGrab, ref int __result)
		{
			if ((!((NetworkBehaviour)__instance).IsOwner && !((NetworkBehaviour)__instance).IsServer) || __result == 50 || (Object)(object)__instance.ItemOnlySlot != (Object)null || (Object)(object)attemptingGrab == (Object)null || (Object)(object)attemptingGrab.itemProperties == (Object)null)
			{
				return;
			}
			Item itemProperties = attemptingGrab.itemProperties;
			if (itemProperties.isScrap || itemProperties.twoHanded || !itemProperties.disallowUtilitySlot)
			{
				return;
			}
			__result = 50;
			if (!_loggedEligibility)
			{
				_loggedEligibility = true;
				ManualLogSource modLogger = Plugin.ModLogger;
				if (modLogger != null)
				{
					modLogger.LogInfo((object)string.Format("[{0}] Feature executed: allowed a vanilla disallowUtilitySlot item into utility slot {1}.", "BetterUtilitySlot", 50));
				}
			}
		}
	}
}