Decompiled source of HollowSails v1.0.0

HollowSails.dll

Decompiled 8 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("HollowSails")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0+b974c83d4a56942b51068e65abc6d5b49ec0561e")]
[assembly: AssemblyProduct("HollowSails")]
[assembly: AssemblyTitle("HollowSails")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.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 HollowSails
{
	[BepInPlugin("zopthemop.hollowsails", "Hollow Sails", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(Ship), "Awake")]
		private static class ShipAwakePatch
		{
			private static void Postfix(Ship __instance)
			{
				MakeSailHollow(__instance);
			}
		}

		[HarmonyPatch(typeof(Ship), "UpdateSailSize")]
		private static class ShipUpdateSailSizePatch
		{
			private static void Postfix(Ship __instance)
			{
				MakeSailHollow(__instance);
			}
		}

		public const string ModGUID = "zopthemop.hollowsails";

		public const string ModName = "Hollow Sails";

		public const string ModVersion = "1.0.0";

		public const string ModDescription = "Makes sails mainly see-through (only edges visible)";

		private static readonly HashSet<string> SailTextureNames = new HashSet<string> { "sail_hide", "sail_white", "sail_diffuse" };

		private static readonly Dictionary<string, Texture2D> TransparentTextures = new Dictionary<string, Texture2D>();

		private void Awake()
		{
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_000c: Expected O, but got Unknown
			Harmony val = new Harmony("zopthemop.hollowsails");
			val.PatchAll();
		}

		private static void MakeSailHollow(Ship ship)
		{
			if ((Object)(object)ship == (Object)null || (Object)(object)ship.m_sailObject == (Object)null)
			{
				return;
			}
			SkinnedMeshRenderer componentInChildren = ship.m_sailObject.GetComponentInChildren<SkinnedMeshRenderer>(true);
			if ((Object)(object)componentInChildren == (Object)null)
			{
				return;
			}
			Material[] materials = ((Renderer)componentInChildren).materials;
			foreach (Material val in materials)
			{
				if (!((Object)(object)val == (Object)null))
				{
					Texture mainTexture = val.mainTexture;
					Texture2D val2 = (Texture2D)(object)((mainTexture is Texture2D) ? mainTexture : null);
					if (!((Object)(object)val2 == (Object)null) && SailTextureNames.Contains(((Object)val2).name))
					{
						val.SetOverrideTag("RenderType", "Transparent");
						val.mainTexture = (Texture)(object)GetTransparentTexture(val2);
					}
				}
			}
		}

		private static Texture2D GetTransparentTexture(Texture2D original)
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Unknown result type (might be due to invalid IL or missing references)
			if (TransparentTextures.TryGetValue(((Object)original).name, out var value))
			{
				return value;
			}
			Texture2D source = MakeReadableCopy(original);
			Texture2D val = CreateHollowTexture(source);
			((Object)val).name = ((Object)original).name + "_transparent";
			((Texture)val).wrapMode = ((Texture)original).wrapMode;
			((Texture)val).filterMode = ((Texture)original).filterMode;
			((Texture)val).anisoLevel = ((Texture)original).anisoLevel;
			TransparentTextures[((Object)original).name] = val;
			return val;
		}

		private static Texture2D MakeReadableCopy(Texture2D texture)
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Expected O, but got Unknown
			//IL_0058: Unknown result type (might be due to invalid IL or missing references)
			RenderTexture temporary = RenderTexture.GetTemporary(((Texture)texture).width, ((Texture)texture).height, 0, (RenderTextureFormat)0, (RenderTextureReadWrite)0);
			RenderTexture active = RenderTexture.active;
			Graphics.Blit((Texture)(object)texture, temporary);
			RenderTexture.active = temporary;
			Texture2D val = new Texture2D(((Texture)texture).width, ((Texture)texture).height, (TextureFormat)4, false);
			val.ReadPixels(new Rect(0f, 0f, (float)((Texture)temporary).width, (float)((Texture)temporary).height), 0, 0);
			val.Apply();
			RenderTexture.active = active;
			RenderTexture.ReleaseTemporary(temporary);
			((Object)val).name = ((Object)texture).name;
			return val;
		}

		private static Texture2D CreateHollowTexture(Texture2D source)
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Expected O, but got Unknown
			//IL_0042: Unknown result type (might be due to invalid IL or missing references)
			//IL_0047: Unknown result type (might be due to invalid IL or missing references)
			//IL_0085: Unknown result type (might be due to invalid IL or missing references)
			//IL_0087: Unknown result type (might be due to invalid IL or missing references)
			Texture2D val = new Texture2D(((Texture)source).width, ((Texture)source).height, (TextureFormat)4, false);
			Color[] pixels = source.GetPixels();
			int width = ((Texture)source).width;
			int height = ((Texture)source).height;
			for (int i = 0; i < height; i++)
			{
				for (int j = 0; j < width; j++)
				{
					int num = i * width + j;
					Color val2 = pixels[num];
					bool flag = j < 4 || j >= width - 4 || i < 16 || i >= height - 16;
					val2.a = (flag ? 1f : 0f);
					pixels[num] = val2;
				}
			}
			val.SetPixels(pixels);
			val.Apply();
			return val;
		}
	}
}