Decompiled source of AnchorImprovements v1.1.1

AnchorImprovements.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
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: AssemblyTitle("AnchorRework")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("AnchorRework")]
[assembly: AssemblyCopyright("Copyright ©  2023")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("1b4ae26a-cac3-415c-8208-6a87974cddf7")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8.1", FrameworkDisplayName = ".NET Framework 4.8.1")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace AnchorRework;

[BepInPlugin("com.nandbrew.anchorimprovements", "Anchor Improvements", "1.1.1")]
[BepInDependency("com.app24.sailwindmoddinghelper", "2.0.3")]
internal class Main : BaseUnityPlugin
{
	public const string GUID = "com.nandbrew.anchorimprovements";

	public const string NAME = "Anchor Improvements";

	public const string VERSION = "1.1.1";

	internal static Main instance;

	internal static ManualLogSource logSource;

	internal static ConfigEntry<string> simplePhysics;

	public void Awake()
	{
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Expected O, but got Unknown
		logSource = ((BaseUnityPlugin)this).Logger;
		Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "com.nandbrew.anchorimprovements");
		simplePhysics = ((BaseUnityPlugin)this).Config.Bind<string>("Options", "Anchor mechanics", "Simple", new ConfigDescription("Simple: Normal with minor improvements \nRealistic: Holding power based on scope (angle)", (AcceptableValueBase)(object)new AcceptableValueList<string>(new string[2] { "Simple", "Realistic" }), Array.Empty<object>()));
	}
}
internal class Patches
{
	[HarmonyPatch(typeof(Anchor))]
	private static class AnchorPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("Start")]
		public static void StartPatch2(Anchor __instance, ref float ___initialMass, AudioSource ___audio, ref float ___anchorDrag, ConfigurableJoint ___joint)
		{
			//IL_001a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0036: Expected O, but got Unknown
			//IL_004c: 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)
			((Component)__instance).gameObject.layer = 0;
			((Component)__instance).gameObject.AddComponent<PickupableBoatAnchor>();
			GameObject val = Object.Instantiate<GameObject>(new GameObject
			{
				name = "anchor_stock"
			}, ((Component)__instance).transform);
			val.transform.localPosition = new Vector3(0f, 0f, -0.28f);
			CapsuleCollider val2 = val.AddComponent<CapsuleCollider>();
			val2.radius = 0.05f;
			val2.height = 0.6f;
			if (((Object)((Joint)___joint).connectedBody).name.Contains("medi medium"))
			{
				((Component)__instance).transform.localScale = new Vector3(1.25f, 1.25f, 1.25f);
			}
			___anchorDrag += 5f;
			___audio.maxDistance *= 3f;
			if (___initialMass == 1f)
			{
				___initialMass = 75f;
			}
		}
	}

	[HarmonyPatch(typeof(Anchor))]
	private static class AnchorPatchesComplex
	{
		private static float Power(float angle)
		{
			return Mathf.Pow(angle, 2f) / 64f;
		}

		[HarmonyPrefix]
		[HarmonyPatch("FixedUpdate")]
		public static bool FixedUpdatePatch(Anchor __instance, ConfigurableJoint ___joint, ref float ___unsetForce, AudioSource ___audio, ref float ___lastLength, ref bool ___grounded, Rigidbody ___body, float ___anchorDrag, float ___initialMass, ref float ___outCurrentForce)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_005e: 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_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0075: Unknown result type (might be due to invalid IL or missing references)
			//IL_0077: Unknown result type (might be due to invalid IL or missing references)
			//IL_0079: Unknown result type (might be due to invalid IL or missing references)
			//IL_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			//IL_00df: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_011b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0144: Unknown result type (might be due to invalid IL or missing references)
			//IL_0149: Unknown result type (might be due to invalid IL or missing references)
			//IL_017c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0181: Unknown result type (might be due to invalid IL or missing references)
			//IL_0338: Unknown result type (might be due to invalid IL or missing references)
			//IL_033d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0349: Unknown result type (might be due to invalid IL or missing references)
			//IL_034e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0264: Unknown result type (might be due to invalid IL or missing references)
			//IL_0206: Unknown result type (might be due to invalid IL or missing references)
			//IL_020b: Unknown result type (might be due to invalid IL or missing references)
			if (Main.simplePhysics.Value != "Realistic")
			{
				return true;
			}
			SoftJointLimit linearLimit = ___joint.linearLimit;
			Vector3 val;
			if (((SoftJointLimit)(ref linearLimit)).limit < 1f)
			{
				___body.mass = 1f;
			}
			else
			{
				___body.mass = ___initialMass;
				Vector3 position = ((Component)___joint).transform.position;
				Vector3 position2 = ((Component)__instance).GetComponent<PickupableBoatAnchor>().GetTopAttach().position;
				float num = Vector3.Angle(position2 - position, ((Component)___joint).transform.root.up);
				float num2 = ((!(num < 90f)) ? Power((0f - num) % 90f) : Power(num));
				___unsetForce = ___initialMass * num2;
				SoftJointLimitSpring linearLimitSpring = ___joint.linearLimitSpring;
				linearLimit = ___joint.linearLimit;
				((SoftJointLimitSpring)(ref linearLimitSpring)).spring = Mathf.Max(5000f - (num2 + ((SoftJointLimit)(ref linearLimit)).limit) * 20f, ___initialMass * 10f);
				((SoftJointLimitSpring)(ref linearLimitSpring)).damper = ((SoftJointLimitSpring)(ref linearLimitSpring)).spring;
				___joint.linearLimitSpring = linearLimitSpring;
				SoftJointLimit linearLimit2 = ___joint.linearLimit;
				((SoftJointLimit)(ref linearLimit2)).contactDistance = (num2 + ((SoftJointLimit)(ref linearLimit2)).limit) / 30f;
				___joint.linearLimit = linearLimit2;
				val = ((Joint)___joint).currentForce;
				if (((Vector3)(ref val)).magnitude > ___unsetForce && !___audio.isPlaying)
				{
					Extensions.InvokePrivateMethod((object)__instance, "ReleaseAnchor", Array.Empty<object>());
				}
				linearLimit = ___joint.linearLimit;
				if ((double)((SoftJointLimit)(ref linearLimit)).limit + 0.01 < (double)___lastLength && num < 20f)
				{
					Extensions.InvokePrivateMethod((object)__instance, "ReleaseAnchor", Array.Empty<object>());
				}
				if (___grounded)
				{
					if (___body.drag < ___anchorDrag)
					{
						___body.drag += Time.deltaTime * (num / 45f + 0.1f);
					}
					else
					{
						val = ___body.velocity;
						if (((Vector3)(ref val)).sqrMagnitude < 5f)
						{
							___body.drag = ___anchorDrag;
							if (!___body.isKinematic && !___audio.isPlaying)
							{
								Extensions.InvokePrivateMethod((object)__instance, "SetAnchor", Array.Empty<object>());
							}
						}
					}
				}
				else if (((Component)__instance).transform.position.y < 0f)
				{
					if (___body.drag > 3f)
					{
						___body.drag -= Time.deltaTime * ___anchorDrag * 0.25f;
					}
					else
					{
						___body.drag = 3f;
					}
				}
			}
			if (Object.op_Implicit((Object)(object)GameState.currentBoat) && (Object)(object)((Component)((Joint)___joint).connectedBody).transform == (Object)(object)GameState.currentBoat.parent)
			{
				___body.interpolation = (RigidbodyInterpolation)1;
			}
			else
			{
				___body.interpolation = (RigidbodyInterpolation)0;
			}
			if (Object.op_Implicit((Object)(object)___audio) && ((Behaviour)___audio).enabled && !___audio.isPlaying)
			{
				((Behaviour)___audio).enabled = false;
			}
			linearLimit = ___joint.linearLimit;
			___lastLength = ((SoftJointLimit)(ref linearLimit)).limit;
			val = ((Joint)___joint).currentForce;
			___outCurrentForce = ((Vector3)(ref val)).magnitude;
			return false;
		}
	}

	[HarmonyPatch(typeof(Anchor))]
	private static class AnchorPatchesSimple
	{
		[HarmonyPatch("FixedUpdate")]
		[HarmonyPostfix]
		public static void FixedUpdatePatchSimple(Anchor __instance, Rigidbody ___body, bool ___grounded, float ___anchorDrag, AudioSource ___audio, ref ConfigurableJoint ___joint)
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_005d: Unknown result type (might be due to invalid IL or missing references)
			if (Main.simplePhysics.Value != "Simple")
			{
				return;
			}
			SoftJointLimitSpring linearLimitSpring = ___joint.linearLimitSpring;
			((SoftJointLimitSpring)(ref linearLimitSpring)).damper = 2000f;
			((SoftJointLimitSpring)(ref linearLimitSpring)).spring = 2000f;
			___joint.linearLimitSpring = linearLimitSpring;
			if (!___grounded || !(___body.drag >= ___anchorDrag))
			{
				return;
			}
			Vector3 velocity = ___body.velocity;
			if (((Vector3)(ref velocity)).sqrMagnitude < 1f)
			{
				bool isColliding = ((Component)__instance).GetComponent<PickupableBoatAnchor>().isColliding;
				___body.drag = ___anchorDrag;
				if (!___body.isKinematic && !___audio.isPlaying && isColliding)
				{
					Extensions.InvokePrivateMethod((object)__instance, "SetAnchor", Array.Empty<object>());
				}
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("ReleaseAnchor")]
		public static bool Prefix(Anchor __instance, ConfigurableJoint ___joint, float ___unsetForce, AudioSource ___audio, float ___lastLength)
		{
			//IL_0027: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: 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_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_0097: Unknown result type (might be due to invalid IL or missing references)
			//IL_009c: Unknown result type (might be due to invalid IL or missing references)
			if (Main.simplePhysics.Value != "Simple")
			{
				return true;
			}
			Vector3 position = ((Component)___joint).transform.position;
			Vector3 position2 = ((Component)__instance).GetComponent<PickupableBoatAnchor>().GetTopAttach().position;
			float num = Vector3.Angle(position2 - position, ((Component)___joint).transform.root.up);
			if (num < 45f)
			{
				SoftJointLimit linearLimit = ___joint.linearLimit;
				if (((SoftJointLimit)(ref linearLimit)).limit + 0.01f < ___lastLength)
				{
					Debug.Log((object)"anchor line was < 45");
					return true;
				}
			}
			Vector3 currentForce = ((Joint)___joint).currentForce;
			if (((Vector3)(ref currentForce)).magnitude >= ___unsetForce)
			{
				Debug.Log((object)"anchor broke free");
				return true;
			}
			if (Object.op_Implicit((Object)(object)((PickupableItem)((Component)__instance).GetComponent<PickupableBoatAnchor>()).held))
			{
				Debug.Log((object)"anchor is held");
				return true;
			}
			return false;
		}
	}

	[HarmonyPatch(typeof(GPButtonRopeWinch))]
	private static class WinchPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("FindBoat")]
		public static void FindBoatPatch(GPButtonRopeWinch __instance, PurchasableBoat ___boat, ref float ___gearRatio, ref float ___rotationSpeed)
		{
			//IL_0045: Unknown result type (might be due to invalid IL or missing references)
			if (((Object)__instance).name.Contains("anchor"))
			{
				if (((Object)((Component)___boat).transform).name.Contains("medi medium"))
				{
					((Component)__instance).transform.localScale = new Vector3(2f, 2f, 1.5f);
				}
				___gearRatio = 25f;
				___rotationSpeed = 8f;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch("Update")]
		public static void WinchUpdatePatch(GoPointer ___stickyClickedBy, bool ___isLookedAt, ref string ___description, RopeController ___rope, ref string ___lookText)
		{
			//IL_0028: 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)
			if (___isLookedAt || Object.op_Implicit((Object)(object)___stickyClickedBy))
			{
				RopeControllerAnchor val = (RopeControllerAnchor)(object)((___rope is RopeControllerAnchor) ? ___rope : null);
				if (val != null)
				{
					SoftJointLimit linearLimit = val.joint.linearLimit;
					___description = Mathf.Round(((SoftJointLimit)(ref linearLimit)).limit) + " yd";
				}
			}
		}
	}

	[HarmonyPatch(typeof(RopeControllerAnchor))]
	private static class RopeControllerAnchorPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("Update")]
		public static void UpdatePatch(ConfigurableJoint ___joint, ref float ___currentResistance, ref float ___maxLength)
		{
			//IL_0073: Unknown result type (might be due to invalid IL or missing references)
			//IL_0078: 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)
			if (Main.simplePhysics.Value == "Simple")
			{
				___maxLength = 50f;
			}
			if (Main.simplePhysics.Value == "Realistic")
			{
				___maxLength = 150f;
			}
			Vector3 currentForce;
			if (((Component)___joint).GetComponent<PickupableBoatAnchor>().isColliding)
			{
				currentForce = ((Joint)___joint).currentForce;
				___currentResistance = Mathf.Max(((Vector3)(ref currentForce)).magnitude, 5f);
			}
			else
			{
				currentForce = ((Joint)___joint).currentForce;
				___currentResistance = Mathf.Min(((Vector3)(ref currentForce)).magnitude / 10f, 10f);
			}
		}
	}
}
internal class PickupableBoatAnchor : PickupableItem
{
	public bool isColliding;

	private float outCurrentSqrDist;

	private ConfigurableJoint joint;

	private Anchor anchor;

	public float yankSpeed = 15f;

	private Transform topAttach;

	public Transform GetTopAttach()
	{
		if ((Object)(object)topAttach == (Object)null)
		{
			topAttach = Extensions.GetPrivateField<Transform>((object)((Component)((Component)((Joint)joint).connectedBody).gameObject.GetComponent<BoatMooringRopes>().GetAnchorController()).GetComponent<RopeEffect>(), "attachmentOne");
		}
		return topAttach;
	}

	private float GetCurrentDistanceSquared()
	{
		//IL_0007: 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)
		return Vector3.SqrMagnitude(((Component)this).transform.position - GetTopAttach().position);
	}

	public override void ExtraLateUpdate()
	{
		//IL_001a: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		if (Object.op_Implicit((Object)(object)base.held))
		{
			float num = outCurrentSqrDist;
			SoftJointLimit linearLimit = joint.linearLimit;
			if (num > ((SoftJointLimit)(ref linearLimit)).limit * 0.9f)
			{
				((GoPointerButton)this).enableRedOutline = true;
				return;
			}
		}
		((GoPointerButton)this).enableRedOutline = false;
	}

	private void Awake()
	{
		base.holdDistance = 1.5f;
		base.heldRotationOffset = 200f;
		base.big = false;
		joint = ((Component)this).GetComponentInParent<ConfigurableJoint>();
		anchor = ((Component)joint).GetComponentInParent<Anchor>();
	}

	private void Update()
	{
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: 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_0066: 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)
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		if (Object.op_Implicit((Object)(object)base.held))
		{
			float num = (outCurrentSqrDist = Mathf.Sqrt(GetCurrentDistanceSquared()));
			SoftJointLimit linearLimit = joint.linearLimit;
			if (num > ((SoftJointLimit)(ref linearLimit)).limit)
			{
				((PickupableItem)this).OnDrop();
				base.held.DropItem();
				Vector3 val = topAttach.position - ((Component)this).transform.position;
				((Component)this).GetComponentInParent<Rigidbody>().AddForceAtPosition(((Vector3)(ref val)).normalized * yankSpeed, ((Component)joint).transform.position, (ForceMode)2);
			}
		}
	}

	public override void OnPickup()
	{
		Extensions.InvokePrivateMethod((object)anchor, "ReleaseAnchor", Array.Empty<object>());
		Main.logSource.LogDebug((object)"Picked up anchor");
	}

	public override void OnDrop()
	{
		if (isColliding)
		{
			Extensions.InvokePrivateMethod((object)anchor, "SetAnchor", Array.Empty<object>());
		}
		((PickupableItem)this).OnDrop();
	}

	private void OnCollisionExit(Collision collision)
	{
		if (((Component)collision.collider).CompareTag("Terrain"))
		{
			isColliding = false;
			if (Object.op_Implicit((Object)(object)base.held))
			{
				Extensions.InvokePrivateMethod((object)anchor, "ReleaseAnchor", Array.Empty<object>());
			}
		}
	}

	private void OnCollisionEnter(Collision collision)
	{
		if (((Component)collision.collider).CompareTag("Terrain"))
		{
			isColliding = true;
		}
	}

	public override void OnScroll(float input)
	{
		base.heldRotationOffset = 200f;
	}
}