Decompiled source of AnchorImprovements v1.1.7

AnchorImprovements.dll

Decompiled 14 hours ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[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;

internal class LookTextPatch
{
	[HarmonyPatch(typeof(LookUI), "ShowLookText")]
	private static class ControlHintPatch
	{
		public static void Postfix(LookUI __instance, GoPointer ___pointer, GoPointerButton button, TextMesh ___controlsText, TextMesh ___textLicon, TextMesh ___textRIcon, bool ___altIconsOn, bool ___showingIcon)
		{
			if (button is PickupableBoatAnchor)
			{
				___controlsText.text = "pick up\n";
				AccessTools.Method(((object)__instance).GetType(), "ShowLicon", (Type[])null, (Type[])null).Invoke(__instance, new object[0]);
			}
		}
	}
}
[BepInPlugin("com.nandbrew.anchorimprovements", "Anchor Improvements", "1.1.7")]
internal class Main : BaseUnityPlugin
{
	public const string GUID = "com.nandbrew.anchorimprovements";

	public const string NAME = "Anchor Improvements";

	public const string VERSION = "1.1.7";

	internal static ManualLogSource logSource;

	internal static ConfigEntry<PhysicsType> simplePhysics;

	internal static ConfigEntry<bool> saveAnchorPosition;

	internal static ConfigEntry<bool> winchInfo;

	internal static List<PickupableBoatAnchor> boatAnchors = new List<PickupableBoatAnchor>();

	public void Awake()
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Expected O, but got Unknown
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Expected O, but got Unknown
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Expected O, but got Unknown
		//IL_0098: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: Expected O, but got Unknown
		//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Expected O, but got Unknown
		logSource = ((BaseUnityPlugin)this).Logger;
		Harmony val = new Harmony("com.nandbrew.anchorimprovements");
		Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "com.nandbrew.anchorimprovements");
		simplePhysics = ((BaseUnityPlugin)this).Config.Bind<PhysicsType>("Options", "Anchor mechanics", PhysicsType.Simple, new ConfigDescription("Simple: Normal with minor improvements \nRealistic: Holding power based on scope (angle)", (AcceptableValueBase)null, Array.Empty<object>()));
		saveAnchorPosition = ((BaseUnityPlugin)this).Config.Bind<bool>("Options", "Save anchor position", true, new ConfigDescription("", (AcceptableValueBase)null, Array.Empty<object>()));
		winchInfo = ((BaseUnityPlugin)this).Config.Bind<bool>("Options", "Winch info", true, new ConfigDescription("Show how many yards of rope are out when looking at windlass", (AcceptableValueBase)null, Array.Empty<object>()));
		MethodInfo methodInfo = AccessTools.Method(typeof(GPButtonRopeWinch), "Update", (Type[])null, (Type[])null);
		MethodInfo methodInfo2 = AccessTools.Method(typeof(WinchPatches), "Postfix", (Type[])null, (Type[])null);
		if (!Chainloader.PluginInfos.ContainsKey("pr0skynesis.sailinfo"))
		{
			val.Patch((MethodBase)methodInfo, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}
	}
}
internal enum PhysicsType
{
	Simple,
	Realistic
}
internal class Patches
{
	[HarmonyPatch(typeof(Anchor))]
	private static class AnchorPatches
	{
		private static readonly GameObject anchorStock = new GameObject
		{
			name = "anchor_stock"
		};

		[HarmonyPostfix]
		[HarmonyPatch("Start")]
		public static void StartPatch2(Anchor __instance, ref float ___initialMass, AudioSource ___audio, ref float ___anchorDrag, ConfigurableJoint ___joint)
		{
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			((Component)__instance).gameObject.layer = 0;
			((Component)__instance).gameObject.AddComponent<PickupableBoatAnchor>();
			GameObject obj = Object.Instantiate<GameObject>(anchorStock, ((Component)__instance).transform);
			obj.transform.localPosition = new Vector3(0f, 0f, -0.28f);
			CapsuleCollider obj2 = obj.AddComponent<CapsuleCollider>();
			obj2.radius = 0.05f;
			obj2.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)
			{
				___initialMass = 100f;
			}
		}
	}

	[HarmonyPatch(typeof(Anchor))]
	private static class AnchorPatchesComplex
	{
		private static float powerDivisor = 64f;

		private static float Power(float angle)
		{
			return Mathf.Pow(angle, 2f) / powerDivisor;
		}

		[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_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: 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_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_006e: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00af: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b4: 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_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_0102: 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_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_0128: Unknown result type (might be due to invalid IL or missing references)
			//IL_014a: Unknown result type (might be due to invalid IL or missing references)
			//IL_014f: Unknown result type (might be due to invalid IL or missing references)
			//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_02b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_02be: Unknown result type (might be due to invalid IL or missing references)
			//IL_02c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fa: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01b9: Unknown result type (might be due to invalid IL or missing references)
			if (Main.simplePhysics.Value != PhysicsType.Realistic)
			{
				return true;
			}
			if (!GameState.playing || !((Component)((Joint)___joint).connectedBody).GetComponentInChildren<BoatHorizon>().closeToPlayer)
			{
				return true;
			}
			SoftJointLimit linearLimit = ___joint.linearLimit;
			Vector3 val;
			if (((SoftJointLimit)(ref linearLimit)).limit > 1f)
			{
				___body.mass = ___initialMass;
				Vector3 position = ((Component)___joint).transform.position;
				float num = Vector3.Angle(((Component)((Joint)___joint).connectedBody).transform.TransformPoint(((Joint)___joint).connectedAnchor) - 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)
				{
					InvokePrivate(__instance, "ReleaseAnchor");
				}
				linearLimit = ___joint.linearLimit;
				if ((double)((SoftJointLimit)(ref linearLimit)).limit + 0.01 < (double)___lastLength && num < 20f)
				{
					InvokePrivate(__instance, "ReleaseAnchor");
				}
				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)
							{
								InvokePrivate(__instance, "SetAnchor");
							}
						}
					}
				}
				else if (((Component)__instance).transform.position.y < 0f)
				{
					if (___body.drag > 3f)
					{
						___body.drag -= Time.deltaTime * ___anchorDrag * 0.25f;
					}
					else
					{
						___body.drag = 3f;
					}
				}
			}
			else
			{
				___body.mass = 1f;
			}
			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_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: 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_0065: Unknown result type (might be due to invalid IL or missing references)
			if (Main.simplePhysics.Value != 0 || !GameState.playing || !((Component)((Joint)___joint).connectedBody).GetComponentInChildren<BoatHorizon>().closeToPlayer)
			{
				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)
				{
					InvokePrivate(__instance, "SetAnchor");
				}
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch("ReleaseAnchor")]
		public static bool Prefix(Anchor __instance, ConfigurableJoint ___joint, float ___unsetForce, AudioSource ___audio, float ___lastLength)
		{
			//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_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0030: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Unknown result type (might be due to invalid IL or missing references)
			//IL_0041: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0072: Unknown result type (might be due to invalid IL or missing references)
			//IL_0053: 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)
			if (Main.simplePhysics.Value != 0)
			{
				return true;
			}
			Vector3 position = ((Component)___joint).transform.position;
			if (Vector3.Angle(((Component)((Joint)___joint).connectedBody).transform.TransformPoint(((Joint)___joint).connectedAnchor) - position, ((Component)___joint).transform.root.up) < 45f)
			{
				SoftJointLimit linearLimit = ___joint.linearLimit;
				if (((SoftJointLimit)(ref linearLimit)).limit + 0.01f < ___lastLength)
				{
					return true;
				}
			}
			Vector3 currentForce = ((Joint)___joint).currentForce;
			if (((Vector3)(ref currentForce)).magnitude >= ___unsetForce)
			{
				return true;
			}
			if (Object.op_Implicit((Object)(object)((PickupableItem)((Component)__instance).GetComponent<PickupableBoatAnchor>()).held))
			{
				return true;
			}
			return false;
		}
	}

	[HarmonyPatch(typeof(RopeControllerAnchor))]
	private static class RopeControllerAnchorPatches
	{
		[HarmonyPostfix]
		[HarmonyPatch("Update")]
		public static void UpdatePatch(RopeControllerAnchor __instance, ConfigurableJoint ___joint, ref float ___currentResistance, ref float ___maxLength)
		{
			//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_005d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_006d: 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_007e: 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)
			if (Main.simplePhysics.Value == PhysicsType.Simple)
			{
				___maxLength = 50f;
			}
			if (Main.simplePhysics.Value == PhysicsType.Realistic)
			{
				___maxLength = 150f;
			}
			Vector3 currentForce;
			if (((Component)___joint).GetComponent<PickupableBoatAnchor>().isColliding || ((Component)___joint).GetComponent<Anchor>().IsSet())
			{
				float num = Vector3.Distance(((Component)((Joint)___joint).connectedBody).transform.TransformPoint(((Joint)___joint).connectedAnchor), ((Component)___joint).transform.position);
				SoftJointLimit linearLimit = ___joint.linearLimit;
				if (num >= ((SoftJointLimit)(ref linearLimit)).limit)
				{
					currentForce = ((Joint)___joint).currentForce;
					___currentResistance = Mathf.Max(((Vector3)(ref currentForce)).magnitude * 0.8f, 5f);
				}
				else
				{
					___currentResistance = 5f;
				}
			}
			else
			{
				currentForce = ((Joint)___joint).currentForce;
				___currentResistance = Mathf.Min(((Vector3)(ref currentForce)).magnitude / 10f, 10f);
			}
		}
	}

	public static object InvokePrivate(object obj, string name)
	{
		return AccessTools.Method(obj.GetType(), name, (Type[])null, (Type[])null).Invoke(obj, null);
	}
}
internal class PickupableBoatAnchor : PickupableItem
{
	public bool isColliding;

	public ConfigurableJoint joint;

	public Anchor anchor;

	public float yankSpeed = 15f;

	private Transform topAttach;

	private RopeControllerAnchor anchorController;

	public string dataName;

	private float currentThrowPower;

	private BoatHorizon boatHorizon;

	private bool closeToPlayer;

	private float initialHoldDist = 1.5f;

	private Traverse HPthrowPower;

	private void Awake()
	{
		base.holdDistance = initialHoldDist;
		base.heldRotationOffset = 200f;
		base.big = true;
		if (!Main.boatAnchors.Contains(this))
		{
			Main.boatAnchors.Add(this);
		}
	}

	public override void Start()
	{
		((GoPointerButton)this).Start();
		joint = ((Component)this).GetComponentInParent<ConfigurableJoint>();
		dataName = "Anchor Improvements." + ((Object)((Joint)joint).connectedBody).name;
		anchor = ((Component)joint).GetComponentInParent<Anchor>();
		GetAnchorController();
		boatHorizon = ((Component)((Joint)joint).connectedBody).gameObject.GetComponentInChildren<BoatHorizon>();
		((MonoBehaviour)this).StartCoroutine(LoadAnchorData());
	}

	private void Update()
	{
		//IL_0037: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: 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_0060: 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 (Object.op_Implicit((Object)(object)base.held) && GetCurrentDistance() >= GetAnchorController().maxLength)
		{
			((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);
		}
		if (closeToPlayer && !boatHorizon.closeToPlayer)
		{
			SaveAnchorData();
		}
		closeToPlayer = boatHorizon.closeToPlayer;
	}

	public void SaveAnchorData()
	{
		//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_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_006e: 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_0085: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: 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)
		//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Unknown result type (might be due to invalid IL or missing references)
		//IL_015b: 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_0189: Unknown result type (might be due to invalid IL or missing references)
		//IL_01af: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01da: Unknown result type (might be due to invalid IL or missing references)
		//IL_01df: Unknown result type (might be due to invalid IL or missing references)
		if (!closeToPlayer)
		{
			return;
		}
		if (Object.op_Implicit((Object)(object)GetAnchorController()) && Main.saveAnchorPosition.Value)
		{
			SoftJointLimit linearLimit = joint.linearLimit;
			if (((SoftJointLimit)(ref linearLimit)).limit > 1f)
			{
				Vector3 val = ((Component)((Joint)joint).connectedBody).transform.TransformPoint(((Joint)joint).connectedAnchor);
				Vector3 val2 = default(Vector3);
				((Vector3)(ref val2))..ctor(((Component)this).transform.position.x - val.x, ((Component)this).transform.position.y - val.y, ((Component)this).transform.position.z - val.z);
				string[] obj = new string[17]
				{
					val2.x.ToString(CultureInfo.InvariantCulture),
					",",
					val2.y.ToString(CultureInfo.InvariantCulture),
					",",
					val2.z.ToString(CultureInfo.InvariantCulture),
					",",
					((RopeController)GetAnchorController()).currentLength.ToString(CultureInfo.InvariantCulture),
					",",
					anchor.IsSet().ToString(),
					",",
					null,
					null,
					null,
					null,
					null,
					null,
					null
				};
				linearLimit = joint.linearLimit;
				obj[10] = ((SoftJointLimit)(ref linearLimit)).limit.ToString(CultureInfo.InvariantCulture);
				obj[11] = ",";
				obj[12] = ((Component)this).transform.eulerAngles.x.ToString(CultureInfo.InvariantCulture);
				obj[13] = ",";
				obj[14] = ((Component)this).transform.eulerAngles.y.ToString(CultureInfo.InvariantCulture);
				obj[15] = ",";
				obj[16] = ((Component)this).transform.eulerAngles.z.ToString(CultureInfo.InvariantCulture);
				string value = string.Concat(obj);
				if (GameState.modData.ContainsKey(dataName))
				{
					GameState.modData[dataName] = value;
				}
				else
				{
					GameState.modData.Add(dataName, value);
				}
				return;
			}
		}
		if (GameState.modData.Remove(dataName))
		{
			Main.logSource.LogDebug((object)("Removed " + dataName + " from modData"));
		}
	}

	private IEnumerator LoadAnchorData()
	{
		yield return (object)new WaitUntil((Func<bool>)(() => GameState.playing && !GameState.justStarted && closeToPlayer));
		if (Main.saveAnchorPosition.Value && GameState.modData.TryGetValue(dataName, out var value))
		{
			string[] strings = value.Split(new char[1] { ',' });
			Vector3 pos = new Vector3(float.Parse(strings[0], CultureInfo.InvariantCulture), float.Parse(strings[1], CultureInfo.InvariantCulture), float.Parse(strings[2], CultureInfo.InvariantCulture));
			float num = float.Parse(strings[5], CultureInfo.InvariantCulture);
			float num2 = Vector3.Magnitude(pos);
			if (num2 > num && num2 < GetAnchorController().maxLength)
			{
				num = num2;
			}
			SoftJointLimit linearLimit = joint.linearLimit;
			((SoftJointLimit)(ref linearLimit)).limit = num;
			((RopeController)GetAnchorController()).currentLength = num / GetAnchorController().maxLength;
			joint.linearLimit = linearLimit;
			yield return (object)new WaitForEndOfFrame();
			((Component)this).transform.position = pos + ((Component)((Joint)joint).connectedBody).transform.TransformPoint(((Joint)joint).connectedAnchor);
			Vector3 eulerAngles = default(Vector3);
			((Vector3)(ref eulerAngles))..ctor(float.Parse(strings[6], CultureInfo.InvariantCulture), float.Parse(strings[7], CultureInfo.InvariantCulture), float.Parse(strings[8], CultureInfo.InvariantCulture));
			((Component)this).transform.eulerAngles = eulerAngles;
			if (strings[4] == "True")
			{
				InvokePrivate(anchor, "SetAnchor");
			}
			GameState.modData.Remove(dataName);
		}
	}

	public RopeControllerAnchor GetAnchorController()
	{
		if ((Object)(object)anchorController == (Object)null)
		{
			anchorController = ((Component)((Joint)joint).connectedBody).gameObject.GetComponent<BoatMooringRopes>().GetAnchorController();
		}
		return anchorController;
	}

	private float GetCurrentDistance()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//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)
		return Vector3.Distance(((Component)this).transform.position, ((Component)((Joint)joint).connectedBody).transform.TransformPoint(((Joint)joint).connectedAnchor));
	}

	public override void ExtraLateUpdate()
	{
		if (Object.op_Implicit((Object)(object)base.held) && GetCurrentDistance() > anchorController.maxLength * 0.9f)
		{
			((GoPointerButton)this).enableRedOutline = true;
		}
		else
		{
			((GoPointerButton)this).enableRedOutline = false;
		}
		if (!Object.op_Implicit((Object)(object)base.held))
		{
			return;
		}
		if (GameInput.GetKey((InputName)10))
		{
			currentThrowPower += Time.deltaTime;
			HPthrowPower.SetValue((object)currentThrowPower);
		}
		else if (GameInput.GetKeyUp((InputName)10))
		{
			Rigidbody component = ((Component)this).GetComponent<Rigidbody>();
			((PickupableItem)this).OnDrop();
			if (currentThrowPower > base.held.throwDelay)
			{
				((MonoBehaviour)base.held).StartCoroutine(ThrowItemAfterDelay(component, currentThrowPower - base.held.throwDelay, base.held));
				currentThrowPower = 0f;
			}
			base.held.DropItem();
		}
	}

	private IEnumerator ThrowItemAfterDelay(Rigidbody heldRigidbody, float force, GoPointer holdingPointer)
	{
		yield return (object)new WaitForFixedUpdate();
		heldRigidbody.AddForce(((Component)holdingPointer).transform.forward * 20f * Mathf.Min(force, 1f), (ForceMode)2);
	}

	public override void OnPickup()
	{
		HPthrowPower = Traverse.Create((object)base.held).Field("currentThrowPower");
		InvokePrivate(anchor, "ReleaseAnchor");
		Main.logSource.LogDebug((object)"Picked up anchor");
		((RopeController)GetAnchorController()).currentLength = anchorController.maxLength;
		((Component)this).GetComponent<Collider>().isTrigger = true;
	}

	public object InvokePrivate(object obj, string name)
	{
		return AccessTools.Method(obj.GetType(), name, (Type[])null, (Type[])null).Invoke(obj, null);
	}

	public override void OnDrop()
	{
		((Component)this).GetComponent<Collider>().isTrigger = false;
		base.holdDistance = initialHoldDist;
		if (isColliding)
		{
			InvokePrivate(anchor, "SetAnchor");
			((RopeController)GetAnchorController()).currentLength = GetCurrentDistance() / anchorController.maxLength;
		}
		else
		{
			((RopeController)GetAnchorController()).currentLength = (GetCurrentDistance() + currentThrowPower * 10f) / anchorController.maxLength;
		}
		((PickupableItem)this).OnDrop();
	}

	private IEnumerator WaitForDoneProcess(float timeout)
	{
		while ((double)GetCurrentDistance() < (double)GetAnchorController().maxLength * 0.8 && !isColliding)
		{
			yield return null;
			timeout -= Time.deltaTime;
			if (timeout <= 0f)
			{
				break;
			}
		}
	}

	private IEnumerator SetRopeLength(float timeout)
	{
		yield return WaitForDone(timeout);
		((RopeController)GetAnchorController()).currentLength = GetCurrentDistance() / anchorController.maxLength;
	}

	private YieldInstruction WaitForDone(float timeout)
	{
		return (YieldInstruction)(object)((MonoBehaviour)this).StartCoroutine(WaitForDoneProcess(timeout));
	}

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

	private void OnCollisionEnter(Collision collision)
	{
		if (((Component)collision.collider).CompareTag("Terrain"))
		{
			isColliding = true;
		}
	}
}
[HarmonyPatch(typeof(SaveLoadManager))]
internal class SaveLoadPatches
{
	[HarmonyPrefix]
	[HarmonyPatch("SaveModData")]
	public static void SavePatch()
	{
		foreach (PickupableBoatAnchor boatAnchor in Main.boatAnchors)
		{
			boatAnchor.SaveAnchorData();
		}
	}
}
[HarmonyPatch(typeof(IslandHorizon), "Start")]
internal static class TerrainFixer
{
	[HarmonyPostfix]
	public static void Postfix(IslandHorizon __instance)
	{
		Transform[] componentsInChildren = ((Component)__instance).GetComponentsInChildren<Transform>();
		foreach (Transform val in componentsInChildren)
		{
			if (((Component)val).CompareTag("Untagged") && ((Object)val).name.ToLower().Contains("terrain"))
			{
				((Component)val).tag = "Terrain";
			}
		}
	}
}
internal static class WinchPatches
{
	[HarmonyPatch(typeof(GPButtonRopeWinch))]
	[HarmonyPostfix]
	[HarmonyPatch("FindBoat")]
	public static void FindBoatPatch(GPButtonRopeWinch __instance, PurchasableBoat ___boat, ref float ___gearRatio, ref float ___rotationSpeed, RopeController ___rope)
	{
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		if (___rope is RopeControllerAnchor)
		{
			if (((Object)((Component)___boat).transform).name.Contains("medi medium"))
			{
				((Component)__instance).transform.localScale = new Vector3(2f, 2f, 1.5f);
			}
			___gearRatio = 25f;
			___rotationSpeed = 8f;
		}
	}

	public static void Postfix(GPButtonRopeWinch __instance, GoPointer ___stickyClickedBy, bool ___isLookedAt, ref string ___description, RopeController ___rope, ref string ___lookText)
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		if (!___isLookedAt && !Object.op_Implicit((Object)(object)___stickyClickedBy))
		{
			return;
		}
		RopeControllerAnchor val = (RopeControllerAnchor)(object)((___rope is RopeControllerAnchor) ? ___rope : null);
		if (val != null)
		{
			string text = "";
			SoftJointLimit linearLimit = val.joint.linearLimit;
			float num = Mathf.Round(((SoftJointLimit)(ref linearLimit)).limit);
			if (Main.winchInfo.Value)
			{
				text = num + " yd";
			}
			___description = text;
		}
	}
}