Decompiled source of FactionScroll v1.0.0

FactionScroll.dll

Decompiled a day ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using Landfall.TABS;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace FactionRowScroll;

[HarmonyPatch(typeof(ExpandedLandfallFactionsGrid), "SpawnFaction")]
internal static class FactionGridPatch
{
	[HarmonyPostfix]
	internal static void Postfix(ExpandedLandfallFactionsGrid __instance)
	{
		try
		{
			FactionRowLayout.Register(__instance);
		}
		catch (Exception ex)
		{
			Plugin.Log.LogError((object)("[FactionRow] " + ex));
		}
	}
}
internal class FactionRowLayout : MonoBehaviour
{
	private sealed class Target
	{
		public ExpandedLandfallFactionsGrid grid;

		public RectTransform gridRect;

		public GridLayoutGroup layout;

		public Canvas canvas;

		public Vector2 originalCellSize;

		public Vector2 originalSpacing;

		public Constraint originalConstraint;

		public int originalConstraintCount;

		public readonly List<RectTransform> all = new List<RectTransform>();

		public readonly Dictionary<RectTransform, bool> isFaction = new Dictionary<RectTransform, bool>();

		public readonly List<RectTransform> factions = new List<RectTransform>();

		public int total;

		public int perPage = 1;

		public int pageCount = 1;

		public int page;

		public float areaWidth;

		public bool applied;

		public bool logged;

		public bool dumped;

		public bool failed;
	}

	private struct ArrowCandidate
	{
		public Sprite sprite;

		public bool horizontal;

		public bool pointsPositive;

		public bool namedArrow;
	}

	private struct ArrowPick
	{
		public Sprite sprite;

		public bool vertical;

		public bool positive;
	}

	internal static FactionRowLayout Instance;

	private const float MinArea = 50f;

	private readonly List<Target> _targets = new List<Target>();

	private float _nextScan;

	private GUIStyle _style;

	private Texture2D _white;

	private ArrowPick _arrowPrev;

	private ArrowPick _arrowNext;

	private bool _prevIsMirror;

	private bool _arrowsResolved;

	private int _scanned;

	private float _styleHeight = -1f;

	private Rect _prevRect;

	private Rect _nextRect;

	private Rect _labelRect;

	private Target _current;

	private static Font _guiFont;

	private static bool _guiFontTried;

	private void Awake()
	{
		Instance = this;
	}

	internal static void Register(ExpandedLandfallFactionsGrid grid)
	{
		if (!((Object)(object)grid == (Object)null) && !((Object)(object)Instance == (Object)null) && Plugin.CfgEnabled.Value)
		{
			Instance.Track(grid);
		}
	}

	private void Track(ExpandedLandfallFactionsGrid grid)
	{
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_008f: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: 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_00b1: Unknown result type (might be due to invalid IL or missing references)
		for (int i = 0; i < _targets.Count; i++)
		{
			Target target = _targets[i];
			if (target != null && (Object)(object)target.grid == (Object)(object)grid)
			{
				return;
			}
		}
		Target target2 = new Target();
		target2.grid = grid;
		ref RectTransform gridRect = ref target2.gridRect;
		Transform transform = ((Component)grid).transform;
		gridRect = (RectTransform)(object)((transform is RectTransform) ? transform : null);
		target2.layout = ((Component)grid).GetComponent<GridLayoutGroup>() ?? ((Component)grid).GetComponentInParent<GridLayoutGroup>();
		target2.canvas = ((Component)grid).GetComponentInParent<Canvas>();
		if ((Object)(object)target2.layout != (Object)null)
		{
			target2.originalCellSize = target2.layout.cellSize;
			target2.originalSpacing = target2.layout.spacing;
			target2.originalConstraint = target2.layout.constraint;
			target2.originalConstraintCount = target2.layout.constraintCount;
		}
		_targets.Add(target2);
		Dump(target2, "first sight");
	}

	private void Update()
	{
		if (!Plugin.CfgEnabled.Value)
		{
			return;
		}
		if (Time.unscaledTime >= _nextScan)
		{
			_nextScan = Time.unscaledTime + 2f;
			ScanForGrids();
		}
		for (int num = _targets.Count - 1; num >= 0; num--)
		{
			Target target = _targets[num];
			if (target == null || (Object)(object)target.grid == (Object)null || (Object)(object)target.gridRect == (Object)null)
			{
				_targets.RemoveAt(num);
			}
			else if (!target.failed && ((Component)target.grid).gameObject.activeInHierarchy)
			{
				RefreshButtons(target);
				if (target.total > 0 && HasUsableArea(target))
				{
					Apply(target);
				}
			}
		}
		HandlePagerInput();
	}

	private void ScanForGrids()
	{
		//IL_001d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			ExpandedLandfallFactionsGrid[] array = Resources.FindObjectsOfTypeAll<ExpandedLandfallFactionsGrid>();
			foreach (ExpandedLandfallFactionsGrid val in array)
			{
				if (!((Object)(object)val == (Object)null))
				{
					Scene scene = ((Component)val).gameObject.scene;
					if (((Scene)(ref scene)).IsValid())
					{
						Track(val);
					}
				}
			}
		}
		catch (Exception ex)
		{
			Plugin.LogWarn("[FactionRow] grid scan failed: " + ex.Message);
		}
	}

	private static bool HasUsableArea(Target target)
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Unknown result type (might be due to invalid IL or missing references)
		RectTransform val = FindVisibleArea(target);
		if ((Object)(object)val != (Object)null)
		{
			Rect rect = val.rect;
			return ((Rect)(ref rect)).width >= 50f;
		}
		return false;
	}

	private void RefreshButtons(Target target)
	{
		RectTransform gridRect = target.gridRect;
		target.all.Clear();
		for (int i = 0; i < ((Transform)gridRect).childCount; i++)
		{
			Transform child = ((Transform)gridRect).GetChild(i);
			if ((Object)(object)child == (Object)null || (Object)(object)((Component)child).GetComponent<ExpandedFactionButton>() == (Object)null)
			{
				continue;
			}
			RectTransform val = (RectTransform)(object)((child is RectTransform) ? child : null);
			if (!((Object)(object)val == (Object)null))
			{
				target.all.Add(val);
				if (!target.isFaction.ContainsKey(val))
				{
					target.isFaction[val] = ((Component)val).gameObject.activeSelf;
				}
			}
		}
		target.factions.Clear();
		for (int j = 0; j < target.all.Count; j++)
		{
			RectTransform val2 = target.all[j];
			if (target.isFaction.TryGetValue(val2, out var value) && value)
			{
				target.factions.Add(val2);
			}
		}
		target.total = target.factions.Count;
	}

	private void Apply(Target target)
	{
		//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_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		//IL_010e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: Unknown result type (might be due to invalid IL or missing references)
		//IL_02e6: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0313: Unknown result type (might be due to invalid IL or missing references)
		//IL_0322: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)target.layout == (Object)null)
		{
			Plugin.LogWarn("[FactionRow] no GridLayoutGroup around '" + PathOf(((Component)target.grid).transform) + "'");
			Dump(target, "no GridLayoutGroup");
			target.failed = true;
			return;
		}
		Plugin.Mode value = Plugin.CfgMode.Value;
		if (value == Plugin.Mode.DumpOnly)
		{
			Dump(target, "dump only");
			target.applied = true;
			return;
		}
		RectTransform val = FindVisibleArea(target);
		if ((Object)(object)val == (Object)null)
		{
			return;
		}
		Rect rect = val.rect;
		float num = ((Rect)(ref rect)).width;
		float num2 = VisibleWidthUnits(target);
		if (num2 >= 50f)
		{
			num = Mathf.Min(num, num2);
		}
		if (num < 50f)
		{
			return;
		}
		GridLayoutGroup layout = target.layout;
		Vector2 val2 = target.originalCellSize;
		Vector2 originalSpacing = target.originalSpacing;
		if (val2.x < 1f)
		{
			val2 = layout.cellSize;
		}
		RectOffset padding = ((LayoutGroup)layout).padding;
		float num3 = num - (float)padding.left - (float)padding.right;
		int num4 = Mathf.Max(1, Mathf.FloorToInt((num3 + originalSpacing.x) / Mathf.Max(1f, val2.x + originalSpacing.x)));
		int num5 = Mathf.Max(1, Mathf.CeilToInt((float)target.total / (float)num4));
		target.perPage = num4;
		target.pageCount = num5;
		target.areaWidth = num;
		target.page = Mathf.Clamp(target.page, 0, num5 - 1);
		if (value == Plugin.Mode.FitAll)
		{
			ApplyFitAll(target, val, num4);
			target.applied = true;
			return;
		}
		if (target.total <= num4)
		{
			if (!target.logged)
			{
				target.logged = true;
				Plugin.LogInfo("[FactionRow] " + target.total + " factions fit in one row (" + num4 + " per row) - nothing to page");
			}
			target.applied = true;
			return;
		}
		layout.constraint = (Constraint)2;
		layout.constraintCount = 1;
		layout.startAxis = (Axis)0;
		layout.startCorner = (Corner)0;
		((LayoutGroup)layout).childAlignment = (TextAnchor)0;
		int num6 = target.page * num4;
		int num7 = Mathf.Min(target.factions.Count, num6 + num4);
		for (int i = 0; i < target.factions.Count; i++)
		{
			RectTransform val3 = target.factions[i];
			if (!((Object)(object)val3 == (Object)null))
			{
				bool flag = i >= num6 && i < num7;
				if (((Component)val3).gameObject.activeSelf != flag)
				{
					((Component)val3).gameObject.SetActive(flag);
				}
			}
		}
		target.applied = true;
		LayoutRebuilder.MarkLayoutForRebuild(target.gridRect);
		if (!target.logged)
		{
			target.logged = true;
			Plugin.LogInfo($"[FactionRow] paged one row: {target.total} factions, {num4} per page -> {num5} page(s), row width {(float)num4 * val2.x + (float)Mathf.Max(0, num4 - 1) * originalSpacing.x:F0} of area {num:F0} (cell {val2.x:F0}, spacing {originalSpacing.x:F0})");
		}
	}

	private void ApplyFitAll(Target target, RectTransform area, int columns)
	{
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0031: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0040: 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)
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0073: 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_00b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d7: 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_0106: 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_0122: Unknown result type (might be due to invalid IL or missing references)
		//IL_012d: Unknown result type (might be due to invalid IL or missing references)
		//IL_013c: Unknown result type (might be due to invalid IL or missing references)
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_01e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f6: Unknown result type (might be due to invalid IL or missing references)
		GridLayoutGroup layout = target.layout;
		int num = Mathf.CeilToInt((float)target.total / (float)Mathf.Max(1, columns));
		RectOffset padding = ((LayoutGroup)layout).padding;
		Vector2 originalCellSize = target.originalCellSize;
		Vector2 originalSpacing = target.originalSpacing;
		if (originalCellSize.x < 1f || originalCellSize.y < 1f)
		{
			return;
		}
		Rect rect = area.rect;
		float num2 = ((Rect)(ref rect)).width - (float)padding.left - (float)padding.right;
		rect = area.rect;
		float num3 = ((Rect)(ref rect)).height - (float)padding.top - (float)padding.bottom;
		float num4 = (num2 - (float)Mathf.Max(0, columns - 1) * originalSpacing.x) / (float)columns;
		float num5 = (num3 - (float)Mathf.Max(0, num - 1) * originalSpacing.y) / (float)num;
		float num6 = Mathf.Max(0.2f, Mathf.Min(new float[3]
		{
			1f,
			num4 / originalCellSize.x,
			num5 / originalCellSize.y
		}));
		layout.constraint = (Constraint)1;
		layout.constraintCount = columns;
		layout.cellSize = new Vector2(Mathf.Floor(originalCellSize.x * num6), Mathf.Floor(originalCellSize.y * num6));
		layout.spacing = new Vector2(Mathf.Floor(originalSpacing.x * num6), Mathf.Floor(originalSpacing.y * num6));
		for (int i = 0; i < target.factions.Count; i++)
		{
			if ((Object)(object)target.factions[i] != (Object)null && !((Component)target.factions[i]).gameObject.activeSelf)
			{
				((Component)target.factions[i]).gameObject.SetActive(true);
			}
		}
		Plugin.LogInfo($"[FactionRow] FitAll: {target.total} factions = {num} rows x {columns} cols -> cells {layout.cellSize.x:F0}x{layout.cellSize.y:F0} (scale {num6:F2})");
	}

	private static RectTransform FindVisibleArea(Target target)
	{
		//IL_003d: 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)
		Transform parent = ((Transform)target.gridRect).parent;
		int num = 0;
		while ((Object)(object)parent != (Object)null && num++ < 8)
		{
			if ((Object)(object)((Component)parent).GetComponent<RectMask2D>() != (Object)null || (Object)(object)((Component)parent).GetComponent<Mask>() != (Object)null)
			{
				RectTransform val = (RectTransform)(object)((parent is RectTransform) ? parent : null);
				if ((Object)(object)val != (Object)null)
				{
					Rect rect = val.rect;
					if (((Rect)(ref rect)).width >= 50f)
					{
						return val;
					}
				}
			}
			parent = parent.parent;
		}
		Transform parent2 = ((Transform)target.gridRect).parent;
		return (RectTransform)(object)((parent2 is RectTransform) ? parent2 : null);
	}

	private static float VisibleWidthUnits(Target target)
	{
		//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_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: 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_0047: 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_0087: 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)
		Vector3[] array = (Vector3[])(object)new Vector3[4];
		target.gridRect.GetWorldCorners(array);
		Camera obj = CameraOf(target);
		Vector2 val = RectTransformUtility.WorldToScreenPoint(obj, array[0]);
		Vector2 val2 = RectTransformUtility.WorldToScreenPoint(obj, array[2]);
		float num = Mathf.Abs(val2.x - val.x);
		float num2 = Mathf.Min(val.x, val2.x);
		if (num < 1f)
		{
			return 0f;
		}
		float num3 = Mathf.Max(0f, (float)Screen.width - num2 - 8f);
		Rect rect = target.gridRect.rect;
		float num4 = ((Rect)(ref rect)).width / num;
		return num3 * num4;
	}

	private void HandlePagerInput()
	{
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_002d: 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_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_007d: 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)
		if (Plugin.CfgMode.Value != Plugin.Mode.Pages)
		{
			return;
		}
		Target target = CurrentTarget();
		if (target == null || target.pageCount <= 1)
		{
			return;
		}
		ComputePagerRects(target);
		Vector2 val = GuiMouse();
		if (Input.GetMouseButtonDown(0))
		{
			if (((Rect)(ref _prevRect)).Contains(val))
			{
				ChangePage(target, -1);
			}
			else if (((Rect)(ref _nextRect)).Contains(val))
			{
				ChangePage(target, 1);
			}
		}
		float y = Input.mouseScrollDelta.y;
		if (Mathf.Abs(y) > 0.01f && RectContains(target, val))
		{
			ChangePage(target, (!(y > 0f)) ? 1 : (-1));
		}
	}

	private void ChangePage(Target target, int delta)
	{
		int num = Mathf.Clamp(target.page + delta, 0, Mathf.Max(0, target.pageCount - 1));
		if (num != target.page)
		{
			target.page = num;
			Apply(target);
			Plugin.LogInfo("[FactionRow] page " + (num + 1) + "/" + target.pageCount);
		}
	}

	private void ComputePagerRects(Target target)
	{
		//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_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: 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_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_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: 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_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: 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)
		//IL_012c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0131: Unknown result type (might be due to invalid IL or missing references)
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0150: Unknown result type (might be due to invalid IL or missing references)
		//IL_016a: Unknown result type (might be due to invalid IL or missing references)
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		Vector3[] array = (Vector3[])(object)new Vector3[4];
		target.gridRect.GetWorldCorners(array);
		Camera obj = CameraOf(target);
		Vector2 val = RectTransformUtility.WorldToScreenPoint(obj, array[0]);
		Vector2 val2 = RectTransformUtility.WorldToScreenPoint(obj, array[2]);
		float num = Mathf.Abs(val2.x - val.x);
		Rect rect = target.gridRect.rect;
		float num2;
		if (!(((Rect)(ref rect)).width > 1f))
		{
			num2 = 1f;
		}
		else
		{
			rect = target.gridRect.rect;
			num2 = num / ((Rect)(ref rect)).width;
		}
		float num3 = num2;
		float num4 = Mathf.Clamp(26f * num3, 18f, 40f);
		float num5 = num4;
		float num6 = Mathf.Clamp(60f * num3, 36f, 90f);
		float num7 = Mathf.Max(val.x, val2.x);
		float num8 = Mathf.Max(val.y, val2.y);
		float num9 = (float)Screen.height - num8 - num4 - 2f + Plugin.CfgPagerOffset.Value * num3;
		num9 = Mathf.Clamp(num9, 0f, Mathf.Max(0f, (float)Screen.height - num4));
		_nextRect = new Rect(num7 - num5, num9, num5, num4);
		_labelRect = new Rect(((Rect)(ref _nextRect)).x - num6, num9, num6, num4);
		_prevRect = new Rect(((Rect)(ref _labelRect)).x - num5, num9, num5, num4);
	}

	private void OnGUI()
	{
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00be: 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_00d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00de: 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_00f2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_010c: Unknown result type (might be due to invalid IL or missing references)
		if (!Plugin.CfgEnabled.Value || Plugin.CfgMode.Value != Plugin.Mode.Pages)
		{
			return;
		}
		Target target = CurrentTarget();
		if (target == null || target.pageCount <= 1)
		{
			return;
		}
		try
		{
			EnsureGuiResources();
			GUI.depth = -10000;
			Vector2 val = GuiMouse();
			float radius = Mathf.Clamp(((Rect)(ref _prevRect)).height * 0.28f, 3f, 12f);
			Color val2 = default(Color);
			((Color)(ref val2))..ctor(0.5f, 0.5f, 0.5f, 0.22f);
			Color val3 = default(Color);
			((Color)(ref val3))..ctor(0.65f, 0.65f, 0.65f, 0.38f);
			DrawPill(_prevRect, ((Rect)(ref _prevRect)).Contains(val) ? val3 : val2, radius);
			DrawPill(_nextRect, ((Rect)(ref _nextRect)).Contains(val) ? val3 : val2, radius);
			DrawPill(_labelRect, val2, radius);
			ResolveArrows();
			DrawArrow(_prevRect, previous: true);
			DrawArrow(_nextRect, previous: false);
			DrawLabel(_labelRect, target.page + 1 + " / " + target.pageCount);
		}
		catch (Exception ex)
		{
			Plugin.LogWarn("[FactionRow] pager draw failed: " + ex.Message);
		}
	}

	private void DrawPill(Rect rect, Color color, float radius)
	{
		//IL_0000: 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)
		GUI.DrawTexture(rect, (Texture)(object)_white, (ScaleMode)0, true, 0f, color, 0f, radius);
	}

	private void DrawArrow(Rect rect, bool previous)
	{
		//IL_000f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
		Plugin.ArrowStyle value = Plugin.CfgArrowStyle.Value;
		if (value == Plugin.ArrowStyle.Text)
		{
			DrawLabel(rect, previous ? "<" : ">");
			return;
		}
		ArrowPick pick = (previous ? _arrowPrev : _arrowNext);
		if (value != Plugin.ArrowStyle.Chevron && (Object)(object)pick.sprite != (Object)null && (Object)(object)pick.sprite.texture != (Object)null)
		{
			DrawSpriteArrow(rect, pick, previous && _prevIsMirror);
			return;
		}
		if (value == Plugin.ArrowStyle.Sprite)
		{
			ArrowPick pick2 = (previous ? _arrowNext : _arrowPrev);
			if ((Object)(object)pick2.sprite != (Object)null && (Object)(object)pick2.sprite.texture != (Object)null)
			{
				DrawSpriteArrow(rect, pick2, turnAround: true);
				return;
			}
		}
		DrawChevron(rect, previous);
	}

	private void DrawSpriteArrow(Rect rect, ArrowPick pick, bool turnAround)
	{
		//IL_004e: 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_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cd: 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)
		//IL_00b9: Unknown result type (might be due to invalid IL or missing references)
		float num = ((!pick.vertical) ? 0f : (pick.positive ? 90f : (-90f)));
		if (turnAround)
		{
			num += 180f;
		}
		num += Plugin.CfgArrowRotate.Value;
		Texture2D texture = pick.sprite.texture;
		Rect val = UvBounds(pick.sprite);
		float num2 = Mathf.Min(((Rect)(ref rect)).width, ((Rect)(ref rect)).height) * 0.5f;
		Rect val2 = default(Rect);
		((Rect)(ref val2))..ctor(((Rect)(ref rect)).center.x - num2 * 0.5f, ((Rect)(ref rect)).center.y - num2 * 0.5f, num2, num2);
		Matrix4x4 matrix = GUI.matrix;
		Color color = GUI.color;
		if (!Mathf.Approximately(num, 0f))
		{
			GUIUtility.RotateAroundPivot(num, ((Rect)(ref rect)).center);
		}
		GUI.color = Color.white;
		GUI.DrawTextureWithTexCoords(val2, (Texture)(object)texture, val, true);
		GUI.color = color;
		GUI.matrix = matrix;
	}

	private void DrawChevron(Rect rect, bool previous)
	{
		//IL_003e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0043: Unknown result type (might be due to invalid IL or missing references)
		//IL_0046: Unknown result type (might be due to invalid IL or missing references)
		//IL_0056: Unknown result type (might be due to invalid IL or missing references)
		//IL_0063: Unknown result type (might be due to invalid IL or missing references)
		//IL_0073: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: 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_00ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: 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_00b9: Unknown result type (might be due to invalid IL or missing references)
		float num = Mathf.Min(((Rect)(ref rect)).width, ((Rect)(ref rect)).height) * 0.5f;
		float thickness = Mathf.Max(1.5f, num * 0.22f);
		float num2 = (previous ? (-1f) : 1f);
		Vector2 center = ((Rect)(ref rect)).center;
		Vector2 to = default(Vector2);
		((Vector2)(ref to))..ctor(center.x + num2 * num * 0.42f, center.y);
		Vector2 val = default(Vector2);
		((Vector2)(ref val))..ctor(center.x - num2 * num * 0.38f, center.y - num * 0.52f);
		Vector2 val2 = default(Vector2);
		((Vector2)(ref val2))..ctor(center.x - num2 * num * 0.38f, center.y + num * 0.52f);
		DrawBar(val, to, thickness);
		DrawBar(val2, to, thickness);
	}

	private void DrawBar(Vector2 from, Vector2 to, float thickness)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0001: Unknown result type (might be due to invalid IL or missing references)
		//IL_0002: Unknown result type (might be due to invalid IL or missing references)
		//IL_0007: 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_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0025: Unknown result type (might be due to invalid IL or missing references)
		//IL_002a: 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_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Unknown result type (might be due to invalid IL or missing references)
		//IL_004e: 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_0054: 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_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_009e: Unknown result type (might be due to invalid IL or missing references)
		Vector2 val = to - from;
		float magnitude = ((Vector2)(ref val)).magnitude;
		if (!(magnitude < 0.5f))
		{
			Vector2 val2 = (from + to) * 0.5f;
			Rect val3 = new Rect(val2.x - magnitude * 0.5f, val2.y - thickness * 0.5f, magnitude, thickness);
			Matrix4x4 matrix = GUI.matrix;
			GUI.color = Color.white;
			GUIUtility.RotateAroundPivot(Mathf.Atan2(val.y, val.x) * 57.29578f, val2);
			GUI.DrawTexture(val3, (Texture)(object)_white, (ScaleMode)0, true, 0f, Color.white, 0f, thickness * 0.5f);
			GUI.matrix = matrix;
		}
	}

	private void ResolveArrows()
	{
		if (_arrowsResolved)
		{
			return;
		}
		_arrowsResolved = true;
		if (Plugin.CfgArrowStyle.Value == Plugin.ArrowStyle.Text)
		{
			return;
		}
		List<ArrowCandidate> list = new List<ArrowCandidate>();
		Sprite prev = null;
		Sprite next = null;
		if (Plugin.CfgArrowStyle.Value != Plugin.ArrowStyle.Chevron)
		{
			string text = (Plugin.CfgArrowSprite.Value ?? string.Empty).Trim();
			if (text.Length > 0)
			{
				Sprite val = FindSpriteByName(text);
				if ((Object)(object)val != (Object)null)
				{
					_arrowNext.sprite = val;
					_arrowNext.vertical = false;
					_arrowNext.positive = true;
					_arrowPrev = _arrowNext;
					_prevIsMirror = true;
					Plugin.LogInfo("[FactionRow] pager arrows: forced sprite " + SpriteName(val) + ", extra rotation " + Plugin.CfgArrowRotate.Value);
					return;
				}
				Plugin.LogWarn("[FactionRow] configured arrow sprite '" + text + "' was not found, falling back");
			}
			try
			{
				ExpandedCustomFactionGrid[] array = Resources.FindObjectsOfTypeAll<ExpandedCustomFactionGrid>();
				FieldInfo fieldInfo = AccessTools.Field(typeof(ExpandedCustomFactionGrid), "m_pageButtons");
				for (int i = 0; i < array.Length; i++)
				{
					if (!(fieldInfo != null))
					{
						break;
					}
					if (!((Object)(object)array[i] == (Object)null))
					{
						object? value = fieldInfo.GetValue(array[i]);
						GameObject val2 = (GameObject)((value is GameObject) ? value : null);
						if (!((Object)(object)val2 == (Object)null))
						{
							ScanImages(val2.transform, list, ref prev, ref next);
						}
					}
				}
			}
			catch (Exception ex)
			{
				Plugin.LogWarn("[FactionRow] faction pager sprite scan failed: " + ex.Message);
			}
			if (list.Count == 0)
			{
				try
				{
					Dropdown[] array2 = Resources.FindObjectsOfTypeAll<Dropdown>();
					for (int j = 0; j < array2.Length; j++)
					{
						if (!((Object)(object)array2[j] == (Object)null) && !((Object)(object)array2[j].template == (Object)null))
						{
							ScanImages((Transform)(object)array2[j].template, list, ref prev, ref next);
							if (list.Count > 0)
							{
								break;
							}
						}
					}
				}
				catch (Exception ex2)
				{
					Plugin.LogWarn("[FactionRow] dropdown arrow scan failed: " + ex2.Message);
				}
			}
			if (list.Count == 0)
			{
				try
				{
					Sprite[] array3 = Resources.FindObjectsOfTypeAll<Sprite>();
					foreach (Sprite val3 in array3)
					{
						if (!((Object)(object)val3 == (Object)null))
						{
							string text2 = (((Object)val3).name ?? string.Empty).ToLowerInvariant();
							if (text2.IndexOf("arrow", StringComparison.Ordinal) >= 0 || text2.IndexOf("chevron", StringComparison.Ordinal) >= 0 || text2.IndexOf("triangle", StringComparison.Ordinal) >= 0)
							{
								AddCandidate(list, val3, text2, ref prev, ref next);
							}
						}
					}
				}
				catch (Exception ex3)
				{
					Plugin.LogWarn("[FactionRow] sprite name scan failed: " + ex3.Message);
				}
			}
		}
		_arrowNext = Pick(list, next: true);
		ArrowPick arrowPrev = Pick(list, next: false);
		if ((Object)(object)arrowPrev.sprite != (Object)null && (Object)(object)arrowPrev.sprite != (Object)(object)_arrowNext.sprite)
		{
			_arrowPrev = arrowPrev;
			_prevIsMirror = false;
		}
		else if ((Object)(object)_arrowNext.sprite != (Object)null)
		{
			_arrowPrev = _arrowNext;
			_prevIsMirror = true;
		}
		else
		{
			_arrowPrev = arrowPrev;
			_prevIsMirror = false;
		}
		Plugin.LogInfo("[FactionRow] pager arrows: style=" + Plugin.CfgArrowStyle.Value.ToString() + " prev=" + PickName(_arrowPrev) + " next=" + PickName(_arrowNext) + " (" + list.Count + " arrow sprite(s) out of " + _scanned + " scanned)");
		if (list.Count > 0 && Plugin.CfgVerbose.Value)
		{
			StringBuilder stringBuilder = new StringBuilder("[FactionRow] arrow candidates:");
			for (int l = 0; l < list.Count; l++)
			{
				ArrowCandidate arrowCandidate = list[l];
				stringBuilder.Append(' ').Append(SpriteName(arrowCandidate.sprite));
				stringBuilder.Append(arrowCandidate.horizontal ? (arrowCandidate.pointsPositive ? "/right" : "/left") : (arrowCandidate.pointsPositive ? "/up" : "/down"));
			}
			stringBuilder.Append("  (set PagerArrowSprite to one of these names to force it)");
			Plugin.LogInfo(stringBuilder.ToString());
		}
	}

	private static Sprite FindSpriteByName(string wanted)
	{
		try
		{
			string text = wanted.ToLowerInvariant();
			Sprite val = null;
			Sprite[] array = Resources.FindObjectsOfTypeAll<Sprite>();
			foreach (Sprite val2 in array)
			{
				if (!((Object)(object)val2 == (Object)null) && !((Object)(object)val2.texture == (Object)null))
				{
					string text2 = (((Object)val2).name ?? string.Empty).ToLowerInvariant();
					if (text2 == text)
					{
						return val2;
					}
					if ((Object)(object)val == (Object)null && text2.Contains(text))
					{
						val = val2;
					}
				}
			}
			return val;
		}
		catch (Exception ex)
		{
			Plugin.LogWarn("[FactionRow] sprite lookup failed: " + ex.Message);
			return null;
		}
	}

	private static ArrowPick Pick(List<ArrowCandidate> candidates, bool next)
	{
		ArrowPick result = default(ArrowPick);
		int num = -1;
		for (int i = 0; i < candidates.Count; i++)
		{
			ArrowCandidate arrowCandidate = candidates[i];
			int num2 = ((arrowCandidate.horizontal && arrowCandidate.pointsPositive == next) ? 8 : (arrowCandidate.horizontal ? 6 : ((arrowCandidate.pointsPositive != next) ? 1 : 3)));
			if (arrowCandidate.namedArrow)
			{
				num2++;
			}
			if (num2 > num)
			{
				num = num2;
				result.sprite = arrowCandidate.sprite;
				result.vertical = !arrowCandidate.horizontal;
				result.positive = arrowCandidate.pointsPositive;
			}
		}
		return result;
	}

	private static string PickName(ArrowPick pick)
	{
		if ((Object)(object)pick.sprite == (Object)null)
		{
			return "<none>";
		}
		return SpriteName(pick.sprite) + ((!pick.vertical) ? (pick.positive ? "/right" : "/left") : (pick.positive ? "/up" : "/down"));
	}

	private void ScanImages(Transform root, List<ArrowCandidate> candidates, ref Sprite prev, ref Sprite next)
	{
		Image[] componentsInChildren = ((Component)root).GetComponentsInChildren<Image>(true);
		foreach (Image val in componentsInChildren)
		{
			if (!((Object)(object)val == (Object)null) && !((Object)(object)val.sprite == (Object)null))
			{
				string text = ((Object)((Component)val).gameObject).name;
				if ((Object)(object)((Component)val).transform.parent != (Object)null)
				{
					text = text + "/" + ((Object)((Component)val).transform.parent).name;
				}
				AddCandidate(candidates, val.sprite, text.ToLowerInvariant(), ref prev, ref next);
			}
		}
	}

	private void AddCandidate(List<ArrowCandidate> candidates, Sprite sprite, string name, ref Sprite prev, ref Sprite next)
	{
		_scanned++;
		if (AnalyseArrow(sprite, out var horizontal, out var pointsPositive))
		{
			ArrowCandidate item = new ArrowCandidate
			{
				sprite = sprite,
				horizontal = horizontal,
				pointsPositive = pointsPositive,
				namedArrow = (name.Contains("arrow") || name.Contains("chevron") || name.Contains("triangle"))
			};
			if (!Contains(candidates, sprite))
			{
				candidates.Add(item);
			}
		}
	}

	private static bool Contains(List<ArrowCandidate> candidates, Sprite sprite)
	{
		for (int i = 0; i < candidates.Count; i++)
		{
			if ((Object)(object)candidates[i].sprite == (Object)(object)sprite)
			{
				return true;
			}
		}
		return false;
	}

	private static bool AnalyseArrow(Sprite sprite, out bool horizontal, out bool pointsPositive)
	{
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: 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)
		horizontal = true;
		pointsPositive = true;
		if ((Object)(object)sprite == (Object)null || (Object)(object)sprite.texture == (Object)null)
		{
			return false;
		}
		if (sprite.packed && (int)sprite.packingRotation != 0)
		{
			return false;
		}
		Rect textureRect = sprite.textureRect;
		if (((Rect)(ref textureRect)).width < 4f || ((Rect)(ref textureRect)).height < 4f)
		{
			return false;
		}
		if (((Rect)(ref textureRect)).width > 160f || ((Rect)(ref textureRect)).height > 160f)
		{
			return false;
		}
		Color[] array = SampleSprite(sprite, 24);
		if (array == null)
		{
			return true;
		}
		int num = 0;
		float num2 = 0f;
		float num3 = 0f;
		for (int i = 0; i < 24; i++)
		{
			for (int j = 0; j < 24; j++)
			{
				if (!(array[i * 24 + j].a <= 0.5f))
				{
					num++;
					num2 += ((float)j + 0.5f) / 24f * 2f - 1f;
					num3 += ((float)i + 0.5f) / 24f * 2f - 1f;
				}
			}
		}
		if (num < 24)
		{
			return false;
		}
		if ((float)num / 576f > 0.7f)
		{
			return false;
		}
		int num4 = 0;
		int[] array2 = new int[4] { 0, 23, 552, 575 };
		for (int k = 0; k < array2.Length; k++)
		{
			if (array[array2[k]].a <= 0.5f)
			{
				num4++;
			}
		}
		if (num4 == 0)
		{
			return false;
		}
		int num5 = 0;
		int num6 = 0;
		for (int l = 0; l < 24; l++)
		{
			int num7 = 0;
			bool flag = false;
			for (int m = 0; m < 24; m++)
			{
				bool num8 = array[l * 24 + m].a > 0.5f;
				if (num8 && !flag)
				{
					num7++;
				}
				flag = num8;
			}
			if (num7 > 0)
			{
				num5++;
				num6 += num7;
			}
		}
		if (num5 == 0)
		{
			return false;
		}
		if ((float)num6 / (float)num5 > 2.5f)
		{
			return false;
		}
		bool[] array3 = new bool[576];
		int[] array4 = new int[576];
		int num9 = 0;
		for (int n = 0; n < 576; n++)
		{
			if (array3[n] || array[n].a <= 0.5f)
			{
				continue;
			}
			num9++;
			if (num9 > 2)
			{
				return false;
			}
			int num10 = 0;
			array4[num10++] = n;
			array3[n] = true;
			while (num10 > 0)
			{
				int num11 = array4[--num10];
				int num12 = num11 % 24;
				int num13 = num11 / 24;
				for (int num14 = -1; num14 <= 1; num14++)
				{
					for (int num15 = -1; num15 <= 1; num15++)
					{
						int num16 = num12 + num15;
						int num17 = num13 + num14;
						if (num16 >= 0 && num17 >= 0 && num16 < 24 && num17 < 24)
						{
							int num18 = num17 * 24 + num16;
							if (!array3[num18] && !(array[num18].a <= 0.5f))
							{
								array3[num18] = true;
								array4[num10++] = num18;
							}
						}
					}
				}
			}
		}
		if (num9 == 0)
		{
			return false;
		}
		float num19 = num2 / (float)num;
		float num20 = num3 / (float)num;
		horizontal = Mathf.Abs(num19) >= Mathf.Abs(num20);
		pointsPositive = (horizontal ? (num19 > 0f) : (num20 > 0f));
		return true;
	}

	private static Rect UvBounds(Sprite sprite)
	{
		//IL_00c5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: 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)
		Vector2[] uv = sprite.uv;
		if (uv != null && uv.Length >= 4)
		{
			float num = float.MaxValue;
			float num2 = float.MinValue;
			float num3 = float.MaxValue;
			float num4 = float.MinValue;
			for (int i = 0; i < uv.Length; i++)
			{
				num = Mathf.Min(num, uv[i].x);
				num2 = Mathf.Max(num2, uv[i].x);
				num3 = Mathf.Min(num3, uv[i].y);
				num4 = Mathf.Max(num4, uv[i].y);
			}
			if (num2 - num > 0.0001f && num4 - num3 > 0.0001f)
			{
				return Rect.MinMaxRect(num, num3, num2, num4);
			}
		}
		Texture2D texture = sprite.texture;
		Rect textureRect = sprite.textureRect;
		return new Rect(((Rect)(ref textureRect)).xMin / (float)((Texture)texture).width, ((Rect)(ref textureRect)).yMin / (float)((Texture)texture).height, ((Rect)(ref textureRect)).width / (float)((Texture)texture).width, ((Rect)(ref textureRect)).height / (float)((Texture)texture).height);
	}

	private static Color[] SampleSprite(Sprite sprite, int size)
	{
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Unknown result type (might be due to invalid IL or missing references)
		//IL_012f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0131: Unknown result type (might be due to invalid IL or missing references)
		//IL_0142: Unknown result type (might be due to invalid IL or missing references)
		//IL_0147: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Unknown result type (might be due to invalid IL or missing references)
		//IL_0162: Unknown result type (might be due to invalid IL or missing references)
		//IL_0168: Unknown result type (might be due to invalid IL or missing references)
		//IL_0175: Expected O, but got Unknown
		//IL_00a3: 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)
		try
		{
			Texture2D texture = sprite.texture;
			Rect textureRect = sprite.textureRect;
			if (((Texture)texture).isReadable)
			{
				Color[] array = (Color[])(object)new Color[size * size];
				for (int i = 0; i < size; i++)
				{
					for (int j = 0; j < size; j++)
					{
						int num = Mathf.Clamp(Mathf.FloorToInt(((Rect)(ref textureRect)).xMin + ((float)j + 0.5f) / (float)size * ((Rect)(ref textureRect)).width), 0, ((Texture)texture).width - 1);
						int num2 = Mathf.Clamp(Mathf.FloorToInt(((Rect)(ref textureRect)).yMin + ((float)i + 0.5f) / (float)size * ((Rect)(ref textureRect)).height), 0, ((Texture)texture).height - 1);
						array[i * size + j] = texture.GetPixel(num, num2);
					}
				}
				return array;
			}
			RenderTexture temporary = RenderTexture.GetTemporary(size, size, 0, (RenderTextureFormat)0, (RenderTextureReadWrite)2);
			RenderTexture active = RenderTexture.active;
			try
			{
				Vector2 val = default(Vector2);
				((Vector2)(ref val))..ctor(((Rect)(ref textureRect)).width / (float)((Texture)texture).width, ((Rect)(ref textureRect)).height / (float)((Texture)texture).height);
				Vector2 val2 = default(Vector2);
				((Vector2)(ref val2))..ctor(((Rect)(ref textureRect)).xMin / (float)((Texture)texture).width, ((Rect)(ref textureRect)).yMin / (float)((Texture)texture).height);
				Graphics.Blit((Texture)(object)texture, temporary, val, val2);
				RenderTexture.active = temporary;
				Texture2D val3 = new Texture2D(size, size, (TextureFormat)4, false);
				val3.ReadPixels(new Rect(0f, 0f, (float)size, (float)size), 0, 0);
				val3.Apply();
				Color[] pixels = val3.GetPixels();
				Object.Destroy((Object)val3);
				return pixels;
			}
			finally
			{
				RenderTexture.active = active;
				RenderTexture.ReleaseTemporary(temporary);
			}
		}
		catch (Exception ex)
		{
			Plugin.LogWarn("[FactionRow] cannot inspect sprite '" + SpriteName(sprite) + "': " + ex.Message);
			return null;
		}
	}

	private static string SpriteName(Sprite sprite)
	{
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: 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)
		if ((Object)(object)sprite == (Object)null)
		{
			return "<none>";
		}
		string[] obj = new string[6]
		{
			((Object)sprite).name,
			"(",
			null,
			null,
			null,
			null
		};
		Rect rect = sprite.rect;
		obj[2] = Mathf.RoundToInt(((Rect)(ref rect)).width).ToString();
		obj[3] = "x";
		rect = sprite.rect;
		obj[4] = Mathf.RoundToInt(((Rect)(ref rect)).height).ToString();
		obj[5] = ")";
		return string.Concat(obj);
	}

	private void DrawLabel(Rect rect, string text)
	{
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0051: Unknown result type (might be due to invalid IL or missing references)
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		_style.normal.textColor = new Color(0f, 0f, 0f, 0.35f);
		GUI.Label(new Rect(((Rect)(ref rect)).x + 1f, ((Rect)(ref rect)).y + 1f, ((Rect)(ref rect)).width, ((Rect)(ref rect)).height), text, _style);
		_style.normal.textColor = Color.white;
		GUI.Label(rect, text, _style);
	}

	private void EnsureGuiResources()
	{
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: Expected O, but got Unknown
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_0080: Expected O, but got Unknown
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)_white == (Object)null)
		{
			_white = new Texture2D(4, 4, (TextureFormat)4, false);
			Color[] array = (Color[])(object)new Color[16];
			for (int i = 0; i < array.Length; i++)
			{
				array[i] = Color.white;
			}
			_white.SetPixels(array);
			_white.Apply();
			((Object)_white).hideFlags = (HideFlags)61;
		}
		if (_style == null)
		{
			_style = new GUIStyle(GUI.skin.label);
			_style.alignment = (TextAnchor)4;
			_style.richText = false;
			_style.clipping = (TextClipping)1;
			Font guiFont = GetGuiFont();
			if ((Object)(object)guiFont != (Object)null)
			{
				_style.font = guiFont;
			}
		}
		if (!Mathf.Approximately(_styleHeight, ((Rect)(ref _prevRect)).height))
		{
			_styleHeight = ((Rect)(ref _prevRect)).height;
			_style.fontSize = Mathf.RoundToInt(Mathf.Clamp(((Rect)(ref _prevRect)).height * 0.58f, 10f, 22f));
		}
	}

	private static Font GetGuiFont()
	{
		if (_guiFontTried)
		{
			return _guiFont;
		}
		_guiFontTried = true;
		try
		{
			_guiFont = Font.CreateDynamicFontFromOSFont(new string[5] { "Microsoft YaHei UI", "Microsoft YaHei", "SimHei", "Segoe UI", "Arial" }, 16);
		}
		catch
		{
			_guiFont = null;
		}
		return _guiFont;
	}

	private Target CurrentTarget()
	{
		Target target = null;
		float num = 0f;
		for (int i = 0; i < _targets.Count; i++)
		{
			Target target2 = _targets[i];
			if (target2 != null && !target2.failed && !((Object)(object)target2.grid == (Object)null) && !((Object)(object)target2.gridRect == (Object)null) && target2.total > 0 && IsVisibleOnScreen(target2, out var screenArea) && screenArea > num)
			{
				num = screenArea;
				target = target2;
			}
		}
		if (target != _current && target != null)
		{
			_current = target;
			Plugin.LogInfo("[FactionRow] active grid: " + PathOf(((Component)target.grid).transform) + " (visible area " + Mathf.RoundToInt(num) + " px)");
		}
		return target;
	}

	private bool IsVisibleOnScreen(Target target, out float screenArea)
	{
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a9: 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_00b1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bd: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00dc: 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_00fa: Unknown result type (might be due to invalid IL or missing references)
		//IL_0100: Unknown result type (might be due to invalid IL or missing references)
		//IL_0119: Unknown result type (might be due to invalid IL or missing references)
		//IL_011f: Unknown result type (might be due to invalid IL or missing references)
		screenArea = 0f;
		if (!((Component)target.grid).gameObject.activeInHierarchy)
		{
			return false;
		}
		Transform val = ((Component)target.grid).transform;
		int num = 0;
		while ((Object)(object)val != (Object)null && num++ < 10)
		{
			Canvas component = ((Component)val).GetComponent<Canvas>();
			if ((Object)(object)component != (Object)null && !((Behaviour)component).enabled)
			{
				return false;
			}
			CanvasGroup component2 = ((Component)val).GetComponent<CanvasGroup>();
			if ((Object)(object)component2 != (Object)null && component2.alpha <= 0.01f)
			{
				return false;
			}
			val = val.parent;
		}
		Vector3[] array = (Vector3[])(object)new Vector3[4];
		target.gridRect.GetWorldCorners(array);
		Camera obj = CameraOf(target);
		Vector2 val2 = RectTransformUtility.WorldToScreenPoint(obj, array[0]);
		Vector2 val3 = RectTransformUtility.WorldToScreenPoint(obj, array[2]);
		float num2 = Mathf.Max(0f, Mathf.Min(val2.x, val3.x));
		float num3 = Mathf.Min((float)Screen.width, Mathf.Max(val2.x, val3.x));
		float num4 = Mathf.Max(0f, Mathf.Min(val2.y, val3.y));
		float num5 = Mathf.Min((float)Screen.height, Mathf.Max(val2.y, val3.y));
		if (num3 - num2 < 30f || num5 - num4 < 10f)
		{
			return false;
		}
		screenArea = (num3 - num2) * (num5 - num4);
		return true;
	}

	private static Vector2 GuiMouse()
	{
		//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)
		//IL_000a: 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_0017: Unknown result type (might be due to invalid IL or missing references)
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		Vector2 val = Vector2.op_Implicit(Input.mousePosition);
		return new Vector2(val.x, (float)Screen.height - val.y);
	}

	private static Camera CameraOf(Target target)
	{
		//IL_0014: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)target.canvas != (Object)null && (int)target.canvas.renderMode != 0)
		{
			return target.canvas.worldCamera;
		}
		return null;
	}

	private static bool RectContains(Target target, Vector2 guiPoint)
	{
		//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_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0029: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: 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_003c: 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_0053: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: 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_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		Vector3[] array = (Vector3[])(object)new Vector3[4];
		target.gridRect.GetWorldCorners(array);
		Camera obj = CameraOf(target);
		Vector2 val = RectTransformUtility.WorldToScreenPoint(obj, array[0]);
		Vector2 val2 = RectTransformUtility.WorldToScreenPoint(obj, array[2]);
		Rect val3 = default(Rect);
		((Rect)(ref val3))..ctor(Mathf.Min(val.x, val2.x), (float)Screen.height - Mathf.Max(val.y, val2.y), Mathf.Abs(val2.x - val.x), Mathf.Abs(val2.y - val.y));
		return ((Rect)(ref val3)).Contains(guiPoint);
	}

	private unsafe void Dump(Target target, string reason)
	{
		//IL_01cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a3: Unknown result type (might be due to invalid IL or missing references)
		//IL_03a8: Unknown result type (might be due to invalid IL or missing references)
		try
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.AppendLine("=== FactionRowScroll report ===");
			stringBuilder.AppendLine("reason: " + reason);
			stringBuilder.AppendLine("mode: " + Plugin.CfgMode.Value);
			stringBuilder.AppendLine("time: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
			stringBuilder.AppendLine();
			stringBuilder.AppendLine("grid: " + PathOf(((Component)target.grid).transform));
			stringBuilder.AppendLine("components: " + Components(((Component)target.grid).transform));
			stringBuilder.AppendLine("children=" + ((Component)target.grid).transform.childCount + " factionButtons=" + target.factions.Count + " (of " + target.all.Count + " ExpandedFactionButton children)");
			stringBuilder.AppendLine("grid rect: " + Describe(target.gridRect));
			stringBuilder.AppendLine();
			if ((Object)(object)target.layout != (Object)null)
			{
				stringBuilder.AppendLine("GridLayoutGroup on: " + PathOf(((Component)target.layout).transform));
				string[] obj = new string[8]
				{
					"  original constraint=",
					((object)Unsafe.As<Constraint, Constraint>(ref target.originalConstraint)/*cast due to .constrained prefix*/).ToString(),
					"/",
					target.originalConstraintCount.ToString(),
					" cell=",
					null,
					null,
					null
				};
				Vector2 originalCellSize = target.originalCellSize;
				obj[5] = ((object)(*(Vector2*)(&originalCellSize))/*cast due to .constrained prefix*/).ToString();
				obj[6] = " spacing=";
				originalCellSize = target.originalSpacing;
				obj[7] = ((object)(*(Vector2*)(&originalCellSize))/*cast due to .constrained prefix*/).ToString();
				stringBuilder.AppendLine(string.Concat(obj));
				stringBuilder.AppendLine("  padding L/R/T/B=" + ((LayoutGroup)target.layout).padding.left + "/" + ((LayoutGroup)target.layout).padding.right + "/" + ((LayoutGroup)target.layout).padding.top + "/" + ((LayoutGroup)target.layout).padding.bottom);
			}
			else
			{
				stringBuilder.AppendLine("GridLayoutGroup: NOT FOUND");
			}
			stringBuilder.AppendLine();
			stringBuilder.AppendLine("ancestors (grid -> root):");
			Transform val = ((Component)target.grid).transform;
			int num = 0;
			while ((Object)(object)val != (Object)null && num++ < 10)
			{
				RectTransform val2 = (RectTransform)(object)((val is RectTransform) ? val : null);
				stringBuilder.AppendLine("  [" + num + "] " + ((Object)val).name + " active=" + ((Component)val).gameObject.activeSelf + " comps=" + Components(val));
				if ((Object)(object)val2 != (Object)null)
				{
					stringBuilder.AppendLine("        " + Describe(val2));
				}
				Canvas component = ((Component)val).GetComponent<Canvas>();
				if ((Object)(object)component != (Object)null)
				{
					stringBuilder.AppendLine("        Canvas.enabled=" + ((Behaviour)component).enabled + " renderMode=" + ((object)component.renderMode/*cast due to .constrained prefix*/).ToString());
				}
				CanvasGroup component2 = ((Component)val).GetComponent<CanvasGroup>();
				if ((Object)(object)component2 != (Object)null)
				{
					stringBuilder.AppendLine("        CanvasGroup alpha=" + component2.alpha + " interactable=" + component2.interactable);
				}
				ScrollRect component3 = ((Component)val).GetComponent<ScrollRect>();
				if ((Object)(object)component3 != (Object)null)
				{
					stringBuilder.AppendLine("        ScrollRect h=" + component3.horizontal + " v=" + component3.vertical);
				}
				val = val.parent;
			}
			stringBuilder.AppendLine();
			stringBuilder.AppendLine("first 2 children:");
			for (int i = 0; i < Mathf.Min(2, ((Component)target.grid).transform.childCount); i++)
			{
				Transform child = ((Component)target.grid).transform.GetChild(i);
				RectTransform val3 = (RectTransform)(object)((child is RectTransform) ? child : null);
				stringBuilder.AppendLine("  " + ((Object)child).name + " active=" + ((Component)child).gameObject.activeSelf + (((Object)(object)val3 != (Object)null) ? (" " + Describe(val3)) : ""));
			}
			File.WriteAllText(PathOfDumpFile(), stringBuilder.ToString(), new UTF8Encoding(encoderShouldEmitUTF8Identifier: false));
			Plugin.LogInfo("[FactionRow] wrote report to " + PathOfDumpFile());
		}
		catch (Exception ex)
		{
			Plugin.LogWarn("[FactionRow] dump failed: " + ex.Message);
		}
	}

	internal static string PathOfDumpFile()
	{
		try
		{
			string text = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			if (string.IsNullOrEmpty(text))
			{
				text = ".";
			}
			return Path.Combine(text, "FactionRow_dump.txt");
		}
		catch
		{
			return "FactionRow_dump.txt";
		}
	}

	private static string Describe(RectTransform rt)
	{
		//IL_001e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0023: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: 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_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_0076: 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_0092: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ae: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)rt == (Object)null)
		{
			return "<null>";
		}
		object[] array = new object[9];
		Rect rect = rt.rect;
		array[0] = ((Rect)(ref rect)).xMin;
		rect = rt.rect;
		array[1] = ((Rect)(ref rect)).xMax;
		rect = rt.rect;
		array[2] = ((Rect)(ref rect)).yMin;
		rect = rt.rect;
		array[3] = ((Rect)(ref rect)).yMax;
		array[4] = rt.sizeDelta;
		array[5] = rt.anchorMin;
		array[6] = rt.anchorMax;
		array[7] = rt.pivot;
		array[8] = rt.anchoredPosition;
		return string.Format("rect=(x{0:F0}..{1:F0}, y{2:F0}..{3:F0}) size={4} anchors={5}-{6} pivot={7} pos={8}", array);
	}

	private static string Components(Transform t)
	{
		if ((Object)(object)t == (Object)null)
		{
			return "<null>";
		}
		StringBuilder stringBuilder = new StringBuilder();
		Component[] components = ((Component)t).GetComponents<Component>();
		foreach (Component val in components)
		{
			if (!((Object)(object)val == (Object)null))
			{
				string name = ((object)val).GetType().Name;
				if (!(name == "Transform") && !(name == "RectTransform"))
				{
					stringBuilder.Append(name).Append(' ');
				}
			}
		}
		return stringBuilder.ToString().Trim();
	}

	private static string PathOf(Transform t)
	{
		if ((Object)(object)t == (Object)null)
		{
			return "<null>";
		}
		string text = ((Object)t).name;
		Transform parent = t.parent;
		int num = 0;
		while ((Object)(object)parent != (Object)null && num++ < 8)
		{
			text = ((Object)parent).name + "/" + text;
			parent = parent.parent;
		}
		return text;
	}
}
[BepInPlugin("saucein.tabs.factionrowscroll", "Faction Row Scroll", "1.8.1")]
[BepInProcess("TotallyAccurateBattleSimulator.exe")]
public class Plugin : BaseUnityPlugin
{
	public enum Mode
	{
		Pages,
		FitAll,
		DumpOnly
	}

	public enum ArrowStyle
	{
		Text,
		Chevron,
		Auto,
		Sprite
	}

	public const string PluginGuid = "saucein.tabs.factionrowscroll";

	public const string PluginName = "Faction Row Scroll";

	public const string PluginVersion = "1.8.1";

	internal static ManualLogSource Log;

	internal static ConfigEntry<bool> CfgEnabled;

	internal static ConfigEntry<Mode> CfgMode;

	internal static ConfigEntry<int> CfgRows;

	internal static ConfigEntry<bool> CfgHorizontalScroll;

	internal static ConfigEntry<bool> CfgMouseWheel;

	internal static ConfigEntry<float> CfgPagerOffset;

	internal static ConfigEntry<ArrowStyle> CfgArrowStyle;

	internal static ConfigEntry<string> CfgArrowSprite;

	internal static ConfigEntry<float> CfgArrowRotate;

	internal static ConfigEntry<bool> CfgVerbose;

	private void Awake()
	{
		//IL_015c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0162: Expected O, but got Unknown
		//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ac: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b2: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		CfgEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enabled", true, "Turn the faction grid fix on or off.");
		CfgMode = ((BaseUnityPlugin)this).Config.Bind<Mode>("General", "LayoutMode", Mode.Pages, "Pages = single row split into pages (click the < > arrows drawn above the row, or use the mouse wheel over it). FitAll = keep all rows but shrink the cells so everything fits. DumpOnly = change nothing and only write FactionRow_dump.txt.");
		CfgRows = ((BaseUnityPlugin)this).Config.Bind<int>("General", "Rows", 1, "Reserved for future layouts; the paged row is always a single row.");
		CfgHorizontalScroll = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "HorizontalScroll", true, "ScrollRow mode only: allow horizontal scrolling.");
		CfgMouseWheel = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "MouseWheelScroll", true, "ScrollRow mode only: use the mouse wheel to scroll the row horizontally.");
		CfgPagerOffset = ((BaseUnityPlugin)this).Config.Bind<float>("General", "PagerOffset", 40f, "How far down the '< 1 / 4 >' pager sits, in UI units. Positive = lower (towards the faction icons), negative = higher (towards the title).");
		CfgArrowStyle = ((BaseUnityPlugin)this).Config.Bind<ArrowStyle>("General", "PagerArrowStyle", ArrowStyle.Text, "Which artwork the pager arrows use. Text = the plain < and > characters (default). Chevron = drawn white chevrons. Auto = a game sprite if one passes shape validation, otherwise chevrons. Sprite = insist on the game's own sprite artwork.");
		CfgArrowSprite = ((BaseUnityPlugin)this).Config.Bind<string>("General", "PagerArrowSprite", string.Empty, "Force one specific arrow sprite by name (case-insensitive, partial names allowed). Leave empty to let the plugin choose. The log lists every candidate it found.");
		CfgArrowRotate = ((BaseUnityPlugin)this).Config.Bind<float>("General", "PagerArrowRotate", 0f, "Extra rotation in degrees applied to the arrow artwork (0 = none). Use it when a forced sprite points the wrong way, e.g. 90 or -90.");
		CfgVerbose = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "VerboseLogging", true, "Log layout details (useful when reporting a problem).");
		Harmony val = new Harmony("saucein.tabs.factionrowscroll");
		try
		{
			val.PatchAll(typeof(FactionGridPatch));
			LogInfo("Harmony patch applied: ExpandedLandfallFactionsGrid.SpawnFaction");
		}
		catch (Exception ex)
		{
			Log.LogError((object)("Could not apply the Harmony patch: " + ex));
		}
		GameObject val2 = new GameObject("FactionRowScroll");
		Object.DontDestroyOnLoad((Object)val2);
		val2.AddComponent<FactionRowLayout>();
		LogInfo("Faction Row Scroll 1.8.1 loaded. Report file: " + FactionRowLayout.PathOfDumpFile());
	}

	internal static void LogInfo(string message)
	{
		if (Log != null)
		{
			if (CfgVerbose != null && !CfgVerbose.Value)
			{
				Log.LogDebug((object)message);
			}
			else
			{
				Log.LogInfo((object)message);
			}
		}
	}

	internal static void LogWarn(string message)
	{
		if (Log != null)
		{
			Log.LogWarning((object)message);
		}
	}
}