Decompiled source of Smartpickup v1.0.2

SmartPickup.dll

Decompiled an hour ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
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("SmartPickup")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("SmartPickup")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("1c4fa49b-2311-4587-a0a0-1095f2b4d1c3")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace SmartAutopickupMod;

public enum AutopickupMode
{
	Normal,
	Smart,
	Off
}
[BepInPlugin("com.yourname.valheim.smartautopickup", "Smartpickup", "1.0.2")]
public class SmartAutopickup : BaseUnityPlugin
{
	public static AutopickupMode CurrentMode;

	public static bool suppressNextAutopickupMessage;

	private void Awake()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		Harmony val = new Harmony("com.yourname.valheim.smartautopickup");
		val.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Smart Autopickup mod loaded. Starting in Normal mode.");
	}

	private void Update()
	{
		if (ZInput.GetButtonDown("AutoPickup"))
		{
			if (CurrentMode == AutopickupMode.Normal)
			{
				CurrentMode = AutopickupMode.Smart;
			}
			else if (CurrentMode == AutopickupMode.Smart)
			{
				CurrentMode = AutopickupMode.Off;
			}
			else if (CurrentMode == AutopickupMode.Off)
			{
				CurrentMode = AutopickupMode.Normal;
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Autopickup mode switched to: " + CurrentMode));
			suppressNextAutopickupMessage = true;
			if ((Object)(object)Player.m_localPlayer != (Object)null)
			{
				string text = ((CurrentMode == AutopickupMode.Normal) ? "On" : CurrentMode.ToString());
				((Character)Player.m_localPlayer).Message((MessageType)1, "Auto-Pickup:" + text, 0, (Sprite)null);
			}
		}
	}

	private void LateUpdate()
	{
		Traverse.Create(typeof(Player)).Field("m_enableAutoPickup").SetValue((object)true);
	}
}
[HarmonyPatch(typeof(Player), "AutoPickup")]
public class Player_AutoPickup_Patch
{
	private static bool Prefix(Player __instance, float dt)
	{
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_0055: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: 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_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_0273: Unknown result type (might be due to invalid IL or missing references)
		//IL_0278: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_02cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e0: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e5: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f4: Unknown result type (might be due to invalid IL or missing references)
		if (SmartAutopickup.CurrentMode == AutopickupMode.Normal)
		{
			return true;
		}
		if (SmartAutopickup.CurrentMode == AutopickupMode.Off)
		{
			return false;
		}
		if (SmartAutopickup.CurrentMode == AutopickupMode.Smart)
		{
			if (((Character)__instance).IsTeleporting())
			{
				return false;
			}
			Vector3 val = ((Component)__instance).transform.position + Vector3.up;
			int value = Traverse.Create((object)__instance).Field("m_autoPickupMask").GetValue<int>();
			object value2 = Traverse.Create((object)__instance).Field("m_inventory").GetValue<object>();
			Collider[] array = Physics.OverlapSphere(val, __instance.m_autoPickupRange, value);
			foreach (Collider val2 in array)
			{
				if (!Object.op_Implicit((Object)(object)val2.attachedRigidbody))
				{
					continue;
				}
				ItemDrop component = ((Component)val2.attachedRigidbody).GetComponent<ItemDrop>();
				if (!((Object)(object)component != (Object)null) || !component.m_autoPickup || component.IsPiece() || ((Humanoid)__instance).HaveUniqueKey(component.m_itemData.m_shared.m_name) || !((Component)component).GetComponent<ZNetView>().IsValid())
				{
					continue;
				}
				bool flag = false;
				List<ItemData> value3 = Traverse.Create(value2).Method("GetAllItems", Array.Empty<object>()).GetValue<List<ItemData>>();
				foreach (ItemData item in value3)
				{
					if (item.m_shared.m_name == component.m_itemData.m_shared.m_name)
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					continue;
				}
				if (!component.CanPickup(true))
				{
					component.RequestOwn();
				}
				else
				{
					if (component.InTar())
					{
						continue;
					}
					component.Load();
					bool flag2 = (bool)Traverse.Create(value2).Method("CanAddItem", new object[2] { component.m_itemData, -1 }).GetValue();
					float num = (float)Traverse.Create(value2).Method("GetTotalWeight", Array.Empty<object>()).GetValue();
					if (!flag2 || !(component.m_itemData.GetWeight(-1) + num <= __instance.GetMaxCarryWeight()))
					{
						continue;
					}
					float num2 = Vector3.Distance(((Component)component).transform.position, val);
					if (num2 <= __instance.m_autoPickupRange)
					{
						if (num2 < 0.3f)
						{
							((Humanoid)__instance).Pickup(((Component)component).gameObject, true, true);
							continue;
						}
						Vector3 val3 = Vector3.Normalize(val - ((Component)component).transform.position);
						float num3 = 15f;
						Transform transform = ((Component)component).transform;
						transform.position += val3 * num3 * dt;
					}
				}
			}
			return false;
		}
		return true;
	}
}
[HarmonyPatch(typeof(Player), "Message")]
public class Player_Message_Patch
{
	private static bool Prefix(Player __instance, MessageType type, string msg, int amount, Sprite icon)
	{
		if (msg != null && msg.StartsWith("$hud_autopickup:") && SmartAutopickup.suppressNextAutopickupMessage)
		{
			SmartAutopickup.suppressNextAutopickupMessage = false;
			return false;
		}
		return true;
	}
}