Decompiled source of YolkMe v1.0.0

YolkMe.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
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("YolkMe")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("YolkMe")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("354f3e60-7323-49b2-8fba-c8c9c871c3d8")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace YolkMe;

public static class PluginConfig
{
	public static ConfigEntry<bool> IsModEnabled { get; private set; }

	public static ConfigEntry<bool> PickUpAllEnabled { get; private set; }

	public static void BindConfig(ConfigFile config)
	{
		IsModEnabled = config.Bind<bool>("_Global", "IsModEnabled", true, "Globally enable or disable this mod. Requires restart to work.");
		PickUpAllEnabled = config.Bind<bool>("Egg", "PickUpAllEnabled", false, "enable or disable picking up warm eggs.  Requires restart to work.");
	}
}
public static class EggPickupController
{
	public static readonly float EggPickupDelay = 0.25f;

	public static float LastEggPickupTime = 0f;

	public static bool IsEgg(ItemDrop itemDrop)
	{
		return ((Object)itemDrop.m_itemData.m_dropPrefab).name == "ChickenEgg";
	}

	public static bool CanPickupEgg(float currentTime)
	{
		return currentTime - LastEggPickupTime > EggPickupDelay;
	}
}
[HarmonyPatch(typeof(EggGrow))]
internal static class EggGrowPatch
{
	[HarmonyPostfix]
	[HarmonyPatch("GrowUpdate")]
	private static void GrowUpdatePostfix(EggGrow __instance)
	{
		if (PluginConfig.IsModEnabled.Value && Object.op_Implicit((Object)(object)__instance.m_nview) && __instance.m_nview.IsValid())
		{
			float @float = __instance.m_nview.m_zdo.GetFloat(ZDOVars.s_growStart, 0f);
			__instance.m_item.m_autoPickup = @float <= 0f;
		}
		if (PluginConfig.IsModEnabled.Value && PluginConfig.PickUpAllEnabled.Value && Object.op_Implicit((Object)(object)__instance.m_nview) && __instance.m_nview.IsValid())
		{
			__instance.m_item.m_autoPickup = true;
		}
	}
}
[HarmonyPatch(typeof(Humanoid))]
internal static class HumanoidPatch
{
	[HarmonyPostfix]
	[HarmonyPatch("Pickup")]
	private static void PickupPostfix(Humanoid __instance, GameObject go, bool __result)
	{
		ItemDrop itemDrop = default(ItemDrop);
		if (PluginConfig.IsModEnabled.Value && __result && (Object)(object)__instance == (Object)(object)Player.m_localPlayer && go.TryGetComponent<ItemDrop>(ref itemDrop) && EggPickupController.IsEgg(itemDrop))
		{
			EggPickupController.LastEggPickupTime = Time.time;
		}
	}
}
[HarmonyPatch(typeof(Player))]
internal static class PlayerPatch
{
	[HarmonyTranspiler]
	[HarmonyPatch("AutoPickup")]
	private static IEnumerable<CodeInstruction> AutoPickupTranspiler(IEnumerable<CodeInstruction> instructions)
	{
		//IL_0003: 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_0023: Expected O, but got Unknown
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Expected O, but got Unknown
		CodeInstruction instruction;
		return new CodeMatcher(instructions, (ILGenerator)null).MatchForward(false, (CodeMatch[])(object)new CodeMatch[2]
		{
			new CodeMatch((OpCode?)OpCodes.Ldloc_S, (object)null, (string)null),
			new CodeMatch((OpCode?)OpCodes.Ldfld, (object)AccessTools.Field(typeof(ItemDrop), "m_autoPickup"), (string)null)
		}).ThrowIfInvalid("Could not patch Player.AutoPickup()! (m_autoPickup)").CopyInstruction(out instruction)
			.Advance(2)
			.InsertAndAdvance((CodeInstruction[])(object)new CodeInstruction[2]
			{
				instruction,
				Transpilers.EmitDelegate<Func<bool, ItemDrop, bool>>((Func<bool, ItemDrop, bool>)AutoPickupDelegate)
			})
			.InstructionEnumeration();
	}

	private static CodeMatcher CopyInstruction(this CodeMatcher matcher, out CodeInstruction instruction)
	{
		instruction = matcher.Instruction;
		return matcher;
	}

	private static bool AutoPickupDelegate(bool autoPickup, ItemDrop itemDrop)
	{
		if (autoPickup && PluginConfig.IsModEnabled.Value && EggPickupController.IsEgg(itemDrop))
		{
			return EggPickupController.CanPickupEgg(Time.time);
		}
		return autoPickup;
	}
}
[BepInPlugin("EardwulfC.valheim.YolkMe", "YolkMe", "1.0.0")]
public sealed class YolkMe : BaseUnityPlugin
{
	public const string PluginGuid = "EardwulfC.valheim.YolkMe";

	public const string PluginName = "YolkMe";

	public const string PluginVersion = "1.0.0";

	private Harmony _harmony;

	private void Awake()
	{
		PluginConfig.BindConfig(((BaseUnityPlugin)this).Config);
		_harmony = Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "EardwulfC.valheim.YolkMe");
	}

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