Decompiled source of ExtraSlots v1.0.5

ExtraSlots.dll

Decompiled a month 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.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Photon.Pun;
using TMPro;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;

[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 ExtraSlots
{
	[BepInPlugin("rev.extraslots", "ExtraSlots", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		public const string Guid = "rev.extraslots";

		public const string Name = "ExtraSlots";

		public const string Version = "1.0.0";

		public const int VanillaSlots = 3;

		public const int MaxSlots = 9;

		internal static ManualLogSource Log;

		internal static ConfigEntry<int> SlotCount;

		internal static ConfigEntry<bool> CenterRow;

		internal static readonly List<ConfigEntry<Key>> SlotKeys = new List<ConfigEntry<Key>>();

		internal static int ExtraSlotCount => Mathf.Clamp(SlotCount.Value, 3, 9) - 3;

		private void Awake()
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Expected O, but got Unknown
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			SlotCount = ((BaseUnityPlugin)this).Config.Bind<int>("General", "SlotCount", 5, new ConfigDescription($"How many item slots the player has. {3} = vanilla.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(3, 9), Array.Empty<object>()));
			CenterRow = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "CenterRow", true, "Keep the row of slots centred on screen. Turn off to leave the vanilla slots in place and add the new ones to the right.");
			for (int i = 3; i < 9; i++)
			{
				Key val = (Key)((i < 9) ? (41 + i) : 50);
				SlotKeys.Add(((BaseUnityPlugin)this).Config.Bind<Key>("Keys", $"Slot{i + 1}", val, $"Key that equips or unequips the item in slot {i + 1}."));
			}
			new Harmony("rev.extraslots").PatchAll(typeof(Patches));
			((Component)this).gameObject.AddComponent<SlotHotkeys>();
			Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
			Log.LogInfo((object)string.Format("{0} {1} loaded ({2} slots)", "ExtraSlots", "1.0.0", SlotCount.Value));
		}
	}
	internal static class Patches
	{
		[HarmonyPatch(typeof(Inventory), "Awake")]
		[HarmonyPostfix]
		private static void InventoryAwake(Inventory __instance)
		{
			List<InventorySpot> allSpots = __instance.GetAllSpots();
			while (allSpots.Count < 3 + Plugin.ExtraSlotCount)
			{
				allSpots.Add(null);
			}
		}

		[HarmonyPatch(typeof(Inventory), "InventorySpotAddAtIndex")]
		[HarmonyPostfix]
		private static void InventorySpotAdded(Inventory __instance, InventorySpot spot, int index)
		{
			if (index != 2 || (Object)(object)spot == (Object)null)
			{
				return;
			}
			try
			{
				SlotBuilder.BuildExtraSpots(__instance, spot);
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Failed to create extra inventory slots: {arg}");
			}
		}
	}
	internal static class SlotBuilder
	{
		private static readonly FieldRef<BatteryVisualLogic, List<GameObject>> BarsRef = AccessTools.FieldRefAccess<BatteryVisualLogic, List<GameObject>>("bars");

		private static readonly MethodInfo BatteryStart = AccessTools.Method(typeof(BatteryVisualLogic), "Start", (Type[])null, (Type[])null);

		private static readonly FieldRef<BatteryVisualLogic, float> ScaleOriginalRef = AccessTools.FieldRefAccess<BatteryVisualLogic, float>("targetScaleOriginal");

		private static readonly FieldRef<BatteryVisualLogic, float> RotationOriginalRef = AccessTools.FieldRefAccess<BatteryVisualLogic, float>("targetRotationOriginal");

		private static readonly FieldRef<BatteryVisualLogic, Vector3> PositionOriginalRef = AccessTools.FieldRefAccess<BatteryVisualLogic, Vector3>("targetPositionOriginal");

		public static void BuildExtraSpots(Inventory inventory, InventorySpot lastVanillaSpot)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0043: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: 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_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			//IL_009f: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: Unknown result type (might be due to invalid IL or missing references)
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_014e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0153: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_0161: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Unknown result type (might be due to invalid IL or missing references)
			//IL_0175: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0196: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_021b: Unknown result type (might be due to invalid IL or missing references)
			//IL_021d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0223: Unknown result type (might be due to invalid IL or missing references)
			//IL_0225: Unknown result type (might be due to invalid IL or missing references)
			int extraSlotCount = Plugin.ExtraSlotCount;
			if (extraSlotCount <= 0)
			{
				return;
			}
			List<InventorySpot> allSpots = inventory.GetAllSpots();
			while (allSpots.Count < 3 + extraSlotCount)
			{
				allSpots.Add(null);
			}
			BatteryVisualLogic componentInChildren = ((Component)lastVanillaSpot).GetComponentInChildren<BatteryVisualLogic>(true);
			Transform parent = ((Component)lastVanillaSpot).transform.parent;
			Vector2 val = ((SemiUI)lastVanillaSpot).showPosition;
			Vector2 hidePosition = ((SemiUI)lastVanillaSpot).hidePosition - ((SemiUI)lastVanillaSpot).showPosition;
			Vector2 val2 = StepBetweenSpots(allSpots, val);
			if (Plugin.CenterRow.Value)
			{
				Vector2 val3 = val2 * (-0.5f * (float)extraSlotCount);
				for (int i = 0; i < 3; i++)
				{
					InventorySpot val4 = allSpots[i];
					if ((Object)(object)val4 != (Object)null)
					{
						((SemiUI)val4).showPosition = ((SemiUI)val4).showPosition + val3;
						((SemiUI)val4).hidePosition = ((SemiUI)val4).hidePosition + val3;
					}
				}
				val += val3;
			}
			for (int j = 3; j < 3 + extraSlotCount; j++)
			{
				if (!((Object)(object)allSpots[j] != (Object)null))
				{
					GameObject val5 = Object.Instantiate<GameObject>(((Component)lastVanillaSpot).gameObject, parent);
					val5.SetActive(false);
					((Object)val5).name = $"Inventory Spot {j + 1}";
					PhotonView[] componentsInChildren = val5.GetComponentsInChildren<PhotonView>(true);
					for (int k = 0; k < componentsInChildren.Length; k++)
					{
						Object.DestroyImmediate((Object)(object)componentsInChildren[k]);
					}
					Vector2 val6 = val + val2 * (float)(j - 2);
					val5.transform.localPosition = new Vector3(val6.x, val6.y, ((Component)lastVanillaSpot).transform.localPosition.z);
					val5.transform.localRotation = ((Component)lastVanillaSpot).transform.localRotation;
					val5.transform.localScale = ((Component)lastVanillaSpot).transform.localScale;
					BatteryVisualLogic[] componentsInChildren2 = val5.GetComponentsInChildren<BatteryVisualLogic>(true);
					foreach (BatteryVisualLogic val7 in componentsInChildren2)
					{
						ApplyRestingTransform(val7, componentInChildren);
						((Component)val7).gameObject.SetActive(true);
						ResetBatteryBars(val7);
						BatteryStart?.Invoke(val7, null);
					}
					InventorySpot component = val5.GetComponent<InventorySpot>();
					component.inventorySpotIndex = j;
					((SemiUI)component).showPosition = val6;
					((SemiUI)component).hidePosition = hidePosition;
					((SemiUI)component).textRectTransform = null;
					SetSlotLabel(component, lastVanillaSpot, j);
					val5.SetActive(true);
					Plugin.Log.LogInfo((object)$"Created inventory slot {j + 1}");
				}
			}
		}

		private static Vector2 StepBetweenSpots(List<InventorySpot> spots, Vector2 anchor)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: 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_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			InventorySpot val = ((spots.Count > 1) ? spots[1] : null);
			if ((Object)(object)val != (Object)null)
			{
				Vector2 result = anchor - ((SemiUI)val).showPosition;
				if (((Vector2)(ref result)).sqrMagnitude > 1f)
				{
					return result;
				}
			}
			Transform transform = ((Component)spots[2]).transform;
			RectTransform val2 = (RectTransform)(object)((transform is RectTransform) ? transform : null);
			float num;
			if ((Object)(object)val2 != (Object)null)
			{
				Rect rect = val2.rect;
				if (((Rect)(ref rect)).width > 1f)
				{
					rect = val2.rect;
					num = ((Rect)(ref rect)).width;
					goto IL_007f;
				}
			}
			num = 40f;
			goto IL_007f;
			IL_007f:
			return new Vector2(num, 0f);
		}

		private static void ResetBatteryBars(BatteryVisualLogic battery)
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			BarsRef.Invoke(battery)?.Clear();
			if ((Object)(object)battery.batteryBarContainer == (Object)null)
			{
				return;
			}
			foreach (Transform item in battery.batteryBarContainer)
			{
				Object.Destroy((Object)(object)((Component)item).gameObject);
			}
		}

		private static void ApplyRestingTransform(BatteryVisualLogic battery, BatteryVisualLogic source)
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: 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)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: 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)
			float num = 1f;
			float num2 = 0f;
			Vector3 localPosition = ((Component)battery).transform.localPosition;
			if ((Object)(object)source != (Object)null)
			{
				num = ScaleOriginalRef.Invoke(source);
				num2 = RotationOriginalRef.Invoke(source);
				localPosition = PositionOriginalRef.Invoke(source);
			}
			if (num <= 0f)
			{
				num = 1f;
			}
			((Component)battery).transform.localPosition = localPosition;
			((Component)battery).transform.localRotation = Quaternion.Euler(0f, 0f, num2);
			((Component)battery).transform.localScale = new Vector3(num, num, num);
		}

		private static void SetSlotLabel(InventorySpot spot, InventorySpot source, int index)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			int num = index - 3;
			if (num < 0 || num >= Plugin.SlotKeys.Count)
			{
				return;
			}
			string text = KeyLabel(Plugin.SlotKeys[num].Value);
			string text2 = (((Object)(object)source.noItem != (Object)null) ? ((TMP_Text)source.noItem).text : null);
			if (string.IsNullOrEmpty(text2))
			{
				text2 = null;
			}
			TextMeshProUGUI[] componentsInChildren = ((Component)spot).GetComponentsInChildren<TextMeshProUGUI>(true);
			foreach (TextMeshProUGUI val in componentsInChildren)
			{
				if ((text2 != null && ((TMP_Text)val).text == text2) || (Object)(object)val == (Object)(object)spot.noItem)
				{
					((TMP_Text)val).text = text;
				}
			}
		}

		private unsafe static string KeyLabel(Key key)
		{
			string text = ((object)(*(Key*)(&key))/*cast due to .constrained prefix*/).ToString();
			if (!text.StartsWith("Digit"))
			{
				return text;
			}
			return text.Substring("Digit".Length);
		}
	}
	internal class SlotHotkeys : MonoBehaviour
	{
		private static readonly MethodInfo HandleInputMethod = AccessTools.Method(typeof(InventorySpot), "HandleInput", (Type[])null, (Type[])null);

		private static readonly FieldInfo ChatActiveField = AccessTools.Field(typeof(ChatManager), "chatActive");

		private static readonly FieldInfo DisableMovementField = AccessTools.Field(typeof(InputManager), "disableMovementTimer");

		private void Update()
		{
			//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_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			if (Keyboard.current == null || (Object)(object)Inventory.instance == (Object)null || InputBlocked())
			{
				return;
			}
			List<InventorySpot> allSpots = Inventory.instance.GetAllSpots();
			for (int i = 3; i < allSpots.Count && i - 3 < Plugin.SlotKeys.Count; i++)
			{
				InventorySpot val = allSpots[i];
				if (!((Object)(object)val == (Object)null))
				{
					Key value = Plugin.SlotKeys[i - 3].Value;
					if ((int)value != 0 && ((ButtonControl)Keyboard.current[value]).wasPressedThisFrame)
					{
						HandleInputMethod.Invoke(val, null);
					}
				}
			}
		}

		private static bool InputBlocked()
		{
			if (SemiFunc.MenuLevel() || SemiFunc.RunIsArena())
			{
				return true;
			}
			if ((Object)(object)ChatManager.instance != (Object)null && ChatActiveField != null && (bool)ChatActiveField.GetValue(ChatManager.instance))
			{
				return true;
			}
			if ((Object)(object)InputManager.instance != (Object)null && DisableMovementField != null && (float)DisableMovementField.GetValue(InputManager.instance) > 0f)
			{
				return true;
			}
			return false;
		}
	}
}