Decompiled source of NoFoodDegradation v1.0.1

NoFoodDegradation.dll

Decompiled a day ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;

[assembly: AssemblyTitle("NoFoodDegradation")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("1.0.1.0")]
namespace NoFoodDegradation;

[BepInPlugin("farmk.NoFoodDegradation", "No Food Degradation", "1.0.1")]
public class NoFoodDegradationPlugin : BaseUnityPlugin
{
	private static NoFoodDegradationPlugin _instance;

	private static MethodInfo _getFoods;

	private static FieldInfo _playerFoodsField;

	private static FieldInfo _foodItem;

	private static FieldInfo _itemShared;

	private static FieldInfo _sharedHealth;

	private static FieldInfo _sharedStamina;

	private static FieldInfo _sharedEitr;

	private static FieldInfo _foodHealth;

	private static FieldInfo _foodStamina;

	private static FieldInfo _foodEitr;

	private static bool _warnedRuntime;

	private void Awake()
	{
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		//IL_007f: Expected O, but got Unknown
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Expected O, but got Unknown
		_instance = this;
		try
		{
			if (!ResolveMembers(((BaseUnityPlugin)this).Logger))
			{
				return;
			}
			Type type = AccessTools.TypeByName("Player");
			List<MethodInfo> list = (from m in AccessTools.GetDeclaredMethods(type)
				where m.Name == "GetTotalFoodValue"
				select m).ToList();
			if (list.Count == 0)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)"Player.GetTotalFoodValue was not found - the game has changed. Mod disabled.");
				return;
			}
			Harmony val = new Harmony("farmk.NoFoodDegradation");
			HarmonyMethod val2 = new HarmonyMethod(typeof(NoFoodDegradationPlugin).GetMethod("Prefix", BindingFlags.Static | BindingFlags.NonPublic));
			foreach (MethodInfo item in list)
			{
				val.Patch((MethodBase)item, val2, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"NoFoodDegradation Mod loaded. Removed all degradation from food.");
		}
		catch (Exception ex)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)("Failed to initialise NoFoodDegradation: " + ex));
		}
	}

	private static bool ResolveMembers(ManualLogSource log)
	{
		Type type = AccessTools.TypeByName("Player");
		if (type == null)
		{
			log.LogError((object)"Type 'Player' not found. Mod disabled.");
			return false;
		}
		Type type2 = AccessTools.Inner(type, "Food");
		if (type2 == null)
		{
			log.LogError((object)"Type 'Player.Food' not found. Mod disabled.");
			return false;
		}
		if (type2.IsValueType)
		{
			log.LogError((object)"Player.Food is now a value type; in-place edits are not possible. Mod disabled.");
			return false;
		}
		_getFoods = AccessTools.DeclaredMethod(type, "GetFoods", Type.EmptyTypes, (Type[])null);
		if (_getFoods == null)
		{
			_playerFoodsField = AccessTools.Field(type, "m_foods");
			if (_playerFoodsField == null)
			{
				log.LogError((object)"Neither Player.GetFoods() nor Player.m_foods found. Mod disabled.");
				return false;
			}
		}
		_foodItem = AccessTools.Field(type2, "m_item");
		_foodHealth = AccessTools.Field(type2, "m_health");
		_foodStamina = AccessTools.Field(type2, "m_stamina");
		_foodEitr = AccessTools.Field(type2, "m_eitr");
		if (_foodItem == null)
		{
			log.LogError((object)"Player.Food.m_item not found. Mod disabled.");
			return false;
		}
		_itemShared = AccessTools.Field(_foodItem.FieldType, "m_shared");
		if (_itemShared == null)
		{
			log.LogError((object)"ItemData.m_shared not found. Mod disabled.");
			return false;
		}
		Type fieldType = _itemShared.FieldType;
		_sharedHealth = AccessTools.Field(fieldType, "m_food");
		_sharedStamina = AccessTools.Field(fieldType, "m_foodStamina");
		_sharedEitr = AccessTools.Field(fieldType, "m_foodEitr");
		if (_foodHealth == null || _sharedHealth == null || _foodStamina == null || _sharedStamina == null)
		{
			log.LogError((object)"Food health/stamina fields not found. Mod disabled.");
			return false;
		}
		if (_foodEitr == null || _sharedEitr == null)
		{
			log.LogWarning((object)"Eitr food fields not found; only health and stamina will be preserved.");
		}
		return true;
	}

	private static void Prefix(object __instance)
	{
		try
		{
			if (!(((!(_getFoods != null)) ? _playerFoodsField.GetValue(__instance) : _getFoods.Invoke(__instance, null)) is IEnumerable enumerable))
			{
				return;
			}
			foreach (object item in enumerable)
			{
				if (item == null)
				{
					continue;
				}
				object value = _foodItem.GetValue(item);
				if (value == null)
				{
					continue;
				}
				object value2 = _itemShared.GetValue(value);
				if (value2 != null)
				{
					_foodHealth.SetValue(item, _sharedHealth.GetValue(value2));
					_foodStamina.SetValue(item, _sharedStamina.GetValue(value2));
					if (_foodEitr != null && _sharedEitr != null)
					{
						_foodEitr.SetValue(item, _sharedEitr.GetValue(value2));
					}
				}
			}
		}
		catch (Exception ex)
		{
			if (!_warnedRuntime)
			{
				_warnedRuntime = true;
				((BaseUnityPlugin)_instance).Logger.LogError((object)("NoFoodDegradation runtime error (logged once): " + ex));
			}
		}
	}
}