Decompiled source of RadiusDetectionPatchForPinAssistant v1.1.0

BepInEx/plugins/PinAssistantRadiusPatch.dll

Decompiled 4 days ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using UnityEngine;
using WxAxW.PinAssistant.Core;
using WxAxW.PinAssistant.Utils;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("PinAssistantRadiusPatch")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("PinAssistantRadiusPatch")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("1e2fbbcd-1b9c-4956-8b09-1d343d666154")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace PinAssistantRadiusPatch;

[BepInPlugin("Mervil.pinassistantradiuspatch", "PinAssistant Radius Detection Patch", "1.0.0")]
public class MervilsRadiusPlugin : BaseUnityPlugin
{
	internal static ConfigEntry<bool> radiusDetectionEnabled;

	internal static ConfigEntry<float> radiusDetectionInterval;

	internal static ConfigEntry<bool> useFullExplorationRadius;

	internal static ConfigEntry<float> radiusDetectionRadius;

	internal static ConfigEntry<KeyboardShortcut> radiusDetectionToggleKey;

	internal static ConfigEntry<bool> radiusDebugMode;

	private void Awake()
	{
		//IL_00ac: 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_00fb: Expected O, but got Unknown
		radiusDetectionEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "RadiusDetectionEnabled", true, "Enable radius-based object detection. If false, only direct visualization detection will be used.");
		radiusDetectionInterval = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RadiusDetectionInterval", 1f, "Time in seconds between automatic radius detection scans.");
		useFullExplorationRadius = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "UsePlayerExploreRadius", true, "If true, radius detection uses the player's current map exploration radius. If false, it uses the configured RadiusDetectionRadius.");
		radiusDetectionRadius = ((BaseUnityPlugin)this).Config.Bind<float>("General", "RadiusDetectionRadius", 50f, "The radius around the player to detect pins (in Unity units, ~meters).");
		radiusDetectionToggleKey = ((BaseUnityPlugin)this).Config.Bind<KeyboardShortcut>("Hotkeys", "RadiusDetectionToggleKey", new KeyboardShortcut((KeyCode)292, (KeyCode[])(object)new KeyCode[1] { (KeyCode)306 }), "Hotkey to toggle radius detection on/off.");
		radiusDebugMode = ((BaseUnityPlugin)this).Config.Bind<bool>("Debug", "RadiusDebugMode", false, "Enable debug logging.");
		Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "Mervil.pinassistantradiuspatch");
		GameObject val = new GameObject("RadiusPinDetector");
		val.AddComponent<RadiusPinDetector>();
		Object.DontDestroyOnLoad((Object)(object)val);
	}
}
public class RadiusPinDetector : MonoBehaviour
{
	private class PinState
	{
		public string NormalizedName;

		public ZDOID ZdoID;

		public bool Mobile;

		public Vector3 LastPosition;

		public GameObject PinnedObject;

		public PinData Pin;
	}

	[CompilerGenerated]
	private sealed class <RadiusDetectorCoroutine>d__24 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		public RadiusPinDetector <>4__this;

		private Type <trackingAssistantType>5__1;

		private Type <modConfigType>5__2;

		private Assembly <pinAsm>5__3;

		private PropertyInfo <trackAssistInstanceProp>5__4;

		private PropertyInfo <modConfigInstanceProp>5__5;

		private Type <trackObjectUIType>5__6;

		private List<PinData> <pinsList>5__7;

		private HashSet<string> <allowedNames>5__8;

		private GameObject[] <allObjects>5__9;

		private int <currentIndex>5__10;

		private int <allObjectsLength>5__11;

		private Assembly[] <>s__12;

		private int <>s__13;

		private Assembly <asm>5__14;

		private long <localPlayerID>5__15;

		private List<PinData>.Enumerator <>s__16;

		private PinData <pin>5__17;

		private float <batchStartTime>5__18;

		private int <batchSize>5__19;

		private Vector3 <playerPos>5__20;

		private float <scanRadius>5__21;

		private float <elapsed>5__22;

		private float <waitTime>5__23;

		private HashSet<string> <currentAltKeys>5__24;

		private HashSet<string> <normalizedKeys>5__25;

		private HashSet<string>.Enumerator <>s__26;

		private string <key>5__27;

		private Dictionary<ZDO, ZNetView> <instanceDict>5__28;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <RadiusDetectorCoroutine>d__24(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<trackingAssistantType>5__1 = null;
			<modConfigType>5__2 = null;
			<pinAsm>5__3 = null;
			<trackAssistInstanceProp>5__4 = null;
			<modConfigInstanceProp>5__5 = null;
			<trackObjectUIType>5__6 = null;
			<pinsList>5__7 = null;
			<allowedNames>5__8 = null;
			<allObjects>5__9 = null;
			<>s__12 = null;
			<asm>5__14 = null;
			<>s__16 = default(List<PinData>.Enumerator);
			<pin>5__17 = null;
			<currentAltKeys>5__24 = null;
			<normalizedKeys>5__25 = null;
			<>s__26 = default(HashSet<string>.Enumerator);
			<key>5__27 = null;
			<instanceDict>5__28 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_0ce5: Unknown result type (might be due to invalid IL or missing references)
			//IL_0d2f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0d7a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0d84: Expected O, but got Unknown
			//IL_0936: Unknown result type (might be due to invalid IL or missing references)
			//IL_0940: Expected O, but got Unknown
			//IL_0c96: Unknown result type (might be due to invalid IL or missing references)
			//IL_0c7f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0c9b: 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_0248: Expected O, but got Unknown
			//IL_0807: Unknown result type (might be due to invalid IL or missing references)
			//IL_082c: Unknown result type (might be due to invalid IL or missing references)
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				goto IL_0259;
			case 1:
				<>1__state = -1;
				goto IL_0259;
			case 2:
				<>1__state = -1;
				goto IL_0d95;
			case 3:
				<>1__state = -1;
				break;
			case 4:
				{
					<>1__state = -1;
					goto IL_0d95;
				}
				IL_0259:
				if ((Object)(object)<>4__this.m_player == (Object)null || (Object)(object)<>4__this.m_minimap == (Object)null || (Object)(object)<>4__this.znetScene == (Object)null)
				{
					if ((Object)(object)<>4__this.m_player == (Object)null && (Object)(object)Player.m_localPlayer != (Object)null)
					{
						<>4__this.m_player = Player.m_localPlayer;
						if (MervilsRadiusPlugin.radiusDebugMode.Value)
						{
							Debug.Log((object)("[RadiusPinDetector][INFO] Player found: " + ((Object)<>4__this.m_player).name));
						}
					}
					if ((Object)(object)<>4__this.m_minimap == (Object)null && (Object)(object)Minimap.instance != (Object)null)
					{
						<>4__this.m_minimap = Minimap.instance;
						<>4__this.exploreRadiusField = typeof(Minimap).GetField("m_exploreRadius", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
						<>4__this.exploreRadiusProp = typeof(Minimap).GetProperty("m_exploreRadius", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
						<>4__this.minimapPinsField = typeof(Minimap).GetField("m_pins", BindingFlags.Instance | BindingFlags.NonPublic);
						if (MervilsRadiusPlugin.radiusDebugMode.Value)
						{
							Debug.Log((object)"[RadiusPinDetector][INFO] Minimap instance found.");
						}
					}
					if ((Object)(object)<>4__this.znetScene == (Object)null && (Object)(object)ZNetScene.instance != (Object)null)
					{
						<>4__this.znetScene = ZNetScene.instance;
						<>4__this.znetInstancesField = typeof(ZNetScene).GetField("m_instances", BindingFlags.Instance | BindingFlags.NonPublic);
						if (MervilsRadiusPlugin.radiusDebugMode.Value)
						{
							Debug.Log((object)"[RadiusPinDetector][INFO] ZNetScene.instance is ready!");
						}
					}
					if (((Object)(object)<>4__this.m_player == (Object)null || (Object)(object)<>4__this.m_minimap == (Object)null || (Object)(object)<>4__this.znetScene == (Object)null) && MervilsRadiusPlugin.radiusDebugMode.Value)
					{
						Debug.Log((object)"[RadiusPinDetector][INFO] Waiting for asset initiation...");
					}
					<>2__current = (object)new WaitForSeconds(5f);
					<>1__state = 1;
					return true;
				}
				<trackingAssistantType>5__1 = null;
				<modConfigType>5__2 = null;
				<pinAsm>5__3 = PinAssistantReflectionHelper.GetPinAssistantAssembly();
				if (<pinAsm>5__3 != null)
				{
					<trackingAssistantType>5__1 = <pinAsm>5__3.GetType("WxAxW.PinAssistant.Core.TrackingAssistant");
					<modConfigType>5__2 = <pinAsm>5__3.GetType("WxAxW.PinAssistant.Configuration.ModConfig");
				}
				else
				{
					if (MervilsRadiusPlugin.radiusDebugMode.Value)
					{
						Debug.LogWarning((object)"[RadiusPinDetector][WARN] PinAssistant assembly not found via helper in Coroutine. Attempting full assembly scan.");
					}
					<>s__12 = AppDomain.CurrentDomain.GetAssemblies();
					for (<>s__13 = 0; <>s__13 < <>s__12.Length; <>s__13++)
					{
						<asm>5__14 = <>s__12[<>s__13];
						if (<trackingAssistantType>5__1 == null)
						{
							<trackingAssistantType>5__1 = <asm>5__14.GetType("WxAxW.PinAssistant.Core.TrackingAssistant");
						}
						if (<modConfigType>5__2 == null)
						{
							<modConfigType>5__2 = <asm>5__14.GetType("WxAxW.PinAssistant.Configuration.ModConfig");
						}
						if (<trackingAssistantType>5__1 != null && <modConfigType>5__2 != null)
						{
							break;
						}
						<asm>5__14 = null;
					}
					<>s__12 = null;
				}
				if (<trackingAssistantType>5__1 == null)
				{
					Debug.LogError((object)"[RadiusPinDetector][ERROR] Could not find WxAxW.PinAssistant.Core.TrackingAssistant type.");
					return false;
				}
				if (<modConfigType>5__2 == null)
				{
					Debug.LogError((object)"[RadiusPinDetector][ERROR] Could not find WxAxW.PinAssistant.Core.ModConfig type.");
					return false;
				}
				<trackAssistInstanceProp>5__4 = <trackingAssistantType>5__1.GetProperty("Instance", BindingFlags.Static | BindingFlags.Public);
				if (<trackAssistInstanceProp>5__4 == null)
				{
					Debug.LogError((object)"[RadiusPinDetector][ERROR] TrackingAssistant.Instance property not found.");
					return false;
				}
				PinAssistantReflectionHelper.TrackingAssistantInstance = <trackAssistInstanceProp>5__4.GetValue(null);
				if (PinAssistantReflectionHelper.TrackingAssistantInstance == null)
				{
					Debug.LogError((object)"[RadiusPinDetector][ERROR] TrackingAssistant.Instance is null.");
					return false;
				}
				<modConfigInstanceProp>5__5 = <modConfigType>5__2.GetProperty("Instance", BindingFlags.Static | BindingFlags.Public);
				if (<modConfigInstanceProp>5__5 == null)
				{
					Debug.LogError((object)"[RadiusPinDetector][ERROR] ModConfig.Instance property not found.");
					return false;
				}
				PinAssistantReflectionHelper.ModConfigInstance = <modConfigInstanceProp>5__5.GetValue(null);
				if (PinAssistantReflectionHelper.ModConfigInstance == null)
				{
					Debug.LogError((object)"[RadiusPinDetector][ERROR] ModConfig.Instance is null.");
					return false;
				}
				PinAssistantReflectionHelper.AddObjAsPinMethod = <trackingAssistantType>5__1.GetMethod("AddObjAsPin", BindingFlags.Instance | BindingFlags.Public);
				if (PinAssistantReflectionHelper.AddObjAsPinMethod == null)
				{
					Debug.LogError((object)"[RadiusPinDetector][ERROR] AddObjAsPin method not found on TrackingAssistant.");
					return false;
				}
				PinAssistantReflectionHelper.AddObjAsPinDelegate = delegate(string name, GameObject gameObj, float float1, float float2)
				{
					PinAssistantReflectionHelper.AddObjAsPinMethod.Invoke(PinAssistantReflectionHelper.TrackingAssistantInstance, new object[4] { name, gameObj, float1, float2 });
				};
				PinAssistantReflectionHelper.TrackedObjectsField = <trackingAssistantType>5__1.GetField("m_trackedObjects", BindingFlags.Instance | BindingFlags.NonPublic);
				if (PinAssistantReflectionHelper.TrackedObjectsField == null)
				{
					Debug.LogError((object)"[RadiusPinDetector][ERROR] m_trackedObjects field not found on TrackingAssistant.");
					return false;
				}
				PinAssistantReflectionHelper.TrackedObjectsInstance = PinAssistantReflectionHelper.TrackedObjectsField.GetValue(PinAssistantReflectionHelper.TrackingAssistantInstance);
				if (PinAssistantReflectionHelper.TrackedObjectsInstance == null)
				{
					if (MervilsRadiusPlugin.radiusDebugMode.Value)
					{
						Debug.LogWarning((object)"[RadiusPinDetector][WARN] m_trackedObjects from TrackingAssistant is null.");
					}
					return false;
				}
				PinAssistantReflectionHelper.AltDictionaryProperty = PinAssistantReflectionHelper.TrackedObjectsInstance.GetType().GetProperty("AltDictionary", BindingFlags.Instance | BindingFlags.Public);
				if (PinAssistantReflectionHelper.AltDictionaryProperty == null)
				{
					Debug.LogError((object)"[RadiusPinDetector][ERROR] AltDictionary property not found on m_trackedObjects object.");
					return false;
				}
				PinAssistantReflectionHelper.M_PinsField = <trackingAssistantType>5__1.GetField("m_pins", BindingFlags.Instance | BindingFlags.NonPublic);
				if (PinAssistantReflectionHelper.M_PinsField == null)
				{
					Debug.LogError((object)"[RadiusPinDetector][ERROR] Could not find TrackingAssistant.m_pins field via reflection. PinAssistantReflectionHelper not fully initialized.");
					return false;
				}
				PinAssistantReflectionHelper.RedundancySameProp = <modConfigType>5__2.GetProperty("RedundancyDistanceSameConfig", BindingFlags.Instance | BindingFlags.Public);
				if (PinAssistantReflectionHelper.RedundancySameProp == null)
				{
					Debug.LogError((object)"[RadiusPinDetector][ERROR] RedundancyDistanceSameConfig property not found on ModConfig.");
					return false;
				}
				PinAssistantReflectionHelper.RedundancyAnyProp = <modConfigType>5__2.GetProperty("RedundancyDistanceAnyConfig", BindingFlags.Instance | BindingFlags.Public);
				if (PinAssistantReflectionHelper.RedundancyAnyProp == null)
				{
					Debug.LogError((object)"[RadiusPinDetector][ERROR] RedundancyDistanceAnyConfig property not found on ModConfig.");
					return false;
				}
				<trackObjectUIType>5__6 = <pinAsm>5__3?.GetType("WxAxW.PinAssistant.Components.TrackObjectUI");
				if (<trackObjectUIType>5__6 == null)
				{
					Debug.LogError((object)"[RadiusPinDetector][ERROR] Could not find WxAxW.PinAssistant.Components.TrackObjectUI type.");
					return false;
				}
				PinAssistantReflectionHelper.TrackObjectUIInstance = <trackObjectUIType>5__6.GetProperty("Instance", BindingFlags.Static | BindingFlags.Public)?.GetValue(null);
				if (PinAssistantReflectionHelper.TrackObjectUIInstance == null)
				{
					Debug.LogError((object)"[RadiusPinDetector][ERROR] TrackObjectUI.Instance property not found or is null.");
					return false;
				}
				PinAssistantReflectionHelper.TrackObjectUI_m_edittingObject = <trackObjectUIType>5__6.GetField("m_edittingObject", BindingFlags.Instance | BindingFlags.NonPublic);
				if (PinAssistantReflectionHelper.TrackObjectUI_m_edittingObject == null)
				{
					Debug.LogError((object)"[RadiusPinDetector][ERROR] TrackObjectUI.m_edittingObject field not found.");
					return false;
				}
				<>4__this.existingPinsForCorrelation.Clear();
				<pinsList>5__7 = <>4__this.GetPinsList();
				if (<pinsList>5__7 != null)
				{
					Player localPlayer = Player.m_localPlayer;
					<localPlayerID>5__15 = ((localPlayer != null) ? localPlayer.GetPlayerID() : 0);
					<>s__16 = <pinsList>5__7.GetEnumerator();
					try
					{
						while (<>s__16.MoveNext())
						{
							<pin>5__17 = <>s__16.Current;
							if ((<pin>5__17.m_ownerID == 0L || <pin>5__17.m_ownerID == <localPlayerID>5__15) && !<>4__this.existingPinsForCorrelation.ContainsKey(<pin>5__17.m_pos))
							{
								<>4__this.existingPinsForCorrelation[<pin>5__17.m_pos] = <pin>5__17;
							}
							<pin>5__17 = null;
						}
					}
					finally
					{
						((IDisposable)<>s__16).Dispose();
					}
					<>s__16 = default(List<PinData>.Enumerator);
					if (MervilsRadiusPlugin.radiusDebugMode.Value)
					{
						Debug.Log((object)$"[RadiusPinDetector][INFO] Collected {<>4__this.existingPinsForCorrelation.Count} existing pins for correlation during scanning.");
					}
				}
				<allowedNames>5__8 = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
				<allObjects>5__9 = Array.Empty<GameObject>();
				<currentIndex>5__10 = 0;
				<allObjectsLength>5__11 = 0;
				goto IL_0d95;
				IL_0d95:
				if ((Object)(object)ZNetScene.instance == (Object)null)
				{
					if (MervilsRadiusPlugin.radiusDebugMode.Value)
					{
						Debug.Log((object)"[RadiusPinDetector][INFO] RadiusDetectorCoroutine: ZNetScene.instance is null, terminating coroutine.");
					}
					return false;
				}
				<batchStartTime>5__18 = Time.realtimeSinceStartup;
				if (!MervilsRadiusPlugin.radiusDetectionEnabled.Value)
				{
					<>2__current = (object)new WaitForSeconds(MervilsRadiusPlugin.radiusDetectionInterval.Value);
					<>1__state = 2;
					return true;
				}
				PinAssistantReflectionHelper.AltDictionaryInstance = PinAssistantReflectionHelper.AltDictionaryProperty.GetValue(PinAssistantReflectionHelper.TrackedObjectsInstance) as Dictionary<string, TrieNode<TrackedObject>>;
				if (PinAssistantReflectionHelper.AltDictionaryInstance != null)
				{
					<currentAltKeys>5__24 = new HashSet<string>(PinAssistantReflectionHelper.AltDictionaryInstance.Keys, StringComparer.OrdinalIgnoreCase);
					<normalizedKeys>5__25 = <currentAltKeys>5__24.Select((string k) => NormalizeName(k)).ToHashSet<string>(StringComparer.OrdinalIgnoreCase);
					if (!<allowedNames>5__8.SetEquals(<normalizedKeys>5__25))
					{
						<allowedNames>5__8.Clear();
						<>s__26 = <normalizedKeys>5__25.GetEnumerator();
						try
						{
							while (<>s__26.MoveNext())
							{
								<key>5__27 = <>s__26.Current;
								<allowedNames>5__8.Add(<key>5__27);
								if (<>4__this.removedPinsForRestoration.ContainsKey(<key>5__27))
								{
									<>4__this.RestoreRemovedPins(<key>5__27);
								}
								<key>5__27 = null;
							}
						}
						finally
						{
							((IDisposable)<>s__26).Dispose();
						}
						<>s__26 = default(HashSet<string>.Enumerator);
						if (MervilsRadiusPlugin.radiusDebugMode.Value)
						{
							Debug.Log((object)string.Format("[RadiusPinDetector][INFO] AltDictionary contains {0} entries: {1}", PinAssistantReflectionHelper.AltDictionaryInstance.Count, string.Join(", ", PinAssistantReflectionHelper.AltDictionaryInstance.Keys)));
							Debug.Log((object)$"[RadiusPinDetector][INFO] allowedNames updated. Total entries: {<allowedNames>5__8.Count}. Pins restored: {<>4__this.removedPinsForRestoration.Count} remaining in cache.");
						}
					}
					<currentAltKeys>5__24 = null;
					<normalizedKeys>5__25 = null;
				}
				else if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.LogWarning((object)"[RadiusPinDetector][WARN] AltDictionary is null! PinAssistant's tracked objects cannot be read.");
				}
				if ((<allObjects>5__9.Length == 0 || <currentIndex>5__10 >= <allObjectsLength>5__11) && (Object)(object)<>4__this.znetScene != (Object)null && <>4__this.znetInstancesField != null)
				{
					<instanceDict>5__28 = <>4__this.znetInstancesField.GetValue(<>4__this.znetScene) as Dictionary<ZDO, ZNetView>;
					if (<instanceDict>5__28 != null)
					{
						<allObjects>5__9 = (from z in <instanceDict>5__28.Values
							where (Object)(object)z != (Object)null && (Object)(object)((Component)z).gameObject != (Object)null
							select ((Component)z).gameObject).ToArray();
					}
					<instanceDict>5__28 = null;
				}
				<currentIndex>5__10 = 0;
				<allObjectsLength>5__11 = <allObjects>5__9.Length;
				<batchSize>5__19 = Mathf.Clamp(<allObjectsLength>5__11 / 60, 50, 200);
				<playerPos>5__20 = (((Object)(object)<>4__this.m_player != (Object)null) ? ((Component)<>4__this.m_player).transform.position : Vector3.zero);
				<scanRadius>5__21 = <>4__this.ResolveScanRadius();
				<>4__this.UpdateRedundancyDistances();
				break;
			}
			if (<currentIndex>5__10 < <allObjectsLength>5__11)
			{
				<>4__this.ProcessAllObjectsInBatches(<allObjects>5__9, ref <currentIndex>5__10, <batchSize>5__19, <allObjectsLength>5__11, <scanRadius>5__21, <playerPos>5__20, <allowedNames>5__8);
				<>2__current = null;
				<>1__state = 3;
				return true;
			}
			<>4__this.CleanUpPinCache(<allowedNames>5__8, <playerPos>5__20, <scanRadius>5__21);
			<elapsed>5__22 = Time.realtimeSinceStartup - <batchStartTime>5__18;
			<waitTime>5__23 = Mathf.Max(0f, MervilsRadiusPlugin.radiusDetectionInterval.Value - <elapsed>5__22);
			<>2__current = (object)new WaitForSeconds(<waitTime>5__23);
			<>1__state = 4;
			return true;
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	private Player m_player;

	private Minimap m_minimap;

	private FieldInfo minimapPinsField;

	private FieldInfo exploreRadiusField;

	private PropertyInfo exploreRadiusProp;

	private ZNetScene znetScene;

	private FieldInfo znetInstancesField;

	private float redundancyDistanceSame = 1f;

	private float redundancyDistanceAny = 1f;

	private float lastRedundancyRefreshTime = 0f;

	private const float redundancyRefreshInterval = 5f;

	private ConfigEntry<float> cachedSameConfigEntry;

	private ConfigEntry<float> cachedAnyConfigEntry;

	private Dictionary<ZDOID, PinState> alreadyPinned = new Dictionary<ZDOID, PinState>();

	private Dictionary<ZDOID, PinState> foundAndNotPinned = new Dictionary<ZDOID, PinState>();

	private Dictionary<string, bool> prefabMobilityCache = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);

	private Dictionary<string, List<PinState>> removedPinsForRestoration = new Dictionary<string, List<PinState>>(StringComparer.OrdinalIgnoreCase);

	private Dictionary<Vector3, PinData> existingPinsForCorrelation = new Dictionary<Vector3, PinData>();

	private Dictionary<Vector3, PinData> nearbyOrphanedPins = new Dictionary<Vector3, PinData>();

	private static readonly Type[] MobileTypes = new Type[3]
	{
		typeof(Humanoid),
		typeof(Character),
		typeof(AnimalAI)
	};

	private static bool IsMobile(GameObject targetObject)
	{
		Type[] mobileTypes = MobileTypes;
		foreach (Type type in mobileTypes)
		{
			if ((Object)(object)targetObject.GetComponent(type) != (Object)null)
			{
				return true;
			}
		}
		return false;
	}

	private void Start()
	{
		((MonoBehaviour)this).StartCoroutine(RadiusDetectorCoroutine());
	}

	private void Update()
	{
		//IL_000d: 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)
		if (MervilsRadiusPlugin.radiusDetectionToggleKey == null)
		{
			return;
		}
		KeyboardShortcut value = MervilsRadiusPlugin.radiusDetectionToggleKey.Value;
		if (((KeyboardShortcut)(ref value)).IsDown())
		{
			MervilsRadiusPlugin.radiusDetectionEnabled.Value = !MervilsRadiusPlugin.radiusDetectionEnabled.Value;
			if (MervilsRadiusPlugin.radiusDebugMode.Value)
			{
				Debug.Log((object)$"[RadiusPinDetector][INFO] Radius detection toggled: {MervilsRadiusPlugin.radiusDetectionEnabled.Value}");
			}
		}
	}

	[IteratorStateMachine(typeof(<RadiusDetectorCoroutine>d__24))]
	private IEnumerator RadiusDetectorCoroutine()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <RadiusDetectorCoroutine>d__24(0)
		{
			<>4__this = this
		};
	}

	private void ProcessAllObjectsInBatches(GameObject[] allObjects, ref int currentIndex, int batchSize, int allObjectsLength, float scanRadius, Vector3 playerPos, HashSet<string> allowedNames)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0009: Unknown result type (might be due to invalid IL or missing references)
		//IL_07d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_07e1: Unknown result type (might be due to invalid IL or missing references)
		//IL_05f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_060a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0618: Unknown result type (might be due to invalid IL or missing references)
		//IL_07fc: Unknown result type (might be due to invalid IL or missing references)
		//IL_0108: 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_0113: Unknown result type (might be due to invalid IL or missing references)
		//IL_0118: Unknown result type (might be due to invalid IL or missing references)
		//IL_0169: 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_016e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0170: Unknown result type (might be due to invalid IL or missing references)
		//IL_0172: Unknown result type (might be due to invalid IL or missing references)
		//IL_018d: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f1: Unknown result type (might be due to invalid IL or missing references)
		//IL_0357: Unknown result type (might be due to invalid IL or missing references)
		//IL_02b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_02bc: Unknown result type (might be due to invalid IL or missing references)
		//IL_02fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_0310: Unknown result type (might be due to invalid IL or missing references)
		//IL_044b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0382: Unknown result type (might be due to invalid IL or missing references)
		//IL_0389: Unknown result type (might be due to invalid IL or missing references)
		//IL_038e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0393: Unknown result type (might be due to invalid IL or missing references)
		//IL_0334: Unknown result type (might be due to invalid IL or missing references)
		//IL_03b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_03bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_0493: Unknown result type (might be due to invalid IL or missing references)
		//IL_0495: Unknown result type (might be due to invalid IL or missing references)
		//IL_04b2: Unknown result type (might be due to invalid IL or missing references)
		//IL_04b7: Unknown result type (might be due to invalid IL or missing references)
		//IL_03d8: Unknown result type (might be due to invalid IL or missing references)
		//IL_03dd: Unknown result type (might be due to invalid IL or missing references)
		//IL_03e6: Unknown result type (might be due to invalid IL or missing references)
		//IL_03fe: Unknown result type (might be due to invalid IL or missing references)
		//IL_0405: Unknown result type (might be due to invalid IL or missing references)
		//IL_040a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0475: Unknown result type (might be due to invalid IL or missing references)
		//IL_0426: Unknown result type (might be due to invalid IL or missing references)
		//IL_04fd: Unknown result type (might be due to invalid IL or missing references)
		//IL_050b: Unknown result type (might be due to invalid IL or missing references)
		//IL_058d: Unknown result type (might be due to invalid IL or missing references)
		//IL_06a1: Unknown result type (might be due to invalid IL or missing references)
		//IL_06f0: Unknown result type (might be due to invalid IL or missing references)
		//IL_06cb: Unknown result type (might be due to invalid IL or missing references)
		//IL_06db: Unknown result type (might be due to invalid IL or missing references)
		//IL_0753: Unknown result type (might be due to invalid IL or missing references)
		//IL_070a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0656: Unknown result type (might be due to invalid IL or missing references)
		//IL_0667: Unknown result type (might be due to invalid IL or missing references)
		//IL_077e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0737: Unknown result type (might be due to invalid IL or missing references)
		//IL_07b4: Unknown result type (might be due to invalid IL or missing references)
		if (currentIndex == 0 && existingPinsForCorrelation.Count > 0)
		{
			nearbyOrphanedPins = existingPinsForCorrelation.Where(delegate(KeyValuePair<Vector3, PinData> pin)
			{
				//IL_0002: Unknown result type (might be due to invalid IL or missing references)
				//IL_0008: Unknown result type (might be due to invalid IL or missing references)
				//IL_000d: 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)
				Vector3 val6 = pin.Key - playerPos;
				return ((Vector3)(ref val6)).sqrMagnitude <= scanRadius * scanRadius;
			}).ToDictionary((KeyValuePair<Vector3, PinData> pin) => pin.Key, (KeyValuePair<Vector3, PinData> pin) => pin.Value);
			if (MervilsRadiusPlugin.radiusDebugMode.Value)
			{
				Debug.Log((object)$"[RadiusPinDetector][INFO] Created nearbyOrphanedPins with {nearbyOrphanedPins.Count} pins within scan radius");
			}
		}
		int num = Math.Min(currentIndex + batchSize, allObjectsLength);
		for (int i = currentIndex; i < num; i++)
		{
			GameObject val = allObjects[i];
			if ((Object)(object)val == (Object)null || !val.activeInHierarchy)
			{
				continue;
			}
			Vector3 val2 = val.transform.position - playerPos;
			if (((Vector3)(ref val2)).sqrMagnitude > scanRadius * scanRadius)
			{
				continue;
			}
			ZNetView component = val.GetComponent<ZNetView>();
			if ((Object)(object)component == (Object)null)
			{
				continue;
			}
			ZDOID val3 = component.GetZDO()?.m_uid ?? ZDOID.None;
			if (val3 == ZDOID.None || (alreadyPinned.TryGetValue(val3, out var value) && !value.Mobile))
			{
				continue;
			}
			if (foundAndNotPinned.TryGetValue(val3, out var value2) && !value2.Mobile)
			{
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.Log((object)$"[RadiusPinDetector][INFO] Skipping '{((Object)val).name}' (ID={val3}) - previously found but not pinned (not mobile).");
				}
				continue;
			}
			string text = NormalizeName(((Object)val).name);
			if (!allowedNames.Contains(text))
			{
				continue;
			}
			bool value3 = false;
			if (!prefabMobilityCache.TryGetValue(text, out value3))
			{
				value3 = IsMobile(val);
				prefabMobilityCache[text] = value3;
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.Log((object)$"[RadiusPinDetector][INFO] Prefab '{text}' detected. Is Mobile: {value3}");
				}
			}
			PinData val4 = null;
			if (nearbyOrphanedPins.Count > 0)
			{
				Vector3 objectPos = val.transform.position;
				KeyValuePair<Vector3, PinData> keyValuePair = nearbyOrphanedPins.FirstOrDefault(delegate(KeyValuePair<Vector3, PinData> kvp)
				{
					//IL_0002: Unknown result type (might be due to invalid IL or missing references)
					//IL_0008: Unknown result type (might be due to invalid IL or missing references)
					//IL_000d: 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)
					Vector3 val5 = kvp.Key - objectPos;
					return ((Vector3)(ref val5)).sqrMagnitude < 0.1f;
				});
				if (keyValuePair.Value != null)
				{
					val4 = keyValuePair.Value;
					existingPinsForCorrelation.Remove(keyValuePair.Key);
					nearbyOrphanedPins.Remove(keyValuePair.Key);
					if (MervilsRadiusPlugin.radiusDebugMode.Value)
					{
						Debug.Log((object)$"[RadiusPinDetector][CORRELATE] Found existing pin for {text} at {keyValuePair.Key}");
					}
				}
			}
			PinState value4 = null;
			bool flag = false;
			if (alreadyPinned.TryGetValue(val3, out value4))
			{
				if (!value4.Mobile)
				{
					continue;
				}
				val2 = val.transform.position - value4.LastPosition;
				if (!(((Vector3)(ref val2)).sqrMagnitude > 1f))
				{
					continue;
				}
				value4.LastPosition = val.transform.position;
				if (value4.Pin != null)
				{
					Vector3 position = val.transform.position;
					value4.Pin.m_pos = new Vector3(position.x, value4.Pin.m_pos.y, position.z);
					if (MervilsRadiusPlugin.radiusDebugMode.Value)
					{
						Debug.Log((object)$"[RadiusPinDetector][INFO] Prefab '{text}' (ID={val3}) moved; pin position updated.");
					}
				}
				continue;
			}
			if (!value3 && foundAndNotPinned.TryGetValue(val3, out value4))
			{
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.Log((object)$"[RadiusPinDetector][INFO] Skipping '{text}' (ID={val3}) - found in foundAndNotPinned (clustered and not mobile).");
				}
				continue;
			}
			value4 = new PinState
			{
				ZdoID = val3,
				NormalizedName = text,
				Mobile = value3,
				LastPosition = val.transform.position,
				PinnedObject = val,
				Pin = val4
			};
			if (val4 != null)
			{
				continue;
			}
			List<PinData> pinsList = GetPinsList();
			if (pinsList == null)
			{
				alreadyPinned.Remove(val3);
				foundAndNotPinned.Remove(val3);
				continue;
			}
			int count = pinsList.Count;
			float num2 = (value3 ? 0f : redundancyDistanceSame);
			float num3 = (value3 ? 0f : redundancyDistanceAny);
			try
			{
				PinAssistantReflectionHelper.AddObjAsPinDelegate?.Invoke(((Object)val).name, val, num2, num3);
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.Log((object)$"[RadiusPinDetector][INFO] Running AddObjAsPin for: {((Object)val).name} (ID={val3}, Mobile={value3}, RedundancySame={num2}, RedundancyAny={num3}). Pins count before PA invoke: {count}, after PA invoke: {pinsList.Count}");
				}
			}
			catch (Exception arg)
			{
				Debug.LogError((object)$"[RadiusPinDetector][ERROR] AddObjAsPin failed for '{((val != null) ? ((Object)val).name : null)}' (ID={val3}): {arg}. Cannot proceed with PinData capture.");
				alreadyPinned.Remove(val3);
				foundAndNotPinned.Remove(val3);
				continue;
			}
			try
			{
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.Log((object)$"[RadiusPinDetector][DEBUG] Searching for PinData for '{((val != null) ? ((Object)val).name : null)}' (ID={val3}) at pos {val.transform.position}. Search range: [{count} - {pinsList.Count}).");
				}
				value4.Pin = CaptureNewlyCreatedPin(val.transform.position, count, pinsList);
				if (value4.Pin != null)
				{
					alreadyPinned[val3] = value4;
					foundAndNotPinned.Remove(val3);
					continue;
				}
				alreadyPinned.Remove(val3);
				if (!value3)
				{
					foundAndNotPinned[val3] = value4;
					if (MervilsRadiusPlugin.radiusDebugMode.Value)
					{
						Debug.Log((object)$"[RadiusPinDetector][INFO] TrackedObject found, but pin was not created for '{((val != null) ? ((Object)val).name : null)}' (ID={val3}). Adding to foundAndNotPinned.");
					}
				}
				else
				{
					foundAndNotPinned.Remove(val3);
					if (MervilsRadiusPlugin.radiusDebugMode.Value)
					{
						Debug.Log((object)$"[RadiusPinDetector][INFO] PinAssistant did not find a pin for mobile object '{((val != null) ? ((Object)val).name : null)}' (ID={val3}). It will be re-evaluated next cycle.");
					}
				}
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.LogWarning((object)$"[RadiusPinDetector][WARN] Could not capture PinData for '{((val != null) ? ((Object)val).name : null)}' (ID={val3}), entry removed/not added to alreadyPinned (PinAssistant might have clustered or failed to add).");
				}
			}
			catch (Exception arg2)
			{
				alreadyPinned.Remove(val3);
				foundAndNotPinned.Remove(val3);
				Debug.LogError((object)$"[RadiusPinDetector][WARN] Failed to capture PinData reference for '{((val != null) ? ((Object)val).name : null)}' (ID={val3}), entry removed/cleaned from tracking: {arg2}");
			}
		}
		currentIndex = num;
	}

	private void CleanUpPinCache(HashSet<string> allowedNames, Vector3 playerPos, float scanRadius)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0008: Unknown result type (might be due to invalid IL or missing references)
		//IL_014b: Unknown result type (might be due to invalid IL or missing references)
		//IL_03ff: Unknown result type (might be due to invalid IL or missing references)
		//IL_0299: Unknown result type (might be due to invalid IL or missing references)
		//IL_0292: 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_01d9: 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)
		//IL_01fb: Unknown result type (might be due to invalid IL or missing references)
		//IL_029e: Unknown result type (might be due to invalid IL or missing references)
		//IL_02a5: Unknown result type (might be due to invalid IL or missing references)
		//IL_02aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_024f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0487: Unknown result type (might be due to invalid IL or missing references)
		//IL_03dc: Unknown result type (might be due to invalid IL or missing references)
		//IL_04b6: Unknown result type (might be due to invalid IL or missing references)
		//IL_030f: 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)
		//IL_031d: 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_0563: Unknown result type (might be due to invalid IL or missing references)
		//IL_0353: Unknown result type (might be due to invalid IL or missing references)
		//IL_058c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0372: Unknown result type (might be due to invalid IL or missing references)
		if ((Object)(object)ZNetScene.instance == (Object)null)
		{
			if (MervilsRadiusPlugin.radiusDebugMode.Value)
			{
				Debug.Log((object)"[RadiusPinDetector][INFO] CleanUpPinCache: ZNetScene.instance is null, skipping cleanup.");
			}
			return;
		}
		HashSet<ZDOID> activeZDOIDs = (from zdo in (znetInstancesField?.GetValue(ZNetScene.instance) as Dictionary<ZDO, ZNetView>)?.Keys
			where zdo != null
			select zdo.m_uid).ToHashSet();
		var list = (from item in alreadyPinned.Select(delegate(KeyValuePair<ZDOID, PinState> pair)
			{
				//IL_0003: Unknown result type (might be due to invalid IL or missing references)
				//IL_0008: Unknown result type (might be due to invalid IL or missing references)
				//IL_0026: Unknown result type (might be due to invalid IL or missing references)
				//IL_003d: 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_0048: Unknown result type (might be due to invalid IL or missing references)
				//IL_004d: Unknown result type (might be due to invalid IL or missing references)
				//IL_00ce: 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_0096: 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_01a7: Unknown result type (might be due to invalid IL or missing references)
				//IL_0144: Unknown result type (might be due to invalid IL or missing references)
				//IL_0194: Unknown result type (might be due to invalid IL or missing references)
				//IL_0182: 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_011f: Unknown result type (might be due to invalid IL or missing references)
				ZDOID key = pair.Key;
				PinState value2 = pair.Value;
				string normalizedName = value2.NormalizedName;
				if (activeZDOIDs != null && !activeZDOIDs.Contains(key))
				{
					Vector3 val2 = value2.LastPosition - playerPos;
					if (((Vector3)(ref val2)).sqrMagnitude <= scanRadius * scanRadius)
					{
						if (MervilsRadiusPlugin.radiusDebugMode.Value)
						{
							Debug.Log((object)$"[RadiusPinDetector][DEBUG] CleanUpPinCache: Pin {key} ('{normalizedName}') for object is DESTROYED and within scan radius. Marking for removal.");
						}
						return new
						{
							Key = key,
							Name = normalizedName,
							IsDestroyed = true,
							ShouldRemove = true,
							Pin = value2.Pin
						};
					}
					return new
					{
						Key = key,
						Name = normalizedName,
						IsDestroyed = true,
						ShouldRemove = false,
						Pin = value2.Pin
					};
				}
				ZNetScene instance = ZNetScene.instance;
				GameObject val3 = ((instance != null) ? instance.FindInstance(key) : null);
				if ((Object)(object)val3 == (Object)null || (Object)(object)val3.gameObject == (Object)null)
				{
					if (!allowedNames.Contains(normalizedName))
					{
						if (MervilsRadiusPlugin.radiusDebugMode.Value)
						{
							Debug.Log((object)$"[RadiusPinDetector][DEBUG] CleanUpPinCache: Pin {key} ('{normalizedName}') for object is UNLOADED and type is NO LONGER ALLOWED (untracked). Marking for removal.");
						}
						return new
						{
							Key = key,
							Name = normalizedName,
							IsDestroyed = false,
							ShouldRemove = true,
							Pin = value2.Pin
						};
					}
					return new
					{
						Key = key,
						Name = normalizedName,
						IsDestroyed = false,
						ShouldRemove = false,
						Pin = value2.Pin
					};
				}
				if (!allowedNames.Contains(normalizedName))
				{
					if (MervilsRadiusPlugin.radiusDebugMode.Value)
					{
						Debug.Log((object)$"[RadiusPinDetector][DEBUG] CleanUpPinCache: Pin {key} ('{normalizedName}') for LOADED object type is NO LONGER ALLOWED (untracked). Marking for removal.");
					}
					return new
					{
						Key = key,
						Name = normalizedName,
						IsDestroyed = false,
						ShouldRemove = true,
						Pin = value2.Pin
					};
				}
				return new
				{
					Key = key,
					Name = normalizedName,
					IsDestroyed = false,
					ShouldRemove = false,
					Pin = value2.Pin
				};
			})
			where item.ShouldRemove
			select item).ToList();
		foreach (var item2 in list)
		{
			if (!alreadyPinned.TryGetValue(item2.Key, out var value))
			{
				continue;
			}
			string name = item2.Name;
			if (value.Pin != null)
			{
				if (!item2.IsDestroyed)
				{
					if (!removedPinsForRestoration.ContainsKey(name))
					{
						removedPinsForRestoration[name] = new List<PinState>();
					}
					PinState item3 = new PinState
					{
						ZdoID = item2.Key,
						NormalizedName = name,
						Mobile = value.Mobile,
						LastPosition = value.LastPosition,
						PinnedObject = value.PinnedObject,
						Pin = value.Pin
					};
					removedPinsForRestoration[name].Add(item3);
					if (MervilsRadiusPlugin.radiusDebugMode.Value)
					{
						Debug.Log((object)$"[RadiusPinDetector][INFO] CleanUpPinCache: Archived pin for '{name}' ({item2.Key}) for potential restoration.");
					}
				}
				else
				{
					Vector3 removedPosition = item2.Pin?.m_pos ?? Vector3.zero;
					if (removedPosition != Vector3.zero)
					{
						List<ZDOID> list2 = (from pair in foundAndNotPinned.Where(delegate(KeyValuePair<ZDOID, PinState> pair)
							{
								//IL_0008: 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)
								//IL_0013: Unknown result type (might be due to invalid IL or missing references)
								//IL_0018: Unknown result type (might be due to invalid IL or missing references)
								Vector3 val = pair.Value.LastPosition - removedPosition;
								float sqrMagnitude = ((Vector3)(ref val)).sqrMagnitude;
								bool flag = pair.Value.NormalizedName == item2.Name && sqrMagnitude <= redundancyDistanceSame * redundancyDistanceSame;
								bool flag2 = sqrMagnitude <= redundancyDistanceAny * redundancyDistanceAny;
								return flag || flag2;
							})
							select pair.Key).ToList();
						foreach (ZDOID item4 in list2)
						{
							string arg = (foundAndNotPinned.ContainsKey(item4) ? foundAndNotPinned[item4].NormalizedName : item2.Name);
							foundAndNotPinned.Remove(item4);
							if (MervilsRadiusPlugin.radiusDebugMode.Value)
							{
								Debug.Log((object)$"[RadiusPinDetector][CLEANUP] Cleared foundAndNotPinned entry for '{arg}' ({item4}) - nearby pinned object was destroyed");
							}
						}
					}
				}
				Minimap.instance.RemovePin(value.Pin);
				value.Pin = null;
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.Log((object)$"[RadiusPinDetector][INFO] CleanUpPinCache: Removed minimap pin and table entry for '{name}' ({item2.Key}) from alreadyPinned.");
				}
			}
			alreadyPinned.Remove(item2.Key);
		}
		var list3 = foundAndNotPinned.Select((KeyValuePair<ZDOID, PinState> pair) => new
		{
			Key = pair.Key,
			Name = pair.Value.NormalizedName
		}).Where(item =>
		{
			//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_002b: 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_00a4: Unknown result type (might be due to invalid IL or missing references)
			if (item.Key == ZDOID.None)
			{
				return true;
			}
			if (activeZDOIDs != null && !activeZDOIDs.Contains(item.Key))
			{
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.Log((object)$"[RadiusPinDetector][DEBUG] CleanUpPinCache: Identifying non-pinned entry {item.Key} ('{item.Name}') for removal because its object is DESTROYED.");
				}
				return true;
			}
			if (!allowedNames.Contains(item.Name))
			{
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.Log((object)$"[RadiusPinDetector][DEBUG] CleanUpPinCache: Identifying non-pinned entry {item.Key} ('{item.Name}') for removal because its type is no longer allowed.");
				}
				return true;
			}
			return false;
		}).ToList();
		foreach (var item5 in list3)
		{
			if (foundAndNotPinned.Remove(item5.Key) && MervilsRadiusPlugin.radiusDebugMode.Value)
			{
				Debug.Log((object)$"[RadiusPinDetector][INFO] CleanUpPinCache: Successfully removed non-pinned entry for '{item5.Name}' ({item5.Key}) from foundAndNotPinned.");
			}
		}
		if (nearbyOrphanedPins.Count <= 0)
		{
			return;
		}
		if (MervilsRadiusPlugin.radiusDebugMode.Value)
		{
			Debug.Log((object)$"[RadiusPinDetector][CLEANUP] Removing {nearbyOrphanedPins.Count} orphaned pins that weren't claimed during scan");
		}
		foreach (PinData value3 in nearbyOrphanedPins.Values)
		{
			Minimap.instance.RemovePin(value3);
			existingPinsForCorrelation.Remove(value3.m_pos);
			if (MervilsRadiusPlugin.radiusDebugMode.Value)
			{
				Debug.Log((object)$"[RadiusPinDetector][CLEANUP] Removed orphaned pin '{value3.m_name}' at {value3.m_pos}");
			}
		}
		nearbyOrphanedPins.Clear();
	}

	private void RestoreRemovedPins(string normalizedName)
	{
		//IL_0078: 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_00c0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ed: Unknown result type (might be due to invalid IL or missing references)
		//IL_0141: Unknown result type (might be due to invalid IL or missing references)
		//IL_0146: Unknown result type (might be due to invalid IL or missing references)
		//IL_015f: 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)
		//IL_01f8: Unknown result type (might be due to invalid IL or missing references)
		if (!removedPinsForRestoration.TryGetValue(normalizedName, out var value))
		{
			return;
		}
		if (MervilsRadiusPlugin.radiusDebugMode.Value)
		{
			Debug.Log((object)$"[RadiusPinDetector][INFO] Restoring {value.Count} pins for retracked object '{normalizedName}'.");
		}
		foreach (PinState item in value)
		{
			if (item.Pin == null)
			{
				continue;
			}
			Minimap.instance.AddPin(item.Pin.m_pos, item.Pin.m_type, item.Pin.m_name, item.Pin.m_save, item.Pin.m_checked, item.Pin.m_ownerID, item.Pin.m_author);
			List<PinData> pinsList = GetPinsList();
			int beforeCount = (pinsList?.Count ?? 1) - 1;
			PinData val = CaptureNewlyCreatedPin(item.Pin.m_pos, beforeCount, pinsList);
			if (val != null)
			{
				item.Pin = val;
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.Log((object)("[RadiusPinDetector][RESTORE] Updated PinState to reference newly created pin for '" + item.NormalizedName + "'"));
				}
			}
			if (item.ZdoID != ZDOID.None)
			{
				alreadyPinned[item.ZdoID] = item;
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.Log((object)$"[RadiusPinDetector][INFO] Re-added archived PinState for '{item.NormalizedName}' (ZDOID={item.ZdoID}) to alreadyPinned.");
				}
			}
			else if (MervilsRadiusPlugin.radiusDebugMode.Value)
			{
				Debug.LogWarning((object)("[RadiusPinDetector][WARN] Archived PinState for '" + item.NormalizedName + "' had invalid ZDOID, not re-added to alreadyPinned."));
			}
			if (MervilsRadiusPlugin.radiusDebugMode.Value)
			{
				Debug.Log((object)$"[RadiusPinDetector][INFO] Restored pin '{item.Pin.m_name}' at {item.Pin.m_pos} for '{normalizedName}'.");
			}
		}
		removedPinsForRestoration.Remove(normalizedName);
		if (MervilsRadiusPlugin.radiusDebugMode.Value)
		{
			Debug.Log((object)("[RadiusPinDetector][INFO] Cleared archived pins for '" + normalizedName + "' from restoration cache."));
		}
	}

	private PinData CaptureNewlyCreatedPin(Vector3 expectedPosition, int beforeCount, List<PinData> pinsList)
	{
		//IL_002d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0032: Unknown result type (might be due to invalid IL or missing references)
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0038: Unknown result type (might be due to invalid IL or missing references)
		if (pinsList == null)
		{
			return null;
		}
		float num = 0.1f;
		float num2 = num * num;
		for (int i = beforeCount; i < pinsList.Count; i++)
		{
			PinData val = pinsList[i];
			if (val != null)
			{
				Vector3 val2 = val.m_pos - expectedPosition;
				if (((Vector3)(ref val2)).sqrMagnitude < num2)
				{
					return val;
				}
			}
		}
		return null;
	}

	private float ResolveScanRadius()
	{
		if (!MervilsRadiusPlugin.useFullExplorationRadius.Value)
		{
			if (MervilsRadiusPlugin.radiusDebugMode.Value)
			{
				Debug.Log((object)$"[RadiusPinDetector][RADIUS] Using configured radius: {MervilsRadiusPlugin.radiusDetectionRadius.Value}");
			}
			return Mathf.Max(1f, MervilsRadiusPlugin.radiusDetectionRadius.Value);
		}
		float lastCapturedRadius = PinAssistantPatches.ValheimMinimapPatches.GetLastCapturedRadius();
		if (MervilsRadiusPlugin.radiusDebugMode.Value)
		{
			Debug.Log((object)$"[RadiusPinDetector][RADIUS] Player's exploration radius is: {lastCapturedRadius}");
		}
		return Mathf.Max(1f, lastCapturedRadius);
	}

	private static string NormalizeName(string raw)
	{
		if (string.IsNullOrEmpty(raw))
		{
			return string.Empty;
		}
		string text = raw.ToLowerInvariant();
		return text.Replace("(clone)", "").Trim();
	}

	private List<PinData> GetPinsList()
	{
		if (minimapPinsField == null)
		{
			Debug.LogError((object)"[RadiusPinDetector][ERROR] GetPinsList: minimapPinsField is null! Reflection failed during initialization.");
			return null;
		}
		if ((Object)(object)m_minimap == (Object)null)
		{
			Debug.LogError((object)"[RadiusPinDetector][ERROR] GetPinsList: m_minimap is null! Minimap instance lost.");
			return null;
		}
		List<PinData> list = minimapPinsField.GetValue(m_minimap) as List<PinData>;
		if (list == null)
		{
			Debug.LogError((object)"[RadiusPinDetector][ERROR] GetPinsList: Reflection returned null. Field exists but value is null. This should never happen!");
			Debug.LogError((object)("[RadiusPinDetector][ERROR] Raw field value: " + (minimapPinsField.GetValue(m_minimap)?.GetType()?.Name ?? "null")));
		}
		return list;
	}

	private void UpdateRedundancyDistances()
	{
		if (Time.time - lastRedundancyRefreshTime < 5f)
		{
			return;
		}
		lastRedundancyRefreshTime = Time.time;
		if (PinAssistantReflectionHelper.RedundancySameProp == null && PinAssistantReflectionHelper.RedundancyAnyProp == null)
		{
			if (MervilsRadiusPlugin.radiusDebugMode.Value)
			{
				Debug.LogWarning((object)"[RadiusPinDetector][INFO] Unable to update redundancy distances -- Properties are null.");
			}
			return;
		}
		float num = redundancyDistanceSame;
		float num2 = redundancyDistanceAny;
		if (cachedSameConfigEntry == null && PinAssistantReflectionHelper.RedundancySameProp != null)
		{
			cachedSameConfigEntry = PinAssistantReflectionHelper.RedundancySameProp.GetValue(PinAssistantReflectionHelper.ModConfigInstance) as ConfigEntry<float>;
		}
		if (cachedAnyConfigEntry == null && PinAssistantReflectionHelper.RedundancyAnyProp != null)
		{
			cachedAnyConfigEntry = PinAssistantReflectionHelper.RedundancyAnyProp.GetValue(PinAssistantReflectionHelper.ModConfigInstance) as ConfigEntry<float>;
		}
		if (cachedSameConfigEntry != null)
		{
			redundancyDistanceSame = cachedSameConfigEntry.Value;
		}
		if (cachedAnyConfigEntry != null)
		{
			redundancyDistanceAny = cachedAnyConfigEntry.Value;
		}
		if (redundancyDistanceSame != num || redundancyDistanceAny != num2)
		{
			foundAndNotPinned.Clear();
			if (MervilsRadiusPlugin.radiusDebugMode.Value)
			{
				Debug.Log((object)$"[RadiusPinDetector][INFO] Redundancy distances changed (old: same={num}, any={num2} | new: same={redundancyDistanceSame}, any={redundancyDistanceAny}). Clearing foundAndNotPinned cache.");
			}
		}
		if (MervilsRadiusPlugin.radiusDebugMode.Value)
		{
			Debug.Log((object)$"[RadiusPinDetector][INFO] Updated redundancy distances: same={redundancyDistanceSame}, any={redundancyDistanceAny}");
		}
	}
}
public static class PinAssistantReflectionHelper
{
	public static MethodInfo AddObjAsPinMethod;

	public static Action<string, GameObject, float, float> AddObjAsPinDelegate;

	public static FieldInfo M_PinsField;

	public static FieldInfo TrackedObjectsField;

	public static FieldInfo TrackObjectUI_m_edittingObject;

	public static PropertyInfo AltDictionaryProperty;

	public static PropertyInfo RedundancySameProp;

	public static PropertyInfo RedundancyAnyProp;

	public static object TrackingAssistantInstance;

	public static object ModConfigInstance;

	public static object TrackObjectUIInstance;

	public static object TrackedObjectsInstance;

	public static Dictionary<string, TrieNode<TrackedObject>> AltDictionaryInstance;

	private static Assembly s_pinAssistantAssembly;

	public static Assembly GetPinAssistantAssembly()
	{
		if (s_pinAssistantAssembly == null)
		{
			s_pinAssistantAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault((Assembly a) => a.GetName().Name == "WxAxW.PinAssistant");
		}
		return s_pinAssistantAssembly;
	}
}
public static class PinAssistantPatches
{
	[HarmonyPatch(typeof(TrackingAssistant))]
	public static class TrackingAssistantPatches
	{
		[HarmonyPrefix]
		[HarmonyPatch("CheckPinPositionExist")]
		public static bool CheckPinPositionExist_Prefix(TrackingAssistant __instance, Vector3 pinPos, ref bool __result, ref bool __runOriginal)
		{
			//IL_00de: Unknown result type (might be due to invalid IL or missing references)
			//IL_012b: Unknown result type (might be due to invalid IL or missing references)
			if (!MervilsRadiusPlugin.radiusDetectionEnabled.Value)
			{
				__runOriginal = true;
				return true;
			}
			long num = 0L;
			bool flag = false;
			if ((Object)(object)Player.m_localPlayer != (Object)null)
			{
				num = Player.m_localPlayer.GetPlayerID();
				flag = true;
			}
			if (!flag)
			{
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.LogWarning((object)"[RadiusPinDetector][WARN] PatchCheckPinPositionExist: Local player not yet available. Letting original method run for now.");
				}
				__runOriginal = true;
				return true;
			}
			if (PinAssistantReflectionHelper.M_PinsField == null)
			{
				Debug.LogError((object)"[RadiusPinDetector][ERROR] PatchCheckPinPositionExist: PinAssistantReflectionHelper.M_PinsField is null. Letting original method run.");
				__runOriginal = true;
				return true;
			}
			if (!(PinAssistantReflectionHelper.M_PinsField.GetValue(__instance) is Dictionary<Vector3, PinData> dictionary))
			{
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.LogWarning((object)"[RadiusPinDetector][WARN] PatchCheckPinPositionExist: PinAssistant's m_pins dictionary is null from helper field. Letting original method run.");
				}
				__runOriginal = true;
				return true;
			}
			if (dictionary.TryGetValue(pinPos, out var value))
			{
				if (value.m_ownerID == 0L || value.m_ownerID == num)
				{
					__result = true;
				}
				else
				{
					if (MervilsRadiusPlugin.radiusDebugMode.Value)
					{
						Debug.Log((object)$"[RadiusPinDetector][DEBUG] PatchCheckPinPositionExist: Ignoring shared pin '{value.m_name}' at {pinPos} (OwnerID={value.m_ownerID}).");
					}
					__result = false;
				}
			}
			else
			{
				__result = false;
			}
			__runOriginal = false;
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch("CheckValidPinPosition")]
		public static bool CheckValidPinPosition_Prefix(TrackingAssistant __instance, Vector3 pinToAdd, string pinName, float redundancyDistance, bool allPins, ref bool __result, ref bool __runOriginal)
		{
			//IL_0136: Unknown result type (might be due to invalid IL or missing references)
			//IL_0139: Unknown result type (might be due to invalid IL or missing references)
			if (!MervilsRadiusPlugin.radiusDetectionEnabled.Value)
			{
				__runOriginal = true;
				return true;
			}
			long num = 0L;
			bool flag = false;
			if ((Object)(object)Player.m_localPlayer != (Object)null)
			{
				num = Player.m_localPlayer.GetPlayerID();
				flag = true;
			}
			if (!flag)
			{
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.LogWarning((object)"[RadiusPinDetector][WARN] PatchCheckValidPinPosition: Local player not yet available. Letting original method run for now.");
				}
				__runOriginal = true;
				return true;
			}
			if (PinAssistantReflectionHelper.M_PinsField == null)
			{
				Debug.LogError((object)"[RadiusPinDetector][ERROR] PatchCheckValidPinPosition: PinAssistantReflectionHelper.M_PinsField is null. Letting original method run.");
				__runOriginal = true;
				return true;
			}
			if (!(PinAssistantReflectionHelper.M_PinsField.GetValue(__instance) is Dictionary<Vector3, PinData> dictionary))
			{
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.LogWarning((object)"[RadiusPinDetector][WARN] PatchCheckValidPinPosition: PinAssistant's m_pins dictionary is null from helper field. Letting original method run.");
				}
				__runOriginal = true;
				return true;
			}
			foreach (PinData value in dictionary.Values)
			{
				if ((value.m_ownerID == 0L || value.m_ownerID == num) && (allPins || value.m_name.IndexOf(pinName, StringComparison.OrdinalIgnoreCase) != -1) && !CheckValidDistance(pinToAdd, value.m_pos, redundancyDistance))
				{
					if (MervilsRadiusPlugin.radiusDebugMode.Value)
					{
						Debug.Log((object)$"[RadiusPinDetector][DEBUG] PatchCheckValidPinPosition: Found redundant pin '{value.m_name}' (OwnerID={value.m_ownerID}).");
					}
					__result = false;
					__runOriginal = false;
					return false;
				}
			}
			__result = true;
			__runOriginal = false;
			return false;
		}

		[HarmonyPrefix]
		[HarmonyPatch("PinLookedObject")]
		public static bool PinLookedObject_Prefix(TrackingAssistant __instance, float lookDistance, float redundancyDistanceSame, float redundancyDistanceAny, ref bool __runOriginal)
		{
			if (MervilsRadiusPlugin.radiusDetectionEnabled.Value)
			{
				__runOriginal = false;
				return false;
			}
			__runOriginal = true;
			return true;
		}

		private static bool CheckValidDistance(Vector3 v1, Vector3 v2, float redundancyDistance)
		{
			//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)
			return Get2DDistanceSquared(v1, v2) > redundancyDistance * redundancyDistance;
		}

		private static float Get2DDistanceSquared(Vector3 v1, Vector3 v2)
		{
			//IL_0001: 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_000f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0015: Unknown result type (might be due to invalid IL or missing references)
			float num = v2.x - v1.x;
			float num2 = v2.z - v1.z;
			return num * num + num2 * num2;
		}
	}

	[HarmonyPatch]
	public static class MinimapAssistantPatches
	{
		[HarmonyTargetMethod]
		private static MethodBase TargetMethod()
		{
			return (PinAssistantReflectionHelper.GetPinAssistantAssembly()?.GetType("WxAxW.PinAssistant.Core.MinimapAssistant"))?.GetMethod("OnTrackedObjectUpdate", BindingFlags.Instance | BindingFlags.NonPublic);
		}

		[HarmonyPostfix]
		public static void OnTrackedObjectUpdate_Postfix(object __instance, object trackedObject, object newTrackedObject, bool modifyPins)
		{
			//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_00e9: Unknown result type (might be due to invalid IL or missing references)
			//IL_0125: Unknown result type (might be due to invalid IL or missing references)
			if (!modifyPins)
			{
				return;
			}
			try
			{
				Type type = newTrackedObject.GetType();
				PropertyInfo property = type.GetProperty("PinColor", BindingFlags.Instance | BindingFlags.Public);
				Color val = (Color)property.GetValue(newTrackedObject);
				PropertyInfo property2 = type.GetProperty("Name", BindingFlags.Instance | BindingFlags.Public);
				PropertyInfo property3 = type.GetProperty("Icon", BindingFlags.Instance | BindingFlags.Public);
				string text = (string)property2.GetValue(newTrackedObject);
				object value = property3.GetValue(newTrackedObject);
				string text2 = text.ToLower() + "_" + value.ToString();
				Type type2 = __instance.GetType();
				FieldInfo field = type2.GetField("m_pins", BindingFlags.Instance | BindingFlags.NonPublic);
				if (field.GetValue(__instance) is IDictionary dictionary && dictionary.Contains(text2))
				{
					object obj = dictionary[text2];
					Type type3 = obj.GetType();
					PropertyInfo property4 = type3.GetProperty("PinColor", BindingFlags.Instance | BindingFlags.Public);
					property4.SetValue(obj, val);
					MethodInfo method = type3.GetMethod("ApplyColor", BindingFlags.Instance | BindingFlags.Public);
					method.Invoke(obj, null);
					if (MervilsRadiusPlugin.radiusDebugMode.Value)
					{
						Debug.Log((object)$"[RadiusPinDetector][INFO] Applied color {val} to PinGroup '{text2}'");
					}
				}
			}
			catch (Exception arg)
			{
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.LogError((object)$"[RadiusPinDetector][ERROR] Color fix failed: {arg}");
				}
			}
		}
	}

	[HarmonyPatch(typeof(Minimap))]
	public static class ValheimMinimapPatches
	{
		private static float lastCapturedRadius = 50f;

		[HarmonyPatch("Explore", new Type[]
		{
			typeof(Vector3),
			typeof(float)
		})]
		[HarmonyPrefix]
		public static void Explore_Prefix(Vector3 p, float radius)
		{
			if (radius != lastCapturedRadius)
			{
				lastCapturedRadius = radius;
				if (MervilsRadiusPlugin.radiusDebugMode.Value)
				{
					Debug.Log((object)$"[RadiusPinDetector][RADIUS-PATCH] REAL radius captured from Explore: {radius}");
				}
			}
		}

		public static float GetLastCapturedRadius()
		{
			return lastCapturedRadius;
		}
	}
}