Decompiled source of ShowNPCs v2.0.0

ShowNPCs.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
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", "2.0.1")]
public class RevealNpcMapPinsPlugin : BaseUnityPlugin
{
	public const string PluginGUID = "com.Horhpeus.valheim.revealnpcpins";

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

	public const string PluginVersion = "2.0.1";

	public const string LogPrefix = "[RevealNPCMapPins] ";

	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
		//IL_002b: Unknown result type (might be due to invalid IL or missing references)
		Log = ((BaseUnityPlugin)this).Logger;
		_harmony = new Harmony("com.Horhpeus.valheim.revealnpcpins");
		_harmony.PatchAll();
		new GameObject("RevealNpcPins_RpcManager").AddComponent<RpcManager>();
		RevealNPCsConfig.Init(((BaseUnityPlugin)this).Config);
		RpcManager.Instance.RunCoroutine(RegisterRPCs());
		Log.LogInfo((object)"[RevealNPCMapPins] Reveal NPC Map Pins loaded");
	}

	private IEnumerator RegisterRPCs()
	{
		while (ZRoutedRpc.instance == null)
		{
			yield return null;
		}
		ZRoutedRpc.instance.Register<Vector3, string>("RevealNpcPin", (Action<long, Vector3, string>)RPC_RevealNpcPin);
		ZRoutedRpc.instance.Register("RequestNpcPins", (Action<long>)RPC_RequestNpcPins);
		Log.LogInfo((object)"[RevealNPCMapPins] RPCs Registered");
	}

	private void OnDestroy()
	{
		Harmony harmony = _harmony;
		if (harmony != null)
		{
			harmony.UnpatchSelf();
		}
	}

	public static void RPC_RevealNpcPin(long sender, Vector3 pos, string prefab)
	{
		//IL_0005: Unknown result type (might be due to invalid IL or missing references)
		RpcManager.Instance.RunCoroutine(AddPinWhenReady(pos, prefab));
	}

	private static IEnumerator AddPinWhenReady(Vector3 pos, string prefab)
	{
		//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)
		while ((Object)(object)Minimap.instance == (Object)null)
		{
			yield return null;
		}
		yield return (object)new WaitForSeconds(0.5f);
		Minimap instance = Minimap.instance;
		MinimapHelpers.RemovePinsNearPosition(instance, pos);
		PinData obj = instance.AddPin(pos, (PinType)8, "", true, false, 0L, default(PlatformUserID));
		obj.m_icon = MinimapHelpers.GetLocationIcon(instance, prefab);
		obj.m_doubleSize = true;
		Log.LogInfo((object)("[RevealNPCMapPins] Added pin for " + prefab));
	}

	public static void RPC_RequestNpcPins(long sender)
	{
		if (ZNet.instance.IsServer())
		{
			Log.LogInfo((object)string.Format("{0}Client {1} requested NPC pins", "[RevealNPCMapPins] ", sender));
			SendPinsToPeer(sender);
		}
	}

	private static void SendPinsToPeer(long uid)
	{
		//IL_000c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Unknown result type (might be due to invalid IL or missing references)
		//IL_0054: Unknown result type (might be due to invalid IL or missing references)
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_0030: Unknown result type (might be due to invalid IL or missing references)
		//IL_009c: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a1: 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_00c0: Unknown result type (might be due to invalid IL or missing references)
		if (RevealNPCsConfig.RevealTrader.Value && MinimapHelpers.VendorPinLocation != Vector3.zero)
		{
			ZRoutedRpc.instance.InvokeRoutedRPC(uid, "RevealNpcPin", new object[2]
			{
				MinimapHelpers.VendorPinLocation,
				"Vendor_BlackForest"
			});
		}
		if (RevealNPCsConfig.RevealHildir.Value && MinimapHelpers.HildirPinLocation != Vector3.zero)
		{
			ZRoutedRpc.instance.InvokeRoutedRPC(uid, "RevealNpcPin", new object[2]
			{
				MinimapHelpers.HildirPinLocation,
				"Hildir_camp"
			});
		}
		if (RevealNPCsConfig.RevealBogWitch.Value && MinimapHelpers.BogWitchPinLocation != Vector3.zero)
		{
			ZRoutedRpc.instance.InvokeRoutedRPC(uid, "RevealNpcPin", new object[2]
			{
				MinimapHelpers.BogWitchPinLocation,
				"BogWitch_Camp"
			});
		}
	}

	public static IEnumerator RequestPinsWhenReady()
	{
		while ((Object)(object)ZNet.instance == (Object)null || !ZNet.instance.IsConnected(ZNet.GetUID()))
		{
			yield return null;
		}
		while ((Object)(object)Player.m_localPlayer == (Object)null)
		{
			yield return null;
		}
		while ((Object)(object)Minimap.instance == (Object)null)
		{
			yield return null;
		}
		yield return (object)new WaitForSeconds(2f);
		Log.LogInfo((object)"[RevealNPCMapPins] Requesting NPC pins from host...");
		ZRoutedRpc.instance.InvokeRoutedRPC(0L, "RequestNpcPins", Array.Empty<object>());
	}
}
[HarmonyPatch(typeof(Game), "Start")]
public static class Game_Start_Patch
{
	private static void Postfix()
	{
		if (!((Object)(object)ZNet.instance == (Object)null) && !ZNet.instance.IsServer())
		{
			RpcManager.Instance.RunCoroutine(RevealNpcMapPinsPlugin.RequestPinsWhenReady());
		}
	}
}
[HarmonyPatch(typeof(ZoneSystem), "Start")]
public static class ZoneSystem_Start_Patch
{
	private static void Postfix()
	{
		if (ZNet.instance.IsServer())
		{
			RpcManager.Instance.RunCoroutine(ComputePins());
		}
	}

	private static IEnumerator ComputePins()
	{
		while ((Object)(object)ZoneSystem.instance == (Object)null || ZoneSystem.instance.m_locationInstances == null || ZoneSystem.instance.m_locationInstances.Count == 0)
		{
			yield return (object)new WaitForSeconds(2f);
		}
		Vector3 val = Vector3.zero;
		Vector3 val2 = Vector3.zero;
		Vector3 val3 = Vector3.zero;
		float num = float.MaxValue;
		float num2 = float.MaxValue;
		float num3 = float.MaxValue;
		foreach (KeyValuePair<Vector2s, LocationInstance> locationInstance in ZoneSystem.instance.m_locationInstances)
		{
			LocationInstance value = locationInstance.Value;
			string prefabName = value.m_location.m_prefabName;
			if (IsRelevant(prefabName))
			{
				float sqrMagnitude = ((Vector3)(ref value.m_position)).sqrMagnitude;
				if (prefabName == "Vendor_BlackForest" && sqrMagnitude < num)
				{
					val = value.m_position;
					num = sqrMagnitude;
				}
				else if (prefabName == "Hildir_camp" && sqrMagnitude < num2)
				{
					val2 = value.m_position;
					num2 = sqrMagnitude;
				}
				else if (prefabName == "BogWitch_Camp" && sqrMagnitude < num3)
				{
					val3 = value.m_position;
					num3 = sqrMagnitude;
				}
			}
		}
		MinimapHelpers.VendorPinLocation = val;
		MinimapHelpers.HildirPinLocation = val2;
		MinimapHelpers.BogWitchPinLocation = val3;
		if (val != Vector3.zero)
		{
			RpcManager.Instance.RunCoroutine(Helpers.AddPinWhenReady(val, "Vendor_BlackForest"));
		}
		if (val2 != Vector3.zero)
		{
			RpcManager.Instance.RunCoroutine(Helpers.AddPinWhenReady(val2, "Hildir_camp"));
		}
		if (val3 != Vector3.zero)
		{
			RpcManager.Instance.RunCoroutine(Helpers.AddPinWhenReady(val3, "BogWitch_Camp"));
		}
		RevealNpcMapPinsPlugin.Log.LogInfo((object)"[RevealNPCMapPins] Host computed NPC pins");
	}

	public static bool IsRelevant(string prefab)
	{
		if ((!RevealNPCsConfig.RevealTrader.Value || !(prefab == "Vendor_BlackForest")) && (!RevealNPCsConfig.RevealHildir.Value || !(prefab == "Hildir_camp")))
		{
			if (RevealNPCsConfig.RevealBogWitch.Value)
			{
				return prefab == "BogWitch_Camp";
			}
			return false;
		}
		return true;
	}
}
public static class Helpers
{
	public static IEnumerator AddPinWhenReady(Vector3 pos, string prefab)
	{
		//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)
		while ((Object)(object)Minimap.instance == (Object)null)
		{
			yield return null;
		}
		Minimap instance = Minimap.instance;
		MinimapHelpers.RemovePinsNearPosition(instance, pos);
		PinData obj = instance.AddPin(pos, (PinType)8, "", true, false, 0L, default(PlatformUserID));
		obj.m_icon = MinimapHelpers.GetLocationIcon(instance, prefab);
		obj.m_doubleSize = true;
	}
}
public static class MinimapHelpers
{
	public static Vector3 VendorPinLocation;

	public static Vector3 HildirPinLocation;

	public static Vector3 BogWitchPinLocation;

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

	public static List<PinData> GetPins(Minimap minimap)
	{
		return typeof(Minimap).GetField("m_pins", BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(minimap) as List<PinData>;
	}
}
public class RpcManager : MonoBehaviour
{
	public static RpcManager Instance;

	private void Awake()
	{
		Instance = this;
		Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
	}

	public void RunCoroutine(IEnumerator routine)
	{
		((MonoBehaviour)this).StartCoroutine(routine);
	}
}
public class RevealNPCsConfig
{
	public static ConfigEntry<bool> EnableRevealNPCs;

	public static ConfigEntry<bool> RevealTrader;

	public static ConfigEntry<bool> RevealHildir;

	public static ConfigEntry<bool> RevealBogWitch;

	public static void Init(ConfigFile config)
	{
		EnableRevealNPCs = config.Bind<bool>("General", "EnableRevealNPCs", true, "");
		RevealTrader = config.Bind<bool>("NPCs", "RevealTrader", true, "");
		RevealHildir = config.Bind<bool>("NPCs", "RevealHildir", true, "");
		RevealBogWitch = config.Bind<bool>("NPCs", "RevealBogWitch", true, "");
	}
}