Decompiled source of MijuSixSlots v1.0.0

plugins/SixSlots.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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("SixSlots")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.2.0")]
[assembly: AssemblyInformationalVersion("1.0.2")]
[assembly: AssemblyProduct("SixSlots")]
[assembly: AssemblyTitle("SixSlots")]
[assembly: AssemblyVersion("1.0.2.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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 MijuMods.SixSlots
{
	[HarmonyPatch]
	internal static class InventoryPatches
	{
		private const int TargetSlotCount = 6;

		private static readonly string[] FrameFieldCandidates = new string[2] { "itemSlotIconFrames", "itemSlotIconFrame" };

		private static readonly string[] IconFieldCandidates = new string[2] { "itemSlotIcons", "itemSlotIcon" };

		[HarmonyPatch(typeof(PlayerControllerB), "Awake")]
		[HarmonyPostfix]
		private static void PlayerAwakePostfix(PlayerControllerB __instance)
		{
			ExpandPlayerInventory(__instance);
		}

		[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
		[HarmonyPostfix]
		private static void PlayerConnectedPostfix(PlayerControllerB __instance)
		{
			ExpandPlayerInventory(__instance);
		}

		[HarmonyPatch(typeof(HUDManager), "Start")]
		[HarmonyPostfix]
		private static void HudStartPostfix(HUDManager __instance)
		{
			try
			{
				ExpandHud(__instance);
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Failed to create six-slot HUD: {arg}");
			}
		}

		private static void ExpandPlayerInventory(PlayerControllerB player)
		{
			if (player.ItemSlots == null)
			{
				Plugin.Log.LogWarning((object)"Player ItemSlots was null; expansion was skipped.");
			}
			else
			{
				if (player.ItemSlots.Length == 6)
				{
					return;
				}
				if (player.ItemSlots.Length > 6)
				{
					Plugin.Log.LogWarning((object)$"Another mod already provides {player.ItemSlots.Length} slots. Six Slots will not shrink it.");
					return;
				}
				Array.Resize(ref player.ItemSlots, 6);
				if (player.currentItemSlot >= 6)
				{
					player.currentItemSlot = 0;
				}
				Plugin.Log.LogInfo((object)$"Expanded {player.playerUsername}'s inventory to {6} slots.");
			}
		}

		private static void ExpandHud(HUDManager hud)
		{
			FieldInfo field;
			Image[] imageArray = GetImageArray(hud, FrameFieldCandidates, out field);
			FieldInfo field2;
			Image[] imageArray2 = GetImageArray(hud, IconFieldCandidates, out field2);
			if (imageArray == null || imageArray2 == null || field == null || field2 == null)
			{
				Plugin.Log.LogError((object)"Could not locate the game's inventory HUD arrays.");
			}
			else
			{
				if (imageArray.Length >= 6 && imageArray2.Length >= 6)
				{
					return;
				}
				if (imageArray.Length == 0 || imageArray2.Length == 0)
				{
					Plugin.Log.LogError((object)"The inventory HUD arrays were empty.");
					return;
				}
				Image val = imageArray[Math.Min(imageArray.Length - 1, 3)];
				Image val2 = imageArray2[Math.Min(imageArray2.Length - 1, 3)];
				List<Image> list = new List<Image>(imageArray);
				List<Image> list2 = new List<Image>(imageArray2);
				float spacing = CalculateSpacing(imageArray);
				while (list.Count < 6)
				{
					int count = list.Count;
					Image val3 = Object.Instantiate<Image>(val, ((Component)val).transform.parent);
					((Object)val3).name = $"ItemSlotFrame ({count})";
					PositionClone(((Graphic)val3).rectTransform, ((Graphic)val).rectTransform, spacing, count - (imageArray.Length - 1));
					((Component)val3).gameObject.SetActive(true);
					list.Add(val3);
					Image val4 = Object.Instantiate<Image>(val2, ((Component)val2).transform.parent);
					((Object)val4).name = $"ItemSlotIcon ({count})";
					PositionClone(((Graphic)val4).rectTransform, ((Graphic)val2).rectTransform, spacing, count - (imageArray2.Length - 1));
					val4.sprite = null;
					((Behaviour)val4).enabled = false;
					((Component)val4).gameObject.SetActive(true);
					list2.Add(val4);
				}
				Recenter(list);
				Recenter(list2);
				field.SetValue(hud, list.ToArray());
				field2.SetValue(hud, list2.ToArray());
				Plugin.Log.LogInfo((object)"Created two additional inventory HUD slots.");
			}
		}

		private static Image[]? GetImageArray(HUDManager hud, IEnumerable<string> candidateNames, out FieldInfo? field)
		{
			Type type = ((object)hud).GetType();
			foreach (string candidateName in candidateNames)
			{
				field = AccessTools.Field(type, candidateName);
				if (field?.GetValue(hud) is Image[] result)
				{
					return result;
				}
			}
			FieldInfo[] fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
			foreach (FieldInfo fieldInfo in fields)
			{
				if (fieldInfo.FieldType == typeof(Image[]))
				{
					string text = fieldInfo.Name.ToLowerInvariant();
					if (text.Contains("item") && text.Contains("slot"))
					{
						field = fieldInfo;
						return fieldInfo.GetValue(hud) as Image[];
					}
				}
			}
			field = null;
			return null;
		}

		private static float CalculateSpacing(Image[] images)
		{
			//IL_000e: 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)
			if (images.Length >= 2)
			{
				float num = ((Graphic)images[1]).rectTransform.anchoredPosition.x - ((Graphic)images[0]).rectTransform.anchoredPosition.x;
				if (Mathf.Abs(num) > 1f)
				{
					return num;
				}
			}
			return 42f;
		}

		private static void PositionClone(RectTransform clone, RectTransform template, float spacing, int offset)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: 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_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			clone.anchorMin = template.anchorMin;
			clone.anchorMax = template.anchorMax;
			clone.pivot = template.pivot;
			clone.sizeDelta = template.sizeDelta;
			((Transform)clone).localScale = ((Transform)template).localScale;
			((Transform)clone).localRotation = ((Transform)template).localRotation;
			clone.anchoredPosition = template.anchoredPosition + Vector2.right * spacing * (float)offset;
		}

		private static void Recenter(IReadOnlyList<Image> images)
		{
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_008a: Unknown result type (might be due to invalid IL or missing references)
			if (images.Count == 0)
			{
				return;
			}
			float num = float.MaxValue;
			float num2 = float.MinValue;
			foreach (Image image in images)
			{
				float x = ((Graphic)image).rectTransform.anchoredPosition.x;
				num = Mathf.Min(num, x);
				num2 = Mathf.Max(num2, x);
			}
			float num3 = (num + num2) * 0.5f;
			foreach (Image image2 in images)
			{
				RectTransform rectTransform = ((Graphic)image2).rectTransform;
				rectTransform.anchoredPosition -= Vector2.right * num3;
			}
		}
	}
	[BepInPlugin("MijuMods.SixSlots", "Six Slots", "1.0.2")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "MijuMods.SixSlots";

		public const string PluginName = "Six Slots";

		public const string PluginVersion = "1.0.2";

		internal static ManualLogSource Log;

		internal static Harmony Harmony;

		private void Awake()
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_001a: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			Harmony = new Harmony("MijuMods.SixSlots");
			Harmony.PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Six Slots 1.0.2 loaded. Inventory target: 6 slots.");
		}
	}
}