Decompiled source of EggAutoPickup v1.0.1

BepInEx/plugins/EggAutoPickup.dll

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

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("EggAutoPickup")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1")]
[assembly: AssemblyProduct("EggAutoPickup")]
[assembly: AssemblyTitle("EggAutoPickup")]
[assembly: AssemblyVersion("1.0.1.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 EggAutoPickup
{
	[BepInPlugin("enigma.eggautopickup", "Egg Auto Pickup", "1.0.1")]
	public sealed class Plugin : BaseUnityPlugin
	{
		private const string PluginId = "enigma.eggautopickup";

		private static Func<EggGrow, bool> canGrow;

		private static FieldRef<EggGrow, ItemDrop> eggItem;

		private void Awake()
		{
			//IL_002e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: Expected O, but got Unknown
			//IL_00e0: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ed: Expected O, but got Unknown
			//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Expected O, but got Unknown
			if (Application.isBatchMode)
			{
				return;
			}
			bool value = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Leave warm eggs", false, "Keep warm eggs on the ground for hatching. Restart Valheim after changing this setting.").Value;
			Harmony val = new Harmony("enigma.eggautopickup");
			try
			{
				if (value)
				{
					canGrow = AccessTools.MethodDelegate<Func<EggGrow, bool>>(AccessTools.DeclaredMethod(typeof(EggGrow), "CanGrow", Type.EmptyTypes, (Type[])null), (object)null, false);
					eggItem = AccessTools.FieldRefAccess<EggGrow, ItemDrop>("m_item");
					val.Patch((MethodBase)AccessTools.DeclaredMethod(typeof(Player), "AutoPickup", new Type[1] { typeof(float) }, (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "CheckWarmEggs", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null);
				}
				val.Patch((MethodBase)AccessTools.DeclaredMethod(typeof(ItemDrop), "Awake", Type.EmptyTypes, (Type[])null), (HarmonyMethod)null, new HarmonyMethod(typeof(Plugin), "EnableEggPickup", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			catch (Exception ex)
			{
				val.UnpatchSelf();
				((BaseUnityPlugin)this).Logger.LogError((object)("Egg Auto Pickup could not start. " + ex.Message));
			}
		}

		private static void EnableEggPickup(ItemDrop __instance)
		{
			if (IsEgg(__instance))
			{
				__instance.m_autoPickup = true;
			}
		}

		private static bool IsEgg(ItemDrop drop)
		{
			GameObject dropPrefab = drop.m_itemData.m_dropPrefab;
			bool flag = Object.op_Implicit((Object)(object)dropPrefab);
			if (flag)
			{
				string name = ((Object)dropPrefab).name;
				bool flag2 = ((name == "ChickenEgg" || name == "AsksvinEgg") ? true : false);
				flag = flag2;
			}
			return flag;
		}

		private static IEnumerable<CodeInstruction> CheckWarmEggs(IEnumerable<CodeInstruction> instructions)
		{
			List<CodeInstruction> list = instructions.ToList();
			FieldInfo field = AccessTools.DeclaredField(typeof(ItemDrop), "m_autoPickup");
			CodeInstruction[] array = list.Where((CodeInstruction instruction) => CodeInstructionExtensions.LoadsField(instruction, field, false)).ToArray();
			if (array.Length != 1)
			{
				throw new InvalidOperationException("Expected one item pickup flag check in Player.AutoPickup.");
			}
			array[0].opcode = OpCodes.Call;
			array[0].operand = AccessTools.DeclaredMethod(typeof(Plugin), "CanAutoPickup", (Type[])null, (Type[])null);
			return list;
		}

		private static bool CanAutoPickup(ItemDrop drop)
		{
			if (!drop.m_autoPickup)
			{
				return false;
			}
			EggGrow val = default(EggGrow);
			if (!IsEgg(drop) || !((Component)drop).TryGetComponent<EggGrow>(ref val))
			{
				return true;
			}
			if (Object.op_Implicit((Object)(object)eggItem.Invoke(val)))
			{
				return !canGrow(val);
			}
			return false;
		}
	}
}