Decompiled source of AutoEat TT v1.0.1

plugins\TeflonTed.AutoEat.dll

Decompiled 19 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: IgnoresAccessChecksTo("assembly_guiutils")]
[assembly: IgnoresAccessChecksTo("assembly_utils")]
[assembly: IgnoresAccessChecksTo("assembly_valheim")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("TeflonTed.AutoEat")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Teflon Ted's Auto Eat — re-eat the same food when a buff fully expires")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+aa3557958a7656958068f9c07e5ebb364e91eab7")]
[assembly: AssemblyProduct("TeflonTed.AutoEat")]
[assembly: AssemblyTitle("TeflonTed.AutoEat")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[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 TeflonTed.AutoEat
{
	[BepInPlugin("com.teflonted.valheim.autoeat", "Teflon Ted's Auto Eat", "1.0.1")]
	public class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.teflonted.valheim.autoeat";

		public const string PluginName = "Teflon Ted's Auto Eat";

		public const string PluginVersion = "1.0.1";

		private void Awake()
		{
			Harmony.CreateAndPatchAll(typeof(Plugin).Assembly, "com.teflonted.valheim.autoeat");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Teflon Ted's Auto Eat 1.0.1 loaded");
		}
	}
	[HarmonyPatch(typeof(Player), "UpdateFood")]
	internal static class Player_UpdateFood_Patch
	{
		private struct PendingEat
		{
			public string SharedName;

			public ItemData OriginalStack;
		}

		private const float FoodTickInterval = 1f;

		private static readonly List<PendingEat> Expiring = new List<PendingEat>();

		private static int Depth;

		private static void Prefix(Player __instance, float dt, bool forceUpdate)
		{
			if (Depth > 0)
			{
				return;
			}
			Expiring.Clear();
			if ((Object)(object)__instance == (Object)null || (Object)(object)__instance != (Object)(object)Player.m_localPlayer || ((Character)__instance).IsDead())
			{
				return;
			}
			float num = __instance.m_foodUpdateTimer + dt * Game.m_foodRate;
			if (!forceUpdate && !(num >= 1f))
			{
				return;
			}
			List<Food> foods = __instance.GetFoods();
			if (foods == null)
			{
				return;
			}
			foreach (Food item in foods)
			{
				if (item?.m_item?.m_shared != null && item.m_time <= 1f)
				{
					Expiring.Add(new PendingEat
					{
						SharedName = item.m_item.m_shared.m_name,
						OriginalStack = item.m_item
					});
				}
			}
		}

		private static void Postfix(Player __instance)
		{
			if (Depth > 0 || Expiring.Count == 0)
			{
				return;
			}
			if ((Object)(object)__instance == (Object)null || (Object)(object)__instance != (Object)(object)Player.m_localPlayer || ((Character)__instance).IsDead())
			{
				Expiring.Clear();
				return;
			}
			Inventory inventory = ((Humanoid)__instance).GetInventory();
			if (inventory == null)
			{
				Expiring.Clear();
				return;
			}
			Depth++;
			try
			{
				foreach (PendingEat item in Expiring)
				{
					ItemData val = FindFoodStack(inventory, item);
					if (val != null && __instance.CanEat(val, false))
					{
						((Humanoid)__instance).ConsumeItem(inventory, val, false);
					}
				}
			}
			finally
			{
				Depth--;
				Expiring.Clear();
			}
		}

		private static ItemData FindFoodStack(Inventory inventory, PendingEat pending)
		{
			if (pending.OriginalStack != null && pending.OriginalStack.m_stack > 0 && inventory.ContainsItem(pending.OriginalStack))
			{
				return pending.OriginalStack;
			}
			if (string.IsNullOrEmpty(pending.SharedName))
			{
				return null;
			}
			ItemData val = null;
			foreach (ItemData allItem in inventory.GetAllItems())
			{
				if (allItem?.m_shared != null && allItem.m_stack > 0 && !(allItem.m_shared.m_name != pending.SharedName))
				{
					if (allItem.m_gridPos.y == 0)
					{
						return allItem;
					}
					if (val == null)
					{
						val = allItem;
					}
				}
			}
			return val;
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}