Decompiled source of BetterFallDamage v1.1.1

BetterFallDamage.dll

Decompiled 10 months ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BetterFallDamage.Patches;
using GameNetcodeStuff;
using HarmonyLib;
using Unity.Netcode;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("BetterFallDamage")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("My first plugin")]
[assembly: AssemblyFileVersion("1.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0")]
[assembly: AssemblyProduct("BetterFallDamage")]
[assembly: AssemblyTitle("BetterFallDamage")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.0.0")]
[module: UnverifiableCode]
namespace BetterFallDamage
{
	[BepInPlugin("BetterFallDamage", "BetterFallDamage", "1.1.0")]
	[BepInProcess("Lethal Company.exe")]
	public class BetterFallDamageClass : BaseUnityPlugin
	{
		public static ManualLogSource BFDLogger;

		internal static bool disableServerSideConfig;

		internal static bool disableFallDamage;

		internal static bool enableWeightScaling;

		internal static int weightScalingFactor;

		internal static float scaledWeightScalingFactor;

		internal static float scaledFallTimeDamage;

		internal static float scaledFallTimeInstaKill;

		internal static float fallTimeDamageExponent;

		internal const float OneSecondOfFalling = 38f;

		internal const float OnePoundInCode = 0.0095f;

		private ConfigEntry<bool> configDisableServerSideConfig;

		private ConfigEntry<bool> configDisableFallDamage;

		private ConfigEntry<bool> configEnableWeightScaling;

		private ConfigEntry<int> configWeightScalingFactor;

		private ConfigEntry<float> configFallTimeDamage;

		private ConfigEntry<float> configFallTimeInstaKill;

		private ConfigEntry<float> configFallTimeDamageExponent;

		private readonly Harmony harmony = new Harmony("lokishorsechild.BetterFallDamage");

		private void Awake()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Expected O, but got Unknown
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Expected O, but got Unknown
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Expected O, but got Unknown
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f6: Expected O, but got Unknown
			//IL_0131: Unknown result type (might be due to invalid IL or missing references)
			//IL_0138: Expected O, but got Unknown
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_017a: Expected O, but got Unknown
			BFDLogger = Logger.CreateLogSource("BetterFallDamage");
			ConfigDescription val = new ConfigDescription("Whether or not to use the host's fall damage settings.", (AcceptableValueBase)null, Array.Empty<object>());
			configDisableServerSideConfig = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DisableServerSideConfig", false, val);
			ConfigDescription val2 = new ConfigDescription("Whether or not fall damage is applied at all.", (AcceptableValueBase)null, Array.Empty<object>());
			configDisableFallDamage = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "DisableFallDamage", false, val2);
			ConfigDescription val3 = new ConfigDescription("Whether or not carry weight will affect fall-damage.", (AcceptableValueBase)null, Array.Empty<object>());
			configEnableWeightScaling = ((BaseUnityPlugin)this).Config.Bind<bool>("WeightScaling", "EnableWeightScaling", true, val3);
			ConfigDescription val4 = new ConfigDescription("The weight at which the player has half the amount of falltime.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 999), Array.Empty<object>());
			configWeightScalingFactor = ((BaseUnityPlugin)this).Config.Bind<int>("WeightScaling", "WeightScalingFactor", 160, val4);
			ConfigDescription val5 = new ConfigDescription("How long (in seconds) a player can spend in the air before taking any fall-damage.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.001f, 999f), Array.Empty<object>());
			configFallTimeDamage = ((BaseUnityPlugin)this).Config.Bind<float>("FallTime", "FallTimeDamage", 0.66f, val5);
			ConfigDescription val6 = new ConfigDescription("How long (in seconds) a player can spend in the air before immediately dying when they hit the ground.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.001f, 999f), Array.Empty<object>());
			configFallTimeInstaKill = ((BaseUnityPlugin)this).Config.Bind<float>("FallTime", "FallTimeInstaKill", 1.2f, val6);
			ConfigDescription val7 = new ConfigDescription("Affects the damage ramp-up between FallTimeDamage and FallTimeInstaKill.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 25.5f), Array.Empty<object>());
			configFallTimeDamageExponent = ((BaseUnityPlugin)this).Config.Bind<float>("FallTime", "FallTimeDamageExponent", 1.7f, val7);
			disableServerSideConfig = configDisableServerSideConfig.Value;
			disableFallDamage = configDisableFallDamage.Value;
			enableWeightScaling = configEnableWeightScaling.Value;
			weightScalingFactor = configWeightScalingFactor.Value;
			scaledWeightScalingFactor = (float)configWeightScalingFactor.Value * 0.0095f;
			scaledFallTimeDamage = configFallTimeDamage.Value * 38f;
			scaledFallTimeInstaKill = configFallTimeInstaKill.Value * 38f;
			fallTimeDamageExponent = configFallTimeDamageExponent.Value;
			harmony.PatchAll(typeof(BFDDamageCalcPatch));
			harmony.PatchAll(typeof(BFDLobbyJoinPatch));
			BFDLogger.LogInfo((object)"Plugin BetterFallDamage loaded");
			BFDLogger = ((BaseUnityPlugin)this).Logger;
		}
	}
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "BetterFallDamage";

		public const string PLUGIN_NAME = "BetterFallDamage";

		public const string PLUGIN_VERSION = "1.1.0";
	}
}
namespace BetterFallDamage.Patches
{
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class BFDDamageCalcPatch
	{
		[HarmonyPatch("PlayerHitGroundEffects")]
		[HarmonyPrefix]
		private static bool Prefix(PlayerControllerB __instance)
		{
			//IL_01e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
			__instance.GetCurrentMaterialStandingOn();
			if (__instance.fallValue < -9f)
			{
				if (__instance.fallValue < -16f)
				{
					__instance.movementAudio.PlayOneShot(StartOfRound.Instance.playerHitGroundHard, 1f);
					WalkieTalkie.TransmitOneShotAudio(__instance.movementAudio, StartOfRound.Instance.playerHitGroundHard, 1f);
				}
				else if (__instance.fallValue < -2f)
				{
					__instance.movementAudio.PlayOneShot(StartOfRound.Instance.playerHitGroundSoft, 1f);
				}
				__instance.LandFromJumpServerRpc(__instance.fallValue < -16f);
			}
			if (!BetterFallDamageClass.disableFallDamage && Mathf.Abs(__instance.fallValueUncapped) > BetterFallDamageClass.scaledFallTimeDamage && !__instance.jetpackControls && !__instance.disablingJetpackControls && !__instance.isSpeedCheating)
			{
				int num6;
				if (BetterFallDamageClass.enableWeightScaling)
				{
					float num = (__instance.carryWeight - 1f) / BetterFallDamageClass.scaledWeightScalingFactor + 1f;
					float num2 = BetterFallDamageClass.scaledFallTimeDamage / num;
					float num3 = BetterFallDamageClass.scaledFallTimeInstaKill / num;
					float num4 = num3 - num2;
					float num5 = (Mathf.Abs(__instance.fallValueUncapped) - num2) / num4;
					num6 = Mathf.FloorToInt(Mathf.Pow(num5, BetterFallDamageClass.fallTimeDamageExponent) * 100f);
				}
				else
				{
					float num7 = BetterFallDamageClass.scaledFallTimeInstaKill - BetterFallDamageClass.scaledFallTimeDamage;
					float num8 = (Mathf.Abs(__instance.fallValueUncapped) - BetterFallDamageClass.scaledFallTimeDamage) / num7;
					num6 = Mathf.FloorToInt(Mathf.Pow(num8, BetterFallDamageClass.fallTimeDamageExponent) * 100f);
				}
				if (num6 > 0)
				{
					__instance.DamagePlayer(Mathf.Min(num6, 100), true, true, (CauseOfDeath)2, 0, true, default(Vector3));
				}
			}
			if (__instance.fallValue < -16f)
			{
				RoundManager.Instance.PlayAudibleNoise(((Component)__instance).transform.position, 7f, 0.5f, 0, false, 0);
			}
			return false;
		}
	}
	[HarmonyPatch]
	internal class BFDLobbyJoinPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch(typeof(StartOfRound), "OnClientConnect")]
		private static void SendConfigToClient(ulong clientId, StartOfRound __instance)
		{
			//IL_010c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_014b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0158: Unknown result type (might be due to invalid IL or missing references)
			//IL_0165: Unknown result type (might be due to invalid IL or missing references)
			//IL_0172: Unknown result type (might be due to invalid IL or missing references)
			//IL_017f: Unknown result type (might be due to invalid IL or missing references)
			//IL_018c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0199: Unknown result type (might be due to invalid IL or missing references)
			//IL_01c6: Unknown result type (might be due to invalid IL or missing references)
			//IL_01dd: Unknown result type (might be due to invalid IL or missing references)
			MemberInfo[] member = typeof(StartOfRound).GetMember("__rpc_exec_stage", BindingFlags.Instance | BindingFlags.NonPublic);
			if (member.Length < 1)
			{
				BetterFallDamageClass.BFDLogger.LogError((object)"Couldn't find __rpc_exec_stage instance");
				return;
			}
			object value = ((FieldInfo)member[0]).GetValue(__instance);
			if (value != null)
			{
				int num = (int)(object)(value as IConvertible);
				if (!((NetworkBehaviour)__instance).IsServer)
				{
					return;
				}
				try
				{
					BetterFallDamageClass.BFDLogger.LogInfo((object)$"Attempting to send config data to client {clientId}");
					NetworkManager networkManager = ((NetworkBehaviour)__instance).NetworkManager;
					if (networkManager != null && networkManager.IsListening && num != 2 && (networkManager.IsServer || networkManager.IsHost))
					{
						MethodInfo method = typeof(StartOfRound).GetMethod("__beginSendClientRpc", BindingFlags.Instance | BindingFlags.NonPublic);
						ClientRpcParams val = default(ClientRpcParams);
						object[] parameters = new object[3]
						{
							3816480838u,
							val,
							(object)(RpcDelivery)0
						};
						FastBufferWriter val2 = (FastBufferWriter)method.Invoke(__instance, parameters);
						BytePacker.WriteValuePacked(val2, BetterFallDamageClass.disableFallDamage);
						BytePacker.WriteValuePacked(val2, BetterFallDamageClass.enableWeightScaling);
						BytePacker.WriteValuePacked(val2, BetterFallDamageClass.weightScalingFactor);
						BytePacker.WriteValuePacked(val2, BetterFallDamageClass.scaledWeightScalingFactor);
						BytePacker.WriteValuePacked(val2, BetterFallDamageClass.scaledFallTimeDamage);
						BytePacker.WriteValuePacked(val2, BetterFallDamageClass.scaledFallTimeInstaKill);
						BytePacker.WriteValuePacked(val2, BetterFallDamageClass.fallTimeDamageExponent);
						MethodInfo method2 = typeof(StartOfRound).GetMethod("__endSendClientRpc", BindingFlags.Instance | BindingFlags.NonPublic);
						object[] parameters2 = new object[4]
						{
							val2,
							3816480838u,
							val,
							(object)(RpcDelivery)0
						};
						method2.Invoke(__instance, parameters2);
						BetterFallDamageClass.BFDLogger.LogInfo((object)" -- Packet send successful");
					}
					return;
				}
				catch
				{
					BetterFallDamageClass.BFDLogger.LogError((object)$" -- Error sending config data to client {clientId}");
					return;
				}
			}
			BetterFallDamageClass.BFDLogger.LogError((object)"Couldn't acquire __rpc_exec_stage info");
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(StartOfRound), "InitializeRPCS_StartOfRound")]
		private static void InitRPCsPatch()
		{
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: Expected O, but got Unknown
			BetterFallDamageClass.BFDLogger.LogInfo((object)$"Adding RPC {3816480838u} to StartOfRound RPC Table");
			NetworkManager.__rpc_func_table.Add(3816480838u, new RpcReceiveHandler(__rpc_handler_3816480838));
		}

		private static void __rpc_handler_3816480838(NetworkBehaviour target, FastBufferReader reader, __RpcParams rpcParams)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			if (BetterFallDamageClass.disableServerSideConfig)
			{
				BetterFallDamageClass.BFDLogger.LogInfo((object)"DisableServerSideConfig set to True - Skipping update from host");
				return;
			}
			NetworkManager networkManager = target.NetworkManager;
			if (networkManager != null && networkManager.IsListening)
			{
				bool flag = default(bool);
				ByteUnpacker.ReadValuePacked(reader, ref flag);
				bool flag2 = default(bool);
				ByteUnpacker.ReadValuePacked(reader, ref flag2);
				int num = default(int);
				ByteUnpacker.ReadValuePacked(reader, ref num);
				float scaledWeightScalingFactor = default(float);
				ByteUnpacker.ReadValuePacked(reader, ref scaledWeightScalingFactor);
				float num2 = default(float);
				ByteUnpacker.ReadValuePacked(reader, ref num2);
				float num3 = default(float);
				ByteUnpacker.ReadValuePacked(reader, ref num3);
				float num4 = default(float);
				ByteUnpacker.ReadValuePacked(reader, ref num4);
				BetterFallDamageClass.disableFallDamage = flag;
				BetterFallDamageClass.enableWeightScaling = flag2;
				BetterFallDamageClass.weightScalingFactor = num;
				BetterFallDamageClass.scaledWeightScalingFactor = scaledWeightScalingFactor;
				BetterFallDamageClass.scaledFallTimeDamage = num2;
				BetterFallDamageClass.scaledFallTimeInstaKill = num3;
				BetterFallDamageClass.fallTimeDamageExponent = num4;
				BetterFallDamageClass.BFDLogger.LogInfo((object)"Data received from host:");
				BetterFallDamageClass.BFDLogger.LogInfo((object)$" -- DisableFallDamage: {flag}");
				BetterFallDamageClass.BFDLogger.LogInfo((object)$" -- EnableWeightScaling: {flag2}");
				BetterFallDamageClass.BFDLogger.LogInfo((object)$" -- WeightScalingFactor: {num}");
				BetterFallDamageClass.BFDLogger.LogInfo((object)$" -- FallTimeDamage: {num2 / 38f}");
				BetterFallDamageClass.BFDLogger.LogInfo((object)$" -- FallTimeInstaKill: {num3 / 38f}");
				BetterFallDamageClass.BFDLogger.LogInfo((object)$" -- FallTimeDamageExponent: {num4}");
			}
		}
	}
}