Decompiled source of DismantleEquipment v1.0.2

DismantleEquipment.dll

Decompiled 2 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[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("DismantleEquipment")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+0583f826b06037719a051b7f3c93ffe95d6ceab3")]
[assembly: AssemblyProduct("DismantleEquipment")]
[assembly: AssemblyTitle("DismantleEquipment")]
[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 DismantleEquipment
{
	[BepInPlugin("com.valheimmods.dismantleequipment", "Dismantle Equipment", "1.0.2")]
	public sealed class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(InventoryGui), "OnRightClickItem")]
		private static class InventoryRightClickPatch
		{
			private static bool Prefix(InventoryGrid grid, ItemData item)
			{
				if (!ZInput.GetKey((KeyCode)304, true) && !ZInput.GetKey((KeyCode)303, true))
				{
					return true;
				}
				Player localPlayer = Player.m_localPlayer;
				if ((Object)(object)localPlayer == (Object)null || (Object)(object)grid == (Object)null || grid.GetInventory() != ((Humanoid)localPlayer).GetInventory())
				{
					return true;
				}
				if (item != null && !UnifiedPopup.IsVisible())
				{
					Dismantle(localPlayer, item);
				}
				return false;
			}
		}

		private sealed class Refund
		{
			internal readonly ItemDrop Item;

			internal readonly int Amount;

			internal Refund(ItemDrop item, int amount)
			{
				Item = item;
				Amount = amount;
			}
		}

		private const string PluginId = "com.valheimmods.dismantleequipment";

		private Harmony _harmony;

		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Expected O, but got Unknown
			_harmony = new Harmony("com.valheimmods.dismantleequipment");
			_harmony.PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Dismantle Equipment loaded: Shift + right-click a crafted item.");
		}

		private void OnDestroy()
		{
			Harmony harmony = _harmony;
			if (harmony != null)
			{
				harmony.UnpatchSelf();
			}
		}

		private static void Dismantle(Player player, ItemData item)
		{
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			if (((Character)player).IsDead())
			{
				return;
			}
			if (!TryGetRefund(item, out var itemCount, out var refunds, out var reason))
			{
				((Character)player).Message((MessageType)2, reason, 0, (Sprite)null, false);
				return;
			}
			Inventory inventory = ((Humanoid)player).GetInventory();
			if (!inventory.GetAllItems().Contains(item))
			{
				return;
			}
			string text = Localize(item.m_shared.m_name);
			((Humanoid)player).UnequipItem(item, true);
			if (!inventory.RemoveItem(item, itemCount))
			{
				((Character)player).Message((MessageType)2, "The item could not be removed.", 0, (Sprite)null, false);
				return;
			}
			Vector3 val = ((Component)player).transform.position + Vector3.up * 0.5f;
			foreach (Refund item2 in refunds)
			{
				int num = item2.Amount;
				int val2 = Math.Max(1, item2.Item.m_itemData.m_shared.m_maxStackSize);
				while (num > 0)
				{
					int num2 = Math.Min(num, val2);
					ItemData obj = item2.Item.m_itemData.Clone();
					obj.m_dropPrefab = ((Component)item2.Item).gameObject;
					ItemDrop.DropItem(obj, num2, val, Quaternion.identity);
					num -= num2;
				}
			}
			((Character)player).Message((MessageType)2, "Dismantled " + itemCount + " × " + text + ".", 0, (Sprite)null, false);
		}

		private static bool TryGetRefund(ItemData item, out int itemCount, out List<Refund> refunds, out string reason)
		{
			itemCount = 0;
			refunds = new List<Refund>();
			reason = "This item has no crafting recipe and cannot be dismantled.";
			ObjectDB instance = ObjectDB.instance;
			Recipe val = ((instance != null) ? instance.GetRecipe(item) : null);
			if ((Object)(object)val == (Object)null || !val.m_enabled || val.m_noCraftOnlyUpgrade || val.m_amount <= 0 || val.m_resources == null || val.m_resources.Length == 0)
			{
				return false;
			}
			if (val.m_requireOnlyOneIngredient)
			{
				reason = "This recipe has alternative ingredients, so its original material is unknown.";
				return false;
			}
			itemCount = val.m_amount;
			if (item.m_stack < itemCount)
			{
				reason = "A complete crafting batch is needed to dismantle this item.";
				return false;
			}
			Requirement[] resources = val.m_resources;
			foreach (Requirement val2 in resources)
			{
				if (val2 == null)
				{
					reason = "The recipe has an invalid material requirement.";
					return false;
				}
				int num = 0;
				for (int j = 1; j <= item.m_quality; j++)
				{
					CraftingStation requiredStation = val.GetRequiredStation(j);
					bool flag = (Object)(object)requiredStation != (Object)null && requiredStation.m_upgrader;
					if (val2.m_upgraderResource == flag)
					{
						num = checked(num + val2.GetAmount(j));
					}
				}
				if (num > 0)
				{
					if ((Object)(object)val2.m_resItem == (Object)null || val2.m_resItem.m_itemData == null)
					{
						reason = "The recipe has an unavailable material.";
						return false;
					}
					refunds.Add(new Refund(val2.m_resItem, num));
				}
			}
			if (refunds.Count == 0)
			{
				reason = "This recipe does not use recoverable materials.";
				return false;
			}
			return true;
		}

		private static string Localize(string name)
		{
			if (Localization.instance == null)
			{
				return name;
			}
			return Localization.instance.Localize(name);
		}
	}
}