Decompiled source of HenEggPickup v2.0.2

BepInEx/plugins/HenEggPickup/HenEggPickup.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.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("HenEggPickup")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("2.0.2.0")]
[assembly: AssemblyInformationalVersion("2.0.2+d85f5f9f75a1db5038740b1b300ff605101b7f16")]
[assembly: AssemblyProduct("HenEggPickup")]
[assembly: AssemblyTitle("HenEggPickup")]
[assembly: AssemblyVersion("2.0.2.0")]
[module: RefSafetyRules(11)]
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;
		}
	}
	[CompilerGenerated]
	[Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace HenEggPickup
{
	internal static class EggRules
	{
		internal static bool IsChickenEgg(string? prefab)
		{
			return string.Equals(prefab, "ChickenEgg", StringComparison.Ordinal);
		}

		internal static bool WithinRadius(float squaredDistance, float radius)
		{
			if (radius > 0f && squaredDistance >= 0f)
			{
				return squaredDistance <= radius * radius;
			}
			return false;
		}
	}
	[BepInPlugin("com.ziluck.valheim.heneggpickup", "Hen Egg Pickup", "2.0.2")]
	public sealed class Plugin : BaseUnityPlugin
	{
		internal readonly struct PickupFlag
		{
			internal readonly ItemDrop Egg;

			internal readonly bool OriginalAutoPickup;

			internal PickupFlag(ItemDrop egg)
			{
				Egg = egg;
				OriginalAutoPickup = egg.m_autoPickup;
			}
		}

		[HarmonyPatch(typeof(Player), "AutoPickup")]
		private static class PickupPatch
		{
			[HarmonyPriority(800)]
			private static void Prefix(Player __instance, bool ___m_enableAutoPickup, ref List<PickupFlag>? __state)
			{
				//IL_004b: Unknown result type (might be due to invalid IL or missing references)
				//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_0099: Unknown result type (might be due to invalid IL or missing references)
				//IL_009e: Unknown result type (might be due to invalid IL or missing references)
				//IL_009f: Unknown result type (might be due to invalid IL or missing references)
				//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
				__state = null;
				if (!pickupEnabled.Value || (Object)(object)__instance != (Object)(object)Player.m_localPlayer || !___m_enableAutoPickup || ((Character)__instance).IsTeleporting() || !(Instances.GetValue(null) is List<ItemDrop> list))
				{
					return;
				}
				bool? flag = null;
				Vector3 val = ((Component)__instance).transform.position + Vector3.up;
				foreach (ItemDrop item in list)
				{
					if (!Object.op_Implicit((Object)(object)item) || !EggRules.IsChickenEgg(Utils.GetPrefabName(((Component)item).gameObject)))
					{
						continue;
					}
					Vector3 val2 = ((Component)item).transform.position - val;
					if (!EggRules.WithinRadius(((Vector3)(ref val2)).sqrMagnitude, __instance.m_autoPickupRange))
					{
						continue;
					}
					bool valueOrDefault = flag == true;
					if (!flag.HasValue)
					{
						valueOrDefault = HasEnoughHens(__instance);
						flag = valueOrDefault;
					}
					if (item.m_autoPickup != flag.Value)
					{
						if (__state == null)
						{
							__state = new List<PickupFlag>();
						}
						__state.Add(new PickupFlag(item));
						item.m_autoPickup = flag.Value;
					}
				}
			}

			[HarmonyPriority(0)]
			private static Exception? Finalizer(Exception? __exception, List<PickupFlag>? __state)
			{
				if (__state != null)
				{
					foreach (PickupFlag item in __state)
					{
						if (Object.op_Implicit((Object)(object)item.Egg))
						{
							item.Egg.m_autoPickup = item.OriginalAutoPickup;
						}
					}
				}
				return __exception;
			}
		}

		public const string Guid = "com.ziluck.valheim.heneggpickup";

		private static ConfigEntry<bool> pickupEnabled = null;

		private static ConfigEntry<int> minimumHens = null;

		private static ConfigEntry<float> henRadius = null;

		private static readonly FieldInfo Instances = AccessTools.Field(typeof(ItemDrop), "s_instances") ?? throw new MissingFieldException(typeof(ItemDrop).FullName, "s_instances");

		private Harmony? harmony;

		private void Awake()
		{
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Expected O, but got Unknown
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Expected O, but got Unknown
			//IL_0099: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Expected O, but got Unknown
			pickupEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Automatically collect chicken eggs only when enough adult hens are near you. Manual pickup is unaffected. Settings apply only to this client.");
			minimumHens = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Minimum Hens", 12, new ConfigDescription("Minimum number of living adult hens within Hen Detection Radius of the player. Chicks do not count.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 1000), Array.Empty<object>()));
			henRadius = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Hen Detection Radius", 10f, new ConfigDescription("Distance in metres from the player to adult hens, including height. Walls do not block detection. Only loaded hens count. This does not change the game's item pickup range.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(1f, 100f), Array.Empty<object>()));
			try
			{
				harmony = new Harmony("com.ziluck.valheim.heneggpickup");
				harmony.PatchAll(typeof(Plugin).Assembly);
				((BaseUnityPlugin)this).Logger.LogInfo((object)$"Hen Egg Pickup ready: {minimumHens.Value} adult hens within {henRadius.Value}m. Local settings only.");
			}
			catch (Exception arg)
			{
				Harmony? obj = harmony;
				if (obj != null)
				{
					obj.UnpatchSelf();
				}
				((BaseUnityPlugin)this).Logger.LogError((object)$"Hen Egg Pickup could not patch automatic pickup: {arg}");
			}
		}

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

		private static bool HasEnoughHens(Player player)
		{
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000d: Unknown result type (might be due to invalid IL or missing references)
			//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_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)
			int num = 0;
			Vector3 position = ((Component)player).transform.position;
			foreach (Character allCharacter in Character.GetAllCharacters())
			{
				if (Object.op_Implicit((Object)(object)allCharacter) && !allCharacter.IsDead() && !(Utils.GetPrefabName(((Component)allCharacter).gameObject) != "Hen"))
				{
					Vector3 val = ((Component)allCharacter).transform.position - position;
					if (EggRules.WithinRadius(((Vector3)(ref val)).sqrMagnitude, henRadius.Value) && ++num >= minimumHens.Value)
					{
						return true;
					}
				}
			}
			return false;
		}
	}
}