Decompiled source of AutoSails v1.0.1

AutoSails.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.AutoSails")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.1.0")]
[assembly: AssemblyInformationalVersion("1.0.1+d8c19a9925e00449f5048059c28d6e1c00052e5c")]
[assembly: AssemblyProduct("AutoSails")]
[assembly: AssemblyTitle("com.kerseb.AutoSails")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.1.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 AutoSails
{
	[BepInPlugin("com.kerseb.AutoSails", "AutoSails", "1.0.1")]
	public class AutoSailsMain : BaseUnityPlugin
	{
		internal static ManualLogSource Logger;

		internal static ConfigEntry<KeyboardShortcut> hoistSails;

		internal static ConfigEntry<KeyboardShortcut> trimSails;

		public void Awake()
		{
			//IL_0033: 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_006f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Expected O, but got Unknown
			Logger = ((BaseUnityPlugin)this).Logger;
			Logger.LogInfo((object)"Plugin com.kerseb.AutoSails is loaded!");
			hoistSails = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Hoist Sails Key", new KeyboardShortcut((KeyCode)105, Array.Empty<KeyCode>()), (ConfigDescription)null);
			trimSails = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "Trim Sails Key", new KeyboardShortcut((KeyCode)106, Array.Empty<KeyCode>()), (ConfigDescription)null);
			Harmony val = new Harmony("com.kerseb.AutoSails");
			val.PatchAll();
		}
	}
	[HarmonyPatch(typeof(GPButtonRopeWinch), "Awake")]
	public class AutoSailsPatches
	{
		private static void Postfix(GPButtonRopeWinch __instance)
		{
			if (!((Object)(object)__instance == (Object)null))
			{
				((Component)__instance).gameObject.AddComponent<AutoSailsControlSail>();
			}
		}
	}
	public class AutoSailsControlSail : MonoBehaviour
	{
		public bool canControl = false;

		private PurchasableBoat boat;

		private Sail sailComponent;

		private GPButtonRopeWinch ropeWinch;

		public bool hoistSails = false;

		public bool trimSails = false;

		public bool hoisted = false;

		private float hoistingSpeed = 0.005f;

		private float trimmingSpeed = 0.0005f;

		private bool reverseReefing;

		private float trimDirection = 1f;

		private float oldEfficiency = 1f;

		private int i = 0;

		public void Awake()
		{
			ropeWinch = ((Component)this).GetComponentInParent<GPButtonRopeWinch>();
			if ((Object)(object)ropeWinch == (Object)null)
			{
				return;
			}
			GameEvents.OnPlayerInput += delegate
			{
				//IL_0006: Unknown result type (might be due to invalid IL or missing references)
				//IL_000b: Unknown result type (might be due to invalid IL or missing references)
				//IL_002d: 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)
				KeyboardShortcut value = AutoSailsMain.hoistSails.Value;
				if (((KeyboardShortcut)(ref value)).IsDown())
				{
					hoistSails = !hoistSails;
				}
				value = AutoSailsMain.trimSails.Value;
				if (((KeyboardShortcut)(ref value)).IsDown())
				{
					trimSails = !trimSails;
				}
			};
		}

		private float SailEfficiency()
		{
			float num = (float)Traverse.Create((object)sailComponent).Field("unamplifiedForwardForce").GetValue();
			float totalForce = GetTotalForce();
			return num / totalForce * 100f;
		}

		private float SailInefficiency()
		{
			float num = (float)Traverse.Create((object)sailComponent).Field("unamplifiedSidewayForce").GetValue();
			float totalForce = GetTotalForce();
			return Mathf.Abs(num / totalForce * 100f);
		}

		private float CombinedEfficiency()
		{
			float num = SailEfficiency();
			if (num <= 0f)
			{
				return num;
			}
			float num2 = 100f - SailInefficiency();
			return (3f * num + num2) / 4f;
		}

		private float GetTotalForce()
		{
			float appliedWindForce = sailComponent.appliedWindForce;
			if (appliedWindForce == 0f)
			{
				return (float)Traverse.Create((object)sailComponent).Field("totalWindForce").GetValue();
			}
			return appliedWindForce / sailComponent.GetCapturedForceFraction();
		}

		private void FixedUpdate()
		{
			if (!((Object)(object)ropeWinch == (Object)null) && ropeWinch.rope is RopeControllerSailReef)
			{
				reverseReefing = (bool)Traverse.Create((object)ropeWinch.rope).Field("reverseReefing").GetValue();
			}
		}

		private void Update()
		{
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Expected O, but got Unknown
			//IL_015e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0168: Expected O, but got Unknown
			if ((Object)(object)ropeWinch == (Object)null)
			{
				return;
			}
			if ((Object)(object)boat == (Object)null)
			{
				boat = (PurchasableBoat)Traverse.Create((object)ropeWinch).Field("boat").GetValue();
			}
			if ((Object)(object)GameState.currentBoat != (Object)null)
			{
				canControl = GameState.currentBoat.IsChildOf(((Component)boat).transform) && boat.isPurchased();
			}
			else if ((Object)(object)GameState.lastBoat != (Object)null)
			{
				canControl = GameState.lastBoat.IsChildOf(((Component)boat).transform) && boat.isPurchased();
			}
			else
			{
				canControl = false;
			}
			if ((Object)(object)ropeWinch.rope == (Object)null)
			{
				return;
			}
			if ((Object)(object)sailComponent == (Object)null && (ropeWinch.rope is RopeControllerSailAngle || ropeWinch.rope is RopeControllerSailAngleSquare || ropeWinch.rope is RopeControllerSailAngleJib))
			{
				sailComponent = (Sail)Traverse.Create((object)ropeWinch.rope).Field("sail").GetValue();
			}
			if (ropeWinch.rope is RopeControllerSailReef)
			{
				if (!hoistSails || !canControl)
				{
					return;
				}
				if (hoisted)
				{
					if (reverseReefing)
					{
						HoistUp();
					}
					else
					{
						HoistDown();
					}
				}
				else if (reverseReefing)
				{
					HoistDown();
				}
				else
				{
					HoistUp();
				}
			}
			else if (ropeWinch.rope is RopeControllerSailAngle)
			{
				if (!trimSails || !canControl)
				{
					return;
				}
				Traverse.Create((object)ropeWinch).Field("currentInput").SetValue((object)((0f - trimDirection) * 5f));
				ropeWinch.ApplyRotation();
				if (i == 20)
				{
					i = 0;
					if (oldEfficiency > CombinedEfficiency())
					{
						trimDirection *= -1f;
					}
					oldEfficiency = CombinedEfficiency();
				}
				if (CombinedEfficiency() == 0f)
				{
					trimDirection = -1f;
					i = 0;
				}
				if (ropeWinch.rope.currentLength >= 1f)
				{
					RopeController rope = ropeWinch.rope;
					rope.currentLength -= trimmingSpeed;
				}
				else if (ropeWinch.rope.currentLength <= 0f)
				{
					RopeController rope2 = ropeWinch.rope;
					rope2.currentLength += trimmingSpeed;
				}
				else
				{
					RopeController rope3 = ropeWinch.rope;
					rope3.currentLength += trimDirection * trimmingSpeed;
				}
				ropeWinch.rope.changed = true;
				i++;
			}
			else if (ropeWinch.rope is RopeControllerSailAngleSquare)
			{
				if (trimSails && !canControl)
				{
				}
			}
			else if (ropeWinch.rope is RopeControllerSailAngleJib && trimSails && !canControl)
			{
			}
		}

		private void HoistDown()
		{
			if (ropeWinch.rope.currentLength > 0f)
			{
				Traverse.Create((object)ropeWinch).Field("currentInput").SetValue((object)25f);
				ropeWinch.ApplyRotation();
				float currentLength = ropeWinch.rope.currentLength;
				RopeController rope = ropeWinch.rope;
				rope.currentLength -= hoistingSpeed;
				if (currentLength == ropeWinch.rope.currentLength || ropeWinch.rope.currentLength < 0f)
				{
					ropeWinch.rope.currentLength = 0f;
					hoistSails = false;
					hoisted = !hoisted;
				}
			}
		}

		private void HoistUp()
		{
			if (ropeWinch.rope.currentLength < 1f)
			{
				Traverse.Create((object)ropeWinch).Field("currentInput").SetValue((object)(-25f));
				ropeWinch.ApplyRotation();
				float currentLength = ropeWinch.rope.currentLength;
				RopeController rope = ropeWinch.rope;
				rope.currentLength += hoistingSpeed;
				if (currentLength == ropeWinch.rope.currentLength || ropeWinch.rope.currentLength > 1f)
				{
					ropeWinch.rope.currentLength = 1f;
					hoistSails = false;
					hoisted = !hoisted;
				}
			}
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "com.kerseb.AutoSails";

		public const string PLUGIN_NAME = "AutoSails";

		public const string PLUGIN_VERSION = "1.0.1";
	}
}