Decompiled source of Custom Enemy Location v1.0.4

CustomEnemyLocation.dll

Decompiled 2 weeks ago
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyCompany("CustomEnemyLocation")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("CustomEnemyLocation")]
[assembly: AssemblyTitle("CustomEnemyLocation")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace com.ugleh.repo;

[BepInPlugin("com.ugleh.repo.customenemylocation", "Custom Enemy Location", "1.0.0")]
public class CustomEnemyLocationPlugin : BaseUnityPlugin
{
	public class EnemyMapTracker
	{
		private readonly Dictionary<EnemyParent, MapCustom> icons = new Dictionary<EnemyParent, MapCustom>();

		public void Add(EnemyParent parent)
		{
			//IL_00a4: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a9: Unknown result type (might be due to invalid IL or missing references)
			if (icons.ContainsKey(parent) || (Object)(object)playerMapSprite == (Object)null)
			{
				return;
			}
			GameDirector instance = GameDirector.instance;
			if (instance == null || instance.PlayerList?.Count != 0)
			{
				Enemy value = Traverse.Create((object)parent).Field("Enemy").GetValue<Enemy>();
				if (!((Object)(object)value == (Object)null))
				{
					MapCustom val = ((Component)value).gameObject.AddComponent<MapCustom>();
					val.sprite = playerMapSprite;
					val.color = enemyIconColor;
					((Object)val).name = "Enemy_Point_" + parent.enemyName;
					icons[parent] = val;
					((MonoBehaviour)parent).StartCoroutine(ScaleMapIcon(val, value));
				}
			}
		}

		public void Remove(EnemyParent parent)
		{
			if (icons.TryGetValue(parent, out var value))
			{
				if ((Object)(object)value.mapCustomEntity != (Object)null)
				{
					Object.Destroy((Object)(object)((Component)value.mapCustomEntity).gameObject);
				}
				icons.Remove(parent);
			}
		}

		private static IEnumerator ScaleMapIcon(MapCustom mc, Enemy enemy)
		{
			yield return (object)new WaitForSeconds(0.2f);
			if (!((Object)(object)mc.mapCustomEntity == (Object)null))
			{
				float scaleFactor = GetIconScaleByEnemyType(enemy.Type);
				((Component)mc.mapCustomEntity).transform.localScale = Vector3.one * scaleFactor;
			}
		}

		private static float GetIconScaleByEnemyType(EnemyType type)
		{
			//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_0003: Unknown result type (might be due to invalid IL or missing references)
			//IL_0004: Unknown result type (might be due to invalid IL or missing references)
			//IL_0005: Unknown result type (might be due to invalid IL or missing references)
			//IL_001f: Expected I4, but got Unknown
			return (int)type switch
			{
				0 => 0.65f, 
				1 => 0.85f, 
				2 => 1f, 
				3 => 1.25f, 
				4 => 1.5f, 
				_ => 1f, 
			};
		}
	}

	[HarmonyPatch(typeof(EnemyParent), "SpawnRPC")]
	public class EnemySpawnPatch
	{
		private static void Postfix(EnemyParent __instance)
		{
			if (enableEnemyIcons.Value)
			{
				tracker.Add(__instance);
				LoggerInstance.LogInfo((object)"Enemy icon added to minimap.");
			}
		}
	}

	[HarmonyPatch(typeof(EnemyParent), "DespawnRPC")]
	public class EnemyDespawnPatch
	{
		private static void Postfix(EnemyParent __instance)
		{
			if (enableEnemyIcons.Value)
			{
				tracker.Remove(__instance);
				LoggerInstance.LogInfo((object)"Enemy icon removed from minimap.");
			}
		}
	}

	public static ManualLogSource LoggerInstance;

	public static Sprite playerMapSprite;

	public static ConfigEntry<bool> enableEnemyIcons;

	public static ConfigEntry<string> enemyIconColorHex;

	public static EnemyMapTracker tracker;

	public static Color enemyIconColor = new Color(0.5f, 0f, 1f);

	private void Awake()
	{
		//IL_0066: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Expected O, but got Unknown
		LoggerInstance = ((BaseUnityPlugin)this).Logger;
		tracker = new EnemyMapTracker();
		enableEnemyIcons = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Enable Enemy Icons", true, "Toggle visibility of enemies on the minimap");
		enemyIconColorHex = ((BaseUnityPlugin)this).Config.Bind<string>("General", "Enemy Icon Color (Hex)", "#8000FF", "Hex color code for enemy icons (e.g. #FF0000 for red)");
		ParseColorConfig();
		Harmony val = new Harmony("com.ugleh.repo.customenemylocation");
		val.PatchAll();
		((BaseUnityPlugin)this).Logger.LogInfo((object)"Custom Enemy Location loaded!");
	}

	private void Update()
	{
		if ((Object)(object)playerMapSprite == (Object)null)
		{
			GameDirector instance = GameDirector.instance;
			if (instance != null && instance.PlayerList?.Count > 0 && (Object)(object)GameDirector.instance.PlayerList[0]?.playerDeathHead?.mapCustom?.sprite != (Object)null)
			{
				playerMapSprite = GameDirector.instance.PlayerList[0].playerDeathHead.mapCustom.sprite;
				((BaseUnityPlugin)this).Logger.LogInfo((object)"Loaded player map icon sprite for enemy markers.");
			}
		}
	}

	private void ParseColorConfig()
	{
		//IL_0076: Unknown result type (might be due to invalid IL or missing references)
		//IL_007b: Unknown result type (might be due to invalid IL or missing references)
		string text = enemyIconColorHex.Value.Trim();
		if (!text.StartsWith("#"))
		{
			text = "#" + text;
		}
		if (!ColorUtility.TryParseHtmlString(text, ref enemyIconColor))
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)("Failed to parse hex color '" + enemyIconColorHex.Value + "', using default Purple."));
			enemyIconColor = new Color(0.5f, 0f, 1f);
		}
	}
}