Decompiled source of FoodDeathPenalty v1.0.0

plugins/FoodDeathPenalty/FoodDeathPenalty.dll

Decompiled 21 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
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.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.6.2", FrameworkDisplayName = ".NET Framework 4.6.2")]
[assembly: AssemblyCompany("FoodDeathPenalty")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("FoodDeathPenalty")]
[assembly: AssemblyTitle("FoodDeathPenalty")]
[assembly: AssemblyVersion("1.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 FoodDeathPenalty
{
	[BepInPlugin("com.yourname.fooddeathpenalty", "FoodDeathPenalty", "1.0.0")]
	public class FoodDeathPenaltyPlugin : BaseUnityPlugin
	{
		public const string Guid = "com.yourname.fooddeathpenalty";

		public const string Name = "FoodDeathPenalty";

		public const string Version = "1.0.0";

		internal static FoodDeathPenaltyPlugin Instance;

		internal static ConfigEntry<float> RemainingFraction;

		internal static List<KeyValuePair<ItemData, float>> Pending;

		private void Awake()
		{
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Expected O, but got Unknown
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			Instance = this;
			RemainingFraction = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RemainingFraction", 0.5f, new ConfigDescription("Fraction of each food's remaining duration kept after death (0.5 = lose 50%, 1 = lose nothing, 0 = vanilla behaviour).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), Array.Empty<object>()));
			new Harmony("com.yourname.fooddeathpenalty").PatchAll();
		}

		internal IEnumerator ApplyFoodsDelayed(Player player)
		{
			yield return (object)new WaitForSeconds(0.5f);
			if ((Object)(object)player == (Object)null || Pending == null)
			{
				yield break;
			}
			List<Food> value = Traverse.Create((object)player).Field("m_foods").GetValue<List<Food>>();
			value?.Clear();
			foreach (KeyValuePair<ItemData, float> entry in Pending)
			{
				if (player.EatFood(entry.Key))
				{
					Food val = value?.Find((Food f) => f.m_item == entry.Key || f.m_item.m_shared.m_name == entry.Key.m_shared.m_name);
					if (val != null)
					{
						val.m_time = entry.Value;
					}
				}
			}
			Pending = null;
		}
	}
	[HarmonyPatch(typeof(Player), "OnDeath")]
	internal static class OnDeathPatch
	{
		private static void Prefix(Player __instance)
		{
			if ((Object)(object)__instance != (Object)(object)Player.m_localPlayer)
			{
				return;
			}
			List<Food> value = Traverse.Create((object)__instance).Field("m_foods").GetValue<List<Food>>();
			if (value == null || value.Count == 0)
			{
				FoodDeathPenaltyPlugin.Pending = null;
				return;
			}
			List<KeyValuePair<ItemData, float>> list = new List<KeyValuePair<ItemData, float>>();
			float value2 = FoodDeathPenaltyPlugin.RemainingFraction.Value;
			foreach (Food item in value)
			{
				if (item?.m_item != null)
				{
					float num = item.m_time * value2;
					if (num > 0f)
					{
						list.Add(new KeyValuePair<ItemData, float>(item.m_item, num));
					}
				}
			}
			FoodDeathPenaltyPlugin.Pending = list;
		}
	}
	[HarmonyPatch(typeof(Player), "OnSpawned")]
	internal static class OnSpawnedPatch
	{
		private static void Postfix(Player __instance)
		{
			if (!((Object)(object)__instance != (Object)(object)Player.m_localPlayer) && FoodDeathPenaltyPlugin.Pending != null)
			{
				((MonoBehaviour)FoodDeathPenaltyPlugin.Instance).StartCoroutine(FoodDeathPenaltyPlugin.Instance.ApplyFoodsDelayed(__instance));
			}
		}
	}
}