Decompiled source of PlayerNametagTweaks v1.0.0

NametagMod.dll

Decompiled 4 months ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.0", FrameworkDisplayName = ".NET Framework 4")]
[assembly: AssemblyCompany("NametagMod")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyDescription("Nametag distance mod")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("NametagMod")]
[assembly: AssemblyTitle("NametagMod")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.0.0.0")]
[module: UnverifiableCode]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Module, AllowMultiple = false, Inherited = false)]
	internal sealed class RefSafetyRulesAttribute : Attribute
	{
		public readonly int Version;

		public RefSafetyRulesAttribute(int P_0)
		{
			Version = P_0;
		}
	}
}
namespace NametagMod
{
	[BepInPlugin("NametagMod", "NametagMod", "1.0.0")]
	public class Plugin : BaseUnityPlugin
	{
		private Harmony harmony;

		private ConfigEntry<float> nametagDistance;

		private ConfigEntry<bool> scaleDownOverDistance;

		private static FieldInfo playerPositionRefPointField = typeof(EnemyHud).GetField("m_refPoint", BindingFlags.Instance | BindingFlags.NonPublic);

		private static FieldInfo hudsDictionaryField = typeof(EnemyHud).GetField("m_huds", BindingFlags.Instance | BindingFlags.NonPublic);

		private static FieldInfo hudDataMainGuiField = typeof(EnemyHud).Assembly.GetType("EnemyHud+HudData").GetField("m_gui", BindingFlags.Instance | BindingFlags.Public);

		public static Plugin Instance { get; private set; }

		private void Awake()
		{
			//IL_000c: Unknown result type (might be due to invalid IL or missing references)
			//IL_0016: Expected O, but got Unknown
			Instance = this;
			harmony = new Harmony("main");
			harmony.PatchAll(typeof(Plugin));
			nametagDistance = ((BaseUnityPlugin)this).Config.Bind<float>("General", "Nametag Distance", 64f, (ConfigDescription)null);
			scaleDownOverDistance = ((BaseUnityPlugin)this).Config.Bind<bool>("General", "Scale down nametag over distance", true, (ConfigDescription)null);
		}

		[HarmonyPatch(typeof(EnemyHud), "TestShow")]
		[HarmonyPostfix]
		private static void ShowCharacterHudPostfix(ref EnemyHud __instance, ref bool __result, Character c, bool isVisible)
		{
			//IL_001e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0029: Unknown result type (might be due to invalid IL or missing references)
			if ((Object)(object)c == (Object)null || !c.IsPlayer())
			{
				return;
			}
			float num = Vector3.Distance((Vector3)playerPositionRefPointField.GetValue(__instance), ((Component)c).transform.position);
			if (!(num > Instance.nametagDistance.Value))
			{
				if (Instance.scaleDownOverDistance.Value && TryGetCharacterHudData(__instance, c, out var hudData))
				{
					HandleHudScaling(hudData, num);
				}
				__result = true;
			}
		}

		private static void HandleHudScaling(object hudData, float distance)
		{
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			object? value = hudDataMainGuiField.GetValue(hudData);
			GameObject val = (GameObject)((value is GameObject) ? value : null);
			if (!((Object)(object)val == (Object)null))
			{
				float num = distance / Instance.nametagDistance.Value;
				float num2 = 0.25f + 0.75f * (1f - num);
				val.transform.localScale = new Vector3(num2, num2, num2);
			}
		}

		private static bool TryGetCharacterHudData(EnemyHud hudController, Character character, out object hudData)
		{
			if (hudsDictionaryField.GetValue(hudController) is IDictionary dictionary)
			{
				foreach (DictionaryEntry item in dictionary)
				{
					if (item.Key == character)
					{
						hudData = item.Value;
						return hudData != null;
					}
				}
			}
			hudData = null;
			return false;
		}
	}
	public static class MyPluginInfo
	{
		public const string PLUGIN_GUID = "NametagMod";

		public const string PLUGIN_NAME = "NametagMod";

		public const string PLUGIN_VERSION = "1.0.0";
	}
}