Decompiled source of Dryzlers Sort Everything Into Chests v1.0.3

SortEverythingIntoChests.dll

Decompiled 4 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
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("SortEverythingIntoChests")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("SortEverythingIntoChests")]
[assembly: AssemblyTitle("SortEverythingIntoChests")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SortEverythingIntoChests;

[HarmonyPatch(typeof(Container))]
internal class ContainerLifecycle
{
	public static readonly List<Container> _containers = new List<Container>();

	[HarmonyPatch(typeof(Container), "Awake")]
	[HarmonyPostfix]
	private static void Awake(Container __instance, ZNetView ___m_nview)
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)___m_nview == (Object)null)
		{
			return;
		}
		ZDO zDO = ___m_nview.GetZDO();
		if (zDO != null)
		{
			_ = zDO.m_uid;
			if (0 == 0 && TryGetValidOwningPiece(___m_nview, out var _) && ((object)__instance).ToString().StartsWith("piece_chest") && !_containers.Contains(__instance))
			{
				_containers.Add(__instance);
			}
		}
	}

	private static bool TryGetValidOwningPiece(ZNetView zNetView, out Piece piece)
	{
		piece = ((Component)zNetView).GetComponent<Piece>();
		return (Object)(object)piece != (Object)null;
	}

	[HarmonyPatch(typeof(Container), "OnDestroyed")]
	[HarmonyPostfix]
	private static void OnDestroyed(Container __instance)
	{
		_containers.Remove(__instance);
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(Container), "GetHoverText")]
	public static string ContainerGetHoverText_Patch(string __result)
	{
		return __result + "\n[<color=yellow><b>" + ValheimMod.keyDepositString.Value + "</b></color>] Sort All To Chests";
	}
}
[BepInPlugin("com.dryzler.valheim.sorteverythingintochests", "Dryzler's Sort Everything Into Chests", "1.0.3")]
[BepInProcess("valheim.exe")]
public class ValheimMod : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Player), "Update")]
	private static class Player_Update_Patch
	{
		private static bool Prefix(Player __instance)
		{
			try
			{
				if (Input.GetKeyDown(keyDepositString.Value.ToLower()))
				{
					doIt();
					return false;
				}
			}
			catch
			{
			}
			return true;
		}
	}

	private readonly Harmony harmony = new Harmony("com.dryzler.valheim.sorteverythingintochests");

	public static KeyCode configDepositKey;

	public static ConfigEntry<int> containerLookupRange { get; private set; }

	public static ConfigEntry<int> excludedSlots { get; private set; }

	public static ConfigEntry<string> keyDepositString { get; private set; }

	private void Awake()
	{
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		containerLookupRange = ((BaseUnityPlugin)this).Config.Bind<int>("Dryzlers Sort Everything Into Chests", "Container Range", 100, "The maximum range to send items");
		excludedSlots = ((BaseUnityPlugin)this).Config.Bind<int>("Dryzlers Sort Everything Into Chests", "Excluded Slots", 8, "The number of slots in the player's inventory that shall not be touched (from the top left corner)");
		keyDepositString = ((BaseUnityPlugin)this).Config.Bind<string>("Dryzlers Sort Everything Into Chests", "Keybinding", "G", "The key to press to sort everything into chests");
		configDepositKey = (KeyCode)Enum.Parse(typeof(KeyCode), keyDepositString.Value);
		harmony.PatchAll();
	}

	public static void doIt()
	{
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_018f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0194: Unknown result type (might be due to invalid IL or missing references)
		//IL_019d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0144: Unknown result type (might be due to invalid IL or missing references)
		//IL_0149: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: Unknown result type (might be due to invalid IL or missing references)
		//IL_0159: Unknown result type (might be due to invalid IL or missing references)
		//IL_0212: Unknown result type (might be due to invalid IL or missing references)
		//IL_0217: Unknown result type (might be due to invalid IL or missing references)
		//IL_0266: Unknown result type (might be due to invalid IL or missing references)
		//IL_026d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0226: Unknown result type (might be due to invalid IL or missing references)
		//IL_022d: Unknown result type (might be due to invalid IL or missing references)
		Inventory inventory = ((Humanoid)Player.m_localPlayer).GetInventory();
		int value = excludedSlots.Value;
		for (int i = inventory.GetWidth() + value; i < inventory.GetWidth() * inventory.GetHeight(); i++)
		{
			Vector2Int val = ConvertToGrid(i);
			ItemData itemAt = inventory.GetItemAt(((Vector2Int)(ref val)).x, ((Vector2Int)(ref val)).y);
			if (itemAt == null || itemAt.m_equipped)
			{
				continue;
			}
			int num = itemAt.m_stack;
			int num2 = 0;
			foreach (Container container in ContainerLifecycle._containers)
			{
				if ((Object)(object)container == (Object)null)
				{
					continue;
				}
				if (num <= 0)
				{
					break;
				}
				bool flag = false;
				if (Vector3.Distance(((Component)container).transform.position, ((Component)Player.m_localPlayer).transform.position) > (float)containerLookupRange.Value)
				{
					continue;
				}
				Inventory inventory2 = container.GetInventory();
				foreach (ItemData allItem in inventory2.GetAllItems())
				{
					if (!(itemAt.m_shared.m_name == allItem.m_shared.m_name))
					{
						continue;
					}
					flag = true;
					int num3 = allItem.m_shared.m_maxStackSize - allItem.m_stack;
					num2 = allItem.m_shared.m_maxStackSize;
					if (num3 > 0)
					{
						if (num <= num3)
						{
							Vector2i itemLocationInBox = GetItemLocationInBox(inventory2, allItem);
							inventory2.MoveItemToThis(inventory, itemAt, num, itemLocationInBox.x, itemLocationInBox.y);
							num -= num;
							Traverse.Create((object)inventory2).Method("Changed", Array.Empty<object>()).GetValue();
							break;
						}
						Vector2i itemLocationInBox2 = GetItemLocationInBox(inventory2, allItem);
						inventory2.MoveItemToThis(inventory, itemAt, num3, itemLocationInBox2.x, itemLocationInBox2.y);
						num -= num3;
						Traverse.Create((object)inventory2).Method("Changed", Array.Empty<object>()).GetValue();
					}
				}
				if (!flag || num2 <= 0)
				{
					continue;
				}
				while (num > 0 && inventory2.HaveEmptySlot())
				{
					Vector2i freeLocationInBox = GetFreeLocationInBox(inventory2);
					if (num >= num2)
					{
						inventory2.MoveItemToThis(inventory, itemAt, num2, freeLocationInBox.x, freeLocationInBox.y);
						num -= num2;
						Traverse.Create((object)inventory2).Method("Changed", Array.Empty<object>()).GetValue();
					}
					else
					{
						inventory2.MoveItemToThis(inventory, itemAt, num, freeLocationInBox.x, freeLocationInBox.y);
						num = 0;
						Traverse.Create((object)inventory2).Method("Changed", Array.Empty<object>()).GetValue();
					}
				}
			}
		}
	}

	private static bool BoxHasItem(Inventory boxInventory, ItemData item)
	{
		for (int i = 0; i < boxInventory.GetWidth(); i++)
		{
			for (int j = 0; j < boxInventory.GetHeight(); j++)
			{
				ItemData itemAt = boxInventory.GetItemAt(i, j);
				if (itemAt != null && itemAt.m_stack > 0 && itemAt.m_shared.m_name == item.m_shared.m_name)
				{
					return true;
				}
			}
		}
		return false;
	}

	private static Vector2i GetFreeLocationInBox(Inventory boxInventory)
	{
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		for (int i = 0; i < boxInventory.GetWidth(); i++)
		{
			for (int j = 0; j < boxInventory.GetHeight(); j++)
			{
				ItemData itemAt = boxInventory.GetItemAt(i, j);
				if (itemAt == null)
				{
					return new Vector2i(i, j);
				}
				if (itemAt.m_stack <= 0)
				{
					return new Vector2i(i, j);
				}
			}
		}
		throw new Exception("no free slot found (should not happen");
	}

	private static Vector2i GetItemLocationInBox(Inventory boxInventory, ItemData item)
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		int num = boxInventory.GetAllItems().IndexOf(item);
		return new Vector2i(boxInventory.GetItem(num).m_gridPos.x, boxInventory.GetItem(num).m_gridPos.y);
	}

	private static Vector2Int ConvertToGrid(int index)
	{
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		Vector2Int result = default(Vector2Int);
		((Vector2Int)(ref result))..ctor(0, 0);
		float num = 1f;
		float num2 = 0f;
		if (index != 0)
		{
			num = (float)Math.Floor((float)index / 8f);
		}
		float num3 = (float)Math.Floor((float)index / 8f);
		num2 = ((float)index / 8f - num3) * 8f;
		((Vector2Int)(ref result)).y = (int)num;
		((Vector2Int)(ref result)).x = (int)num2;
		return result;
	}
}