Decompiled source of BetterVehicleControls v1.1.3

BepInEx/plugins/BetterVehicleControls/BetterVehicleControls.dll

Decompiled 3 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using LethalCompanyInputUtils.Api;
using Microsoft.CodeAnalysis;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.InputSystem;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: IgnoresAccessChecksTo("")]
[assembly: AssemblyCompany("BetterVehicleControls")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.1.3.0")]
[assembly: AssemblyInformationalVersion("1.1.3+65e090713685603fc4fbafd09de81c5c13f83f83")]
[assembly: AssemblyProduct("BetterVehicleControls")]
[assembly: AssemblyTitle("BetterVehicleControls")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 BetterVehicleControls
{
	[BepInPlugin("Dev1A3.BetterVehicleControls", "BetterVehicleControls", "1.0.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	internal class PluginLoader : BaseUnityPlugin
	{
		internal const string modGUID = "Dev1A3.BetterVehicleControls";

		internal const string modVersion = "1.0.0";

		private readonly Harmony harmony = new Harmony("Dev1A3.BetterVehicleControls");

		private static bool initialized;

		internal static VehicleControls VehicleControlsInstance;

		internal static ManualLogSource logSource;

		internal static int maxTurboBoosts = 4;

		public static PluginLoader Instance { get; private set; }

		private void Awake()
		{
			if (!initialized)
			{
				initialized = true;
				Instance = this;
				logSource = ((BaseUnityPlugin)this).Logger;
				VehicleControlsInstance = new VehicleControls();
				FixesConfig.InitConfig();
				Assembly executingAssembly = Assembly.GetExecutingAssembly();
				harmony.PatchAll(executingAssembly);
			}
		}

		public void BindConfig<T>(ref ConfigEntry<T> config, string section, string key, T defaultValue, string description = "")
		{
			config = ((BaseUnityPlugin)this).Config.Bind<T>(section, key, defaultValue, description);
		}
	}
	internal class FixesConfig
	{
		internal static ConfigEntry<bool> AutoSwitchDriveReverse;

		internal static ConfigEntry<bool> AutoSwitchFromParked;

		internal static ConfigEntry<bool> AutoSwitchToParked;

		internal static ConfigEntry<bool> RecenterWheel;

		internal static ConfigEntry<int> RecenterWheelSpeed;

		internal static ConfigEntry<int> ChanceToStartIgnition;

		internal static ConfigEntry<int> MaxTurboBoosts;

		internal static void InitConfig()
		{
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Expected O, but got Unknown
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Expected O, but got Unknown
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_011f: Expected O, but got Unknown
			PluginLoader.Instance.BindConfig(ref AutoSwitchDriveReverse, "Settings", "Automatic Gearbox", defaultValue: true, "Should the gear automatically switch between drive & reverse when pressing the forward/backwards buttons?");
			PluginLoader.Instance.BindConfig(ref AutoSwitchFromParked, "Settings", "Automatic Handbrake Release", defaultValue: false, "Should the gear automatically switch to drive/reverse from parked?");
			PluginLoader.Instance.BindConfig(ref AutoSwitchToParked, "Settings", "Automatic Handbrake Pull", defaultValue: false, "Should the gear automatically switch to parked when the key is taken from the ignition?");
			PluginLoader.Instance.BindConfig(ref RecenterWheel, "Settings", "Automatically Center Wheel", defaultValue: true, "Should the wheel be automatically re-centered?");
			AcceptableValueRange<int> val = new AcceptableValueRange<int>(-1, 20);
			RecenterWheelSpeed = ((BaseUnityPlugin)PluginLoader.Instance).Config.Bind<int>("Settings", "Center Wheel Speed", -1, new ConfigDescription("How fast should the wheel be re-centered? (Instant: 0, Vanilla: -1)", (AcceptableValueBase)(object)val, Array.Empty<object>()));
			AcceptableValueRange<int> val2 = new AcceptableValueRange<int>(0, 101);
			ChanceToStartIgnition = ((BaseUnityPlugin)PluginLoader.Instance).Config.Bind<int>("Settings", "Ignition Chance", 0, new ConfigDescription("What should the success chance for the ignition be? If set to 0 this will increase the chance each time the ignition is used. (Vanilla: 0)", (AcceptableValueBase)(object)val2, Array.Empty<object>()));
			AcceptableValueRange<int> val3 = new AcceptableValueRange<int>(1, 100);
			MaxTurboBoosts = ((BaseUnityPlugin)PluginLoader.Instance).Config.Bind<int>("Settings", "Turbo Boosts", 5, new ConfigDescription("How many turbo boosts should you be able to have queued up at the same time? (Vanilla: 5)", (AcceptableValueBase)(object)val3, Array.Empty<object>()));
			PluginLoader.maxTurboBoosts = MaxTurboBoosts.Value;
			MaxTurboBoosts.SettingChanged += delegate
			{
				PluginLoader.maxTurboBoosts = MaxTurboBoosts.Value;
			};
		}
	}
	internal class VehicleControls : LcInputActions
	{
		[InputAction(/*Could not decode attribute arguments.*/)]
		public InputAction GasPedalKey { get; set; }

		[InputAction(/*Could not decode attribute arguments.*/)]
		public InputAction BrakePedalKey { get; set; }

		[InputAction(/*Could not decode attribute arguments.*/)]
		public InputAction TurboKey { get; set; }

		[InputAction(/*Could not decode attribute arguments.*/)]
		public InputAction JumpKey { get; set; }

		[InputAction(/*Could not decode attribute arguments.*/)]
		public InputAction MoveForwardsKey { get; set; }

		[InputAction(/*Could not decode attribute arguments.*/)]
		public InputAction MoveBackwardsKey { get; set; }

		[InputAction(/*Could not decode attribute arguments.*/)]
		public InputAction GearShiftForwardKey { get; set; }

		[InputAction(/*Could not decode attribute arguments.*/)]
		public InputAction GearShiftBackwardKey { get; set; }

		[InputAction(/*Could not decode attribute arguments.*/)]
		public InputAction WheelCenterKey { get; set; }

		[InputAction(/*Could not decode attribute arguments.*/)]
		public InputAction ToggleHeadlightsKey { get; set; }

		[InputAction(/*Could not decode attribute arguments.*/)]
		public InputAction ActivateHornKey { get; set; }

		[InputAction(/*Could not decode attribute arguments.*/)]
		public InputAction ToggleWipersKey { get; set; }

		[InputAction(/*Could not decode attribute arguments.*/)]
		public InputAction ToggleMagnetKey { get; set; }
	}
	internal static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "BetterVehicleControls";

		public const string PLUGIN_NAME = "BetterVehicleControls";

		public const string PLUGIN_VERSION = "1.1.3";
	}
}
namespace BetterVehicleControls.Patches
{
	[HarmonyPatch]
	internal static class Patches_VehicleController
	{
		internal static VehicleController controlledVehicle;

		public static bool centerKeyPressed;

		public static VehicleController GetControlledVehicle()
		{
			if ((Object)(object)controlledVehicle == (Object)null || !controlledVehicle.localPlayerInControl)
			{
				VehicleController val = ((IEnumerable<VehicleController>)Object.FindObjectsByType<VehicleController>((FindObjectsSortMode)0)).FirstOrDefault((Func<VehicleController, bool>)((VehicleController x) => x.localPlayerInControl));
				if ((Object)(object)val != (Object)null && val.localPlayerInControl)
				{
					controlledVehicle = val;
				}
				else
				{
					controlledVehicle = null;
				}
			}
			return controlledVehicle;
		}

		[HarmonyPatch(typeof(VehicleController), "Start")]
		[HarmonyPrefix]
		public static void Start(VehicleController __instance)
		{
			if (((NetworkBehaviour)__instance).IsOwner && __instance.headlightsContainer.activeSelf != ((Object)(object)TimeOfDay.Instance != (Object)null && TimeOfDay.Instance.normalizedTimeOfDay >= 0.63f))
			{
				__instance.ToggleHeadlightsLocalClient();
				PluginLoader.logSource.LogInfo((object)"Automatically toggled headlights due to time of day.");
			}
		}

		[HarmonyPatch(typeof(VehicleController), "TryIgnition")]
		[HarmonyPrefix]
		public static void TryIgnition(ref float ___chanceToStartIgnition)
		{
			if (FixesConfig.ChanceToStartIgnition.Value > 0)
			{
				___chanceToStartIgnition = FixesConfig.ChanceToStartIgnition.Value;
			}
		}

		[HarmonyPatch(typeof(VehicleController), "RemoveKeyFromIgnition")]
		[HarmonyPostfix]
		public static void RemoveKeyFromIgnition(VehicleController __instance)
		{
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Invalid comparison between Unknown and I4
			if (FixesConfig.AutoSwitchToParked.Value && __instance.localPlayerInControl)
			{
				int num = 3;
				if ((int)__instance.gear != num)
				{
					__instance.ShiftToGearAndSync(num);
				}
			}
		}

		[HarmonyPatch(typeof(VehicleController), "ActivateControl")]
		[HarmonyPostfix]
		public static void ActivateControl(VehicleController __instance)
		{
			__instance.setControlTips = true;
			(__instance.testingVehicleInEditor ? __instance.input.actions : IngamePlayerSettings.Instance.playerInput.actions).FindAction("Jump", false).performed -= __instance.DoTurboBoost;
			PluginLoader.VehicleControlsInstance.TurboKey.performed += __instance.DoTurboBoost;
			PluginLoader.VehicleControlsInstance.JumpKey.performed += DoJump;
			PluginLoader.VehicleControlsInstance.GearShiftForwardKey.performed += ChangeGear_Forward;
			PluginLoader.VehicleControlsInstance.GearShiftBackwardKey.performed += ChangeGear_Backward;
			PluginLoader.VehicleControlsInstance.ToggleMagnetKey.performed += ActivateMagnet;
			PluginLoader.VehicleControlsInstance.ToggleHeadlightsKey.performed += ActivateHeadlights;
			PluginLoader.VehicleControlsInstance.ToggleWipersKey.performed += ActivateWipers;
			PluginLoader.VehicleControlsInstance.ActivateHornKey.performed += ActivateHorn;
			PluginLoader.VehicleControlsInstance.ActivateHornKey.canceled += ActivateHorn;
			centerKeyPressed = false;
		}

		[HarmonyPatch(typeof(VehicleController), "DisableControl")]
		[HarmonyPostfix]
		public static void DisableControl(VehicleController __instance)
		{
			if (!__instance.testingVehicleInEditor)
			{
				_ = IngamePlayerSettings.Instance.playerInput.actions;
			}
			else
			{
				_ = __instance.input.actions;
			}
			PluginLoader.VehicleControlsInstance.TurboKey.performed -= __instance.DoTurboBoost;
			PluginLoader.VehicleControlsInstance.JumpKey.performed -= DoJump;
			PluginLoader.VehicleControlsInstance.GearShiftForwardKey.performed -= ChangeGear_Forward;
			PluginLoader.VehicleControlsInstance.GearShiftBackwardKey.performed -= ChangeGear_Backward;
			PluginLoader.VehicleControlsInstance.ToggleMagnetKey.performed -= ActivateMagnet;
			PluginLoader.VehicleControlsInstance.ToggleHeadlightsKey.performed -= ActivateHeadlights;
			PluginLoader.VehicleControlsInstance.ToggleWipersKey.performed -= ActivateWipers;
			PluginLoader.VehicleControlsInstance.ActivateHornKey.performed -= ActivateHorn;
			PluginLoader.VehicleControlsInstance.ActivateHornKey.canceled -= ActivateHorn;
		}

		[HarmonyPatch(typeof(VehicleController), "GetVehicleInput")]
		[HarmonyPostfix]
		public static void GetVehicleInput(VehicleController __instance, ref float ___steeringWheelAnimFloat)
		{
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Invalid comparison between Unknown and I4
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Invalid comparison between Unknown and I4
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Invalid comparison between Unknown and I4
			if (!__instance.localPlayerInControl)
			{
				centerKeyPressed = false;
				return;
			}
			__instance.brakePedalPressed = PluginLoader.VehicleControlsInstance.BrakePedalKey.IsPressed();
			int num = 0;
			if (PluginLoader.VehicleControlsInstance.GasPedalKey.IsPressed())
			{
				__instance.drivePedalPressed = true;
			}
			else if (PluginLoader.VehicleControlsInstance.MoveForwardsKey.IsPressed())
			{
				num = 1;
				__instance.drivePedalPressed = true;
			}
			else if (PluginLoader.VehicleControlsInstance.MoveBackwardsKey.IsPressed())
			{
				num = 2;
				__instance.drivePedalPressed = true;
			}
			else
			{
				__instance.drivePedalPressed = false;
			}
			if (__instance.drivePedalPressed && ((FixesConfig.AutoSwitchFromParked.Value && (int)__instance.gear == 3) || (FixesConfig.AutoSwitchDriveReverse.Value && (int)__instance.gear != 3 && num != 0)))
			{
				int num2 = ((num != 2) ? 1 : 2);
				if ((int)__instance.gear != num2)
				{
					__instance.ShiftToGearAndSync(num2);
				}
			}
			if (!centerKeyPressed && PluginLoader.VehicleControlsInstance.WheelCenterKey.triggered)
			{
				centerKeyPressed = true;
			}
			if (__instance.moveInputVector.x == 0f && (FixesConfig.RecenterWheel.Value || centerKeyPressed))
			{
				if ((float)FixesConfig.RecenterWheelSpeed.Value < 0f)
				{
					__instance.steeringInput = Mathf.MoveTowards(__instance.steeringInput, 0f, __instance.steeringWheelTurnSpeed * Time.deltaTime);
				}
				else if ((float)FixesConfig.RecenterWheelSpeed.Value > 0f)
				{
					__instance.steeringInput = Mathf.MoveTowards(__instance.steeringInput, 0f, (float)FixesConfig.RecenterWheelSpeed.Value * Time.deltaTime);
				}
				else
				{
					__instance.steeringInput = __instance.moveInputVector.x;
					__instance.steeringAnimValue = __instance.steeringInput;
					___steeringWheelAnimFloat = __instance.steeringAnimValue;
				}
				if (centerKeyPressed && __instance.steeringInput == 0f)
				{
					centerKeyPressed = false;
				}
			}
		}

		[HarmonyPatch(typeof(VehicleController), "SetCarEffects")]
		[HarmonyPrefix]
		public static void SetCarEffects(VehicleController __instance, ref float setSteering)
		{
			setSteering = 0f;
			__instance.steeringWheelAnimFloat = __instance.steeringInput / 6f;
		}

		public static void ChangeGear_Forward(CallbackContext context)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected I4, but got Unknown
			if (!((CallbackContext)(ref context)).performed)
			{
				return;
			}
			VehicleController val = GetControlledVehicle();
			if ((Object)(object)val != (Object)null)
			{
				int num = (int)val.gear;
				if (num < 3)
				{
					val.ShiftToGearAndSync(num + 1);
				}
			}
		}

		public static void ChangeGear_Backward(CallbackContext context)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0020: Expected I4, but got Unknown
			if (!((CallbackContext)(ref context)).performed)
			{
				return;
			}
			VehicleController val = GetControlledVehicle();
			if ((Object)(object)val != (Object)null)
			{
				int num = (int)val.gear;
				if (num > 1)
				{
					val.ShiftToGearAndSync(num - 1);
				}
			}
		}

		public static void ActivateMagnet(CallbackContext context)
		{
			//IL_0046: 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)
			if (!((CallbackContext)(ref context)).performed)
			{
				return;
			}
			GameObject val = GameObject.Find("Environment/HangarShip/MagnetLever");
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			AnimatedObjectTrigger component = val.GetComponent<AnimatedObjectTrigger>();
			if ((Object)(object)component == (Object)null)
			{
				return;
			}
			VehicleController val2 = GetControlledVehicle();
			if (!((Object)(object)val2 == (Object)null))
			{
				float num = Vector3.Distance(((Component)val2).transform.position, StartOfRound.Instance.magnetPoint.position);
				if (num >= 20f)
				{
					PluginLoader.logSource.LogDebug((object)$"Vehicle is too far away from the magnet to toggle it. Distance: {num}");
					return;
				}
				component.TriggerAnimation(GameNetworkManager.Instance.localPlayerController);
				string text = (component.boolValue ? "Activated" : "Deactivated");
				HUDManager.Instance.DisplayGlobalNotification("Ship Magnet " + text);
			}
		}

		public static void ActivateHeadlights(CallbackContext context)
		{
			if (((CallbackContext)(ref context)).performed)
			{
				VehicleController obj = GetControlledVehicle();
				if (obj != null)
				{
					obj.ToggleHeadlightsLocalClient();
				}
			}
		}

		public static void ActivateWipers(CallbackContext context)
		{
			if (!((CallbackContext)(ref context)).performed)
			{
				return;
			}
			VehicleController val = GetControlledVehicle();
			if ((Object)(object)val != (Object)null)
			{
				Transform obj = ((Component)val).transform.Find("Meshes/Windwipers/WindwipersAnim");
				AnimatedObjectTrigger obj2 = ((obj != null) ? ((Component)obj).GetComponent<AnimatedObjectTrigger>() : null);
				if (obj2 != null)
				{
					obj2.TriggerAnimation(StartOfRound.Instance.localPlayerController);
				}
			}
		}

		public static void ActivateHorn(CallbackContext context)
		{
			VehicleController val = GetControlledVehicle();
			if ((Object)(object)val != (Object)null && ((((CallbackContext)(ref context)).performed && !val.honkingHorn) || (((CallbackContext)(ref context)).canceled && val.honkingHorn)))
			{
				val.SetHonkingLocalClient(!val.honkingHorn);
			}
		}

		public static void DoJump(CallbackContext context)
		{
			//IL_0034: 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_0093: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			if (!((CallbackContext)(ref context)).performed)
			{
				return;
			}
			VehicleController val = GetControlledVehicle();
			if ((Object)(object)val == (Object)null || val.jumpingInCar || val.keyIsInDriverHand)
			{
				return;
			}
			if (val.turboBoosts == 0)
			{
				val.DoTurboBoost(context);
			}
			else if (!val.turboBoostParticle.isPlaying)
			{
				Vector2 val2 = IngamePlayerSettings.Instance.playerInput.actions.FindAction("Move", false).ReadValue<Vector2>();
				if (((NetworkBehaviour)val).IsOwner)
				{
					val.jumpingInCar = true;
					GameNetworkManager.Instance.localPlayerController.playerBodyAnimator.SetTrigger("SA_JumpInCar");
					((MonoBehaviour)val).StartCoroutine(val.jerkCarUpward(Vector2.op_Implicit(val2)));
				}
				val.springAudio.PlayOneShot(val.jumpInCarSFX);
			}
		}

		[HarmonyPatch(typeof(VehicleController), "AddTurboBoost")]
		[HarmonyTranspiler]
		public static IEnumerable<CodeInstruction> AddTurboBoost(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0048: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Expected O, but got Unknown
			List<CodeInstruction> list = new List<CodeInstruction>();
			bool flag = false;
			foreach (CodeInstruction instruction in instructions)
			{
				if (!flag && instruction.opcode == OpCodes.Ldc_I4_5)
				{
					flag = true;
					CodeInstruction item = new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(PluginLoader), "maxTurboBoosts"));
					list.Add(item);
				}
				else
				{
					list.Add(instruction);
				}
			}
			if (!flag)
			{
				PluginLoader.logSource.LogWarning((object)"AddTurboBoost failed to replace maxTurboBoosts");
			}
			return list.AsEnumerable();
		}

		[HarmonyPatch(typeof(VehicleController), "ReactToDamage")]
		[HarmonyTranspiler]
		public static IEnumerable<CodeInstruction> ReactToDamage(IEnumerable<CodeInstruction> instructions)
		{
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Expected O, but got Unknown
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Expected O, but got Unknown
			List<CodeInstruction> list = new List<CodeInstruction>();
			bool flag = false;
			foreach (CodeInstruction instruction in instructions)
			{
				if (!flag && instruction.opcode == OpCodes.Ldc_R4 && instruction.operand?.ToString() == "5")
				{
					flag = true;
					CodeInstruction item = new CodeInstruction(OpCodes.Ldsfld, (object)AccessTools.Field(typeof(PluginLoader), "maxTurboBoosts"));
					list.Add(item);
					CodeInstruction item2 = new CodeInstruction(OpCodes.Conv_R4, (object)null);
					list.Add(item2);
				}
				else
				{
					list.Add(instruction);
				}
			}
			if (!flag)
			{
				PluginLoader.logSource.LogWarning((object)"ReactToDamage failed to replace maxTurboBoosts");
			}
			return list.AsEnumerable();
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}