Decompiled source of Sated v1.1.12

Sated.dll

Decompiled 2 weeks ago
using System;
using System.Collections;
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.12.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.1.12.0")]
namespace Sated;

[BepInPlugin("dev.crystal.sated", "Sated", "1.1.12.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
		}

		[CompilerGenerated]
		private sealed class <GetTotalFoodValue_Transpiler>d__1 : IEnumerable<CodeInstruction>, IEnumerable, IEnumerator<CodeInstruction>, IDisposable, IEnumerator
		{
			private int <>1__state;

			private CodeInstruction <>2__current;

			private int <>l__initialThreadId;

			private IEnumerable<CodeInstruction> instructions;

			public IEnumerable<CodeInstruction> <>3__instructions;

			private TranspilerState <state>5__2;

			private IEnumerator<CodeInstruction> <>7__wrap2;

			CodeInstruction IEnumerator<CodeInstruction>.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			object IEnumerator.Current
			{
				[DebuggerHidden]
				get
				{
					return <>2__current;
				}
			}

			[DebuggerHidden]
			public <GetTotalFoodValue_Transpiler>d__1(int <>1__state)
			{
				this.<>1__state = <>1__state;
				<>l__initialThreadId = Environment.CurrentManagedThreadId;
			}

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				int num = <>1__state;
				if (num == -3 || (uint)(num - 1) <= 9u)
				{
					try
					{
					}
					finally
					{
						<>m__Finally1();
					}
				}
				<>7__wrap2 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_0197: Unknown result type (might be due to invalid IL or missing references)
				//IL_01a1: Expected O, but got Unknown
				//IL_0213: Unknown result type (might be due to invalid IL or missing references)
				//IL_021d: Expected O, but got Unknown
				//IL_028f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0299: Expected O, but got Unknown
				try
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
						<>1__state = -1;
						<state>5__2 = TranspilerState.Searching;
						<>7__wrap2 = instructions.GetEnumerator();
						<>1__state = -3;
						break;
					case 1:
						<>1__state = -3;
						break;
					case 2:
						<>1__state = -3;
						break;
					case 3:
						<>1__state = -3;
						break;
					case 4:
						<>1__state = -3;
						<state>5__2 = TranspilerState.ReplacingStamina;
						break;
					case 5:
						<>1__state = -3;
						break;
					case 6:
						<>1__state = -3;
						<state>5__2 = TranspilerState.ReplacingEitr;
						break;
					case 7:
						<>1__state = -3;
						break;
					case 8:
						<>1__state = -3;
						<state>5__2 = TranspilerState.Finishing;
						break;
					case 9:
						<>1__state = -3;
						break;
					case 10:
						<>1__state = -3;
						break;
					}
					while (<>7__wrap2.MoveNext())
					{
						CodeInstruction current = <>7__wrap2.Current;
						switch (<state>5__2)
						{
						case TranspilerState.Searching:
							if (current.opcode == OpCodes.Ldloca_S)
							{
								<state>5__2 = TranspilerState.Checking1;
							}
							<>2__current = current;
							<>1__state = 1;
							return true;
						case TranspilerState.Checking1:
							if (current.opcode == OpCodes.Call)
							{
								<state>5__2 = TranspilerState.Checking2;
							}
							else
							{
								<state>5__2 = TranspilerState.Searching;
							}
							<>2__current = current;
							<>1__state = 2;
							return true;
						case TranspilerState.Checking2:
							if (current.opcode == OpCodes.Stloc_1)
							{
								<state>5__2 = TranspilerState.ReplacingHp;
							}
							else
							{
								<state>5__2 = TranspilerState.Searching;
							}
							<>2__current = current;
							<>1__state = 3;
							return true;
						case TranspilerState.ReplacingHp:
							if (current.opcode == OpCodes.Ldfld)
							{
								<>2__current = new CodeInstruction(OpCodes.Call, (object)typeof(Player_Patches).GetMethod("GetHp", BindingFlags.Static | BindingFlags.NonPublic));
								<>1__state = 4;
								return true;
							}
							<>2__current = current;
							<>1__state = 5;
							return true;
						case TranspilerState.ReplacingStamina:
							if (current.opcode == OpCodes.Ldfld)
							{
								<>2__current = new CodeInstruction(OpCodes.Call, (object)typeof(Player_Patches).GetMethod("GetStamina", BindingFlags.Static | BindingFlags.NonPublic));
								<>1__state = 6;
								return true;
							}
							<>2__current = current;
							<>1__state = 7;
							return true;
						case TranspilerState.ReplacingEitr:
							if (current.opcode == OpCodes.Ldfld)
							{
								<>2__current = new CodeInstruction(OpCodes.Call, (object)typeof(Player_Patches).GetMethod("GetEitr", BindingFlags.Static | BindingFlags.NonPublic));
								<>1__state = 8;
								return true;
							}
							<>2__current = current;
							<>1__state = 9;
							return true;
						case TranspilerState.Finishing:
							<>2__current = current;
							<>1__state = 10;
							return true;
						}
					}
					<>m__Finally1();
					<>7__wrap2 = null;
					return false;
				}
				catch
				{
					//try-fault
					((IDisposable)this).Dispose();
					throw;
				}
			}

			bool IEnumerator.MoveNext()
			{
				//ILSpy generated this explicit interface implementation from .override directive in MoveNext
				return this.MoveNext();
			}

			private void <>m__Finally1()
			{
				<>1__state = -1;
				if (<>7__wrap2 != null)
				{
					<>7__wrap2.Dispose();
				}
			}

			[DebuggerHidden]
			void IEnumerator.Reset()
			{
				throw new NotSupportedException();
			}

			[DebuggerHidden]
			IEnumerator<CodeInstruction> IEnumerable<CodeInstruction>.GetEnumerator()
			{
				<GetTotalFoodValue_Transpiler>d__1 <GetTotalFoodValue_Transpiler>d__;
				if (<>1__state == -2 && <>l__initialThreadId == Environment.CurrentManagedThreadId)
				{
					<>1__state = 0;
					<GetTotalFoodValue_Transpiler>d__ = this;
				}
				else
				{
					<GetTotalFoodValue_Transpiler>d__ = new <GetTotalFoodValue_Transpiler>d__1(0);
				}
				<GetTotalFoodValue_Transpiler>d__.instructions = <>3__instructions;
				return <GetTotalFoodValue_Transpiler>d__;
			}

			[DebuggerHidden]
			IEnumerator IEnumerable.GetEnumerator()
			{
				return ((IEnumerable<CodeInstruction>)this).GetEnumerator();
			}
		}

		[IteratorStateMachine(typeof(<GetTotalFoodValue_Transpiler>d__1))]
		[HarmonyPatch("GetTotalFoodValue")]
		[HarmonyTranspiler]
		private static IEnumerable<CodeInstruction> GetTotalFoodValue_Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <GetTotalFoodValue_Transpiler>d__1(-2)
			{
				<>3__instructions = instructions
			};
		}

		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));
	}
}