Decompiled source of JetpackHandling v1.0.5

JetpackHandling.dll

Decompiled a month ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using JetpackHandling.Patches;
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: AssemblyTitle("JetpackHandling")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("JetpackHandling")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7cd580c7-049b-47dd-ae19-29c989a65cf4")]
[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 JetpackHandling
{
	[BepInPlugin("Invertigo.JetpackHandling", "JetpackHandling", "1.0.5")]
	public class Plugin : BaseUnityPlugin
	{
		public const string ModGUID = "Invertigo.JetpackHandling";

		public const string ModName = "JetpackHandling";

		public const string ModVersion = "1.0.5";

		private readonly Harmony harmony = new Harmony("Invertigo.JetpackHandling");

		private static Plugin Instance;

		private void Awake()
		{
			ManualLogSource val = Logger.CreateLogSource("Invertigo.JetpackHandling");
			val.LogInfo((object)"Plugin Invertigo.JetpackHandling is loaded!");
			if ((Object)(object)Instance == (Object)null)
			{
				Instance = this;
			}
			harmony.PatchAll(typeof(PlayerControllerBPatch));
			harmony.PatchAll(typeof(JetpackDamagePatch));
			harmony.PatchAll(typeof(JetpackKillPatch));
			harmony.PatchAll(typeof(JetpackItemPatch));
			harmony.PatchAll(typeof(NetworkPrefabPatch));
		}
	}
	[HarmonyPatch(typeof(NetworkManager))]
	internal static class NetworkPrefabPatch
	{
		[HarmonyPostfix]
		[HarmonyPatch("SetSingleton")]
		private static void RegisterPrefab()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("Invertigo.JetpackHandling Prefab");
			((Object)val).hideFlags = (HideFlags)(((Object)val).hideFlags | 0x3D);
			Object.DontDestroyOnLoad((Object)(object)val);
			NetworkObject obj = val.AddComponent<NetworkObject>();
			FieldInfo field = typeof(NetworkObject).GetField("GlobalObjectIdHash", BindingFlags.Instance | BindingFlags.NonPublic);
			if (field != null)
			{
				field.SetValue(obj, GetHash("Invertigo.JetpackHandling"));
			}
			NetworkManager.Singleton.PrefabHandler.AddNetworkPrefab(val);
		}

		private static uint GetHash(string value)
		{
			return value?.Aggregate(17u, (uint current, char c) => (current * 31) ^ c) ?? 0;
		}
	}
}
namespace JetpackHandling.Patches
{
	[HarmonyPatch(typeof(JetpackItem), "Update")]
	internal class JetpackItemPatch
	{
		public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
		{
			return instructions;
		}
	}
	[HarmonyPatch(typeof(JetpackItem), "ExplodeJetpackServerRpc")]
	internal class JetpackExplosionPatch
	{
		[HarmonyPrefix]
		private static bool PreventExplosion()
		{
			return false;
		}
	}
	[HarmonyPatch(typeof(JetpackItem), "Update")]
	internal class JetpackBatteryPatch
	{
		private static bool batteryModified = false;

		private static readonly FieldInfo JetpackDeaccellerationField = typeof(JetpackItem).GetField("jetpackDeaccelleration", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

		[HarmonyPostfix]
		private static void JetpackUpdate(JetpackItem __instance)
		{
			if (JetpackDeaccellerationField != null)
			{
				JetpackDeaccellerationField.SetValue(__instance, 15f);
			}
			if (!batteryModified && (Object)(object)((GrabbableObject)__instance).itemProperties != (Object)null)
			{
				Item itemProperties = ((GrabbableObject)__instance).itemProperties;
				itemProperties.batteryUsage *= 5f;
				batteryModified = true;
			}
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB))]
	internal class PlayerControllerBPatch
	{
		private static readonly FieldInfo IsJumpingField = typeof(PlayerControllerB).GetField("isJumping", BindingFlags.Instance | BindingFlags.NonPublic);

		private static readonly FieldInfo IsFallingFromJumpField = typeof(PlayerControllerB).GetField("isFallingFromJump", BindingFlags.Instance | BindingFlags.NonPublic);

		[HarmonyPatch("Update")]
		[HarmonyPostfix]
		private static void JetpackHandlingPatch(PlayerControllerB __instance)
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			//IL_0205: Unknown result type (might be due to invalid IL or missing references)
			//IL_020f: Unknown result type (might be due to invalid IL or missing references)
			ManualLogSource val = new ManualLogSource("jetpackhandlingsource");
			Logger.Sources.Add((ILogSource)(object)val);
			float num = 0f;
			bool flag = false;
			bool flag2 = true;
			bool flag3 = false;
			if ((!__instance.inSpecialInteractAnimation || __instance.inShockingMinigame) && (Object)(object)__instance.currentlyHeldObjectServer != (Object)null && __instance.currentlyHeldObjectServer.itemProperties.itemName == "Jetpack")
			{
				if (__instance.jetpackControls && !__instance.disablingJetpackControls)
				{
					flag3 = true;
					flag2 = false;
				}
				else
				{
					flag = true;
					flag2 = false;
				}
			}
			__instance.takingFallDamage = false;
			if (flag)
			{
				__instance.fallValue += 38f * Time.deltaTime;
				float num2 = Mathf.Lerp(0.15f, 1f, Mathf.Abs(__instance.fallValue) / 18f);
				__instance.fallValue = Mathf.MoveTowards(__instance.fallValue, -18f, 10f * Time.deltaTime * num2);
				__instance.fallValueUncapped = 0f;
			}
			else if (flag2)
			{
				if ((double)__instance.health > 25.0 && (double)__instance.fallValue < -30.0 && __instance.thisController.isGrounded)
				{
					val.LogInfo((object)"taking damage from fall");
					val.LogInfo((object)$"fallValue = {__instance.fallValue}");
					val.LogInfo((object)$"fallValueUncapped = {__instance.fallValueUncapped}");
					val.LogInfo((object)$"__instance.inSpecialInteractAnimation = {__instance.inSpecialInteractAnimation}");
					val.LogInfo((object)$"isJumping = {IsJumpingField.GetValue(__instance)}");
					val.LogInfo((object)$"isFallingFromJump = {IsFallingFromJumpField.GetValue(__instance)}");
					__instance.DamagePlayer(Mathf.Clamp((int)__instance.fallValue, 20, 100), true, true, (CauseOfDeath)2, 0, true, Vector3.ClampMagnitude(__instance.velocityLastFrame, 50f));
				}
				if (__instance.thisController.isGrounded || __instance.isClimbingLadder)
				{
					__instance.fallValue = -7f;
					__instance.fallValueUncapped = -7f;
				}
			}
			else if (flag3)
			{
				__instance.fallValue = Mathf.MoveTowards(__instance.fallValue, -1f, 7f * Time.deltaTime + num);
				__instance.fallValueUncapped = 0f;
			}
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB), "DamagePlayer")]
	internal class JetpackDamagePatch
	{
		[HarmonyPrefix]
		private static bool PreventDamage(PlayerControllerB __instance, CauseOfDeath causeOfDeath, bool fallDamage)
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Invalid comparison between Unknown and I4
			if (__instance.jetpackControls || __instance.disablingJetpackControls)
			{
				return false;
			}
			if ((int)causeOfDeath == 2 && fallDamage && (Object)(object)__instance.currentlyHeldObjectServer != (Object)null && __instance.currentlyHeldObjectServer.itemProperties.itemName == "Jetpack")
			{
				return false;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(PlayerControllerB), "KillPlayer")]
	internal class JetpackKillPatch
	{
		[HarmonyPrefix]
		private static bool PreventDeath(PlayerControllerB __instance)
		{
			if (__instance.jetpackControls || __instance.disablingJetpackControls)
			{
				return false;
			}
			return true;
		}
	}
}