Decompiled source of More Satchels v1.1.3

BepInEx/plugins/CrackHotbar8/CrackHotbar8.dll

Decompiled 42 minutes 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 HarmonyLib;
using Microsoft.CodeAnalysis;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyVersion("0.0.0.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 CrackHotbar8
{
	[BepInPlugin("crack.hotbar8", "CRACK - 8 Hotbar Slots", "1.1.3")]
	public sealed class Hotbar8Plugin : BaseUnityPlugin
	{
		public const string Guid = "crack.hotbar8";

		public const string Version = "1.1.3";

		internal const int SlotLimit = 8;

		internal static ManualLogSource Log;

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			try
			{
				new Harmony("crack.hotbar8").PatchAll(typeof(Hotbar8Patches));
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"8-slot hotbar loaded (limit {8}); keys 1-8 select slots 1-8.");
			}
			catch (Exception arg)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)$"Failed to apply hotbar patches: {arg}");
			}
		}
	}
	internal static class Hotbar8Patches
	{
		private static readonly FieldInfo UnlockedField = AccessTools.Field(typeof(HotbarSlots), "_unlocked");

		private static readonly FieldRef<Dictionary<ulong, int>> UnlockedRef = AccessTools.StaticFieldRefAccess<Dictionary<ulong, int>>(UnlockedField);

		[HarmonyPatch(typeof(HotbarSlots), "get_Max")]
		[HarmonyPostfix]
		private static void MaxPostfix(ref int __result)
		{
			__result = 8;
		}

		[HarmonyPatch(typeof(HotbarSlots), "ServerAddSlot")]
		[HarmonyPrefix]
		private static bool ServerAddSlotPrefix(ulong steamId, ref bool __result)
		{
			try
			{
				if (steamId == 0L)
				{
					__result = false;
					return false;
				}
				int num = HotbarSlots.Get(steamId);
				if (num >= 8)
				{
					__result = false;
					return false;
				}
				if (num < 4)
				{
					return true;
				}
				Dictionary<ulong, int> dictionary = UnlockedRef.Invoke();
				if (dictionary == null)
				{
					__result = false;
					return false;
				}
				dictionary[steamId] = num + 1;
				__result = true;
				return false;
			}
			catch (Exception arg)
			{
				ManualLogSource log = Hotbar8Plugin.Log;
				if (log != null)
				{
					log.LogWarning((object)$"ServerAddSlot patch failed, falling back to vanilla: {arg}");
				}
				return true;
			}
		}

		[HarmonyPatch(typeof(HotbarSlots), "LoadSaveData")]
		[HarmonyPostfix]
		private static void LoadSaveDataPostfix(HotbarSlotsSaveData data)
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_003c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			if (data == null || data.version == 0 || data.players == null)
			{
				return;
			}
			try
			{
				Dictionary<ulong, int> dictionary = UnlockedRef.Invoke();
				if (dictionary == null)
				{
					return;
				}
				foreach (HotbarSlotsEntry player in data.players)
				{
					if (player.steamId != 0L)
					{
						dictionary[player.steamId] = Math.Min(Math.Max(player.slots, 2), 8);
					}
				}
			}
			catch (Exception arg)
			{
				ManualLogSource log = Hotbar8Plugin.Log;
				if (log != null)
				{
					log.LogWarning((object)$"LoadSaveData patch failed: {arg}");
				}
			}
		}

		[HarmonyPatch(typeof(SatchelInteractable), "CanInteract")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> SatchelCanInteractTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			return ReplaceFourWithEight(instructions);
		}

		[HarmonyPatch(typeof(SatchelItem), "OnPickUp")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> SatchelOnPickUpTranspiler(IEnumerable<CodeInstruction> instructions)
		{
			return ReplaceFourWithEight(instructions);
		}

		private static IEnumerable<CodeInstruction> ReplaceFourWithEight(IEnumerable<CodeInstruction> instructions)
		{
			bool replaced = false;
			foreach (CodeInstruction instruction in instructions)
			{
				if (instruction.opcode == OpCodes.Ldc_I4_4)
				{
					yield return new CodeInstruction(OpCodes.Ldc_I4_8, (object)null);
					replaced = true;
				}
				else
				{
					yield return instruction;
				}
			}
			if (!replaced)
			{
				ManualLogSource log = Hotbar8Plugin.Log;
				if (log != null)
				{
					log.LogWarning((object)"A patched method no longer contains the 4-slot constant; the hotbar limit may be partially enforced.");
				}
			}
		}
	}
}