Decompiled source of NamePlateResize v1.0.0

NamePlateResize.dll

Decompiled 9 hours ago
using System;
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 HarmonyLib;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: AssemblyTitle("NamePlateResize")]
[assembly: AssemblyDescription("Shrinks the world-space name/guild nameplates above characters in Erenshor")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("NamePlateResize")]
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("7c4e1a2b-9d3f-4a61-b58c-2e0f6d7a9c14")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: TargetFramework(".NETFramework,Version=v4.8", FrameworkDisplayName = ".NET Framework 4.8")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace NamePlateResize;

[BepInPlugin("com.erenshor.nameplateresize", "NamePlate Resize", "1.0.0")]
public class NamePlateResizePlugin : BaseUnityPlugin
{
	public const string PluginGuid = "com.erenshor.nameplateresize";

	public const string PluginName = "NamePlate Resize";

	public const string PluginVersion = "1.0.0";

	private const int KindSim = 0;

	private const int KindNpc = 1;

	private const int KindPlayer = 2;

	private const int KindCount = 3;

	private static readonly string[] KindNames = new string[3] { "sim", "NPC", "player" };

	private static readonly string[] KindSections = new string[3] { "Sims", "NPCs", "Yourself" };

	private static ManualLogSource _log;

	private static readonly ConfigEntry<float>[] _cfgPlateScale = new ConfigEntry<float>[3];

	private static readonly ConfigEntry<float>[] _cfgHeightOffset = new ConfigEntry<float>[3];

	private static bool _baselineCaptured;

	private static bool _parRtIsPlateRoot;

	private static readonly float[] _appliedPlateScale = new float[3] { 1f, 1f, 1f };

	private static readonly float[] _appliedHeightOffset = new float[3];

	private static bool _failureLogged;

	private static readonly bool[] _kindAnnounced = new bool[3];

	private void Awake()
	{
		_log = ((BaseUnityPlugin)this).Logger;
		BindKind(0, 1f, "sims -- the green names above other players");
		BindKind(1, 1f, "NPCs and mobs, including vendors and mining nodes");
		BindKind(2, 1f, "your own character");
		for (int i = 0; i < 3; i++)
		{
			_appliedPlateScale[i] = _cfgPlateScale[i].Value;
			_appliedHeightOffset[i] = _cfgHeightOffset[i].Value;
		}
		Harmony.CreateAndPatchAll(typeof(NamePlateResizePlugin), (string)null);
		_log.LogInfo((object)("Active. " + DescribeSettings()));
		_log.LogInfo((object)"Prefab baseline will be logged when the first nameplate spawns.");
	}

	private void BindKind(int kind, float defaultScale, string who)
	{
		//IL_003f: Unknown result type (might be due to invalid IL or missing references)
		//IL_0049: Expected O, but got Unknown
		//IL_007a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Expected O, but got Unknown
		string text = KindSections[kind];
		_cfgPlateScale[kind] = ((BaseUnityPlugin)this).Config.Bind<float>(text, "NameplateScale", defaultScale, new ConfigDescription("The size of the floating nameplate above " + who + ".\n1.0 is the size the game draws it, so nothing changes until you\nlower this.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(0.1f, 3f), Array.Empty<object>()));
		_cfgHeightOffset[kind] = ((BaseUnityPlugin)this).Config.Bind<float>(text, "HeightOffset", 0f, new ConfigDescription("Moves that nameplate up or down, in metres.\nA smaller plate leaves a wider gap above the head, so around -0.15\noften sits better. 0 keeps the game's own placement.", (AcceptableValueBase)(object)new AcceptableValueRange<float>(-2f, 2f), Array.Empty<object>()));
	}

	private static int ClassifyPlate(NamePlate plate)
	{
		Character myself = plate.Myself;
		if ((Object)(object)myself == (Object)null)
		{
			return 2;
		}
		NPC myNPC = myself.MyNPC;
		if ((Object)(object)myNPC == (Object)null)
		{
			return 1;
		}
		return (!myNPC.SimPlayer) ? 1 : 0;
	}

	private static void ApplyToPlate(NamePlate plate, int kind, float scaleFactor, float heightDelta)
	{
		//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_0042: 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_0057: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)plate == (Object)null))
		{
			if (!_baselineCaptured)
			{
				CaptureBaseline(plate);
			}
			Transform transform = ((Component)plate).transform;
			if (scaleFactor != 1f)
			{
				transform.localScale *= scaleFactor;
			}
			if (heightDelta != 0f)
			{
				transform.position += new Vector3(0f, heightDelta, 0f);
			}
		}
	}

	private static void CaptureBaseline(NamePlate plate)
	{
		_baselineCaptured = true;
		Transform transform = ((Component)plate).transform;
		Transform myParRT = (Transform)(object)plate.MyParRT;
		_parRtIsPlateRoot = (Object)(object)myParRT != (Object)null && (Object)(object)myParRT == (Object)(object)transform;
		if (_parRtIsPlateRoot)
		{
			_log.LogWarning((object)"This build's NamePlate.MyParRT IS the plate root, so the game rewrites that scale every frame on any plate showing a lifebar. Mob nameplates may not stay resized. Sims and your own plate are unaffected.");
		}
	}

	private static void NamePlateStarted(NamePlate plate)
	{
		int num = ClassifyPlate(plate);
		ApplyToPlate(plate, num, _appliedPlateScale[num], _appliedHeightOffset[num]);
		if (!_kindAnnounced[num])
		{
			_kindAnnounced[num] = true;
			string text = (((Object)(object)plate.MyStats != (Object)null) ? plate.MyStats.MyName : "(you)");
			_log.LogInfo((object)$"First {KindNames[num]} nameplate classified: \"{text}\" -- NameplateScale {_cfgPlateScale[num].Value}, HeightOffset {_cfgHeightOffset[num].Value}.");
		}
	}

	private static string DescribeSettings()
	{
		string text = "";
		for (int i = 0; i < 3; i++)
		{
			if (i > 0)
			{
				text += " | ";
			}
			text += $"{KindSections[i]}: scale={_cfgPlateScale[i].Value} height={_cfgHeightOffset[i].Value}";
		}
		return text;
	}

	[HarmonyPatch(typeof(NamePlate), "Start")]
	[HarmonyPostfix]
	public static void NamePlate_Start_Postfix(NamePlate __instance)
	{
		try
		{
			NamePlateStarted(__instance);
		}
		catch (Exception ex)
		{
			if (!_failureLogged)
			{
				_failureLogged = true;
				_log.LogError((object)("Could not resize a nameplate; leaving all plates at vanilla size. This is reported once only. " + ex));
			}
		}
	}
}