Decompiled source of BetterPlayerNames v0.1.0

BetterPlayerNames.dll

Decompiled 4 months ago
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 Crest;
using Fusion;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UI;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("BetterPlayerNames")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("BetterPlayerNames")]
[assembly: AssemblyCopyright("Copyright ©  2024")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("92ba1a06-2c7e-4b24-bf86-0a93c36c1736")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace BetterPlayerNames;

[BepInPlugin("randyknapp.mods.betterplayernames", "Better Player Names", "0.1.0")]
public class BetterPlayerNames : BaseUnityPlugin
{
	public const string GUID = "randyknapp.mods.betterplayernames";

	public const string NAME = "Better Player Names";

	public const string VERSION = "0.1.0";

	public static ConfigEntry<float> NearThreshold;

	public static ConfigEntry<float> FarThreshold;

	public static ConfigEntry<float> HiddenThreshold;

	public static ConfigEntry<float> NearScale;

	public static ConfigEntry<float> FarScale;

	public static ManualLogSource logger;

	public void Awake()
	{
		logger = ((BaseUnityPlugin)this).Logger;
		NearThreshold = ((BaseUnityPlugin)this).Config.Bind<float>("Config", "Near Threshold", 30f, "Distance before the player name begins scaling down.");
		FarThreshold = ((BaseUnityPlugin)this).Config.Bind<float>("Config", "Far Threshold", 100f, "Maximum distance to scale the player name.");
		HiddenThreshold = ((BaseUnityPlugin)this).Config.Bind<float>("Config", "Hidden Threshold", -1f, "If greater than zero, if the player is farther than this threshold, the player name does not show.");
		NearScale = ((BaseUnityPlugin)this).Config.Bind<float>("Config", "Near Scale", 1f, "Scale of the player name when it is at or closer than the Near Threshold.");
		FarScale = ((BaseUnityPlugin)this).Config.Bind<float>("Config", "Far Scale", 0.3f, "Scale of the player name when it is at or farther than the Far Threshold.");
		((BaseUnityPlugin)this).Logger.LogWarning((object)"Better Player Names Loaded");
		((BaseUnityPlugin)this).Logger.LogWarning((object)$"- {((ConfigEntryBase)NearThreshold).Definition.Key}: {NearThreshold.Value}");
		((BaseUnityPlugin)this).Logger.LogWarning((object)$"- {((ConfigEntryBase)FarThreshold).Definition.Key}: {FarThreshold.Value}");
		((BaseUnityPlugin)this).Logger.LogWarning((object)$"- {((ConfigEntryBase)HiddenThreshold).Definition.Key}: {HiddenThreshold.Value}");
		((BaseUnityPlugin)this).Logger.LogWarning((object)$"- {((ConfigEntryBase)NearScale).Definition.Key}: {NearScale.Value}");
		((BaseUnityPlugin)this).Logger.LogWarning((object)$"- {((ConfigEntryBase)FarScale).Definition.Key}: {FarScale.Value}");
		Harmony.CreateAndPatchAll(Assembly.GetExecutingAssembly(), "randyknapp.mods.betterplayernames");
	}
}
[HarmonyPatch]
public static class PlayerDummyPatch
{
	private static readonly FieldInfo NameHintFieldInfo = typeof(PlayerDummy).GetField("NameHint", BindingFlags.Instance | BindingFlags.NonPublic);

	private static readonly MethodInfo RefreshDisplayOfHintMethodInfo = typeof(PlayerDummy).GetMethod("RefreshDisplayOfHint", BindingFlags.Instance | BindingFlags.NonPublic);

	private static int _counter = 0;

	[HarmonyPatch(typeof(PlayerDummy), "RefreshDisplayOfHint")]
	[HarmonyPrefix]
	public static bool RefreshDisplayOfHint_Prefix(PlayerDummy __instance)
	{
		//IL_0069: 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_007d: 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_0099: Unknown result type (might be due to invalid IL or missing references)
		//IL_00aa: Unknown result type (might be due to invalid IL or missing references)
		//IL_00af: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
		//IL_020d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0224: Unknown result type (might be due to invalid IL or missing references)
		if (!Object.op_Implicit((Object)(object)WorldScene.code))
		{
			return false;
		}
		foreach (Transform item in WorldScene.code.allPlayerDummies.items)
		{
			if ((Object)(object)item == (Object)(object)((Component)__instance).transform || (Object)(object)item == (Object)null)
			{
				continue;
			}
			PlayerDummy component = ((Component)item).GetComponent<PlayerDummy>();
			Vector3 val = item.position - ((Component)FPSPlayer.code).transform.position;
			float magnitude = ((Vector3)(ref val)).magnitude;
			float value = BetterPlayerNames.HiddenThreshold.Value;
			float num = Utility.ContAngle(val, ((Component)OceanRenderer.Instance.ViewCamera).transform.forward, Vector3.up);
			if ((value < 0f || magnitude < value) && (Object)(object)item.parent == (Object)null && num > -90f && num < 90f)
			{
				component.ChangeDisplayOfHint(true);
				float value2 = BetterPlayerNames.NearThreshold.Value;
				float value3 = BetterPlayerNames.FarThreshold.Value;
				float value4 = BetterPlayerNames.NearScale.Value;
				float value5 = BetterPlayerNames.FarScale.Value;
				object? value6 = NameHintFieldInfo.GetValue(component);
				Transform val2 = (Transform)((value6 is Transform) ? value6 : null);
				if ((Object)(object)val2 == (Object)null)
				{
					if (_counter % 60 == 0)
					{
						BetterPlayerNames.logger.LogWarning((object)"Could not get NameHint");
					}
					_counter++;
					continue;
				}
				float num2 = Mathf.Lerp(value4, value5, Mathf.InverseLerp(value2, value3, magnitude));
				val2.localScale = new Vector3(num2, num2, 1f);
				((Component)val2).GetComponent<Text>().text = $"{component.CharacterName} ({(int)magnitude}m)";
				Outline component2 = ((Component)val2).GetComponent<Outline>();
				if ((Object)(object)component2 == (Object)null)
				{
					component2 = ((Component)val2).gameObject.AddComponent<Outline>();
					((Shadow)component2).effectColor = Color.black;
					((Shadow)component2).effectDistance = new Vector2(2f, 2f);
				}
			}
			else
			{
				component.ChangeDisplayOfHint(false);
			}
		}
		return false;
	}

	[HarmonyPatch(typeof(PlayerDummy), "CharacterNameChanged")]
	[HarmonyPostfix]
	private static void CharacterNameChanged_Postfix(Changed<PlayerDummy> changed)
	{
		PlayerDummy behaviour = changed.Behaviour;
		RefreshDisplayOfHintMethodInfo.Invoke(behaviour, new object[0]);
	}
}