Decompiled source of AutoPilot v1.0.3

AutoPilot.dll

Decompiled 2 days ago
using System;
using System.Diagnostics;
using System.Reflection;
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 Microsoft.CodeAnalysis;
using SailwindModdingHelper;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("com.kerseb.AutoPilot")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.3.0")]
[assembly: AssemblyInformationalVersion("1.0.3+5f744032013abfc4939c816af8460d28fd30ff94")]
[assembly: AssemblyProduct("AutoPilot")]
[assembly: AssemblyTitle("com.kerseb.AutoPilot")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.3.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 AutoPilot
{
	[BepInPlugin("com.kerseb.AutoPilot", "AutoPilot", "1.0.3")]
	[BepInDependency("com.app24.sailwindmoddinghelper", "2.1.1")]
	public class AutoPilotMain : BaseUnityPlugin
	{
		internal static ManualLogSource Logger;

		internal static ConfigEntry<KeyboardShortcut> activateAutopilot;

		internal static ConfigEntry<KeyboardShortcut> leftAutopilot;

		internal static ConfigEntry<KeyboardShortcut> rightAutopilot;

		public static ConfigEntry<bool> rudderHUDConfig;

		private void Awake()
		{
			//IL_0023: 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_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			activateAutopilot = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Activate Autopilot Key", new KeyboardShortcut((KeyCode)111, Array.Empty<KeyCode>()), (ConfigDescription)null);
			leftAutopilot = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Left Autopilot Key", new KeyboardShortcut((KeyCode)107, Array.Empty<KeyCode>()), (ConfigDescription)null);
			rightAutopilot = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Right Autopilot Key", new KeyboardShortcut((KeyCode)108, Array.Empty<KeyCode>()), (ConfigDescription)null);
			rudderHUDConfig = ((BaseUnityPlugin)this).Config.Bind<bool>("HUD", "rudderHUD", false, "Enables or disables the rudder HUD. Requires restarting the game. For Debug Purpose");
			Harmony val = new Harmony("com.kerseb.AutoPilot");
			val.PatchAll();
		}
	}
	[HarmonyPatch(typeof(Rudder), "Start")]
	public class AutoPilotPatches
	{
		public static void Postfix(Rudder __instance)
		{
			BoatHorizon componentInParent = ((Component)__instance).GetComponentInParent<BoatHorizon>();
			Transform val = ((componentInParent != null) ? ((Component)componentInParent).transform : null);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			Transform val2;
			Transform val3;
			if (((Object)val).name == "cutterModel")
			{
				val2 = ((Component)__instance).transform.Find("rudder_tiller_cutter");
				val3 = ((Component)__instance).transform.Find("rudder_tiller_cutter_center");
			}
			else
			{
				if (!(((Object)val).name == "paraw"))
				{
					GPButtonSteeringWheel[] componentsInChildren = ((Component)val).GetComponentsInChildren<GPButtonSteeringWheel>();
					GPButtonSteeringWheel[] array = componentsInChildren;
					foreach (GPButtonSteeringWheel val4 in array)
					{
						((Component)val4).gameObject.AddComponent<AutoPilotSteerage>();
					}
					return;
				}
				val2 = val.Find("hull_regular").Find("rudder_right_reg").Find("tiller_reg");
				val3 = val.Find("hull_extension").Find("rudder_right_ext").Find("tiller_ext");
			}
			((Component)val2).gameObject.AddComponent<AutoPilotSteerage>();
			((Component)val3).gameObject.AddComponent<AutoPilotSteerage>();
		}
	}
	public class AutoPilotSteerage : MonoBehaviour
	{
		private Transform boat;

		private HingeJoint rudderJoint;

		private Rudder rudder;

		private GPButtonSteeringWheel steeringWheel;

		private bool autopilotActive = false;

		private float autopilotCourse;

		private bool activated = false;

		private float threshold = 15f;

		private float currentInputMax;

		private float headingDifference;

		public bool allowUserInput = true;

		public bool canControl = false;

		public void Awake()
		{
			boat = ((Component)((Component)this).GetComponentInParent<PurchasableBoat>()).transform;
			rudder = ((Component)boat).GetComponentInChildren<Rudder>();
			rudderJoint = ((Component)rudder).GetComponent<HingeJoint>();
			steeringWheel = ((Component)boat).GetComponentInChildren<GPButtonSteeringWheel>();
			GameEvents.OnPlayerInput += delegate
			{
				//IL_0014: Unknown result type (might be due to invalid IL or missing references)
				//IL_0019: Unknown result type (might be due to invalid IL or missing references)
				//IL_003b: 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_006b: Unknown result type (might be due to invalid IL or missing references)
				//IL_0070: Unknown result type (might be due to invalid IL or missing references)
				if (allowUserInput)
				{
					KeyboardShortcut value = AutoPilotMain.activateAutopilot.Value;
					if (((KeyboardShortcut)(ref value)).IsDown())
					{
						autopilotActive = !autopilotActive;
					}
					value = AutoPilotMain.leftAutopilot.Value;
					if (((KeyboardShortcut)(ref value)).IsDown())
					{
						autopilotCourse = angleCorrection(autopilotCourse - 5f);
					}
					value = AutoPilotMain.rightAutopilot.Value;
					if (((KeyboardShortcut)(ref value)).IsDown())
					{
						autopilotCourse = angleCorrection(autopilotCourse + 5f);
					}
				}
			};
		}

		public void Update()
		{
			//IL_0147: Unknown result type (might be due to invalid IL or missing references)
			//IL_014c: Unknown result type (might be due to invalid IL or missing references)
			if (AutoPilotMain.rudderHUDConfig.Value && (((GoPointerButton)steeringWheel).IsLookedAt() || ((GoPointerButton)steeringWheel).IsStickyClicked() || ((GoPointerButton)steeringWheel).IsCliked()))
			{
				((GoPointerButton)steeringWheel).description = "";
				GPButtonSteeringWheel obj = steeringWheel;
				((GoPointerButton)obj).description = ((GoPointerButton)obj).description + "\n BoatHeading: " + Mathf.RoundToInt(BoatHeading());
				GPButtonSteeringWheel obj2 = steeringWheel;
				((GoPointerButton)obj2).description = ((GoPointerButton)obj2).description + "\n Autopilot: " + Mathf.RoundToInt(autopilotCourse);
			}
			if ((Object)(object)GameState.currentBoat != (Object)null)
			{
				canControl = GameState.currentBoat.IsChildOf(((Component)boat).transform);
			}
			else if ((Object)(object)GameState.lastBoat != (Object)null)
			{
				canControl = GameState.lastBoat.IsChildOf(((Component)boat).transform);
			}
			else
			{
				canControl = false;
			}
			if (autopilotActive && canControl)
			{
				if (!activated)
				{
					JointLimits limits = rudderJoint.limits;
					currentInputMax = ((JointLimits)(ref limits)).max * steeringWheel.gearRatio;
					autopilotCourse = BoatHeading();
					activated = true;
				}
				if (!(bool)Traverse.Create((object)steeringWheel).Field("locked").GetValue())
				{
					Traverse.Create((object)steeringWheel).Field("locked").SetValue((object)true);
				}
				headingDifference = autopilotCourse - BoatHeading();
				if (headingDifference > 180f)
				{
					headingDifference -= 360f;
				}
				else if (headingDifference <= -180f)
				{
					headingDifference += 360f;
				}
				if (headingDifference >= 0f)
				{
					if (Mathf.Abs(headingDifference) > threshold)
					{
						steeringWheel.currentInput = 0f - currentInputMax;
					}
					else
					{
						steeringWheel.currentInput = 0f - smoothSteering(headingDifference);
					}
				}
				else if (Mathf.Abs(headingDifference) > threshold)
				{
					steeringWheel.currentInput = currentInputMax;
				}
				else
				{
					steeringWheel.currentInput = smoothSteering(headingDifference);
				}
			}
			else if (activated)
			{
				activated = false;
			}
		}

		private float angleCorrection(float angle)
		{
			angle = (angle % 360f + 360f) % 360f;
			return angle;
		}

		private float smoothSteering(float headingDifference)
		{
			headingDifference = Mathf.Abs(headingDifference);
			float num = Mathf.Max(headingDifference, 0.001f);
			return currentInputMax * Mathf.Log10(1f + num) / Mathf.Log10(181f);
		}

		private float BoatHeading()
		{
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			float num = Vector3.SignedAngle(boat.forward, Vector3.forward, -Vector3.up);
			return (num < 0f) ? (num + 360f) : num;
		}

		public void toggleAutoPilot()
		{
			autopilotActive = !autopilotActive;
		}

		public void setCourse(float course)
		{
			autopilotCourse = course;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "com.kerseb.AutoPilot";

		public const string PLUGIN_NAME = "AutoPilot";

		public const string PLUGIN_VERSION = "1.0.3";
	}
}