Decompiled source of CraftingStorageCompatibility v1.0.3

plugins/MultiCraftAzuCompatibility.dll

Decompiled 10 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using AzuCraftyBoxes;
using AzuCraftyBoxes.IContainers;
using BalrondItemRarity.UI;
using BepInEx;
using BepInEx.Bootstrap;
using EpicLoot_UnityLib;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyCompany("MultiCraftAzuCompatibility")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("MultiCraftAzuCompatibility")]
[assembly: AssemblyTitle("MultiCraftAzuCompatibility")]
[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;
		}
	}
}
[BepInPlugin("local.multicraft.azucraftyboxes.compatibility", "Crafting Storage Compatibility", "1.0.0")]
[BepInDependency("maximods.valheim.multicraft", "1.5.0")]
[BepInDependency("Azumatt.AzuCraftyBoxes", "1.8.19")]
[BepInDependency("balrond.astafaraios.Runeforging", "1.0.3")]
[BepInDependency("randyknapp.mods.epicloot", "0.14.13")]
public sealed class Plugin : BaseUnityPlugin
{
	public const string Id = "local.multicraft.azucraftyboxes.compatibility";

	private static Func<bool> shouldPrevent;

	private static Action<List<IContainer>> begin;

	private static Func<string, int> count;

	private Harmony harmony;

	private void Awake()
	{
		//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0106: Expected O, but got Unknown
		//IL_011d: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: Expected O, but got Unknown
		//IL_0198: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a5: Expected O, but got Unknown
		try
		{
			Assembly assembly = typeof(AzuCraftyBoxesPlugin).Assembly;
			Type type = assembly.GetType("AzuCraftyBoxes.Util.Functions.UiItemBank", throwOnError: true);
			Type type2 = assembly.GetType("AzuCraftyBoxes.Util.Functions.MiscFunctions", throwOnError: true);
			shouldPrevent = (Func<bool>)Delegate.CreateDelegate(typeof(Func<bool>), AccessTools.Method(type2, "ShouldPrevent", (Type[])null, (Type[])null));
			begin = (Action<List<IContainer>>)Delegate.CreateDelegate(typeof(Action<List<IContainer>>), AccessTools.Method(type, "Begin", (Type[])null, (Type[])null));
			count = (Func<string, int>)Delegate.CreateDelegate(typeof(Func<string, int>), AccessTools.Method(type, "GetTotalAnyQuality", (Type[])null, (Type[])null));
			MethodInfo methodInfo = AccessTools.Method(((object)Chainloader.PluginInfos["maximods.valheim.multicraft"].Instance).GetType().Assembly.GetType("MultiCraft.Helper", throwOnError: true), "GetAvailableItems", new Type[1] { typeof(string) }, (Type[])null);
			if (methodInfo == null)
			{
				throw new MissingMethodException("MultiCraft.Helper.GetAvailableItems");
			}
			harmony = new Harmony("local.multicraft.azucraftyboxes.compatibility");
			harmony.Patch((MethodBase)methodInfo, new HarmonyMethod(typeof(Plugin), "CountAvailable", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			PatchImprove("PlayerHasCost", typeof(ImproveCost));
			PatchImprove("Consume", typeof(ImproveCost));
			PatchImprove("SetupCostRequirement", typeof(ImproveUI));
			harmony.Patch((MethodBase)AccessTools.Method(typeof(ImproveUI), "SetupCostRequirement", (Type[])null, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "ShowImproveCount", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Enabled MultiCraft stock counts and Runeforging Improve storage support. Radius/access/toggle inherited from AzuCraftyBoxes.");
		}
		catch (Exception ex)
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
			((BaseUnityPlugin)this).Logger.LogError((object)("Could not enable compatibility; original mod behaviour retained. " + ex));
		}
	}

	private void PatchImprove(string method, Type type)
	{
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: Expected O, but got Unknown
		harmony.Patch((MethodBase)AccessTools.Method(type, method, (Type[])null, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "RedirectImproveInventory", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
	}

	private static IEnumerable<CodeInstruction> RedirectImproveInventory(IEnumerable<CodeInstruction> instructions, MethodBase original)
	{
		List<CodeInstruction> list = new List<CodeInstruction>(instructions);
		MethodInfo methodInfo = AccessTools.Method(typeof(Inventory), "CountItems", new Type[3]
		{
			typeof(string),
			typeof(int),
			typeof(bool)
		}, (Type[])null);
		MethodInfo methodInfo2 = AccessTools.Method(typeof(Inventory), "RemoveItem", new Type[4]
		{
			typeof(string),
			typeof(int),
			typeof(int),
			typeof(bool)
		}, (Type[])null);
		int num = 0;
		foreach (CodeInstruction item in list)
		{
			string text = (CodeInstructionExtensions.Calls(item, methodInfo) ? "CountImproveMaterial" : (CodeInstructionExtensions.Calls(item, methodInfo2) ? "RemoveImproveMaterial" : null));
			if (text != null)
			{
				item.opcode = OpCodes.Call;
				item.operand = AccessTools.Method(typeof(Plugin), text, (Type[])null, (Type[])null);
				num++;
			}
		}
		if (num != 1)
		{
			throw new InvalidOperationException("Unexpected Improve method: " + original.Name + " (" + num + " inventory calls)");
		}
		return list;
	}

	private static bool UseStorage(Inventory inventory)
	{
		if ((Object)(object)Player.m_localPlayer != (Object)null && inventory == ((Humanoid)Player.m_localPlayer).GetInventory())
		{
			return !shouldPrevent();
		}
		return false;
	}

	private static int CountImproveMaterial(Inventory inventory, string name, int quality, bool matchWorldLevel)
	{
		if (!UseStorage(inventory))
		{
			return inventory.CountItems(name, quality, matchWorldLevel);
		}
		return InventoryManagement.Instance.CountItem(name);
	}

	private static void RemoveImproveMaterial(Inventory inventory, string name, int amount, int quality, bool worldLevelBased)
	{
		if (UseStorage(inventory))
		{
			InventoryManagement.Instance.RemoveItem(name, amount);
		}
		else
		{
			inventory.RemoveItem(name, amount, quality, worldLevelBased);
		}
	}

	private static void ShowImproveCount(Transform elementRoot, Player player, ImproveCostEntry entry)
	{
		if (!((Object)(object)player == (Object)null) && !((Object)(object)entry?.Item == (Object)null))
		{
			Transform obj = elementRoot.Find("res_amount");
			TMP_Text val = ((obj != null) ? ((Component)obj).GetComponent<TMP_Text>() : null);
			if (!((Object)(object)val == (Object)null))
			{
				int num = CountImproveMaterial(((Humanoid)player).GetInventory(), entry.Item.m_itemData.m_shared.m_name, -1, matchWorldLevel: true);
				val.text = entry.Amount + "/" + num;
				val.enableAutoSizing = true;
				val.fontSizeMin = 10f;
				val.fontSizeMax = 16f;
			}
		}
	}

	private static bool CountAvailable(string itemName, ref int __result)
	{
		if ((Object)(object)Player.m_localPlayer == (Object)null || shouldPrevent())
		{
			return true;
		}
		begin(API.GetNearbyContainers<Player>(Player.m_localPlayer, AzuCraftyBoxesPlugin.mRange.Value));
		__result = count(itemName);
		return false;
	}

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