Decompiled source of LwfPinnedSlots v2.0.0

BepInEx/plugins/LwfPinnedSlots.dll

Decompiled 3 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BaseSystem;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Items;
using Items.Inventory;
using R3;
using Sounds;
using UI.SelectableWindow.Cell;
using UI.SelectableWindow.ISelectableWindows;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using Utility.SaveData;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace LwfPinnedSlots;

[BepInPlugin("kiyonakanata.lwfpinnedslots", "LWF Pinned Slots", "2.0.0")]
public sealed class PinnedSlotsPlugin : BaseUnityPlugin
{
	internal const string PluginGuid = "kiyonakanata.lwfpinnedslots";

	internal const string PluginName = "LWF Pinned Slots";

	internal const string PluginVersion = "2.0.0";

	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> Enabled;

	internal static ConfigEntry<bool> ShowCounts;

	internal static ConfigEntry<int> GhostBrightness;

	private InventoryManager _attached;

	private void Awake()
	{
		Log = ((BaseUnityPlugin)this).Logger;
		try
		{
			Boot();
		}
		catch (Exception ex)
		{
			Log.LogWarning((object)("[boot] LWF Pinned Slots 2.0.0 disabled (the game version may have changed): " + ex));
			((BaseUnityPlugin)this).Config.SettingChanged -= OnSettingChanged;
			((Behaviour)this).enabled = false;
		}
	}

	private void Boot()
	{
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_006f: Expected O, but got Unknown
		Enabled = ((BaseUnityPlugin)this).Config.Bind<bool>("1. General", "Enabled", true, "");
		ShowCounts = ((BaseUnityPlugin)this).Config.Bind<bool>("3. View", "Show 0 and 1 on pinned slots", true, "");
		GhostBrightness = ((BaseUnityPlugin)this).Config.Bind<int>("3. View", "Empty pinned slot icon brightness (%)", 35, new ConfigDescription("", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 100), new object[0]));
		((BaseUnityPlugin)this).Config.SettingChanged += OnSettingChanged;
		Log.LogInfo((object)("[boot] LWF Pinned Slots 2.0.0 (game " + BuildDefinition.GetCurrentVersionNumber() + ")"));
	}

	private void Update()
	{
		InventoryManager playerInventory = InventoryManager.playerInventory;
		if (!object.ReferenceEquals(playerInventory, _attached))
		{
			_attached = playerInventory;
			if (playerInventory != null)
			{
				OldSlots.MigrateOnce(((BaseUnityPlugin)this).Config, playerInventory);
				SlotView.Attach(playerInventory);
			}
		}
	}

	private void OnSettingChanged(object sender, SettingChangedEventArgs e)
	{
		SlotView.RefreshAll();
	}

	private void OnDestroy()
	{
		((BaseUnityPlugin)this).Config.SettingChanged -= OnSettingChanged;
		SlotView.Detach();
	}
}
internal static class OldSlots
{
	private const int OldSlotCount = 11;

	private static bool _done;

	internal static void MigrateOnce(ConfigFile cfg, InventoryManager inv)
	{
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Expected O, but got Unknown
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Expected O, but got Unknown
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Expected O, but got Unknown
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b7: Expected O, but got Unknown
		//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Expected O, but got Unknown
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f1: Expected O, but got Unknown
		//IL_0112: Unknown result type (might be due to invalid IL or missing references)
		if (_done)
		{
			return;
		}
		_done = true;
		try
		{
			string[] array = new string[11];
			bool flag = false;
			for (int i = 0; i < 11; i++)
			{
				ConfigDefinition val = new ConfigDefinition("2. Slots", "Slot " + (i + 1));
				ConfigEntry<string> val2 = cfg.Bind<string>(val, "", new ConfigDescription("", (AcceptableValueBase)null, new object[0]));
				array[i] = ((val2.Value == null) ? "" : val2.Value.Trim());
				if (array[i].Length > 0)
				{
					flag = true;
				}
				cfg.Remove(val);
			}
			ConfigDefinition val3 = new ConfigDefinition("3. View", "Pin marker size");
			ConfigDefinition val4 = new ConfigDefinition("3. View", "Pin marker color");
			cfg.Bind<int>(val3, 14, new ConfigDescription("", (AcceptableValueBase)null, new object[0]));
			cfg.Bind<string>(val4, "#FFFFFF", new ConfigDescription("", (AcceptableValueBase)null, new object[0]));
			cfg.Remove(val3);
			cfg.Remove(val4);
			cfg.Save();
			if (!flag || (int)LwfSaveDataAccessor.GetInventorySortMode() != 0)
			{
				return;
			}
			string[] array2 = InventorySortOrder.Normalize((IEnumerable<string>)null);
			string[] array3 = InventorySortOrder.Normalize((IEnumerable<string>)LwfSaveDataAccessor.GetInventorySortOrder());
			for (int j = 0; j < array2.Length; j++)
			{
				if (j >= array3.Length || array3[j] != array2[j])
				{
					return;
				}
			}
			string[] array4 = new string[array2.Length];
			List<string> list = new List<string>(array2);
			for (int k = 0; k < 11 && k < array4.Length; k++)
			{
				if (array[k].Length > 0 && list.Remove(array[k]))
				{
					array4[k] = array[k];
				}
			}
			for (int l = 0; l < array4.Length; l++)
			{
				if (array4[l] == null)
				{
					array4[l] = list[0];
					list.RemoveAt(0);
				}
			}
			LwfSaveDataAccessor.SetInventorySort((InventorySortMode)1, (IEnumerable<string>)array4);
			PinnedSlotsPlugin.Log.LogInfo((object)("[migrate] copied 1.x pinned slots into the game's fixed positions: " + string.Join(", ", array4)));
		}
		catch (Exception ex)
		{
			PinnedSlotsPlugin.Log.LogWarning((object)("[migrate] " + ex));
		}
	}
}
internal static class SlotView
{
	private const string NoneId = "None";

	private const string CaretakerId = "Summon-Caretaker";

	private static readonly HashSet<CellComponents> _ghosted = new HashSet<CellComponents>();

	private static IDisposable _changed;

	private static IDisposable _sortChanged;

	internal static void Attach(InventoryManager inv)
	{
		Detach();
		if (inv != null && inv.Hotbar != null)
		{
			_changed = ObservableSubscribeExtensions.Subscribe<Unit>(inv.Hotbar.OnChanged, (Action<Unit>)delegate
			{
				RefreshAll();
			});
			_sortChanged = ObservableSubscribeExtensions.Subscribe<Unit>(LwfSaveDataAccessor.OnInventorySortChanged, (Action<Unit>)delegate
			{
				RefreshAll();
			});
			RefreshAll();
		}
	}

	internal static void Detach()
	{
		if (_changed != null)
		{
			_changed.Dispose();
			_changed = null;
		}
		if (_sortChanged != null)
		{
			_sortChanged.Dispose();
			_sortChanged = null;
		}
		_ghosted.Clear();
		SlotMouse.DetachAll();
	}

	internal static void RefreshAll()
	{
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Invalid comparison between Unknown and I4
		//IL_0168: Unknown result type (might be due to invalid IL or missing references)
		//IL_0133: Unknown result type (might be due to invalid IL or missing references)
		InventoryManager playerInventory = InventoryManager.playerInventory;
		if (playerInventory == null || playerInventory.GetWindow() == null || playerInventory.Hotbar == null)
		{
			return;
		}
		try
		{
			List<CellComponents> cellComponentsList = ((WindowWithCells)playerInventory.GetWindow()).GetCellComponentsList();
			if (cellComponentsList == null)
			{
				return;
			}
			bool flag = (int)LwfSaveDataAccessor.GetInventorySortMode() == 1;
			string[] array = InventorySortOrder.Normalize((IEnumerable<string>)LwfSaveDataAccessor.GetInventorySortOrder());
			bool flag2 = PinnedSlotsPlugin.Enabled.Value && PinnedSlotsPlugin.ShowCounts.Value && flag;
			float num = (float)Mathf.Clamp(PinnedSlotsPlugin.GhostBrightness.Value, 0, 100) / 100f;
			for (int i = 0; i < cellComponentsList.Count; i++)
			{
				CellComponents val = cellComponentsList[i];
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				SlotMouse.Ensure(val, i);
				string text = playerInventory.Hotbar.ItemID(i);
				int num2 = playerInventory.Hotbar.ItemCount(i);
				bool flag3 = num2 <= 0 || text == "None";
				string text2 = ((flag && i < array.Length) ? array[i] : null);
				if (flag2 && text2 != null && flag3 && text2 != "Summon-Caretaker")
				{
					Sprite sprite = ItemParamGetter.GetSprite(text2);
					if ((Object)(object)sprite != (Object)null)
					{
						val.SetImageSprite(sprite);
					}
					val.SetImageColor(new Color(1f, 1f, 1f, num));
					val.SetText("0");
					_ghosted.Add(val);
					continue;
				}
				if (_ghosted.Remove(val))
				{
					val.SetImageColor(Color.white);
					if (flag3)
					{
						val.SetImageSprite(ItemParamGetter.GetSprite("None"));
						val.SetText("");
					}
				}
				if (!flag3 && num2 == 1)
				{
					val.SetText((flag2 && text2 != null && text != "Wand") ? "1" : "");
				}
			}
		}
		catch (Exception ex)
		{
			PinnedSlotsPlugin.Log.LogError((object)("[view] " + ex));
		}
	}
}
internal static class SlotOrder
{
	private const string NoneId = "None";

	internal static void Swap(int a, int b)
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_001c: Invalid comparison between Unknown and I4
		InventoryManager playerInventory = InventoryManager.playerInventory;
		if (playerInventory == null || playerInventory.Hotbar == null || a == b)
		{
			return;
		}
		bool flag = (int)LwfSaveDataAccessor.GetInventorySortMode() != 1;
		List<string> list2;
		if (flag)
		{
			List<string> list = new List<string>();
			for (int i = 0; i < playerInventory.Hotbar.Count; i++)
			{
				string text = playerInventory.Hotbar.ItemID(i);
				if (text != "None" && playerInventory.Hotbar.ItemCount(i) > 0 && !list.Contains(text))
				{
					list.Add(text);
				}
			}
			list2 = new List<string>(InventorySortOrder.Normalize((IEnumerable<string>)list));
		}
		else
		{
			list2 = new List<string>(InventorySortOrder.Normalize((IEnumerable<string>)LwfSaveDataAccessor.GetInventorySortOrder()));
		}
		if (a >= 0 && b >= 0 && a < list2.Count && b < list2.Count)
		{
			string value = list2[a];
			list2[a] = list2[b];
			list2[b] = value;
			LwfSaveDataAccessor.SetInventorySort((InventorySortMode)1, (IEnumerable<string>)list2);
			if ((Object)(object)UISoundPlayer.Instance != (Object)null)
			{
				UISoundPlayer.Instance.PlayRotateSE(true);
			}
			PinnedSlotsPlugin.Log.LogInfo((object)("[edit] swap slot " + (a + 1) + " <-> " + (b + 1) + " (" + list2[b] + " <-> " + list2[a] + ")" + (flag ? " and switched to fixed positions" : "")));
		}
	}
}
internal static class SlotMouse
{
	internal static void Ensure(CellComponents cell, int index)
	{
		GameObject val = HostOf(cell);
		if (!((Object)(object)val == (Object)null))
		{
			SlotCellHandle slotCellHandle = val.GetComponent<SlotCellHandle>();
			if ((Object)(object)slotCellHandle == (Object)null)
			{
				slotCellHandle = val.AddComponent<SlotCellHandle>();
			}
			slotCellHandle.Index = index;
			slotCellHandle.Cell = cell;
		}
	}

	internal static void DetachAll()
	{
		SlotCellHandle.DestroyGhost();
		InventoryManager playerInventory = InventoryManager.playerInventory;
		if (playerInventory == null || playerInventory.GetWindow() == null)
		{
			return;
		}
		List<CellComponents> cellComponentsList = ((WindowWithCells)playerInventory.GetWindow()).GetCellComponentsList();
		if (cellComponentsList == null)
		{
			return;
		}
		foreach (CellComponents item in cellComponentsList)
		{
			if ((Object)(object)item == (Object)null)
			{
				continue;
			}
			GameObject val = HostOf(item);
			if (!((Object)(object)val == (Object)null))
			{
				SlotCellHandle component = val.GetComponent<SlotCellHandle>();
				if ((Object)(object)component != (Object)null)
				{
					Object.Destroy((Object)(object)component);
				}
			}
		}
	}

	private static GameObject HostOf(CellComponents cell)
	{
		Button button = cell.GetButton();
		if ((Object)(object)button != (Object)null)
		{
			return ((Component)button).gameObject;
		}
		return cell.GetGameObject();
	}
}
internal sealed class SlotCellHandle : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler, IEventSystemHandler
{
	private const string GhostName = "LwfPinnedSlots.Ghost";

	private const string NoneId = "None";

	private static GameObject _ghost;

	internal int Index;

	internal CellComponents Cell;

	public void OnBeginDrag(PointerEventData e)
	{
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Invalid comparison between Unknown and I4
		if ((int)e.button != 0 || !PinnedSlotsPlugin.Enabled.Value)
		{
			return;
		}
		InventoryManager playerInventory = InventoryManager.playerInventory;
		if (playerInventory == null || playerInventory.Hotbar == null)
		{
			return;
		}
		string text = playerInventory.Hotbar.ItemID(Index);
		bool flag = playerInventory.Hotbar.ItemCount(Index) > 0 && text != "None";
		string text2 = null;
		if ((int)LwfSaveDataAccessor.GetInventorySortMode() == 1)
		{
			string[] array = InventorySortOrder.Normalize((IEnumerable<string>)LwfSaveDataAccessor.GetInventorySortOrder());
			if (Index < array.Length)
			{
				text2 = array[Index];
			}
		}
		if (flag || text2 != null)
		{
			MakeGhost(ItemParamGetter.GetSprite(flag ? text : text2), e);
		}
	}

	public void OnDrag(PointerEventData e)
	{
		MoveGhost(e);
	}

	public void OnEndDrag(PointerEventData e)
	{
		DestroyGhost();
	}

	public void OnDrop(PointerEventData e)
	{
		if (PinnedSlotsPlugin.Enabled.Value && !((Object)(object)e.pointerDrag == (Object)null))
		{
			SlotCellHandle component = e.pointerDrag.GetComponent<SlotCellHandle>();
			if (!((Object)(object)component == (Object)null) && !((Object)(object)component == (Object)(object)this) && component.Index != Index)
			{
				SlotOrder.Swap(component.Index, Index);
			}
		}
	}

	private void MakeGhost(Sprite sprite, PointerEventData e)
	{
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: Expected O, but got Unknown
		//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f0: Unknown result type (might be due to invalid IL or missing references)
		DestroyGhost();
		Image val = (((Object)(object)Cell != (Object)null) ? Cell.GetImage() : null);
		Canvas componentInParent = ((Component)this).GetComponentInParent<Canvas>();
		if (!((Object)(object)val == (Object)null) && !((Object)(object)componentInParent == (Object)null))
		{
			componentInParent = componentInParent.rootCanvas;
			_ghost = new GameObject("LwfPinnedSlots.Ghost", new Type[2]
			{
				typeof(RectTransform),
				typeof(Image)
			});
			_ghost.transform.SetParent(((Component)componentInParent).transform, false);
			_ghost.transform.SetAsLastSibling();
			Image component = _ghost.GetComponent<Image>();
			component.sprite = sprite;
			((Graphic)component).raycastTarget = false;
			((Graphic)component).color = new Color(1f, 1f, 1f, 0.8f);
			RectTransform component2 = _ghost.GetComponent<RectTransform>();
			Rect rect = ((Graphic)val).rectTransform.rect;
			component2.sizeDelta = ((Rect)(ref rect)).size;
			MoveGhost(e);
		}
	}

	private static void MoveGhost(PointerEventData e)
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: 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)
		if ((Object)(object)_ghost == (Object)null)
		{
			return;
		}
		RectTransform component = _ghost.GetComponent<RectTransform>();
		Canvas componentInParent = _ghost.GetComponentInParent<Canvas>();
		if (!((Object)(object)componentInParent == (Object)null))
		{
			Camera val = (((int)componentInParent.renderMode == 0) ? null : componentInParent.worldCamera);
			Transform transform = ((Component)componentInParent).transform;
			Vector3 position = default(Vector3);
			if (RectTransformUtility.ScreenPointToWorldPointInRectangle((RectTransform)(object)((transform is RectTransform) ? transform : null), e.position, val, ref position))
			{
				((Transform)component).position = position;
			}
		}
	}

	internal static void DestroyGhost()
	{
		if ((Object)(object)_ghost != (Object)null)
		{
			Object.Destroy((Object)(object)_ghost);
			_ghost = null;
		}
	}

	private void OnDisable()
	{
		DestroyGhost();
	}
}