Decompiled source of VargheimArmoryRackStyles v0.1.0

plugins/VargheimArmoryRackStyles.dll

Decompiled 2 hours ago
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using HarmonyLib;
using Jotunn.Managers;
using Jotunn.Utils;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[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 Vargheim.ArmoryRack
{
	[BepInPlugin("digi.vargheim.armoryrackstyles", "Vargheim Armory Rack Styles", "0.1.0")]
	[BepInDependency(/*Could not decode attribute arguments.*/)]
	[BepInDependency("Therzie.Armory", "1.4.3")]
	[BepInDependency("Therzie.Warfare", "1.9.6")]
	[NetworkCompatibility(/*Could not decode attribute arguments.*/)]
	public sealed class Plugin : BaseUnityPlugin
	{
		private sealed class Reservation
		{
			internal Sprite[] Icons;

			internal ItemData PendingPicker;
		}

		private sealed class RackSet
		{
			internal GameObject Station;

			internal Mesh Mesh;

			internal Material[] Paints = (Material[])(object)new Material[7];

			internal Sprite BaseIcon;

			internal Sprite[] Icons = (Sprite[])(object)new Sprite[8];
		}

		public const string Version = "0.1.0";

		private const string Shield = "ShieldBronzeBanded_TW";

		private const string Station = "Armory_TW";

		private static readonly string[] Materials = new string[7] { "shieldmat6_TW", "shieldmat1_TW", "shieldmat2_TW", "shieldmat3_TW", "shieldmat5_TW", "shieldmat7_TW", "shieldmat4_TW" };

		private static Plugin instance;

		private static RackSet cached;

		private static readonly ConditionalWeakTable<SharedData, Reservation> reservations = new ConditionalWeakTable<SharedData, Reservation>();

		private static string lastIssue;

		private void Awake()
		{
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Expected O, but got Unknown
			//IL_00db: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e9: Expected O, but got Unknown
			//IL_0123: Unknown result type (might be due to invalid IL or missing references)
			//IL_0131: Expected O, but got Unknown
			instance = this;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Vargheim Armory Rack Styles 0.1.0 loaded.");
			Harmony val = new Harmony("digi.vargheim.armoryrackstyles");
			val.Patch((MethodBase)AccessTools.Method(typeof(ZNetScene), "Awake", (Type[])null, (Type[])null), (HarmonyMethod)null, Last("SceneReady"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(ObjectDB), "Awake", (Type[])null, (Type[])null), (HarmonyMethod)null, Last("ItemTableReady"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(ZoneSystem), "Start", (Type[])null, (Type[])null), (HarmonyMethod)null, Last("WorldReady"), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(ItemStyle), "Setup", new Type[1] { typeof(int) }, (Type[])null), new HarmonyMethod(typeof(Plugin), "DrawStyle", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			val.Patch((MethodBase)AccessTools.Method(typeof(VariantDialog), "Setup", new Type[1] { typeof(ItemData) }, (Type[])null), new HarmonyMethod(typeof(Plugin), "LimitPendingPicker", (Type[])null), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
		}

		private static HarmonyMethod Last(string name)
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Expected O, but got Unknown
			return new HarmonyMethod(typeof(Plugin), name, (Type[])null)
			{
				priority = 0
			};
		}

		private static void SceneReady(ZNetScene __instance)
		{
			Register(ObjectDB.instance, __instance);
		}

		private static void ItemTableReady(ObjectDB __instance)
		{
			Register(__instance, ZNetScene.instance);
		}

		private static void WorldReady()
		{
			Register(ObjectDB.instance, ZNetScene.instance);
		}

		private static bool DrawStyle(ItemStyle __instance, int __0)
		{
			RackModel component = ((Component)__instance).GetComponent<RackModel>();
			if (!((Object)(object)component == (Object)null))
			{
				return !component.Show(__0);
			}
			return true;
		}

		private static void LimitPendingPicker(ref ItemData __0)
		{
			if (__0 != null && __0.m_shared != null && reservations.TryGetValue(__0.m_shared, out var value) && value.PendingPicker != null)
			{
				__0 = value.PendingPicker;
			}
		}

		private static void Register(ObjectDB db, ZNetScene scene)
		{
			//IL_004f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0055: Invalid comparison between Unknown and I4
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			//IL_0135: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)db == (Object)null)
			{
				return;
			}
			try
			{
				GameObject itemPrefab = db.GetItemPrefab("ShieldBronzeBanded_TW");
				if (!Object.op_Implicit((Object)(object)itemPrefab))
				{
					return;
				}
				ItemDrop component = itemPrefab.GetComponent<ItemDrop>();
				if (!Object.op_Implicit((Object)(object)component))
				{
					throw new InvalidOperationException("Bronze Banded Shield has no ItemDrop.");
				}
				SharedData shared = component.m_itemData.m_shared;
				Reservation reservation = Reserve(shared);
				if ((int)SystemInfo.graphicsDeviceType == 4 || !Object.op_Implicit((Object)(object)scene))
				{
					return;
				}
				GameObject prefab = scene.GetPrefab("Armory_TW");
				if (!Object.op_Implicit((Object)(object)prefab))
				{
					return;
				}
				Transform val = itemPrefab.transform.Find("attach/model");
				if (!Object.op_Implicit((Object)(object)val) || !Object.op_Implicit((Object)(object)((Component)val).GetComponent<ItemStyle>()) || !Object.op_Implicit((Object)(object)((Component)val).GetComponent<MeshRenderer>()))
				{
					throw new InvalidOperationException("The equipped Bronze Banded Shield hierarchy changed.");
				}
				RackModel rackModel = ((Component)val).GetComponent<RackModel>();
				if (Object.op_Implicit((Object)(object)rackModel) && Object.op_Implicit((Object)(object)rackModel.Source) && reservation.PendingPicker == null)
				{
					return;
				}
				MeshFilter component2 = ((Component)val).GetComponent<MeshFilter>();
				if (Object.op_Implicit((Object)(object)component2) && Object.op_Implicit((Object)(object)component2.sharedMesh) && !(((Object)component2.sharedMesh).name != "ShieldIron"))
				{
					Bounds bounds = component2.sharedMesh.bounds;
					if (!(Mathf.Abs(((Bounds)(ref bounds)).size.x - 1.01884f) > 0.001f))
					{
						if (cached == null || (Object)(object)cached.Station != (Object)(object)prefab || (Object)(object)cached.BaseIcon != (Object)(object)shared.m_icons[0])
						{
							cached = ReadSet(prefab, shared.m_icons[0]);
						}
						if (!Object.op_Implicit((Object)(object)rackModel))
						{
							rackModel = ((Component)val).gameObject.AddComponent<RackModel>();
						}
						rackModel.Source = cached.Mesh;
						rackModel.Paints = cached.Paints;
						shared.m_icons = cached.Icons;
						reservation.Icons = shared.m_icons;
						reservation.PendingPicker = null;
						lastIssue = null;
						((BaseUnityPlugin)instance).Logger.LogInfo((object)"Bronze Banded Shield: original plus seven Armory rack styles registered.");
						return;
					}
				}
				throw new InvalidOperationException("The Bronze Banded Shield source mesh changed.");
			}
			catch (Exception ex)
			{
				if (lastIssue != ex.Message)
				{
					((BaseUnityPlugin)instance).Logger.LogWarning((object)ex.Message);
				}
				lastIssue = ex.Message;
			}
		}

		private static Reservation Reserve(SharedData data)
		{
			//IL_008f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0094: Unknown result type (might be due to invalid IL or missing references)
			//IL_0095: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a6: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Expected O, but got Unknown
			//IL_00b7: Expected O, but got Unknown
			if (reservations.TryGetValue(data, out var value))
			{
				if (data.m_variants != 8 || data.m_icons != value.Icons)
				{
					throw new InvalidOperationException("Another system changed the rack shield variant table.");
				}
				return value;
			}
			if (data.m_variants > 1 || data.m_icons == null || data.m_icons.Length != 1)
			{
				throw new InvalidOperationException("The base shield's style table differs from the supported one-icon layout.");
			}
			Sprite[] array = (Sprite[])(object)new Sprite[Materials.Length + 1];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = data.m_icons[0];
			}
			value = new Reservation
			{
				Icons = array,
				PendingPicker = new ItemData
				{
					m_shared = new SharedData
					{
						m_icons = data.m_icons,
						m_variants = 1
					}
				}
			};
			reservations.Add(data, value);
			data.m_icons = array;
			data.m_variants = array.Length;
			((BaseUnityPlugin)instance).Logger.LogInfo((object)"Bronze Banded Shield: reserved rack style save indices 1-7.");
			return value;
		}

		private static RackSet ReadSet(GameObject station, Sprite original)
		{
			//IL_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
			//IL_0106: Unknown result type (might be due to invalid IL or missing references)
			//IL_010b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0110: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_012c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0130: Unknown result type (might be due to invalid IL or missing references)
			RackSet rackSet = new RackSet
			{
				Station = station,
				BaseIcon = original
			};
			rackSet.Icons[0] = original;
			int num = 0;
			while (num < Materials.Length)
			{
				string text = "Shield rack/Shield prop (" + (num + 1) + ")";
				Transform val = station.transform.Find(text);
				MeshFilter val2 = (Object.op_Implicit((Object)(object)val) ? ((Component)val).GetComponent<MeshFilter>() : null);
				MeshRenderer val3 = (Object.op_Implicit((Object)(object)val) ? ((Component)val).GetComponent<MeshRenderer>() : null);
				if (Object.op_Implicit((Object)(object)val2) && Object.op_Implicit((Object)(object)val2.sharedMesh) && Object.op_Implicit((Object)(object)val3) && Object.op_Implicit((Object)(object)((Renderer)val3).sharedMaterial) && !(((Object)val2.sharedMesh).name != "Object_6_6_0") && !(((Object)((Renderer)val3).sharedMaterial).name != Materials[num]))
				{
					Bounds bounds = val2.sharedMesh.bounds;
					Vector3 val4 = ((Bounds)(ref bounds)).center - new Vector3(1652.2175f, 841.42786f, -300f);
					if (!(((Vector3)(ref val4)).sqrMagnitude > 0.01f))
					{
						bounds = val2.sharedMesh.bounds;
						if (!(Mathf.Abs(((Bounds)(ref bounds)).size.y - 600f) > 0.1f))
						{
							if (Object.op_Implicit((Object)(object)rackSet.Mesh) && (Object)(object)rackSet.Mesh != (Object)(object)val2.sharedMesh)
							{
								throw new InvalidOperationException("The rack props no longer share one shield mesh.");
							}
							rackSet.Mesh = val2.sharedMesh;
							rackSet.Paints[num] = ((Renderer)val3).sharedMaterial;
							num++;
							continue;
						}
					}
				}
				throw new InvalidOperationException("Unexpected Armory shield mesh or material at " + text);
			}
			for (int i = 1; i < rackSet.Icons.Length; i++)
			{
				rackSet.Icons[i] = original;
				try
				{
					rackSet.Icons[i] = MakeIcon(rackSet.Mesh, rackSet.Paints[i - 1], i);
				}
				catch (Exception ex)
				{
					((BaseUnityPlugin)instance).Logger.LogWarning((object)("Using original icon for rack variant " + i + ": " + ex.Message));
				}
			}
			return rackSet;
		}

		private static Sprite MakeIcon(Mesh mesh, Material paint, int index)
		{
			//IL_0011: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Expected O, but got Unknown
			//IL_002c: 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_0048: 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_0052: 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_0068: Unknown result type (might be due to invalid IL or missing references)
			GameObject val = new GameObject("ArmoryRackStyle" + index);
			val.transform.position = new Vector3(0f, 8000f, 0f);
			try
			{
				RackModel.AddView(val.transform, mesh, paint, Vector3.zero, 1f, Quaternion.Inverse(Quaternion.LookRotation(Vector3.right, Vector3.back)));
				Sprite obj = RenderManager.Instance.Render(val, Quaternion.identity);
				if (!Object.op_Implicit((Object)(object)obj))
				{
					throw new InvalidOperationException("Could not render rack style icon " + index);
				}
				return obj;
			}
			finally
			{
				val.SetActive(false);
				Object.Destroy((Object)(object)val);
			}
		}
	}
	public sealed class RackModel : MonoBehaviour
	{
		public Mesh Source;

		public Material[] Paints;

		private GameObject view;

		private MeshRenderer original;

		private bool previousEnabled;

		internal bool Show(int index)
		{
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_0089: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0098: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00dd: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ea: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f8: Unknown result type (might be due to invalid IL or missing references)
			//IL_0104: Unknown result type (might be due to invalid IL or missing references)
			//IL_0109: Unknown result type (might be due to invalid IL or missing references)
			//IL_010d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0118: 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_0122: Unknown result type (might be due to invalid IL or missing references)
			//IL_0127: Unknown result type (might be due to invalid IL or missing references)
			//IL_0140: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			if (index < 1 || Paints == null || index > Paints.Length || !Object.op_Implicit((Object)(object)Source))
			{
				if (Object.op_Implicit((Object)(object)view) && view.activeSelf)
				{
					view.SetActive(false);
					((Renderer)original).enabled = previousEnabled;
				}
				return false;
			}
			if (!Object.op_Implicit((Object)(object)view))
			{
				original = ((Component)this).GetComponent<MeshRenderer>();
				Bounds bounds = ((Component)this).GetComponent<MeshFilter>().sharedMesh.bounds;
				float num = Mathf.Min(((Bounds)(ref bounds)).size.x, ((Bounds)(ref bounds)).size.y);
				Quaternion val = Quaternion.LookRotation(Vector3.down, Vector3.right);
				Vector3 val2 = default(Vector3);
				((Vector3)(ref val2))..ctor(1633.1488f, 842.45544f, -301.76172f);
				Vector3 val3 = new Vector3(-0.012605f, 0f, 0.04614f);
				Vector3 val4 = val2;
				Bounds bounds2 = Source.bounds;
				Vector3 val5 = val4 - ((Bounds)(ref bounds2)).center;
				bounds2 = Source.bounds;
				Vector3 center = val3 - val * (val5 * (num / ((Bounds)(ref bounds2)).size.y));
				view = AddView(((Component)this).transform, Source, Paints[index - 1], center, num, val);
				IncludeInDropLods(view.GetComponent<MeshRenderer>());
				previousEnabled = ((Renderer)original).enabled;
			}
			else if (!view.activeSelf)
			{
				previousEnabled = ((Renderer)original).enabled;
				view.SetActive(true);
			}
			((Renderer)view.GetComponent<MeshRenderer>()).sharedMaterial = Paints[index - 1];
			((Renderer)original).enabled = false;
			return true;
		}

		private void IncludeInDropLods(MeshRenderer replacement)
		{
			ItemDrop componentInParent = ((Component)this).GetComponentInParent<ItemDrop>();
			LODGroup val = (Object.op_Implicit((Object)(object)componentInParent) ? ((Component)componentInParent).GetComponent<LODGroup>() : null);
			if (!Object.op_Implicit((Object)(object)val))
			{
				return;
			}
			LOD[] lODs = val.GetLODs();
			bool flag = false;
			for (int i = 0; i < lODs.Length; i++)
			{
				Renderer[] array = lODs[i].renderers;
				if (array != null && Array.IndexOf(array, (Renderer)(object)original) >= 0 && Array.IndexOf(array, (Renderer)(object)replacement) < 0)
				{
					Array.Resize(ref array, array.Length + 1);
					array[^1] = (Renderer)(object)replacement;
					lODs[i].renderers = array;
					flag = true;
				}
			}
			if (flag)
			{
				val.SetLODs(lODs);
			}
		}

		internal static GameObject AddView(Transform parent, Mesh mesh, Material paint, Vector3 center, float diameter, Quaternion rotation)
		{
			//IL_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000b: Unknown result type (might be due to invalid IL or missing references)
			//IL_001c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: 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_0045: 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_0058: Unknown result type (might be due to invalid IL or missing references)
			//IL_0062: Unknown result type (might be due to invalid IL or missing references)
			//IL_0068: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0071: Unknown result type (might be due to invalid IL or missing references)
			//IL_0074: Unknown result type (might be due to invalid IL or missing references)
			//IL_007a: Unknown result type (might be due to invalid IL or missing references)
			//IL_007f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0084: Unknown result type (might be due to invalid IL or missing references)
			//IL_008e: Unknown result type (might be due to invalid IL or missing references)
			//IL_009a: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a7: Expected O, but got Unknown
			Bounds bounds = mesh.bounds;
			float num = diameter / ((Bounds)(ref bounds)).size.y;
			GameObject val = new GameObject("rack_shield_face")
			{
				layer = ((Component)parent).gameObject.layer
			};
			val.transform.SetParent(parent, false);
			val.transform.localRotation = rotation;
			val.transform.localScale = Vector3.one * num;
			Transform transform = val.transform;
			bounds = mesh.bounds;
			transform.localPosition = center - rotation * (((Bounds)(ref bounds)).center * num);
			val.AddComponent<MeshFilter>().sharedMesh = mesh;
			((Renderer)val.AddComponent<MeshRenderer>()).sharedMaterial = paint;
			return val;
		}
	}
}