Decompiled source of FoodBuffConfig v1.1.2

plugins/FoodBuffConfig/FoodBuffConfig.dll

Decompiled 2 weeks ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Managers;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[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 FoodBuffConfigMod
{
	[BepInPlugin("0zzerod.FoodBuffConfig", "FoodBuffConfig", "1.1.2")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class FoodBuffConfigPlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Player), "EatFood")]
		public static class Patch_Player_EatFood
		{
			private static readonly Dictionary<string, float> _originalBurnTimes = new Dictionary<string, float>();

			private static void Prefix(ItemData item)
			{
				if (item?.m_shared == null)
				{
					return;
				}
				GameObject dropPrefab = item.m_dropPrefab;
				string text = ((dropPrefab != null) ? ((Object)dropPrefab).name : null);
				if (text != null && Instance.FoodNames.Contains(text))
				{
					if (!_originalBurnTimes.TryGetValue(text, out var value))
					{
						value = item.m_shared.m_foodBurnTime;
						_originalBurnTimes[text] = value;
					}
					float num = value * Instance.DurationMultiplier.Value;
					float value2 = Instance.MaxFoodBurnTime.Value;
					float num2 = Mathf.Ceil(num / value2) * value2;
					item.m_shared.m_foodBurnTime = num2;
					Log.LogInfo((object)$"Scaled duration for '{item.m_shared.m_name}': *={Instance.DurationMultiplier.Value:F1} → {num2:F1}s");
				}
			}

			private static void Postfix(ItemData item)
			{
				if (item?.m_shared != null)
				{
					GameObject dropPrefab = item.m_dropPrefab;
					string text = ((dropPrefab != null) ? ((Object)dropPrefab).name : null);
					if (text != null && _originalBurnTimes.TryGetValue(text, out var value))
					{
						item.m_shared.m_foodBurnTime = value;
					}
				}
			}
		}

		public static ManualLogSource Log;

		private ConfigEntry<string> FoodNamesRaw;

		private ConfigEntry<float> HealthBonusMultiplier;

		private ConfigEntry<float> StaminaBonusMultiplier;

		private ConfigEntry<float> EitrBonusMultiplier;

		private ConfigEntry<float> DurationMultiplier;

		private ConfigEntry<float> MaxFoodHealth;

		private ConfigEntry<float> MaxFoodStamina;

		private ConfigEntry<float> MaxFoodEitr;

		private ConfigEntry<float> MaxFoodBurnTime;

		private List<string> FoodNames = new List<string>();

		private static FoodBuffConfigPlugin _instance;

		public static FoodBuffConfigPlugin Instance => _instance;

		private void Awake()
		{
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			_instance = this;
			Log = ((BaseUnityPlugin)this).Logger;
			BindConfig();
			FoodNames = (from s in FoodNamesRaw.Value.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)
				select s.Trim() into s
				where s.Length > 0
				select s).ToList();
			Log.LogInfo((object)string.Format("[BuffConfig] Loaded {0} food names: {1}", FoodNames.Count, string.Join(", ", FoodNames)));
			PrefabManager.OnPrefabsRegistered += OnPrefabsReady;
			new Harmony("0zzerod.FoodBuffConfig").PatchAll();
			Log.LogInfo((object)"[FoodBuffConfig] Harmony patches applied");
		}

		private void BindConfig()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Expected O, but got Unknown
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Expected O, but got Unknown
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Expected O, but got Unknown
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Expected O, but got Unknown
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Expected O, but got Unknown
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fe: Expected O, but got Unknown
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Expected O, but got Unknown
			//IL_0156: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Expected O, but got Unknown
			//IL_0187: Unknown result type (might be due to invalid IL or missing references)
			//IL_0191: Expected O, but got Unknown
			//IL_01b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c2: Expected O, but got Unknown
			string text = "FoodBuffConfig";
			ConfigurationManagerAttributes val = new ConfigurationManagerAttributes
			{
				IsAdminOnly = true
			};
			FoodNamesRaw = ((BaseUnityPlugin)this).Config.Bind<string>(text, "Food Names", "FeastMeadows, FeastBlackforest, FeastSwamps, FeastOceans, FeastMountains, FeastPlains, FeastMistlands, FeastAshlands, FeastDeepNorth", new ConfigDescription("Comma-separated internal item names to boost when eaten.", (AcceptableValueBase)null, Array.Empty<object>()));
			HealthBonusMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>(text, "Health Bonus Multiplier", 1.2f, new ConfigDescription("Multiplier for max health and regen bonus from food.", (AcceptableValueBase)null, new object[1] { val }));
			StaminaBonusMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>(text, "Stamina Bonus Multiplier", 1.1f, new ConfigDescription("Multiplier for stamina bonus from food.", (AcceptableValueBase)null, new object[1] { val }));
			EitrBonusMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>(text, "Eitr Bonus Multiplier", 1.5f, new ConfigDescription("Multiplier for Eitr bonus from food.", (AcceptableValueBase)null, new object[1] { val }));
			DurationMultiplier = ((BaseUnityPlugin)this).Config.Bind<float>(text, "Duration Multiplier", 4f, new ConfigDescription("Multiplier for duration of food effects when eaten.", (AcceptableValueBase)null, new object[1] { val }));
			MaxFoodHealth = ((BaseUnityPlugin)this).Config.Bind<float>(text, "Health Rounding", 5f, new ConfigDescription("Rounding unit for health bonus from food.", (AcceptableValueBase)null, new object[1] { val }));
			MaxFoodStamina = ((BaseUnityPlugin)this).Config.Bind<float>(text, "Stamina Rounding", 1f, new ConfigDescription("Rounding unit for stamina bonus from food.", (AcceptableValueBase)null, new object[1] { val }));
			MaxFoodEitr = ((BaseUnityPlugin)this).Config.Bind<float>(text, "Eitr Rounding", 1f, new ConfigDescription("Rounding unit for Eitr bonus from food.", (AcceptableValueBase)null, new object[1] { val }));
			MaxFoodBurnTime = ((BaseUnityPlugin)this).Config.Bind<float>(text, "Time Rounding", 60f, new ConfigDescription("Rounding unit in seconds for duration of food effect.", (AcceptableValueBase)null, new object[1] { val }));
		}

		private void OnPrefabsReady()
		{
			PrefabManager.OnPrefabsRegistered -= OnPrefabsReady;
			foreach (string foodName in FoodNames)
			{
				GameObject val = PrefabManager.Instance.GetPrefab(foodName) ?? PrefabManager.Instance.GetPrefab(foodName.ToLowerInvariant());
				if ((Object)(object)val == (Object)null)
				{
					Log.LogWarning((object)("[FoodBuffConfig] Could not find prefab for '" + foodName + "'"));
					continue;
				}
				ItemDrop component = val.GetComponent<ItemDrop>();
				if ((Object)(object)component == (Object)null)
				{
					Log.LogWarning((object)("[FoodBuffConfig] Prefab '" + foodName + "' missing ItemDrop"));
					continue;
				}
				SharedData shared = component.m_itemData.m_shared;
				shared.m_food *= HealthBonusMultiplier.Value;
				shared.m_foodRegen *= HealthBonusMultiplier.Value;
				shared.m_foodStamina *= StaminaBonusMultiplier.Value;
				shared.m_foodEitr *= EitrBonusMultiplier.Value;
				float num = Mathf.Ceil(shared.m_food);
				float value = MaxFoodHealth.Value;
				shared.m_food = Mathf.Ceil(num / value) * value;
				float num2 = Mathf.Ceil(shared.m_foodStamina);
				float value2 = MaxFoodStamina.Value;
				shared.m_foodStamina = Mathf.Ceil(num2 / value2) * value2;
				float num3 = Mathf.Ceil(shared.m_foodEitr);
				float value3 = MaxFoodEitr.Value;
				shared.m_foodEitr = Mathf.Ceil(num3 / value3) * value3;
				Log.LogInfo((object)$"[FoodBuffConfig] Scaled stats for '{foodName}': health×{HealthBonusMultiplier.Value:F2}, regen×{HealthBonusMultiplier.Value:F2}, stam×{StaminaBonusMultiplier.Value:F2}, eitr×{EitrBonusMultiplier.Value:F2}");
			}
		}
	}
}