Decompiled source of ShowNPCs v1.0.2

ShowNPCs.dll

Decompiled 2 weeks 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 BepInEx.Logging;
using HarmonyLib;
using Splatform;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("ShowNPCs")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ShowNPCs")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("4d612142-ebf0-4f83-98c9-187f17f8b27a")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace RevealNpcMapPins;

[BepInPlugin("com.Horhpeus.valheim.revealnpcpins", "Reveal NPC Map Pins", "1.0.2")]
public class RevealNpcMapPinsPlugin : BaseUnityPlugin
{
	public const string PluginGUID = "com.Horhpeus.valheim.revealnpcpins";

	public const string PluginName = "Reveal NPC Map Pins";

	public const string PluginVersion = "1.0.2";

	private Harmony _harmony;

	internal static ManualLogSource Log;

	private void Awake()
	{
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_001b: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		_harmony = new Harmony("com.Horhpeus.valheim.revealnpcpins");
		_harmony.PatchAll();
		Log.LogInfo((object)"Reveal NPC Map Pins loaded");
		RevealNPCsConfig.Init(((BaseUnityPlugin)this).Config);
	}

	private void OnDestroy()
	{
		Harmony harmony = _harmony;
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}
}
public static class MinimapHelpers
{
	public static Sprite GetLocationIcon(Minimap minimap, string prefab)
	{
		//IL_0033: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Expected O, but got Unknown
		MethodInfo method = typeof(Minimap).GetMethod("GetLocationIcon", BindingFlags.Instance | BindingFlags.NonPublic);
		if (!(method != null))
		{
			return null;
		}
		return (Sprite)method.Invoke(minimap, new object[1] { prefab });
	}

	public static Vector3 FindClosestToOrigin(List<Vector3> vectors)
	{
		//IL_000b: 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)
		//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_0040: 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_005e: Unknown result type (might be due to invalid IL or missing references)
		if (vectors == null || vectors.Count == 0)
		{
			return Vector3.zero;
		}
		Vector3 result = vectors[0];
		float num = ((Vector3)(ref result)).sqrMagnitude;
		foreach (Vector3 vector in vectors)
		{
			Vector3 current = vector;
			float sqrMagnitude = ((Vector3)(ref current)).sqrMagnitude;
			if (sqrMagnitude < num)
			{
				result = current;
				num = sqrMagnitude;
			}
		}
		return result;
	}

	public static void RemovePinsNearPosition(Minimap minimap, Vector3 position, float tolerance = 1f)
	{
		//IL_001f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0024: Unknown result type (might be due to invalid IL or missing references)
		List<PinData> pins = GetPins(minimap);
		if (pins == null)
		{
			return;
		}
		for (int num = pins.Count - 1; num >= 0; num--)
		{
			PinData val = pins[num];
			if (Vector3.Distance(val.m_pos, position) <= tolerance)
			{
				minimap.RemovePin(val);
			}
		}
	}

	public static List<PinData> GetPins(Minimap minimap)
	{
		FieldInfo field = typeof(Minimap).GetField("m_pins", BindingFlags.Instance | BindingFlags.NonPublic);
		if (field == null)
		{
			return null;
		}
		return (List<PinData>)field.GetValue(minimap);
	}
}
[HarmonyPatch(typeof(Minimap), "Awake")]
public static class Minimap_Awake_ReapplyNpcIcons
{
	[CompilerGenerated]
	private sealed class <>c__DisplayClass2_0
	{
		public string prefab;

		public Func<KeyValuePair<Vector2i, LocationInstance>, bool> <>9__0;

		internal bool <AddNpcPinCoroutine>b__0(KeyValuePair<Vector2i, LocationInstance> kv)
		{
			//IL_0002: Unknown result type (might be due to invalid IL or missing references)
			return kv.Value.m_location.m_prefabName == prefab;
		}
	}

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

		private object <>2__current;

		public string prefab;

		private <>c__DisplayClass2_0 <>8__1;

		public float retryDelay;

		public Minimap minimap;

		private List<Vector3> <positions>5__2;

		private int <prevCount>5__3;

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

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

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

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<>8__1 = null;
			<positions>5__2 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_0117: 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_011d: Unknown result type (might be due to invalid IL or missing references)
			//IL_011e: Unknown result type (might be due to invalid IL or missing references)
			//IL_00d1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00db: Expected O, but got Unknown
			//IL_0132: Unknown result type (might be due to invalid IL or missing references)
			//IL_0143: Unknown result type (might be due to invalid IL or missing references)
			//IL_0150: Unknown result type (might be due to invalid IL or missing references)
			//IL_0156: Unknown result type (might be due to invalid IL or missing references)
			//IL_0193: Unknown result type (might be due to invalid IL or missing references)
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				<>8__1 = new <>c__DisplayClass2_0();
				<>8__1.prefab = prefab;
				<positions>5__2 = new List<Vector3>();
				<prevCount>5__3 = -1;
				break;
			case 1:
				<>1__state = -1;
				break;
			}
			if (<positions>5__2.Count == 0 || <positions>5__2.Count != <prevCount>5__3)
			{
				<positions>5__2 = (from kv in ZoneSystem.instance.m_locationInstances
					where kv.Value.m_location.m_prefabName == <>8__1.prefab
					select kv.Value.m_position).ToList();
				<prevCount>5__3 = <positions>5__2.Count;
				<>2__current = (object)new WaitForSeconds(retryDelay);
				<>1__state = 1;
				return true;
			}
			Vector3 val = MinimapHelpers.FindClosestToOrigin(<positions>5__2);
			if (val == Vector3.zero)
			{
				return false;
			}
			MinimapHelpers.RemovePinsNearPosition(minimap, val);
			PinData obj = minimap.AddPin(val, (PinType)8, "", true, false, 0L, default(PlatformUserID));
			obj.m_icon = MinimapHelpers.GetLocationIcon(minimap, <>8__1.prefab);
			obj.m_doubleSize = true;
			RevealNpcMapPinsPlugin.Log.LogInfo((object)$"Added pin for {<>8__1.prefab} at {val}");
			return false;
		}

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

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

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

		private object <>2__current;

		private Minimap <minimap>5__2;

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

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

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

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<minimap>5__2 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				goto IL_0043;
			case 1:
				<>1__state = -1;
				goto IL_0043;
			case 2:
				<>1__state = -1;
				goto IL_00c7;
			case 3:
				<>1__state = -1;
				goto IL_0109;
			case 4:
				{
					<>1__state = -1;
					break;
				}
				IL_0043:
				if ((Object)(object)ZoneSystem.instance == (Object)null || ZoneSystem.instance.m_locationInstances == null || ZoneSystem.instance.m_locationInstances.Count == 0 || (Object)(object)Minimap.instance == (Object)null)
				{
					<>2__current = null;
					<>1__state = 1;
					return true;
				}
				<minimap>5__2 = Minimap.instance;
				if (RevealNPCsConfig.RevealTrader.Value)
				{
					<>2__current = ((MonoBehaviour)<minimap>5__2).StartCoroutine(AddNpcPinCoroutine(<minimap>5__2, "Vendor_BlackForest"));
					<>1__state = 2;
					return true;
				}
				goto IL_00c7;
				IL_00c7:
				if (RevealNPCsConfig.RevealHildir.Value)
				{
					<>2__current = ((MonoBehaviour)<minimap>5__2).StartCoroutine(AddNpcPinCoroutine(<minimap>5__2, "Hildir_camp"));
					<>1__state = 3;
					return true;
				}
				goto IL_0109;
				IL_0109:
				if (RevealNPCsConfig.RevealBogWitch.Value)
				{
					<>2__current = ((MonoBehaviour)<minimap>5__2).StartCoroutine(AddNpcPinCoroutine(<minimap>5__2, "BogWitch_Camp"));
					<>1__state = 4;
					return true;
				}
				break;
			}
			return false;
		}

		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 static void Postfix(Minimap __instance)
	{
		if (RevealNPCsConfig.EnableRevealNPCs.Value)
		{
			((MonoBehaviour)__instance).StartCoroutine(WaitForLocationsThenAddPins());
		}
	}

	[IteratorStateMachine(typeof(<WaitForLocationsThenAddPins>d__1))]
	public static IEnumerator WaitForLocationsThenAddPins()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <WaitForLocationsThenAddPins>d__1(0);
	}

	[IteratorStateMachine(typeof(<AddNpcPinCoroutine>d__2))]
	private static IEnumerator AddNpcPinCoroutine(Minimap minimap, string prefab, float retryDelay = 1f)
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <AddNpcPinCoroutine>d__2(0)
		{
			minimap = minimap,
			prefab = prefab,
			retryDelay = retryDelay
		};
	}
}
public class RevealNPCsConfig
{
	public static ConfigEntry<bool> EnableRevealNPCs { get; set; }

	public static ConfigEntry<bool> RevealTrader { get; set; }

	public static ConfigEntry<bool> RevealHildir { get; set; }

	public static ConfigEntry<bool> RevealBogWitch { get; set; }

	public static void Init(ConfigFile config)
	{
		EnableRevealNPCs = config.Bind<bool>("General", "EnableRevealNPCs", true, "Enable revealing NPC map pins");
		RevealTrader = config.Bind<bool>("NPCs", "RevealTrader", true, "Reveal the trader's map pin");
		RevealHildir = config.Bind<bool>("NPCs", "RevealHildir", true, "Reveal Hildir's map pin");
		RevealBogWitch = config.Bind<bool>("NPCs", "RevealBogWitch", true, "Reveal the Bog Witch's map pin");
	}
}