Decompiled source of KeepFoodOnDeath v1.2.0

KeepFoodOnDeath.dll

Decompiled 12 hours ago
using System;
using System.Collections;
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.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyVersion("0.0.0.0")]
[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 KeepFoodOnDeath
{
	[BepInPlugin("com.Sneth.keepfoodondeath", "Keep Food On Death", "1.0.0")]
	public class KeepFoodOnDeathPlugin : BaseUnityPlugin
	{
		public const string PluginGuid = "com.Sneth.keepfoodondeath";

		public const string PluginName = "Keep Food On Death";

		public const string PluginVersion = "1.0.0";

		internal static ConfigEntry<bool> ModEnabled;

		internal static ConfigEntry<bool> KeepFood;

		internal static ConfigEntry<bool> FullHealthOnRespawn;

		internal static ConfigEntry<bool> FullStaminaOnRespawn;

		internal static ArrayList SavedFoods;

		internal static string SavedPlayerName;

		internal static bool RestorePending;

		private void Awake()
		{
			//IL_0086: Unknown result type (might be due to invalid IL or missing references)
			ModEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Master switch for the mod.");
			KeepFood = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "KeepFoodAndBuffs", true, "Keep eaten food (with its remaining time, health, stamina and regen) after dying.");
			FullHealthOnRespawn = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "FullHealthOnRespawn", true, "Respawn with full health (based on the restored food).");
			FullStaminaOnRespawn = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "FullStaminaOnRespawn", false, "Also respawn with full stamina.");
			new Harmony("com.Sneth.keepfoodondeath").PatchAll();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Keep Food On Death 1.0.0 loaded.");
		}
	}
	[HarmonyPatch(typeof(Player), "OnDeath")]
	internal static class Player_OnDeath_Patch
	{
		private static void Prefix(Player __instance)
		{
			try
			{
				if (KeepFoodOnDeathPlugin.ModEnabled.Value && !((Object)(object)__instance != (Object)(object)Player.m_localPlayer))
				{
					KeepFoodOnDeathPlugin.SavedFoods = ((Traverse.Create((object)__instance).Field("m_foods").GetValue() is IList c) ? new ArrayList(c) : new ArrayList());
					KeepFoodOnDeathPlugin.SavedPlayerName = __instance.GetPlayerName();
					KeepFoodOnDeathPlugin.RestorePending = true;
				}
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("[KeepFoodOnDeath] Failed to save food: " + ex));
			}
		}
	}
	[HarmonyPatch(typeof(Player), "Update")]
	internal static class Player_Update_Patch
	{
		private static void Postfix(Player __instance)
		{
			if (!KeepFoodOnDeathPlugin.RestorePending || (Object)(object)__instance != (Object)(object)Player.m_localPlayer || ((Character)__instance).IsDead())
			{
				return;
			}
			KeepFoodOnDeathPlugin.RestorePending = false;
			try
			{
				if (!KeepFoodOnDeathPlugin.ModEnabled.Value || KeepFoodOnDeathPlugin.SavedPlayerName != __instance.GetPlayerName())
				{
					return;
				}
				Traverse val = Traverse.Create((object)__instance);
				if (KeepFoodOnDeathPlugin.KeepFood.Value && KeepFoodOnDeathPlugin.SavedFoods != null && val.Field("m_foods").GetValue() is IList list)
				{
					list.Clear();
					foreach (object savedFood in KeepFoodOnDeathPlugin.SavedFoods)
					{
						list.Add(savedFood);
					}
					val.Method("UpdateFood", new Type[2]
					{
						typeof(float),
						typeof(bool)
					}, new object[2] { 0f, true }).GetValue();
				}
				if (KeepFoodOnDeathPlugin.FullHealthOnRespawn.Value)
				{
					((Character)__instance).SetHealth(((Character)__instance).GetMaxHealth());
				}
				if (KeepFoodOnDeathPlugin.FullStaminaOnRespawn.Value)
				{
					val.Field("m_stamina").SetValue((object)((Character)__instance).GetMaxStamina());
				}
			}
			catch (Exception ex)
			{
				Debug.LogError((object)("[KeepFoodOnDeath] Failed to restore food: " + ex));
			}
			finally
			{
				KeepFoodOnDeathPlugin.SavedFoods = null;
			}
		}
	}
}