Decompiled source of FeedFromHand v1.2.0

FeedFromHand.dll

Decompiled a year ago
using System;
using System.Diagnostics;
using System.Reflection;
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("FeedFromHand")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FeedFromHand")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("a99fd3be-c055-4c36-8bb4-1b81a1ee7df9")]
[assembly: AssemblyFileVersion("1.2.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.2.0.0")]
[module: UnverifiableCode]
namespace FeedFromHand;

[BepInPlugin("org.bepinex.plugins.feedfromhand", "FeedFromHand", "1.2.0")]
[BepInProcess("valheim.exe")]
public class FeedFromHand : BaseUnityPlugin
{
	[HarmonyPatch]
	internal class FeedFromHand_Patch
	{
		[HarmonyPatch(typeof(Humanoid), "UseItem")]
		public static class Humanoid_UseItem_Prefix_Patch
		{
			public static bool Prefix(Humanoid __instance, ItemData item)
			{
				//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
				//IL_00c7: Invalid comparison between Unknown and I4
				//IL_016b: Unknown result type (might be due to invalid IL or missing references)
				GameObject hoverObject = __instance.GetHoverObject();
				if ((Object)(object)hoverObject == (Object)null)
				{
					return true;
				}
				if (!Object.op_Implicit((Object)(object)hoverObject.GetComponent<MonsterAI>()) && !Object.op_Implicit((Object)(object)hoverObject.GetComponent<Character>()) && !Object.op_Implicit((Object)(object)hoverObject.GetComponent<Tameable>()) && !Object.op_Implicit((Object)(object)hoverObject.GetComponent<Humanoid>()))
				{
					return true;
				}
				MonsterAI component = hoverObject.GetComponent<MonsterAI>();
				Character component2 = hoverObject.GetComponent<Character>();
				Tameable component3 = hoverObject.GetComponent<Tameable>();
				Humanoid component4 = hoverObject.GetComponent<Humanoid>();
				ZNetView component5 = hoverObject.GetComponent<ZNetView>();
				string hoverName = component2.GetHoverName();
				if (!component2.IsTamed())
				{
					return true;
				}
				if (!component.CanConsume(item))
				{
					if ((int)item.m_shared.m_itemType == 2)
					{
						((Character)__instance).Message((MessageType)2, hoverName + " can't consume " + item.m_shared.m_name + ".", 0, (Sprite)null);
						return false;
					}
					return true;
				}
				if (component2.GetHealth() < component2.GetMaxHealth())
				{
					ZDO zDO = component5.GetZDO();
					if (!zDO.GetBool("CanConsume", false))
					{
						return false;
					}
					component2.Heal((float)HealingReceived, true);
					component3.Interact(component4, false, false);
					__instance.DoInteractAnimation(hoverObject.transform.position);
					if (UseItem)
					{
						Inventory inventory = __instance.GetInventory();
						inventory.RemoveOneItem(item);
					}
					if (SuccessMessage)
					{
						((Character)__instance).Message((MessageType)2, hoverName + " is happy!", 0, (Sprite)null);
					}
					zDO.Set("CanConsume", false);
					return false;
				}
				((Character)__instance).Message((MessageType)2, hoverName + " is already max health.", 0, (Sprite)null);
				return false;
			}
		}

		[HarmonyPatch(typeof(MonsterAI), "UpdateAI")]
		public static class MonsterAI_UpdateAI_Prefix_Patch
		{
			public static void Prefix(MonsterAI __instance, ref float dt)
			{
				Character componentInParent = ((Component)__instance).GetComponentInParent<Character>();
				if (componentInParent.IsTamed())
				{
					ZNetView componentInParent2 = ((Component)__instance).GetComponentInParent<ZNetView>();
					ZDO zDO = componentInParent2.GetZDO();
					float num = zDO.GetFloat("TimeSinceConsumption", 0f) + dt;
					zDO.Set("TimeSinceConsumption", num);
					if (num >= Cooldown)
					{
						zDO.Set("CanConsume", true);
						zDO.Set("TimeSinceConsumption", 0f);
					}
				}
			}
		}

		public static int HealingReceived = 50;

		public static float Cooldown = 60f;

		public static bool UseItem = true;

		public static bool SuccessMessage = true;
	}

	private readonly Harmony harmony = new Harmony("org.bepinex.plugins.feedfromhand");

	private ConfigEntry<int> HealingReceived;

	private ConfigEntry<float> Cooldown;

	private ConfigEntry<bool> UseItem;

	private ConfigEntry<bool> SuccessMessage;

	private void Awake()
	{
		harmony.PatchAll();
		HealingReceived = ((BaseUnityPlugin)this).Config.Bind<int>("FeedFromHand", "HealingReceived", FeedFromHand_Patch.HealingReceived, "Amount that is healed upon consuming item");
		Cooldown = ((BaseUnityPlugin)this).Config.Bind<float>("FeedFromHand", "Cooldown", FeedFromHand_Patch.Cooldown, "Feeding cooldown");
		UseItem = ((BaseUnityPlugin)this).Config.Bind<bool>("FeedFromHand", "UseItem", FeedFromHand_Patch.UseItem, "Whether or not item is removed from inventory when item is consumed");
		SuccessMessage = ((BaseUnityPlugin)this).Config.Bind<bool>("FeedFromHand", "SuccessMessage", FeedFromHand_Patch.SuccessMessage, "Whether or not success message (Wolf is Happy!) is shown when item is consumed");
		HealingReceived.SettingChanged += HealingReceived_SettingChanged;
		Cooldown.SettingChanged += Cooldown_SettingChanged;
		UseItem.SettingChanged += UseItem_SettingChanged;
		SuccessMessage.SettingChanged += SuccessMessage_SettingChanged;
		SettingsChanged();
	}

	private void SettingsChanged()
	{
		HealingReceived_SettingChanged(null, null);
		Cooldown_SettingChanged(null, null);
		UseItem_SettingChanged(null, null);
		SuccessMessage_SettingChanged(null, null);
	}

	private void HealingReceived_SettingChanged(object sender, EventArgs e)
	{
		FeedFromHand_Patch.HealingReceived = HealingReceived.Value;
	}

	private void Cooldown_SettingChanged(object sender, EventArgs e)
	{
		FeedFromHand_Patch.Cooldown = Cooldown.Value;
	}

	private void UseItem_SettingChanged(object sender, EventArgs e)
	{
		FeedFromHand_Patch.UseItem = UseItem.Value;
	}

	private void SuccessMessage_SettingChanged(object sender, EventArgs e)
	{
		FeedFromHand_Patch.SuccessMessage = SuccessMessage.Value;
	}
}