Decompiled source of CreativeFlight v3.0.0

plugins/CreativeFlight/CreativeFlight.dll

Decompiled 2 weeks ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using CreativeFlight.Patches;
using EquinoxsDebuggingTools;
using EquinoxsModUtils;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("CreativeFlight")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CreativeFlight")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("a80e7664-9d15-4b04-8455-6ea1c6f5595b")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace CreativeFlight
{
	public static class Jetpack
	{
		public static bool isFlying;

		public static void StartFlight()
		{
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			isFlying = true;
			EMU.SetPrivateField<PlayerFirstPersonController>("m_VerticalSpeed", Player.instance.fpcontroller, (object)0f);
			EMU.SetPrivateField<Stilts>("_stiltHeight", Player.instance.equipment.hoverPack, (object)0f);
			Player.instance.fpcontroller.m_Rigidbody.velocity = Vector3.zero;
			Player.instance.fpcontroller.maxWalkSpeed = 10f;
			Player.instance.fpcontroller.maxRunSpeed = 16f;
			EDT.Log("Status", "Started Flying");
		}

		public static void StopFlight()
		{
			isFlying = false;
			Player.instance.fpcontroller.maxWalkSpeed = 5f;
			Player.instance.fpcontroller.maxRunSpeed = 8f;
			EDT.Log("Status", "Stopped Flying");
		}
	}
	[BepInPlugin("com.equinox.CreativeFlight", "CreativeFlight", "3.0.0")]
	public class CreativeFlightPlugin : BaseUnityPlugin
	{
		private const string MyGUID = "com.equinox.CreativeFlight";

		private const string PluginName = "CreativeFlight";

		private const string VersionString = "3.0.0";

		private static readonly Harmony Harmony = new Harmony("com.equinox.CreativeFlight");

		public static ManualLogSource Log = new ManualLogSource("CreativeFlight");

		public static ConfigEntry<int> DoubleTapThreshold;

		public static ConfigEntry<KeyCode> DescendKey;

		public static ConfigEntry<bool> LandToEndFlight;

		public static ConfigEntry<float> HorizontalThrust;

		public static ConfigEntry<float> VerticalThrust;

		public static ConfigEntry<float> Friction;

		public static ConfigEntry<int> SprintSpeed;

		public static bool isEnabled = true;

		public static float sSinceLastAscendPress;

		public static float sSinceLastDescendPress;

		private static float doubleTapThresholdSeconds => 0.001f * (float)DoubleTapThreshold.Value;

		private void Awake()
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: CreativeFlight, VersionString: 3.0.0 is loading...");
			Harmony.PatchAll();
			CreateConfigEntries();
			ApplyPatches();
			((BaseUnityPlugin)this).Logger.LogInfo((object)"PluginName: CreativeFlight, VersionString: 3.0.0 is loaded.");
			Log = ((BaseUnityPlugin)this).Logger;
		}

		private void Update()
		{
			if (LoadingStates.hasGameLoaded)
			{
				sSinceLastAscendPress += Time.deltaTime;
				sSinceLastDescendPress += Time.deltaTime;
				StartStopIfDoubleTap();
				if (LandToEndFlight.Value && Jetpack.isFlying && CheckIfNearGround())
				{
					Jetpack.StopFlight();
				}
				if (Jetpack.isFlying && Player.instance.equipment.hoverPack.active)
				{
					Player.instance.equipment.hoverPack.DeactivateStilts();
				}
			}
		}

		private void CreateConfigEntries()
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			//IL_0057: Unknown result type (might be due to invalid IL or missing references)
			//IL_0061: Expected O, but got Unknown
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Expected O, but got Unknown
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Expected O, but got Unknown
			//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Expected O, but got Unknown
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Expected O, but got Unknown
			//IL_0173: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: Expected O, but got Unknown
			DoubleTapThreshold = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Double Tap Threshold", 300, new ConfigDescription("The time interval in milliseconds during which two key presses are registered as a double tap", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 500), Array.Empty<object>()));
			DescendKey = ((BaseUnityPlugin)this).Config.Bind<KeyCode>("General", "Descend Key", (KeyCode)99, new ConfigDescription("The key to press to descend or double tap to end flight. Left Control is not recommended if you are using Blueprints.", (AcceptableValueBase)null, Array.Empty<object>()));
			LandToEndFlight = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Land To End Flight", false, new ConfigDescription("When enabled, touching the ground will end flight.", (AcceptableValueBase)null, Array.Empty<object>()));
			HorizontalThrust = ((BaseUnityPlugin)this).Config.Bind<float>("Forces", "Horizontal Thrust", 10f, new ConfigDescription("Controls horizontal acceleration while flying", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, float.MaxValue), Array.Empty<object>()));
			VerticalThrust = ((BaseUnityPlugin)this).Config.Bind<float>("Forces", "Vertical Thrust", 150f, new ConfigDescription("Controls vertical acceleration while flying", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, float.MaxValue), Array.Empty<object>()));
			Friction = ((BaseUnityPlugin)this).Config.Bind<float>("Forces", "Friction", 0.15f, new ConfigDescription("Controls how quickly you slow to a stop while flying", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.01f, 0.9f), Array.Empty<object>()));
			SprintSpeed = ((BaseUnityPlugin)this).Config.Bind<int>("Forces", "Sprint Speed", 150, new ConfigDescription("Sprint speed as a percentage of walking speed. E.g. default '150' means 50% faster than walking.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(100, 200), Array.Empty<object>()));
		}

		private void ApplyPatches()
		{
			Harmony.CreateAndPatchAll(typeof(PlayerFirstPersonControllerPatch), (string)null);
			Harmony.CreateAndPatchAll(typeof(StiltsPatch), (string)null);
		}

		private void StartStopIfDoubleTap()
		{
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			if (UnityInput.Current.GetKeyDown((KeyCode)32))
			{
				if (sSinceLastAscendPress < doubleTapThresholdSeconds)
				{
					if (!Jetpack.isFlying)
					{
						Jetpack.StartFlight();
					}
					else
					{
						Jetpack.StopFlight();
					}
				}
				else if (sSinceLastAscendPress < 1f)
				{
					EDT.Log("Controls", $"Double space tap did not meet threshold | {sSinceLastAscendPress} > {doubleTapThresholdSeconds}");
				}
				sSinceLastAscendPress = 0f;
			}
			else if (UnityInput.Current.GetKeyDown(DescendKey.Value) && !UnityInput.Current.GetKeyDown((KeyCode)306))
			{
				if (sSinceLastDescendPress < doubleTapThresholdSeconds && Jetpack.isFlying)
				{
					Jetpack.StopFlight();
				}
				else if (sSinceLastDescendPress < 1f && sSinceLastDescendPress > doubleTapThresholdSeconds)
				{
					EDT.Log("Controls", $"Double {DescendKey.Value} tap did not meet threshold | {sSinceLastDescendPress} > {doubleTapThresholdSeconds}");
				}
				sSinceLastDescendPress = 0f;
			}
		}

		private bool CheckIfNearGround()
		{
			//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_0023: Unknown result type (might be due to invalid IL or missing references)
			//IL_0028: Unknown result type (might be due to invalid IL or missing references)
			//IL_0034: 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_0056: Unknown result type (might be due to invalid IL or missing references)
			PlayerFirstPersonController fpcontroller = Player.instance.fpcontroller;
			LayerMask val = (LayerMask)EMU.GetPrivateField<PlayerFirstPersonController>("groundLayer", fpcontroller);
			RaycastHit val2 = default(RaycastHit);
			if (Physics.Raycast(((Component)fpcontroller).transform.position, Vector3.down, ref val2, 2.2f, LayerMask.op_Implicit(val)))
			{
				float num = ((Component)fpcontroller).transform.position.y - ((RaycastHit)(ref val2)).point.y;
				EDT.PacedLog("Landing", $"Raycast hit, user is {num} away from ground", 1f);
				return num < 0.2f;
			}
			EDT.PacedLog("Landing", "Raycast did not hit", 1f);
			return false;
		}
	}
}
namespace CreativeFlight.Patches
{
	internal class PlayerFirstPersonControllerPatch
	{
		private static bool ascend => UnityInput.Current.GetKey((KeyCode)32) && !UnityInput.Current.GetKey((KeyCode)306);

		private static bool descend => UnityInput.Current.GetKey(CreativeFlightPlugin.DescendKey.Value) && !UnityInput.Current.GetKey((KeyCode)306);

		private static float hThrust => CreativeFlightPlugin.HorizontalThrust.Value;

		private static float vThrust => CreativeFlightPlugin.VerticalThrust.Value;

		private static float friction => CreativeFlightPlugin.Friction.Value;

		[HarmonyPatch(typeof(PlayerFirstPersonController), "CalculateHorizontalMovement")]
		[HarmonyPrefix]
		private static bool RecalcHorizontalMovement(PlayerFirstPersonController __instance)
		{
			//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_0124: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_012f: Unknown result type (might be due to invalid IL or missing references)
			//IL_013a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_0145: 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_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_0160: Unknown result type (might be due to invalid IL or missing references)
			if (ShouldUseDefault(__instance))
			{
				return true;
			}
			float num = (float)EMU.GetPrivateField<PlayerFirstPersonController>("m_HorizontalSpeed", Player.instance.fpcontroller);
			num *= 1f - friction;
			if (UnityInput.Current.GetKey((KeyCode)119) || UnityInput.Current.GetKey((KeyCode)115) || UnityInput.Current.GetKey((KeyCode)97) || UnityInput.Current.GetKey((KeyCode)100))
			{
				num += hThrust;
			}
			if (UnityInput.Current.GetKey((KeyCode)304))
			{
				float num2 = num;
				num *= (float)CreativeFlightPlugin.SprintSpeed.Value / 100f;
				EDT.PacedLog("Controls", $"User is pressing sprint > oldSpeed: {num2} , newSpeed: {num}", 1f);
			}
			else
			{
				EDT.PacedLog("Controls", "User is not pressing sprint", 1f);
			}
			Vector3 forward = ((Component)__instance.cam).transform.forward;
			forward.y = 0f;
			((Vector3)(ref forward)).Normalize();
			Vector2 moveAxes = InputHandler.instance.MoveAxes;
			Vector3 val = Quaternion.LookRotation(forward, Vector3.up) * new Vector3(moveAxes.x, 0f, moveAxes.y) * num;
			EMU.SetPrivateField<PlayerFirstPersonController>("m_DesiredHorizontalVelocity", Player.instance.fpcontroller, (object)val);
			return false;
		}

		[HarmonyPatch(typeof(PlayerFirstPersonController), "CalculateVerticalMovement")]
		[HarmonyPrefix]
		private static bool RecalcVerticalMovement(PlayerFirstPersonController __instance)
		{
			if (ShouldUseDefault(__instance))
			{
				return true;
			}
			EMU.SetPrivateField<Stilts>("_active", Player.instance.equipment.hoverPack, (object)false);
			float num = (float)EMU.GetPrivateField<PlayerFirstPersonController>("m_VerticalSpeed", Player.instance.fpcontroller);
			num *= 1f - friction;
			if (ascend)
			{
				num += vThrust * Time.deltaTime;
			}
			if (descend)
			{
				num -= vThrust * Time.deltaTime;
			}
			EMU.SetPrivateField<PlayerFirstPersonController>("m_VerticalSpeed", Player.instance.fpcontroller, (object)num);
			return false;
		}

		private static bool ShouldUseDefault(PlayerFirstPersonController __instance)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Invalid comparison between Unknown and I4
			if (!CreativeFlightPlugin.isEnabled)
			{
				return true;
			}
			ControlState val = (ControlState)EMU.GetPrivateField<PlayerFirstPersonController>("curControls", __instance);
			if ((int)val == 3)
			{
				return true;
			}
			if (!Jetpack.isFlying)
			{
				return true;
			}
			return false;
		}

		[HarmonyPatch(typeof(PlayerFirstPersonController), "AutoCrouch")]
		[HarmonyPrefix]
		private static bool BlockAutoCrouch()
		{
			if (ShouldUseDefault(Player.instance.fpcontroller))
			{
				return true;
			}
			return false;
		}

		[HarmonyPatch(typeof(PlayerFirstPersonController), "UpdateHoverPackStatus")]
		[HarmonyPrefix]
		public static bool BlockUpdateHoverPackStatus(PlayerFirstPersonController __instance)
		{
			if (ShouldUseDefault(Player.instance.fpcontroller))
			{
				return true;
			}
			return false;
		}

		[HarmonyPatch(typeof(PlayerFirstPersonController), "DeactivateHoverPack")]
		[HarmonyPrefix]
		public static bool BlockDeactivateHoverpack()
		{
			if (ShouldUseDefault(Player.instance.fpcontroller))
			{
				return true;
			}
			return false;
		}
	}
	internal class StiltsPatch
	{
		[HarmonyPatch(typeof(Stilts), "ActivateStilts")]
		[HarmonyPrefix]
		private static bool BlockActivateStilts()
		{
			Jetpack.StartFlight();
			return false;
		}
	}
}