Decompiled source of GracefulLanding v1.0.7

GracefulLanding.dll

Decompiled 2 months 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;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("GracefulLanding")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Crystal")]
[assembly: AssemblyProduct("GracefulLanding")]
[assembly: AssemblyCopyright("Copyright © 2023 Crystal Ferrai")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("59e3a8b0-42d8-4f95-b9e2-e61bbe7f58f4")]
[assembly: AssemblyFileVersion("1.0.7.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.7.0")]
namespace GracefulLanding;

[BepInPlugin("dev.crystal.gracefullanding", "Graceful Landing", "1.0.7.0")]
[BepInProcess("valheim.exe")]
[BepInProcess("valheim_server.exe")]
public class GracefulLandingPlugin : BaseUnityPlugin
{
	[HarmonyPatch(typeof(Character))]
	private static class Character_Patches
	{
		[CompilerGenerated]
		private sealed class <UpdateGroundContact_Transpiler>d__0 : 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 IEnumerator<CodeInstruction> <>7__wrap1;

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

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

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

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

			private bool MoveNext()
			{
				try
				{
					switch (<>1__state)
					{
					default:
						return false;
					case 0:
						<>1__state = -1;
						<>7__wrap1 = instructions.GetEnumerator();
						<>1__state = -3;
						break;
					case 1:
						<>1__state = -3;
						break;
					}
					if (<>7__wrap1.MoveNext())
					{
						CodeInstruction current = <>7__wrap1.Current;
						if (current.opcode == OpCodes.Ldc_R4)
						{
							float num = (float)current.operand;
							if (num != 4f)
							{
								if (num != 16f)
								{
									if (num == 100f)
									{
										current.operand = MaxDamageAmount.Value;
									}
								}
								else
								{
									current.operand = MaxDamageHeight.Value;
								}
							}
							else
							{
								current.operand = MinDamageHeight.Value;
							}
						}
						<>2__current = current;
						<>1__state = 1;
						return true;
					}
					<>m__Finally1();
					<>7__wrap1 = 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__wrap1 != null)
				{
					<>7__wrap1.Dispose();
				}
			}

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

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

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

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

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

	public static ConfigEntry<float> MinDamageHeight;

	public static ConfigEntry<float> MaxDamageHeight;

	public static ConfigEntry<float> MaxDamageAmount;

	private static Harmony sCharacterHarmony;

	private void Awake()
	{
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Expected O, but got Unknown
		MinDamageHeight = ((BaseUnityPlugin)this).Config.Bind<float>("Falling", "MinDamageHeight", 8f, "The minimum distance you must fall to receive any fall damage. Allowed range 1-10000. Game default 4.");
		MinDamageHeight.SettingChanged += Falling_SettingChanged;
		MaxDamageHeight = ((BaseUnityPlugin)this).Config.Bind<float>("Falling", "MaxDamageHeight", 64f, "The minimum distance you must to receive maximum fall damage. Allowed range 1-10000. Must be equal to or higher than MinDamageHeight. Game default 16.");
		MaxDamageHeight.SettingChanged += Falling_SettingChanged;
		MaxDamageAmount = ((BaseUnityPlugin)this).Config.Bind<float>("Falling", "MaxDamageAmount", 100f, "The maximum fall damage that can be received. Allowed range 0-10000. Game default 100.");
		MaxDamageAmount.SettingChanged += Falling_SettingChanged;
		ClampConfig();
		sCharacterHarmony = new Harmony("dev.crystal.gracefullanding_Character");
		sCharacterHarmony.PatchAll(typeof(Character_Patches));
	}

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

	private static void ClampConfig()
	{
		if (MinDamageHeight.Value < 1f)
		{
			MinDamageHeight.Value = 1f;
		}
		if (MinDamageHeight.Value > 10000f)
		{
			MinDamageHeight.Value = 10000f;
		}
		if (MaxDamageHeight.Value < MinDamageHeight.Value)
		{
			MaxDamageHeight.Value = MinDamageHeight.Value;
		}
		if (MaxDamageHeight.Value > 10000f)
		{
			MaxDamageHeight.Value = 10000f;
		}
		if (MaxDamageAmount.Value < 0f)
		{
			MaxDamageAmount.Value = 0f;
		}
		if (MaxDamageAmount.Value > 10000f)
		{
			MaxDamageAmount.Value = 10000f;
		}
	}

	private void Falling_SettingChanged(object sender, EventArgs e)
	{
		ClampConfig();
		sCharacterHarmony.UnpatchSelf();
		sCharacterHarmony.PatchAll(typeof(Character_Patches));
	}
}