Decompiled source of Simple Depot v0.1.0

SimpleDepot/SimpleDepot.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("SimpleDepot")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SimpleDepot")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("8841453d-5b4e-4c3a-9928-1b0ca323798f")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SimpleDepot;

[BepInPlugin("nb.wackjob.SimpleDepot", "Simple Depot", "0.1.0")]
public class SimpleDepot : BaseUnityPlugin
{
	private readonly Harmony harmony = new Harmony("nb.wackjob.SimpleDepot");

	private void Awake()
	{
		harmony.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Simple Depot loaded.");
	}

	private void OnDestroy()
	{
		Harmony obj = harmony;
		if (obj != null)
		{
			obj.UnpatchSelf();
		}
	}
}
public static class Depot
{
	private delegate bool ContainerCheckAccessDelegate(Container instance, long playerID);

	private delegate void ContainerSaveDelegate(Container instance);

	private delegate void InventoryChangedDelegate(Inventory instance, bool success, bool cheatedStateChanged);

	[HarmonyPatch(typeof(Player), "Update")]
	public static class Player_Update_Patch
	{
		[HarmonyPostfix]
		private static void Postfix(Player __instance)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer) && Input.GetKeyDown(Hotkey) && (!((Object)(object)Chat.instance != (Object)null) || !Chat.instance.HasFocus()) && !Console.IsVisible())
			{
				QuickStackNearby(__instance);
			}
		}
	}

	public static KeyCode Hotkey = (KeyCode)110;

	public static float Range = 10f;

	private static readonly ContainerCheckAccessDelegate ContainerCheckAccess = AccessTools.MethodDelegate<ContainerCheckAccessDelegate>(AccessTools.Method(typeof(Container), "CheckAccess", new Type[1] { typeof(long) }, (Type[])null), (object)null, true);

	private static readonly ContainerSaveDelegate ContainerSave = AccessTools.MethodDelegate<ContainerSaveDelegate>(AccessTools.Method(typeof(Container), "Save", Type.EmptyTypes, (Type[])null), (object)null, true);

	private static readonly InventoryChangedDelegate InventoryChanged = AccessTools.MethodDelegate<InventoryChangedDelegate>(AccessTools.Method(typeof(Inventory), "Changed", new Type[2]
	{
		typeof(bool),
		typeof(bool)
	}, (Type[])null), (object)null, true);

	private static void QuickStackNearby(Player player)
	{
		if ((Object)(object)player == (Object)null)
		{
			return;
		}
		Inventory inventory = ((Humanoid)player).GetInventory();
		if (inventory == null)
		{
			return;
		}
		List<Container> nearbyContainers = GetNearbyContainers(player);
		if (nearbyContainers.Count == 0)
		{
			((Character)player).Message((MessageType)2, "No nearby chests", 0, (Sprite)null, false);
			return;
		}
		List<ItemData> list = inventory.GetAllItems().ToList();
		int num = 0;
		HashSet<Container> hashSet = new HashSet<Container>();
		foreach (ItemData playerItem in list)
		{
			if (playerItem == null || playerItem.m_stack <= 0 || playerItem.m_equipped)
			{
				continue;
			}
			List<Container> list2 = nearbyContainers.Where((Container container) => ContainerContainsItem(container, playerItem)).ToList();
			if (list2.Count == 0)
			{
				continue;
			}
			foreach (Container item in list2)
			{
				if (playerItem.m_stack <= 0)
				{
					break;
				}
				int num2 = MoveItemToContainer(inventory, playerItem, item);
				if (num2 > 0)
				{
					num += num2;
					hashSet.Add(item);
				}
			}
		}
		foreach (Container item2 in hashSet)
		{
			if (!((Object)(object)item2 == (Object)null))
			{
				Inventory inventory2 = item2.GetInventory();
				if (inventory2 != null)
				{
					InventoryChanged(inventory2, success: true, cheatedStateChanged: false);
				}
				ContainerSave(item2);
			}
		}
		InventoryChanged(inventory, success: true, cheatedStateChanged: false);
		if (num > 0)
		{
			((Character)player).Message((MessageType)2, $"Quick stacked {num} items", 0, (Sprite)null, false);
		}
		else
		{
			((Character)player).Message((MessageType)2, "Nothing to quick stack", 0, (Sprite)null, false);
		}
	}

	private static List<Container> GetNearbyContainers(Player player)
	{
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		List<Container> list = new List<Container>();
		Collider[] array = Physics.OverlapSphere(((Component)player).transform.position, Range);
		HashSet<Container> hashSet = new HashSet<Container>();
		Collider[] array2 = array;
		foreach (Collider val in array2)
		{
			if (!((Object)(object)val == (Object)null))
			{
				Container componentInParent = ((Component)val).GetComponentInParent<Container>();
				if (!((Object)(object)componentInParent == (Object)null) && hashSet.Add(componentInParent) && componentInParent.GetInventory() != null && ContainerCheckAccess(componentInParent, player.GetPlayerID()))
				{
					list.Add(componentInParent);
				}
			}
		}
		return list.OrderBy((Container container) => Vector3.SqrMagnitude(((Component)container).transform.position - ((Component)player).transform.position)).ToList();
	}

	private static bool ContainerContainsItem(Container container, ItemData playerItem)
	{
		if ((Object)(object)container == (Object)null || playerItem == null)
		{
			return false;
		}
		Inventory inventory = container.GetInventory();
		if (inventory == null)
		{
			return false;
		}
		foreach (ItemData allItem in inventory.GetAllItems())
		{
			if (allItem == null || !allItem.IsSameType(playerItem))
			{
				continue;
			}
			return true;
		}
		return false;
	}

	private static int MoveItemToContainer(Inventory playerInventory, ItemData playerItem, Container container)
	{
		if (playerInventory == null || playerItem == null || (Object)(object)container == (Object)null)
		{
			return 0;
		}
		Inventory inventory = container.GetInventory();
		if (inventory == null)
		{
			return 0;
		}
		int stack = playerItem.m_stack;
		if (stack <= 0)
		{
			return 0;
		}
		int num = stack;
		List<ItemData> list = inventory.GetAllItems().ToList();
		foreach (ItemData item in list)
		{
			if (num <= 0)
			{
				break;
			}
			if (item != null && item.IsSameType(playerItem))
			{
				int num2 = item.m_shared.m_maxStackSize - item.m_stack;
				if (num2 > 0)
				{
					int num3 = Mathf.Min(num, num2);
					item.m_stack += num3;
					num -= num3;
				}
			}
		}
		while (num > 0)
		{
			int num4 = Mathf.Min(num, playerItem.m_shared.m_maxStackSize);
			ItemData val = playerItem.Clone();
			val.m_stack = num4;
			val.m_equipped = false;
			if (!inventory.AddItem(val))
			{
				break;
			}
			num -= num4;
		}
		int num5 = stack - num;
		if (num5 <= 0)
		{
			return 0;
		}
		if (num <= 0)
		{
			playerInventory.RemoveItem(playerItem);
		}
		else
		{
			playerItem.m_stack = num;
		}
		return num5;
	}
}