Decompiled source of UnstayedJunkSailMast v1.2.10

BepInEx/plugins/UnstayedJunkSailMast/UnstayedJunkSailMast.dll

Decompiled 4 days ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using ShipyardExpansion;
using ShipyardExpansion.Scripts;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = ".NET Standard 2.0")]
[assembly: AssemblyCompany("UnstayedJunkSailMast")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.2.10.0")]
[assembly: AssemblyInformationalVersion("1.2.10")]
[assembly: AssemblyProduct("UnstayedJunkSailMast")]
[assembly: AssemblyTitle("UnstayedJunkSailMast")]
[assembly: InternalsVisibleTo("UnstayedJunkSailMast.Tests")]
[assembly: AssemblyVersion("1.2.10.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 UnstayedJunkSailMast
{
	[BepInPlugin("dogeggz.unstayedjunksailmast", "Unstayed Junk Sail Mast", "1.2.10")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string PluginGuid = "dogeggz.unstayedjunksailmast";

		public const string PluginName = "Unstayed Junk Sail Mast";

		public const string PluginVersion = "1.2.10";

		public const string ShipyardExpansionGuid = "com.nandbrew.shipyardexpansion";

		private Harmony harmony;

		internal static ManualLogSource LogSource { get; private set; }

		internal static string PluginDirectory { get; private set; }

		private void Awake()
		{
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0039: Expected O, but got Unknown
			LogSource = ((BaseUnityPlugin)this).Logger;
			PluginDirectory = Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location) ?? string.Empty;
			harmony = new Harmony("dogeggz.unstayedjunksailmast");
			harmony.PatchAll(typeof(Plugin).Assembly);
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Unstayed Junk Sail Mast 1.2.10 loaded; required Shipyard Expansion dependency is active.");
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
			RectangleJunkSails.Reset();
			RectangleJunkAssets.Reset();
			UnstayedBoatRegistry.Clear();
			LogSource = null;
			PluginDirectory = null;
		}
	}
	internal static class RectangleJunkAssets
	{
		private const string BundleFileName = "unstayedjunksailmast_assets";

		private const string NarrowPrefabPath = "assets/ujsmfinal/narrow_rectangle_junk.prefab";

		private const string WidePrefabPath = "assets/ujsmfinal/wide_rectangle_junk.prefab";

		private static AssetBundle bundle;

		private static GameObject narrowPrefab;

		private static GameObject widePrefab;

		internal static GameObject LoadNarrowPrefab()
		{
			if ((Object)(object)narrowPrefab != (Object)null)
			{
				return narrowPrefab;
			}
			if (!EnsureBundleLoaded())
			{
				return null;
			}
			narrowPrefab = bundle.LoadAsset<GameObject>("assets/ujsmfinal/narrow_rectangle_junk.prefab");
			if ((Object)(object)narrowPrefab == (Object)null)
			{
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogError((object)"Could not find the final narrow rectangle Junk prefab in the Rectangle Junk AssetBundle.");
				}
			}
			return narrowPrefab;
		}

		internal static GameObject LoadWidePrefab()
		{
			if ((Object)(object)widePrefab != (Object)null)
			{
				return widePrefab;
			}
			if (!EnsureBundleLoaded())
			{
				return null;
			}
			widePrefab = bundle.LoadAsset<GameObject>("assets/ujsmfinal/wide_rectangle_junk.prefab");
			if ((Object)(object)widePrefab == (Object)null)
			{
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogError((object)"Could not find the final wide rectangle Junk prefab in the Rectangle Junk AssetBundle.");
				}
			}
			return widePrefab;
		}

		private static bool EnsureBundleLoaded()
		{
			if ((Object)(object)bundle != (Object)null)
			{
				return true;
			}
			string text = Path.Combine(Plugin.PluginDirectory, "assets", "unstayedjunksailmast_assets");
			if (!File.Exists(text))
			{
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogError((object)("Could not add Rectangle Junk sails: AssetBundle not found at " + text + "."));
				}
				return false;
			}
			try
			{
				bundle = AssetBundle.LoadFromFile(text);
				if ((Object)(object)bundle != (Object)null)
				{
					return true;
				}
				ManualLogSource logSource2 = Plugin.LogSource;
				if (logSource2 != null)
				{
					logSource2.LogError((object)("Could not load Rectangle Junk AssetBundle at " + text + "."));
				}
			}
			catch (Exception ex)
			{
				ManualLogSource logSource3 = Plugin.LogSource;
				if (logSource3 != null)
				{
					logSource3.LogError((object)("Could not load final Rectangle Junk assets: " + ex));
				}
			}
			bundle = null;
			return false;
		}

		internal static void Reset()
		{
			narrowPrefab = null;
			widePrefab = null;
			AssetBundle obj = bundle;
			if (obj != null)
			{
				obj.Unload(false);
			}
			bundle = null;
		}
	}
	internal static class RectangleJunkSails
	{
		internal const int LegacyNarrowIndex = 131;

		internal const int LegacyWideIndex = 132;

		internal const int NarrowIndex = 200;

		internal const int WideIndex = 201;

		private const int ShipyardExpansionSailCapacity = 512;

		private const float WindAxisToleranceDegrees = 0.01f;

		private static readonly int[] RuntimeMaterialSourceIndices = new int[7] { 101, 27, 28, 29, 100, 102, 103 };

		private static GameObject prefabContainer;

		private static bool legacyNarrowIndexWasAvailable;

		private static bool legacyWideIndexWasAvailable;

		internal static GameObject NarrowPrefab { get; private set; }

		internal static GameObject WidePrefab { get; private set; }

		internal static bool EnsureRegistered(PrefabsDirectory directory)
		{
			if ((Object)(object)directory == (Object)null || directory.sails == null)
			{
				return false;
			}
			NarrowPrefab = GetRegisteredRectangle(directory, 200);
			WidePrefab = GetRegisteredRectangle(directory, 201);
			if ((Object)(object)NarrowPrefab == (Object)null || (Object)(object)WidePrefab == (Object)null)
			{
				Build(directory);
				NarrowPrefab = GetRegisteredRectangle(directory, 200);
				WidePrefab = GetRegisteredRectangle(directory, 201);
			}
			if ((Object)(object)NarrowPrefab != (Object)null)
			{
				EnsureShipyardExpansionComponents(NarrowPrefab);
			}
			if ((Object)(object)WidePrefab != (Object)null)
			{
				EnsureShipyardExpansionComponents(WidePrefab);
			}
			if ((Object)(object)NarrowPrefab != (Object)null)
			{
				return (Object)(object)WidePrefab != (Object)null;
			}
			return false;
		}

		internal static bool CanMigrateLegacyIndex(int prefabIndex)
		{
			return prefabIndex switch
			{
				131 => legacyNarrowIndexWasAvailable, 
				132 => legacyWideIndexWasAvailable, 
				_ => false, 
			};
		}

		internal static void Build(PrefabsDirectory directory)
		{
			if ((Object)(object)directory == (Object)null || directory.sails == null)
			{
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogError((object)"Could not add Rectangle Junk sails: PrefabsDirectory.sails is unavailable.");
				}
				return;
			}
			if (directory.sails.Length < 512)
			{
				legacyNarrowIndexWasAvailable = false;
				legacyWideIndexWasAvailable = false;
				ManualLogSource logSource2 = Plugin.LogSource;
				if (logSource2 != null)
				{
					logSource2.LogError((object)"Could not add Rectangle Junk sails: Shipyard Expansion did not provide its 512-slot sail registry.");
				}
				return;
			}
			legacyNarrowIndexWasAvailable = (Object)(object)directory.sails[131] == (Object)null;
			legacyWideIndexWasAvailable = (Object)(object)directory.sails[132] == (Object)null;
			GameObject val = FindRuntimeMaterialSource(directory);
			if ((Object)(object)val == (Object)null)
			{
				ManualLogSource logSource3 = Plugin.LogSource;
				if (logSource3 != null)
				{
					logSource3.LogError((object)"Could not add Rectangle Junk sails: no live Junk Square rendering materials were found.");
				}
				return;
			}
			EnsurePrefabContainer();
			NarrowPrefab = EnsureFinalRectangle(directory, val, RectangleJunkAssets.LoadNarrowPrefab(), 200, "narrow rectangle junk");
			WidePrefab = EnsureFinalRectangle(directory, val, RectangleJunkAssets.LoadWidePrefab(), 201, "wide rectangle junk");
			ManualLogSource logSource4 = Plugin.LogSource;
			if (logSource4 != null)
			{
				logSource4.LogInfo((object)(((Object)(object)NarrowPrefab != (Object)null) ? ("Registered narrow rectangle junk at sail index " + 200 + ".") : "Narrow rectangle junk was not registered."));
			}
			ManualLogSource logSource5 = Plugin.LogSource;
			if (logSource5 != null)
			{
				logSource5.LogInfo((object)(((Object)(object)WidePrefab != (Object)null) ? ("Registered wide rectangle junk at sail index " + 201 + ".") : "Wide rectangle junk was not registered."));
			}
		}

		internal static void AddToShipyard(PrefabsDirectory directory, ref GameObject[] sailPrefabs)
		{
			NarrowPrefab = GetRegisteredRectangle(directory, 200);
			WidePrefab = GetRegisteredRectangle(directory, 201);
			if ((Object)(object)NarrowPrefab == (Object)null || (Object)(object)WidePrefab == (Object)null)
			{
				return;
			}
			List<GameObject> list = new List<GameObject>();
			HashSet<GameObject> hashSet = new HashSet<GameObject>();
			if (sailPrefabs != null)
			{
				for (int i = 0; i < sailPrefabs.Length; i++)
				{
					GameObject val = sailPrefabs[i];
					Sail val2 = (((Object)(object)val != (Object)null) ? val.GetComponent<Sail>() : null);
					if ((Object)(object)val != (Object)null && ((Object)(object)val2 == (Object)null || !IsRectangleIndex(val2.prefabIndex)) && hashSet.Add(val))
					{
						list.Add(val);
					}
				}
			}
			AddUnique(list, hashSet, NarrowPrefab);
			AddUnique(list, hashSet, WidePrefab);
			sailPrefabs = list.ToArray();
		}

		internal static void RemoveFromShipyard(ref GameObject[] sailPrefabs)
		{
			if (sailPrefabs == null)
			{
				return;
			}
			List<GameObject> list = new List<GameObject>(sailPrefabs.Length);
			for (int i = 0; i < sailPrefabs.Length; i++)
			{
				GameObject val = sailPrefabs[i];
				Sail val2 = (((Object)(object)val != (Object)null) ? val.GetComponent<Sail>() : null);
				if ((Object)(object)val2 == (Object)null || !IsRectangleIndex(val2.prefabIndex))
				{
					list.Add(val);
				}
			}
			sailPrefabs = list.ToArray();
		}

		internal static void PrepareMast(Mast mast)
		{
			if (!((Object)(object)mast == (Object)null) && mast.sails != null)
			{
				for (int i = 0; i < mast.sails.Count; i++)
				{
					GameObject val = mast.sails[i];
					(((Object)(object)val != (Object)null) ? val.GetComponent<RectangleJunkSailRig>() : null)?.PrepareForMastUpdate();
				}
			}
		}

		internal static void ApplyToMast(Mast mast)
		{
			if ((Object)(object)mast == (Object)null || mast.sails == null)
			{
				return;
			}
			for (int i = 0; i < mast.sails.Count; i++)
			{
				GameObject val = mast.sails[i];
				RectangleJunkSailRig rectangleJunkSailRig = (((Object)(object)val != (Object)null) ? val.GetComponent<RectangleJunkSailRig>() : null);
				if ((Object)(object)rectangleJunkSailRig != (Object)null && rectangleJunkSailRig.Initialize(mast))
				{
					rectangleJunkSailRig.BindAssignedWinch();
				}
			}
		}

		internal static void Reset()
		{
			NarrowPrefab = null;
			WidePrefab = null;
			legacyNarrowIndexWasAvailable = false;
			legacyWideIndexWasAvailable = false;
			RectangleJunkRenderState.Reset();
			if ((Object)(object)prefabContainer != (Object)null)
			{
				Object.Destroy((Object)(object)prefabContainer);
				prefabContainer = null;
			}
		}

		private static void EnsurePrefabContainer()
		{
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Expected O, but got Unknown
			if (!((Object)(object)prefabContainer != (Object)null))
			{
				prefabContainer = new GameObject("UJSM Rectangle Junk Prefabs");
				Object.DontDestroyOnLoad((Object)(object)prefabContainer);
				prefabContainer.SetActive(false);
			}
		}

		private static GameObject EnsureFinalRectangle(PrefabsDirectory directory, GameObject runtimeMaterialSource, GameObject authoredSource, int prefabIndex, string sailName)
		{
			GameObject val = directory.sails[prefabIndex];
			if ((Object)(object)val != (Object)null)
			{
				RectangleJunkSailRig component = val.GetComponent<RectangleJunkSailRig>();
				Sail component2 = val.GetComponent<Sail>();
				if ((Object)(object)component != (Object)null && (Object)(object)component2 != (Object)null && component2.prefabIndex == prefabIndex)
				{
					EnsureShipyardExpansionComponents(val);
					return val;
				}
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogError((object)("Could not register " + sailName + " at index " + prefabIndex + ": that sail slot is already occupied by " + ((Object)val).name + "."));
				}
				return null;
			}
			if ((Object)(object)authoredSource == (Object)null)
			{
				return null;
			}
			GameObject val2 = null;
			try
			{
				val2 = Object.Instantiate<GameObject>(authoredSource, prefabContainer.transform);
				((Object)val2).name = prefabIndex + " SAIL " + sailName;
				ConfigureFinalRectangle(val2, runtimeMaterialSource, prefabIndex, sailName);
				directory.sails[prefabIndex] = val2;
				return val2;
			}
			catch (Exception ex)
			{
				if ((Object)(object)val2 != (Object)null)
				{
					Object.DestroyImmediate((Object)(object)val2);
				}
				ManualLogSource logSource2 = Plugin.LogSource;
				if (logSource2 != null)
				{
					logSource2.LogError((object)("Could not build authored " + sailName + ": " + ex));
				}
				return null;
			}
		}

		private static void ConfigureFinalRectangle(GameObject clone, GameObject runtimeMaterialSource, int prefabIndex, string sailName)
		{
			Sail sail = RequireComponent<Sail>(clone, "Sail");
			SailConnections connections = RequireComponent<SailConnections>(clone, "SailConnections");
			HingeJoint hinge = RequireComponent<HingeJoint>(clone, "HingeJoint");
			Animator componentInChildren = clone.GetComponentInChildren<Animator>(true);
			Transform val = FindDescendant(((Object)(object)componentInChildren != (Object)null) ? ((Component)componentInChildren).transform : null, "SAIL_junk_square");
			Transform val2 = FindDescendant(val, "sail_rope_att__angle_mid_rectangle_");
			if ((Object)(object)componentInChildren == (Object)null || (Object)(object)val == (Object)null || (Object)(object)val2 == (Object)null)
			{
				throw new InvalidOperationException("the authored Rectangle Junk hierarchy is incomplete");
			}
			ValidateFinalRuntimeRig(clone, sail, connections, hinge, val2, prefabIndex, sailName);
			AssignRuntimeMaterials(clone, runtimeMaterialSource);
			clone.AddComponent<RectangleJunkSailRig>();
			EnsureShipyardExpansionComponents(clone);
		}

		private static void EnsureShipyardExpansionComponents(GameObject prefab)
		{
			if ((Object)(object)prefab.GetComponent<SailScaler>() == (Object)null)
			{
				prefab.AddComponent<SailScaler>();
			}
			if ((Object)(object)prefab.GetComponent<SailTextureChanger>() == (Object)null)
			{
				prefab.AddComponent<SailTextureChanger>().Setup();
			}
		}

		private static GameObject GetRegisteredRectangle(PrefabsDirectory directory, int prefabIndex)
		{
			if ((Object)(object)directory == (Object)null || directory.sails == null || prefabIndex < 0 || prefabIndex >= directory.sails.Length)
			{
				return null;
			}
			GameObject val = directory.sails[prefabIndex];
			Sail val2 = (((Object)(object)val != (Object)null) ? val.GetComponent<Sail>() : null);
			RectangleJunkSailRig rectangleJunkSailRig = (((Object)(object)val != (Object)null) ? val.GetComponent<RectangleJunkSailRig>() : null);
			if (!((Object)(object)val2 != (Object)null) || !((Object)(object)rectangleJunkSailRig != (Object)null) || val2.prefabIndex != prefabIndex)
			{
				return null;
			}
			return val;
		}

		private static void ValidateFinalRuntimeRig(GameObject clone, Sail sail, SailConnections connections, HingeJoint hinge, Transform sheetAnchor, int prefabIndex, string sailName)
		{
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_009d: Unknown result type (might be due to invalid IL or missing references)
			RopeController reefController = connections.reefController;
			RopeControllerSailReef val = (RopeControllerSailReef)(object)((reefController is RopeControllerSailReef) ? reefController : null);
			RopeController angleControllerMid = connections.angleControllerMid;
			RopeControllerSailAngle val2 = (RopeControllerSailAngle)(object)((angleControllerMid is RopeControllerSailAngle) ? angleControllerMid : null);
			Transform midRopeAttachment = connections.midRopeAttachment;
			RopeEffect val3 = (((Object)(object)midRopeAttachment != (Object)null) ? ((Component)midRopeAttachment).GetComponent<RopeEffect>() : null);
			RopeEffect val4 = (((Object)(object)val2 != (Object)null) ? ((Component)val2).GetComponent<RopeEffect>() : null);
			if (sail.prefabIndex != prefabIndex || sail.sailName != sailName || sail.squareSail || sail.obsolete || (Object)(object)sail.windcenter == (Object)null || Quaternion.Angle(clone.transform.rotation, sail.windcenter.rotation) > 0.01f || (Object)(object)connections.sail != (Object)(object)sail || (Object)(object)val == (Object)null || (Object)(object)val2 == (Object)null || (Object)(object)midRopeAttachment == (Object)null || (Object)(object)val3 == (Object)null || (Object)(object)val4 == (Object)null || (Object)(object)connections.angleControllerLeft != (Object)null || (Object)(object)connections.angleControllerRight != (Object)null || (Object)(object)val2.sailHinge != (Object)(object)hinge || (Object)(object)((Component)val2).transform.parent != (Object)(object)clone.transform || (Object)(object)midRopeAttachment.parent != (Object)(object)clone.transform || (Object)(object)val4.attachment != (Object)(object)midRopeAttachment || (Object)(object)val3.attachment != (Object)(object)sheetAnchor || !val3.sheet || (Object)(object)val.sail != (Object)(object)sail || !val.reverseReefing || (Object)(object)clone.GetComponent<SquareAngleMaster>() != (Object)null || (Object)(object)clone.GetComponent<SquareTopsailAngleMirror>() != (Object)null)
			{
				throw new InvalidOperationException("the final Rectangle Junk prefab has invalid serialized vanilla Junk rig or wind-axis configuration");
			}
		}

		private static GameObject FindRuntimeMaterialSource(PrefabsDirectory directory)
		{
			for (int i = 0; i < RuntimeMaterialSourceIndices.Length; i++)
			{
				int num = RuntimeMaterialSourceIndices[i];
				if (num >= 0 && num < directory.sails.Length)
				{
					GameObject val = directory.sails[num];
					if (HasRuntimeMaterials(val))
					{
						return val;
					}
				}
			}
			return null;
		}

		private static bool HasRuntimeMaterials(GameObject candidate)
		{
			Sail val = (((Object)(object)candidate != (Object)null) ? candidate.GetComponent<Sail>() : null);
			Renderer val2 = (((Object)(object)val != (Object)null && (Object)(object)val.cloth != (Object)null) ? ((Component)val.cloth).GetComponent<Renderer>() : null);
			ReefEffectAnimUniversal val3 = (((Object)(object)candidate != (Object)null) ? candidate.GetComponentInChildren<ReefEffectAnimUniversal>(true) : null);
			if ((Object)(object)val2 != (Object)null && val2.sharedMaterials.Length != 0 && (Object)(object)val2.sharedMaterials[0] != (Object)null && (Object)(object)val3 != (Object)null && (Object)(object)val3.furledSail != (Object)null && val3.furledSail.sharedMaterials.Length != 0)
			{
				return (Object)(object)val3.furledSail.sharedMaterials[0] != (Object)null;
			}
			return false;
		}

		private static void AssignRuntimeMaterials(GameObject target, GameObject source)
		{
			Sail val = RequireComponent<Sail>(target, "Sail");
			Sail val2 = RequireComponent<Sail>(source, "Junk Square Sail");
			Renderer val3 = (((Object)(object)val.cloth != (Object)null) ? ((Component)val.cloth).GetComponent<Renderer>() : null);
			Renderer val4 = (((Object)(object)val2.cloth != (Object)null) ? ((Component)val2.cloth).GetComponent<Renderer>() : null);
			ReefEffectAnimUniversal componentInChildren = target.GetComponentInChildren<ReefEffectAnimUniversal>(true);
			ReefEffectAnimUniversal componentInChildren2 = source.GetComponentInChildren<ReefEffectAnimUniversal>(true);
			if ((Object)(object)val3 == (Object)null || (Object)(object)val4 == (Object)null || (Object)(object)componentInChildren == (Object)null || (Object)(object)componentInChildren.furledSail == (Object)null || (Object)(object)componentInChildren2 == (Object)null || (Object)(object)componentInChildren2.furledSail == (Object)null || val4.sharedMaterials.Length == 0 || componentInChildren2.furledSail.sharedMaterials.Length == 0)
			{
				throw new InvalidOperationException("the live Junk Square rendering materials are incomplete");
			}
			RectangleJunkRenderState.CaptureAndApply(target, source, val3, val4, componentInChildren.furledSail, componentInChildren2.furledSail);
		}

		internal static bool IsRectangle(GameObject sailObject)
		{
			return IsRectangle(((Object)(object)sailObject != (Object)null) ? sailObject.GetComponent<Sail>() : null);
		}

		internal static bool IsRectangle(Sail sail)
		{
			if ((Object)(object)sail != (Object)null)
			{
				return IsRectangleIndex(sail.prefabIndex);
			}
			return false;
		}

		private static bool IsRectangleIndex(int prefabIndex)
		{
			if (prefabIndex != 200)
			{
				return prefabIndex == 201;
			}
			return true;
		}

		internal static void SynchronizeCollisionCheckerScale(Sail sail)
		{
			//IL_00a5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00be: Unknown result type (might be due to invalid IL or missing references)
			if (!((Object)(object)sail == (Object)null) && IsRectangle(((Component)sail).gameObject) && !((Object)(object)sail.cloth == (Object)null) && !((Object)(object)((Component)sail.cloth).transform.parent == (Object)null))
			{
				SailConnections component = ((Component)sail).GetComponent<SailConnections>();
				Transform val = (((Object)(object)component != (Object)null && (Object)(object)component.colChecker != (Object)null) ? ((Component)component.colChecker).transform : null);
				if (!((Object)(object)val == (Object)null) && !((Object)(object)val.parent == (Object)null) && ((Component)val.parent).gameObject.layer == 8)
				{
					Vector3 localScale = ((Component)sail.cloth).transform.parent.localScale;
					val.localScale = new Vector3(localScale.x, localScale.z, localScale.y);
				}
			}
		}

		internal static void FurlNewlyInstalledSail(GameObject sailObject)
		{
			if (!IsRectangle(sailObject))
			{
				return;
			}
			Sail component = sailObject.GetComponent<Sail>();
			SailConnections component2 = sailObject.GetComponent<SailConnections>();
			RopeControllerSailReef val = (RopeControllerSailReef)(((Object)(object)component2 != (Object)null) ? /*isinst with value type is only supported in some contexts*/: null);
			if ((Object)(object)val == (Object)null)
			{
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogWarning((object)"Could not furl newly installed Rectangle Junk: its reef controller is missing.");
				}
			}
			else
			{
				component.currentUnroll = 0f;
				val.reverseReefing = true;
				((RopeController)val).currentLength = 1f;
				((RopeController)val).changed = true;
			}
		}

		internal static void SynchronizeWinchVisibility(Mast mast)
		{
			if ((Object)(object)mast == (Object)null)
			{
				return;
			}
			bool hasRectangle = false;
			if (mast.sails != null)
			{
				for (int i = 0; i < mast.sails.Count; i++)
				{
					if (IsRectangle(mast.sails[i]))
					{
						hasRectangle = true;
						break;
					}
				}
			}
			HashSet<GPButtonRopeWinch> seen = new HashSet<GPButtonRopeWinch>();
			SynchronizeWinchArray(mast.reefWinch, hasRectangle, seen);
			SynchronizeWinchArray(mast.midAngleWinch, hasRectangle, seen);
			SynchronizeWinchArray(mast.leftAngleWinch, hasRectangle, seen);
			SynchronizeWinchArray(mast.rightAngleWinch, hasRectangle, seen);
		}

		private static void SynchronizeWinchArray(GPButtonRopeWinch[] winches, bool hasRectangle, HashSet<GPButtonRopeWinch> seen)
		{
			if (winches == null)
			{
				return;
			}
			foreach (GPButtonRopeWinch val in winches)
			{
				if (!((Object)(object)val == (Object)null) && seen.Add(val))
				{
					RectangleJunkWinchVisibility rectangleJunkWinchVisibility = ((Component)val).GetComponent<RectangleJunkWinchVisibility>();
					if ((Object)(object)rectangleJunkWinchVisibility == (Object)null && hasRectangle && (Object)(object)val.rope == (Object)null)
					{
						rectangleJunkWinchVisibility = ((Component)val).gameObject.AddComponent<RectangleJunkWinchVisibility>();
					}
					rectangleJunkWinchVisibility?.SetHidden(hasRectangle && (Object)(object)val.rope == (Object)null);
				}
			}
		}

		private static T RequireComponent<T>(GameObject target, string componentName) where T : Component
		{
			T component = target.GetComponent<T>();
			if ((Object)(object)component == (Object)null)
			{
				throw new InvalidOperationException(componentName + " is missing");
			}
			return component;
		}

		private static Transform FindDescendant(Transform root, string name)
		{
			if ((Object)(object)root == (Object)null)
			{
				return null;
			}
			Transform[] componentsInChildren = ((Component)root).GetComponentsInChildren<Transform>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				if (((Object)componentsInChildren[i]).name == name)
				{
					return componentsInChildren[i];
				}
			}
			return null;
		}

		private static void AddUnique(List<GameObject> result, HashSet<GameObject> seen, GameObject prefab)
		{
			if ((Object)(object)prefab != (Object)null && seen.Add(prefab))
			{
				result.Add(prefab);
			}
		}
	}
	internal sealed class RectangleJunkSailRig : MonoBehaviour
	{
		internal const float TangentialWindFraction = 0.5f;

		internal const float StaticWindMultiplier = 1f;

		internal const float BendingStiffness = 0.7f;

		private const float OrdinaryJunkSolverFrequency = 120f;

		private Mast mast;

		private Sail sail;

		private RopeControllerSailReef reefController;

		private ReefEffectAnimUniversal reefEffect;

		private WindCloth windCloth;

		private GPButtonRopeWinch boundWinch;

		private bool originalWinchReverseResistance;

		private bool initialized;

		private bool refreshClothPending;

		private bool warningLogged;

		internal bool Initialize(Mast owningMast)
		{
			mast = owningMast;
			if (initialized)
			{
				return true;
			}
			sail = ((Component)this).GetComponent<Sail>();
			SailConnections component = ((Component)this).GetComponent<SailConnections>();
			reefController = (RopeControllerSailReef)(((Object)(object)component != (Object)null) ? /*isinst with value type is only supported in some contexts*/: null);
			reefEffect = ((Component)this).GetComponentInChildren<ReefEffectAnimUniversal>(true);
			windCloth = (((Object)(object)sail != (Object)null && (Object)(object)sail.cloth != (Object)null) ? ((Component)sail.cloth).GetComponent<WindCloth>() : null);
			if ((Object)(object)sail == (Object)null || (Object)(object)component == (Object)null || (Object)(object)reefController == (Object)null || (Object)(object)reefEffect == (Object)null || (Object)(object)sail.cloth == (Object)null || (Object)(object)windCloth == (Object)null)
			{
				return Fail("its reefing components are incomplete");
			}
			ApplyRectangleJunkClothDynamics();
			ApplyControllerDirection();
			initialized = true;
			refreshClothPending = true;
			return true;
		}

		internal void PrepareForMastUpdate()
		{
			RestoreWinch();
		}

		internal void BindAssignedWinch()
		{
			if (!initialized || (Object)(object)mast == (Object)null)
			{
				return;
			}
			GPButtonRopeWinch[] componentsInChildren = ((Component)(((Object)(object)mast.shipRigidbody != (Object)null) ? ((Component)mast.shipRigidbody).transform : ((Component)mast).transform)).GetComponentsInChildren<GPButtonRopeWinch>(true);
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				if ((Object)(object)componentsInChildren[i] != (Object)null && (Object)(object)componentsInChildren[i].rope == (Object)(object)reefController)
				{
					BindWinch(componentsInChildren[i]);
					return;
				}
			}
			RestoreWinch();
		}

		private void LateUpdate()
		{
			if (initialized && refreshClothPending)
			{
				refreshClothPending = false;
				RefreshCloth();
				((Behaviour)this).enabled = false;
			}
		}

		private void OnDestroy()
		{
			RestoreWinch();
		}

		private void ApplyControllerDirection()
		{
			float num = Mathf.Clamp01(sail.currentUnroll);
			reefController.reverseReefing = true;
			((RopeController)reefController).currentLength = 1f - num;
			((RopeController)reefController).changed = true;
		}

		private void ApplyRectangleJunkClothDynamics()
		{
			sail.cloth.bendingStiffness = 0.7f;
			sail.cloth.clothSolverFrequency = 120f;
			windCloth.staticMultiplier = 1f;
		}

		private void BindWinch(GPButtonRopeWinch winch)
		{
			if ((Object)(object)boundWinch == (Object)(object)winch)
			{
				winch.reverseWindResistance = true;
				return;
			}
			RestoreWinch();
			boundWinch = winch;
			originalWinchReverseResistance = winch.reverseWindResistance;
			winch.reverseWindResistance = true;
		}

		private void RestoreWinch()
		{
			if ((Object)(object)boundWinch != (Object)null)
			{
				boundWinch.reverseWindResistance = originalWinchReverseResistance;
			}
			boundWinch = null;
		}

		private void RefreshCloth()
		{
			if ((Object)(object)reefEffect == (Object)null || !((Behaviour)reefEffect).isActiveAndEnabled)
			{
				return;
			}
			try
			{
				reefEffect.RefreshCloth();
			}
			catch (Exception ex)
			{
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogWarning((object)("Could not refresh Rectangle Junk cloth for " + ((Object)this).name + ": " + ex.Message));
				}
			}
		}

		private bool Fail(string reason)
		{
			if (!warningLogged)
			{
				warningLogged = true;
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogWarning((object)("Could not initialize Rectangle Junk rig for " + ((Object)this).name + ": " + reason + "."));
				}
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(PrefabsDirectory), "Start")]
	[HarmonyAfter(new string[] { "com.nandbrew.shipyardexpansion" })]
	internal static class RectangleJunkPrefabPatch
	{
		private static void Prefix(PrefabsDirectory __instance)
		{
			RectangleJunkSails.EnsureRegistered(__instance);
		}
	}
	[HarmonyPatch(typeof(Shipyard), "ActivateDocuments")]
	[HarmonyAfter(new string[] { "com.nandbrew.shipyardexpansion" })]
	internal static class RectangleJunkShipyardPatch
	{
		private static readonly HashSet<string> SaleSceneNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "island 9 E Dragon Cliffs", "island 27 Lagoon SwampShipyard" };

		private static void Prefix(Shipyard __instance, ref GameObject[] ___sailPrefabs)
		{
			PrefabsDirectory instance = PrefabsDirectory.instance;
			bool num = RectangleJunkSails.EnsureRegistered(instance);
			bool flag = IsSaleLocation(__instance);
			if (num && flag)
			{
				RectangleJunkSails.AddToShipyard(instance, ref ___sailPrefabs);
			}
			else
			{
				RectangleJunkSails.RemoveFromShipyard(ref ___sailPrefabs);
			}
			if (!num && flag)
			{
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogError((object)"Rectangle Junk sails are unavailable: indices 200/201 could not be registered.");
				}
			}
		}

		internal static bool IsSaleLocation(Shipyard shipyard)
		{
			//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)
			if ((Object)(object)shipyard == (Object)null)
			{
				return false;
			}
			Scene scene = ((Component)shipyard).gameObject.scene;
			if (((Scene)(ref scene)).IsValid())
			{
				return IsSaleSceneName(((Scene)(ref scene)).name);
			}
			return false;
		}

		internal static bool IsSaleSceneName(string sceneName)
		{
			if (!string.IsNullOrEmpty(sceneName))
			{
				return SaleSceneNames.Contains(sceneName);
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(WindCloth), "Update")]
	internal static class RectangleJunkWindClothPatch
	{
		private static void Prefix(Sail ___sail, Cloth ___cloth, out bool __state)
		{
			__state = RectangleJunkSails.IsRectangle(___sail) && (Object)(object)___cloth != (Object)null;
		}

		private static void Postfix(Sail ___sail, Cloth ___cloth, bool __state)
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_000a: 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)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Unknown result type (might be due to invalid IL or missing references)
			//IL_0024: Unknown result type (might be due to invalid IL or missing references)
			//IL_0025: 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)
			if (__state)
			{
				Vector3 externalAcceleration = ___cloth.externalAcceleration;
				Vector3 val = Vector3.Project(externalAcceleration, ((Component)___sail).transform.up);
				Vector3 val2 = externalAcceleration - val;
				___cloth.externalAcceleration = val + val2 * 0.5f;
			}
		}
	}
	[HarmonyPatch(typeof(Mast), "UpdateControllerAttachments")]
	internal static class RectangleJunkMastAttachmentPatch
	{
		private static void Prefix(Mast __instance)
		{
			RectangleJunkSails.PrepareMast(__instance);
		}

		private static void Postfix(Mast __instance)
		{
			RectangleJunkSails.ApplyToMast(__instance);
		}
	}
	[HarmonyPatch(typeof(Mast), "UpdateWinchesEnabled")]
	internal static class RectangleJunkWinchVisibilityPatch
	{
		private static void Postfix(Mast __instance)
		{
			RectangleJunkSails.SynchronizeWinchVisibility(__instance);
		}
	}
	[HarmonyPatch(typeof(ShipyardSailColChecker), "RunColCheck")]
	internal static class RectangleJunkCollisionCheckerScalePatch
	{
		private static void Postfix(Sail ___sail)
		{
			RectangleJunkSails.SynchronizeCollisionCheckerScale(___sail);
		}
	}
	[HarmonyPatch(typeof(Mast), "AttachSailToMast")]
	internal static class RectangleJunkShipyardInstallPatch
	{
		private static void Prefix(GameObject sailObject, out bool __state)
		{
			Sail val = (((Object)(object)sailObject != (Object)null) ? sailObject.GetComponent<Sail>() : null);
			__state = (Object)(object)GameState.currentShipyard != (Object)null && (Object)(object)val != (Object)null && !val.IsInstalled() && RectangleJunkSails.IsRectangle(sailObject);
		}

		private static void Postfix(GameObject sailObject, bool __state)
		{
			if (__state)
			{
				RectangleJunkSails.FurlNewlyInstalledSail(sailObject);
			}
		}
	}
	internal static class RectangleJunkRenderState
	{
		private sealed class RendererState
		{
			private readonly Material[] materials;

			private readonly ShadowCastingMode shadowCastingMode;

			private readonly bool receiveShadows;

			private readonly LightProbeUsage lightProbeUsage;

			private readonly ReflectionProbeUsage reflectionProbeUsage;

			private readonly MotionVectorGenerationMode motionVectors;

			private readonly bool allowOcclusionWhenDynamic;

			private readonly int sortingLayerId;

			private readonly int sortingOrder;

			private readonly int layer;

			private RendererState(Renderer source)
			{
				//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_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_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_0044: Unknown result type (might be due to invalid IL or missing references)
				//IL_0049: Unknown result type (might be due to invalid IL or missing references)
				materials = source.sharedMaterials;
				shadowCastingMode = source.shadowCastingMode;
				receiveShadows = source.receiveShadows;
				lightProbeUsage = source.lightProbeUsage;
				reflectionProbeUsage = source.reflectionProbeUsage;
				motionVectors = source.motionVectorGenerationMode;
				allowOcclusionWhenDynamic = source.allowOcclusionWhenDynamic;
				sortingLayerId = source.sortingLayerID;
				sortingOrder = source.sortingOrder;
				layer = ((Component)source).gameObject.layer;
			}

			internal static RendererState Capture(Renderer source)
			{
				if ((Object)(object)source == (Object)null || source.sharedMaterials.Length == 0 || (Object)(object)source.sharedMaterials[0] == (Object)null)
				{
					throw new InvalidOperationException("the live Junk Square renderer state is incomplete");
				}
				return new RendererState(source);
			}

			internal void ApplyShared(Renderer target)
			{
				target.sharedMaterials = materials;
				ApplyRendererSettings(target);
			}

			internal bool ApplyInstances(Renderer target)
			{
				bool flag = RendererSettingsDiffer(target);
				Material[] array = target.materials;
				for (int i = 0; i < array.Length; i++)
				{
					Material val = array[i];
					Material val2 = materials[Math.Min(i, materials.Length - 1)];
					if (!((Object)(object)val == (Object)null) && !((Object)(object)val2 == (Object)null))
					{
						flag |= MaterialStateDiffers(val, val2);
						ApplyMaterialState(val, val2);
					}
				}
				target.materials = array;
				ApplyRendererSettings(target);
				return flag;
			}

			private void ApplyRendererSettings(Renderer target)
			{
				//IL_0002: Unknown result type (might be due to invalid IL or missing references)
				//IL_001a: 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_0032: Unknown result type (might be due to invalid IL or missing references)
				target.shadowCastingMode = shadowCastingMode;
				target.receiveShadows = receiveShadows;
				target.lightProbeUsage = lightProbeUsage;
				target.reflectionProbeUsage = reflectionProbeUsage;
				target.motionVectorGenerationMode = motionVectors;
				target.allowOcclusionWhenDynamic = allowOcclusionWhenDynamic;
				target.sortingLayerID = sortingLayerId;
				target.sortingOrder = sortingOrder;
				((Component)target).gameObject.layer = layer;
			}

			private bool RendererSettingsDiffer(Renderer target)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0007: Unknown result type (might be due to invalid IL or missing references)
				//IL_001d: 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_002b: 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_0039: 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)
				if (target.shadowCastingMode == shadowCastingMode && target.receiveShadows == receiveShadows && target.lightProbeUsage == lightProbeUsage && target.reflectionProbeUsage == reflectionProbeUsage && target.motionVectorGenerationMode == motionVectors && target.allowOcclusionWhenDynamic == allowOcclusionWhenDynamic && target.sortingLayerID == sortingLayerId && target.sortingOrder == sortingOrder)
				{
					return ((Component)target).gameObject.layer != layer;
				}
				return true;
			}

			private static bool MaterialStateDiffers(Material target, Material source)
			{
				if ((Object)(object)target.shader != (Object)(object)source.shader || target.renderQueue != source.renderQueue || target.enableInstancing != source.enableInstancing || target.doubleSidedGI != source.doubleSidedGI || target.GetTag("RenderType", false, string.Empty) != source.GetTag("RenderType", false, string.Empty) || !KeywordsMatch(target.shaderKeywords, source.shaderKeywords))
				{
					return true;
				}
				if (!FloatPropertyDiffers(target, source, "_ZWrite") && !FloatPropertyDiffers(target, source, "_SrcBlend") && !FloatPropertyDiffers(target, source, "_DstBlend"))
				{
					return FloatPropertyDiffers(target, source, "_Mode");
				}
				return true;
			}

			private static void ApplyMaterialState(Material target, Material source)
			{
				//IL_0023: Unknown result type (might be due to invalid IL or missing references)
				//IL_001b: 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_0034: 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_0039: 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_0049: 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_00a2: 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_00e6: 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)
				bool num = target.HasProperty("_MainTex");
				Texture mainTexture = (num ? target.mainTexture : null);
				Vector2 mainTextureScale = (num ? target.mainTextureScale : Vector2.one);
				Vector2 mainTextureOffset = (num ? target.mainTextureOffset : Vector2.zero);
				bool flag = target.HasProperty("_Color");
				Color val = (flag ? target.GetColor("_Color") : Color.white);
				target.shader = source.shader;
				target.CopyPropertiesFromMaterial(source);
				target.shaderKeywords = source.shaderKeywords;
				target.renderQueue = source.renderQueue;
				target.enableInstancing = source.enableInstancing;
				target.doubleSidedGI = source.doubleSidedGI;
				target.globalIlluminationFlags = source.globalIlluminationFlags;
				target.SetOverrideTag("RenderType", source.GetTag("RenderType", false, string.Empty));
				if (num && target.HasProperty("_MainTex"))
				{
					target.mainTexture = mainTexture;
					target.mainTextureScale = mainTextureScale;
					target.mainTextureOffset = mainTextureOffset;
				}
				if (flag && target.HasProperty("_Color"))
				{
					target.SetColor("_Color", val);
				}
			}

			private static bool FloatPropertyDiffers(Material target, Material source, string propertyName)
			{
				if (!target.HasProperty(propertyName) || !source.HasProperty(propertyName))
				{
					return false;
				}
				return !Mathf.Approximately(target.GetFloat(propertyName), source.GetFloat(propertyName));
			}

			private static bool KeywordsMatch(string[] first, string[] second)
			{
				first = first ?? Array.Empty<string>();
				second = second ?? Array.Empty<string>();
				if (first.Length != second.Length)
				{
					return false;
				}
				HashSet<string> hashSet = new HashSet<string>(second);
				for (int i = 0; i < first.Length; i++)
				{
					if (!hashSet.Contains(first[i]))
					{
						return false;
					}
				}
				return true;
			}
		}

		private const string MainTextureProperty = "_MainTex";

		private const string ColorProperty = "_Color";

		private const string RenderTypeTag = "RenderType";

		private static readonly string[] BoomRendererNames = new string[5] { "boom_010", "boom_011", "boom_012", "boom_013", "boom_014" };

		private static RendererState clothState;

		private static RendererState furledState;

		private static bool correctionLogged;

		internal static void CaptureAndApply(GameObject targetSail, GameObject sourceSail, Renderer targetCloth, Renderer sourceCloth, Renderer targetFurled, Renderer sourceFurled)
		{
			clothState = RendererState.Capture(sourceCloth);
			furledState = RendererState.Capture(sourceFurled);
			clothState.ApplyShared(targetCloth);
			furledState.ApplyShared(targetFurled);
			ApplyBoomStates(targetSail, sourceSail);
		}

		private static void ApplyBoomStates(GameObject targetSail, GameObject sourceSail)
		{
			for (int i = 0; i < BoomRendererNames.Length; i++)
			{
				string rendererName = BoomRendererNames[i];
				Renderer target = FindUniqueRenderer(targetSail, rendererName);
				RendererState.Capture(FindUniqueRenderer(sourceSail, rendererName)).ApplyShared(target);
			}
		}

		private static Renderer FindUniqueRenderer(GameObject root, string rendererName)
		{
			Renderer val = null;
			Renderer[] array = (((Object)(object)root != (Object)null) ? root.GetComponentsInChildren<Renderer>(true) : Array.Empty<Renderer>());
			foreach (Renderer val2 in array)
			{
				if (!(((Object)((Component)val2).gameObject).name != rendererName))
				{
					if ((Object)(object)val != (Object)null)
					{
						throw new InvalidOperationException("multiple renderers named " + rendererName + " were found in the Junk Square hierarchy");
					}
					val = val2;
				}
			}
			if ((Object)(object)val == (Object)null)
			{
				throw new InvalidOperationException("renderer " + rendererName + " is missing from the Junk Square hierarchy");
			}
			return val;
		}

		internal static void Normalize(GameObject sailObject)
		{
			if (clothState == null || furledState == null || !RectangleJunkSails.IsRectangle(sailObject))
			{
				return;
			}
			Sail component = sailObject.GetComponent<Sail>();
			Renderer val = (((Object)(object)component != (Object)null && (Object)(object)component.cloth != (Object)null) ? ((Component)component.cloth).GetComponent<Renderer>() : null);
			ReefEffectAnimUniversal componentInChildren = sailObject.GetComponentInChildren<ReefEffectAnimUniversal>(true);
			Renderer val2 = (((Object)(object)componentInChildren != (Object)null) ? componentInChildren.furledSail : null);
			if (!((Object)(object)val == (Object)null) && !((Object)(object)val2 == (Object)null) && (clothState.ApplyInstances(val) | furledState.ApplyInstances(val2)) && !correctionLogged)
			{
				correctionLogged = true;
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogWarning((object)"Corrected Rectangle Junk renderer state after a runtime material change so Sailwind fog can use the stock Junk Square shader and depth settings.");
				}
			}
		}

		internal static void Reset()
		{
			clothState = null;
			furledState = null;
			correctionLogged = false;
		}
	}
	[HarmonyPatch(typeof(Sail), "Start")]
	internal static class RectangleJunkSailRenderStartPatch
	{
		private static void Postfix(Sail __instance)
		{
			RectangleJunkRenderState.Normalize(((Component)__instance).gameObject);
		}
	}
	[HarmonyPatch(typeof(Sail), "ChangeSailColor")]
	internal static class RectangleJunkSailColorRenderPatch
	{
		private static void Postfix(Sail __instance)
		{
			RectangleJunkRenderState.Normalize(((Component)__instance).gameObject);
		}
	}
	[HarmonyPatch(typeof(SailTextureChanger), "UpdateMaterial")]
	[HarmonyAfter(new string[] { "com.nandbrew.shipyardexpansion" })]
	internal static class RectangleJunkSailTextureRenderPatch
	{
		private static void Postfix(SailTextureChanger __instance)
		{
			RectangleJunkRenderState.Normalize(((Component)__instance).gameObject);
		}
	}
	internal sealed class RectangleJunkWinchVisibility : MonoBehaviour
	{
		private readonly List<Renderer> hiddenRenderers = new List<Renderer>();

		private readonly List<Collider> hiddenColliders = new List<Collider>();

		internal void SetHidden(bool shouldHide)
		{
			Restore();
			if (!shouldHide)
			{
				return;
			}
			Renderer[] componentsInChildren = ((Component)this).GetComponentsInChildren<Renderer>(true);
			foreach (Renderer val in componentsInChildren)
			{
				if ((Object)(object)val != (Object)null && val.enabled)
				{
					hiddenRenderers.Add(val);
					val.enabled = false;
				}
			}
			Collider[] componentsInChildren2 = ((Component)this).GetComponentsInChildren<Collider>(true);
			foreach (Collider val2 in componentsInChildren2)
			{
				if ((Object)(object)val2 != (Object)null && val2.enabled)
				{
					hiddenColliders.Add(val2);
					val2.enabled = false;
				}
			}
		}

		private void OnDestroy()
		{
			Restore();
		}

		private void Restore()
		{
			for (int i = 0; i < hiddenRenderers.Count; i++)
			{
				if ((Object)(object)hiddenRenderers[i] != (Object)null)
				{
					hiddenRenderers[i].enabled = true;
				}
			}
			for (int j = 0; j < hiddenColliders.Count; j++)
			{
				if ((Object)(object)hiddenColliders[j] != (Object)null)
				{
					hiddenColliders[j].enabled = true;
				}
			}
			hiddenRenderers.Clear();
			hiddenColliders.Clear();
		}
	}
	internal static class RectangleJunkSailIndexMigration
	{
		private const int CurrentLayoutVersion = 1;

		internal static void PrepareLoad(SaveableBoatCustomization customization, SaveBoatCustomizationData data)
		{
			if (data == null || data.sails == null)
			{
				return;
			}
			int sceneIndex;
			bool flag = TryGetSceneIndex(customization, out sceneIndex);
			if (flag && IsCurrent(sceneIndex))
			{
				return;
			}
			int num;
			int num2;
			if (flag)
			{
				num = (HasLegacyModFootprint(sceneIndex) ? 1 : 0);
				if (num != 0)
				{
					num2 = 1;
					goto IL_003d;
				}
			}
			else
			{
				num = 0;
			}
			num2 = (RectangleJunkSails.CanMigrateLegacyIndex(131) ? 1 : 0);
			goto IL_003d;
			IL_003d:
			bool migrateNarrow = (byte)num2 != 0;
			bool migrateWide = num != 0 || RectangleJunkSails.CanMigrateLegacyIndex(132);
			int num3 = RemapLegacyIndices(data.sails, migrateNarrow, migrateWide);
			if (flag)
			{
				MarkCurrent(sceneIndex);
			}
			if (num3 > 0)
			{
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogInfo((object)("Migrated " + num3 + " Rectangle Junk sail index record(s) from 131/132 to 200/201" + (flag ? (" for boat scene " + sceneIndex) : string.Empty) + "."));
				}
			}
		}

		internal static void MarkCurrent(SaveableBoatCustomization customization)
		{
			if (TryGetSceneIndex(customization, out var sceneIndex))
			{
				MarkCurrent(sceneIndex);
			}
		}

		internal static int RemapLegacyIndices(IList<SaveSailData> sails, bool migrateNarrow, bool migrateWide)
		{
			if (sails == null)
			{
				return 0;
			}
			int num = 0;
			for (int i = 0; i < sails.Count; i++)
			{
				SaveSailData val = sails[i];
				if (val != null)
				{
					if (migrateNarrow && val.prefabIndex == 131)
					{
						val.prefabIndex = 200;
						num++;
					}
					else if (migrateWide && val.prefabIndex == 132)
					{
						val.prefabIndex = 201;
						num++;
					}
				}
			}
			return num;
		}

		private static bool TryGetSceneIndex(SaveableBoatCustomization customization, out int sceneIndex)
		{
			SaveableObject val = (((Object)(object)customization != (Object)null) ? ((Component)customization).GetComponent<SaveableObject>() : null);
			if ((Object)(object)val != (Object)null)
			{
				sceneIndex = val.sceneIndex;
				return true;
			}
			sceneIndex = -1;
			return false;
		}

		private static bool IsCurrent(int sceneIndex)
		{
			if (GameState.modData == null)
			{
				return false;
			}
			if (GameState.modData.TryGetValue(GetLayoutKey(sceneIndex), out var value) && int.TryParse(value, out var result))
			{
				return result >= 1;
			}
			return false;
		}

		private static bool HasLegacyModFootprint(int sceneIndex)
		{
			if (GameState.modData == null)
			{
				return false;
			}
			string text = "dogeggz.unstayedjunksailmast." + sceneIndex;
			if (!GameState.modData.ContainsKey(text + ".activeMasts"))
			{
				return GameState.modData.ContainsKey(text + ".indexLayoutVersion");
			}
			return true;
		}

		private static void MarkCurrent(int sceneIndex)
		{
			if (GameState.modData != null)
			{
				GameState.modData[GetLayoutKey(sceneIndex)] = 1.ToString();
			}
		}

		private static string GetLayoutKey(int sceneIndex)
		{
			return "dogeggz.unstayedjunksailmast." + sceneIndex + ".rectangleSailIndexLayoutVersion";
		}
	}
	[HarmonyPatch(typeof(SaveLoadManager), "LoadNeeds")]
	[HarmonyBefore(new string[] { "com.nandbrew.shipyardexpansion" })]
	internal static class EarlyModDataLoadPatch
	{
		private static void Postfix(SaveContainer __0)
		{
			GameState.modData = ((__0 != null && __0.modData != null) ? __0.modData : new Dictionary<string, string>());
			UnstayedMastIndexCoordinator.RebindFromLoadedModData();
		}
	}
	[HarmonyPatch(typeof(SaveableBoatCustomization), "Awake")]
	[HarmonyAfter(new string[] { "com.nandbrew.shipyardexpansion" })]
	internal static class BoatCustomizationAwakePatch
	{
		private static void Postfix(SaveableBoatCustomization __instance, BoatCustomParts ___parts, BoatRefs ___refs)
		{
			try
			{
				UnstayedMastBuilder.TryBuild(__instance, ___parts, ___refs);
			}
			catch (Exception ex)
			{
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogError((object)("Could not build unstayed mast options on " + ((Object)__instance).name + ": " + ex));
				}
			}
		}
	}
	[HarmonyPatch(typeof(BoatRefs), "RegisterMast")]
	internal static class IdempotentUnstayedMastRegistrationPatch
	{
		private static bool Prefix(BoatRefs __instance, Mast mast)
		{
			if ((Object)(object)__instance == (Object)null || (Object)(object)mast == (Object)null || (Object)(object)((Component)mast).GetComponent<UnstayedMastMarker>() == (Object)null || __instance.masts == null)
			{
				return true;
			}
			int orderIndex = mast.orderIndex;
			if (orderIndex >= 0 && orderIndex < __instance.masts.Length)
			{
				return (Object)(object)__instance.masts[orderIndex] != (Object)(object)mast;
			}
			return true;
		}
	}
	[HarmonyPatch(typeof(ShipyardUI), "SailMastCompatible")]
	internal static class SailMastCompatibilityPatch
	{
		private static bool Prefix(GameObject sailPrefab, ref bool __result)
		{
			Shipyard currentShipyard = GameState.currentShipyard;
			Mast val = (((Object)(object)currentShipyard != (Object)null) ? currentShipyard.sailInstaller.GetCurrentMast() : null);
			if ((Object)(object)val == (Object)null || (Object)(object)((Component)val).GetComponent<UnstayedMastMarker>() == (Object)null)
			{
				return true;
			}
			__result = UnstayedSailRules.IsAllowed(sailPrefab);
			return false;
		}
	}
	[HarmonyPatch(typeof(ShipyardSailInstaller), "AddNewSail")]
	internal static class AddNewSailPatch
	{
		private static bool Prefix(ShipyardSailInstaller __instance, GameObject sailObject)
		{
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			Mast currentMast = __instance.GetCurrentMast();
			if ((Object)(object)currentMast == (Object)null || (Object)(object)((Component)currentMast).GetComponent<UnstayedMastMarker>() == (Object)null || UnstayedSailRules.IsAllowed(sailObject))
			{
				return true;
			}
			Sail val = (((Object)(object)sailObject != (Object)null) ? sailObject.GetComponent<Sail>() : null);
			ManualLogSource logSource = Plugin.LogSource;
			if (logSource != null)
			{
				logSource.LogWarning((object)("Rejected " + (((Object)(object)val != (Object)null) ? val.sailName : "unknown sail") + " on an unstayed mast."));
			}
			if ((Object)(object)sailObject != (Object)null)
			{
				Scene scene = sailObject.scene;
				if (((Scene)(ref scene)).IsValid())
				{
					Object.Destroy((Object)(object)sailObject);
				}
			}
			return false;
		}
	}
	[HarmonyPatch(typeof(ShipyardPartsInstaller), "UpdateOrder")]
	internal static class ShipyardOrderNormalizationPatch
	{
		private static void Prefix(BoatCustomParts ___currentParts, BoatPartsOrder ___currentOrder)
		{
			List<int> list = UnstayedSelectionRules.NormalizeOrder(___currentParts, ___currentOrder);
			if ((Object)(object)ShipyardUI.instance == (Object)null || (Object)(object)___currentParts == (Object)null || ___currentOrder == null)
			{
				return;
			}
			for (int i = 0; i < list.Count; i++)
			{
				int num = list[i];
				if (num >= 0 && num < ___currentParts.availableParts.Count && num < ___currentOrder.orderedOptions.Length)
				{
					BoatPart val = ___currentParts.availableParts[num];
					int num2 = ___currentOrder.orderedOptions[num];
					if (num2 >= 0 && num2 < val.partOptions.Count)
					{
						ShipyardUI.instance.ChangePartsOptionText(num, val.partOptions[num2].optionName);
					}
				}
			}
		}
	}
	[HarmonyPatch(typeof(SaveableBoatCustomization), "LoadData")]
	[HarmonyAfter(new string[] { "com.nandbrew.shipyardexpansion" })]
	internal static class BoatCustomizationLoadPatch
	{
		private static void Prefix(SaveableBoatCustomization __instance, SaveBoatCustomizationData data)
		{
			UnstayedSaveCompatibility.PrepareLoad(__instance, data);
		}

		private static void Postfix(SaveableBoatCustomization __instance)
		{
			BoatCustomParts component = ((Component)__instance).GetComponent<BoatCustomParts>();
			if (UnstayedSelectionRules.NormalizeActive(component))
			{
				component.RefreshParts();
			}
		}
	}
	[HarmonyPatch(typeof(SaveableBoatCustomization), "GetData")]
	internal static class BoatCustomizationSavePatch
	{
		private static void Postfix(SaveableBoatCustomization __instance, SaveBoatCustomizationData __result)
		{
			UnstayedSaveCompatibility.RecordActiveOptions(__instance, __result);
		}
	}
	internal static class UnstayedSailRules
	{
		internal static bool IsAllowed(GameObject sailObject)
		{
			//IL_002b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0031: Invalid comparison between Unknown and I4
			//IL_0034: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Invalid comparison between Unknown and I4
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			Sail val = (((Object)(object)sailObject != (Object)null) ? sailObject.GetComponent<Sail>() : null);
			if ((Object)(object)val == (Object)null)
			{
				return false;
			}
			string text = UnstayedNameRules.Normalize(val.sailName);
			if ((int)val.category != 2 && ((int)val.category != 4 || !(text == "fin sail")))
			{
				if ((int)val.category == 0)
				{
					return UnstayedNameRules.IsNamedVariant(text, "junk square");
				}
				return false;
			}
			return true;
		}

		internal static bool UsesDiameterCompensation(Sail sail)
		{
			if ((Object)(object)sail != (Object)null && !RectangleJunkSails.IsRectangle(((Component)sail).gameObject))
			{
				return IsAllowed(((Component)sail).gameObject);
			}
			return false;
		}
	}
	internal static class UnstayedSelectionRules
	{
		internal static List<int> NormalizeOrder(BoatCustomParts parts, BoatPartsOrder order)
		{
			List<int> list = new List<int>();
			if ((Object)(object)parts == (Object)null || order == null || !UnstayedBoatRegistry.TryGet(parts, out var profile))
			{
				return list;
			}
			for (int i = 0; i < profile.Masts.Count; i++)
			{
				UnstayedMastProfile unstayedMastProfile = profile.Masts[i];
				int num = parts.availableParts.IndexOf(unstayedMastProfile.MastPart);
				int num2 = unstayedMastProfile.MastPart.partOptions.IndexOf(unstayedMastProfile.UnstayedOption);
				if (num >= 0 && num2 >= 0 && num < order.orderedOptions.Length && order.orderedOptions[num] == num2)
				{
					ForceEmptySelections(parts, order.orderedOptions, unstayedMastProfile.RestrictedSelections, list);
				}
			}
			return list;
		}

		internal static bool NormalizeActive(BoatCustomParts parts)
		{
			if ((Object)(object)parts == (Object)null || !UnstayedBoatRegistry.TryGet(parts, out var profile))
			{
				return false;
			}
			bool result = false;
			for (int i = 0; i < profile.Masts.Count; i++)
			{
				UnstayedMastProfile unstayedMastProfile = profile.Masts[i];
				int num = unstayedMastProfile.MastPart.partOptions.IndexOf(unstayedMastProfile.UnstayedOption);
				if (num < 0 || unstayedMastProfile.MastPart.activeOption != num)
				{
					continue;
				}
				for (int j = 0; j < unstayedMastProfile.RestrictedSelections.Count; j++)
				{
					RestrictedPartSelection restrictedPartSelection = unstayedMastProfile.RestrictedSelections[j];
					if (restrictedPartSelection.Kind == RestrictedPartKind.Rigging && !((Object)(object)restrictedPartSelection.EmptyOption == (Object)null))
					{
						int num2 = restrictedPartSelection.Part.partOptions.IndexOf(restrictedPartSelection.EmptyOption);
						if (num2 >= 0 && restrictedPartSelection.Part.activeOption != num2)
						{
							restrictedPartSelection.Part.activeOption = num2;
							result = true;
						}
					}
				}
			}
			return result;
		}

		private static void ForceEmptySelections(BoatCustomParts parts, int[] orderedOptions, List<RestrictedPartSelection> selections, List<int> changedPartIndices)
		{
			for (int i = 0; i < selections.Count; i++)
			{
				RestrictedPartSelection restrictedPartSelection = selections[i];
				if (restrictedPartSelection.Kind != RestrictedPartKind.Rigging || (Object)(object)restrictedPartSelection.EmptyOption == (Object)null)
				{
					continue;
				}
				int num = parts.availableParts.IndexOf(restrictedPartSelection.Part);
				int num2 = restrictedPartSelection.Part.partOptions.IndexOf(restrictedPartSelection.EmptyOption);
				if (num >= 0 && num < orderedOptions.Length && num2 >= 0 && orderedOptions[num] != num2)
				{
					orderedOptions[num] = num2;
					if (!changedPartIndices.Contains(num))
					{
						changedPartIndices.Add(num);
					}
				}
			}
		}
	}
	internal static class UnstayedSaveCompatibility
	{
		private const char RecordSeparator = ';';

		private const char FieldSeparator = '=';

		private const int IndexLayoutVersion = 2;

		internal static void PrepareLoad(SaveableBoatCustomization customization, SaveBoatCustomizationData data)
		{
			RectangleJunkSailIndexMigration.PrepareLoad(customization, data);
			RemapSavedOptions(customization, data);
			MigrateMastIndices(customization, data);
		}

		internal static void RecordActiveOptions(SaveableBoatCustomization customization, SaveBoatCustomizationData data)
		{
			RectangleJunkSailIndexMigration.MarkCurrent(customization);
			BoatCustomParts component = ((Component)customization).GetComponent<BoatCustomParts>();
			if (data == null || !UnstayedBoatRegistry.TryGet(component, out var profile) || GameState.modData == null || (GetIndexLayoutVersion(profile.SceneIndex) < 2 && HasActiveMastRecords(profile.SceneIndex)))
			{
				return;
			}
			List<string> list = new List<string>();
			for (int i = 0; i < profile.Masts.Count; i++)
			{
				UnstayedMastProfile unstayedMastProfile = profile.Masts[i];
				int num = component.availableParts.IndexOf(unstayedMastProfile.MastPart);
				int num2 = unstayedMastProfile.MastPart.partOptions.IndexOf(unstayedMastProfile.UnstayedOption);
				if (num >= 0 && num2 >= 0 && num < data.partActiveOptions.Count && data.partActiveOptions[num] == num2)
				{
					UnstayedMastSourceIdentity unstayedMastSourceIdentity = (((Object)(object)unstayedMastProfile.Marker != (Object)null) ? unstayedMastProfile.Marker.Identity : null);
					if (unstayedMastSourceIdentity != null && !string.IsNullOrEmpty(unstayedMastSourceIdentity.StableId))
					{
						list.Add(num + "=" + Uri.EscapeDataString(unstayedMastSourceIdentity.StableId));
					}
				}
			}
			string key = GetKey(profile.SceneIndex);
			if (list.Count == 0)
			{
				GameState.modData.Remove(key);
			}
			else
			{
				GameState.modData[key] = string.Join(';'.ToString(), list.ToArray());
			}
			GameState.modData[GetIndexLayoutKey(profile.SceneIndex)] = 2.ToString();
		}

		private static void RemapSavedOptions(SaveableBoatCustomization customization, SaveBoatCustomizationData data)
		{
			BoatCustomParts component = ((Component)customization).GetComponent<BoatCustomParts>();
			if (data == null || data.partActiveOptions == null || !UnstayedBoatRegistry.TryGet(component, out var profile) || GameState.modData == null || !GameState.modData.TryGetValue(GetKey(profile.SceneIndex), out var value) || string.IsNullOrEmpty(value))
			{
				return;
			}
			string[] array = value.Split(new char[1] { ';' });
			for (int i = 0; i < array.Length; i++)
			{
				int num = array[i].IndexOf('=');
				if (num <= 0 || !int.TryParse(array[i].Substring(0, num), out var result) || result < 0 || result >= data.partActiveOptions.Count)
				{
					continue;
				}
				string persistedSourceId;
				try
				{
					persistedSourceId = Uri.UnescapeDataString(array[i].Substring(num + 1));
				}
				catch (UriFormatException)
				{
					continue;
				}
				UnstayedMastProfile unstayedMastProfile = FindMatchingMastProfile(profile, component, persistedSourceId, result);
				if (unstayedMastProfile != null)
				{
					int num2 = component.availableParts.IndexOf(unstayedMastProfile.MastPart);
					int num3 = unstayedMastProfile.MastPart.partOptions.IndexOf(unstayedMastProfile.UnstayedOption);
					if (num2 >= 0 && num2 < data.partActiveOptions.Count && num3 >= 0)
					{
						data.partActiveOptions[num2] = num3;
					}
				}
			}
		}

		private static UnstayedMastProfile FindMatchingMastProfile(UnstayedBoatProfile profile, BoatCustomParts parts, string persistedSourceId, int savedPartIndex)
		{
			UnstayedMastProfile result = null;
			int num = 0;
			bool flag = false;
			for (int i = 0; i < profile.Masts.Count; i++)
			{
				UnstayedMastProfile unstayedMastProfile = profile.Masts[i];
				UnstayedMastSourceIdentity unstayedMastSourceIdentity = (((Object)(object)unstayedMastProfile.Marker != (Object)null) ? unstayedMastProfile.Marker.Identity : null);
				if (unstayedMastSourceIdentity == null)
				{
					continue;
				}
				int num2 = unstayedMastSourceIdentity.GetPersistedMatchScore(persistedSourceId);
				if (num2 > 0)
				{
					if (parts.availableParts.IndexOf(unstayedMastProfile.MastPart) == savedPartIndex)
					{
						num2 += 5;
					}
					if (num2 > num)
					{
						num = num2;
						result = unstayedMastProfile;
						flag = false;
					}
					else if (num2 == num)
					{
						flag = true;
					}
				}
			}
			if (!flag)
			{
				return result;
			}
			ManualLogSource logSource = Plugin.LogSource;
			if (logSource != null)
			{
				logSource.LogWarning((object)("Did not remap an ambiguous saved unstayed mast source identity: " + persistedSourceId + "."));
			}
			return null;
		}

		private static void MigrateMastIndices(SaveableBoatCustomization customization, SaveBoatCustomizationData data)
		{
			BoatCustomParts component = ((Component)customization).GetComponent<BoatCustomParts>();
			if (data == null || !UnstayedBoatRegistry.TryGet(component, out var profile) || GameState.modData == null)
			{
				return;
			}
			HashSet<int> hashSet = new HashSet<int>();
			if (profile.RetiredMastIndices != null)
			{
				for (int i = 0; i < profile.RetiredMastIndices.Count; i++)
				{
					hashSet.Add(profile.RetiredMastIndices[i]);
				}
			}
			if (GetIndexLayoutVersion(profile.SceneIndex) < 2 && HasActiveMastRecords(profile.SceneIndex))
			{
				HashSet<int> hashSet2 = FindForeignExtendedMastIndices(((Component)customization).transform);
				for (int j = 96; j < 128; j++)
				{
					if (!hashSet2.Contains(j))
					{
						hashSet.Add(j);
					}
				}
			}
			int num = RemoveSavedSails(data, hashSet);
			int num2 = RemoveShipyardExpansionSailRecords(profile.SceneIndex, hashSet);
			if (num > 0 || num2 > 0)
			{
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogWarning((object)("Migrated unstayed mast indices for scene " + profile.SceneIndex + "; discarded " + num + " legacy sail(s) and " + num2 + " Shipyard Expansion sail setting record(s). No refund is issued by this migration."));
				}
			}
			GameState.modData[GetIndexLayoutKey(profile.SceneIndex)] = 2.ToString();
		}

		private static int RemoveSavedSails(SaveBoatCustomizationData data, HashSet<int> discardedIndices)
		{
			if (data.sails == null || discardedIndices.Count == 0)
			{
				return 0;
			}
			int num = 0;
			for (int num2 = data.sails.Count - 1; num2 >= 0; num2--)
			{
				SaveSailData val = data.sails[num2];
				if (val != null && discardedIndices.Contains(val.mastIndex))
				{
					data.sails.RemoveAt(num2);
					num++;
				}
			}
			return num;
		}

		private static HashSet<int> FindForeignExtendedMastIndices(Transform boat)
		{
			HashSet<int> hashSet = new HashSet<int>();
			if ((Object)(object)boat == (Object)null)
			{
				return hashSet;
			}
			Mast[] componentsInChildren = ((Component)boat).GetComponentsInChildren<Mast>(true);
			foreach (Mast val in componentsInChildren)
			{
				if ((Object)(object)val != (Object)null && UnstayedMastIndexRules.IsExtendedIndex(val.orderIndex) && (Object)(object)((Component)val).GetComponent<UnstayedMastMarker>() == (Object)null)
				{
					hashSet.Add(val.orderIndex);
				}
			}
			return hashSet;
		}

		private static int RemoveShipyardExpansionSailRecords(int sceneIndex, HashSet<int> discardedIndices)
		{
			if (discardedIndices.Count == 0 || GameState.modData == null)
			{
				return 0;
			}
			string key = "SEboatSails." + sceneIndex;
			if (!GameState.modData.TryGetValue(key, out var value) || string.IsNullOrEmpty(value))
			{
				return 0;
			}
			int num = value.LastIndexOf('|');
			string obj = ((num >= 0) ? value.Substring(0, num) : value);
			string text = ((num >= 0) ? value.Substring(num) : string.Empty);
			string[] array = obj.Split(new char[1] { ')' }, StringSplitOptions.RemoveEmptyEntries);
			List<string> list = new List<string>();
			int num2 = 0;
			for (int i = 0; i < array.Length; i++)
			{
				int num3 = array[i].IndexOf('(');
				if (num3 <= 0 || !int.TryParse(array[i].Substring(0, num3), out var result) || !discardedIndices.Contains(result))
				{
					list.Add(array[i] + ")");
				}
				else
				{
					num2++;
				}
			}
			if (num2 > 0)
			{
				GameState.modData[key] = string.Concat(string.Concat(list.ToArray()), text);
			}
			return num2;
		}

		private static bool HasActiveMastRecords(int sceneIndex)
		{
			if (GameState.modData.TryGetValue(GetKey(sceneIndex), out var value))
			{
				return !string.IsNullOrEmpty(value);
			}
			return false;
		}

		private static int GetIndexLayoutVersion(int sceneIndex)
		{
			if (!GameState.modData.TryGetValue(GetIndexLayoutKey(sceneIndex), out var value) || !int.TryParse(value, out var result))
			{
				return 0;
			}
			return result;
		}

		private static string GetKey(int sceneIndex)
		{
			return "dogeggz.unstayedjunksailmast." + sceneIndex + ".activeMasts";
		}

		private static string GetIndexLayoutKey(int sceneIndex)
		{
			return "dogeggz.unstayedjunksailmast." + sceneIndex + ".indexLayoutVersion";
		}
	}
	internal sealed class UnstayedMastMarker : MonoBehaviour
	{
		internal UnstayedMastSourceIdentity Identity;
	}
	internal sealed class EmptyRestrictedPartMarker : MonoBehaviour
	{
	}
	internal sealed class UnstayedBoatMarker : MonoBehaviour
	{
		private void OnDestroy()
		{
			BoatCustomParts component = ((Component)this).GetComponent<BoatCustomParts>();
			if ((Object)(object)component != (Object)null)
			{
				UnstayedBoatRegistry.Unregister(component);
			}
		}
	}
	internal enum RestrictedPartKind
	{
		Rigging,
		RiggingAccessory,
		CrowsNest
	}
	internal sealed class RestrictedPartSelection
	{
		internal BoatPart Part;

		internal List<BoatPart> OwningMastParts;

		internal BoatPartOption EmptyOption;

		internal List<BoatPartOption> NonEmptyOptions;

		internal RestrictedPartKind Kind;
	}
	internal sealed class UnstayedMastProfile
	{
		internal BoatPart MastPart;

		internal BoatPartOption UnstayedOption;

		internal UnstayedMastMarker Marker;

		internal bool UsesFixedVanillaIndex;

		internal List<RestrictedPartSelection> RestrictedSelections;
	}
	internal sealed class UnstayedBoatProfile
	{
		internal BoatCustomParts Parts;

		internal BoatRefs Refs;

		internal int SceneIndex;

		internal List<UnstayedMastProfile> Masts;

		internal List<int> RetiredMastIndices;
	}
	internal static class UnstayedBoatRegistry
	{
		private static readonly Dictionary<BoatCustomParts, UnstayedBoatProfile> Profiles = new Dictionary<BoatCustomParts, UnstayedBoatProfile>();

		internal static void Register(UnstayedBoatProfile profile)
		{
			Profiles[profile.Parts] = profile;
		}

		internal static bool TryGet(BoatCustomParts parts, out UnstayedBoatProfile profile)
		{
			if ((Object)(object)parts == (Object)null)
			{
				profile = null;
				return false;
			}
			return Profiles.TryGetValue(parts, out profile);
		}

		internal static void Unregister(BoatCustomParts parts)
		{
			Profiles.Remove(parts);
		}

		internal static List<UnstayedBoatProfile> GetProfiles()
		{
			return new List<UnstayedBoatProfile>(Profiles.Values);
		}

		internal static void Clear()
		{
			Profiles.Clear();
		}
	}
	internal static class UnstayedMastBuilder
	{
		private sealed class SurfaceFittingPlacement
		{
			internal Transform Target;

			internal Vector2 RootRadial;

			internal float SourceSurfaceRadius;

			internal Vector2 SourceSurfaceRootPoint;

			internal Vector3 OriginalWorldPosition;
		}

		private static readonly string[] SmallJunkAnchorPaths = new string[3] { "junk small/structure/mast", "junk small/structure/mast_center", "junk small/structure/mast_001" };

		private static readonly string[] MediumJunkAnchorPaths = new string[5] { "junk medium (actual)/structure/mast_mid_0", "junk medium (actual)/structure/mast_mid_1", "junk medium (actual)/structure/mast_mizzen_0", "junk medium (actual)/structure/mast_mizzen_1", "junk medium (actual)/structure/mast_front_" };

		private static readonly string[] LargeJunkAnchorPaths = new string[4] { "junk large (3)/junk large (3)/structure/masts_structure/mast_main_1", "junk large (3)/junk large (3)/structure/masts_structure/mast_main_2", "junk large (3)/junk large (3)/structure/masts_structure/mast_back", "junk large (3)/junk large (3)/structure/masts_structure/mast_front" };

		private const float DiameterScale = 1.41f;

		private const float MaxSurfaceFittingOffset = 1f;

		private const string ScaledMastBodyName = "UJSM_scaled_mast_body";

		private static readonly HashSet<string> SmallJunkRemovedNames = new HashSet<string>(StringComparer.OrdinalIgnoreCase) { "mast_holder", "mast_holder_001", "mast_holder_002" };

		private const float MaximumSurfaceCorrection = 1f;

		private const float MinimumMastSectionBand = 0.01f;

		private const float MastSectionBandFraction = 0.0025f;

		internal static void TryBuild(SaveableBoatCustomization customization, BoatCustomParts parts, BoatRefs refs)
		{
			if ((Object)(object)customization == (Object)null || (Object)(object)parts == (Object)null || (Object)(object)refs == (Object)null || (Object)(object)((Component)customization).GetComponent<UnstayedBoatMarker>() != (Object)null)
			{
				return;
			}
			SaveableObject component = ((Component)customization).GetComponent<SaveableObject>();
			if ((Object)(object)component == (Object)null || !IsSupportedBoat(component.sceneIndex) || !HasShipyardExpansionMastCapacity(refs))
			{
				return;
			}
			List<BoatPart> list = FindTargetMastParts(((Component)customization).transform, parts, component.sceneIndex);
			if (list.Count == 0)
			{
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogError((object)("No eligible mast part groups were found on " + ((Object)customization).name + "."));
				}
				return;
			}
			Dictionary<BoatPart, List<BoatPartOption>> dictionary = new Dictionary<BoatPart, List<BoatPartOption>>();
			for (int i = 0; i < list.Count; i++)
			{
				dictionary[list[i]] = GetEligibleSources(list[i]);
			}
			List<RestrictedPartSelection> selections = FindRestrictedSelections(((Component)customization).transform, parts, list, dictionary);
			List<UnstayedMastProfile> list2 = new List<UnstayedMastProfile>();
			UnstayedMastIndexAllocator unstayedMastIndexAllocator = new UnstayedMastIndexAllocator(((Component)customization).transform, component.sceneIndex);
			BoatCustomColors component2 = ((Component)customization).GetComponent<BoatCustomColors>();
			List<MeshRenderer> list3 = new List<MeshRenderer>();
			for (int j = 0; j < list.Count; j++)
			{
				BoatPart val = list[j];
				List<BoatPartOption> list4 = dictionary[val];
				if (list4.Count == 0)
				{
					continue;
				}
				List<RestrictedPartSelection> restrictedSelectionsForMast = GetRestrictedSelectionsForMast(selections, val);
				for (int k = 0; k < list4.Count; k++)
				{
					BoatPartOption val2 = list4[k];
					UnstayedMastSourceIdentity identity = UnstayedMastSourceIdentity.Create(val2, val, ((Component)customization).transform, component.sceneIndex);
					if (!unstayedMastIndexAllocator.TryClaim(val2, identity, out var mastIndex, out var usesFixedVanillaIndex))
					{
						continue;
					}
					BoatPartOption val3 = CloneUnstayedMast(val2, component.sceneIndex, mastIndex, identity, restrictedSelectionsForMast);
					if (!((Object)(object)val3 == (Object)null))
					{
						val.partOptions.Add(val3);
						AddMutualRestrictions(val3, restrictedSelectionsForMast);
						if ((Object)(object)component2 != (Object)null)
						{
							CollectPaintedCopies(component2.masts, ((Component)val2).transform, ((Component)val3).transform, list3);
						}
						list2.Add(new UnstayedMastProfile
						{
							MastPart = val,
							UnstayedOption = val3,
							Marker = ((Component)val3).GetComponent<UnstayedMastMarker>(),
							UsesFixedVanillaIndex = usesFixedVanillaIndex,
							RestrictedSelections = new List<RestrictedPartSelection>(restrictedSelectionsForMast)
						});
					}
				}
			}
			unstayedMastIndexAllocator.Commit();
			if (list3.Count > 0)
			{
				AddToMastPaint(component2, list3);
			}
			if (list2.Count == 0)
			{
				ManualLogSource logSource2 = Plugin.LogSource;
				if (logSource2 != null)
				{
					logSource2.LogError((object)("No unstayed mast options were created on " + ((Object)customization).name + "."));
				}
				return;
			}
			RestrictNoShroudsToUnstayedMasts(selections);
			((Component)customization).gameObject.AddComponent<UnstayedBoatMarker>();
			UnstayedBoatRegistry.Register(new UnstayedBoatProfile
			{
				Parts = parts,
				Refs = refs,
				SceneIndex = component.sceneIndex,
				Masts = list2,
				RetiredMastIndices = unstayedMastIndexAllocator.GetRetiredIndices()
			});
			int num = 0;
			for (int l = 0; l < list2.Count; l++)
			{
				if (list2[l].UsesFixedVanillaIndex)
				{
					num++;
				}
			}
			ManualLogSource logSource3 = Plugin.LogSource;
			if (logSource3 != null)
			{
				logSource3.LogInfo((object)("Added " + list2.Count + " unstayed mast option(s) to " + ((Object)customization).name + " (scene " + component.sceneIndex + "): " + num + " fixed vanilla, " + (list2.Count - num) + " extended."));
			}
		}

		private static bool IsSupportedBoat(int sceneIndex)
		{
			if (sceneIndex != 90 && sceneIndex != 80)
			{
				return sceneIndex == 70;
			}
			return true;
		}

		private static bool HasShipyardExpansionMastCapacity(BoatRefs refs)
		{
			if (refs.masts == null || refs.masts.Length < 128)
			{
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogError((object)"Could not add unstayed masts: Shipyard Expansion did not provide its 128-slot mast registry.");
				}
				return false;
			}
			return true;
		}

		private static List<BoatPart> FindTargetMastParts(Transform boat, BoatCustomParts parts, int sceneIndex)
		{
			List<BoatPart> list = new List<BoatPart>();
			string[] array = sceneIndex switch
			{
				80 => MediumJunkAnchorPaths, 
				90 => SmallJunkAnchorPaths, 
				_ => LargeJunkAnchorPaths, 
			};
			for (int i = 0; i < array.Length; i++)
			{
				Transform val = boat.Find(array[i]);
				if ((Object)(object)val == (Object)null)
				{
					val = FindUniqueMastByName(boat, GetLastPathSegment(array[i]));
				}
				BoatPartOption option = (((Object)(object)val != (Object)null) ? ((Component)val).GetComponent<BoatPartOption>() : null);
				BoatPart value = FindContainingPart(parts, option);
				AddUnique(list, value);
			}
			for (int j = 0; j < parts.availableParts.Count; j++)
			{
				BoatPart val2 = parts.availableParts[j];
				if (val2 != null && val2.category == 0 && ContainsEligibleMast(val2))
				{
					AddUnique(list, val2);
				}
			}
			list.Sort((BoatPart left, BoatPart right) => parts.availableParts.IndexOf(left).CompareTo(parts.availableParts.IndexOf(right)));
			return list;
		}

		private static Transform FindUniqueMastByName(Transform root, string name)
		{
			Mast[] componentsInChildren = ((Component)root).GetComponentsInChildren<Mast>(true);
			Transform val = null;
			for (int i = 0; i < componentsInChildren.Length; i++)
			{
				if (string.Equals(((Object)componentsInChildren[i]).name, name, StringComparison.OrdinalIgnoreCase) && !((Object)(object)((Component)componentsInChildren[i]).GetComponent<BoatPartOption>() == (Object)null))
				{
					if ((Object)(object)val != (Object)null)
					{
						return null;
					}
					val = ((Component)componentsInChildren[i]).transform;
				}
			}
			return val;
		}

		private static string GetLastPathSegment(string path)
		{
			int num = path.LastIndexOf('/');
			if (num < 0)
			{
				return path;
			}
			return path.Substring(num + 1);
		}

		private static BoatPart FindContainingPart(BoatCustomParts parts, BoatPartOption option)
		{
			if ((Object)(object)option == (Object)null)
			{
				return null;
			}
			for (int i = 0; i < parts.availableParts.Count; i++)
			{
				BoatPart val = parts.availableParts[i];
				if (val != null && val.partOptions != null && val.partOptions.Contains(option))
				{
					return val;
				}
			}
			return null;
		}

		private static bool ContainsEligibleMast(BoatPart part)
		{
			if (part.partOptions == null)
			{
				return false;
			}
			for (int i = 0; i < part.partOptions.Count; i++)
			{
				if (IsEligibleSource(part.partOptions[i]))
				{
					return true;
				}
			}
			return false;
		}

		private static List<BoatPartOption> GetEligibleSources(BoatPart part)
		{
			List<BoatPartOption> list = new List<BoatPartOption>();
			BoatPartOption[] array = part.partOptions.ToArray();
			for (int i = 0; i < array.Length; i++)
			{
				if (IsEligibleSource(array[i]))
				{
					list.Add(array[i]);
				}
			}
			return list;
		}

		private static bool IsEligibleSource(BoatPartOption option)
		{
			Mast val = (((Object)(object)option != (Object)null) ? ((Component)option).GetComponent<Mast>() : null);
			if ((Object)(object)val == (Object)null || (Object)(object)((Component)option).GetComponent<UnstayedMastMarker>() != (Object)null)
			{
				return false;
			}
			string text = UnstayedNameRules.Normalize(option.optionName + " " + ((Object)option).name + " " + GetHierarchyPath(((Component)option).transform));
			if (text.Contains("bermuda") || text.Contains("bowsprit") || text.Contains("forestay") || text.Contains("midstay") || text.Contains("front stay") || text.Contains("back stay") || text.Contains("mast stay") || text.Contains("sprit mast") || text.StartsWith("unstayed ", StringComparison.Ordinal) || val.onlyStaysails)
			{
				return false;
			}
			return true;
		}

		private static string GetHierarchyPath(Transform transform, Transform stopAt = null)
		{
			if ((Object)(object)transform == (Object)null)
			{
				return string.Empty;
			}
			List<string> list = new List<string>();
			Transform val = transform;
			while ((Object)(object)val != (Object)null && (Object)(object)val != (Object)(object)stopAt)
			{
				list.Add(((Object)val).name);
				val = val.parent;
			}
			list.Reverse();
			return string.Join("/", list.ToArray());
		}

		private static string GetRelativePath(Transform transform, Transform root)
		{
			if ((Object)(object)transform == (Object)(object)root)
			{
				return string.Empty;
			}
			List<string> list = new List<string>();
			Transform val = transform;
			while ((Object)(object)val != (Object)null && (Object)(object)val != (Object)(object)root)
			{
				list.Add(((Object)val).name);
				val = val.parent;
			}
			if ((Object)(object)val != (Object)(object)root)
			{
				return null;
			}
			list.Reverse();
			return string.Join("/", list.ToArray());
		}

		private static bool IsWithin(Transform transform, Transform root)
		{
			if ((Object)(object)transform == (Object)null || (Object)(object)root == (Object)null)
			{
				return false;
			}
			if (!((Object)(object)transform == (Object)(object)root))
			{
				return transform.IsChildOf(root);
			}
			return true;
		}

		private static bool Contains(List<BoatPartOption> options, BoatPartOption value)
		{
			return options?.Contains(value) ?? false;
		}

		private static void AddUnique<T>(List<T> list, T value) where T : class
		{
			if (value != null && !list.Contains(value))
			{
				list.Add(value);
			}
		}

		private static BoatPartOption CloneUnstayedMast(BoatPartOption sourceOption, int sceneIndex, int mastIndex, UnstayedMastSourceIdentity identity, List<RestrictedPartSelection> restrictedSelections)
		{
			Mast component = ((Component)sourceOption).GetComponent<Mast>();
			if ((Object)(object)component == (Object)null || (Object)(object)component.walkColMast == (Object)null)
			{
				ManualLogSource logSource = Plugin.LogSource;
				if (logSource != null)
				{
					logSource.LogError((object)("Skipped " + sourceOption.optionName + ": source mast or walk collider is missing."));
				}
				return null;
			}
			string text = (string.IsNullOrWhiteSpace(sourceOption.optionName) ? ((Object)sourceOption).name : sourceOption.optionName);
			Transform val = null;
			Transform val2 = null;
			try
			{
				bool activeSelf = ((Component)sourceOption).gameObject.activeSelf;
				try
				{
					((Component)sourceOption).gameObject.SetActive(false);
					val = Object.Instantiate<Transform>(((Component)sourceOption).transform, ((Component)sourceOption).transform.parent);
					((Component)val).gameObject.SetActive(false);
					val2 = Object.Instantiate<Transform>(component.walkColMast, component.walkColMast.parent);
					((Component)val2).gameObject.SetActive(false);
				}
				finally
				{
					((Component)sourceOption).gameObject.SetActive(activeSelf);
				}
				InitializeCloneTransforms(sourceOption, component, val, val2);
				Mast component2 = ((Component)val).GetComponent<Mast>();
				BoatPartOption component3 = ((Component)val).GetComponent<BoatPartOption>();
				if ((Object)(object)component2 == (Object)null || (Object)(object)component3 == (Object)null)
				{
					throw new InvalidOperationException("cloned components are missing");
				}
				InitializeClonedMast(component2, val2, sourceOption, mastIndex);
				InitializeClonedOption(component3, val2, sourceOption, text, sceneIndex);
				DisableClonedRiggingVisuals(sourceOption, component.walkColMast, component3, val2, restrictedSelections);
				PruneHierarchy(val, sceneIndex);
				PruneHierarchy(val2, sceneIndex);
				Dictionary<Collider, Collider> visualColliderMap = ExtractScaledRootBodyAndPlaceFittings(val, "UJSM_scaled_mast_body", forceTriggerColliders: true, isWalkBody: false);
				Dictionary<Collider, Collider> walkColliderMap = ExtractScaledRootBodyAndPlaceFittings(val2, "UJSM_scaled_walk_body", forceTriggerColliders: false, isWalkBody: true);
				component2.mastCols = CloneMastColliders(component, component2, sceneIndex, visualColliderMap, walkColliderMap);
				((Component)val).gameObject.AddComponent<UnstayedMastMarker>().Identity = identity;
				return component3;
			}
			catch (Exception ex)
			{
				if ((Object)(object)val != (Object)null)
				{
					Object.DestroyImmediate((Object)(object)((Component)val).gameObject);
				}
				if ((Object)(object)val2 != (Object)null)
				{
					Object.DestroyImmediate((Object)(object)((Component)val2).gameObject);
				}
				ManualLogSource logSource2 = Plugin.LogSource;
				if (logSource2 != null)
				{
					logSource2.LogError((object)("Skipped " + text + ": " + ex));
				}
				return null;
			}
		}

		private static void CollectPaintedCopies(MeshRenderer[] painted, Transform source, Transform copy, List<MeshRenderer> result)
		{
			if (painted == null)
			{
				return;
			}
			foreach (MeshRenderer val in painted)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				Transform val2;
				if ((Object)(object)((Component)val).transform == (Object)(object)source)
				{
					val2 = copy.Find("UJSM_scaled_mast_body");
				}
				else
				{
					if (!((Component)val).transform.IsChildOf(source))
					{
						continue;
					}
					val2 = copy.Find(GetHierarchyPath(((Component)val).transform, source));
				}
				MeshRenderer val3 = (((Object)(object)val2 != (Object)null) ? ((Component)val2).GetComponent<MeshRenderer>() : null);
				if ((Object)(object)val3 != (Object)null)
				{
					result.Add(val3);
				}
			}
		}

		private static void AddToMastPaint(BoatCustomColors paint, List<MeshRenderer> renderers)
		{
			MeshRenderer[] array = paint.masts;
			int num = array.Length;
			Array.Resize(ref array, num + renderers.Count);
			renderers.CopyTo(array, num);
			paint.masts = array;
		}

		private static void InitializeCloneTransforms(BoatPartOption sourceOption, Mast sourceMast, Transform cloneTransform, Transform cloneWalk)
		{
			//IL_0038: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: 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_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_007c: Unknown result type (might be due to invalid IL or missing references)
			//IL_008d: Unknown result type (might be due to invalid IL or missing references)
			((Object)cloneTransform).name = "Unstayed_" + ((Object)sourceOption).name;
			((Object)cloneWalk).name = "Unstayed_" + ((Object)sourceMast.walkColMast).name;
			cloneTransform.localPosition = ((Component)sourceOption).transform.localPosition;
			cloneTransform.localEulerAngles = ((Component)sourceOption).transform.localEulerAngles;
			cloneTransform.localScale = ((Component)sourceOption).transform.localScale;
			cloneWalk.localPosition = sourceMast.walkColMast.localPosition;
			cloneWalk.localEulerAngles = sourceMast.walkColMast.localEulerAngles;
			cloneWalk.localScale = sourceMast.walkColMast.localScale;
		}

		private static void InitializeClonedMast(Mast cloneMast, Transform cloneWalk, BoatPartOption sourceOption, int mastIndex)
		{
			cloneMast.orderIndex = mastIndex;
			cloneMast.walkColMast = cloneWalk;
			cloneMast.shipRigidbody = ((Component)sourceOption).GetComponentInParent<Rigidbody>();
			cloneMast.startSailPrefab = null;
			cloneMast.startSailPrefabs = (GameObject[])(object)new GameObject[0];
			cloneMast.startSailsHeightOffsets = new float[0];
		}

		private static void InitializeClonedOption(BoatPartOption cloneOption, Transform cloneWalk, BoatPartOption sourceOption, string sourceDisplayName, int sceneIndex)
		{
			cloneOption.optionName = "Unstayed " + sourceDisplayName;
			cloneOption.mass = sourceOption.mass * 2;
			cloneOption.walkColObject = ((Component)cloneWalk).gameObject;
			cloneOption.requires = cloneOption.requires ?? new List<BoatPartOption>();
			cloneOption.requiresDisabled = cloneOption.requiresDisabled ?? new List<BoatPartOption>();
			cloneOption.childOptions = FilterChildOptions(cloneOption.childOptions, sceneIndex);
		}

		private static void DisableClonedRiggingVisuals(BoatPartOption sourceOption, Transform sourceWalk, BoatPartOption cloneOption, Transform cloneWalk, List<RestrictedPartSelection> selections)
		{
			HashSet<GameObject> hashSet = new HashSet<GameObject>();
			for (int i = 0; i < selections.Count; i++)
			{
				RestrictedPartSelection restrictedPartSelection = selections[i];
				if (restrictedPartSelection.Kind != RestrictedPartKind.Rigging && restrictedPartSelection.Kind != RestrictedPartKind.RiggingAccessory)
				{
					continue;
				}
				for (int j = 0; j < restrictedPartSelection.NonEmptyOptions.Count; j++)
				{
					BoatPartOption val = restrictedPartSelection.NonEmptyOptions[j];
					if ((Object)(object)val == (Object)null || val.childOptions == null)
					{
						continue;
					}
					for (int k = 0; k < val.childOptions.Length; k++)
					{
						GameObject val2 = val.childOptions[k];
						if (!((Object)(object)val2 == (Object)null))
						{
							AddMappedVisual(hashSet, val2.transform, ((Component)sourceOption).transform, ((Component)cloneOption).transform);
							AddMappedVisual(hashSet, val2.transform, sourceWalk, cloneWalk);
						}
					}
				}
			}
			AddOrphanedTelltales(hashSet, ((Component)cloneOption).transform);
			AddOrphanedTelltales(hashSet, cloneWalk);
			if (hashSet.Count == 0)
			{
				return;
			}
			foreach (GameObject item in hashSet)
			{
				if ((Object)(object)item != (Object)null)
				{
					item.SetActive(false);
				}
			}
			List<GameObject> list = new List<GameObject>();
			if (cloneOption.childOptions != null)
			{
				for (int l = 0; l < cloneOption.childOptions.Length; l++)
				{
					GameObject val3 = cloneOption.childOptions[l];
					if ((Object)(object)val3 != (Object)null && !hashSet.Contains(val3))
					{
						list.Add(val3);
					}
				}
			}
			cloneOption.childOptions = list.ToArray();
		}

		private static void AddMappedVisual(HashSet<GameObject> result, Transform sourceVisual, Transform sourceRoot, Transform cloneRoot)
		{
			Transform val = MapToClone(sourceVisual, sourceRoot, cloneRoot);
			if ((Object)(object)val != (Object)null && (Object)(object)val != (Object)(object)cloneRoot)
			{
				result.Add(((Component)val).gameObject);
			}
		}

		private static void AddOrphanedTelltales(HashSet<GameObject> result, Transform mastRoot)
		{
			if ((Object)(object)mastRoot == (Object)null)
			{
				return;
			}
			Transform[] componentsInChildren = ((Component)mastRoot).GetComponentsInChildren<Transform>(true);
			foreach (Transform val in componentsInChildren)
			{
				if (!((Object)(object)val == (Object)(object)mastRoot))
				{
					string text = UnstayedNameRules.Normalize(((Object)val).name);
					bool flag = text.Contains("telltale");
					bool flag2 = text == "wind flag" && (Object)(object)val.parent == (Object)(object)mastRoot;
					if (flag || flag2)
					{
						result.Add(((Component)val).gameObject);
					}
				}
			}
		}

		private static Dictionary<Collider, Collider> ExtractScaledRootBodyAndPlaceFittings(Transform root, string bodyName, bool forceTriggerColliders, bool isWalkBody)
		{
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0022: Expected O, but got Unknown
			//IL_006e: 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_0092: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)root == (Object)null)
			{
				throw new ArgumentNullException("root");
			}
			List<SurfaceFittingPlacement> list = CaptureSurfaceFittings(root);
			GameObject val = new GameObject(bodyName);
			val.layer = ((Component)root).gameObject.layer;
			val.tag = ((Component)root).gameObject.tag;
			val.isStatic = ((Component)root).gameObject.isStatic;
			if (isWalkBody)
			{
				val.AddComponent<UnstayedScaledBodyMarker>();
			}
			Transform transform = val.transform;
			transform.SetParent(root, false);
			transform.localPosition = Vector3.zero;
			transform.localRotation = Quaternion.identity;
			transform.localScale = new Vector3(1.41f, 1.41f, 1f);
			bool num = CopyRootRenderer(root, val);
			Dictionary<Collider, Collider> dictionary = CopyRootColliders(root, val, forceTriggerColliders);
			if (!num && dictionary.Count == 0)
			{
				Object.DestroyImmediate((Object)(object)val);
				throw new InvalidOperationException("root mast body has no renderer or collider components");
			}
			if (list.Count > 0)
			{
				PlaceSurfaceFittings(root, list);
			}
			return dictionary;
		}

		private static bool CopyRootRenderer(Transform sourceRoot, GameObject target)
		{
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: 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_00c7: Unknown result type (might be due to invalid IL or missing references)
			MeshRenderer component = ((Component)sourceRoot).GetComponent<MeshRenderer>();
			MeshFilter component2 = ((Component)sourceRoot).GetComponent<MeshFilter>();
			Renderer component3 = ((Component)sourceRoot).GetComponent<Renderer>();
			if ((Object)(object)component == (Object)null)
			{
				if ((Object)(object)component3 != (Object)null)
				{
					throw new InvalidOperationException("unsupported root renderer type " + ((object)component3).GetType().Name);
				}
				return false;
			}
			if ((Object)(object)component2 == (Object)null || (Object)(object)component2.sharedMesh == (Object)null)
			{
				throw new InvalidOperationException("root MeshRenderer has no source mesh");
			}
			target.AddComponent<MeshFilter>().sharedMesh = component2.sharedMesh;
			MeshRenderer obj = target.AddComponent<MeshRenderer>();
			((Renderer)obj).sharedMaterials = ((Renderer)component).sharedMaterials;
			((Renderer)obj).shadowCastingMode = ((Renderer)component).shadowCastingMode;
			((Renderer)obj).receiveShadows = ((Renderer)component).receiveShadows;
			((Renderer)obj).lightProbeUsage = ((Renderer)component).lightProbeUsage;
			((Renderer)obj).reflectionProbeUsage = ((Renderer)component).reflectionProbeUsage;
			((Renderer)obj).probeAnchor = ((Renderer)component).probeAnchor;
			((Renderer)obj).motionVectorGenerationMode = ((Renderer)component).motionVectorGenerationMode;
			((Renderer)obj).allowOcclusionWhenDynamic = ((Renderer)component).allowOcclusionWhenDynamic;
			((Renderer)obj).sortingLayerID = ((Renderer)component).sortingLayerID;
			((Renderer)obj).sortingOrder = ((Renderer)component).sortingOrder;
			((Renderer)obj).enabled = ((Renderer)component).enabled;
			((Renderer)component).enabled = false;
			return true;
		}

		private static Dictionary<Collider, Collider> CopyRootColliders(Transform sourceRoot, GameObject target, bool forceTrigger)
		{
			Dictionary<Collider, Collider> dictionary = new Dictionary<Collider, Collider>();
			Collider[] components = ((Component)sourceRoot).GetComponents<Collider>();
			foreach (Collider val in components)
			{
				Collider val2 = CopyCollider(val, target);
				val2.sharedMaterial = val.sharedMaterial;
				val2.isTrigger = forceTrigger || val.isTrigger;
				val2.contactOffset = val.contactOffset;
				val2.enabled = val.enabled;
				val.enabled = false;
				dictionary[val] = val2;
			}
			return dictionary;
		}

		private static Collider CopyCollider(Collider source, GameObject target)
		{
			//IL_0018: 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_009a: 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_00c9: Unknown result type (might be due to invalid IL or missing references)
			CapsuleCollider val = (CapsuleCollider)(object)((source is CapsuleCollider) ? source : null);
			if ((Object)(object)val != (Object)null)
			{
				CapsuleCollider obj = target.AddComponent<CapsuleCollider>();
				obj.center = val.center;
				obj.radius = val.radius;
				obj.height = val.height;
				obj.direction = val.direction;
				return (Collider)(object)obj;
			}
			MeshCollider val2 = (MeshCollider)(object)((source is MeshCollider) ? source : null);
			if ((Object)(object)val2 != (Object)null)
			{
				MeshCollider obj2 = target.AddComponent<MeshCollider>();
				obj2.sharedMesh = val2.sharedMesh;
				obj2.convex =