Decompiled source of HideSail v1.0.9

HideSail.dll

Decompiled 3 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("HideSail")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("HideSail")]
[assembly: AssemblyTitle("HideSail")]
[assembly: AssemblyVersion("1.0.0.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 HideSail
{
	internal static class ConfigLogDump
	{
		internal static void Schedule(BaseUnityPlugin plugin, ManualLogSource log)
		{
			((MonoBehaviour)plugin).StartCoroutine(Run(plugin, log));
		}

		private static IEnumerator Run(BaseUnityPlugin plugin, ManualLogSource log)
		{
			yield return null;
			yield return null;
			try
			{
				StringBuilder stringBuilder = new StringBuilder();
				foreach (ConfigDefinition key in plugin.Config.Keys)
				{
					string text = key.Key.ToLowerInvariant();
					if (text.Contains("pass") || text.Contains("token") || text.Contains("secret"))
					{
						continue;
					}
					ConfigEntryBase val = plugin.Config[key];
					if (!object.Equals(val.BoxedValue, val.DefaultValue))
					{
						if (stringBuilder.Length > 0)
						{
							stringBuilder.Append("; ");
						}
						stringBuilder.Append(key.Section).Append('.').Append(key.Key)
							.Append('=')
							.Append(val.BoxedValue);
						if (stringBuilder.Length > 1500)
						{
							stringBuilder.Append("; ...");
							break;
						}
					}
				}
				log.LogInfo((object)("[HideSail] config (changed from default): " + ((stringBuilder.Length == 0) ? "none" : stringBuilder.ToString())));
			}
			catch (Exception ex)
			{
				log.LogWarning((object)("[HideSail] config dump failed: " + ex.Message));
			}
		}
	}
	[BepInPlugin("zenox.hidesail", "HideSail", "1.0.9")]
	public class HideSailPlugin : BaseUnityPlugin
	{
		public const string PluginGUID = "zenox.hidesail";

		public const string PluginName = "HideSail";

		public const string PluginVersion = "1.0.9";

		public static ConfigEntry<KeyboardShortcut> HideSailKey;

		public static ConfigEntry<float> SailHiddenAlpha;

		public static ConfigEntry<float> UpdateInterval;

		private readonly Harmony harmony = new Harmony("zenox.hidesail");

		private const string SailHiddenZdoKey = "hidesail_hidden";

		private Ship currentShip;

		private float shipCheckTimer;

		private bool appliedSailHidden;

		private void Awake()
		{
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			ConfigLogDump.Schedule((BaseUnityPlugin)(object)this, ((BaseUnityPlugin)this).Logger);
			HideSailKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Sailing", "HideSailKey", new KeyboardShortcut((KeyCode)104, Array.Empty<KeyCode>()), "Key to toggle the visibility of your current ship's sail (visual only, does not affect sailing speed).");
			SailHiddenAlpha = ((BaseUnityPlugin)this).Config.Bind<float>("Sailing", "SailHiddenAlpha", 0.01f, "How transparent the sail becomes when hidden (0 = fully invisible, 1 = fully opaque). A small value like 0.01 keeps a faint hint of the sail's up/down position.");
			UpdateInterval = ((BaseUnityPlugin)this).Config.Bind<float>("Sailing", "UpdateInterval", 0.1f, "Seconds between checks for which ship you're currently on.");
			harmony.PatchAll(Assembly.GetExecutingAssembly());
			((BaseUnityPlugin)this).Logger.LogInfo((object)"HideSail v1.0.9 loaded.");
		}

		private void OnDestroy()
		{
			Harmony obj = harmony;
			if (obj != null)
			{
				obj.UnpatchSelf();
			}
		}

		private void Update()
		{
			//IL_00cd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
			Player localPlayer = Player.m_localPlayer;
			if ((Object)(object)localPlayer == (Object)null)
			{
				currentShip = null;
				return;
			}
			Ship val = currentShip;
			shipCheckTimer += Time.deltaTime;
			if (shipCheckTimer >= UpdateInterval.Value)
			{
				shipCheckTimer = 0f;
				currentShip = FindPlayerShip(localPlayer);
				if ((Object)(object)currentShip != (Object)null)
				{
					ZNetView component = ((Component)currentShip).GetComponent<ZNetView>();
					if ((Object)(object)component != (Object)null && component.IsValid())
					{
						bool flag = component.GetZDO().GetBool("hidesail_hidden", false);
						if ((Object)(object)currentShip != (Object)(object)val || flag != appliedSailHidden)
						{
							ApplySailVisibility(currentShip, flag);
						}
					}
				}
			}
			if (!((Object)(object)currentShip != (Object)null))
			{
				return;
			}
			KeyboardShortcut value = HideSailKey.Value;
			if (!((KeyboardShortcut)(ref value)).IsDown())
			{
				return;
			}
			ZNetView component2 = ((Component)currentShip).GetComponent<ZNetView>();
			if ((Object)(object)component2 != (Object)null && component2.IsValid())
			{
				if (!component2.IsOwner())
				{
					component2.ClaimOwnership();
				}
				bool flag2 = !component2.GetZDO().GetBool("hidesail_hidden", false);
				component2.GetZDO().Set("hidesail_hidden", flag2);
				ApplySailVisibility(currentShip, flag2);
			}
		}

		private void ApplySailVisibility(Ship ship, bool hidden)
		{
			//IL_003b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0040: Unknown result type (might be due to invalid IL or missing references)
			//IL_0046: 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_0052: Unknown result type (might be due to invalid IL or missing references)
			//IL_0059: Unknown result type (might be due to invalid IL or missing references)
			List<Renderer> sailRenderers = GetSailRenderers(ship);
			if (sailRenderers.Count == 0)
			{
				return;
			}
			float num = (hidden ? SailHiddenAlpha.Value : 1f);
			foreach (Renderer item in sailRenderers)
			{
				Color color = item.material.color;
				item.material.color = new Color(color.r, color.g, color.b, num);
			}
			appliedSailHidden = hidden;
		}

		private static List<Renderer> GetSailRenderers(Ship ship)
		{
			List<Renderer> list = new List<Renderer>();
			if ((Object)(object)ship.m_sailObject != (Object)null)
			{
				list.AddRange(ship.m_sailObject.GetComponentsInChildren<Renderer>(true));
			}
			if ((Object)(object)ship.m_sailCloth != (Object)null)
			{
				Renderer[] componentsInChildren = ((Component)ship.m_sailCloth).GetComponentsInChildren<Renderer>(true);
				foreach (Renderer item in componentsInChildren)
				{
					if (!list.Contains(item))
					{
						list.Add(item);
					}
				}
			}
			return list;
		}

		private static Ship FindPlayerShip(Player player)
		{
			Ship localShip = Ship.GetLocalShip();
			if ((Object)(object)localShip != (Object)null && localShip.IsPlayerInBoat(player))
			{
				return localShip;
			}
			localShip = ((Character)player).GetStandingOnShip();
			if ((Object)(object)localShip != (Object)null && localShip.IsPlayerInBoat(player))
			{
				return localShip;
			}
			localShip = player.GetControlledShip();
			if ((Object)(object)localShip != (Object)null && localShip.IsPlayerInBoat(player))
			{
				return localShip;
			}
			return null;
		}
	}
}