Decompiled source of LootSweep v0.1.1

BepInEx/plugins/LootSweep.dll

Decompiled a week 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 Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("LootSweep")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("LootSweep")]
[assembly: AssemblyTitle("LootSweep")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace ValheimLootVacuum
{
	[BepInPlugin("vansum.lootsweep", "LootSweep", "0.1.1")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Player), "TakeInput")]
		private static class InputPatch
		{
			private static void Postfix(Player __instance, bool __result)
			{
				if (!((Object)(object)instance == (Object)null) && !((Object)(object)__instance != (Object)(object)Player.m_localPlayer))
				{
					instance.SetGameplayHold(__result && ZInput.GetButton("Use"));
				}
			}
		}

		private static Plugin? instance;

		private ConfigEntry<float> pickupRadius = null;

		private Harmony harmony = null;

		private readonly Collider[] nearbyColliders = (Collider[])(object)new Collider[128];

		private readonly HashSet<ItemDrop> nearbyItems = new HashSet<ItemDrop>();

		private bool gameplayHold;

		private float nextPickupTime;

		private float nextCapacityMessageTime;

		private readonly HashSet<ItemDrop> attractedItems = new HashSet<ItemDrop>();

		private readonly List<ItemDrop> finishedItems = new List<ItemDrop>();

		private const float PickupSpeed = 15f;

		private const float PickupDistance = 0.3f;

		private const float PickupInterval = 0.1f;

		private void Awake()
		{
			//IL_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Expected O, but got Unknown
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Expected O, but got Unknown
			instance = this;
			pickupRadius = ((BaseUnityPlugin)this).Config.Bind<float>("General", "PickupRadius", 5f, new ConfigDescription("Pickup radius in meters.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 10f), Array.Empty<object>()));
			harmony = new Harmony("vansum.lootsweep");
			harmony.PatchAll(typeof(InputPatch));
			((BaseUnityPlugin)this).Logger.LogInfo((object)$"LootSweep loaded! Pickup radius: {pickupRadius.Value} meters.");
		}

		private void Update()
		{
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				SetGameplayHold(isHeld: false);
				attractedItems.Clear();
				return;
			}
			if (!gameplayHold)
			{
				attractedItems.Clear();
				return;
			}
			MoveAttractedItems(localPlayer);
			if (!(Time.time < nextPickupTime))
			{
				nextPickupTime = Time.time + 0.1f;
				CollectNearbyItems(localPlayer);
			}
		}

		private void MoveAttractedItems(Player player)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			//IL_016a: Unknown result type (might be due to invalid IL or missing references)
			//IL_016f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0174: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0186: Unknown result type (might be due to invalid IL or missing references)
			//IL_018b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0192: Unknown result type (might be due to invalid IL or missing references)
			//IL_019c: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
			finishedItems.Clear();
			Vector3 val = ((Component)player).transform.position + Vector3.up;
			Inventory inventory = ((Humanoid)player).GetInventory();
			foreach (ItemDrop attractedItem in attractedItems)
			{
				if ((Object)(object)attractedItem == (Object)null)
				{
					finishedItems.Add(attractedItem);
					continue;
				}
				if (!attractedItem.CanPickup(true))
				{
					attractedItem.RequestOwn();
					finishedItems.Add(attractedItem);
					continue;
				}
				if (attractedItem.InTar())
				{
					finishedItems.Add(attractedItem);
					continue;
				}
				attractedItem.Load();
				if (!inventory.CanAddItem(attractedItem.m_itemData, -1))
				{
					ShowCapacityMessage(player);
					finishedItems.Add(attractedItem);
					continue;
				}
				float weight = attractedItem.m_itemData.GetWeight(-1);
				if (inventory.GetTotalWeight() + weight > player.GetMaxCarryWeight())
				{
					ShowCapacityMessage(player);
					finishedItems.Add(attractedItem);
					continue;
				}
				float num = Vector3.Distance(((Component)attractedItem).transform.position, val);
				if (num <= 0.3f)
				{
					attractedItem.Pickup((Humanoid)(object)player);
					finishedItems.Add(attractedItem);
					continue;
				}
				Vector3 val2 = val - ((Component)attractedItem).transform.position;
				Vector3 normalized = ((Vector3)(ref val2)).normalized;
				Transform transform = ((Component)attractedItem).transform;
				transform.position += normalized * 15f * Time.deltaTime;
			}
			foreach (ItemDrop finishedItem in finishedItems)
			{
				attractedItems.Remove(finishedItem);
			}
		}

		private void CollectNearbyItems(Player player)
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			nearbyItems.Clear();
			Vector3 val = ((Component)player).transform.position + Vector3.up;
			int num = Physics.OverlapSphereNonAlloc(val, pickupRadius.Value, nearbyColliders);
			for (int i = 0; i < num; i++)
			{
				Collider val2 = nearbyColliders[i];
				if ((Object)(object)val2 == (Object)null || (Object)(object)val2.attachedRigidbody == (Object)null)
				{
					continue;
				}
				ItemDrop component = ((Component)val2.attachedRigidbody).GetComponent<ItemDrop>();
				if ((Object)(object)component == (Object)null || component.IsPiece() || component.InTar() || !nearbyItems.Add(component))
				{
					continue;
				}
				if (!component.CanPickup(true))
				{
					component.RequestOwn();
				}
				else
				{
					if (component.InTar())
					{
						continue;
					}
					component.Load();
					Inventory inventory = ((Humanoid)player).GetInventory();
					if (!inventory.CanAddItem(component.m_itemData, -1))
					{
						ShowCapacityMessage(player);
						continue;
					}
					float weight = component.m_itemData.GetWeight(-1);
					if (inventory.GetTotalWeight() + weight > player.GetMaxCarryWeight())
					{
						ShowCapacityMessage(player);
					}
					else
					{
						attractedItems.Add(component);
					}
				}
			}
		}

		private void ShowCapacityMessage(Player player)
		{
			if (!(Time.time < nextCapacityMessageTime))
			{
				((Character)player).Message((MessageType)2, "$msg_noroom", 0, (Sprite)null, false);
				nextCapacityMessageTime = Time.time + 1.5f;
			}
		}

		private void SetGameplayHold(bool isHeld)
		{
			if (isHeld != gameplayHold)
			{
				gameplayHold = isHeld;
				((BaseUnityPlugin)this).Logger.LogDebug((object)(isHeld ? "Gameplay hold started." : "Gameplay hold stopped."));
			}
		}

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