Decompiled source of Muted Sails v0.1.1

Plugins/VentureValheim.MutedSails.dll

Decompiled a month ago
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Jotunn.Managers;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("VentureValheim.MutedSails")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("VentureValheim.MutedSails")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("E1AE7638-9DC5-41AF-86E5-BBC015CD89A6")]
[assembly: AssemblyFileVersion("0.1.1")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.1.1.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
}
namespace VentureValheim.MutedSails
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("com.orianaventure.mod.MutedSails", "MutedSails", "0.1.1")]
	public class MutedSailsPlugin : BaseUnityPlugin
	{
		private const string ModName = "MutedSails";

		private const string ModVersion = "0.1.1";

		private const string Author = "com.orianaventure.mod";

		private const string ModGUID = "com.orianaventure.mod.MutedSails";

		private static string ConfigFileName = "com.orianaventure.mod.MutedSails.cfg";

		private static string ConfigFileFullPath;

		private readonly Harmony HarmonyInstance = new Harmony("com.orianaventure.mod.MutedSails");

		public static readonly ManualLogSource MutedSailsLogger;

		public static ConfigEntry<bool> CE_TransparencyEnabled;

		private static ConfigEntry<KeyCode> CE_ToggleKey;

		private DateTime _lastReloadTime;

		private const long RELOAD_DELAY = 10000000L;

		public static bool GetTransparencyEnabled()
		{
			return CE_TransparencyEnabled.Value;
		}

		public static KeyCode GetToggleKey()
		{
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			return CE_ToggleKey.Value;
		}

		private void AddConfig<T>(string key, string section, string description, bool synced, T value, ref ConfigEntry<T> configEntry)
		{
			string extendedDescription = GetExtendedDescription(description, synced);
			configEntry = ((BaseUnityPlugin)this).Config.Bind<T>(section, key, value, extendedDescription);
		}

		public string GetExtendedDescription(string description, bool synchronizedSetting)
		{
			return description + (synchronizedSetting ? " [Synced with Server]" : " [Not Synced with Server]");
		}

		private void Awake()
		{
			MutedSailsLogger.LogInfo((object)"This one took like way longer to figure out than I wanted. Bees!");
			AddConfig("TransparencyEnabled", "General", "False to disable the transparency (boolean).", synced: false, value: true, ref CE_TransparencyEnabled);
			AddConfig("ToggleKey", "General", "Keycode to toggle the transparency on and off (Unity.KeyCode).", synced: false, (KeyCode)121, ref CE_ToggleKey);
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			HarmonyInstance.PatchAll(executingAssembly);
			SetupWatcher();
			MutedSails.ConfigurationDirty = true;
		}

		private void OnDestroy()
		{
			((BaseUnityPlugin)this).Config.Save();
		}

		private void SetupWatcher()
		{
			_lastReloadTime = DateTime.Now;
			FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(Paths.ConfigPath, ConfigFileName);
			fileSystemWatcher.Changed += ReadConfigValues;
			fileSystemWatcher.Created += ReadConfigValues;
			fileSystemWatcher.Renamed += ReadConfigValues;
			fileSystemWatcher.IncludeSubdirectories = true;
			fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
			fileSystemWatcher.EnableRaisingEvents = true;
		}

		private void ReadConfigValues(object sender, FileSystemEventArgs e)
		{
			DateTime now = DateTime.Now;
			long num = now.Ticks - _lastReloadTime.Ticks;
			if (File.Exists(ConfigFileFullPath) && num >= 10000000)
			{
				try
				{
					MutedSailsLogger.LogDebug((object)"Attempting to reload configuration...");
					((BaseUnityPlugin)this).Config.Reload();
				}
				catch
				{
					MutedSailsLogger.LogError((object)("There was an issue loading " + ConfigFileName));
					return;
				}
				_lastReloadTime = now;
				MutedSails.ConfigurationDirty = true;
			}
		}

		static MutedSailsPlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
			MutedSailsLogger = Logger.CreateLogSource("MutedSails");
			CE_TransparencyEnabled = null;
			CE_ToggleKey = null;
		}
	}
	public class MutedSails
	{
		public class MutedSailTracker : MonoBehaviour
		{
			public Material TransparentSailMaterial;

			public Material OriginalSailMaterial;

			public bool IsTransparent;
		}

		[HarmonyPatch(typeof(Ship), "Awake")]
		public static class Patch_Ship_Awake
		{
			private static void Postfix(Ship __instance)
			{
				MutedSailTracker mutedSailTracker = default(MutedSailTracker);
				if (!((Component)__instance).TryGetComponent<MutedSailTracker>(ref mutedSailTracker))
				{
					AddMutedSailTracker(ref __instance);
					MutedSailsPlugin.MutedSailsLogger.LogWarning((object)("Added late sail information to " + ((Object)__instance).name + "! This should not happen!"));
				}
			}
		}

		[HarmonyPatch(typeof(Ship), "UpdateSailSize")]
		public static class Patch_Ship_UpdateSailSize
		{
			private static void Prefix(Ship __instance, out bool __state)
			{
				__state = __instance.m_sailWasInPosition;
			}

			private static void Postfix(Ship __instance, bool __state)
			{
				if ((__state && !ConfigurationDirty) || (Object)(object)__instance.m_sailCloth == (Object)null || (Object)(object)Player.m_localPlayer == (Object)null)
				{
					return;
				}
				ConfigurationDirty = false;
				SkinnedMeshRenderer component = ((Component)__instance.m_sailCloth).GetComponent<SkinnedMeshRenderer>();
				MutedSailTracker component2 = ((Component)__instance).gameObject.GetComponent<MutedSailTracker>();
				if (!((Object)(object)component == (Object)null) && !((Object)(object)component2 == (Object)null))
				{
					bool flag = MutedSailsPlugin.GetTransparencyEnabled() && __instance.HasPlayerOnboard();
					if (flag && !component2.IsTransparent)
					{
						((Renderer)component).material = component2.TransparentSailMaterial;
						component2.IsTransparent = true;
					}
					else if (!flag && component2.IsTransparent)
					{
						((Renderer)component).material = component2.OriginalSailMaterial;
						component2.IsTransparent = false;
					}
				}
			}
		}

		[HarmonyPriority(100)]
		[HarmonyPatch(typeof(ObjectDB), "Awake")]
		public static class Patch_ObjectDB_Awake
		{
			private static void Postfix()
			{
				//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)
				Scene activeScene = SceneManager.GetActiveScene();
				if (((Scene)(ref activeScene)).name.Equals("main"))
				{
					UpdateSails();
				}
			}
		}

		[HarmonyPatch(typeof(Player), "Update")]
		public static class Patch_Player_Update
		{
			private static void Postfix(Player __instance)
			{
				//IL_0008: Unknown result type (might be due to invalid IL or missing references)
				if (((Character)__instance).TakeInput() && ZInput.GetKeyDown(MutedSailsPlugin.GetToggleKey(), true))
				{
					((ConfigEntryBase)MutedSailsPlugin.CE_TransparencyEnabled).BoxedValue = !MutedSailsPlugin.CE_TransparencyEnabled.Value;
					ConfigurationDirty = true;
				}
			}
		}

		public static bool ConfigurationDirty;

		public static void UpdateSails()
		{
			Ship ship = default(Ship);
			foreach (GameObject prefab in ZNetScene.instance.m_prefabs)
			{
				if ((Object)(object)prefab != (Object)null && prefab.TryGetComponent<Ship>(ref ship))
				{
					AddMutedSailTracker(ref ship);
					MutedSailsPlugin.MutedSailsLogger.LogInfo((object)("Tracking sail information for " + ((Object)prefab).name + "!"));
				}
			}
		}

		public static void AddMutedSailTracker(ref Ship ship)
		{
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_005c: Expected O, but got Unknown
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			Cloth componentInChildren = ((Component)ship).GetComponentInChildren<Cloth>();
			if (!((Object)(object)componentInChildren == (Object)null))
			{
				SkinnedMeshRenderer component = ((Component)componentInChildren).GetComponent<SkinnedMeshRenderer>();
				if (!((Object)(object)component == (Object)null) && ((Renderer)component).materials != null && ((Renderer)component).materials.Length >= 1)
				{
					Material val = ((Renderer)component).materials[0];
					Texture mainTexture = val.mainTexture;
					Texture2D val2 = (Texture2D)(object)((mainTexture is Texture2D) ? mainTexture : null);
					Material val3 = new Material(Cache.GetPrefab<Shader>("Custom/LitParticles"));
					val3.SetTexture("_MainTex", (Texture)(object)val2);
					val3.SetFloat("_Cutoff", 0.1f);
					val3.SetVector("_Color", new Vector4(1f, 1f, 1f, 0.2f));
					MutedSailTracker mutedSailTracker = ((Component)ship).gameObject.AddComponent<MutedSailTracker>();
					mutedSailTracker.TransparentSailMaterial = val3;
					mutedSailTracker.OriginalSailMaterial = val;
				}
			}
		}
	}
}