Decompiled source of FixPlayerName v1.0.9

FixPlayerName.dll

Decompiled 2 months ago
using System;
using System.Collections;
using System.Collections.Generic;
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 GameNetcodeStuff;
using HarmonyLib;
using Steamworks;
using TMPro;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: AssemblyTitle("FixPlayerName")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FixPlayerName")]
[assembly: AssemblyCopyright("Copyright ©  2025")]
[assembly: AssemblyTrademark("")]
[assembly: ComVisible(false)]
[assembly: Guid("cccf9fe4-8869-41a6-a0e0-35a6c851f5d7")]
[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 FixPlayerName;

[BepInPlugin("FixPlayerName", "chuxia.FixPlayerName", "1.0.9")]
public class FixPlayerNamePlugin : BaseUnityPlugin
{
	public static ManualLogSource ManualLog;

	public static ConfigEntry<int> WorkInterval { get; set; }

	public void Awake()
	{
		ManualLog = ((BaseUnityPlugin)this).Logger;
		WorkInterval = ((BaseUnityPlugin)this).Config.Bind<int>("Work", "Interval", 30, "Seconds");
		Harmony.CreateAndPatchAll(typeof(Patches), (string)null);
		Harmony.CreateAndPatchAll(typeof(UpdateMapTargetPostfixPatch), (string)null);
	}
}
[HarmonyPatch]
public static class UpdateMapTargetPostfixPatch
{
	private static MethodBase TargetMethod()
	{
		Type type = AccessTools.Inner(typeof(ManualCameraRenderer), "<updateMapTarget>d__71");
		return AccessTools.Method(type, "MoveNext", (Type[])null, (Type[])null);
	}

	[HarmonyPostfix]
	public static void Postfix(object __instance, ref bool __result)
	{
		if (!__result)
		{
			FieldInfo fieldInfo = AccessTools.Field(__instance.GetType(), "<>4__this");
			object? value = fieldInfo.GetValue(__instance);
			ManualCameraRenderer val = (ManualCameraRenderer)((value is ManualCameraRenderer) ? value : null);
			if ((Object)(object)val != (Object)null && (Object)(object)val.targetedPlayer != (Object)null && ((TMP_Text)StartOfRound.Instance.mapScreenPlayerName).text != val.targetedPlayer.playerUsername)
			{
				FixPlayerNamePlugin.ManualLog.LogInfo((object)$"ManualCameraRenderer.updateMapTarget -> {val.targetedPlayer.playerUsername}|{val.targetedPlayer.playerSteamId}");
				((TMP_Text)StartOfRound.Instance.mapScreenPlayerName).text = val.targetedPlayer.playerUsername;
			}
		}
	}
}
public static class Patches
{
	[CompilerGenerated]
	private sealed class <Fix>d__10 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		private QuickMenuManager <quick>5__1;

		private PlayerControllerB[] <players>5__2;

		private int <i>5__3;

		private string <name>5__4;

		private bool <request>5__5;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <Fix>d__10(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<quick>5__1 = null;
			<players>5__2 = null;
			<name>5__4 = null;
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_00b7: Unknown result type (might be due to invalid IL or missing references)
			//IL_00bc: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
			//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
			//IL_01e4: Unknown result type (might be due to invalid IL or missing references)
			//IL_01ee: Expected O, but got Unknown
			int num = <>1__state;
			if (num != 0)
			{
				if (num != 1)
				{
					return false;
				}
				<>1__state = -1;
				<name>5__4 = null;
				goto IL_024a;
			}
			<>1__state = -1;
			if (StartOfRound.Instance.connectedPlayersAmount == 0)
			{
				return false;
			}
			if (!Reg)
			{
				SteamFriends.OnPersonaStateChange += SteamFriends_OnPersonaStateChange;
				Reg = true;
			}
			<quick>5__1 = Object.FindObjectOfType<QuickMenuManager>();
			<players>5__2 = StartOfRound.Instance.allPlayerScripts;
			<i>5__3 = 0;
			goto IL_025d;
			IL_025d:
			if (<i>5__3 < <players>5__2.Length)
			{
				if (<players>5__2[<i>5__3].playerSteamId != 0)
				{
					Friend val = new Friend(SteamId.op_Implicit(<players>5__2[<i>5__3].playerSteamId));
					<name>5__4 = ((Friend)(ref val)).Name;
					if (<name>5__4 == "[unknown]")
					{
						<request>5__5 = SteamFriends.RequestUserInformation(SteamId.op_Implicit(<players>5__2[<i>5__3].playerSteamId), true);
						FixPlayerNamePlugin.ManualLog.LogInfo((object)$"RequestFixUnknow -> {<request>5__5}");
					}
					if (((TMP_Text)<quick>5__1.playerListSlots[<i>5__3].usernameHeader).text != <name>5__4)
					{
						<players>5__2[<i>5__3].playerUsername = <name>5__4;
						((TMP_Text)<players>5__2[<i>5__3].usernameBillboardText).text = <name>5__4;
						<quick>5__1.playerListSlots[<i>5__3].playerSteamId = <players>5__2[<i>5__3].playerSteamId;
						((TMP_Text)<quick>5__1.playerListSlots[<i>5__3].usernameHeader).text = <name>5__4;
					}
					<>2__current = (object)new WaitForSeconds(0.1f);
					<>1__state = 1;
					return true;
				}
				if (<quick>5__1.playerListSlots[<i>5__3].slotContainer.activeSelf)
				{
					<quick>5__1.playerListSlots[<i>5__3].slotContainer.SetActive(false);
				}
				goto IL_024a;
			}
			return false;
			IL_024a:
			<i>5__3++;
			goto IL_025d;
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	[CompilerGenerated]
	private sealed class <doFix>d__9 : IEnumerator<object>, IDisposable, IEnumerator
	{
		private int <>1__state;

		private object <>2__current;

		object IEnumerator<object>.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		object IEnumerator.Current
		{
			[DebuggerHidden]
			get
			{
				return <>2__current;
			}
		}

		[DebuggerHidden]
		public <doFix>d__9(int <>1__state)
		{
			this.<>1__state = <>1__state;
		}

		[DebuggerHidden]
		void IDisposable.Dispose()
		{
			<>1__state = -2;
		}

		private bool MoveNext()
		{
			//IL_003a: Unknown result type (might be due to invalid IL or missing references)
			//IL_0044: Expected O, but got Unknown
			switch (<>1__state)
			{
			default:
				return false;
			case 0:
				<>1__state = -1;
				break;
			case 1:
				<>1__state = -1;
				<>2__current = Fix();
				<>1__state = 2;
				return true;
			case 2:
				<>1__state = -1;
				break;
			}
			if ((Object)(object)StartOfRound.Instance.localPlayerController != (Object)null)
			{
				<>2__current = (object)new WaitForSeconds((float)FixPlayerNamePlugin.WorkInterval.Value);
				<>1__state = 1;
				return true;
			}
			return false;
		}

		bool IEnumerator.MoveNext()
		{
			//ILSpy generated this explicit interface implementation from .override directive in MoveNext
			return this.MoveNext();
		}

		[DebuggerHidden]
		void IEnumerator.Reset()
		{
			throw new NotSupportedException();
		}
	}

	private static Coroutine doFixCoroutine;

	public static bool Reg { get; set; }

	[HarmonyPostfix]
	[HarmonyPatch(typeof(GameNetworkManager), "SteamMatchmaking_OnLobbyMemberJoined")]
	public static void SteamMatchmaking_OnLobbyMemberJoinedPostfix(GameNetworkManager __instance)
	{
		if (!GameNetworkManager.Instance.disableSteam && StartOfRound.Instance.shipHasLanded)
		{
			((MonoBehaviour)__instance).StartCoroutine(Fix());
		}
	}

	[HarmonyPriority(0)]
	[HarmonyPostfix]
	[HarmonyPatch(typeof(StartOfRound), "SetShipReadyToLand")]
	private static void EndGamefix()
	{
		AccessTools.DeclaredField(typeof(HUDManager), "spectatingPlayerBoxes").SetValue(HUDManager.Instance, new Dictionary<Animator, PlayerControllerB>());
		AccessTools.DeclaredField(typeof(HUDManager), "boxesAdded").SetValue(HUDManager.Instance, 0);
	}

	[HarmonyPostfix]
	[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
	public static void ConnectClientToPlayerObjectPostfix(PlayerControllerB __instance)
	{
		if (!GameNetworkManager.Instance.disableSteam && doFixCoroutine == null)
		{
			doFixCoroutine = ((MonoBehaviour)__instance).StartCoroutine(doFix());
		}
	}

	[HarmonyPrefix]
	[HarmonyPatch(typeof(GameNetworkManager), "Disconnect")]
	public static void OnDestroy(PlayerControllerB __instance)
	{
		if (doFixCoroutine != null)
		{
			((MonoBehaviour)__instance).StopCoroutine(doFixCoroutine);
			SteamFriends.OnPersonaStateChange -= SteamFriends_OnPersonaStateChange;
			doFixCoroutine = null;
		}
	}

	[IteratorStateMachine(typeof(<doFix>d__9))]
	public static IEnumerator doFix()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <doFix>d__9(0);
	}

	[IteratorStateMachine(typeof(<Fix>d__10))]
	private static IEnumerator Fix()
	{
		//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
		return new <Fix>d__10(0);
	}

	private static void SteamFriends_OnPersonaStateChange(Friend obj)
	{
		//IL_0047: Unknown result type (might be due to invalid IL or missing references)
		//IL_0048: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b4: Unknown result type (might be due to invalid IL or missing references)
		if (!((Object)(object)StartOfRound.Instance != (Object)null) || StartOfRound.Instance.allPlayerScripts == null)
		{
			return;
		}
		QuickMenuManager val = Object.FindObjectOfType<QuickMenuManager>();
		PlayerControllerB[] allPlayerScripts = StartOfRound.Instance.allPlayerScripts;
		for (int i = 0; i < allPlayerScripts.Length; i++)
		{
			if (allPlayerScripts[i].playerSteamId == obj.Id.Value)
			{
				FixPlayerNamePlugin.ManualLog.LogInfo((object)$"SteamFriends_OnPersonaStateChange -> {((Friend)(ref obj)).Name}|{val.playerListSlots[i].playerSteamId}");
				allPlayerScripts[i].playerUsername = ((Friend)(ref obj)).Name;
				((TMP_Text)allPlayerScripts[i].usernameBillboardText).text = ((Friend)(ref obj)).Name;
				allPlayerScripts[i].playerSteamId = obj.Id.Value;
				((TMP_Text)val.playerListSlots[i].usernameHeader).text = ((Friend)(ref obj)).Name;
			}
		}
	}
}