Decompiled source of CustomClanUIFixes v0.2.1

mt2_custom_clan_ui_fixes.Plugin.dll

Decompiled 4 days ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("mt2_custom_clan_ui_fixes.Plugin")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("0.2.1.0")]
[assembly: AssemblyInformationalVersion("0.2.1+91b56c14b27dbeaec34f669f0afecae12223fb15")]
[assembly: AssemblyProduct("mt2_custom_clan_ui_fixes.Plugin")]
[assembly: AssemblyTitle("mt2_custom_clan_ui_fixes.Plugin")]
[assembly: AssemblyVersion("0.2.1.0")]
[module: RefSafetyRules(11)]
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;
		}
	}
	[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 mt2_custom_clan_ui_fixes.Plugin
{
	[HarmonyPatch]
	public static class LogbookArtifactsPaging
	{
		public static bool Enabled = true;

		public static int ColumnsPerPage = 0;

		public static bool Balance = true;

		public static float WidthBudget = 0f;

		public static int RetryFrames = 10;

		public static bool Verbose = true;

		private static readonly FieldInfo? FColecciones = AccessTools.Field(typeof(CompendiumSectionBlessings), "collectionUIs");

		private static readonly FieldInfo? FPantalla = AccessTools.Field(typeof(CompendiumSection), "compendiumScreen");

		private static readonly MethodInfo? MRefrescarFlechas = AccessTools.Method(typeof(CompendiumScreen), "RefreshPageTurnZones", (Type[])null, (Type[])null);

		private static readonly List<List<int>> paginas = new List<List<int>>();

		private static int pagina;

		private static readonly List<float> anchos = new List<float>();

		private static readonly List<float> previas = new List<float>();

		private static bool medidasEstables;

		private static bool zonaTrazada;

		private static bool contenedorTrazado;

		private static int ultimoTotal = -1;

		private static int ultimasPaginas = -1;

		private static int ultimaPagina = -1;

		[HarmonyPatch(typeof(CompendiumSectionBlessings), "InitializeImpl")]
		[HarmonyPostfix]
		private static void TrasInicializar(CompendiumSectionBlessings __instance)
		{
			anchos.Clear();
			previas.Clear();
			medidasEstables = false;
			paginas.Clear();
			pagina = 0;
			Lanzar(__instance);
		}

		[HarmonyPatch(typeof(CompendiumSection), "Open")]
		[HarmonyPostfix]
		private static void TrasAbrir(CompendiumSection __instance)
		{
			CompendiumSectionBlessings val = (CompendiumSectionBlessings)(object)((__instance is CompendiumSectionBlessings) ? __instance : null);
			if (val != null)
			{
				pagina = 0;
				Lanzar(val);
			}
		}

		[HarmonyPatch(typeof(CompendiumSection), "CanTurnPage")]
		[HarmonyPrefix]
		private static bool AntesDePoderPasar(CompendiumSection __instance, TurnDir dir, ref bool __result)
		{
			//IL_0016: Unknown result type (might be due to invalid IL or missing references)
			//IL_0017: Unknown result type (might be due to invalid IL or missing references)
			//IL_0019: Expected I4, but got Unknown
			if (!Enabled || !(__instance is CompendiumSectionBlessings))
			{
				return true;
			}
			int num = pagina + dir;
			__result = paginas.Count > 1 && num >= 0 && num < paginas.Count;
			return false;
		}

		[HarmonyPatch(typeof(CompendiumSection), "TurnPage")]
		[HarmonyPrefix]
		private static bool AntesDePasar(CompendiumSection __instance, int dir)
		{
			if (Enabled)
			{
				CompendiumSectionBlessings val = (CompendiumSectionBlessings)(object)((__instance is CompendiumSectionBlessings) ? __instance : null);
				if (val != null)
				{
					if (paginas.Count <= 1)
					{
						return false;
					}
					int num = Mathf.Clamp(pagina + dir, 0, paginas.Count - 1);
					if (num != pagina)
					{
						pagina = num;
						Pintar(val);
					}
					return false;
				}
			}
			return true;
		}

		private static void Lanzar(CompendiumSectionBlessings seccion)
		{
			if (!Enabled || (Object)(object)seccion == (Object)null)
			{
				return;
			}
			Repartir(seccion);
			if (!((Behaviour)seccion).isActiveAndEnabled)
			{
				return;
			}
			try
			{
				((MonoBehaviour)seccion).StartCoroutine(RepartirUnosFrames(seccion));
			}
			catch (Exception ex)
			{
				Log("no se pudo encolar el reintento: " + ex.Message, aviso: true);
			}
		}

		private static IEnumerator RepartirUnosFrames(CompendiumSectionBlessings seccion)
		{
			for (int i = 0; i < Mathf.Max(1, RetryFrames); i++)
			{
				yield return null;
				if ((Object)(object)seccion == (Object)null || !((Behaviour)seccion).isActiveAndEnabled)
				{
					break;
				}
				Repartir(seccion);
			}
		}

		private static void Repartir(CompendiumSectionBlessings seccion)
		{
			try
			{
				List<RectTransform> list = Columnas(seccion);
				if (list.Count == 0)
				{
					return;
				}
				Transform parent = ((Transform)list[0]).parent;
				RectTransform val = (RectTransform)(object)((parent is RectTransform) ? parent : null);
				if (val == null)
				{
					return;
				}
				if (!medidasEstables || anchos.Count != list.Count)
				{
					bool flag = true;
					foreach (RectTransform item in list)
					{
						if (!((Component)item).gameObject.activeSelf)
						{
							((Component)item).gameObject.SetActive(true);
							flag = false;
						}
					}
					if (!flag || !Medir(list))
					{
						return;
					}
				}
				TrazarContenedor(val);
				float num = AnchoUtil(val) - PaddingHorizontal((Transform)(object)val);
				if (!(num <= 1f))
				{
					List<List<int>> collection = ((ColumnsPerPage > 0) ? PorNumeroFijo(list.Count, ColumnsPerPage) : (Balance ? Equilibrado(num) : Llenando(num)));
					paginas.Clear();
					paginas.AddRange(collection);
					pagina = Mathf.Clamp(pagina, 0, paginas.Count - 1);
					Pintar(seccion);
				}
			}
			catch (Exception ex)
			{
				Log("fallo repartiendo la pagina: " + ex, aviso: true);
			}
		}

		private static List<List<int>> Equilibrado(float presupuesto)
		{
			int count = Llenando(presupuesto).Count;
			if (count <= 1)
			{
				return Llenando(presupuesto);
			}
			float num = 0f;
			foreach (float ancho in anchos)
			{
				num = Mathf.Max(num, ancho);
			}
			float num2 = num;
			float num3 = presupuesto;
			for (int i = 0; i < 30; i++)
			{
				if (!(num3 - num2 > 0.5f))
				{
					break;
				}
				float num4 = (num2 + num3) / 2f;
				if (Llenando(num4).Count <= count)
				{
					num3 = num4;
				}
				else
				{
					num2 = num4;
				}
			}
			return Llenando(num3);
		}

		private static List<List<int>> Llenando(float capacidad)
		{
			List<List<int>> list = new List<List<int>>();
			List<int> list2 = new List<int>();
			float num = 0f;
			for (int i = 0; i < anchos.Count; i++)
			{
				if (list2.Count > 0 && num + anchos[i] > capacidad + 0.5f)
				{
					list.Add(list2);
					list2 = new List<int>();
					num = 0f;
				}
				list2.Add(i);
				num += anchos[i];
			}
			if (list2.Count > 0)
			{
				list.Add(list2);
			}
			return list;
		}

		private static List<List<int>> PorNumeroFijo(int total, int porPagina)
		{
			List<List<int>> list = new List<List<int>>();
			List<int> list2 = new List<int>();
			for (int i = 0; i < total; i++)
			{
				if (list2.Count >= porPagina)
				{
					list.Add(list2);
					list2 = new List<int>();
				}
				list2.Add(i);
			}
			if (list2.Count > 0)
			{
				list.Add(list2);
			}
			return list;
		}

		private static bool Medir(List<RectTransform> columnas)
		{
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_005b: 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)
			List<float> list = new List<float>(columnas.Count);
			List<float> list2 = new List<float>(columnas.Count);
			List<float> list3 = new List<float>(columnas.Count);
			for (int i = 0; i < columnas.Count; i++)
			{
				float num = ((i + 1 < columnas.Count) ? Mathf.Abs(((Transform)columnas[i + 1]).localPosition.x - ((Transform)columnas[i]).localPosition.x) : 0f);
				Rect rect = columnas[i].rect;
				float num2 = Mathf.Max(0f, ((Rect)(ref rect)).width);
				list2.Add(num2);
				list3.Add(num);
				float num3 = Mathf.Max(num2, num);
				if (num3 <= 1f && i > 0)
				{
					num3 = list[i - 1];
				}
				list.Add(num3);
			}
			float num4 = 0f;
			foreach (float item in list)
			{
				num4 += item;
			}
			if (num4 <= 1f)
			{
				previas.Clear();
				return false;
			}
			bool flag = previas.Count == list.Count;
			if (flag)
			{
				for (int j = 0; j < list.Count; j++)
				{
					if (Mathf.Abs(previas[j] - list[j]) > 0.5f)
					{
						flag = false;
						break;
					}
				}
			}
			previas.Clear();
			previas.AddRange(list);
			if (!flag)
			{
				return false;
			}
			anchos.Clear();
			anchos.AddRange(list);
			medidasEstables = true;
			StringBuilder stringBuilder = new StringBuilder();
			StringBuilder stringBuilder2 = new StringBuilder();
			for (int k = 0; k < list.Count; k++)
			{
				stringBuilder.Append((k == 0) ? "" : " ").Append($"{list[k]:0}");
				stringBuilder2.Append((k == 0) ? "" : " ").Append($"{list2[k]:0}/{list3[k]:0}");
			}
			Log($"medidas confirmadas: {list.Count} columnas, {num4:0} px en total, " + $"ocupacion [{stringBuilder}]");
			Log($"rect/paso por columna: [{stringBuilder2}]");
			return true;
		}

		private static void Pintar(CompendiumSectionBlessings seccion)
		{
			List<RectTransform> list = Columnas(seccion);
			if (list.Count == 0 || paginas.Count == 0)
			{
				return;
			}
			List<int> list2 = paginas[Mathf.Clamp(pagina, 0, paginas.Count - 1)];
			for (int i = 0; i < list.Count; i++)
			{
				bool flag = paginas.Count <= 1 || list2.Contains(i);
				if (((Component)list[i]).gameObject.activeSelf != flag)
				{
					((Component)list[i]).gameObject.SetActive(flag);
				}
			}
			RefrescarFlechas(seccion);
			if (list.Count == ultimoTotal && paginas.Count == ultimasPaginas && pagina == ultimaPagina)
			{
				return;
			}
			ultimoTotal = list.Count;
			ultimasPaginas = paginas.Count;
			ultimaPagina = pagina;
			StringBuilder stringBuilder = new StringBuilder();
			foreach (List<int> pagina in paginas)
			{
				float num = 0f;
				foreach (int item in pagina)
				{
					if (item < anchos.Count)
					{
						num += anchos[item];
					}
				}
				stringBuilder.Append((stringBuilder.Length == 0) ? "" : " + ").Append($"{pagina.Count} ({num:0} px)");
			}
			Log($"{list.Count} columnas en {paginas.Count} pagina(s) [{stringBuilder}], " + $"mostrando la {LogbookArtifactsPaging.pagina + 1} con {list2.Count}");
		}

		private static void RefrescarFlechas(CompendiumSectionBlessings seccion)
		{
			try
			{
				object obj = FPantalla?.GetValue(seccion);
				if (obj != null)
				{
					MRefrescarFlechas?.Invoke(obj, null);
				}
			}
			catch (Exception ex)
			{
				Log("no se pudieron refrescar las flechas: " + ex.Message, aviso: true);
			}
		}

		private static List<RectTransform> Columnas(CompendiumSectionBlessings seccion)
		{
			List<RectTransform> list = new List<RectTransform>();
			if (!(FColecciones?.GetValue(seccion) is IEnumerable enumerable))
			{
				return list;
			}
			foreach (object item in enumerable)
			{
				Component val = (Component)((item is Component) ? item : null);
				if (val != null && (Object)(object)val != (Object)null)
				{
					Transform transform = val.transform;
					RectTransform val2 = (RectTransform)(object)((transform is RectTransform) ? transform : null);
					if (val2 != null)
					{
						list.Add(val2);
					}
				}
			}
			return list;
		}

		private static float AnchoUtil(RectTransform raiz)
		{
			//IL_0019: Unknown result type (might be due to invalid IL or missing references)
			//IL_0067: 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_007b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0080: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Unknown result type (might be due to invalid IL or missing references)
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0050: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_0112: Unknown result type (might be due to invalid IL or missing references)
			//IL_0117: Unknown result type (might be due to invalid IL or missing references)
			if (WidthBudget > 1f)
			{
				return WidthBudget;
			}
			StringBuilder stringBuilder = new StringBuilder();
			float x = ((Transform)raiz).lossyScale.x;
			float num = 0f;
			bool flag = AnchoLoMandaElContenido((Transform)(object)raiz);
			Rect rect;
			if (!flag)
			{
				rect = raiz.rect;
				if (((Rect)(ref rect)).width > 1f)
				{
					rect = raiz.rect;
					num = ((Rect)(ref rect)).width;
				}
			}
			string name = ((Object)raiz).name;
			rect = raiz.rect;
			object arg = ((Rect)(ref rect)).width;
			rect = raiz.rect;
			stringBuilder.Append($"{name} {arg:0}x{((Rect)(ref rect)).height:0}").Append(flag ? " (el ancho lo manda el contenido, no cuenta)" : "");
			Transform parent = ((Transform)raiz).parent;
			RectTransform val = (RectTransform)(object)((parent is RectTransform) ? parent : null);
			int num2 = 0;
			while ((Object)(object)val != (Object)null && num2++ < 8)
			{
				rect = val.rect;
				float width = ((Rect)(ref rect)).width;
				float num3 = ((x > 0.0001f) ? (width * (((Transform)val).lossyScale.x / x)) : width);
				bool flag2 = AnchoLoMandaElContenido((Transform)(object)val);
				string name2 = ((Object)val).name;
				object arg2 = width;
				rect = val.rect;
				stringBuilder.Append($" <- {name2} {arg2:0}x{((Rect)(ref rect)).height:0}").Append((Mathf.Abs(num3 - width) > 1f) ? $" (={num3:0} en la raiz)" : "").Append(flag2 ? " (crece, no cuenta)" : "");
				if (!flag2 && num3 > 1f && (num <= 0f || num3 < num))
				{
					num = num3;
				}
				Transform parent2 = ((Transform)val).parent;
				val = (RectTransform)(object)((parent2 is RectTransform) ? parent2 : null);
			}
			if (!zonaTrazada)
			{
				zonaTrazada = true;
				Log($"zona: {stringBuilder} -> ancho util {num:0}");
			}
			return num;
		}

		private static void TrazarContenedor(RectTransform raiz)
		{
			if (contenedorTrazado || !Verbose)
			{
				return;
			}
			contenedorTrazado = true;
			StringBuilder stringBuilder = new StringBuilder();
			Component[] components = ((Component)raiz).GetComponents<Component>();
			foreach (Component val in components)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				Type type = ((object)val).GetType();
				stringBuilder.Append(' ').Append(type.Name);
				if (!type.Name.Contains("LayoutGroup"))
				{
					continue;
				}
				stringBuilder.Append('(');
				string[] array = new string[8] { "spacing", "cellSize", "constraint", "constraintCount", "childForceExpandWidth", "childControlWidth", "childAlignment", "reverseArrangement" };
				foreach (string text in array)
				{
					object obj = null;
					try
					{
						obj = type.GetProperty(text)?.GetValue(val, null);
					}
					catch
					{
					}
					if (obj != null)
					{
						stringBuilder.Append(text).Append('=').Append(obj)
							.Append(' ');
					}
				}
				stringBuilder.Append($"padding={PaddingHorizontal((Transform)(object)raiz):0})");
			}
			Log($"contenedor {((Object)raiz).name}:{stringBuilder}");
		}

		private static float PaddingHorizontal(Transform t)
		{
			Component[] components = ((Component)t).GetComponents<Component>();
			foreach (Component val in components)
			{
				if ((Object)(object)val == (Object)null || !((object)val).GetType().Name.Contains("LayoutGroup"))
				{
					continue;
				}
				try
				{
					object obj = ((object)val).GetType().GetProperty("padding")?.GetValue(val, null);
					if (obj == null)
					{
						continue;
					}
					object obj2 = obj.GetType().GetProperty("left")?.GetValue(obj, null);
					object obj3 = obj.GetType().GetProperty("right")?.GetValue(obj, null);
					if (obj2 == null)
					{
						obj2 = 0;
					}
					return Convert.ToSingle(obj2) + Convert.ToSingle(obj3 ?? ((object)0));
				}
				catch
				{
					return 0f;
				}
			}
			return 0f;
		}

		private static bool AnchoLoMandaElContenido(Transform t)
		{
			Component[] components = ((Component)t).GetComponents<Component>();
			foreach (Component val in components)
			{
				if ((Object)(object)val == (Object)null || !((object)val).GetType().Name.Contains("ContentSizeFitter"))
				{
					continue;
				}
				Behaviour val2 = (Behaviour)(object)((val is Behaviour) ? val : null);
				if (val2 != null && !val2.enabled)
				{
					continue;
				}
				try
				{
					PropertyInfo property = ((object)val).GetType().GetProperty("horizontalFit");
					if (property == null)
					{
						return true;
					}
					object value = property.GetValue(val, null);
					if (value == null)
					{
						return true;
					}
					return Convert.ToInt32(value) != 0;
				}
				catch
				{
					return true;
				}
			}
			return false;
		}

		private static void Log(string mensaje, bool aviso = false)
		{
			if (aviso)
			{
				Plugin.Logger.LogWarning((object)("[ArtifactsPaging] " + mensaje));
			}
			else if (Verbose)
			{
				Plugin.Logger.LogInfo((object)("[ArtifactsPaging] " + mensaje));
			}
		}
	}
	[HarmonyPatch]
	public static class LogbookClanFit
	{
		public static bool Enabled = true;

		public static float MinScale = 0.45f;

		public static int MaxAutoColumns = 2;

		public static float ColumnSpacing = 16f;

		public static float HeaderReserve = 130f;

		public static float ScaleMultiplier = 1f;

		public static float HeightBudget = 0f;

		public static float WidthBudget = 0f;

		public static int RetryFrames = 5;

		public static bool Verbose = true;

		private static readonly FieldInfo? FClasses = AccessTools.Field(typeof(CompendiumSectionChampUpgrades), "classesOptionRoot");

		private static readonly FieldInfo? FCrew = AccessTools.Field(typeof(CompendiumSectionChampUpgrades), "crewClassesOptionRoot");

		private static readonly FieldInfo? FClases = AccessTools.Field(typeof(CompendiumSectionChampUpgrades), "availableClasses");

		private static float itemNatural;

		private static float vgapNatural;

		private static float hgapNatural;

		private static Vector2 origen;

		private static bool yaListado;

		private static int ultimoTotal = -1;

		private static int ultimoN = -1;

		[HarmonyPatch(typeof(CompendiumSectionChampUpgrades), "InitializeImpl")]
		[HarmonyPostfix]
		private static void TrasInicializar(CompendiumSectionChampUpgrades __instance)
		{
			Lanzar(__instance);
		}

		[HarmonyPatch(typeof(CompendiumSectionChampUpgrades), "Open")]
		[HarmonyPostfix]
		private static void TrasAbrir(CompendiumSectionChampUpgrades __instance)
		{
			Lanzar(__instance);
		}

		private static void Lanzar(CompendiumSectionChampUpgrades seccion)
		{
			if (!Enabled || (Object)(object)seccion == (Object)null)
			{
				return;
			}
			Encajar(seccion);
			if (!((Behaviour)seccion).isActiveAndEnabled)
			{
				return;
			}
			try
			{
				((MonoBehaviour)seccion).StartCoroutine(EncajarUnosFrames(seccion));
			}
			catch (Exception ex)
			{
				Log("no se pudo encolar el reintento: " + ex.Message, aviso: true);
			}
		}

		private static IEnumerator EncajarUnosFrames(CompendiumSectionChampUpgrades seccion)
		{
			for (int i = 0; i < Mathf.Max(1, RetryFrames); i++)
			{
				yield return null;
				if ((Object)(object)seccion == (Object)null || !((Behaviour)seccion).isActiveAndEnabled)
				{
					break;
				}
				Encajar(seccion);
			}
		}

		private static void Encajar(CompendiumSectionChampUpgrades seccion)
		{
			//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ec: Unknown result type (might be due to invalid IL or missing references)
			//IL_0155: Unknown result type (might be due to invalid IL or missing references)
			//IL_015a: Unknown result type (might be due to invalid IL or missing references)
			//IL_018e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0193: Unknown result type (might be due to invalid IL or missing references)
			//IL_0178: Unknown result type (might be due to invalid IL or missing references)
			//IL_017d: Unknown result type (might be due to invalid IL or missing references)
			//IL_01fd: Unknown result type (might be due to invalid IL or missing references)
			//IL_03bf: Unknown result type (might be due to invalid IL or missing references)
			//IL_0225: Unknown result type (might be due to invalid IL or missing references)
			//IL_0211: Unknown result type (might be due to invalid IL or missing references)
			//IL_03e7: Unknown result type (might be due to invalid IL or missing references)
			//IL_03d3: Unknown result type (might be due to invalid IL or missing references)
			//IL_0414: Unknown result type (might be due to invalid IL or missing references)
			//IL_042a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0436: Unknown result type (might be due to invalid IL or missing references)
			//IL_0486: Unknown result type (might be due to invalid IL or missing references)
			//IL_048b: Unknown result type (might be due to invalid IL or missing references)
			//IL_048f: Unknown result type (might be due to invalid IL or missing references)
			//IL_04a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_04a9: Unknown result type (might be due to invalid IL or missing references)
			//IL_04ad: Unknown result type (might be due to invalid IL or missing references)
			//IL_0566: Unknown result type (might be due to invalid IL or missing references)
			//IL_056b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0575: Unknown result type (might be due to invalid IL or missing references)
			//IL_051a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0531: Unknown result type (might be due to invalid IL or missing references)
			//IL_05df: Unknown result type (might be due to invalid IL or missing references)
			//IL_05e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_05f7: Unknown result type (might be due to invalid IL or missing references)
			//IL_05fc: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				ListarClanes(seccion);
				Transform val = ComoTransform(FClasses?.GetValue(seccion));
				Transform val2 = ComoTransform(FCrew?.GetValue(seccion));
				if ((Object)(object)val == (Object)null)
				{
					return;
				}
				Transform parent = val.parent;
				RectTransform val3 = (RectTransform)(object)((parent is RectTransform) ? parent : null);
				if (val3 == null)
				{
					return;
				}
				bool flag = (Object)(object)val2 != (Object)null && ((Component)val2).gameObject.activeInHierarchy;
				List<Transform> list = new List<Transform>();
				list.AddRange(Hijos(val));
				if (flag)
				{
					list.AddRange(Hijos(val2));
				}
				if (list.Count == 0)
				{
					return;
				}
				RectTransform val4 = Zona(val3);
				if ((Object)(object)val4 == (Object)null)
				{
					return;
				}
				Rect rect;
				float num;
				if (!(WidthBudget > 1f))
				{
					rect = val4.rect;
					num = ((Rect)(ref rect)).width;
				}
				else
				{
					num = WidthBudget;
				}
				float num2 = num;
				float num3;
				if (!(HeightBudget > 1f))
				{
					rect = val4.rect;
					num3 = ((Rect)(ref rect)).height;
				}
				else
				{
					num3 = HeightBudget;
				}
				float num4 = num3;
				if (num4 <= 1f || num2 <= 1f)
				{
					return;
				}
				float num5 = Mathf.Clamp(HeaderReserve, 0f, num4 * 0.5f);
				num4 -= num5;
				if (MaxAutoColumns <= 2)
				{
					RectTransform val5 = (RectTransform)(object)((val is RectTransform) ? val : null);
					float num6;
					if (val5 == null)
					{
						num6 = 0f;
					}
					else
					{
						rect = val5.rect;
						num6 = ((Rect)(ref rect)).height;
					}
					float num7 = num6;
					if (flag)
					{
						RectTransform val6 = (RectTransform)(object)((val2 is RectTransform) ? val2 : null);
						if (val6 != null)
						{
							float num8 = num7;
							rect = val6.rect;
							num7 = Mathf.Max(num8, ((Rect)(ref rect)).height);
						}
					}
					rect = val3.rect;
					float width = ((Rect)(ref rect)).width;
					if (!(num7 <= 1f))
					{
						float num9 = Mathf.Min(1f, num4 / num7);
						if (width > 1f)
						{
							num9 = Mathf.Min(num9, num2 / width);
						}
						num9 = Mathf.Clamp(num9 * Mathf.Clamp(ScaleMultiplier, 0.1f, 1f), MinScale, 1f);
						val.localScale = Vector3.one;
						if ((Object)(object)val2 != (Object)null)
						{
							val2.localScale = Vector3.one;
						}
						((Transform)val3).localScale = new Vector3(num9, num9, 1f);
						if (list.Count != ultimoTotal || ultimoN != 2)
						{
							ultimoTotal = list.Count;
							ultimoN = 2;
							Log($"{list.Count} rombos, rejilla del juego, contenido " + $"{width:0}x{num7:0}, zona {num2:0}x{num4:0}, factor {num9:0.00}");
						}
					}
				}
				else
				{
					if (!Medir(val, flag ? val2 : null))
					{
						return;
					}
					ApagarLayouts(val);
					if (flag)
					{
						ApagarLayouts(val2);
					}
					ApagarLayouts((Transform)(object)val3);
					int count = list.Count;
					int num10 = 2;
					float num11 = 0f;
					for (int i = 1; i <= Mathf.Max(1, MaxAutoColumns); i++)
					{
						int num12 = Mathf.CeilToInt((float)count / (float)i);
						float num13 = ((i > 2) ? ColumnSpacing : hgapNatural);
						float num14 = (float)num12 * itemNatural + (float)(num12 - 1) * vgapNatural;
						float num15 = (float)i * itemNatural + (float)(i - 1) * num13;
						float num16 = Mathf.Min(1f, Mathf.Min(num4 / num14, num2 / num15));
						if (num16 > num11 + 0.001f)
						{
							num11 = num16;
							num10 = i;
						}
					}
					num11 = Mathf.Clamp(num11 * Mathf.Clamp(ScaleMultiplier, 0.1f, 1f), MinScale, 1f);
					val.localScale = Vector3.one;
					if ((Object)(object)val2 != (Object)null)
					{
						val2.localScale = Vector3.one;
					}
					((Transform)val3).localScale = new Vector3(num11, num11, 1f);
					int num17 = Mathf.CeilToInt((float)count / (float)num10);
					float num18 = ((num10 > 2) ? ColumnSpacing : hgapNatural);
					float num19 = ((((Transform)val4).lossyScale.x != 0f) ? (((Transform)val3).lossyScale.x / ((Transform)val4).lossyScale.x) : num11);
					float num20 = (itemNatural + num18) * num19;
					float num21 = (itemNatural + vgapNatural) * num19;
					float num22 = itemNatural * num19;
					float num23 = (float)(num10 - 1) * num20 + num22;
					float num24 = (float)(num17 - 1) * num21 + num22;
					rect = val4.rect;
					float num25 = ((Rect)(ref rect)).center.y - num5 / 2f;
					rect = val4.rect;
					float num26 = ((Rect)(ref rect)).center.x - num23 / 2f + num22 / 2f;
					float num27 = num25 + num24 / 2f - num22 / 2f;
					for (int j = 0; j < count; j++)
					{
						int num28 = j / num17;
						int num29 = j % num17;
						Transform obj = list[j];
						float num30 = 0f;
						float num31 = 0f;
						RectTransform val7 = (RectTransform)(object)((obj is RectTransform) ? obj : null);
						if (val7 != null)
						{
							num30 = (val7.pivot.x - 0.5f) * num22;
							num31 = (val7.pivot.y - 0.5f) * num22;
						}
						obj.position = ((Transform)val4).TransformPoint(new Vector3(num26 + (float)num28 * num20 + num30, num27 - (float)num29 * num21 + num31, 0f));
						obj.localScale = Vector3.one;
					}
					if (count != ultimoTotal || num10 != ultimoN)
					{
						ultimoTotal = count;
						ultimoN = num10;
						string text = $"rejilla {num23:0}x{num24:0} centrada en {((Object)val4).name} ";
						object[] array = new object[4];
						rect = val4.rect;
						array[0] = ((Rect)(ref rect)).width;
						rect = val4.rect;
						array[1] = ((Rect)(ref rect)).height;
						array[2] = num26;
						array[3] = num27;
						Log(text + string.Format("({0:0}x{1:0}), salida ({2:0}, {3:0})", array));
						Log($"{count} rombos en {num10} columnas de {num17}, factor {num11:0.00} " + $"(zona {num2:0}x{num4:0} tras reservar {num5:0} de encabezado, " + $"rombo {itemNatural:0}, huecos {vgapNatural:0}/{num18:0})");
					}
				}
			}
			catch (Exception ex)
			{
				Log("fallo encajando la pagina: " + ex, aviso: true);
			}
		}

		private static List<Transform> Hijos(Transform raiz)
		{
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Expected O, but got Unknown
			List<Transform> list = new List<Transform>();
			foreach (Transform item in raiz)
			{
				Transform val = item;
				if (((Component)val).gameObject.activeSelf)
				{
					list.Add(val);
				}
			}
			return list;
		}

		private static bool Medir(Transform c1, Transform? c2)
		{
			//IL_0043: 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_0053: 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_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_006f: 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_0114: Unknown result type (might be due to invalid IL or missing references)
			//IL_0120: Unknown result type (might be due to invalid IL or missing references)
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_013b: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
			//IL_00da: Unknown result type (might be due to invalid IL or missing references)
			if (itemNatural > 0f)
			{
				return true;
			}
			List<Transform> list = Hijos(c1);
			if (list.Count < 2)
			{
				return false;
			}
			Transform obj = list[0];
			RectTransform val = (RectTransform)(object)((obj is RectTransform) ? obj : null);
			if (val != null)
			{
				Transform obj2 = list[1];
				RectTransform val2 = (RectTransform)(object)((obj2 is RectTransform) ? obj2 : null);
				if (val2 != null)
				{
					Rect rect = val.rect;
					float height = ((Rect)(ref rect)).height;
					rect = val.rect;
					float width = ((Rect)(ref rect)).width;
					float num = Mathf.Abs(((Transform)val2).localPosition.y - ((Transform)val).localPosition.y);
					if (height < 10f || width < 10f || num < 10f)
					{
						return false;
					}
					itemNatural = height;
					vgapNatural = Mathf.Clamp(num - height, 0f, 200f);
					hgapNatural = 96f;
					if ((Object)(object)c2 != (Object)null)
					{
						float num2 = Mathf.Abs(c2.localPosition.x - c1.localPosition.x);
						if (num2 > width && num2 - width < 400f)
						{
							hgapNatural = num2 - width;
						}
					}
					origen = new Vector2(c1.localPosition.x + ((Transform)val).localPosition.x, c1.localPosition.y + ((Transform)val).localPosition.y);
					Log($"medidas naturales: rombo {itemNatural:0}x{width:0}, fila {vgapNatural:0}, " + $"columna {hgapNatural:0}, salida ({origen.x:0}, {origen.y:0})");
					return true;
				}
			}
			return false;
		}

		private static void ApagarLayouts(Transform t)
		{
			Component[] components = ((Component)t).GetComponents<Component>();
			foreach (Component val in components)
			{
				if ((Object)(object)val == (Object)null)
				{
					continue;
				}
				string name = ((object)val).GetType().Name;
				if (name.Contains("LayoutGroup") || name.Contains("ContentSizeFitter"))
				{
					Behaviour val2 = (Behaviour)(object)((val is Behaviour) ? val : null);
					if (val2 != null && val2.enabled)
					{
						val2.enabled = false;
						Log("layout apagado en " + ((Object)t).name + ": " + name);
					}
				}
			}
		}

		private static RectTransform? Zona(RectTransform desde)
		{
			//IL_0023: 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_0036: Unknown result type (might be due to invalid IL or missing references)
			//IL_003b: 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)
			//IL_0059: 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_006e: Unknown result type (might be due to invalid IL or missing references)
			StringBuilder stringBuilder = new StringBuilder();
			Transform parent = ((Transform)desde).parent;
			RectTransform val = (RectTransform)(object)((parent is RectTransform) ? parent : null);
			int num = 0;
			while ((Object)(object)val != (Object)null && num++ < 8)
			{
				string name = ((Object)val).name;
				Rect rect = val.rect;
				object arg = ((Rect)(ref rect)).width;
				rect = val.rect;
				stringBuilder.Append($" <- {name} {arg:0}x{((Rect)(ref rect)).height:0}");
				rect = val.rect;
				if (((Rect)(ref rect)).width > 1f)
				{
					rect = val.rect;
					if (((Rect)(ref rect)).height > 1f)
					{
						return val;
					}
				}
				Transform parent2 = ((Transform)val).parent;
				val = (RectTransform)(object)((parent2 is RectTransform) ? parent2 : null);
			}
			Log($"zona: ningun ancestro mide;{stringBuilder}", aviso: true);
			return null;
		}

		private static void ListarClanes(CompendiumSectionChampUpgrades seccion)
		{
			if (yaListado || !Verbose || FClases == null || !(FClases.GetValue(seccion) is IEnumerable enumerable))
			{
				return;
			}
			List<string> list = new List<string>();
			foreach (object item in enumerable)
			{
				if (item != null)
				{
					MethodInfo? method = item.GetType().GetMethod("GetTitle", Type.EmptyTypes);
					MethodInfo method2 = item.GetType().GetMethod("IsCrew", Type.EmptyTypes);
					string text = (method?.Invoke(item, null) as string) ?? item.ToString();
					object obj = method2?.Invoke(item, null);
					bool flag = obj is bool && (bool)obj;
					list.Add(flag ? (text + " (crew)") : text);
				}
			}
			yaListado = true;
			Log($"clases registradas ({list.Count}): " + string.Join(", ", list));
		}

		private static Transform? ComoTransform(object? o)
		{
			if (o != null)
			{
				GameObject val = (GameObject)((o is GameObject) ? o : null);
				if (val == null)
				{
					Component val2 = (Component)((o is Component) ? o : null);
					if (val2 != null)
					{
						return val2.transform;
					}
					return null;
				}
				return val.transform;
			}
			return null;
		}

		private static void Log(string mensaje, bool aviso = false)
		{
			if (aviso)
			{
				Plugin.Logger.LogWarning((object)("[LogbookFit] " + mensaje));
			}
			else if (Verbose)
			{
				Plugin.Logger.LogInfo((object)("[LogbookFit] " + mensaje));
			}
		}
	}
	[BepInPlugin("mt2_custom_clan_ui_fixes.Plugin", "mt2_custom_clan_ui_fixes.Plugin", "0.2.1")]
	public class Plugin : BaseUnityPlugin
	{
		internal static ManualLogSource Logger = new ManualLogSource("mt2_custom_clan_ui_fixes.Plugin");

		public void Awake()
		{
			//IL_02ad: Unknown result type (might be due to invalid IL or missing references)
			Logger = ((BaseUnityPlugin)this).Logger;
			ConfigEntry<bool> val = ((BaseUnityPlugin)this).Config.Bind<bool>("LogbookFit", "Enabled", true, "Encaja los rombos de clan de la pagina de mejoras del logbook cuando no caben.");
			ConfigEntry<float> val2 = ((BaseUnityPlugin)this).Config.Bind<float>("LogbookFit", "MinScale", 0.45f, "Hasta donde se deja encoger un rombo. 1 = tamano original.");
			ConfigEntry<int> val3 = ((BaseUnityPlugin)this).Config.Bind<int>("LogbookFit", "MaxAutoColumns", 3, "Columnas como mucho. 2 = se deja la rejilla del juego y solo se escala; 3 aprovecha el ancho de la hoja.");
			ConfigEntry<float> val4 = ((BaseUnityPlugin)this).Config.Bind<float>("LogbookFit", "ColumnSpacing", 16f, "Separacion entre columnas cuando se pasa de dos. La del juego son 96 px.");
			ConfigEntry<float> val5 = ((BaseUnityPlugin)this).Config.Bind<float>("LogbookFit", "HeaderReserve", 130f, "Pixeles que se reservan arriba para el titulo de la hoja. La zona medida lo incluye, asi que sin esto los rombos se le suben encima cuando hay muchos clanes.");
			ConfigEntry<float> val6 = ((BaseUnityPlugin)this).Config.Bind<float>("LogbookFit", "ScaleMultiplier", 1f, "Multiplica el factor calculado, para dejar los rombos algo mas pequenos de lo que haria falta. 1 = lo justo para que quepan; 0,9 = un 10% mas pequenos.");
			ConfigEntry<float> val7 = ((BaseUnityPlugin)this).Config.Bind<float>("LogbookFit", "HeightBudget", 0f, "Alto util de la hoja en pixeles. 0 = detectarlo solo (medido: 1000).");
			ConfigEntry<float> val8 = ((BaseUnityPlugin)this).Config.Bind<float>("LogbookFit", "WidthBudget", 0f, "Ancho util de la hoja en pixeles. 0 = detectarlo solo (medido: 400). Subelo a 440 para rombos a tamano original.");
			ConfigEntry<int> val9 = ((BaseUnityPlugin)this).Config.Bind<int>("LogbookFit", "RetryFrames", 5, "Frames que se reintenta la colocacion tras abrir la pantalla. El juego crea los botones de la columna de tripulacion uno o mas frames despues.");
			ConfigEntry<bool> obj = ((BaseUnityPlugin)this).Config.Bind<bool>("LogbookFit", "Verbose", true, "Escribe en LogOutput.log lo que mide y lo que ajusta.");
			LogbookClanFit.Enabled = val.Value;
			LogbookClanFit.MinScale = val2.Value;
			LogbookClanFit.MaxAutoColumns = val3.Value;
			LogbookClanFit.ColumnSpacing = val4.Value;
			LogbookClanFit.HeaderReserve = val5.Value;
			LogbookClanFit.ScaleMultiplier = val6.Value;
			LogbookClanFit.HeightBudget = val7.Value;
			LogbookClanFit.WidthBudget = val8.Value;
			LogbookClanFit.RetryFrames = val9.Value;
			LogbookClanFit.Verbose = obj.Value;
			ConfigEntry<bool> val10 = ((BaseUnityPlugin)this).Config.Bind<bool>("ArtifactsPaging", "Enabled", true, "Pagina la pagina de artefactos del logbook cuando las columnas de clan no caben a lo ancho. Usa las flechas de paso de pagina del propio juego.");
			ConfigEntry<int> val11 = ((BaseUnityPlugin)this).Config.Bind<int>("ArtifactsPaging", "ColumnsPerPage", 0, "Columnas por pagina. 0 = las que quepan segun el ancho medido.");
			ConfigEntry<bool> val12 = ((BaseUnityPlugin)this).Config.Bind<bool>("ArtifactsPaging", "Balance", true, "Reparte las columnas en paginas igual de llenas (24 columnas -> 12 y 12) en vez de llenar la primera y dejar la ultima a medias (17 y 7).");
			ConfigEntry<float> val13 = ((BaseUnityPlugin)this).Config.Bind<float>("ArtifactsPaging", "WidthBudget", 0f, "Ancho util de la hoja en pixeles. 0 = detectarlo solo. Si el reparto se queda corto o largo, mira la linea 'zona:' del log y fija aqui el ancho bueno.");
			ConfigEntry<int> val14 = ((BaseUnityPlugin)this).Config.Bind<int>("ArtifactsPaging", "RetryFrames", 10, "Frames que se reintenta el reparto tras abrir la pantalla, mientras el juego termina de crear y colocar las columnas. Las medidas no se dan por buenas hasta que dos pasadas seguidas coinciden, asi que conviene que sobren.");
			ConfigEntry<bool> obj2 = ((BaseUnityPlugin)this).Config.Bind<bool>("ArtifactsPaging", "Verbose", true, "Escribe en LogOutput.log lo que mide y como reparte.");
			LogbookArtifactsPaging.Enabled = val10.Value;
			LogbookArtifactsPaging.ColumnsPerPage = val11.Value;
			LogbookArtifactsPaging.Balance = val12.Value;
			LogbookArtifactsPaging.WidthBudget = val13.Value;
			LogbookArtifactsPaging.RetryFrames = val14.Value;
			LogbookArtifactsPaging.Verbose = obj2.Value;
			new Harmony("mt2_custom_clan_ui_fixes.Plugin").PatchAll();
			Logger.LogInfo((object)"Plugin mt2_custom_clan_ui_fixes.Plugin is loaded!");
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "mt2_custom_clan_ui_fixes.Plugin";

		public const string PLUGIN_NAME = "mt2_custom_clan_ui_fixes.Plugin";

		public const string PLUGIN_VERSION = "0.2.1";
	}
}