Decompiled source of Venture Pride v1.0.0

Plugins/VentureValheim.Pride.dll

Decompiled 3 days 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 Jotunn.Utils;
using MagicaCloth2;
using Microsoft.CodeAnalysis;
using UnityEngine;
using UnityEngine.SceneManagement;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("VentureValheim.Pride")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("VentureValheim.Pride")]
[assembly: AssemblyCopyright("Copyright ©  2026")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("EAB77D37-3399-4964-BA66-150DFCF3667E")]
[assembly: AssemblyFileVersion("1.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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]
	[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.Pride
{
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInPlugin("com.orianaventure.mod.Pride", "Pride", "1.0.0")]
	public class PridePlugin : BaseUnityPlugin
	{
		private const string ModName = "Pride";

		private const string ModVersion = "1.0.0";

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

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

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

		private static string ConfigFileFullPath;

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

		public static readonly ManualLogSource PrideLogger;

		public static GameObject Root;

		internal static AssetBundle PrideBundle;

		public static Texture2D PrideTexture;

		public static Texture2D TransTexture;

		public static Texture2D RoundShieldTexture;

		public static Texture2D BannerTexture;

		private static ConfigEntry<bool> CE_ReplaceSailsPride;

		private static ConfigEntry<bool> CE_ReplaceSailsTrans;

		private static ConfigEntry<bool> CE_ReplaceShields;

		private static ConfigEntry<bool> CE_ReplaceBanner;

		private readonly ConfigurationManagerAttributes AdminConfig = new ConfigurationManagerAttributes
		{
			IsAdminOnly = true
		};

		private readonly ConfigurationManagerAttributes ClientConfig = new ConfigurationManagerAttributes
		{
			IsAdminOnly = false
		};

		private DateTime _lastReloadTime;

		private const long RELOAD_DELAY = 10000000L;

		public static bool GetReplaceSailsPride()
		{
			return CE_ReplaceSailsPride.Value;
		}

		public static bool GetReplaceSailsTrans()
		{
			return CE_ReplaceSailsTrans.Value;
		}

		public static bool GetReplaceShields()
		{
			return CE_ReplaceShields.Value;
		}

		public static bool GetReplaceBanner()
		{
			return CE_ReplaceBanner.Value;
		}

		private void AddConfig<T>(string key, string section, string description, bool synced, T value, ref ConfigEntry<T> configEntry)
		{
			//IL_0033: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Expected O, but got Unknown
			string extendedDescription = GetExtendedDescription(description, synced);
			configEntry = ((BaseUnityPlugin)this).Config.Bind<T>(section, key, value, new ConfigDescription(extendedDescription, (AcceptableValueBase)null, new object[1] { synced ? AdminConfig : ClientConfig }));
		}

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

		private void Awake()
		{
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Expected O, but got Unknown
			AddConfig("ReplaceSailsRainbow", "General", "True to replace all sails with the rainbow pride flag (boolean).", synced: false, value: true, ref CE_ReplaceSailsPride);
			AddConfig("ReplaceSailsTrans", "General", "True to replace all sails with the trans flag when ReplaceSailsRainbow is false (boolean).", synced: false, value: false, ref CE_ReplaceSailsTrans);
			AddConfig("ReplaceShields", "General", "True to add inclusive pride flag all round shields (boolean).", synced: false, value: true, ref CE_ReplaceShields);
			AddConfig("ReplaceBanner", "General", "True to replace the red/white/blue banner (piece_banner06) with the trans flag colors (boolean).", synced: false, value: true, ref CE_ReplaceBanner);
			PrideLogger.LogInfo((object)"Happy Pride Month (and beyond)!");
			Root = new GameObject("PrideRoot");
			Root.SetActive(false);
			Object.DontDestroyOnLoad((Object)(object)Root);
			PrideBundle = AssetUtils.LoadAssetBundleFromResources("vv_pride", Assembly.GetExecutingAssembly());
			PrideTexture = PrideBundle.LoadAsset<Texture2D>("vv_PrideFlagSquare");
			TransTexture = PrideBundle.LoadAsset<Texture2D>("vv_TransFlagSquare");
			RoundShieldTexture = PrideBundle.LoadAsset<Texture2D>("vv_ShieldwoodPrideUnderlay");
			BannerTexture = PrideBundle.LoadAsset<Texture2D>("vv_PrideBannerOverlay");
			if ((Object)(object)PrideTexture == (Object)null || (Object)(object)TransTexture == (Object)null || (Object)(object)RoundShieldTexture == (Object)null || (Object)(object)BannerTexture == (Object)null)
			{
				PrideLogger.LogError((object)"Missing textures!!!");
			}
			Assembly executingAssembly = Assembly.GetExecutingAssembly();
			HarmonyInstance.PatchAll(executingAssembly);
			SetupWatcher();
		}

		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
				{
					PrideLogger.LogDebug((object)"Attempting to reload configuration...");
					((BaseUnityPlugin)this).Config.Reload();
				}
				catch
				{
					PrideLogger.LogError((object)("There was an issue loading " + ConfigFileName));
					return;
				}
				_lastReloadTime = now;
				PrideShip.TryUpdateSails();
				PrideShield.TryUpdateShield();
				PrideBanner.TryUpdateBanner();
			}
		}

		static PridePlugin()
		{
			string configPath = Paths.ConfigPath;
			char directorySeparatorChar = Path.DirectorySeparatorChar;
			ConfigFileFullPath = configPath + directorySeparatorChar + ConfigFileName;
			PrideLogger = Logger.CreateLogSource("Pride");
			CE_ReplaceSailsPride = null;
			CE_ReplaceSailsTrans = null;
			CE_ReplaceShields = null;
			CE_ReplaceBanner = null;
		}
	}
	internal class IconMerge
	{
		internal static Texture2D DuplicateTexture(Texture texture)
		{
			//IL_0039: Unknown result type (might be due to invalid IL or missing references)
			//IL_003e: Unknown result type (might be due to invalid IL or missing references)
			//IL_004d: 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)
			//IL_006c: Expected O, but got Unknown
			if ((Object)(object)texture == (Object)null)
			{
				return null;
			}
			int width = texture.width;
			int height = texture.height;
			RenderTexture active = RenderTexture.active;
			RenderTexture temporary = RenderTexture.GetTemporary(width, height, 0, (RenderTextureFormat)7, (RenderTextureReadWrite)2);
			Graphics.Blit(texture, temporary);
			RenderTexture.active = temporary;
			Texture2D val = new Texture2D(width, height);
			val.ReadPixels(new Rect(0f, 0f, (float)width, (float)height), 0, 0);
			val.Apply();
			RenderTexture.active = active;
			RenderTexture.ReleaseTemporary(temporary);
			return val;
		}

		internal static Texture2D MergeTextures(Texture2D baseTexture, Texture2D overlayTexture, float alpha = 0f)
		{
			//IL_0010: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			//IL_0023: Unknown result type (might be due to invalid IL or missing references)
			int width = ((Texture)baseTexture).width;
			int height = ((Texture)baseTexture).height;
			Texture2D merged = new Texture2D(width, height);
			for (int i = 0; i < width; i++)
			{
				for (int j = 0; j < height; j++)
				{
					merged.SetPixel(i, j, Color.clear);
					ApplyPixel(ref merged, baseTexture, overlayTexture, i, j, alpha);
				}
			}
			merged.Apply();
			return merged;
		}

		private static void ApplyPixel(ref Texture2D merged, Texture2D baseTex, Texture2D overlayTex, int x, int y, float alphaOverride)
		{
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_0009: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0013: Unknown result type (might be due to invalid IL or missing references)
			//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_001a: 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_0027: 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_0057: 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_005e: 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_004d: Unknown result type (might be due to invalid IL or missing references)
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0054: Unknown result type (might be due to invalid IL or missing references)
			Color pixel = baseTex.GetPixel(x, y);
			Color pixel2 = overlayTex.GetPixel(x, y);
			Color clear = Color.clear;
			if (pixel2.a > 0.9f)
			{
				clear = pixel2;
			}
			else if (pixel2.a > 0.1f)
			{
				float num = ((alphaOverride != 0f) ? alphaOverride : pixel2.a);
				clear = Color.Lerp(pixel, pixel2, num);
			}
			else
			{
				clear = pixel;
			}
			merged.SetPixel(x, y, clear);
		}
	}
	public class PrideBanner
	{
		[HarmonyPriority(100)]
		[HarmonyPatch(typeof(ObjectDB), "Awake")]
		public static class Patch_ObjectDB_Awake
		{
			private static void Postfix()
			{
				TryUpdateBanner();
			}
		}

		private static Texture OriginalTexture;

		private static Texture BannerCombinedTexture;

		public static void UpdateBanner()
		{
			GameObject banner = PrefabManager.Instance.GetPrefab("piece_banner06");
			PridePlugin.PrideLogger.LogDebug((object)"Replacing texture for piece_banner06!");
			ModifyMaterial(ref banner);
		}

		public static void ModifyMaterial(ref GameObject banner)
		{
			//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e8: Expected O, but got Unknown
			if ((Object)(object)banner == (Object)null)
			{
				PridePlugin.PrideLogger.LogWarning((object)"Cannot replace texture, object not found!");
				return;
			}
			Transform obj = Utils.FindChild(banner.transform, "default", (IterativeSearchType)0);
			MeshRenderer val = ((obj != null) ? ((Component)obj).GetComponent<MeshRenderer>() : null);
			if ((Object)(object)val == (Object)null || ((Renderer)val).materials == null || ((Renderer)val).materials.Length < 1)
			{
				PridePlugin.PrideLogger.LogWarning((object)"Cannot replace texture, texture not found!");
				return;
			}
			if (!PridePlugin.GetReplaceBanner())
			{
				if ((Object)(object)OriginalTexture != (Object)null)
				{
					((Renderer)val).materials[0].mainTexture = OriginalTexture;
				}
				return;
			}
			if ((Object)(object)OriginalTexture == (Object)null)
			{
				Texture mainTexture = ((Renderer)val).materials[0].mainTexture;
				if (mainTexture.isReadable)
				{
					OriginalTexture = mainTexture;
				}
				else
				{
					OriginalTexture = (Texture)(object)IconMerge.DuplicateTexture(mainTexture);
				}
			}
			if ((Object)(object)BannerCombinedTexture == (Object)null)
			{
				BannerCombinedTexture = (Texture)(object)IconMerge.MergeTextures((Texture2D)OriginalTexture, PridePlugin.BannerTexture);
			}
			((Renderer)val).materials[0].mainTexture = BannerCombinedTexture;
		}

		public static void TryUpdateBanner()
		{
			//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"))
			{
				UpdateBanner();
			}
		}
	}
	public class PrideShield
	{
		[HarmonyPriority(100)]
		[HarmonyPatch(typeof(ObjectDB), "Awake")]
		public static class Patch_ObjectDB_Awake
		{
			private static void Postfix()
			{
				TryUpdateShield();
			}
		}

		private static Texture OriginalTexture;

		private static Texture RoundShieldCombinedTexture;

		public static void UpdateShield()
		{
			GameObject shield = PrefabManager.Instance.GetPrefab("ShieldWood");
			PridePlugin.PrideLogger.LogDebug((object)"Replacing texture for ShieldWood!");
			ModifyMaterial(ref shield);
		}

		public static void ModifyMaterial(ref GameObject shield)
		{
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Expected O, but got Unknown
			if ((Object)(object)shield == (Object)null)
			{
				PridePlugin.PrideLogger.LogWarning((object)"Cannot replace texture, object not found!");
				return;
			}
			MeshRenderer componentInChildren = shield.GetComponentInChildren<MeshRenderer>();
			if ((Object)(object)componentInChildren == (Object)null || ((Renderer)componentInChildren).materials == null || ((Renderer)componentInChildren).materials.Length < 1)
			{
				PridePlugin.PrideLogger.LogWarning((object)"Cannot replace texture, texture not found!");
				return;
			}
			if (!PridePlugin.GetReplaceShields())
			{
				if ((Object)(object)OriginalTexture != (Object)null)
				{
					((Renderer)componentInChildren).materials[0].mainTexture = OriginalTexture;
				}
				return;
			}
			if ((Object)(object)OriginalTexture == (Object)null)
			{
				Texture mainTexture = ((Renderer)componentInChildren).materials[0].mainTexture;
				if (mainTexture.isReadable)
				{
					OriginalTexture = mainTexture;
				}
				else
				{
					OriginalTexture = (Texture)(object)IconMerge.DuplicateTexture(mainTexture);
				}
			}
			if ((Object)(object)RoundShieldCombinedTexture == (Object)null)
			{
				RoundShieldCombinedTexture = (Texture)(object)IconMerge.MergeTextures(PridePlugin.RoundShieldTexture, (Texture2D)OriginalTexture);
			}
			((Renderer)componentInChildren).materials[0].mainTexture = RoundShieldCombinedTexture;
		}

		public static void TryUpdateShield()
		{
			//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"))
			{
				UpdateShield();
			}
		}
	}
	public class PrideShip
	{
		[HarmonyPriority(100)]
		[HarmonyPatch(typeof(ObjectDB), "Awake")]
		public static class Patch_ObjectDB_Awake
		{
			private static void Postfix()
			{
				TryUpdateSails();
			}
		}

		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))
				{
					PridePlugin.PrideLogger.LogDebug((object)("Replacing sail texture for " + ((Object)prefab).name + "!"));
					ModifyMaterial(ref ship);
				}
			}
		}

		public static void ModifyMaterial(ref Ship ship)
		{
			MagicaCloth componentInChildren = ((Component)ship).GetComponentInChildren<MagicaCloth>();
			if (!((Object)(object)componentInChildren == (Object)null))
			{
				SkinnedMeshRenderer componentInChildren2 = ((Component)componentInChildren).GetComponentInChildren<SkinnedMeshRenderer>();
				if (!((Object)(object)componentInChildren2 == (Object)null) && ((Renderer)componentInChildren2).materials != null && ((Renderer)componentInChildren2).materials.Length >= 1)
				{
					((Renderer)componentInChildren2).materials[0].mainTexture = (Texture)(object)(PridePlugin.GetReplaceSailsPride() ? PridePlugin.PrideTexture : PridePlugin.TransTexture);
				}
			}
		}

		public static void TryUpdateSails()
		{
			//IL_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0014: Unknown result type (might be due to invalid IL or missing references)
			if (PridePlugin.GetReplaceSailsPride() || PridePlugin.GetReplaceSailsTrans())
			{
				Scene activeScene = SceneManager.GetActiveScene();
				if (((Scene)(ref activeScene)).name.Equals("main"))
				{
					UpdateSails();
				}
			}
		}
	}
}