Decompiled source of RadarLabelsFix v1.0.0

RadarLabelsFix.dll

Decompiled 5 hours ago
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Logging;
using GameNetcodeStuff;
using HarmonyLib;
using TMPro;
using UnityEngine;

[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace RadarLabelsFix;

[BepInPlugin("Gezang.RadarLabelsFix", "RadarLabelsFix", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
	public const string Guid = "Gezang.RadarLabelsFix";

	public const string Name = "RadarLabelsFix";

	public const string Version = "1.0.0";

	internal static ManualLogSource Log;

	private void Awake()
	{
		//IL_0010: Unknown result type (might be due to invalid IL or missing references)
		//IL_0016: Expected O, but got Unknown
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_002c: Expected O, but got Unknown
		//IL_003c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0042: Expected O, but got Unknown
		Log = ((BaseUnityPlugin)this).Logger;
		Harmony val = new Harmony("Gezang.RadarLabelsFix");
		HarmonyMethod val2 = new HarmonyMethod(typeof(Plugin), "RelabelInstance", (Type[])null);
		HarmonyMethod val3 = new HarmonyMethod(typeof(Plugin), "RelabelMainMap", (Type[])null);
		Harmony harmony = val;
		Type typeFromHandle = typeof(ManualCameraRenderer);
		string method = "SyncOrderOfRadarBoostersInList";
		HarmonyMethod postfix = val2;
		Patch(harmony, typeFromHandle, method, null, postfix);
		harmony = val;
		typeFromHandle = typeof(ManualCameraRenderer);
		method = "AddTransformAsTargetToRadar";
		postfix = val2;
		Patch(harmony, typeFromHandle, method, null, postfix);
		harmony = val;
		typeFromHandle = typeof(ManualCameraRenderer);
		method = "RemoveTargetFromRadar";
		postfix = val2;
		Patch(harmony, typeFromHandle, method, null, postfix);
		harmony = val;
		typeFromHandle = typeof(PlayerControllerB);
		method = "SendNewPlayerValuesClientRpc";
		postfix = val3;
		Patch(harmony, typeFromHandle, method, null, postfix);
		harmony = val;
		typeFromHandle = typeof(StartOfRound);
		method = "OnPlayerConnectedClientRpc";
		postfix = val3;
		Patch(harmony, typeFromHandle, method, null, postfix);
		harmony = val;
		typeFromHandle = typeof(StartOfRound);
		method = "OnPlayerDC";
		postfix = val3;
		Patch(harmony, typeFromHandle, method, null, postfix);
		Patch(val, typeof(ManualCameraRenderer), "updateMapTarget", val2);
		Patch(val, typeof(Terminal), "ParsePlayerSentence", val3);
		Log.LogInfo((object)string.Format("{0} {1} loaded", "RadarLabelsFix", "1.0.0"));
	}

	private static void Patch(Harmony harmony, Type type, string method, HarmonyMethod prefix = null, HarmonyMethod postfix = null)
	{
		try
		{
			MethodInfo methodInfo = AccessTools.Method(type, method, (Type[])null, (Type[])null);
			if (methodInfo == null)
			{
				Log.LogWarning((object)$"Could not find {type.Name}.{method}; skipping that hook");
			}
			else
			{
				harmony.Patch((MethodBase)methodInfo, prefix, postfix, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
			}
		}
		catch (Exception arg)
		{
			Log.LogWarning((object)$"Failed to hook {type.Name}.{method}: {arg}");
		}
	}

	private static void RelabelInstance(ManualCameraRenderer __instance)
	{
		Relabel(__instance);
	}

	private static void RelabelMainMap()
	{
		Relabel((!((Object)(object)StartOfRound.Instance != (Object)null)) ? null : StartOfRound.Instance.mapScreen);
	}

	internal static void Relabel(ManualCameraRenderer map)
	{
		try
		{
			if ((Object)(object)map == (Object)null || map.radarTargets == null)
			{
				return;
			}
			List<TransformAndName> radarTargets = map.radarTargets;
			bool flag = false;
			for (int i = 0; i < radarTargets.Count; i++)
			{
				TransformAndName val = radarTargets[i];
				if (val != null && !val.isNonPlayer && !((Object)(object)val.transform == (Object)null))
				{
					PlayerControllerB component = ((Component)val.transform).GetComponent<PlayerControllerB>();
					if (!((Object)(object)component == (Object)null) && !string.IsNullOrEmpty(component.playerUsername) && val.name != component.playerUsername)
					{
						Log.LogInfo((object)$"Radar target #{i}: label '{val.name}' -> '{component.playerUsername}' (tracks {((Object)((Component)component).gameObject).name})");
						val.name = component.playerUsername;
						flag = true;
					}
				}
			}
			if (flag)
			{
				RefreshMonitorName(map);
			}
		}
		catch (Exception arg)
		{
			Log.LogWarning((object)$"Relabel failed: {arg}");
		}
	}

	private static void RefreshMonitorName(ManualCameraRenderer map)
	{
		StartOfRound instance = StartOfRound.Instance;
		if (!((Object)(object)instance == (Object)null) && !((Object)(object)map != (Object)(object)instance.mapScreen) && !((Object)(object)instance.mapScreenPlayerName == (Object)null))
		{
			int targetTransformIndex = map.targetTransformIndex;
			if (targetTransformIndex >= 0 && targetTransformIndex < map.radarTargets.Count && map.radarTargets[targetTransformIndex] != null)
			{
				((TMP_Text)instance.mapScreenPlayerName).text = map.radarTargets[targetTransformIndex].name ?? string.Empty;
			}
		}
	}
}