Decompiled source of BeastheimBanners v1.3.2

BeastheimBanners.dll

Decompiled 7 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using Jotunn;
using Jotunn.Configs;
using Jotunn.Entities;
using Jotunn.Managers;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("0.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 BeastheimBanners
{
	[BepInPlugin("beastheim.banners", "BeastheimBanners", "1.3.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	public class BeastheimBannersPlugin : BaseUnityPlugin
	{
		private struct BannerDefinition
		{
			public string PrefabId;

			public string TextureFile;

			public string DisplayName;

			public string Description;

			public BannerDefinition(string prefabId, string textureFile, string displayName, string description)
			{
				PrefabId = prefabId;
				TextureFile = textureFile;
				DisplayName = displayName;
				Description = description;
			}
		}

		public const string PluginGuid = "beastheim.banners";

		public const string PluginName = "BeastheimBanners";

		public const string PluginVersion = "1.3.0";

		private const float TargetHeight = 2.974f;

		private const float TargetWidth = 1.18f;

		private const string VanillaBannerBase = "piece_banner01";

		private const string FirstPartyPrefabPrefix = "BeastheimBanner_";

		private static readonly BannerDefinition[] Banners = new BannerDefinition[5]
		{
			new BannerDefinition("banner1", "Banner1.png", "Beasts of Midgard War Banner", "Let the beast within answer the call of Midgard."),
			new BannerDefinition("banner5", "Banner5.png", "Beasts of Midgard Battle Standard", "Beneath the crossed axes, the beasts stand as one."),
			new BannerDefinition("BeastheimBanner_GildedLady", "GildedLady.png", "The Gilded Lady", "Gold for her grace. Ravens for her wisdom. Glory for those who earn her favor."),
			new BannerDefinition("BeastheimBanner_TheUnbound", "TheUnbound.png", "The Unbound", "No chain holds forever. When the last one breaks, even the gods will run."),
			new BannerDefinition("BeastheimBanner_WrathOfTheDeep", "WrathOfTheDeep.png", "Wrath of the Deep", "Where the sea meets the storm, even gods fear what stirs below.")
		};

		private void Awake()
		{
			PrefabManager.OnVanillaPrefabsAvailable += CreateBanners;
		}

		private void CreateBanners()
		{
			BannerDefinition[] banners = Banners;
			for (int i = 0; i < banners.Length; i++)
			{
				BannerDefinition bannerDefinition = banners[i];
				CreateBanner(bannerDefinition.PrefabId, bannerDefinition.TextureFile, bannerDefinition.DisplayName, bannerDefinition.Description);
			}
		}

		private void CreateBanner(string prefabName, string textureFileName, string displayName, string description)
		{
			//IL_00bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c6: Expected O, but got Unknown
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f4: Expected O, but got Unknown
			GameObject val = PrefabManager.Instance.CreateClonedPrefab(prefabName, "piece_banner01");
			if ((Object)(object)val == (Object)null)
			{
				Logger.LogError((object)("[BeastheimBanners] Failed to clone 'piece_banner01' for '" + prefabName + "' - is Jotunn's vanilla prefab cache populated yet?"));
				return;
			}
			Texture2D val2 = LoadTexture(textureFileName);
			if ((Object)(object)val2 == (Object)null)
			{
				Logger.LogError((object)("[BeastheimBanners] Could not load texture '" + textureFileName + "' for '" + prefabName + "'"));
				return;
			}
			Piece component = val.GetComponent<Piece>();
			if ((Object)(object)component != (Object)null)
			{
				component.m_name = displayName;
				component.m_description = description;
			}
			Renderer val3 = FindClothRenderer(val);
			if ((Object)(object)val3 == (Object)null)
			{
				Logger.LogWarning((object)("[BeastheimBanners] Could not identify the cloth renderer on cloned '" + prefabName + "' - texture/scale not applied, prefab still registered with vanilla art."));
			}
			else
			{
				Material val4 = new Material(val3.sharedMaterial);
				val4.mainTexture = (Texture)(object)val2;
				val3.sharedMaterial = val4;
				ApplySizeCompensation(val3, prefabName);
			}
			bool flag = PieceManager.Instance.AddPiece(new CustomPiece(val, PieceTables.Hammer, false));
			Logger.LogInfo((object)string.Format("[BeastheimBanners] Registered '{0}' (cloned from '{1}', texture '{2}', AddPiece={3})", prefabName, "piece_banner01", textureFileName, flag));
		}

		private Renderer FindClothRenderer(GameObject clone)
		{
			Renderer[] componentsInChildren = clone.GetComponentsInChildren<Renderer>(true);
			Renderer val = ((IEnumerable<Renderer>)componentsInChildren).FirstOrDefault((Func<Renderer, bool>)((Renderer r) => ((Object)((Component)r).gameObject).name == "default"));
			if ((Object)(object)val != (Object)null)
			{
				return val;
			}
			Renderer val2 = ((IEnumerable<Renderer>)componentsInChildren).FirstOrDefault((Func<Renderer, bool>)((Renderer r) => (Object)(object)r.sharedMaterial != (Object)null && (Object)(object)r.sharedMaterial.shader != (Object)null && ((Object)r.sharedMaterial.shader).name.IndexOf("Vegetation", StringComparison.OrdinalIgnoreCase) >= 0));
			if ((Object)(object)val2 != (Object)null)
			{
				return val2;
			}
			return componentsInChildren.OrderByDescending(delegate(Renderer r)
			{
				//IL_0001: Unknown result type (might be due to invalid IL or missing references)
				//IL_0006: 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)
				Bounds bounds = r.bounds;
				Vector3 size = ((Bounds)(ref bounds)).size;
				return ((Vector3)(ref size)).sqrMagnitude;
			}).FirstOrDefault();
		}

		private void ApplySizeCompensation(Renderer clothRenderer, string prefabName)
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_003f: Unknown result type (might be due to invalid IL or missing references)
			//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_0059: Unknown result type (might be due to invalid IL or missing references)
			//IL_008b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0090: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ac: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e5: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d0: Unknown result type (might be due to invalid IL or missing references)
			MeshFilter component = ((Component)clothRenderer).GetComponent<MeshFilter>();
			if ((Object)(object)component == (Object)null || (Object)(object)component.sharedMesh == (Object)null)
			{
				Logger.LogWarning((object)("[BeastheimBanners] '" + prefabName + "' cloth renderer has no MeshFilter/mesh - skipping size compensation."));
				return;
			}
			Bounds bounds = component.sharedMesh.bounds;
			Vector3 size = ((Bounds)(ref bounds)).size;
			Logger.LogInfo((object)$"[BeastheimBanners] '{prefabName}' vanilla cloth mesh local bounds: {size} (target height={2.974f}, width={1.18f})");
			Transform transform = ((Component)clothRenderer).transform;
			Vector3 localScale = transform.localScale;
			if (size.y > 0.0001f)
			{
				localScale.y *= 2.974f / size.y;
			}
			if (size.z > 0.0001f)
			{
				localScale.z *= 1.18f / size.z;
			}
			transform.localScale = localScale;
			Logger.LogInfo((object)$"[BeastheimBanners] '{prefabName}' cloth child rescaled to local scale {localScale}");
		}

		private Texture2D LoadTexture(string fileName)
		{
			//IL_0032: Unknown result type (might be due to invalid IL or missing references)
			//IL_0038: Expected O, but got Unknown
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0115: Unknown result type (might be due to invalid IL or missing references)
			//IL_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_012a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0133: Expected O, but got Unknown
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
			string text = Path.Combine(Path.GetDirectoryName(((BaseUnityPlugin)this).Info.Location), fileName);
			if (!File.Exists(text))
			{
				Logger.LogError((object)("[BeastheimBanners] Texture file not found: " + text));
				return null;
			}
			Bitmap val = new Bitmap(text);
			try
			{
				int width = ((Image)val).Width;
				int height = ((Image)val).Height;
				BitmapData val2 = val.LockBits(new Rectangle(0, 0, width, height), (ImageLockMode)1, (PixelFormat)2498570);
				byte[] array = new byte[val2.Stride * height];
				Marshal.Copy(val2.Scan0, array, 0, array.Length);
				val.UnlockBits(val2);
				Color32[] array2 = (Color32[])(object)new Color32[width * height];
				for (int i = 0; i < height; i++)
				{
					int num = (height - 1 - i) * val2.Stride;
					int num2 = i * width;
					for (int j = 0; j < width; j++)
					{
						int num3 = num + j * 4;
						byte b = array[num3];
						byte b2 = array[num3 + 1];
						byte b3 = array[num3 + 2];
						byte b4 = array[num3 + 3];
						array2[num2 + j] = new Color32(b3, b2, b, b4);
					}
				}
				Texture2D val3 = new Texture2D(width, height, (TextureFormat)4, false);
				val3.SetPixels32(array2);
				val3.Apply();
				((Texture)val3).wrapMode = (TextureWrapMode)1;
				((Object)val3).name = fileName;
				return val3;
			}
			finally
			{
				((IDisposable)val)?.Dispose();
			}
		}
	}
}