Decompiled source of Citadel Valheim Pack v1.0.3

TombstoneTakeAllFix.dll

Decompiled 21 hours ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Bootstrap;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace TombstoneFix;

[BepInPlugin("com.geniz.tombstonefix", "TombstoneTakeAllFix", "1.0.3")]
public class Plugin : BaseUnityPlugin
{
	private void Awake()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_0081: Expected O, but got Unknown
		//IL_0115: Unknown result type (might be due to invalid IL or missing references)
		//IL_0123: Expected O, but got Unknown
		Harmony val = new Harmony("com.geniz.tombstonefix");
		val.PatchAll(typeof(Plugin));
		try
		{
			Type type = AccessTools.TypeByName("AzuEPI.Game.Patches.TombstonePatches+AutoEquipAfterTombstoneGrab");
			if (type != null)
			{
				MethodInfo methodInfo = AccessTools.Method(type, "Postfix", (Type[])null, (Type[])null);
				if (methodInfo != null)
				{
					MethodInfo methodInfo2 = AccessTools.Method(typeof(Plugin), "AutoEquipAfterTombstoneGrab_Prefix", (Type[])null, (Type[])null);
					val.Patch((MethodBase)methodInfo, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					((BaseUnityPlugin)this).Logger.LogInfo((object)"Successfully hooked AzuEPI AutoEquipAfterTombstoneGrab to protect friend tombstones.");
				}
			}
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not hook AzuEPI AutoEquip: " + ex.Message));
		}
		try
		{
			Type type2 = AccessTools.TypeByName("AzuEPI.Game.Favoriting.FavoritingMode");
			if (type2 != null)
			{
				MethodInfo methodInfo = AccessTools.Method(type2, "IsExternalFavoritingModLoaded", (Type[])null, (Type[])null);
				if (methodInfo != null)
				{
					MethodInfo methodInfo2 = AccessTools.Method(typeof(Plugin), "AzuEPI_IsExternalFavoritingModLoaded_Prefix", (Type[])null, (Type[])null);
					val.Patch((MethodBase)methodInfo, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
					((BaseUnityPlugin)this).Logger.LogInfo((object)"Successfully hooked AzuEPI IsExternalFavoritingModLoaded to delegate favoriting to OttoStash.");
				}
			}
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Could not hook AzuEPI FavoritingMode: " + ex.Message));
		}
		((BaseUnityPlugin)this).Logger.LogInfo((object)"TombstoneTakeAllFix v1.0.3 loaded successfully.");
	}

	public static bool AzuEPI_IsExternalFavoritingModLoaded_Prefix(ref bool __result)
	{
		if (Chainloader.PluginInfos != null && Chainloader.PluginInfos.ContainsKey("potto007.OttoStash"))
		{
			__result = true;
			return false;
		}
		return true;
	}

	private static bool CheckIsOwner(TombStone ts)
	{
		if ((Object)(object)ts == (Object)null)
		{
			return false;
		}
		try
		{
			return Traverse.Create((object)ts).Method("IsOwner", new object[0]).GetValue<bool>();
		}
		catch
		{
			return false;
		}
	}

	public static bool AutoEquipAfterTombstoneGrab_Prefix(TombStone __instance)
	{
		if ((Object)(object)__instance != (Object)null && !CheckIsOwner(__instance))
		{
			return false;
		}
		return true;
	}

	[HarmonyPatch(typeof(TombStone), "EasyFitInInventory")]
	[HarmonyPrefix]
	public static bool TombStone_EasyFitInInventory_Prefix(TombStone __instance, ref bool __result)
	{
		if ((Object)(object)__instance != (Object)null && !CheckIsOwner(__instance))
		{
			__result = false;
			return false;
		}
		return true;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(TombStone), "OnTakeAllSuccess")]
	public static bool TombStone_OnTakeAllSuccess_Prefix(TombStone __instance)
	{
		if ((Object)(object)__instance != (Object)null && !CheckIsOwner(__instance))
		{
			return false;
		}
		return true;
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(Container), "RPC_TakeAllResponse")]
	public static void Container_RPC_TakeAllResponse_Prefix(Container __instance, bool granted)
	{
		//IL_00a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		if (!granted || !((Object)(object)__instance != (Object)null))
		{
			return;
		}
		try
		{
			TombStone componentInParent = ((Component)__instance).GetComponentInParent<TombStone>();
			if (!((Object)(object)componentInParent != (Object)null) || CheckIsOwner(componentInParent))
			{
				return;
			}
			Inventory inventory = __instance.GetInventory();
			if (inventory == null)
			{
				return;
			}
			List<ItemData> allItems = inventory.GetAllItems();
			for (int i = 0; i < allItems.Count; i++)
			{
				if (allItems[i] != null)
				{
					allItems[i].m_equipped = false;
					if (allItems[i].m_gridPos.y >= 4)
					{
						allItems[i].m_gridPos = new Vector2i(-1, -1);
					}
				}
			}
		}
		catch
		{
		}
	}

	[HarmonyPatch(typeof(InventoryGui), "OnTakeAll")]
	[HarmonyPrefix]
	public static bool InventoryGui_OnTakeAll_Prefix(InventoryGui __instance)
	{
		if ((Object)(object)__instance == (Object)null)
		{
			return true;
		}
		Container value = Traverse.Create((object)__instance).Field("m_currentContainer").GetValue<Container>();
		if ((Object)(object)value == (Object)null)
		{
			return true;
		}
		TombStone componentInParent = ((Component)value).GetComponentInParent<TombStone>();
		if ((Object)(object)componentInParent == (Object)null)
		{
			return true;
		}
		if (CheckIsOwner(componentInParent))
		{
			return true;
		}
		TakeAllFromForeignTombstone(__instance, value);
		return false;
	}

	private static void TakeAllFromForeignTombstone(InventoryGui gui, Container container)
	{
		//IL_0251: Unknown result type (might be due to invalid IL or missing references)
		//IL_0256: Unknown result type (might be due to invalid IL or missing references)
		//IL_026c: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null || ((Character)localPlayer).IsTeleporting())
			{
				return;
			}
			Inventory inventory = ((Humanoid)localPlayer).GetInventory();
			Inventory inventory2 = container.GetInventory();
			if (inventory == null || inventory2 == null)
			{
				return;
			}
			Traverse.Create((object)gui).Method("SetupDragItem", new object[3] { null, null, 1 }).GetValue();
			List<ItemData> list = new List<ItemData>(inventory2.GetAllItems());
			if (list.Count == 0)
			{
				Traverse.Create((object)gui).Method("CloseContainer", new object[0]).GetValue();
				return;
			}
			int num = 4;
			int width = inventory.GetWidth();
			for (int i = 0; i < list.Count; i++)
			{
				ItemData val = list[i];
				if (val == null)
				{
					continue;
				}
				val.m_equipped = false;
				if (val.m_shared != null && val.m_shared.m_maxStackSize > 1)
				{
					List<ItemData> allItems = inventory.GetAllItems();
					for (int j = 0; j < allItems.Count; j++)
					{
						ItemData val2 = allItems[j];
						if (val2 == null || val2.m_gridPos.y >= num || !(val2.m_shared.m_name == val.m_shared.m_name) || val2.m_quality != val.m_quality || val2.m_variant != val.m_variant)
						{
							continue;
						}
						int num2 = val2.m_shared.m_maxStackSize - val2.m_stack;
						if (num2 > 0)
						{
							int num3 = Math.Min(num2, val.m_stack);
							val2.m_stack += num3;
							val.m_stack -= num3;
							if (val.m_stack <= 0)
							{
								inventory2.RemoveItem(val);
								break;
							}
						}
					}
				}
				if (val.m_stack > 0)
				{
					Vector2i val3 = FindFirstFreeBackpackSlot(inventory, width, num);
					if (val3.x < 0)
					{
						break;
					}
					if (inventory.AddItem(val, val3))
					{
						inventory2.RemoveItem(val);
					}
				}
			}
			if (inventory2.NrOfItems() == 0)
			{
				Traverse.Create((object)gui).Method("CloseContainer", new object[0]).GetValue();
			}
		}
		catch (Exception ex)
		{
			ZLog.LogError((object)("TombstoneTakeAllFix: Error in TakeAllFromForeignTombstone: " + ex));
		}
	}

	private static Vector2i FindFirstFreeBackpackSlot(Inventory inv, int width, int maxRows)
	{
		//IL_0057: 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)
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		int num = Math.Min(inv.GetHeight(), maxRows);
		for (int i = 0; i < num; i++)
		{
			for (int j = 0; j < width; j++)
			{
				if (inv.GetItemAt(j, i) == null)
				{
					return new Vector2i(j, i);
				}
			}
		}
		return new Vector2i(-1, -1);
	}
}