Decompiled source of FixPlayerName v1.1.1

FixPlayerName.dll

Decompiled 3 weeks ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using FixPlayerName.Patches;
using GameNetcodeStuff;
using HarmonyLib;
using Microsoft.CodeAnalysis;
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: IgnoresAccessChecksTo("AmazingAssets.TerrainToMesh")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp-firstpass")]
[assembly: IgnoresAccessChecksTo("Assembly-CSharp")]
[assembly: IgnoresAccessChecksTo("ClientNetworkTransform")]
[assembly: IgnoresAccessChecksTo("com.olegknyazev.softmask")]
[assembly: IgnoresAccessChecksTo("DissonanceVoip")]
[assembly: IgnoresAccessChecksTo("EasyTextEffects")]
[assembly: IgnoresAccessChecksTo("Facepunch Transport for Netcode for GameObjects")]
[assembly: IgnoresAccessChecksTo("Facepunch.Steamworks.Win64")]
[assembly: IgnoresAccessChecksTo("Unity.AI.Navigation")]
[assembly: IgnoresAccessChecksTo("Unity.Animation.Rigging")]
[assembly: IgnoresAccessChecksTo("Unity.Animation.Rigging.DocCodeExamples")]
[assembly: IgnoresAccessChecksTo("Unity.Burst")]
[assembly: IgnoresAccessChecksTo("Unity.Burst.Unsafe")]
[assembly: IgnoresAccessChecksTo("Unity.Collections")]
[assembly: IgnoresAccessChecksTo("Unity.Collections.LowLevel.ILSupport")]
[assembly: IgnoresAccessChecksTo("Unity.InputSystem")]
[assembly: IgnoresAccessChecksTo("Unity.InputSystem.ForUI")]
[assembly: IgnoresAccessChecksTo("Unity.Jobs")]
[assembly: IgnoresAccessChecksTo("Unity.Mathematics")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.Common")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.MetricTypes")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetStats")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetStatsMonitor.Component")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetStatsMonitor.Configuration")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetStatsMonitor.Implementation")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetStatsReporting")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetworkProfiler.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.Multiplayer.Tools.NetworkSolutionInterface")]
[assembly: IgnoresAccessChecksTo("Unity.Netcode.Components")]
[assembly: IgnoresAccessChecksTo("Unity.Netcode.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.Networking.Transport")]
[assembly: IgnoresAccessChecksTo("Unity.ProBuilder.Csg")]
[assembly: IgnoresAccessChecksTo("Unity.ProBuilder")]
[assembly: IgnoresAccessChecksTo("Unity.ProBuilder.KdTree")]
[assembly: IgnoresAccessChecksTo("Unity.ProBuilder.Poly2Tri")]
[assembly: IgnoresAccessChecksTo("Unity.ProBuilder.Stl")]
[assembly: IgnoresAccessChecksTo("Unity.Profiling.Core")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.Core.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.Core.ShaderLibrary")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.HighDefinition.Config.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.HighDefinition.Runtime")]
[assembly: IgnoresAccessChecksTo("Unity.RenderPipelines.ShaderGraph.ShaderGraphLibrary")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Authentication")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Analytics")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Components")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Configuration")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Device")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Environments")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Environments.Internal")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Internal")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Networking")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Registration")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Scheduler")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Telemetry")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Core.Threading")]
[assembly: IgnoresAccessChecksTo("Unity.Services.QoS")]
[assembly: IgnoresAccessChecksTo("Unity.Services.Relay")]
[assembly: IgnoresAccessChecksTo("Unity.TextMeshPro")]
[assembly: IgnoresAccessChecksTo("Unity.Timeline")]
[assembly: IgnoresAccessChecksTo("Unity.VisualEffectGraph.Runtime")]
[assembly: IgnoresAccessChecksTo("UnityEngine.ARModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.NVIDIAModule")]
[assembly: IgnoresAccessChecksTo("UnityEngine.UI")]
[assembly: AssemblyCompany("chuxiaaaa")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyDescription("\r\n\t\tFixed incorrect or unknown player names.\r\n\t")]
[assembly: AssemblyFileVersion("1.1.1.0")]
[assembly: AssemblyInformationalVersion("1.1.1+11a936a70019caef8815bf830c97cbfab66bfb07")]
[assembly: AssemblyProduct("FixPlayerName")]
[assembly: AssemblyTitle("FixPlayerName")]
[assembly: AssemblyMetadata("RepositoryUrl", "__PROJECT_URL__")]
[assembly: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("1.1.1.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.Class | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Parameter | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableAttribute : Attribute
	{
		public readonly byte[] NullableFlags;

		public NullableAttribute(byte P_0)
		{
			NullableFlags = new byte[1] { P_0 };
		}

		public NullableAttribute(byte[] P_0)
		{
			NullableFlags = P_0;
		}
	}
	[CompilerGenerated]
	[Microsoft.CodeAnalysis.Embedded]
	[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Method | AttributeTargets.Interface | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
	internal sealed class NullableContextAttribute : Attribute
	{
		public readonly byte Flag;

		public NullableContextAttribute(byte P_0)
		{
			Flag = P_0;
		}
	}
	[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;
		}
	}
}
[BepInPlugin("FixPlayerName", "FixPlayerName", "1.1.1")]
public class Plugin : BaseUnityPlugin
{
	public static ManualLogSource Log;

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

	private void Awake()
	{
		Log = ((BaseUnityPlugin)this).Logger;
		WorkInterval = ((BaseUnityPlugin)this).Config.Bind<int>("Work", "Interval", 30, "Seconds");
		Harmony.CreateAndPatchAll(typeof(FixPatches), (string)null);
		Harmony.CreateAndPatchAll(typeof(UpdateMapTargetPostfixPatch), (string)null);
		Log.LogInfo((object)"Plugin FixPlayerName is loaded!");
	}
}
internal static class LCMPluginInfo
{
	public const string PLUGIN_GUID = "FixPlayerName";

	public const string PLUGIN_NAME = "FixPlayerName";

	public const string PLUGIN_VERSION = "1.1.1";
}
namespace FixPlayerName.Patches
{
	[HarmonyWrapSafe]
	public static class FixPatches
	{
		[CompilerGenerated]
		private sealed class <FixPlayerNamesRoutine>d__8 : IEnumerator<object>, IEnumerator, IDisposable
		{
			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 <FixPlayerNamesRoutine>d__8(int <>1__state)
			{
				this.<>1__state = <>1__state;
			}

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

			private bool MoveNext()
			{
				switch (<>1__state)
				{
				default:
					return false;
				case 0:
					<>1__state = -1;
					if (!_isRegistered)
					{
						SteamFriends.OnPersonaStateChange += OnPersonaStateChange;
						_isRegistered = true;
					}
					break;
				case 1:
					<>1__state = -1;
					<>2__current = RefreshPlayerNames();
					<>1__state = 2;
					return true;
				case 2:
					<>1__state = -1;
					break;
				}
				if ((Object)(object)StartOfRound.Instance?.localPlayerController != (Object)null)
				{
					<>2__current = _workInterval;
					<>1__state = 1;
					return true;
				}
				StopFixCoroutine();
				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();
			}
		}

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

			private object <>2__current;

			private StartOfRound <instance>5__1;

			private QuickMenuManager <quickMenu>5__2;

			private PlayerControllerB[] <players>5__3;

			private int <i>5__4;

			private PlayerControllerB <player>5__5;

			private PlayerListSlot <slot>5__6;

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

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

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

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<instance>5__1 = null;
				<quickMenu>5__2 = null;
				<players>5__3 = null;
				<player>5__5 = null;
				<slot>5__6 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				int num = <>1__state;
				if (num != 0)
				{
					if (num != 1)
					{
						return false;
					}
					<>1__state = -1;
					<player>5__5 = null;
					<slot>5__6 = null;
					goto IL_015e;
				}
				<>1__state = -1;
				<instance>5__1 = StartOfRound.Instance;
				if (<instance>5__1?.allPlayerScripts == null || <instance>5__1.connectedPlayersAmount == 0)
				{
					return false;
				}
				<quickMenu>5__2 = Object.FindObjectOfType<QuickMenuManager>();
				if ((Object)(object)<quickMenu>5__2 == (Object)null)
				{
					return false;
				}
				<players>5__3 = <instance>5__1.allPlayerScripts;
				<i>5__4 = 0;
				goto IL_0170;
				IL_0170:
				if (<i>5__4 < <players>5__3.Length)
				{
					<player>5__5 = <players>5__3[<i>5__4];
					<slot>5__6 = <quickMenu>5__2.playerListSlots[<i>5__4];
					if (<player>5__5.playerSteamId == 0)
					{
						if (<slot>5__6.slotContainer.activeSelf)
						{
							<slot>5__6.slotContainer.SetActive(false);
						}
						goto IL_015e;
					}
					if (!<slot>5__6.slotContainer.activeSelf)
					{
						<slot>5__6.slotContainer.SetActive(true);
					}
					<>2__current = UpdatePlayerName(<player>5__5, <slot>5__6);
					<>1__state = 1;
					return true;
				}
				return false;
				IL_015e:
				<i>5__4++;
				goto IL_0170;
			}

			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 <UpdatePlayerName>d__10 : IEnumerator<object>, IEnumerator, IDisposable
		{
			private int <>1__state;

			private object <>2__current;

			public PlayerControllerB player;

			public PlayerListSlot slot;

			private Friend <friend>5__1;

			private string <steamName>5__2;

			private bool <requested>5__3;

			private Exception <ex>5__4;

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

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

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

			[DebuggerHidden]
			void IDisposable.Dispose()
			{
				<steamName>5__2 = null;
				<ex>5__4 = null;
				<>1__state = -2;
			}

			private bool MoveNext()
			{
				//IL_0025: Unknown result type (might be due to invalid IL or missing references)
				//IL_002a: Unknown result type (might be due to invalid IL or missing references)
				//IL_002f: Unknown result type (might be due to invalid IL or missing references)
				//IL_0066: Unknown result type (might be due to invalid IL or missing references)
				if (<>1__state != 0)
				{
					return false;
				}
				<>1__state = -1;
				try
				{
					<friend>5__1 = new Friend(SteamId.op_Implicit(player.playerSteamId));
					<steamName>5__2 = ((Friend)(ref <friend>5__1)).Name;
					if (<steamName>5__2 == "[unknown]")
					{
						<requested>5__3 = SteamFriends.RequestUserInformation(SteamId.op_Implicit(player.playerSteamId), true);
						Plugin.Log.LogInfo((object)$"Requested user info for {player.playerSteamId}: {<requested>5__3}");
						return false;
					}
					if (player.playerUsername != <steamName>5__2)
					{
						player.playerUsername = <steamName>5__2;
						((TMP_Text)player.usernameBillboardText).text = <steamName>5__2;
					}
					if (((TMP_Text)slot.usernameHeader).text != <steamName>5__2)
					{
						((TMP_Text)slot.usernameHeader).text = <steamName>5__2;
						slot.playerSteamId = player.playerSteamId;
					}
					<steamName>5__2 = null;
				}
				catch (Exception ex)
				{
					<ex>5__4 = ex;
					Plugin.Log.LogError((object)$"Error updating player name: {<ex>5__4}");
				}
				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? _fixCoroutine;

		private static bool _isRegistered;

		private static readonly WaitForSeconds _workInterval = new WaitForSeconds((float)Plugin.WorkInterval.Value);

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

		[HarmonyPostfix]
		[HarmonyPatch(typeof(StartOfRound), "SetShipReadyToLand")]
		private static void ResetHUDManager()
		{
			HUDManager instance = HUDManager.Instance;
			if (!((Object)(object)instance == (Object)null))
			{
				instance.spectatingPlayerBoxes = new Dictionary<Animator, PlayerControllerB>();
				instance.boxesAdded = 0;
			}
		}

		[HarmonyPostfix]
		[HarmonyPatch(typeof(PlayerControllerB), "ConnectClientToPlayerObject")]
		public static void ConnectClientToPlayerObjectPostfix(PlayerControllerB __instance)
		{
			if (!GameNetworkManager.Instance.disableSteam)
			{
				StopFixCoroutine((MonoBehaviour)(object)__instance);
				_fixCoroutine = ((MonoBehaviour)__instance).StartCoroutine(FixPlayerNamesRoutine());
			}
		}

		[HarmonyPrefix]
		[HarmonyPatch(typeof(GameNetworkManager), "Disconnect")]
		public static void OnDisconnect()
		{
			StopFixCoroutine();
		}

		private static void StopFixCoroutine(MonoBehaviour behaviour = null)
		{
			if (_fixCoroutine != null)
			{
				MonoBehaviour val = (MonoBehaviour)(((object)behaviour) ?? ((object)StartOfRound.Instance?.localPlayerController));
				if (val != null)
				{
					val.StopCoroutine(_fixCoroutine);
				}
				if (_isRegistered)
				{
					SteamFriends.OnPersonaStateChange -= OnPersonaStateChange;
					_isRegistered = false;
				}
				_fixCoroutine = null;
			}
		}

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

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

		[IteratorStateMachine(typeof(<UpdatePlayerName>d__10))]
		private static IEnumerator UpdatePlayerName(PlayerControllerB player, PlayerListSlot slot)
		{
			//yield-return decompiler failed: Unexpected instruction in Iterator.Dispose()
			return new <UpdatePlayerName>d__10(0)
			{
				player = player,
				slot = slot
			};
		}

		private static void OnPersonaStateChange(Friend friend)
		{
			//IL_0055: Unknown result type (might be due to invalid IL or missing references)
			//IL_0056: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cb: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cc: Unknown result type (might be due to invalid IL or missing references)
			try
			{
				StartOfRound instance = StartOfRound.Instance;
				if (instance?.allPlayerScripts == null)
				{
					return;
				}
				QuickMenuManager val = Object.FindObjectOfType<QuickMenuManager>();
				if ((Object)(object)val == (Object)null)
				{
					return;
				}
				PlayerControllerB[] allPlayerScripts = instance.allPlayerScripts;
				for (int i = 0; i < allPlayerScripts.Length; i++)
				{
					PlayerControllerB val2 = allPlayerScripts[i];
					if (val2.playerSteamId == friend.Id.Value)
					{
						Plugin.Log.LogInfo((object)("Persona state changed: " + ((Friend)(ref friend)).Name));
						val2.playerUsername = ((Friend)(ref friend)).Name;
						((TMP_Text)val2.usernameBillboardText).text = ((Friend)(ref friend)).Name;
						PlayerListSlot val3 = val.playerListSlots[i];
						((TMP_Text)val3.usernameHeader).text = ((Friend)(ref friend)).Name;
						val3.playerSteamId = friend.Id.Value;
						if (!val3.slotContainer.activeSelf)
						{
							val3.slotContainer.SetActive(true);
						}
						break;
					}
				}
			}
			catch (Exception arg)
			{
				Plugin.Log.LogError((object)$"Error in persona state change handler: {arg}");
			}
		}
	}
	[HarmonyPatch]
	[HarmonyWrapSafe]
	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)
				{
					Plugin.Log.LogInfo((object)$"ManualCameraRenderer.updateMapTarget -> {val.targetedPlayer.playerUsername}|{val.targetedPlayer.playerSteamId}");
					((TMP_Text)StartOfRound.Instance.mapScreenPlayerName).text = val.targetedPlayer.playerUsername;
				}
			}
		}
	}
}
namespace System.Runtime.CompilerServices
{
	[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
	internal sealed class IgnoresAccessChecksToAttribute : Attribute
	{
		public IgnoresAccessChecksToAttribute(string assemblyName)
		{
		}
	}
}