Decompiled source of PeakCustomNameMod v1.0.9

plugins/PeakCustomNameMod/PeakCustomNameMod.dll

Decompiled a week ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using BepInEx;
using BepInEx.Logging;
using ExitGames.Client.Photon;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using Peak.Network;
using Photon.Pun;
using Photon.Realtime;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.Events;
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: TargetFramework(".NETStandard,Version=v2.1", FrameworkDisplayName = ".NET Standard 2.1")]
[assembly: AssemblyCompany("PeakCustomNameMod")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("PeakCustomNameMod")]
[assembly: AssemblyTitle("PeakCustomNameMod")]
[assembly: AssemblyVersion("1.0.0.0")]
[module: RefSafetyRules(11)]
namespace Microsoft.CodeAnalysis
{
	[CompilerGenerated]
	[Embedded]
	internal sealed class EmbeddedAttribute : Attribute
	{
	}
}
namespace System.Runtime.CompilerServices
{
	[CompilerGenerated]
	[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 PeakCustomNameMod
{
	[BepInPlugin("Mr.PeakCustomName", "PEAK 自定义游戏名", "1.0.9")]
	public class CustomNamePlugin : BaseUnityPlugin
	{
		[HarmonyPatch(typeof(NetworkingUtilities), "GetUsername")]
		internal static class GetUsernamePatch
		{
			private static bool Prefix(ref string __result)
			{
				if (HasCustomName())
				{
					__result = NetworkingUtilities.Sanitize(GetStoredName());
					return false;
				}
				return true;
			}
		}

		[HarmonyPatch(/*Could not decode attribute arguments.*/)]
		internal static class CharacterNamePatch
		{
			private static void Postfix(Character __instance, ref string __result)
			{
				try
				{
					if ((Object)(object)__instance == (Object)null)
					{
						return;
					}
					PhotonView photonView = ((MonoBehaviourPun)__instance).photonView;
					if (!((Object)(object)photonView == (Object)null))
					{
						int ownerActorNr = photonView.OwnerActorNr;
						if (CustomNamesByActor.TryGetValue(ownerActorNr, out var value) && !string.IsNullOrEmpty(value))
						{
							__result = value;
						}
					}
				}
				catch
				{
				}
			}
		}

		[HarmonyPatch(typeof(UIPlayerNames), "UpdateName")]
		internal static class UpdateNamePatch
		{
			private static void Postfix(UIPlayerNames __instance, int index)
			{
				try
				{
					if (index < 0 || index >= __instance.playerNameText.Length)
					{
						return;
					}
					PlayerName val = __instance.playerNameText[index];
					if (!((Object)(object)val == (Object)null) && !((Object)(object)val.characterInteractable == (Object)null) && !((Object)(object)val.text == (Object)null))
					{
						string name = val.characterInteractable.GetName();
						if (!string.IsNullOrEmpty(name) && ((TMP_Text)val.text).text != name)
						{
							((TMP_Text)val.text).text = name;
						}
					}
				}
				catch
				{
				}
			}
		}

		[HarmonyPatch(typeof(PassportManager), "ToggleOpen")]
		internal static class ToggleOpenPatch
		{
			private static void Postfix()
			{
				PassportNameInjector.OnPassportToggle();
			}
		}

		internal class PropListener : MonoBehaviourPunCallbacks
		{
			public override void OnPlayerPropertiesUpdate(Player targetPlayer, Hashtable changedProps)
			{
				if (changedProps != null && ((Dictionary<object, object>)(object)changedProps).ContainsKey((object)"PeakCustomName"))
				{
					int actorNumber = targetPlayer.ActorNumber;
					string text = changedProps[(object)"PeakCustomName"] as string;
					if (!string.IsNullOrEmpty(text))
					{
						CustomNamesByActor[actorNumber] = text;
					}
					else
					{
						CustomNamesByActor.Remove(actorNumber);
					}
					Log.LogInfo((object)string.Format("收到队友自定义名同步: actor#{0} = '{1}'", actorNumber, text ?? ""));
				}
			}
		}

		internal static ManualLogSource Log;

		private const string PrefKey = "peak_custom_name";

		private const string PropKey = "PeakCustomName";

		private static readonly Dictionary<int, string> CustomNamesByActor = new Dictionary<int, string>();

		private void Awake()
		{
			//IL_001d: Unknown result type (might be due to invalid IL or missing references)
			//IL_0023: Expected O, but got Unknown
			//IL_002f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0035: Expected O, but got Unknown
			Log = ((BaseUnityPlugin)this).Logger;
			try
			{
				Harmony val = new Harmony(((BaseUnityPlugin)this).Info.Metadata.GUID);
				val.PatchAll();
				GameObject val2 = new GameObject("PeakCustomNamePropListener");
				val2.AddComponent<PropListener>();
				Object.DontDestroyOnLoad((Object)(object)val2);
				Log.LogInfo((object)"PEAK 自定义游戏名 已加载(Harmony 补丁 + Photon 属性回调已注册)");
			}
			catch (Exception arg)
			{
				Log.LogError((object)$"初始化失败: {arg}");
			}
		}

		public static string GetStoredName()
		{
			return PlayerPrefs.GetString("peak_custom_name", "");
		}

		public static bool HasCustomName()
		{
			return !string.IsNullOrEmpty(GetStoredName());
		}

		public static void SaveName(string name)
		{
			PlayerPrefs.SetString("peak_custom_name", name ?? "");
			PlayerPrefs.Save();
		}

		public static void ClearName()
		{
			PlayerPrefs.DeleteKey("peak_custom_name");
			PlayerPrefs.Save();
		}

		public static string EffectiveName()
		{
			return HasCustomName() ? GetStoredName() : NetworkingUtilities.GetUsername();
		}

		public static void ApplyToPhoton()
		{
			//IL_0021: Unknown result type (might be due to invalid IL or missing references)
			//IL_0026: Unknown result type (might be due to invalid IL or missing references)
			//IL_0032: Expected O, but got Unknown
			//IL_0034: Expected O, but got Unknown
			try
			{
				string text = (HasCustomName() ? GetStoredName() : "");
				PhotonNetwork.NickName = EffectiveName();
				Hashtable val = new Hashtable();
				((Dictionary<object, object>)val).Add((object)"PeakCustomName", (object)text);
				Hashtable val2 = val;
				PhotonNetwork.LocalPlayer.SetCustomProperties(val2, (Hashtable)null, (WebFlags)null);
				Log.LogInfo((object)("已同步游戏名到 Photon: custom='" + text + "' nickname='" + EffectiveName() + "'"));
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("同步到 Photon 失败(可能尚未连接房间): " + ex.Message));
			}
		}
	}
	internal static class PassportNameInjector
	{
		private static TMP_InputField _input;

		private static TextMeshProUGUI _label;

		private static Color _labelOriginalColor = Color.white;

		private static bool _initialized;

		private static bool _editing;

		public static void OnPassportToggle()
		{
			PassportManager instance = PassportManager.instance;
			if (!((Object)(object)instance == (Object)null) && ((MenuWindow)instance).isOpen)
			{
				EnsureInjected(instance);
				ExitEdit();
				RefreshLabel();
			}
		}

		private static void EnsureInjected(PassportManager pm)
		{
			//IL_0061: 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)
			//IL_009e: Unknown result type (might be due to invalid IL or missing references)
			if (_initialized)
			{
				return;
			}
			TextMeshProUGUI nt = pm.nameText;
			if ((Object)(object)nt == (Object)null || (Object)(object)((TMP_Text)nt).transform == (Object)null)
			{
				return;
			}
			_label = nt;
			_labelOriginalColor = ((Graphic)nt).color;
			((Graphic)nt).raycastTarget = true;
			Image val = ((Component)nt).gameObject.GetComponent<Image>() ?? ((Component)nt).gameObject.AddComponent<Image>();
			((Graphic)val).color = Color.clear;
			((Graphic)val).raycastTarget = true;
			TMP_InputField val2 = ((Component)nt).gameObject.GetComponent<TMP_InputField>() ?? ((Component)nt).gameObject.AddComponent<TMP_InputField>();
			val2.textComponent = (TMP_Text)(object)nt;
			((Selectable)val2).targetGraphic = (Graphic)(object)val;
			val2.characterLimit = 32;
			val2.placeholder = null;
			((Selectable)val2).interactable = false;
			((Behaviour)val2).enabled = true;
			EventTrigger et = ((Component)nt).gameObject.GetComponent<EventTrigger>() ?? ((Component)nt).gameObject.AddComponent<EventTrigger>();
			AddTrigger(et, (EventTriggerType)4, delegate
			{
				EnterEdit();
			});
			AddTrigger(et, (EventTriggerType)0, delegate
			{
				//IL_0022: Unknown result type (might be due to invalid IL or missing references)
				if (!_editing)
				{
					((Graphic)nt).color = new Color(0.6f, 0.85f, 1f);
				}
			});
			AddTrigger(et, (EventTriggerType)1, delegate
			{
				//IL_0013: Unknown result type (might be due to invalid IL or missing references)
				if (!_editing)
				{
					((Graphic)nt).color = _labelOriginalColor;
				}
			});
			((UnityEvent<string>)(object)val2.onEndEdit).AddListener((UnityAction<string>)OnEndEdit);
			((UnityEvent<string>)(object)val2.onSubmit).AddListener((UnityAction<string>)OnEndEdit);
			_input = val2;
			_initialized = true;
			CustomNamePlugin.Log.LogInfo((object)"护照名字已设为「点击原地编辑」(无黑框)");
		}

		private static void AddTrigger(EventTrigger et, EventTriggerType type, UnityAction<BaseEventData> cb)
		{
			//IL_0001: Unknown result type (might be due to invalid IL or missing references)
			//IL_0006: Unknown result type (might be due to invalid IL or missing references)
			//IL_0007: Unknown result type (might be due to invalid IL or missing references)
			//IL_0008: Unknown result type (might be due to invalid IL or missing references)
			//IL_000e: Expected O, but got Unknown
			Entry val = new Entry
			{
				eventID = type
			};
			((UnityEvent<BaseEventData>)(object)val.callback).AddListener(cb);
			et.triggers.Add(val);
		}

		private static void RefreshLabel()
		{
			if ((Object)(object)_label != (Object)null)
			{
				((TMP_Text)_label).text = CustomNamePlugin.EffectiveName();
			}
		}

		private static void EnterEdit()
		{
			if (!_editing && !((Object)(object)_input == (Object)null) && !((Object)(object)_label == (Object)null))
			{
				((Selectable)_input).interactable = true;
				_input.text = CustomNamePlugin.EffectiveName();
				_input.ActivateInputField();
				_input.caretPosition = _input.text.Length;
				_editing = true;
			}
		}

		private static void ExitEdit()
		{
			//IL_004c: Unknown result type (might be due to invalid IL or missing references)
			if (_editing)
			{
				_input.DeactivateInputField(false);
				((Selectable)_input).interactable = false;
				if ((Object)(object)_label != (Object)null)
				{
					((TMP_Text)_label).text = CustomNamePlugin.EffectiveName();
					((Graphic)_label).color = _labelOriginalColor;
				}
				_editing = false;
			}
		}

		private static void OnEndEdit(string value)
		{
			string text = value ?? "";
			if (string.IsNullOrWhiteSpace(text))
			{
				CustomNamePlugin.ClearName();
			}
			else
			{
				string text2 = NetworkingUtilities.Sanitize(text);
				if (text2.Length > 32)
				{
					text2 = text2.Substring(0, 32);
				}
				CustomNamePlugin.SaveName(text2);
			}
			CustomNamePlugin.ApplyToPhoton();
			UpdatePassportTexts();
			ExitEdit();
			CustomNamePlugin.Log.LogInfo((object)"护照内已保存游戏名");
		}

		private static void UpdatePassportTexts()
		{
			PassportManager instance = PassportManager.instance;
			if (!((Object)(object)instance == (Object)null))
			{
				string text = CustomNamePlugin.EffectiveName();
				if ((Object)(object)instance.nameText != (Object)null)
				{
					((TMP_Text)instance.nameText).text = text;
				}
				if ((Object)(object)instance.passportNumberText != (Object)null)
				{
					((TMP_Text)instance.passportNumberText).text = PassportManager.GeneratePassportNumber(text);
				}
			}
		}
	}
}