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.1.0.0")]
[assembly: AssemblyInformationalVersion("1.1.0+c15e6ab93ffda4883caf3bb16ddc00da524b25d8")]
[assembly: AssemblyProduct("AutoPilot")]
[assembly: AssemblyTitle("com.kerseb.AutoPilot")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.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 AutoPilot
{
[BepInPlugin("com.kerseb.AutoPilot", "AutoPilot", "1.1.0")]
[BepInDependency("com.app24.sailwindmoddinghelper", "2.1.1")]
public class AutoPilotMain : BaseUnityPlugin
{
internal static ManualLogSource Logger;
internal static ConfigEntry<KeyboardShortcut> activateAutopilotCourse;
internal static ConfigEntry<KeyboardShortcut> activateAutopilotWind;
internal static ConfigEntry<KeyboardShortcut> leftAutopilot;
internal static ConfigEntry<KeyboardShortcut> rightAutopilot;
internal static ConfigEntry<KeyboardShortcut> tackJibeKey;
internal static ConfigEntry<float> angleAdjustStep;
internal static ConfigEntry<float> kp;
internal static ConfigEntry<float> ki;
internal static ConfigEntry<float> kd;
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_0098: Unknown result type (might be due to invalid IL or missing references)
//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
//IL_0184: Unknown result type (might be due to invalid IL or missing references)
//IL_018a: Expected O, but got Unknown
Logger = ((BaseUnityPlugin)this).Logger;
activateAutopilotCourse = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Activate Autopilot Key (compass)", new KeyboardShortcut((KeyCode)111, Array.Empty<KeyCode>()), (ConfigDescription)null);
activateAutopilotWind = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Activate Autopilot Key (wind)", new KeyboardShortcut((KeyCode)112, 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);
tackJibeKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Auto tack/jibe to opposite side", new KeyboardShortcut((KeyCode)59, Array.Empty<KeyCode>()), (ConfigDescription)null);
angleAdjustStep = ((BaseUnityPlugin)this).Config.Bind<float>("Controls", "AngleStep", 5f, "Degrees to adjust per key press");
kp = ((BaseUnityPlugin)this).Config.Bind<float>("Controls", "kp", 0.03f, "PID Costant: Proportional gain");
ki = ((BaseUnityPlugin)this).Config.Bind<float>("Controls", "ki", 0.005f, "PID Costant: Integral gain");
kd = ((BaseUnityPlugin)this).Config.Bind<float>("Controls", "kd", 0.015f, "PID Costant: Derivative gain");
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)
{
if (!((Object)(object)__instance == (Object)null))
{
((Component)__instance).gameObject.AddComponent<AutoPilotSteerage>();
}
}
}
public class AutoPilotSteerage : MonoBehaviour
{
private Transform boat;
private HingeJoint rudderJoint;
private Rudder rudder;
private GPButtonSteeringWheel steeringWheel;
private Rigidbody shipRigidbody;
private bool autopilotActiveCourse = false;
private bool autopilotActiveWind = false;
private float autopilotCourse;
private bool activated = false;
private float currentInputMax;
public bool allowUserInput = true;
public bool canControl = false;
private float integral;
private float lastError;
private float lastTime;
private bool autoTacking = false;
public void Awake()
{
//IL_0053: Unknown result type (might be due to invalid IL or missing references)
//IL_005d: Expected O, but got Unknown
rudder = ((Component)this).GetComponent<Rudder>();
if (!Object.op_Implicit((Object)(object)rudder))
{
return;
}
GPButtonSteeringWheel[] array = Object.FindObjectsOfType<GPButtonSteeringWheel>();
GPButtonSteeringWheel[] array2 = array;
foreach (GPButtonSteeringWheel val in array2)
{
if ((Object)(object)rudder == (Object)(Rudder)Traverse.Create((object)val).Field("rudder").GetValue())
{
steeringWheel = val;
boat = ((Component)((Component)this).GetComponentInParent<PurchasableBoat>()).transform;
rudderJoint = val.attachedRudder;
shipRigidbody = ((Component)boat).GetComponent<Rigidbody>();
}
}
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_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_00a2: Unknown result type (might be due to invalid IL or missing references)
//IL_00a7: Unknown result type (might be due to invalid IL or missing references)
//IL_0110: 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)
//IL_018d: Unknown result type (might be due to invalid IL or missing references)
//IL_0192: Unknown result type (might be due to invalid IL or missing references)
if (allowUserInput)
{
KeyboardShortcut value = AutoPilotMain.activateAutopilotCourse.Value;
if (((KeyboardShortcut)(ref value)).IsDown())
{
autopilotActiveCourse = !autopilotActiveCourse;
if (autopilotActiveWind)
{
autopilotActiveWind = false;
activated = false;
}
}
value = AutoPilotMain.activateAutopilotWind.Value;
if (((KeyboardShortcut)(ref value)).IsDown())
{
autopilotActiveWind = !autopilotActiveWind;
if (autopilotActiveCourse)
{
autopilotActiveCourse = false;
activated = false;
}
}
if (!autoTacking)
{
value = AutoPilotMain.leftAutopilot.Value;
if (((KeyboardShortcut)(ref value)).IsDown())
{
autopilotCourse -= AutoPilotMain.angleAdjustStep.Value;
autopilotCourse = ((autopilotCourse < -180f) ? (180f - Mathf.Repeat(0f - autopilotCourse, 180f)) : autopilotCourse);
}
}
if (!autoTacking)
{
value = AutoPilotMain.rightAutopilot.Value;
if (((KeyboardShortcut)(ref value)).IsDown())
{
autopilotCourse += AutoPilotMain.angleAdjustStep.Value;
autopilotCourse = ((autopilotCourse > 180f) ? (-180f + Mathf.Repeat(autopilotCourse, 180f)) : autopilotCourse);
}
}
if ((autopilotActiveCourse || autopilotActiveWind) && canControl)
{
value = AutoPilotMain.tackJibeKey.Value;
if (((KeyboardShortcut)(ref value)).IsDown() && !autoTacking)
{
InitiateAutoTackJibe();
}
}
}
};
}
public void Update()
{
//IL_01d4: Unknown result type (might be due to invalid IL or missing references)
//IL_01d9: Unknown result type (might be due to invalid IL or missing references)
if (!Object.op_Implicit((Object)(object)rudder) || !Object.op_Implicit((Object)(object)steeringWheel))
{
return;
}
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() < 0f) ? (BoatHeading() + 360f) : BoatHeading());
GPButtonSteeringWheel obj2 = steeringWheel;
((GoPointerButton)obj2).description = ((GoPointerButton)obj2).description + "\n Autopilot: " + Mathf.RoundToInt(autopilotCourse);
GPButtonSteeringWheel obj3 = steeringWheel;
((GoPointerButton)obj3).description = ((GoPointerButton)obj3).description + "\n Mode: " + (autopilotActiveCourse ? "Compass" : (autopilotActiveWind ? "Wind" : "Off"));
}
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 ((autopilotActiveCourse || autopilotActiveWind) && canControl)
{
if (!activated)
{
JointLimits limits = rudderJoint.limits;
currentInputMax = ((JointLimits)(ref limits)).max * steeringWheel.gearRatio;
if (autopilotActiveCourse)
{
autopilotCourse = BoatHeading();
}
else if (autopilotActiveWind)
{
autopilotCourse = GetApparentWindAngle();
}
activated = true;
}
if (!(bool)Traverse.Create((object)steeringWheel).Field("locked").GetValue())
{
Traverse.Create((object)steeringWheel).Field("locked").SetValue((object)true);
}
if (autopilotActiveWind || autoTacking)
{
ApplySteering(GetApparentWindAngle(), autopilotCourse);
}
else if (autopilotActiveCourse)
{
ApplySteering(BoatHeading(), autopilotCourse);
}
}
else if (activated)
{
activated = false;
}
}
private float AngleCorrection(float angle)
{
angle = Mathf.Clamp(angle, -180f, 180f);
return angle;
}
private void ApplySteering(float currentAngle, float targetAngle)
{
if (autoTacking && Mathf.Abs(currentAngle - autopilotCourse) <= 1f)
{
autopilotCourse = (autopilotActiveCourse ? BoatHeading() : autopilotCourse);
autoTacking = false;
integral = 0f;
}
float num = Mathf.DeltaAngle(targetAngle, currentAngle);
float num2 = Time.time - lastTime;
if (num2 > 0.05f)
{
float num3 = num;
integral += num * num2;
integral = Mathf.Clamp(integral, -50f, 50f);
float num4 = (num - lastError) / num2;
float num5 = AutoPilotMain.kp.Value * num3 + AutoPilotMain.ki.Value * integral + AutoPilotMain.kd.Value * num4;
num5 = Mathf.Clamp(num5, -1f, 1f);
steeringWheel.currentInput = currentInputMax * num5;
lastError = num;
lastTime = Time.time;
}
}
private void InitiateAutoTackJibe()
{
autopilotCourse = (autopilotActiveWind ? autopilotCourse : (0f - GetApparentWindAngle()));
autoTacking = true;
}
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 angle = Vector3.SignedAngle(boat.forward, Vector3.forward, -Vector3.up);
return AngleCorrection(angle);
}
public void ToggleAutoPilotWind()
{
autopilotActiveWind = !autopilotActiveWind;
autopilotActiveCourse = autopilotActiveCourse;
}
public void ToggleAutoPilotCourse()
{
autopilotActiveCourse = !autopilotActiveCourse;
autopilotActiveWind = autopilotActiveWind;
}
public void SetCourse(float course)
{
autopilotCourse = course;
}
private float GetApparentWindAngle()
{
//IL_0002: Unknown result type (might be due to invalid IL or missing references)
//IL_000d: Unknown result type (might be due to invalid IL or missing references)
//IL_0012: Unknown result type (might be due to invalid IL or missing references)
//IL_0017: Unknown result type (might be due to invalid IL or missing references)
//IL_001e: 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_0029: Unknown result type (might be due to invalid IL or missing references)
//IL_002e: Unknown result type (might be due to invalid IL or missing references)
try
{
Vector3 val = Wind.currentWind - shipRigidbody.velocity;
float angle = Vector3.SignedAngle(-shipRigidbody.velocity, val, -Vector3.up);
return AngleCorrection(angle);
}
catch (Exception arg)
{
AutoPilotMain.Logger.LogError((object)$"GetApparentWindAngle error: {arg}");
return 0f;
}
}
}
public static class MyPluginInfo
{
public const string PLUGIN_GUID = "com.kerseb.AutoPilot";
public const string PLUGIN_NAME = "AutoPilot";
public const string PLUGIN_VERSION = "1.1.0";
}
}