Decompiled source of Cosmetic Box Scanner v1.0.0

plugins/CosmeticBoxScanner.dll

Decompiled 2 hours ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Controls;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace CosmeticBoxScanner;

public class BoxEntry
{
	public int InstanceId;

	public int Number;

	public CosmeticWorldObject Box;

	public PhysGrabObject Grab;

	public RoomVolumeCheck RoomCheck;

	public Rarity Rarity;

	public Vector3 Position;

	public Vector3 SpawnPosition;

	public string Location = "?";

	public bool InTruck;

	public bool InExtraction;

	public bool Exploding;

	public bool FromRegistry;

	public float Distance;

	public float HeightDelta;

	public bool IsAlive
	{
		get
		{
			if ((Object)(object)Box != (Object)null)
			{
				return ((Component)Box).gameObject.activeInHierarchy;
			}
			return false;
		}
	}
}
public class BoxScanner : MonoBehaviour
{
	public static BoxScanner Instance;

	public readonly List<BoxEntry> Boxes = new List<BoxEntry>();

	private readonly Dictionary<int, BoxEntry> byId = new Dictionary<int, BoxEntry>();

	private readonly HashSet<int> seenThisPass = new HashSet<int>();

	public int TargetAmount = -1;

	public string LevelName = "";

	public bool InLevel;

	public int ScanVersion;

	public Vector3 PlayerPosition;

	private float scanTimer;

	private float deepScanTimer;

	private int nextNumber = 1;

	private object lastRoundDirector;

	private static FieldInfo fTargetAmount;

	private static FieldInfo fInExtraction;

	private static FieldInfo fExploding;

	private static FieldInfo fSpawnPosition;

	private static FieldInfo fRoomVolumeCheck;

	private static FieldInfo fPhysGrabObject;

	private static readonly Dictionary<string, string> prettyCache = new Dictionary<string, string>();

	private void Awake()
	{
		Instance = this;
		fTargetAmount = typeof(ValuableDirector).GetField("cosmeticWorldObjectTargetAmount", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		fInExtraction = typeof(CosmeticWorldObject).GetField("inExtraction", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		fExploding = typeof(CosmeticWorldObject).GetField("exploding", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		fSpawnPosition = typeof(CosmeticWorldObject).GetField("spawnPosition", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		fRoomVolumeCheck = typeof(CosmeticWorldObject).GetField("roomVolumeCheck", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		fPhysGrabObject = typeof(CosmeticWorldObject).GetField("physGrabObject", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
		if (fTargetAmount == null || fInExtraction == null || fRoomVolumeCheck == null)
		{
			Plugin.Log.LogWarning((object)"Some CosmeticWorldObject/ValuableDirector fields were not found - game version changed? Scanner still works with reduced info.");
		}
	}

	private void Update()
	{
		try
		{
			if (!IsInPlayableLevel())
			{
				if (InLevel || Boxes.Count > 0)
				{
					Clear("left level");
				}
				InLevel = false;
				return;
			}
			if (!object.ReferenceEquals(lastRoundDirector, RoundDirector.instance))
			{
				Clear("new level");
				lastRoundDirector = RoundDirector.instance;
			}
			if (!InLevel)
			{
				InLevel = true;
				LevelName = GetLevelName();
				deepScanTimer = 0f;
				scanTimer = 0f;
			}
			UpdatePlayerPosition();
			scanTimer -= Time.unscaledDeltaTime;
			deepScanTimer -= Time.unscaledDeltaTime;
			if (scanTimer <= 0f)
			{
				scanTimer = Plugin.ScanInterval.Value;
				bool flag = deepScanTimer <= 0f;
				if (flag)
				{
					deepScanTimer = Plugin.DeepScanInterval.Value;
				}
				Scan(flag);
			}
		}
		catch (Exception ex)
		{
			scanTimer = 2f;
			Plugin.Log.LogError((object)("Scanner error: " + ex));
		}
	}

	public static bool IsInPlayableLevel()
	{
		if ((Object)(object)RoundDirector.instance == (Object)null || (Object)(object)LevelGenerator.Instance == (Object)null)
		{
			return false;
		}
		if (!LevelGenerator.Instance.Generated)
		{
			return false;
		}
		if ((Object)(object)RunManager.instance == (Object)null || (Object)(object)RunManager.instance.levelCurrent == (Object)null)
		{
			return false;
		}
		if (SemiFunc.MenuLevel() || SemiFunc.RunIsLobby() || SemiFunc.RunIsShop())
		{
			return false;
		}
		return true;
	}

	private static string GetLevelName()
	{
		Level val = (((Object)(object)RunManager.instance != (Object)null) ? RunManager.instance.levelCurrent : null);
		if ((Object)(object)val == (Object)null)
		{
			return "";
		}
		string text = val.NarrativeName;
		if (string.IsNullOrEmpty(text))
		{
			text = ((Object)val).name;
		}
		return text;
	}

	private void UpdatePlayerPosition()
	{
		//IL_0018: Unknown result type (might be due to invalid IL or missing references)
		//IL_001d: 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_0040: 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_0063: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)PlayerAvatar.instance != (Object)null)
		{
			PlayerPosition = ((Component)PlayerAvatar.instance).transform.position;
		}
		else if ((Object)(object)PlayerController.instance != (Object)null)
		{
			PlayerPosition = ((Component)PlayerController.instance).transform.position;
		}
		else if ((Object)(object)Camera.main != (Object)null)
		{
			PlayerPosition = ((Component)Camera.main).transform.position;
		}
	}

	public void Clear(string reason)
	{
		if (Boxes.Count > 0 && Plugin.LogDetections.Value)
		{
			Plugin.Log.LogInfo((object)("Clearing " + Boxes.Count + " box(es): " + reason));
		}
		Boxes.Clear();
		byId.Clear();
		nextNumber = 1;
		TargetAmount = -1;
		LevelName = "";
		InLevel = false;
		ScanVersion++;
	}

	private void Scan(bool deep)
	{
		//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
		seenThisPass.Clear();
		bool flag = false;
		List<CosmeticWorldObject> cosmeticWorldObjects = RoundDirector.instance.cosmeticWorldObjects;
		if (cosmeticWorldObjects != null)
		{
			for (int i = 0; i < cosmeticWorldObjects.Count; i++)
			{
				flag |= Consider(cosmeticWorldObjects[i], fromRegistry: true);
			}
		}
		if (deep)
		{
			CosmeticWorldObject[] array = Object.FindObjectsByType<CosmeticWorldObject>((FindObjectsInactive)0, (FindObjectsSortMode)0);
			for (int j = 0; j < array.Length; j++)
			{
				flag |= Consider(array[j], fromRegistry: false);
			}
		}
		for (int num = Boxes.Count - 1; num >= 0; num--)
		{
			BoxEntry boxEntry = Boxes[num];
			if (!boxEntry.IsAlive || (!seenThisPass.Contains(boxEntry.InstanceId) && (deep || boxEntry.FromRegistry)))
			{
				if (Plugin.LogDetections.Value)
				{
					Plugin.Log.LogInfo((object)$"Box #{boxEntry.Number} ({RarityName(boxEntry.Rarity)}) gone (extracted/destroyed).");
				}
				Boxes.RemoveAt(num);
				byId.Remove(boxEntry.InstanceId);
				flag = true;
			}
		}
		for (int k = 0; k < Boxes.Count; k++)
		{
			Refresh(Boxes[k]);
		}
		if ((Object)(object)ValuableDirector.instance != (Object)null && fTargetAmount != null)
		{
			int num2 = (int)fTargetAmount.GetValue(ValuableDirector.instance);
			if (num2 != TargetAmount)
			{
				TargetAmount = num2;
				flag = true;
			}
		}
		Boxes.Sort(CompareByDistance);
		if (flag && Plugin.LogDetections.Value)
		{
			Plugin.Log.LogInfo((object)$"Scan [{LevelName}]: {Boxes.Count} box(es) tracked, game reports {TargetAmount} spawned.");
		}
		ScanVersion++;
	}

	private static int CompareByDistance(BoxEntry a, BoxEntry b)
	{
		return a.Distance.CompareTo(b.Distance);
	}

	private bool Consider(CosmeticWorldObject cwo, bool fromRegistry)
	{
		//IL_007e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0083: Unknown result type (might be due to invalid IL or missing references)
		//IL_012e: Unknown result type (might be due to invalid IL or missing references)
		//IL_011c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0133: Unknown result type (might be due to invalid IL or missing references)
		//IL_018a: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)cwo == (Object)null)
		{
			return false;
		}
		if (!((Component)cwo).gameObject.activeInHierarchy)
		{
			return false;
		}
		int instanceID = ((Object)cwo).GetInstanceID();
		seenThisPass.Add(instanceID);
		if (byId.TryGetValue(instanceID, out var value))
		{
			if (fromRegistry)
			{
				value.FromRegistry = true;
			}
			return false;
		}
		value = new BoxEntry();
		value.InstanceId = instanceID;
		value.Number = nextNumber++;
		value.Box = cwo;
		value.FromRegistry = fromRegistry;
		value.Rarity = cwo.rarity;
		value.Grab = (PhysGrabObject)((fPhysGrabObject != null) ? /*isinst with value type is only supported in some contexts*/: null);
		if ((Object)(object)value.Grab == (Object)null)
		{
			value.Grab = ((Component)cwo).GetComponent<PhysGrabObject>();
		}
		value.RoomCheck = (RoomVolumeCheck)((fRoomVolumeCheck != null) ? /*isinst with value type is only supported in some contexts*/: null);
		if ((Object)(object)value.RoomCheck == (Object)null)
		{
			value.RoomCheck = ((Component)cwo).GetComponent<RoomVolumeCheck>();
		}
		value.SpawnPosition = (Vector3)((fSpawnPosition != null) ? ((Vector3)fSpawnPosition.GetValue(cwo)) : ((Component)cwo).transform.position);
		Refresh(value);
		Boxes.Add(value);
		byId[instanceID] = value;
		if (Plugin.LogDetections.Value)
		{
			Plugin.Log.LogInfo((object)string.Format("DETECTED #{0} rarity={1} pos=({2:F2}, {3:F2}, {4:F2}) location=\"{5}\" level=\"{6}\" source={7} object=\"{8}\" id={9}", value.Number, RarityName(value.Rarity), value.Position.x, value.Position.y, value.Position.z, value.Location, LevelName, fromRegistry ? "registry" : "scene-sweep", ((Object)((Component)cwo).gameObject).name, instanceID));
		}
		return true;
	}

	private void Refresh(BoxEntry e)
	{
		//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_0081: 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_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0088: Unknown result type (might be due to invalid IL or missing references)
		//IL_0089: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_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_00f5: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0101: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)e.Box == (Object)null))
		{
			Vector3 val = (e.Position = (((Object)(object)e.Grab != (Object)null && e.Grab.centerPoint != Vector3.zero) ? e.Grab.centerPoint : ((!((Object)(object)e.Grab != (Object)null) || !((Object)(object)e.Grab.rb != (Object)null)) ? ((Component)e.Box).transform.position : e.Grab.rb.worldCenterOfMass)));
			e.Rarity = e.Box.rarity;
			e.InExtraction = fInExtraction != null && (bool)fInExtraction.GetValue(e.Box);
			e.Exploding = fExploding != null && (bool)fExploding.GetValue(e.Box);
			Vector3 val2 = val - PlayerPosition;
			e.Distance = ((Vector3)(ref val2)).magnitude;
			e.HeightDelta = val2.y;
			e.Location = ResolveLocation(e);
		}
	}

	private static string ResolveLocation(BoxEntry e)
	{
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		e.InTruck = false;
		RoomVolumeCheck roomCheck = e.RoomCheck;
		if ((Object)(object)roomCheck != (Object)null && roomCheck.CurrentRooms != null)
		{
			for (int i = 0; i < roomCheck.CurrentRooms.Count; i++)
			{
				RoomVolume val = roomCheck.CurrentRooms[i];
				if (!((Object)(object)val == (Object)null))
				{
					if (val.Truck)
					{
						e.InTruck = true;
						return "Truck";
					}
					if (val.Extraction)
					{
						return "Extraction point";
					}
					if ((Object)(object)val.Module != (Object)null)
					{
						return PrettyModule(SemiFunc.ModuleNameGet(((Component)val.Module).transform));
					}
				}
			}
		}
		try
		{
			return PrettyModule(SemiFunc.ModuleNameGet(e.Position));
		}
		catch
		{
			return "Unknown area";
		}
	}

	public static string PrettyModule(string raw)
	{
		if (string.IsNullOrEmpty(raw))
		{
			return "Unknown area";
		}
		if (prettyCache.TryGetValue(raw, out var value))
		{
			return value;
		}
		string text = raw.Replace("(Clone)", "").Trim();
		string[] array = text.Split(new string[1] { " - " }, StringSplitOptions.None);
		if (array.Length >= 2 && array[0].Trim() == "Module")
		{
			text = array[^1].Trim();
		}
		if (text.Length == 0 || text == "no module")
		{
			text = "Unknown area";
		}
		prettyCache[raw] = text;
		return text;
	}

	public BoxEntry FindById(int id)
	{
		if (!byId.TryGetValue(id, out var value))
		{
			return null;
		}
		return value;
	}

	public static string RarityName(Rarity r)
	{
		//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_0018: Expected I4, but got Unknown
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		return (int)r switch
		{
			0 => "Common", 
			1 => "Uncommon", 
			2 => "Rare", 
			3 => "Ultra Rare", 
			_ => ((object)r).ToString(), 
		};
	}

	public static Color RarityColor(Rarity r)
	{
		//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_0018: Expected I4, but got Unknown
		//IL_0026: 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_0041: 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_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0062: Unknown result type (might be due to invalid IL or missing references)
		//IL_0078: 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_0083: Unknown result type (might be due to invalid IL or missing references)
		return (Color)((int)r switch
		{
			0 => Color32.op_Implicit(new Color32((byte)0, byte.MaxValue, (byte)2, byte.MaxValue)), 
			1 => Color32.op_Implicit(new Color32((byte)0, (byte)195, byte.MaxValue, byte.MaxValue)), 
			2 => Color32.op_Implicit(new Color32(byte.MaxValue, (byte)110, (byte)244, byte.MaxValue)), 
			3 => Color32.op_Implicit(new Color32(byte.MaxValue, (byte)199, (byte)0, byte.MaxValue)), 
			_ => Color.white, 
		});
	}
}
public enum PanelStyle
{
	Icons,
	Detailed,
	Hidden
}
[BepInPlugin("CosmeticBoxScanner", "Cosmetic Box Scanner", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string PluginGuid = "CosmeticBoxScanner";

	public const string PluginName = "Cosmetic Box Scanner";

	public const string PluginVersion = "1.0.0";

	internal static ManualLogSource Log;

	internal static Plugin Instance;

	public static ConfigEntry<Key> KeyTogglePanel;

	public static ConfigEntry<Key> KeyNext;

	public static ConfigEntry<Key> KeyPrev;

	public static ConfigEntry<Key> KeyToggleMarkers;

	public static ConfigEntry<Key> KeyClearTarget;

	public static ConfigEntry<PanelStyle> DefaultPanel;

	public static ConfigEntry<bool> ShowWorldMarkers;

	public static ConfigEntry<bool> ShowCoordinates;

	public static ConfigEntry<float> PanelX;

	public static ConfigEntry<float> PanelY;

	public static ConfigEntry<int> MaxRows;

	public static ConfigEntry<float> UiScale;

	public static ConfigEntry<bool> AutoTrackNearest;

	public static ConfigEntry<float> ScanInterval;

	public static ConfigEntry<float> DeepScanInterval;

	public static ConfigEntry<bool> LogDetections;

	private void Awake()
	{
		//IL_0145: Unknown result type (might be due to invalid IL or missing references)
		//IL_014f: Expected O, but got Unknown
		//IL_0183: Unknown result type (might be due to invalid IL or missing references)
		//IL_018d: Expected O, but got Unknown
		//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c0: Expected O, but got Unknown
		//IL_01f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_01fe: Expected O, but got Unknown
		//IL_0252: Unknown result type (might be due to invalid IL or missing references)
		//IL_025c: Expected O, but got Unknown
		//IL_0290: Unknown result type (might be due to invalid IL or missing references)
		//IL_029a: Expected O, but got Unknown
		//IL_02c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ca: Expected O, but got Unknown
		Instance = this;
		Log = ((BaseUnityPlugin)this).Logger;
		KeyTogglePanel = ((BaseUnityPlugin)this).Config.Bind<Key>("Keys", "TogglePanel", (Key)99, "Cycle the panel: icons + counts -> detailed list -> hidden.");
		KeyPrev = ((BaseUnityPlugin)this).Config.Bind<Key>("Keys", "SelectPrevious", (Key)100, "Track the previous box in the list.");
		KeyNext = ((BaseUnityPlugin)this).Config.Bind<Key>("Keys", "SelectNext", (Key)101, "Track the next box in the list.");
		KeyToggleMarkers = ((BaseUnityPlugin)this).Config.Bind<Key>("Keys", "ToggleWorldMarkers", (Key)102, "Show/hide on-screen markers for every box.");
		KeyClearTarget = ((BaseUnityPlugin)this).Config.Bind<Key>("Keys", "ClearTarget", (Key)103, "Stop tracking the selected box.");
		DefaultPanel = ((BaseUnityPlugin)this).Config.Bind<PanelStyle>("Display", "DefaultPanel", PanelStyle.Icons, "Panel shown when a level starts: Icons (box icon + count per rarity), Detailed (full list with room/coordinates) or Hidden. F6 cycles at runtime.");
		ShowWorldMarkers = ((BaseUnityPlugin)this).Config.Bind<bool>("Display", "ShowWorldMarkers", true, "Draw a small marker over every detected box (visible through walls).");
		ShowCoordinates = ((BaseUnityPlugin)this).Config.Bind<bool>("Display", "ShowCoordinates", true, "Show X/Y/Z coordinates in the panel.");
		PanelX = ((BaseUnityPlugin)this).Config.Bind<float>("Display", "PanelX", 0.985f, new ConfigDescription("Horizontal anchor of the panel's right edge (0 = left, 1 = right of screen).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), new object[0]));
		PanelY = ((BaseUnityPlugin)this).Config.Bind<float>("Display", "PanelY", 0.3f, new ConfigDescription("Vertical position of the panel's top edge (0 = top, 1 = bottom).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0f, 1f), new object[0]));
		MaxRows = ((BaseUnityPlugin)this).Config.Bind<int>("Display", "MaxRows", 8, new ConfigDescription("Maximum boxes listed in the panel.", (AcceptableValueBase)(object)new AcceptableValueRange<int>(1, 30), new object[0]));
		UiScale = ((BaseUnityPlugin)this).Config.Bind<float>("Display", "UiScale", 1f, new ConfigDescription("Extra UI scale on top of automatic resolution scaling.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 2.5f), new object[0]));
		AutoTrackNearest = ((BaseUnityPlugin)this).Config.Bind<bool>("Display", "AutoTrackNearest", true, "Automatically track the nearest box when nothing is selected (e.g. after the tracked box is extracted).");
		ScanInterval = ((BaseUnityPlugin)this).Config.Bind<float>("Scanning", "ScanInterval", 0.25f, new ConfigDescription("Seconds between refreshes of the box registry (positions, rooms, distances).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.05f, 5f), new object[0]));
		DeepScanInterval = ((BaseUnityPlugin)this).Config.Bind<float>("Scanning", "DeepScanInterval", 3f, new ConfigDescription("Seconds between full scene sweeps that catch boxes not in the game's registry (e.g. spawned by other mods).", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.5f, 60f), new object[0]));
		LogDetections = ((BaseUnityPlugin)this).Config.Bind<bool>("Scanning", "LogDetections", true, "Write every newly detected box (rarity, coordinates, room) to the BepInEx log.");
		GameObject val = new GameObject("CosmeticBoxScanner");
		((Object)val).hideFlags = (HideFlags)61;
		Object.DontDestroyOnLoad((Object)(object)val);
		val.AddComponent<BoxScanner>();
		val.AddComponent<ScannerUI>();
		Log.LogInfo((object)"Cosmetic Box Scanner 1.0.0 loaded. F6 panel, F7/F8 select, F9 markers, F10 clear.");
	}
}
public class ScannerUI : MonoBehaviour
{
	public static ScannerUI Instance;

	public int SelectedId;

	private bool userCleared;

	private PanelStyle panelMode;

	private GUIStyle styleRow;

	private GUIStyle styleHeader;

	private GUIStyle styleSmall;

	private GUIStyle styleMarker;

	private GUIStyle styleNav;

	private GUIStyle styleShadow;

	private GUIStyle styleCount;

	private Texture2D texWhite;

	private Texture2D texArrow;

	private Texture2D texStar;

	private Texture2D texBoxFallback;

	private Texture boxIcon;

	private bool boxIconSearched;

	private readonly int[] rarityCounts = new int[4];

	private float lastScale = -1f;

	private int builtVersion = -1;

	private readonly List<string> rowText = new List<string>();

	private readonly List<Color> rowColor = new List<Color>();

	private readonly List<bool> rowSelected = new List<bool>();

	private string headerText = "";

	private string footerText = "";

	private static FieldInfo fMenuPage;

	private string lastHideReason;

	private float nextUiErrorLog;

	private static Camera cachedCam;

	private readonly List<Rect> usedLabels = new List<Rect>();

	private float Scale => Mathf.Max(0.6f, (float)Screen.height / 1080f) * Plugin.UiScale.Value;

	private void Awake()
	{
		Instance = this;
		panelMode = Plugin.DefaultPanel.Value;
		fMenuPage = typeof(MenuManager).GetField("currentMenuPage", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
	}

	private void Update()
	{
		BoxScanner instance = BoxScanner.Instance;
		if ((Object)(object)instance == (Object)null)
		{
			return;
		}
		if (!instance.InLevel)
		{
			SelectedId = 0;
			userCleared = false;
			return;
		}
		HandleInput(instance);
		if (SelectedId != 0 && instance.FindById(SelectedId) == null)
		{
			SelectedId = 0;
		}
		if (SelectedId == 0 && !userCleared && Plugin.AutoTrackNearest.Value && instance.Boxes.Count > 0)
		{
			SelectedId = instance.Boxes[0].InstanceId;
		}
	}

	private void HandleInput(BoxScanner s)
	{
		//IL_002a: 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_007c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a3: 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)
		Keyboard current = Keyboard.current;
		if (current != null && (!((Object)(object)ChatManager.instance != (Object)null) || !ChatManager.instance.StateIsActive()))
		{
			if (Pressed(current, Plugin.KeyTogglePanel.Value))
			{
				panelMode = (PanelStyle)((int)(panelMode + 1) % 3);
				builtVersion = -1;
			}
			if (Pressed(current, Plugin.KeyToggleMarkers.Value))
			{
				Plugin.ShowWorldMarkers.Value = !Plugin.ShowWorldMarkers.Value;
			}
			if (Pressed(current, Plugin.KeyClearTarget.Value))
			{
				SelectedId = 0;
				userCleared = true;
				builtVersion = -1;
			}
			if (Pressed(current, Plugin.KeyNext.Value))
			{
				Cycle(s, 1);
			}
			if (Pressed(current, Plugin.KeyPrev.Value))
			{
				Cycle(s, -1);
			}
		}
	}

	private static bool Pressed(Keyboard kb, Key k)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		if ((int)k == 0)
		{
			return false;
		}
		try
		{
			return ((ButtonControl)kb[k]).wasPressedThisFrame;
		}
		catch
		{
			return false;
		}
	}

	private void Cycle(BoxScanner s, int dir)
	{
		if (s.Boxes.Count == 0)
		{
			return;
		}
		List<BoxEntry> list = new List<BoxEntry>(s.Boxes);
		list.Sort((BoxEntry a, BoxEntry b) => a.Number.CompareTo(b.Number));
		int num = -1;
		for (int num2 = 0; num2 < list.Count; num2++)
		{
			if (list[num2].InstanceId == SelectedId)
			{
				num = num2;
			}
		}
		num = ((num >= 0) ? ((num + dir + list.Count) % list.Count) : ((dir <= 0) ? (list.Count - 1) : 0));
		SelectedId = list[num].InstanceId;
		userCleared = false;
		builtVersion = -1;
	}

	private bool ShouldDraw()
	{
		string text = HideReason();
		if (text != lastHideReason && (Object)(object)BoxScanner.Instance != (Object)null && BoxScanner.Instance.InLevel)
		{
			Plugin.Log.LogInfo((object)((text == null) ? "Overlay visible." : ("Overlay hidden: " + text)));
			lastHideReason = text;
		}
		return text == null;
	}

	private string HideReason()
	{
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0035: Invalid comparison between Unknown and I4
		//IL_0041: Unknown result type (might be due to invalid IL or missing references)
		BoxScanner instance = BoxScanner.Instance;
		if ((Object)(object)instance == (Object)null || !instance.InLevel)
		{
			return "not in a level";
		}
		if ((Object)(object)GameDirector.instance != (Object)null && (int)GameDirector.instance.currentState != 2)
		{
			return "game state " + GameDirector.instance.currentState;
		}
		if ((Object)(object)MenuManager.instance != (Object)null && fMenuPage != null)
		{
			object? value = fMenuPage.GetValue(MenuManager.instance);
			Component val = (Component)((value is Component) ? value : null);
			if ((Object)(object)val != (Object)null && val.gameObject.activeInHierarchy)
			{
				return "menu page open: " + ((Object)val).name;
			}
		}
		return null;
	}

	private void OnGUI()
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		//IL_000b: Invalid comparison between Unknown and I4
		if ((int)Event.current.type != 7)
		{
			return;
		}
		try
		{
			if (!ShouldDraw())
			{
				return;
			}
			EnsureStyles();
			BoxScanner instance = BoxScanner.Instance;
			Camera camera = GetCamera();
			if ((Object)(object)camera != (Object)null)
			{
				if (Plugin.ShowWorldMarkers.Value)
				{
					DrawWorldMarkers(instance, camera);
				}
				DrawNavigation(instance, camera);
			}
			if (panelMode == PanelStyle.Detailed)
			{
				DrawPanel(instance);
			}
			else if (panelMode == PanelStyle.Icons)
			{
				DrawIconBar(instance);
			}
		}
		catch (Exception ex)
		{
			if (Time.unscaledTime >= nextUiErrorLog)
			{
				nextUiErrorLog = Time.unscaledTime + 10f;
				Plugin.Log.LogError((object)("UI error: " + ex));
			}
		}
	}

	private static Camera GetCamera()
	{
		if ((Object)(object)cachedCam != (Object)null && ((Behaviour)cachedCam).isActiveAndEnabled)
		{
			return cachedCam;
		}
		cachedCam = Camera.main;
		return cachedCam;
	}

	private Texture GetBoxIcon()
	{
		if ((Object)(object)boxIcon != (Object)null)
		{
			return boxIcon;
		}
		if (!boxIconSearched && (Object)(object)CosmeticWorldObjectUI.instance != (Object)null)
		{
			boxIconSearched = true;
			try
			{
				GameObject elementPrefab = CosmeticWorldObjectUI.instance.elementPrefab;
				if ((Object)(object)elementPrefab != (Object)null)
				{
					RawImage componentInChildren = elementPrefab.GetComponentInChildren<RawImage>(true);
					if ((Object)(object)componentInChildren != (Object)null)
					{
						boxIcon = componentInChildren.texture;
					}
				}
				Plugin.Log.LogInfo((object)("Box icon: " + (((Object)(object)boxIcon != (Object)null) ? ("using game texture '" + ((Object)boxIcon).name + "' " + boxIcon.width + "x" + boxIcon.height) : "not found, using built-in icon")));
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("Box icon lookup failed: " + ex.Message));
			}
		}
		if (!((Object)(object)boxIcon != (Object)null))
		{
			return (Texture)(object)texBoxFallback;
		}
		return boxIcon;
	}

	private void DrawIconBar(BoxScanner s)
	{
		//IL_0103: 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_014c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0287: Unknown result type (might be due to invalid IL or missing references)
		//IL_017f: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d6: Unknown result type (might be due to invalid IL or missing references)
		//IL_020c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0219: Unknown result type (might be due to invalid IL or missing references)
		//IL_0220: Unknown result type (might be due to invalid IL or missing references)
		//IL_0249: Unknown result type (might be due to invalid IL or missing references)
		//IL_026c: Unknown result type (might be due to invalid IL or missing references)
		if (builtVersion != s.ScanVersion)
		{
			RebuildPanelText(s);
		}
		float scale = Scale;
		float num = 40f * scale;
		float num2 = 46f * scale;
		float num3 = 110f * scale;
		float num4 = 8f * scale;
		int num5 = 0;
		for (int num6 = 3; num6 >= 0; num6--)
		{
			if (rarityCounts[num6] > 0)
			{
				num5++;
			}
		}
		float num7 = num4 * 2f + (float)Mathf.Max(1, num5) * num2;
		float num8 = Mathf.Clamp((float)Screen.width * Plugin.PanelX.Value - num3, 0f, Mathf.Max(0f, (float)Screen.width - num3));
		float num9 = Mathf.Clamp((float)Screen.height * Plugin.PanelY.Value, 0f, Mathf.Max(0f, (float)Screen.height - num7));
		Texture val = GetBoxIcon();
		float num10 = num9 + num4;
		if (num5 == 0)
		{
			GUI.color = new Color(1f, 1f, 1f, 0.35f);
			GUI.DrawTexture(new Rect(num8 + num4, num10 + (num2 - num) / 2f, num, num), val, (ScaleMode)2);
			ShadowLabel(new Rect(num8 + num4 + num + 6f * scale, num10, num3 - num - num4 * 2f, num2), (s.TargetAmount == 0) ? "x0" : "...", styleCount, new Color(1f, 1f, 1f, 0.5f));
		}
		Rect val3 = default(Rect);
		for (int num11 = 3; num11 >= 0; num11--)
		{
			if (rarityCounts[num11] != 0)
			{
				Color val2 = BoxScanner.RarityColor((Rarity)num11);
				((Rect)(ref val3))..ctor(num8 + num4, num10 + (num2 - num) / 2f, num, num);
				GUI.color = new Color(0f, 0f, 0f, 0.7f);
				GUI.DrawTexture(new Rect(((Rect)(ref val3)).x + 2f * scale, ((Rect)(ref val3)).y + 2f * scale, ((Rect)(ref val3)).width, ((Rect)(ref val3)).height), val, (ScaleMode)2);
				GUI.color = val2;
				GUI.DrawTexture(val3, val, (ScaleMode)2);
				ShadowLabel(new Rect(num8 + num4 + num + 6f * scale, num10, num3 - num - num4 * 2f, num2), "x" + rarityCounts[num11], styleCount, val2);
				num10 += num2;
			}
		}
		GUI.color = Color.white;
	}

	private void RebuildPanelText(BoxScanner s)
	{
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Expected I4, but got Unknown
		//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_033f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0352: Unknown result type (might be due to invalid IL or missing references)
		//IL_0365: Unknown result type (might be due to invalid IL or missing references)
		//IL_0378: Unknown result type (might be due to invalid IL or missing references)
		//IL_038b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0276: Unknown result type (might be due to invalid IL or missing references)
		//IL_027b: Unknown result type (might be due to invalid IL or missing references)
		builtVersion = s.ScanVersion;
		rowText.Clear();
		rowColor.Clear();
		rowSelected.Clear();
		for (int i = 0; i < rarityCounts.Length; i++)
		{
			rarityCounts[i] = 0;
		}
		for (int j = 0; j < s.Boxes.Count; j++)
		{
			int num = (int)s.Boxes[j].Rarity;
			if (num >= 0 && num < rarityCounts.Length)
			{
				rarityCounts[num]++;
			}
		}
		string text = ((s.TargetAmount >= 0) ? $"{s.Boxes.Count} found / {s.TargetAmount} spawned" : $"{s.Boxes.Count} found");
		headerText = "COSMETIC BOXES  -  " + text;
		int num2 = Mathf.Min(s.Boxes.Count, Plugin.MaxRows.Value);
		for (int k = 0; k < num2; k++)
		{
			BoxEntry boxEntry = s.Boxes[k];
			bool flag = boxEntry.InstanceId == SelectedId;
			string text2 = (boxEntry.InTruck ? "  [in truck]" : (boxEntry.InExtraction ? "  [in extraction]" : (boxEntry.Exploding ? "  [DAMAGED!]" : "")));
			string text3 = Vertical(boxEntry.HeightDelta);
			string text4 = string.Format("{0}#{1}  {2}  {3:F1} m{4}  -  {5}{6}", flag ? "> " : "   ", boxEntry.Number, BoxScanner.RarityName(boxEntry.Rarity).ToUpperInvariant(), boxEntry.Distance, (text3.Length > 0) ? (" " + text3) : "", boxEntry.Location, text2);
			if (Plugin.ShowCoordinates.Value)
			{
				text4 += $"\n      X {boxEntry.Position.x:F1}   Y {boxEntry.Position.y:F1}   Z {boxEntry.Position.z:F1}";
			}
			rowText.Add(text4);
			rowColor.Add(BoxScanner.RarityColor(boxEntry.Rarity));
			rowSelected.Add(flag);
		}
		if (s.Boxes.Count == 0)
		{
			footerText = ((s.TargetAmount == 0) ? "No cosmetic boxes on this level." : "Scanning...");
		}
		else if (s.Boxes.Count > num2)
		{
			footerText = $"+{s.Boxes.Count - num2} more (farther away)";
		}
		else
		{
			footerText = "";
		}
		footerText = footerText + ((footerText.Length > 0) ? "\n" : "") + $"{Plugin.KeyPrev.Value}/{Plugin.KeyNext.Value} select   {Plugin.KeyClearTarget.Value} clear   {Plugin.KeyToggleMarkers.Value} markers   {Plugin.KeyTogglePanel.Value} panel";
	}

	private static string Vertical(float dy)
	{
		if (dy > 2.5f)
		{
			return $"({dy:F0}m up)";
		}
		if (dy < -2.5f)
		{
			return $"({0f - dy:F0}m down)";
		}
		return "";
	}

	private void DrawPanel(BoxScanner s)
	{
		//IL_003a: Unknown result type (might be due to invalid IL or missing references)
		//IL_004d: Expected O, but got Unknown
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Expected O, but got Unknown
		//IL_00e4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f7: Expected O, but got Unknown
		//IL_016f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0188: Unknown result type (might be due to invalid IL or missing references)
		//IL_01a7: Unknown result type (might be due to invalid IL or missing references)
		//IL_01c2: Unknown result type (might be due to invalid IL or missing references)
		//IL_026f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0279: Unknown result type (might be due to invalid IL or missing references)
		//IL_0244: Unknown result type (might be due to invalid IL or missing references)
		//IL_025d: Unknown result type (might be due to invalid IL or missing references)
		//IL_030b: Unknown result type (might be due to invalid IL or missing references)
		//IL_02da: Unknown result type (might be due to invalid IL or missing references)
		//IL_02f5: Unknown result type (might be due to invalid IL or missing references)
		if (builtVersion != s.ScanVersion)
		{
			RebuildPanelText(s);
		}
		float scale = Scale;
		float num = 430f * scale;
		float num2 = 6f * scale;
		float num3 = num2;
		float num4 = styleHeader.CalcHeight(new GUIContent(headerText), num - 2f * num2);
		num3 += num4 + 2f * scale;
		float[] array = new float[rowText.Count];
		for (int i = 0; i < rowText.Count; i++)
		{
			array[i] = styleRow.CalcHeight(new GUIContent(rowText[i]), num - 2f * num2);
			num3 += array[i] + 2f * scale;
		}
		float num5 = ((footerText.Length > 0) ? styleSmall.CalcHeight(new GUIContent(footerText), num - 2f * num2) : 0f);
		num3 += num5 + num2;
		float num6 = (float)Screen.width * Plugin.PanelX.Value - num;
		float num7 = (float)Screen.height * Plugin.PanelY.Value;
		num6 = Mathf.Clamp(num6, 0f, Mathf.Max(0f, (float)Screen.width - num));
		num7 = Mathf.Clamp(num7, 0f, Mathf.Max(0f, (float)Screen.height - num3));
		FillRect(new Rect(num6, num7, num, num3), new Color(0f, 0f, 0f, 0.55f));
		float num8 = num7 + num2;
		GUI.color = new Color(1f, 0.85f, 0.3f);
		GUI.Label(new Rect(num6 + num2, num8, num - 2f * num2, num4), headerText, styleHeader);
		num8 += num4 + 2f * scale;
		Rect val = default(Rect);
		for (int j = 0; j < rowText.Count; j++)
		{
			((Rect)(ref val))..ctor(num6 + num2, num8, num - 2f * num2, array[j]);
			if (rowSelected[j])
			{
				FillRect(new Rect(num6 + 2f * scale, num8 - 1f * scale, num - 4f * scale, array[j] + 2f * scale), new Color(1f, 1f, 1f, 0.12f));
			}
			GUI.color = rowColor[j];
			GUI.Label(val, rowText[j], styleRow);
			num8 += array[j] + 2f * scale;
		}
		if (num5 > 0f)
		{
			GUI.color = new Color(1f, 1f, 1f, 0.6f);
			GUI.Label(new Rect(num6 + num2, num8, num - 2f * num2, num5), footerText, styleSmall);
		}
		GUI.color = Color.white;
	}

	private static bool ToScreen(Camera cam, Vector3 world, out Vector2 screen, out bool inFront)
	{
		//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_003b: 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)
		Vector3 val = cam.WorldToViewportPoint(world);
		inFront = val.z > 0f;
		screen = new Vector2(val.x * (float)Screen.width, (1f - val.y) * (float)Screen.height);
		if (inFront && val.x >= 0f && val.x <= 1f && val.y >= 0f)
		{
			return val.y <= 1f;
		}
		return false;
	}

	private void DrawWorldMarkers(BoxScanner s, Camera cam)
	{
		//IL_012d: 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_0056: 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_0060: 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_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_00d6: 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_00f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_0111: 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)
		float scale = Scale;
		usedLabels.Clear();
		Rect val = default(Rect);
		for (int i = 0; i < s.Boxes.Count; i++)
		{
			BoxEntry boxEntry = s.Boxes[i];
			if (boxEntry.InstanceId == SelectedId || !ToScreen(cam, boxEntry.Position, out var screen, out var _))
			{
				continue;
			}
			float num = 20f * scale;
			Color c = BoxScanner.RarityColor(boxEntry.Rarity);
			DrawStar(screen, num, c);
			((Rect)(ref val))..ctor(screen.x - 80f * scale, screen.y + num * 0.6f, 160f * scale, 16f * scale);
			for (int j = 0; j < 8; j++)
			{
				if (!Overlaps(val))
				{
					break;
				}
				((Rect)(ref val)).y = ((Rect)(ref val)).y + 15f * scale;
			}
			usedLabels.Add(val);
			ShadowLabel(val, $"#{boxEntry.Number} {BoxScanner.RarityName(boxEntry.Rarity)} {boxEntry.Distance:F0}m", styleMarker, c);
		}
		GUI.color = Color.white;
	}

	private void DrawStar(Vector2 p, float size, Color c)
	{
		//IL_0026: 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_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0096: Unknown result type (might be due to invalid IL or missing references)
		float num = Mathf.Max(3f, size * 0.18f);
		GUI.color = new Color(0f, 0f, 0f, 0.8f);
		GUI.DrawTexture(new Rect(p.x - (size + num) / 2f, p.y - (size + num) / 2f + num * 0.15f, size + num, size + num), (Texture)(object)texStar);
		GUI.color = c;
		GUI.DrawTexture(new Rect(p.x - size / 2f, p.y - size / 2f, size, size), (Texture)(object)texStar);
	}

	private bool Overlaps(Rect r)
	{
		//IL_000b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0013: Unknown result type (might be due to invalid IL or missing references)
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		for (int i = 0; i < usedLabels.Count; i++)
		{
			Rect val = usedLabels[i];
			if (Mathf.Abs(((Rect)(ref val)).center.x - ((Rect)(ref r)).center.x) < 110f * Scale && Mathf.Abs(((Rect)(ref val)).center.y - ((Rect)(ref r)).center.y) < 14f * Scale)
			{
				return true;
			}
		}
		return false;
	}

	private void DrawNavigation(BoxScanner s, Camera cam)
	{
		//IL_0022: Unknown result type (might be due to invalid IL or missing references)
		//IL_0027: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Unknown result type (might be due to invalid IL or missing references)
		//IL_002f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: 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_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a2: 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_00dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_031c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0327: Unknown result type (might be due to invalid IL or missing references)
		//IL_032c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0331: Unknown result type (might be due to invalid IL or missing references)
		//IL_0339: Unknown result type (might be due to invalid IL or missing references)
		//IL_033e: Unknown result type (might be due to invalid IL or missing references)
		//IL_034c: Unknown result type (might be due to invalid IL or missing references)
		//IL_034e: Unknown result type (might be due to invalid IL or missing references)
		//IL_037f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0381: Unknown result type (might be due to invalid IL or missing references)
		//IL_0383: Unknown result type (might be due to invalid IL or missing references)
		//IL_045a: Unknown result type (might be due to invalid IL or missing references)
		//IL_04c4: Unknown result type (might be due to invalid IL or missing references)
		//IL_04ce: Unknown result type (might be due to invalid IL or missing references)
		//IL_04d4: Unknown result type (might be due to invalid IL or missing references)
		//IL_0231: Unknown result type (might be due to invalid IL or missing references)
		//IL_0233: Unknown result type (might be due to invalid IL or missing references)
		//IL_023e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0243: Unknown result type (might be due to invalid IL or missing references)
		//IL_0248: Unknown result type (might be due to invalid IL or missing references)
		//IL_0274: Unknown result type (might be due to invalid IL or missing references)
		//IL_0279: Unknown result type (might be due to invalid IL or missing references)
		//IL_027d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0284: Unknown result type (might be due to invalid IL or missing references)
		//IL_02ca: Unknown result type (might be due to invalid IL or missing references)
		//IL_02da: Unknown result type (might be due to invalid IL or missing references)
		//IL_0304: Unknown result type (might be due to invalid IL or missing references)
		//IL_0314: Unknown result type (might be due to invalid IL or missing references)
		if (SelectedId == 0)
		{
			return;
		}
		BoxEntry boxEntry = s.FindById(SelectedId);
		if (boxEntry == null)
		{
			return;
		}
		float scale = Scale;
		Color val = BoxScanner.RarityColor(boxEntry.Rarity);
		if (ToScreen(cam, boxEntry.Position, out var screen, out var _))
		{
			float num = 34f * scale;
			float num2 = 1f + 0.15f * Mathf.Sin(Time.unscaledTime * 6f);
			DrawStar(screen, num * num2, val);
			ShadowLabel(new Rect(screen.x - 120f * scale, screen.y + num, 240f * scale, 24f * scale), $"#{boxEntry.Number} {BoxScanner.RarityName(boxEntry.Rarity).ToUpperInvariant()}  {boxEntry.Distance:F1} m", styleNav, val);
		}
		else
		{
			Vector3 val2 = ((Component)cam).transform.InverseTransformPoint(boxEntry.Position);
			Vector2 val3 = default(Vector2);
			((Vector2)(ref val3))..ctor(val2.x, 0f - val2.y);
			if (val2.z < 0f && ((Vector2)(ref val3)).sqrMagnitude < 0.0001f)
			{
				((Vector2)(ref val3))..ctor(0f, 1f);
			}
			if (val2.z < 0f && Mathf.Abs(val3.x) < Mathf.Abs(val3.y) * 0.25f)
			{
				val3.x = ((val3.x >= 0f) ? Mathf.Abs(val3.y) : (0f - Mathf.Abs(val3.y)));
			}
			((Vector2)(ref val3)).Normalize();
			Vector2 val4 = default(Vector2);
			((Vector2)(ref val4))..ctor((float)Screen.width / 2f, (float)Screen.height / 2f);
			float num3 = 60f * scale;
			float num4 = ((val3.x != 0f) ? (((float)Screen.width / 2f - num3) / Mathf.Abs(val3.x)) : float.MaxValue);
			float num5 = ((val3.y != 0f) ? (((float)Screen.height / 2f - num3) / Mathf.Abs(val3.y)) : float.MaxValue);
			Vector2 val5 = val4 + val3 * Mathf.Min(num4, num5);
			float num6 = Mathf.Atan2(val3.y, val3.x) * 57.29578f + 90f;
			float num7 = 36f * scale;
			Matrix4x4 matrix = GUI.matrix;
			GUIUtility.RotateAroundPivot(num6, val5);
			GUI.color = Color.black;
			GUI.DrawTexture(new Rect(val5.x - num7 / 2f - 2f, val5.y - num7 / 2f - 2f, num7 + 4f, num7 + 4f), (Texture)(object)texArrow);
			GUI.color = val;
			GUI.DrawTexture(new Rect(val5.x - num7 / 2f, val5.y - num7 / 2f, num7, num7), (Texture)(object)texArrow);
			GUI.matrix = matrix;
		}
		Vector3 val6 = boxEntry.Position - ((Component)cam).transform.position;
		Vector3 forward = ((Component)cam).transform.forward;
		forward.y = 0f;
		Vector3 val7 = val6;
		val7.y = 0f;
		string text = "";
		if (((Vector3)(ref forward)).sqrMagnitude > 0.001f && ((Vector3)(ref val7)).sqrMagnitude > 0.25f)
		{
			float num8 = Vector3.SignedAngle(forward, val7, Vector3.up);
			text = ((Mathf.Abs(num8) < 15f) ? "straight ahead" : ((!(Mathf.Abs(num8) > 150f)) ? string.Format("{0:F0}° {1}", Mathf.Abs(num8), (num8 < 0f) ? "left" : "right") : "behind you"));
		}
		string text2 = ((boxEntry.HeightDelta > 2.5f) ? $"{boxEntry.HeightDelta:F1} m above" : ((boxEntry.HeightDelta < -2.5f) ? $"{0f - boxEntry.HeightDelta:F1} m below" : "same level"));
		string text3 = $"TRACKING #{boxEntry.Number} {BoxScanner.RarityName(boxEntry.Rarity).ToUpperInvariant()}  |  {boxEntry.Distance:F1} m  |  {text}  |  {text2}  |  {boxEntry.Location}";
		float num9 = 26f * scale;
		Rect r = default(Rect);
		((Rect)(ref r))..ctor(0f, (float)Screen.height - 120f * scale, (float)Screen.width, num9);
		ShadowLabel(r, text3, styleNav, val);
		GUI.color = Color.white;
	}

	private void ShadowLabel(Rect r, string text, GUIStyle st, Color c)
	{
		//IL_0014: 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_0052: 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)
		GUI.color = new Color(0f, 0f, 0f, 0.85f);
		GUI.Label(new Rect(((Rect)(ref r)).x + 1f, ((Rect)(ref r)).y + 1f, ((Rect)(ref r)).width, ((Rect)(ref r)).height), text, st);
		GUI.color = c;
		GUI.Label(r, text, st);
	}

	private void FillRect(Rect r, Color c)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_0012: Unknown result type (might be due to invalid IL or missing references)
		GUI.color = c;
		GUI.DrawTexture(r, (Texture)(object)texWhite);
		GUI.color = Color.white;
	}

	private void EnsureStyles()
	{
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a4: Expected O, but got Unknown
		//IL_00d2: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00fd: Expected O, but got Unknown
		//IL_011f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0148: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: Expected O, but got Unknown
		//IL_0170: Unknown result type (might be due to invalid IL or missing references)
		//IL_017a: Expected O, but got Unknown
		//IL_01b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ba: Expected O, but got Unknown
		//IL_01fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0206: Expected O, but got Unknown
		//IL_020d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0217: Expected O, but got Unknown
		float scale = Scale;
		if (styleRow == null || !Mathf.Approximately(scale, lastScale))
		{
			lastScale = scale;
			if ((Object)(object)texWhite == (Object)null)
			{
				texWhite = Texture2D.whiteTexture;
			}
			if ((Object)(object)texArrow == (Object)null)
			{
				texArrow = MakeArrow(64);
			}
			if ((Object)(object)texStar == (Object)null)
			{
				texStar = MakeStar(64);
			}
			if ((Object)(object)texBoxFallback == (Object)null)
			{
				texBoxFallback = MakeBoxIcon(48);
			}
			styleHeader = new GUIStyle(GUI.skin.label);
			styleHeader.fontSize = Mathf.RoundToInt(15f * scale);
			styleHeader.fontStyle = (FontStyle)1;
			styleHeader.normal.textColor = Color.white;
			styleHeader.wordWrap = false;
			styleRow = new GUIStyle(GUI.skin.label);
			styleRow.fontSize = Mathf.RoundToInt(13f * scale);
			styleRow.normal.textColor = Color.white;
			styleRow.wordWrap = true;
			styleRow.richText = false;
			styleSmall = new GUIStyle(styleRow);
			styleSmall.fontSize = Mathf.RoundToInt(11f * scale);
			styleMarker = new GUIStyle(styleRow);
			styleMarker.fontSize = Mathf.RoundToInt(12f * scale);
			styleMarker.alignment = (TextAnchor)1;
			styleMarker.wordWrap = false;
			styleNav = new GUIStyle(styleRow);
			styleNav.fontSize = Mathf.RoundToInt(16f * scale);
			styleNav.fontStyle = (FontStyle)1;
			styleNav.alignment = (TextAnchor)1;
			styleNav.wordWrap = false;
			styleShadow = new GUIStyle(styleRow);
			styleCount = new GUIStyle(styleRow);
			styleCount.fontSize = Mathf.RoundToInt(24f * scale);
			styleCount.fontStyle = (FontStyle)1;
			styleCount.alignment = (TextAnchor)3;
			styleCount.wordWrap = false;
			builtVersion = -1;
		}
	}

	private static Texture2D MakeArrow(int n)
	{
		//IL_0004: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: Expected O, but got Unknown
		//IL_006e: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		Texture2D val = new Texture2D(n, n, (TextureFormat)4, false);
		((Object)val).hideFlags = (HideFlags)61;
		Color val2 = default(Color);
		((Color)(ref val2))..ctor(1f, 1f, 1f, 0f);
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				float num = (float)i / (float)(n - 1);
				float num2 = (1f - num) * 0.5f;
				float num3 = Mathf.Abs((float)j / (float)(n - 1) - 0.5f);
				val.SetPixel(j, i, (num3 <= num2) ? Color.white : val2);
			}
		}
		val.Apply();
		return val;
	}

	private static Texture2D MakeStar(int n)
	{
		//IL_006d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0074: Expected O, but got Unknown
		//IL_0056: 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_0118: Unknown result type (might be due to invalid IL or missing references)
		Vector2[] array = (Vector2[])(object)new Vector2[10];
		for (int i = 0; i < 10; i++)
		{
			float num = (float)Math.PI / 2f + (float)i * (float)Math.PI / 5f;
			float num2 = ((i % 2 == 0) ? 0.5f : 0.2f);
			ref Vector2 reference = ref array[i];
			reference = new Vector2(0.5f + num2 * Mathf.Cos(num), 0.47f + num2 * Mathf.Sin(num));
		}
		Texture2D val = new Texture2D(n, n, (TextureFormat)4, false);
		((Object)val).hideFlags = (HideFlags)61;
		((Texture)val).filterMode = (FilterMode)1;
		((Texture)val).wrapMode = (TextureWrapMode)1;
		for (int j = 0; j < n; j++)
		{
			for (int k = 0; k < n; k++)
			{
				int num3 = 0;
				for (int l = 0; l < 4; l++)
				{
					for (int m = 0; m < 4; m++)
					{
						if (InPoly(array, ((float)k + ((float)m + 0.5f) / 4f) / (float)n, ((float)j + ((float)l + 0.5f) / 4f) / (float)n))
						{
							num3++;
						}
					}
				}
				val.SetPixel(k, j, new Color(1f, 1f, 1f, (float)num3 / 16f));
			}
		}
		val.Apply();
		return val;
	}

	private static bool InPoly(Vector2[] p, float x, float y)
	{
		bool flag = false;
		int num = 0;
		int num2 = p.Length - 1;
		while (num < p.Length)
		{
			if (p[num].y > y != p[num2].y > y && x < (p[num2].x - p[num].x) * (y - p[num].y) / (p[num2].y - p[num].y) + p[num].x)
			{
				flag = !flag;
			}
			num2 = num++;
		}
		return flag;
	}

	private static Texture2D MakeBoxIcon(int n)
	{
		//IL_0004: Unknown result type (might be due to invalid IL or missing references)
		//IL_000a: Expected O, but got Unknown
		//IL_00ba: 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_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ef: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f4: Unknown result type (might be due to invalid IL or missing references)
		Texture2D val = new Texture2D(n, n, (TextureFormat)4, false);
		((Object)val).hideFlags = (HideFlags)61;
		Color val2 = default(Color);
		((Color)(ref val2))..ctor(1f, 1f, 1f, 0f);
		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < n; j++)
			{
				float num = (float)j / (float)(n - 1);
				float num2 = (float)i / (float)(n - 1);
				bool flag = num > 0.12f && num < 0.88f && num2 > 0.1f && num2 < 0.72f;
				bool flag2 = num > 0.06f && num < 0.94f && num2 >= 0.74f && num2 < 0.9f;
				bool flag3 = flag && num2 > 0.36f && num2 < 0.46f;
				Color val3 = val2;
				if (flag2)
				{
					val3 = Color.white;
				}
				else if (flag3)
				{
					((Color)(ref val3))..ctor(1f, 1f, 1f, 0.55f);
				}
				else if (flag)
				{
					val3 = Color.white;
				}
				val.SetPixel(j, i, val3);
			}
		}
		val.Apply();
		return val;
	}
}