Decompiled source of Smartpickup v1.0.3

SmartPickup.dll

Decompiled a month 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 BepInEx.Configuration;
using BepInEx.Logging;
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.3")]
public class SmartAutopickup : BaseUnityPlugin
{
	public static AutopickupMode CurrentMode;

	public static bool suppressNextAutopickupMessage;

	public static ManualLogSource Log;

	public static ConfigEntry<AutopickupMode> DefaultMode;

	public static ConfigEntry<float> PickupRange;

	private void Awake()
	{
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		DefaultMode = ((BaseUnityPlugin)this).Config.Bind<AutopickupMode>("General", "DefaultMode", AutopickupMode.Normal, "Default autopickup mode at startup.");
		PickupRange = ((BaseUnityPlugin)this).Config.Bind<float>("General", "PickupRange", 5f, "Pickup range for smart mode (in game units).");
		CurrentMode = DefaultMode.Value;
		try
		{
			Harmony val = new Harmony("com.yourname.valheim.smartautopickup");
			val.PatchAll();
			Log.LogInfo((object)$"Smart Autopickup mod loaded. Mode: {CurrentMode}, Pickup Range: {PickupRange.Value}");
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Error in Awake: " + ex));
		}
	}

	private void Update()
	{
		try
		{
			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;
				}
				Log.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);
				}
			}
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Error in Update: " + ex));
		}
	}

	private void LateUpdate()
	{
		try
		{
			Traverse.Create(typeof(Player)).Field("m_enableAutoPickup").SetValue((object)true);
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Error in LateUpdate: " + ex));
		}
	}
}
[HarmonyPatch(typeof(Player), "AutoPickup")]
public class Player_AutoPickup_Patch
{
	private static bool Prefix(Player __instance, float dt)
	{
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_005b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		//IL_027c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0281: Unknown result type (might be due to invalid IL or missing references)
		//IL_02bc: 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_02d4: 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_02ea: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ee: 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)
		//IL_02f9: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			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>();
				float value3 = SmartAutopickup.PickupRange.Value;
				Collider[] array = Physics.OverlapSphere(val, value3, 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> value4 = Traverse.Create(value2).Method("GetAllItems", Array.Empty<object>()).GetValue<List<ItemData>>();
					foreach (ItemData item in value4)
					{
						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 <= value3)
						{
							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;
			}
		}
		catch (Exception ex)
		{
			SmartAutopickup.Log.LogError((object)("Error in Player_AutoPickup_Patch Prefix: " + ex));
		}
		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)
	{
		try
		{
			if (msg != null && msg.StartsWith("$hud_autopickup:") && SmartAutopickup.suppressNextAutopickupMessage)
			{
				SmartAutopickup.suppressNextAutopickupMessage = false;
				return false;
			}
		}
		catch (Exception ex)
		{
			SmartAutopickup.Log.LogError((object)("Error in Player_Message_Patch Prefix: " + ex));
		}
		return true;
	}
}