Decompiled source of EntranceFallFix v0.1.0

EntranceFallFix.dll

Decompiled 2 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using ModMenu;
using UnityEngine;
using UnityEngine.AI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("EntranceFallFix")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.1.0.0")]
[assembly: AssemblyInformationalVersion("0.1.0+d0bc68fb3c5e363b354eb236ae87a643c00139e3")]
[assembly: AssemblyProduct("EntranceFallFix")]
[assembly: AssemblyTitle("EntranceFallFix")]
[assembly: AssemblyVersion("0.1.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 EntranceFallFix
{
	[HarmonyPatch(typeof(EntranceTeleport), "FindExitPoint")]
	internal static class ExitPick
	{
		private static readonly List<EntranceTeleport> Matches = new List<EntranceTeleport>();

		private static readonly HashSet<int> Reported = new HashSet<int>();

		[HarmonyPrefix]
		private static bool Prefix(EntranceTeleport __instance, ref bool __result)
		{
			if (!Plugin.ExitCheck.Value)
			{
				return true;
			}
			Matches.Clear();
			EntranceTeleport[] array = Object.FindObjectsOfType<EntranceTeleport>();
			foreach (EntranceTeleport val in array)
			{
				if (val.isEntranceToBuilding != __instance.isEntranceToBuilding && val.entranceId == __instance.entranceId)
				{
					Matches.Add(val);
				}
			}
			if (Matches.Count == 1)
			{
				__instance.exitScript = Matches[0];
			}
			else if (Matches.Count > 1)
			{
				__instance.exitScript = Choose(__instance);
			}
			__result = (Object)(object)__instance.exitScript != (Object)null;
			return false;
		}

		private static EntranceTeleport Choose(EntranceTeleport door)
		{
			//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0163: Unknown result type (might be due to invalid IL or missing references)
			EntranceTeleport val = Matches[Matches.Count - 1];
			EntranceTeleport val2 = (Good(val) ? val : null);
			if ((Object)(object)val2 == (Object)null)
			{
				foreach (EntranceTeleport match in Matches)
				{
					if (Good(match))
					{
						val2 = match;
						break;
					}
				}
			}
			if ((Object)(object)val2 == (Object)null)
			{
				val2 = val;
			}
			if (Reported.Add(((Object)door).GetInstanceID()))
			{
				List<string> list = new List<string>();
				foreach (EntranceTeleport match2 in Matches)
				{
					list.Add(string.Format("{0} at {1:F1}{2}", ((Object)match2).name, Point(match2), Good(match2) ? "" : " (no floor)"));
				}
				Plugin.Log.LogWarning((object)(string.Format("{0} (id {1}, {2}) has {3} matching exits: ", ((Object)door).name, door.entranceId, door.isEntranceToBuilding ? "outside" : "inside", Matches.Count) + string.Join("; ", list) + string.Format(". Using {0} at {1:F1}{2}.", ((Object)val2).name, Point(val2), ((Object)(object)val2 == (Object)(object)val) ? " (vanilla's pick)" : " instead of vanilla's pick")));
			}
			return val2;
		}

		private static Vector3 Point(EntranceTeleport t)
		{
			//IL_0020: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)t.entrancePoint != (Object)null))
			{
				return ((Component)t).transform.position;
			}
			return t.entrancePoint.position;
		}

		private static bool Good(EntranceTeleport t)
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)t != (Object)null && (Object)(object)t.entrancePoint != (Object)null)
			{
				return Plugin.GoodArrival(t.entrancePoint.position);
			}
			return false;
		}

		[HarmonyPatch(typeof(RoundManager), "FinishGeneratingLevel")]
		[HarmonyPostfix]
		private static void NewRound()
		{
			Reported.Clear();
		}
	}
	internal static class MenuHooks
	{
		internal const string ModMenuGuid = "daniel.lethalcompany.modmenu";

		private const string Tab = "Entrance Fall Fix";

		[MethodImpl(MethodImplOptions.NoInlining)]
		internal static void Register()
		{
			MenuApi.Toggle("Entrance Fall Fix", "Catch falls after a door", (Func<bool>)(() => Plugin.Rescue.Value), (Func<bool, string>)delegate(bool on)
			{
				Plugin.Rescue.Value = on;
				return (!on) ? "fall rescue off" : "fall rescue on: you're put back on the floor if you drop through after a door";
			}, false, false, (Func<string>)null, "only you; General > Fall rescue in the config");
			MenuApi.Toggle("Entrance Fall Fix", "Use the exit with a floor", (Func<bool>)(() => Plugin.ExitCheck.Value), (Func<bool, string>)delegate(bool on)
			{
				Plugin.ExitCheck.Value = on;
				return (!on) ? "exit check off: doors pick exits the vanilla way" : "when an interior has two matching exits, doors use the one with floor under it";
			}, false, false, (Func<string>)null, "only you; General > Exit check in the config");
		}
	}
	[BepInPlugin("daniel.lethalcompany.entrancefallfix", "EntranceFallFix", "0.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class Plugin : BaseUnityPlugin
	{
		internal const string Guid = "daniel.lethalcompany.entrancefallfix";

		internal const string Version = "0.1.0";

		internal static ManualLogSource Log;

		internal static ConfigEntry<bool> Rescue;

		internal static ConfigEntry<bool> ExitCheck;

		internal static int GroundMask
		{
			get
			{
				if (!((Object)(object)StartOfRound.Instance != (Object)null))
				{
					return 1107298561;
				}
				return StartOfRound.Instance.collidersAndRoomMaskAndDefault;
			}
		}

		private void Awake()
		{
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Expected O, but got Unknown
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			Log = ((BaseUnityPlugin)this).Logger;
			Rescue = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Fall rescue", true, "After you go through a door, if you fall through the floor you're put back on solid floor at that door, with no fall damage.");
			ExitCheck = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Exit check", true, "When an interior has more than one exit with the same id, doors use one that has floor under it (vanilla takes whichever comes last).");
			GameObject val = new GameObject("EntranceFallFix");
			Object.DontDestroyOnLoad((Object)val);
			((Object)val).hideFlags = (HideFlags)61;
			Watch.Instance = val.AddComponent<Watch>();
			((Behaviour)Watch.Instance).enabled = false;
			new Harmony("daniel.lethalcompany.entrancefallfix").PatchAll(typeof(Plugin).Assembly);
			if (Chainloader.PluginInfos.ContainsKey("daniel.lethalcompany.modmenu"))
			{
				MenuHooks.Register();
			}
			Log.LogInfo((object)("EntranceFallFix 0.1.0 loaded (fall rescue " + (Rescue.Value ? "on" : "off") + ", exit check " + (ExitCheck.Value ? "on" : "off") + ")."));
		}

		internal static bool FloorAt(Vector3 p, out Vector3 floor)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0063: 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_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_007d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_0088: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			floor = p;
			RaycastHit val = default(RaycastHit);
			if (!Physics.Raycast(p + Vector3.up * 1.5f, Vector3.down, ref val, 4.5f, GroundMask, (QueryTriggerInteraction)1))
			{
				return false;
			}
			if (((RaycastHit)(ref val)).normal.y < 0.6f)
			{
				return false;
			}
			floor = ((RaycastHit)(ref val)).point + Vector3.up * 0.1f;
			return !Physics.CheckCapsule(floor + Vector3.up * 0.45f, floor + Vector3.up * 1.9f, 0.3f, GroundMask, (QueryTriggerInteraction)1);
		}

		internal static bool GoodArrival(Vector3 p)
		{
			//IL_0000: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			if (FloorAt(p, out var floor))
			{
				return floor.y - p.y <= 0.6f;
			}
			return false;
		}
	}
	internal sealed class Watch : MonoBehaviour
	{
		[HarmonyPatch(typeof(PlayerControllerB), "TeleportPlayer")]
		private static class OnTeleport
		{
			[HarmonyPostfix]
			private static void Postfix(PlayerControllerB __instance, Vector3 pos)
			{
				//IL_0066: Unknown result type (might be due to invalid IL or missing references)
				//IL_006b: Unknown result type (might be due to invalid IL or missing references)
				//IL_006c: 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 (!Plugin.Rescue.Value || (Object)(object)Instance == (Object)null || Instance.rescuing)
				{
					return;
				}
				GameNetworkManager instance = GameNetworkManager.Instance;
				if ((Object)(object)instance == (Object)null || (Object)(object)__instance != (Object)(object)instance.localPlayerController)
				{
					return;
				}
				EntranceTeleport[] array = Object.FindObjectsOfType<EntranceTeleport>();
				foreach (EntranceTeleport val in array)
				{
					if ((Object)(object)val.entrancePoint != (Object)null)
					{
						Vector3 val2 = val.entrancePoint.position - pos;
						if (((Vector3)(ref val2)).sqrMagnitude < 0.25f)
						{
							Begin(__instance, val);
							break;
						}
					}
				}
			}
		}

		internal static Watch Instance;

		private const float Seconds = 6f;

		private const float Drop = 3f;

		private const float Sink = 2.5f;

		private const float SinkSide = 2f;

		private const float LeaveDistance = 8f;

		private const float BelowNodes = 10f;

		private const int MaxRescues = 3;

		private PlayerControllerB me;

		private Vector3 arrival;

		private float yaw;

		private float until;

		private float lowest;

		private bool touched;

		private bool inside;

		private bool rescuing;

		private int rescues;

		private int quietFrames;

		internal static void Begin(PlayerControllerB p, EntranceTeleport door)
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)Instance == (Object)null) && Plugin.Rescue.Value && !Instance.rescuing)
			{
				Watch instance = Instance;
				instance.me = p;
				instance.arrival = door.entrancePoint.position;
				instance.yaw = door.entrancePoint.eulerAngles.y;
				instance.until = Time.time + 6f;
				instance.touched = false;
				instance.rescues = 0;
				instance.quietFrames = 0;
				instance.inside = !door.isEntranceToBuilding;
				instance.lowest = LowestNode(instance.inside);
				((Behaviour)instance).enabled = true;
			}
		}

		private void Update()
		{
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0100: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_01a0: Unknown result type (might be due to invalid IL or missing references)
			if (quietFrames > 0)
			{
				Calm();
				quietFrames--;
			}
			if ((Object)(object)me == (Object)null || !Plugin.Rescue.Value || Time.time > until || me.isPlayerDead || !me.isPlayerControlled)
			{
				Stop();
			}
			else
			{
				if (me.jetpackControls || me.isClimbingLadder || me.inSpecialInteractAnimation || me.isUnderwater)
				{
					return;
				}
				Vector3 position = ((Component)me).transform.position;
				Vector3 val = position - arrival;
				val.y = 0f;
				if (((Vector3)(ref val)).magnitude > 8f)
				{
					Stop();
					return;
				}
				if ((Object)(object)me.thisController != (Object)null && me.thisController.isGrounded && position.y > arrival.y - 2.5f)
				{
					touched = true;
				}
				float num = arrival.y - position.y;
				string text = null;
				if (!touched && num > 3f)
				{
					text = $"dropped {num:F1} m without touching ground";
				}
				else if (num > 2.5f && ((Vector3)(ref val)).magnitude < 2f)
				{
					text = $"is {num:F1} m under the door's floor";
				}
				else if (position.y < lowest - 10f)
				{
					text = $"is {lowest - position.y:F1} m under the lowest AI node";
				}
				if (text != null)
				{
					Rescue(text);
				}
			}
		}

		private void Rescue(string why)
		{
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0101: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: Unknown result type (might be due to invalid IL or missing references)
			if (rescues >= 3)
			{
				Plugin.Log.LogWarning((object)$"Fall rescue: player {why} again after {3} tries; giving up.");
				Stop(now: true);
				return;
			}
			rescues++;
			if (!Spot(rescues, out var spot, out var how))
			{
				Plugin.Log.LogWarning((object)$"Fall rescue: player {why}, but no floor was found near {arrival:F1}.");
				Stop(now: true);
				return;
			}
			Plugin.Log.LogWarning((object)$"Fall rescue: local player {why} after a door at {arrival:F1}; putting them on {how} at {spot:F1} (try {rescues}).");
			rescuing = true;
			try
			{
				me.TeleportPlayer(spot, true, yaw, false, true);
			}
			finally
			{
				rescuing = false;
			}
			me.ResetFallGravity();
			quietFrames = 8;
			Calm();
			touched = false;
			arrival = spot;
			until = Time.time + 6f;
		}

		private bool Spot(int attempt, out Vector3 spot, out string how)
		{
			//IL_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			NavMeshHit val = default(NavMeshHit);
			for (int i = attempt; i <= 3; i++)
			{
				if (i == 1 && Plugin.FloorAt(arrival, out spot))
				{
					how = "the floor at the door";
					return true;
				}
				if (i == 2 && NavMesh.SamplePosition(arrival, ref val, 6f, -1) && Plugin.FloorAt(((NavMeshHit)(ref val)).position, out spot))
				{
					how = "the NavMesh by the door";
					return true;
				}
				if (i == 3 && NearestNode(out spot))
				{
					how = "the nearest AI node";
					return true;
				}
			}
			spot = default(Vector3);
			how = null;
			return false;
		}

		private bool NearestNode(out Vector3 spot)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Unknown result type (might be due to invalid IL or missing references)
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			spot = default(Vector3);
			GameObject[] array = Nodes(inside);
			if (array == null)
			{
				return false;
			}
			float num = float.MaxValue;
			GameObject[] array2 = array;
			foreach (GameObject val in array2)
			{
				if (!((Object)(object)val == (Object)null))
				{
					Vector3 val2 = val.transform.position - arrival;
					float sqrMagnitude = ((Vector3)(ref val2)).sqrMagnitude;
					if (sqrMagnitude < num && Plugin.FloorAt(val.transform.position, out var floor))
					{
						num = sqrMagnitude;
						spot = floor;
					}
				}
			}
			return num < float.MaxValue;
		}

		private static GameObject[] Nodes(bool inside)
		{
			RoundManager instance = RoundManager.Instance;
			if ((Object)(object)instance == (Object)null)
			{
				return null;
			}
			if (!inside)
			{
				return instance.outsideAINodes;
			}
			return instance.insideAINodes;
		}

		private static float LowestNode(bool inside)
		{
			//IL_002d: Unknown result type (might be due to invalid IL or missing references)
			float num = float.PositiveInfinity;
			GameObject[] array = Nodes(inside);
			if (array != null)
			{
				GameObject[] array2 = array;
				foreach (GameObject val in array2)
				{
					if ((Object)(object)val != (Object)null)
					{
						num = Mathf.Min(num, val.transform.position.y);
					}
				}
			}
			if (!float.IsPositiveInfinity(num))
			{
				return num;
			}
			return float.NegativeInfinity;
		}

		private void Calm()
		{
			if (!((Object)(object)me == (Object)null))
			{
				me.fallValue = 0f;
				me.fallValueUncapped = 0f;
				me.takingFallDamage = false;
			}
		}

		private void Stop(bool now = false)
		{
			if (quietFrames <= 0 || now || !((Object)(object)me != (Object)null))
			{
				quietFrames = 0;
				((Behaviour)this).enabled = false;
				me = null;
			}
		}
	}
}