Decompiled source of G920CruiserSupport v1.0.0

G920Cruiser/G920Cruiser.dll

Decompiled 4 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.Utilities;

[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("G920Cruiser")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("G920Cruiser")]
[assembly: AssemblyTitle("G920Cruiser")]
[assembly: AssemblyVersion("1.0.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 G920Cruiser
{
	[BepInPlugin("local.g920cruiser", "G920 Cruiser Support", "0.1.0")]
	public class Plugin : BaseUnityPlugin
	{
		internal struct WheelState
		{
			public float Steer;

			public float Gas;

			public float Brake;

			public bool ShiftFwd;

			public bool ShiftBack;
		}

		internal static Plugin Instance;

		internal static ConfigEntry<string> Mode;

		internal static ConfigEntry<string> DeviceName;

		internal static ConfigEntry<string> WheelPath;

		internal static ConfigEntry<string> GasPath;

		internal static ConfigEntry<string> BrakePath;

		internal static ConfigEntry<string> ShiftUpPath;

		internal static ConfigEntry<string> ShiftDownPath;

		internal static ConfigEntry<float> WheelLeft;

		internal static ConfigEntry<float> WheelRight;

		internal static ConfigEntry<float> WheelScale;

		internal static ConfigEntry<float> Deadzone;

		internal static ConfigEntry<float> MaxSteer;

		internal static ConfigEntry<float> GasRest;

		internal static ConfigEntry<float> GasPressed;

		internal static ConfigEntry<float> BrakeRest;

		internal static ConfigEntry<float> BrakePressed;

		internal static ConfigEntry<float> PedalThreshold;

		internal static ConfigEntry<bool> SyncToOthers;

		internal static ConfigEntry<bool> TriggersAsPedals;

		internal static ConfigEntry<string> ReverseButton;

		internal static ConfigEntry<string> DriveButtons;

		internal static ConfigEntry<string> ParkButton;

		internal static ConfigEntry<float> NeutralParkSeconds;

		internal static ConfigEntry<float> CameraTurnSpeed;

		internal static ConfigEntry<string> JumpButton;

		internal static ConfigEntry<string> KeyButton;

		internal static ConfigEntry<string> CamLeftButton;

		internal static ConfigEntry<string> CamRightButton;

		internal static ConfigEntry<int> GearDriveId;

		internal static ConfigEntry<int> GearReverseId;

		internal static ConfigEntry<int> GearParkId;

		private static InputDevice _dev;

		private static float _lastGas;

		private static float _lastBrake;

		private static float _lastSteer;

		internal static bool PedalsArmed;

		private static float _nextScan;

		private static readonly HashSet<string> _warned = new HashSet<string>();

		private readonly Dictionary<string, float> _lastAxis = new Dictionary<string, float>();

		private readonly Dictionary<string, bool> _lastButton = new Dictionary<string, bool>();

		private float _nextWatch;

		internal static void Log(string message)
		{
			((BaseUnityPlugin)Instance).Logger.LogInfo((object)message);
		}

		private void Awake()
		{
			Instance = this;
			((Object)((Component)this).gameObject).hideFlags = (HideFlags)61;
			((MonoBehaviour)this).Invoke("DumpOnce", 10f);
			Mode = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Mode", "Direct", "Direct = read the G920 itself. Gamepad = read an Xbox-style controller (e.g. G920 through x360ce): left stick X is the wheel.");
			DeviceName = ((BaseUnityPlugin)this).Config.Bind<string>("Direct", "DeviceNameContains", "G920", "Text that must appear in the device name. Press F9 in game to list devices.");
			WheelPath = ((BaseUnityPlugin)this).Config.Bind<string>("Direct", "WheelControl", "stick/x", "Control path of the wheel axis on the device. Press F9 in game to list the paths and live values.");
			WheelLeft = ((BaseUnityPlugin)this).Config.Bind<float>("Direct", "WheelRawFullLeft", -1f, "Raw value when the wheel is turned fully left.");
			WheelRight = ((BaseUnityPlugin)this).Config.Bind<float>("Direct", "WheelRawFullRight", 1f, "Raw value when the wheel is turned fully right.");
			GasPath = ((BaseUnityPlugin)this).Config.Bind<string>("Direct", "GasControl", "stick/y", "Control path of the gas pedal axis. Empty = unused (keyboard W still works).");
			GasRest = ((BaseUnityPlugin)this).Config.Bind<float>("Direct", "GasRawReleased", -1f, "Raw value with the gas pedal released.");
			GasPressed = ((BaseUnityPlugin)this).Config.Bind<float>("Direct", "GasRawPressed", 1f, "Raw value with the gas pedal fully pressed.");
			BrakePath = ((BaseUnityPlugin)this).Config.Bind<string>("Direct", "BrakeControl", "z", "Control path of the brake pedal axis. Empty = unused. Can be the same path as the gas (merged pedals).");
			BrakeRest = ((BaseUnityPlugin)this).Config.Bind<float>("Direct", "BrakeRawReleased", 1f, "Raw value with the brake released.");
			BrakePressed = ((BaseUnityPlugin)this).Config.Bind<float>("Direct", "BrakeRawPressed", -1f, "Raw value with the brake fully pressed.");
			PedalThreshold = ((BaseUnityPlugin)this).Config.Bind<float>("Direct", "PedalThreshold", 0.15f, "The game's pedals are on/off. Pressed further than this (0-1) counts as pressed.");
			ShiftDownPath = ((BaseUnityPlugin)this).Config.Bind<string>("Direct", "ShiftBackButton", "", "Button that steps the lever one position toward Park (e.g. a paddle). Empty = unused.");
			ShiftUpPath = ((BaseUnityPlugin)this).Config.Bind<string>("Direct", "ShiftForwardButton", "", "Button that steps the lever one position toward Drive (Park > Reverse > Drive). Empty = unused.");
			ReverseButton = ((BaseUnityPlugin)this).Config.Bind<string>("Gearstick", "ReverseButton", "button12", "Button that is held while the H-shifter is in reverse (e.g. button18). Empty = unused.");
			DriveButtons = ((BaseUnityPlugin)this).Config.Bind<string>("Gearstick", "DriveButtons", "button13,button14,button15,button16,button17,button18", "Comma separated buttons held in the forward gears, e.g. button12,button13,button14,button15,button16,button17. Empty = unused.");
			ParkButton = ((BaseUnityPlugin)this).Config.Bind<string>("Gearstick", "ParkButton", "button13", "Optional button that puts the Cruiser in Park. Empty = unused.");
			NeutralParkSeconds = ((BaseUnityPlugin)this).Config.Bind<float>("Gearstick", "NeutralParkSeconds", 0f, "If > 0: shifter left in neutral (no gear button held) this many seconds puts the Cruiser in Park. 0 = off.");
			GearDriveId = ((BaseUnityPlugin)this).Config.Bind<int>("Gearstick", "GameGearIdDrive", 1, "The game's internal gear number for Drive. Swap these three if the lever goes to the wrong gear.");
			GearReverseId = ((BaseUnityPlugin)this).Config.Bind<int>("Gearstick", "GameGearIdReverse", 2, "The game's internal gear number for Reverse.");
			GearParkId = ((BaseUnityPlugin)this).Config.Bind<int>("Gearstick", "GameGearIdPark", 3, "The game's internal gear number for Park.");
			WheelScale = ((BaseUnityPlugin)this).Config.Bind<float>("Feel", "WheelScale", 1f, "Multiplier on wheel angle. 1.5 = full lock is reached at 2/3 of your wheel's range.");
			Deadzone = ((BaseUnityPlugin)this).Config.Bind<float>("Feel", "Deadzone", 0.01f, "Centre deadzone (0-0.3).");
			MaxSteer = ((BaseUnityPlugin)this).Config.Bind<float>("Feel", "MaxSteerInput", 3f, "Steering value at full lock. The game itself clamps at 3 (= 45 degrees of wheel angle). Do not go above 3.");
			SyncToOthers = ((BaseUnityPlugin)this).Config.Bind<bool>("Network", "SyncSteeringToOthers", true, "Make other players see the wheel turn correctly. Disable if it looks wrong for them.");
			TriggersAsPedals = ((BaseUnityPlugin)this).Config.Bind<bool>("Gamepad", "TriggersAsPedals", false, "In Gamepad mode, use right trigger = gas, left trigger = brake.");
			JumpButton = ((BaseUnityPlugin)this).Config.Bind<string>("Buttons", "JumpButton", "trigger", "Button that makes the Cruiser jump / turbo boost (A button = 'trigger', which is how Unity names button 1). Empty = unused.");
			KeyButton = ((BaseUnityPlugin)this).Config.Bind<string>("Buttons", "KeyButton", "button4", "Button that inserts/turns the key (start engine) or removes it when the engine is running (Y button = button4). Empty = unused.");
			CamLeftButton = ((BaseUnityPlugin)this).Config.Bind<string>("Buttons", "CameraLeftButton", "button6", "Button held to turn your camera left (left paddle = button6). Empty = unused.");
			CamRightButton = ((BaseUnityPlugin)this).Config.Bind<string>("Buttons", "CameraRightButton", "button5", "Button held to turn your camera right (right paddle = button5). Empty = unused.");
			CameraTurnSpeed = ((BaseUnityPlugin)this).Config.Bind<float>("Buttons", "CameraTurnSpeed", 600f, "How fast the camera turns while the paddle is held (mouse pixels per second). Raise or lower to taste.");
			((BaseUnityPlugin)this).Logger.LogInfo((object)("Config: mode=" + Mode.Value + " reverse='" + ReverseButton.Value + "' drive='" + DriveButtons.Value + "' park='" + ParkButton.Value + "' jump='" + JumpButton.Value + "' key='" + KeyButton.Value + "' cam=" + CamLeftButton.Value + "/" + CamRightButton.Value + " gearIds(drive/rev/park)=" + GearDriveId.Value + "/" + GearReverseId.Value + "/" + GearParkId.Value));
			Harmony.CreateAndPatchAll(typeof(Plugin).Assembly, "local.g920cruiser");
			((BaseUnityPlugin)this).Logger.LogInfo((object)"G920 Cruiser Support loaded. Press F9 in game to dump input devices and live axis values.");
		}

		private void DumpOnce()
		{
			DumpDevices();
		}

		private void Update()
		{
			Keyboard current = Keyboard.current;
			if (current != null && ((ButtonControl)current.f9Key).wasPressedThisFrame)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)"F9 pressed");
				DumpDevices();
			}
			WatchControls();
			TurnCamera();
		}

		private void WatchControls()
		{
			//IL_002a: Unknown result type (might be due to invalid IL or missing references)
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0037: Unknown result type (might be due to invalid IL or missing references)
			if (Time.unscaledTime < _nextWatch)
			{
				return;
			}
			_nextWatch = Time.unscaledTime + 0.2f;
			InputDevice val = FindDevice();
			if (val == null)
			{
				return;
			}
			Enumerator<InputControl> enumerator = val.allControls.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					InputControl current = enumerator.Current;
					string text = current.path.Substring(((InputControl)val).path.Length + 1);
					ButtonControl val2 = (ButtonControl)(object)((current is ButtonControl) ? current : null);
					if (val2 != null)
					{
						bool isPressed = val2.isPressed;
						if (_lastButton.TryGetValue(text, out var value) && value != isPressed)
						{
							((BaseUnityPlugin)this).Logger.LogInfo((object)("[button] " + text + (isPressed ? " pressed" : " released")));
						}
						_lastButton[text] = isPressed;
						continue;
					}
					AxisControl val3 = (AxisControl)(object)((current is AxisControl) ? current : null);
					if (val3 != null)
					{
						float num = ((InputControl<float>)(object)val3).ReadValue();
						if (!_lastAxis.TryGetValue(text, out var value2))
						{
							_lastAxis[text] = num;
						}
						else if (Mathf.Abs(num - value2) > 0.05f)
						{
							_lastAxis[text] = num;
							((BaseUnityPlugin)this).Logger.LogInfo((object)("[axis] " + text + " = " + num.ToString("F3")));
						}
					}
				}
			}
			finally
			{
				((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
			}
		}

		private void TurnCamera()
		{
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			if (Mode.Value.ToLowerInvariant() != "direct")
			{
				return;
			}
			InputDevice val = FindDevice();
			if (val == null || IsStaleDefaultState(val))
			{
				return;
			}
			Mouse current = Mouse.current;
			if (current != null)
			{
				ButtonControl val2 = Button(val, CamLeftButton.Value);
				ButtonControl val3 = Button(val, CamRightButton.Value);
				bool num = val2 != null && val2.isPressed;
				bool flag = val3 != null && val3.isPressed;
				if (num != flag)
				{
					float num2 = (flag ? 1f : (-1f)) * CameraTurnSpeed.Value * Time.unscaledDeltaTime;
					InputSystem.QueueDeltaStateEvent<Vector2>((InputControl)(object)((Pointer)current).delta, new Vector2(num2, 0f), -1.0);
				}
			}
		}

		internal static bool Read(out WheelState s)
		{
			s = default(WheelState);
			if (Mode.Value.ToLowerInvariant() == "gamepad")
			{
				Gamepad current = Gamepad.current;
				if (current == null)
				{
					return false;
				}
				s.Steer = Shape(((InputControl<float>)(object)((Vector2Control)current.leftStick).x).ReadValue());
				if (TriggersAsPedals.Value)
				{
					s.Gas = ((InputControl<float>)(object)current.rightTrigger).ReadValue();
					s.Brake = ((InputControl<float>)(object)current.leftTrigger).ReadValue();
				}
				return true;
			}
			InputDevice val = FindDevice();
			if (val == null)
			{
				return false;
			}
			if (IsStaleDefaultState(val))
			{
				return false;
			}
			AxisControl val2 = Axis(val, WheelPath.Value);
			if (val2 == null)
			{
				return false;
			}
			AxisControl val3 = ((InputControl)val).TryGetChildControl<AxisControl>("z");
			AxisControl val4 = ((InputControl)val).TryGetChildControl<AxisControl>("rz");
			if (val3 != null && val4 != null && ((InputControl<float>)(object)val3).ReadValue() == 0f && ((InputControl<float>)(object)val4).ReadValue() == 0f)
			{
				s.Steer = _lastSteer;
				s.Gas = _lastGas;
				s.Brake = _lastBrake;
			}
			else
			{
				float v = Mathf.InverseLerp(WheelLeft.Value, WheelRight.Value, ((InputControl<float>)(object)val2).ReadValue()) * 2f - 1f;
				s.Steer = Shape(v);
				AxisControl val5 = Axis(val, GasPath.Value);
				if (val5 != null)
				{
					s.Gas = Mathf.InverseLerp(GasRest.Value, GasPressed.Value, ((InputControl<float>)(object)val5).ReadValue());
				}
				AxisControl val6 = Axis(val, BrakePath.Value);
				if (val6 != null)
				{
					s.Brake = Mathf.InverseLerp(BrakeRest.Value, BrakePressed.Value, ((InputControl<float>)(object)val6).ReadValue());
				}
				_lastSteer = s.Steer;
				_lastGas = s.Gas;
				_lastBrake = s.Brake;
			}
			if (s.Gas < 0.05f && s.Brake < 0.05f)
			{
				PedalsArmed = true;
			}
			if (!PedalsArmed)
			{
				s.Gas = 0f;
				s.Brake = 0f;
			}
			ButtonControl val7 = Button(val, ShiftUpPath.Value);
			if (val7 != null)
			{
				s.ShiftFwd = val7.wasPressedThisFrame;
			}
			ButtonControl val8 = Button(val, ShiftDownPath.Value);
			if (val8 != null)
			{
				s.ShiftBack = val8.wasPressedThisFrame;
			}
			return true;
		}

		internal static bool IsStaleDefaultState(InputDevice dev)
		{
			AxisControl val = ((InputControl)dev).TryGetChildControl<AxisControl>("stick/x");
			AxisControl val2 = ((InputControl)dev).TryGetChildControl<AxisControl>("stick/y");
			AxisControl val3 = ((InputControl)dev).TryGetChildControl<AxisControl>("z");
			AxisControl val4 = ((InputControl)dev).TryGetChildControl<AxisControl>("rz");
			if (val == null || val2 == null || val3 == null || val4 == null)
			{
				return false;
			}
			if (((InputControl<float>)(object)val).ReadValue() < -0.98f && ((InputControl<float>)(object)val3).ReadValue() < -0.98f && Mathf.Abs(((InputControl<float>)(object)val4).ReadValue() + 0.78f) < 0.03f)
			{
				return Mathf.Abs(((InputControl<float>)(object)val2).ReadValue() - 0.084f) < 0.03f;
			}
			return false;
		}

		private static float Shape(float v)
		{
			v = Mathf.Clamp(v * WheelScale.Value, -1f, 1f);
			float num = Mathf.Clamp(Deadzone.Value, 0f, 0.3f);
			float num2 = Mathf.Abs(v);
			if (num2 < num)
			{
				return 0f;
			}
			return Mathf.Sign(v) * (num2 - num) / (1f - num);
		}

		internal static InputDevice FindDevice()
		{
			//IL_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0052: 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_005a: 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_0087: Unknown result type (might be due to invalid IL or missing references)
			if (_dev != null && _dev.added)
			{
				return _dev;
			}
			_dev = null;
			if (Time.unscaledTime < _nextScan)
			{
				return null;
			}
			_nextScan = Time.unscaledTime + 2f;
			string value = DeviceName.Value.ToLowerInvariant();
			Enumerator<InputDevice> enumerator = InputSystem.devices.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					InputDevice current = enumerator.Current;
					string[] obj = new string[5]
					{
						((InputControl)current).displayName,
						" ",
						null,
						null,
						null
					};
					InputDeviceDescription description = current.description;
					obj[2] = ((InputDeviceDescription)(ref description)).product;
					obj[3] = " ";
					obj[4] = ((InputControl)current).name;
					if (string.Concat(obj).ToLowerInvariant().Contains(value))
					{
						_dev = current;
						((BaseUnityPlugin)Instance).Logger.LogInfo((object)("Using input device: " + ((InputControl)current).displayName + " (" + ((InputControl)current).layout + ")"));
						break;
					}
				}
			}
			finally
			{
				((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
			}
			return _dev;
		}

		private static AxisControl Axis(InputDevice dev, string path)
		{
			if (string.IsNullOrWhiteSpace(path))
			{
				return null;
			}
			AxisControl val = ((InputControl)dev).TryGetChildControl<AxisControl>(path);
			if (val == null && _warned.Add(path))
			{
				((BaseUnityPlugin)Instance).Logger.LogWarning((object)("No axis control '" + path + "' on " + ((InputControl)dev).displayName + ". Press F9 to list valid paths."));
			}
			return val;
		}

		internal static ButtonControl Button(InputDevice dev, string path)
		{
			if (string.IsNullOrWhiteSpace(path))
			{
				return null;
			}
			ButtonControl val = ((InputControl)dev).TryGetChildControl<ButtonControl>(path);
			if (val == null && path.Trim().ToLowerInvariant() == "button1")
			{
				val = ((InputControl)dev).TryGetChildControl<ButtonControl>("trigger");
			}
			if (val == null && _warned.Add(path))
			{
				((BaseUnityPlugin)Instance).Logger.LogWarning((object)("No button control '" + path + "' on " + ((InputControl)dev).displayName + ". Press F9 to list valid paths."));
			}
			return val;
		}

		private static void DumpDevices()
		{
			//IL_000b: 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)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0060: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			StringBuilder stringBuilder = new StringBuilder("\n=== F9 input dump ===\nAll devices:\n");
			Enumerator<InputDevice> enumerator = InputSystem.devices.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					InputDevice current = enumerator.Current;
					string[] obj = new string[6]
					{
						"  ",
						((InputControl)current).displayName,
						"  | layout=",
						((InputControl)current).layout,
						" | product=",
						null
					};
					InputDeviceDescription description = current.description;
					obj[5] = ((InputDeviceDescription)(ref description)).product;
					stringBuilder.AppendLine(string.Concat(obj));
				}
			}
			finally
			{
				((IDisposable)enumerator/*cast due to .constrained prefix*/).Dispose();
			}
			InputDevice val = FindDevice();
			if (val == null)
			{
				stringBuilder.AppendLine("No device matching '" + DeviceName.Value + "'. Change DeviceNameContains in the config.");
			}
			else
			{
				stringBuilder.AppendLine("\nControls on " + ((InputControl)val).displayName + " (turn the wheel / press pedals and press F9 again to see values change):");
				Enumerator<InputControl> enumerator2 = val.allControls.GetEnumerator();
				try
				{
					while (enumerator2.MoveNext())
					{
						InputControl current2 = enumerator2.Current;
						ButtonControl val2 = (ButtonControl)(object)((current2 is ButtonControl) ? current2 : null);
						if (val2 != null)
						{
							if (val2.isPressed)
							{
								stringBuilder.AppendLine("  [button PRESSED] " + ((InputControl)val2).path.Substring(((InputControl)val).path.Length + 1));
							}
							continue;
						}
						AxisControl val3 = (AxisControl)(object)((current2 is AxisControl) ? current2 : null);
						if (val3 != null)
						{
							stringBuilder.AppendLine("  [axis] " + ((InputControl)val3).path.Substring(((InputControl)val).path.Length + 1) + " = " + ((InputControl<float>)(object)val3).ReadValue().ToString("F4"));
						}
					}
				}
				finally
				{
					((IDisposable)enumerator2/*cast due to .constrained prefix*/).Dispose();
				}
			}
			((BaseUnityPlugin)Instance).Logger.LogInfo((object)stringBuilder.ToString());
		}
	}
	[HarmonyPatch(typeof(VehicleController), "GetVehicleInput")]
	internal static class SteeringPatch
	{
		private static bool _active;

		private static float _remoteAnim;

		private static void Postfix(VehicleController __instance, ref float ___steeringWheelAnimFloat)
		{
			//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			if (!__instance.localPlayerInControl)
			{
				_active = false;
				Plugin.PedalsArmed = false;
				return;
			}
			if (!Plugin.Read(out var s))
			{
				_active = false;
				Plugin.PedalsArmed = false;
				return;
			}
			if (!_active)
			{
				_active = true;
				_remoteAnim = ___steeringWheelAnimFloat;
			}
			float num = Mathf.Clamp(Plugin.MaxSteer.Value, 0.1f, 3f);
			__instance.steeringInput = s.Steer * num;
			___steeringWheelAnimFloat = Mathf.Clamp(__instance.steeringInput * 0.3f, -0.99f, 0.99f);
			__instance.steeringAnimValue = 0f;
			if (Plugin.SyncToOthers.Value)
			{
				float num2 = Mathf.Clamp((___steeringWheelAnimFloat - _remoteAnim) * 20f, -1f, 1f);
				_remoteAnim = Mathf.Clamp(_remoteAnim + num2 * __instance.steeringWheelTurnSpeed * Time.deltaTime / 6f, -0.99f, 0.99f);
				__instance.moveInputVector = new Vector2(num2, __instance.moveInputVector.y);
			}
			if (s.Gas > Plugin.PedalThreshold.Value)
			{
				__instance.drivePedalPressed = true;
			}
			if (s.Brake > Plugin.PedalThreshold.Value)
			{
				__instance.brakePedalPressed = true;
			}
		}
	}
	[HarmonyPatch(typeof(VehicleController), "Update")]
	internal static class GearPatch
	{
		private static bool _active;

		private static float _neutralTime;

		private static bool _neutralDone;

		private static readonly Dictionary<string, bool> _prev = new Dictionary<string, bool>();

		private static IEnumerable<string> Split(string list)
		{
			if (string.IsNullOrWhiteSpace(list))
			{
				yield break;
			}
			string[] array = list.Split(',');
			for (int i = 0; i < array.Length; i++)
			{
				string text = array[i].Trim();
				if (text.Length > 0)
				{
					yield return text;
				}
			}
		}

		private static bool Any(InputDevice dev, string list, bool edge, string exclude = null)
		{
			foreach (string item in Split(list))
			{
				if (!string.IsNullOrWhiteSpace(exclude) && string.Equals(item, exclude.Trim(), StringComparison.OrdinalIgnoreCase))
				{
					continue;
				}
				ButtonControl val = Plugin.Button(dev, item);
				if (val == null)
				{
					continue;
				}
				bool isPressed = val.isPressed;
				if (!edge)
				{
					if (isPressed)
					{
						return true;
					}
					continue;
				}
				_prev.TryGetValue(item, out var value);
				if (isPressed && !value)
				{
					return true;
				}
			}
			return false;
		}

		private static void Snapshot(InputDevice dev)
		{
			_prev.Clear();
			string[] array = new string[7]
			{
				Plugin.ReverseButton.Value,
				Plugin.DriveButtons.Value,
				Plugin.ParkButton.Value,
				Plugin.ShiftUpPath.Value,
				Plugin.ShiftDownPath.Value,
				Plugin.JumpButton.Value,
				Plugin.KeyButton.Value
			};
			for (int i = 0; i < array.Length; i++)
			{
				foreach (string item in Split(array[i]))
				{
					ButtonControl val = Plugin.Button(dev, item);
					_prev[item] = val != null && val.isPressed;
				}
			}
		}

		private static void Postfix(VehicleController __instance)
		{
			if (!__instance.localPlayerInControl)
			{
				_active = false;
			}
			else
			{
				if (Plugin.Mode.Value.ToLowerInvariant() != "direct")
				{
					return;
				}
				InputDevice val = Plugin.FindDevice();
				if (val == null || Plugin.IsStaleDefaultState(val))
				{
					_active = false;
					return;
				}
				int value = Plugin.GearDriveId.Value;
				int value2 = Plugin.GearReverseId.Value;
				int value3 = Plugin.GearParkId.Value;
				int num = Convert.ToInt32(Traverse.Create((object)__instance).Field("gear").GetValue());
				string value4 = Plugin.ParkButton.Value;
				bool flag = Any(val, Plugin.ReverseButton.Value, edge: false);
				bool flag2 = Any(val, Plugin.DriveButtons.Value, edge: false, value4);
				bool flag3 = Any(val, value4, edge: false);
				int num2 = 0;
				bool active = _active;
				if (!active)
				{
					_active = true;
					_neutralTime = 0f;
					_neutralDone = false;
					if (flag3)
					{
						num2 = value3;
					}
					else if (flag)
					{
						num2 = value2;
					}
					else if (flag2)
					{
						num2 = value;
					}
				}
				else
				{
					if (Any(val, value4, edge: true))
					{
						num2 = value3;
					}
					else if (Any(val, Plugin.ReverseButton.Value, edge: true))
					{
						num2 = value2;
					}
					else if (Any(val, Plugin.DriveButtons.Value, edge: true, value4))
					{
						num2 = value;
					}
					if (flag || flag2 || flag3)
					{
						_neutralTime = 0f;
						_neutralDone = false;
					}
					else if (Plugin.NeutralParkSeconds.Value > 0f && !_neutralDone)
					{
						_neutralTime += Time.deltaTime;
						if (_neutralTime >= Plugin.NeutralParkSeconds.Value)
						{
							num2 = value3;
							_neutralDone = true;
						}
					}
					if (Any(val, Plugin.ShiftUpPath.Value, edge: true))
					{
						num2 = ((num == value3) ? value2 : ((num == value2) ? value : num));
					}
					if (Any(val, Plugin.ShiftDownPath.Value, edge: true))
					{
						num2 = ((num == value) ? value2 : ((num == value2) ? value3 : num));
					}
				}
				if (num2 != 0 && num2 != num)
				{
					Plugin.Log("[gear] " + num + " -> " + num2);
					__instance.ShiftToGearAndSync(num2);
				}
				if (active && Any(val, Plugin.JumpButton.Value, edge: true))
				{
					Plugin.Log("[jump] button pressed");
					DoJump(__instance);
				}
				if (active && Any(val, Plugin.KeyButton.Value, edge: true))
				{
					Plugin.Log("[key] button pressed");
					DoKey(__instance);
				}
				Snapshot(val);
			}
		}

		private static void DoJump(VehicleController car)
		{
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			Traverse val = Traverse.Create((object)car);
			if (val.Field("jumpingInCar").GetValue<bool>())
			{
				Plugin.Log("[jump] skipped: already jumping");
				return;
			}
			if (val.Field("keyIsInDriverHand").GetValue<bool>())
			{
				Plugin.Log("[jump] skipped: key animation playing");
				return;
			}
			float num = (car.drivePedalPressed ? 1f : (car.brakePedalPressed ? (-1f) : 0f));
			car.UseTurboBoostLocalClient(new Vector2(0f, num));
			car.UseTurboBoostServerRpc();
		}

		private static void DoKey(VehicleController car)
		{
			Traverse val = Traverse.Create((object)car);
			if (val.Field("keyIgnitionCoroutine").GetValue() != null)
			{
				Plugin.Log("[key] skipped: key animation still playing");
			}
			else if (val.Field("ignitionStarted").GetValue<bool>())
			{
				car.RemoveKeyFromIgnition();
			}
			else
			{
				car.StartTryCarIgnition();
			}
		}
	}
}