Decompiled source of DelTT v1.0.0

plugins/DelTT.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("DelTT")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("DelTT")]
[assembly: AssemblyTitle("DelTT")]
[assembly: AssemblyVersion("1.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 DelTT
{
	[BepInPlugin("lukas.deltt", "DelTT", "1.0.0")]
	public class DelTTPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "lukas.deltt";

		public const string PluginName = "DelTT";

		public const string PluginVersion = "1.0.0";

		internal static ConfigEntry<KeyCode> DeleteKey;

		internal static ConfigEntry<bool> ConfirmFirstDelete;

		internal static ConfigEntry<TextLanguage> Language;

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0067: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Expected O, but got Unknown
			DeleteKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "DeleteKey", (KeyCode)127, "Key that deletes the hovered item in the open inventory or chest.");
			ConfirmFirstDelete = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "ConfirmFirstDelete", true, "Show a confirmation popup on the first delete after opening the inventory or a chest.");
			Language = ((BaseUnityPlugin)this).Config.Bind<TextLanguage>("General", "Language", TextLanguage.Auto, "Language of the popup and messages. Auto = Slovak when the game is in Slovak, otherwise English.");
			_harmony = new Harmony("lukas.deltt");
			_harmony.PatchAll(typeof(InventoryGuiPatches));
			((BaseUnityPlugin)this).Logger.LogInfo((object)"DelTT 1.0.0 loaded.");
		}

		private void OnDestroy()
		{
			ItemDeleter.CancelPending();
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}
	}
	[HarmonyPatch(typeof(InventoryGui))]
	internal static class InventoryGuiPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("Show")]
		private static void ShowPostfix()
		{
			ItemDeleter.ResetSession();
		}

		[HarmonyPostfix]
		[HarmonyPatch("Hide")]
		private static void HidePostfix()
		{
			ItemDeleter.CancelPending();
			ItemDeleter.ResetSession();
		}

		[HarmonyPostfix]
		[HarmonyPatch("Update")]
		private static void UpdatePostfix(InventoryGui __instance)
		{
			ItemDeleter.Tick(__instance);
		}
	}
	internal static class ItemDeleter
	{
		[CompilerGenerated]
		private static class <>O
		{
			public static PopupButtonCallback <0>__OnYes;

			public static PopupButtonCallback <1>__OnNo;
		}

		private static readonly FieldRef<InventoryGui, GameObject> DragGoRef = AccessTools.FieldRefAccess<InventoryGui, GameObject>("m_dragGo");

		private static bool _confirmedThisSession;

		private static bool _popupOpen;

		private static Inventory _pendingInventory;

		private static ItemData _pendingItem;

		public static void ResetSession()
		{
			_confirmedThisSession = false;
		}

		public static void Tick(InventoryGui gui)
		{
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			Inventory inventory;
			ItemData item;
			if (_popupOpen)
			{
				if (!UnifiedPopup.IsVisible() || !InventoryGui.IsVisible())
				{
					ClearPending();
				}
				else if (ZInput.GetKeyDown((KeyCode)27, true))
				{
					OnNo();
				}
			}
			else if (InventoryGui.IsVisible() && ZInput.GetKeyDown(DelTTPlugin.DeleteKey.Value, true) && !IsInputBlocked(gui) && TryGetHoveredItem(gui, out inventory, out item))
			{
				if (_confirmedThisSession || !DelTTPlugin.ConfirmFirstDelete.Value || !UnifiedPopup.IsAvailable())
				{
					DeleteItem(inventory, item);
				}
				else
				{
					AskForConfirmation(inventory, item);
				}
			}
		}

		public static void CancelPending()
		{
			if (_popupOpen)
			{
				ClearPending();
				if (UnifiedPopup.IsAvailable() && UnifiedPopup.IsVisible())
				{
					UnifiedPopup.Pop();
				}
			}
		}

		private static bool IsInputBlocked(InventoryGui gui)
		{
			if ((Object)(object)Chat.instance != (Object)null && Chat.instance.HasFocus())
			{
				return true;
			}
			if (Console.IsVisible() || TextInput.IsVisible() || Menu.IsVisible() || Minimap.IsOpen())
			{
				return true;
			}
			if ((Object)(object)DragGoRef.Invoke(gui) != (Object)null || ((Object)(object)gui.m_splitDialog != (Object)null && gui.m_splitDialog.IsActive))
			{
				return true;
			}
			return IsTextFieldFocused();
		}

		private static bool IsTextFieldFocused()
		{
			GameObject val = (((Object)(object)EventSystem.current != (Object)null) ? EventSystem.current.currentSelectedGameObject : null);
			if ((Object)(object)val == (Object)null)
			{
				return false;
			}
			TMP_InputField component = val.GetComponent<TMP_InputField>();
			if ((Object)(object)component != (Object)null && component.isFocused)
			{
				return true;
			}
			InputField component2 = val.GetComponent<InputField>();
			if ((Object)(object)component2 != (Object)null)
			{
				return component2.isFocused;
			}
			return false;
		}

		private static bool TryGetHoveredItem(InventoryGui gui, out Inventory inventory, out ItemData item)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			Vector2i cursor = default(Vector2i);
			((Vector2i)(ref cursor))..ctor(ZInput.pointerPosition);
			if (TryGetFromGrid(gui.m_playerGrid, cursor, out inventory, out item))
			{
				return true;
			}
			if (gui.IsContainerOpen() && TryGetFromGrid(gui.ContainerGrid, cursor, out inventory, out item))
			{
				return true;
			}
			inventory = null;
			item = null;
			return false;
		}

		private static bool TryGetFromGrid(InventoryGrid grid, Vector2i cursor, out Inventory inventory, out ItemData item)
		{
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			inventory = null;
			item = null;
			if ((Object)(object)grid == (Object)null || !((Component)grid).gameObject.activeInHierarchy || grid.GetInventory() == null)
			{
				return false;
			}
			item = grid.GetItem(cursor);
			if (item == null)
			{
				return false;
			}
			inventory = grid.GetInventory();
			return true;
		}

		private static void AskForConfirmation(Inventory inventory, ItemData item)
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: Expected O, but got Unknown
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Expected O, but got Unknown
			//IL_0063: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Expected O, but got Unknown
			_pendingInventory = inventory;
			_pendingItem = item;
			_popupOpen = true;
			string text = string.Format(Texts.ConfirmBody, "<color=orange>" + Describe(item) + "</color>");
			string confirmHeader = Texts.ConfirmHeader;
			object obj = <>O.<0>__OnYes;
			if (obj == null)
			{
				PopupButtonCallback val = OnYes;
				<>O.<0>__OnYes = val;
				obj = (object)val;
			}
			object obj2 = <>O.<1>__OnNo;
			if (obj2 == null)
			{
				PopupButtonCallback val2 = OnNo;
				<>O.<1>__OnNo = val2;
				obj2 = (object)val2;
			}
			UnifiedPopup.Push((PopupBase)new YesNoPopup(confirmHeader, text, (PopupButtonCallback)obj, (PopupButtonCallback)obj2, false, true));
		}

		private static void OnYes()
		{
			Inventory pendingInventory = _pendingInventory;
			ItemData pendingItem = _pendingItem;
			ClearPending();
			UnifiedPopup.Pop();
			_confirmedThisSession = true;
			if (InventoryGui.IsVisible())
			{
				DeleteItem(pendingInventory, pendingItem);
			}
		}

		private static void OnNo()
		{
			ClearPending();
			UnifiedPopup.Pop();
		}

		private static void ClearPending()
		{
			_popupOpen = false;
			_pendingInventory = null;
			_pendingItem = null;
		}

		private static void DeleteItem(Inventory inventory, ItemData item)
		{
			if (inventory != null && item != null && inventory.ContainsItem(item))
			{
				Player localPlayer = Player.m_localPlayer;
				if ((Object)(object)localPlayer != (Object)null && inventory == ((Humanoid)localPlayer).GetInventory() && ((Humanoid)localPlayer).IsItemEquiped(item))
				{
					((Humanoid)localPlayer).UnequipItem(item, false);
				}
				string arg = Describe(item);
				if (inventory.RemoveItem(item) && localPlayer != null)
				{
					((Character)localPlayer).Message((MessageType)1, string.Format(Texts.Deleted, arg), 0, (Sprite)null, false);
				}
			}
		}

		private static string Describe(ItemData item)
		{
			string text = ((Localization.instance != null) ? Localization.instance.Localize(item.m_shared.m_name) : item.m_shared.m_name);
			if (item.m_stack <= 1)
			{
				return text;
			}
			return $"{text} x{item.m_stack}";
		}
	}
	internal enum TextLanguage
	{
		Auto,
		English,
		Slovak
	}
	internal static class Texts
	{
		private static bool UseSlovak
		{
			get
			{
				switch (DelTTPlugin.Language.Value)
				{
				case TextLanguage.Slovak:
					return true;
				case TextLanguage.English:
					return false;
				default:
					if (Localization.instance != null)
					{
						return Localization.instance.GetSelectedLanguage() == "Slovak";
					}
					return false;
				}
			}
		}

		public static string ConfirmHeader
		{
			get
			{
				if (!UseSlovak)
				{
					return "Delete item";
				}
				return "Vymazať predmet";
			}
		}

		public static string ConfirmBody
		{
			get
			{
				if (!UseSlovak)
				{
					return "Do you really want to permanently delete {0}?\n\nFurther items will be deleted without asking until you close the inventory.";
				}
				return "Naozaj chceš natrvalo vymazať {0}?\n\nĎalšie predmety sa až do zatvorenia inventára budú mazať bez pýtania.";
			}
		}

		public static string Deleted
		{
			get
			{
				if (!UseSlovak)
				{
					return "Deleted: {0}";
				}
				return "Vymazané: {0}";
			}
		}
	}
}