Decompiled source of BetterMap v1.0.5

BetterMap.dll

Decompiled 2 weeks ago
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 Unity.VisualScripting;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("BetterMap")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BetterMap")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("B0176CF2-1E17-4E09-B0E9-0A21BA1B599F")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace BetterMap;

[BepInPlugin("BetterMap", "BetterMap", "1.0.5")]
public class BetterMap : BaseUnityPlugin
{
	[HarmonyPatch(typeof(EnemyParent), "SpawnRPC")]
	public static class EnemySpawnRPCHookPatch
	{
		private static void Postfix(EnemyParent __instance)
		{
			//IL_0064: Unknown result type (might be due to invalid IL or missing references)
			//IL_0069: Unknown result type (might be due to invalid IL or missing references)
			//IL_006b: Unknown result type (might be due to invalid IL or missing references)
			//IL_007e: Expected I4, but got Unknown
			//IL_008c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0091: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bf: 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_00d6: Unknown result type (might be due to invalid IL or missing references)
			if (!configShowEnemies.Value)
			{
				return;
			}
			FieldInfo field = typeof(EnemyParent).GetField("Enemy", BindingFlags.Instance | BindingFlags.NonPublic);
			if (field == null)
			{
				return;
			}
			object? value = field.GetValue(__instance);
			Enemy val = (Enemy)((value is Enemy) ? value : null);
			if ((Object)(object)val == (Object)null)
			{
				return;
			}
			GameObject mapCustomGameObject = GetMapCustomGameObject(val);
			MapCustom component = mapCustomGameObject.GetComponent<MapCustom>();
			if ((Object)(object)component == (Object)null)
			{
				component = mapCustomGameObject.AddComponent<MapCustom>();
				Difficulty difficulty = __instance.difficulty;
				switch ((int)difficulty)
				{
				case 0:
					component.sprite = enemySpriteSquare;
					component.color = Color.red;
					break;
				case 1:
					component.sprite = enemySpriteCircle;
					component.color = Color.red;
					break;
				case 2:
					component.sprite = enemySpriteTriangle;
					component.color = Color.red;
					break;
				default:
					component.sprite = enemySpriteSquare;
					component.color = Color.red;
					break;
				}
			}
		}
	}

	[HarmonyPatch(typeof(EnemyParent), "DespawnRPC")]
	public static class EnemyDeSpawnRPCHookPatch
	{
		private static void Postfix(EnemyParent __instance)
		{
			if (!configShowEnemies.Value)
			{
				return;
			}
			FieldInfo field = typeof(EnemyParent).GetField("Enemy", BindingFlags.Instance | BindingFlags.NonPublic);
			if (!(field != null))
			{
				return;
			}
			object? value = field.GetValue(__instance);
			Enemy val = (Enemy)((value is Enemy) ? value : null);
			if (!((Object)(object)val != (Object)null))
			{
				return;
			}
			MapCustom component = GetMapCustomGameObject(val).GetComponent<MapCustom>();
			if ((Object)(object)component != (Object)null)
			{
				if ((Object)(object)component.mapCustomEntity != (Object)null)
				{
					Object.Destroy((Object)(object)((Component)component.mapCustomEntity).gameObject);
				}
				Object.Destroy((Object)(object)component);
			}
		}
	}

	[HarmonyPatch(typeof(LevelGenerator), "GenerateDone")]
	public static class LevelGeneratorGenerateDoneHookPatch
	{
		private static void Postfix(LevelGenerator __instance)
		{
			AddAllPlayersToMap();
			if (!configExploreAllRooms.Value)
			{
				return;
			}
			foreach (RoomVolume item in Object.FindObjectsOfType<RoomVolume>().ToList())
			{
				item.SetExplored();
			}
		}
	}

	[HarmonyPatch(typeof(PlayerAvatar))]
	public static class PlayerAvatarRPCPatch
	{
		[HarmonyPatch("ReviveRPC")]
		[HarmonyPostfix]
		public static void ReviveRPC(PlayerAvatar __instance)
		{
			AddPlayerToMap(__instance);
		}

		[HarmonyPatch("PlayerDeathRPC")]
		[HarmonyPostfix]
		public static void PlayerDeathRPC(PlayerAvatar __instance)
		{
			RemovePlayerFromMap(__instance);
		}
	}

	private static Sprite enemySpriteSquare;

	private static Sprite enemySpriteCircle;

	private static Sprite enemySpriteTriangle;

	internal static ConfigEntry<bool> configShowTeammates;

	internal static ConfigEntry<bool> configShowEnemies;

	internal static ConfigEntry<bool> configExploreAllRooms;

	private void Awake()
	{
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		configShowTeammates = ((BaseUnityPlugin)this).Config.Bind<bool>("Display Options", "ShowTeammates", true, "Display teammate on the map");
		configShowEnemies = ((BaseUnityPlugin)this).Config.Bind<bool>("Display Options", "ShowEnemies", true, "Display enemy on the map");
		configExploreAllRooms = ((BaseUnityPlugin)this).Config.Bind<bool>("Gameplay Options", "ExploreAllRooms", false, "Automatically explore all rooms at the start of the game");
		new Harmony("BetterMap").PatchAll(Assembly.GetExecutingAssembly());
		CreateEnemySprites();
	}

	private static void CreateEnemySprites()
	{
		if (!((Object)(object)enemySpriteSquare != (Object)null) || !((Object)(object)enemySpriteCircle != (Object)null) || !((Object)(object)enemySpriteTriangle != (Object)null))
		{
			enemySpriteSquare = CreateSquareSprite();
			enemySpriteCircle = CreateCircleSprite();
			enemySpriteTriangle = CreateTriangleSprite();
		}
	}

	private static Sprite CreateSquareSprite()
	{
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000c: Expected O, but got Unknown
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0020: Unknown result type (might be due to invalid IL or missing references)
		//IL_004b: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		int num = 5;
		Texture2D val = new Texture2D(num, num, (TextureFormat)5, false);
		Color[] array = (Color[])(object)new Color[num * num];
		for (int i = 0; i < array.Length; i++)
		{
			array[i] = Color.white;
		}
		val.SetPixels(array);
		val.Apply();
		return Sprite.Create(val, new Rect(0f, 0f, (float)num, (float)num), new Vector2(0.5f, 0.5f));
	}

	private static Sprite CreateCircleSprite()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Expected O, but got Unknown
		//IL_00ab: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ba: Unknown result type (might be due to invalid IL or missing references)
		//IL_006f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0074: Unknown result type (might be due to invalid IL or missing references)
		//IL_0060: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		int num = 10;
		Texture2D val = new Texture2D(num, num, (TextureFormat)5, false);
		Color[] array = (Color[])(object)new Color[num * num];
		float num2 = (float)num / 2f;
		float num3 = (float)num / 2f - 1f;
		for (int i = 0; i < num; i++)
		{
			for (int j = 0; j < num; j++)
			{
				int num4 = i * num + j;
				float num5 = (float)j - num2;
				float num6 = (float)i - num2;
				if (num5 * num5 + num6 * num6 <= num3 * num3)
				{
					array[num4] = Color.white;
				}
				else
				{
					array[num4] = Color.clear;
				}
			}
		}
		val.SetPixels(array);
		val.Apply();
		return Sprite.Create(val, new Rect(0f, 0f, (float)num, (float)num), new Vector2(0.5f, 0.5f));
	}

	private static Sprite CreateTriangleSprite()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: Expected O, but got Unknown
		//IL_00c2: 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_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0068: Unknown result type (might be due to invalid IL or missing references)
		//IL_0069: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0086: Unknown result type (might be due to invalid IL or missing references)
		//IL_008b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0077: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Unknown result type (might be due to invalid IL or missing references)
		int num = 10;
		Texture2D val = new Texture2D(num, num, (TextureFormat)5, false);
		Color[] array = (Color[])(object)new Color[num * num];
		Vector2 a = default(Vector2);
		((Vector2)(ref a))..ctor((float)num / 2f, (float)(num - 1));
		Vector2 b = default(Vector2);
		((Vector2)(ref b))..ctor(1f, 1f);
		Vector2 c = default(Vector2);
		((Vector2)(ref c))..ctor((float)(num - 2), 1f);
		for (int i = 0; i < num; i++)
		{
			for (int j = 0; j < num; j++)
			{
				Vector2 p = new Vector2((float)j, (float)i);
				int num2 = i * num + j;
				if (IsPointInTriangle(p, a, b, c))
				{
					array[num2] = Color.white;
				}
				else
				{
					array[num2] = Color.clear;
				}
			}
		}
		val.SetPixels(array);
		val.Apply();
		return Sprite.Create(val, new Rect(0f, 0f, (float)num, (float)num), new Vector2(0.5f, 0.5f));
	}

	private static bool IsPointInTriangle(Vector2 p, Vector2 a, Vector2 b, Vector2 c)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_0006: Unknown result type (might be due to invalid IL or missing references)
		//IL_000d: 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_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0021: Unknown result type (might be due to invalid IL or missing references)
		//IL_0028: Unknown result type (might be due to invalid IL or missing references)
		//IL_002e: Unknown result type (might be due to invalid IL or missing references)
		//IL_0037: 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_0044: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0052: Unknown result type (might be due to invalid IL or missing references)
		//IL_0058: Unknown result type (might be due to invalid IL or missing references)
		//IL_005f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0065: Unknown result type (might be due to invalid IL or missing references)
		//IL_006f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_007c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0082: Unknown result type (might be due to invalid IL or missing references)
		//IL_008a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0090: Unknown result type (might be due to invalid IL or missing references)
		//IL_0097: Unknown result type (might be due to invalid IL or missing references)
		//IL_009d: Unknown result type (might be due to invalid IL or missing references)
		float num = (p.x - b.x) * (a.y - b.y) - (a.x - b.x) * (p.y - b.y);
		float num2 = (p.x - c.x) * (b.y - c.y) - (b.x - c.x) * (p.y - c.y);
		float num3 = (p.x - a.x) * (c.y - a.y) - (c.x - a.x) * (p.y - a.y);
		bool flag = num < 0f || num2 < 0f || num3 < 0f;
		bool flag2 = num > 0f || num2 > 0f || num3 > 0f;
		return !(flag && flag2);
	}

	public static void AddPlayerToMap(PlayerAvatar playerAvatar)
	{
		//IL_0059: Unknown result type (might be due to invalid IL or missing references)
		//IL_005e: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_006c: Unknown result type (might be due to invalid IL or missing references)
		if (configShowTeammates.Value && !((Object)(object)playerAvatar == (Object)null) && !((Object)(object)((Component)playerAvatar).GetComponent<MapCustom>() != (Object)null))
		{
			MapCustom val = ComponentHolderProtocol.AddComponent<MapCustom>((Object)(object)playerAvatar);
			FieldInfo field = typeof(PlayerAvatarVisuals).GetField("color", BindingFlags.Instance | BindingFlags.NonPublic);
			if (field != null)
			{
				Color color = (Color)field.GetValue(playerAvatar.playerAvatarVisuals);
				val.sprite = enemySpriteSquare;
				val.color = color;
			}
		}
	}

	public static void RemovePlayerFromMap(PlayerAvatar playerAvatar)
	{
		if (!configShowTeammates.Value || (Object)(object)playerAvatar == (Object)null)
		{
			return;
		}
		MapCustom component = ((Component)playerAvatar).GetComponent<MapCustom>();
		if (!((Object)(object)component == (Object)null))
		{
			if ((Object)(object)component.mapCustomEntity != (Object)null)
			{
				Object.Destroy((Object)(object)component.mapCustomEntity);
			}
			Object.Destroy((Object)(object)component);
		}
	}

	public static void AddAllPlayersToMap()
	{
		if (!configShowTeammates.Value || (Object)(object)GameDirector.instance == (Object)null)
		{
			return;
		}
		List<PlayerAvatar> playerList = GameDirector.instance.PlayerList;
		if (playerList == null || playerList.Count == 0)
		{
			return;
		}
		foreach (PlayerAvatar item in playerList)
		{
			AddPlayerToMap(item);
		}
	}

	public static GameObject GetMapCustomGameObject(Enemy enemy)
	{
		if ((Object)(object)enemy == (Object)null)
		{
			return null;
		}
		EnemyRigidbody item = GetEnemyRigidbody(enemy).Item1;
		if (!((Object)(object)item != (Object)null))
		{
			return ((Component)enemy).gameObject;
		}
		return ((Component)item).gameObject;
	}

	public static (EnemyRigidbody, bool) GetEnemyRigidbody(Enemy enemy)
	{
		//IL_004f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0067: Expected O, but got Unknown
		FieldInfo field = typeof(Enemy).GetField("Rigidbody", BindingFlags.Instance | BindingFlags.NonPublic);
		FieldInfo field2 = typeof(Enemy).GetField("HasRigidbody", BindingFlags.Instance | BindingFlags.NonPublic);
		if (field == null || field2 == null)
		{
			return (null, false);
		}
		EnemyRigidbody val = (EnemyRigidbody)field.GetValue(enemy);
		bool item = (bool)field2.GetValue(enemy);
		return (val, item);
	}
}