Decompiled source of Sated v1.1.11

Sated.dll

Decompiled 5 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("Sated")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Crystal")]
[assembly: AssemblyProduct("Sated")]
[assembly: AssemblyCopyright("Copyright © 2023 Crystal")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("06b242d5-3a1a-407a-a9d1-a816c4750099")]
[assembly: AssemblyFileVersion("1.1.11.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.1.11.0")]
namespace Sated;

[BepInPlugin("dev.crystal.sated", "Sated", "1.1.11.0")]
[BepInProcess("valheim.exe")]
[BepInProcess("valheim_server.exe")]
public class SatedPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Player))]
	private static class Player_Patches
	{
		private enum TranspilerState
		{
			Searching,
			Checking1,
			Checking2,
			ReplacingHp,
			ReplacingStamina,
			ReplacingEitr,
			Finishing
		}

		[HarmonyPatch("GetTotalFoodValue")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> GetTotalFoodValue_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			TranspilerState state = TranspilerState.Searching;
			foreach (CodeInstruction instruction in instructions)
			{
				switch (state)
				{
				case TranspilerState.Searching:
					if (instruction.opcode == OpCodes.Ldloca_S)
					{
						state = TranspilerState.Checking1;
					}
					yield return instruction;
					break;
				case TranspilerState.Checking1:
					state = ((instruction.opcode == OpCodes.Call) ? TranspilerState.Checking2 : TranspilerState.Searching);
					yield return instruction;
					break;
				case TranspilerState.Checking2:
					state = ((instruction.opcode == OpCodes.Stloc_1) ? TranspilerState.ReplacingHp : TranspilerState.Searching);
					yield return instruction;
					break;
				case TranspilerState.ReplacingHp:
					if (instruction.opcode == OpCodes.Ldfld)
					{
						yield return new CodeInstruction(OpCodes.Call, (object)typeof(Player_Patches).GetMethod("GetHp", BindingFlags.Static | BindingFlags.NonPublic));
						state = TranspilerState.ReplacingStamina;
					}
					else
					{
						yield return instruction;
					}
					break;
				case TranspilerState.ReplacingStamina:
					if (instruction.opcode == OpCodes.Ldfld)
					{
						yield return new CodeInstruction(OpCodes.Call, (object)typeof(Player_Patches).GetMethod("GetStamina", BindingFlags.Static | BindingFlags.NonPublic));
						state = TranspilerState.ReplacingEitr;
					}
					else
					{
						yield return instruction;
					}
					break;
				case TranspilerState.ReplacingEitr:
					if (instruction.opcode == OpCodes.Ldfld)
					{
						yield return new CodeInstruction(OpCodes.Call, (object)typeof(Player_Patches).GetMethod("GetEitr", BindingFlags.Static | BindingFlags.NonPublic));
						state = TranspilerState.Finishing;
					}
					else
					{
						yield return instruction;
					}
					break;
				case TranspilerState.Finishing:
					yield return instruction;
					break;
				}
			}
		}

		private static float GetHp(Food food)
		{
			return (1f - Mathf.Pow(GetTime(food), HealthCurveExponent.Value)) * food.m_item.m_shared.m_food;
		}

		private static float GetStamina(Food food)
		{
			return (1f - Mathf.Pow(GetTime(food), StaminaCurveExponent.Value)) * food.m_item.m_shared.m_foodStamina;
		}

		private static float GetEitr(Food food)
		{
			return (1f - Mathf.Pow(GetTime(food), EitrCurveExponent.Value)) * food.m_item.m_shared.m_foodEitr;
		}

		private static float GetTime(Food food)
		{
			return 1f - food.m_time / food.m_item.m_shared.m_foodBurnTime;
		}
	}

	public const string ModId = "dev.crystal.sated";

	public static ConfigEntry<float> HealthCurveExponent;

	public static ConfigEntry<float> StaminaCurveExponent;

	public static ConfigEntry<float> EitrCurveExponent;

	private static Harmony sPlayerHarmony;

	private static readonly FieldInfo sPlayerFoodsField;

	static SatedPlugin()
	{
		sPlayerFoodsField = typeof(Player).GetField("m_foods", BindingFlags.Instance | BindingFlags.NonPublic);
	}

	private void Awake()
	{
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Expected O, but got Unknown
		HealthCurveExponent = ((BaseUnityPlugin)this).Config.Bind<float>("Food", "HealthCurveExponent", 8f, "The value of the exponent 'e' used in the food curve formula 'y = 1 - x^e' for calculating added health. Valid range 0.1 - 100. Higher values make you full longer, but also drop off more suddenly. A value of 1 indicates a linear decline. Values less than 1 invert the curve, causing a faster initial decline which gradually slows down.");
		HealthCurveExponent.SettingChanged += CurveExponent_SettingChanged;
		StaminaCurveExponent = ((BaseUnityPlugin)this).Config.Bind<float>("Food", "StaminaCurveExponent", 8f, "The value of the exponent 'e' used in the food curve formula 'y = 1 - x^e' for calculating added stamina. Valid range 0.1 - 100. Higher values make you full longer, but also drop off more suddenly. A value of 1 indicates a linear decline. Values less than 1 invert the curve, causing a faster initial decline which gradually slows down.");
		StaminaCurveExponent.SettingChanged += CurveExponent_SettingChanged;
		EitrCurveExponent = ((BaseUnityPlugin)this).Config.Bind<float>("Food", "EitrCurveExponent", 8f, "The value of the exponent 'e' used in the food curve formula 'y = 1 - x^e' for calculating added eitr. Valid range 0.1 - 100. Higher values make you full longer, but also drop off more suddenly. A value of 1 indicates a linear decline. Values less than 1 invert the curve, causing a faster initial decline which gradually slows down.");
		EitrCurveExponent.SettingChanged += CurveExponent_SettingChanged;
		ClampConfig();
		sPlayerHarmony = new Harmony("dev.crystal.sated_Player");
		sPlayerHarmony.PatchAll(typeof(Player_Patches));
	}

	private void OnDestroy()
	{
		sPlayerHarmony.UnpatchSelf();
	}

	private static void ClampConfig()
	{
		if (HealthCurveExponent.Value < 0.1f)
		{
			HealthCurveExponent.Value = 0.1f;
		}
		if (HealthCurveExponent.Value > 100f)
		{
			HealthCurveExponent.Value = 100f;
		}
		if (StaminaCurveExponent.Value < 0.1f)
		{
			StaminaCurveExponent.Value = 0.1f;
		}
		if (StaminaCurveExponent.Value > 100f)
		{
			StaminaCurveExponent.Value = 100f;
		}
		if (EitrCurveExponent.Value < 0.1f)
		{
			EitrCurveExponent.Value = 0.1f;
		}
		if (EitrCurveExponent.Value > 100f)
		{
			EitrCurveExponent.Value = 100f;
		}
	}

	private void CurveExponent_SettingChanged(object sender, EventArgs e)
	{
		ClampConfig();
	}

	private static AssetBundle LoadAssetBundle(string name)
	{
		Assembly callingAssembly = Assembly.GetCallingAssembly();
		return AssetBundle.LoadFromStream(callingAssembly.GetManifestResourceStream(callingAssembly.GetName().Name + "." + name));
	}
}