Decompiled source of BoatSailSaveFix v1.0.0

BoatSailSaveFix\BoatSailSaveFix.dll

Decompiled a day ago
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.0", FrameworkDisplayName = "")]
[assembly: AssemblyCompany("gaucl")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Fixes vanilla Sailwind bug where boat sails are omitted from save data when masts are inactive.")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("BoatSailSaveFix")]
[assembly: AssemblyTitle("BoatSailSaveFix")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace BoatSailSaveFix;

[BepInPlugin("gaucl.boatsailsavefix", "Boat Sail Save Fix", "1.0.0")]
public sealed class Plugin : BaseUnityPlugin
{
	public const string PluginGuid = "gaucl.boatsailsavefix";

	public const string PluginName = "Boat Sail Save Fix";

	public const string PluginVersion = "1.0.0";

	internal static ManualLogSource Log { get; private set; }

	private void Awake()
	{
		Log = ((BaseUnityPlugin)this).Logger;
		Harmony.CreateAndPatchAll(typeof(SaveableBoatCustomization_GetData_Patch), (string)null);
		Harmony.CreateAndPatchAll(typeof(SaveableBoatCustomization_LoadData_Patch), (string)null);
		Log.LogInfo((object)"Boat Sail Save Fix 1.0.0 loaded");
	}
}
internal static class SailDataHelper
{
	internal static SaveSailData FromSail(Sail sail, Mast mast)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0005: 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_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Unknown result type (might be due to invalid IL or missing references)
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		//IL_004c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_0071: Expected O, but got Unknown
		return new SaveSailData
		{
			prefabIndex = sail.prefabIndex,
			mastIndex = mast.orderIndex,
			installHeight = sail.GetCurrentInstallHeight(),
			minAngle = sail.minAngle,
			maxAngle = sail.maxAngle,
			health = 100f,
			sailColor = sail.activeColor,
			scaleY = sail.GetScaleY(),
			scaleZ = sail.GetScaleZ()
		};
	}

	internal static string Key(SaveSailData sail)
	{
		return sail.prefabIndex + ":" + sail.mastIndex + ":" + sail.installHeight.ToString("R");
	}

	internal static List<SaveSailData> CloneList(List<SaveSailData> source)
	{
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_003b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: 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_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_0094: Expected O, but got Unknown
		List<SaveSailData> list = new List<SaveSailData>(source.Count);
		foreach (SaveSailData item in source)
		{
			list.Add(new SaveSailData
			{
				prefabIndex = item.prefabIndex,
				mastIndex = item.mastIndex,
				installHeight = item.installHeight,
				minAngle = item.minAngle,
				maxAngle = item.maxAngle,
				health = item.health,
				sailColor = item.sailColor,
				scaleY = item.scaleY,
				scaleZ = item.scaleZ
			});
		}
		return list;
	}
}
[HarmonyPatch(typeof(SaveableBoatCustomization), "GetData")]
internal static class SaveableBoatCustomization_GetData_Patch
{
	private static void Postfix(SaveableBoatCustomization __instance, ref SaveBoatCustomizationData __result)
	{
		if (__result?.sails == null)
		{
			return;
		}
		BoatRefs component = ((Component)__instance).GetComponent<BoatRefs>();
		if (!Object.op_Implicit((Object)(object)component))
		{
			return;
		}
		HashSet<string> hashSet = new HashSet<string>();
		foreach (SaveSailData sail in __result.sails)
		{
			hashSet.Add(SailDataHelper.Key(sail));
		}
		int num = 0;
		Mast[] masts = component.masts;
		foreach (Mast val in masts)
		{
			if (!Object.op_Implicit((Object)(object)val) || ((Component)val).gameObject.activeInHierarchy || val.sails == null)
			{
				continue;
			}
			foreach (GameObject sail2 in val.sails)
			{
				if (!Object.op_Implicit((Object)(object)sail2))
				{
					continue;
				}
				Sail component2 = sail2.GetComponent<Sail>();
				if (Object.op_Implicit((Object)(object)component2))
				{
					SaveSailData val2 = SailDataHelper.FromSail(component2, val);
					if (hashSet.Add(SailDataHelper.Key(val2)))
					{
						__result.sails.Add(val2);
						num++;
					}
				}
			}
		}
		if (num > 0)
		{
			Plugin.Log.LogInfo((object)$"Preserved {num} sail(s) from inactive masts while saving {((Object)((Component)__instance).gameObject).name}");
		}
	}
}
[HarmonyPatch(typeof(SaveableBoatCustomization), "LoadData")]
internal static class SaveableBoatCustomization_LoadData_Patch
{
	private static readonly Dictionary<int, List<SaveSailData>> Cache = new Dictionary<int, List<SaveSailData>>();

	private static void Prefix(SaveableBoatCustomization __instance, SaveBoatCustomizationData data)
	{
		if (data?.sails != null && data.sails.Count <= 0)
		{
			SaveableObject component = ((Component)__instance).GetComponent<SaveableObject>();
			if (Object.op_Implicit((Object)(object)component) && Cache.TryGetValue(component.sceneIndex, out var value) && value.Count != 0)
			{
				data.sails = SailDataHelper.CloneList(value);
				Plugin.Log.LogInfo((object)$"Restored {value.Count} cached sail(s) for {((Object)((Component)__instance).gameObject).name} (sceneIndex {component.sceneIndex})");
			}
		}
	}

	private static void Postfix(SaveableBoatCustomization __instance, SaveBoatCustomizationData data)
	{
		if (data?.sails != null && data.sails.Count != 0)
		{
			SaveableObject component = ((Component)__instance).GetComponent<SaveableObject>();
			if (Object.op_Implicit((Object)(object)component))
			{
				Cache[component.sceneIndex] = SailDataHelper.CloneList(data.sails);
			}
		}
	}
}