Decompiled source of IdentityRename v1.2.0

BepInEx/plugins/IdentityRename/IdentityRename.dll

Decompiled 18 hours ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using Microsoft.CodeAnalysis;
using Peak.Network;
using Photon.Pun;
using Photon.Realtime;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]
[assembly: TargetFramework(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]
[assembly: AssemblyCompany("IdentityRename")]
[assembly: AssemblyConfiguration("Release")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyInformationalVersion("1.2.0")]
[assembly: AssemblyProduct("IdentityRename")]
[assembly: AssemblyTitle("IdentityRename")]
[assembly: AssemblyVersion("1.2.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 ABUF.IdentityRename
{
	public static class Identity
	{
		public const int MaxLength = 32;

		private static readonly Regex RichText = new Regex("<[^>]*>", RegexOptions.Compiled);

		public static ConfigEntry<string> DisplayName;

		public static ConfigEntry<bool> UseColor;

		public static ConfigEntry<string>[] Colors = new ConfigEntry<string>[5];

		public static ConfigEntry<int>[] Lengths = new ConfigEntry<int>[5];

		private static readonly string[] DefaultColors = new string[5] { "FF4D4D", "4DC3FF", "FFD970", "FF69B4", "FFFFFF" };

		private static readonly int[] DefaultLengths = new int[5] { 1, 2, 0, 0, 0 };

		public static string Chosen => Clean((DisplayName == null) ? string.Empty : DisplayName.Value);

		public static string SteamName
		{
			get
			{
				try
				{
					return Clean(NetworkingUtilities.GetUsername());
				}
				catch
				{
					return string.Empty;
				}
			}
		}

		public static void Init(ConfigFile config)
		{
			//IL_00b8: Unknown result type (might be due to invalid IL or missing references)
			//IL_00c2: Expected O, but got Unknown
			DisplayName = config.Bind<string>("身份", "显示名", "", "游戏内显示名(留空 = 使用 Steam 名)。改完重进房间生效。");
			UseColor = config.Bind<bool>("身份", "彩色名字", true, "按下面的颜色设置给显示名分段上色。开启后不要再开其它改名字颜色的模组。");
			for (int i = 0; i < 5; i++)
			{
				Colors[i] = config.Bind<string>("颜色", "第" + (i + 1) + "段颜色", DefaultColors[i], "颜色码(不要带 #),例如 FF4D4D。");
				Lengths[i] = config.Bind<int>("颜色", "第" + (i + 1) + "段字数", DefaultLengths[i], new ConfigDescription("这一段负责几个字;0 表示不用这一段。", (AcceptableValueBase)(object)new AcceptableValueRange<int>(0, 32), Array.Empty<object>()));
			}
		}

		public static string Clean(string raw)
		{
			if (string.IsNullOrEmpty(raw))
			{
				return string.Empty;
			}
			string text = RichText.Replace(raw, string.Empty);
			text = text.Replace('\n', ' ').Replace('\r', ' ').Replace('\t', ' ')
				.Trim();
			if (text.Length > 32)
			{
				text = text.Substring(0, 32).Trim();
			}
			return text;
		}

		public static void Apply()
		{
			try
			{
				string text = Chosen;
				if (text.Length == 0)
				{
					text = SteamName;
				}
				if (text.Length == 0)
				{
					return;
				}
				string text2 = text;
				if (UseColor != null && UseColor.Value)
				{
					string text3 = BuildColored(text);
					if (text3.Length > 0)
					{
						text2 = text3;
					}
				}
				if (PhotonNetwork.NickName != text2)
				{
					PhotonNetwork.NickName = text2;
					Plugin.Log.LogInfo((object)("[Identity] 显示名已设为 " + text2));
				}
			}
			catch (Exception ex)
			{
				Plugin.Log.LogWarning((object)("[Identity] 设置显示名失败:" + ex.Message));
			}
		}

		private static string BuildColored(string name)
		{
			List<string> list = new List<string>();
			int num = 0;
			for (int i = 0; i < 5; i++)
			{
				if (Lengths[i] != null && Colors[i] != null)
				{
					int value = Lengths[i].Value;
					string text = CleanColor(Colors[i].Value);
					if (value > 0 && text.Length != 0 && num < name.Length)
					{
						int num2 = Math.Min(value, name.Length - num);
						string text2 = name.Substring(num, num2);
						num += num2;
						list.Add("<color=#" + text + ">" + text2 + "</color>");
					}
				}
			}
			if (num < name.Length)
			{
				list.Add(name.Substring(num));
			}
			return string.Concat(list.ToArray());
		}

		private static string CleanColor(string raw)
		{
			if (string.IsNullOrEmpty(raw))
			{
				return string.Empty;
			}
			string text = raw.Trim().TrimStart(new char[1] { '#' }).Trim();
			if (text.Length != 6 && text.Length != 8)
			{
				return string.Empty;
			}
			foreach (char c in text)
			{
				if ((c < '0' || c > '9') && (c < 'a' || c > 'f') && (c < 'A' || c > 'F'))
				{
					return string.Empty;
				}
			}
			return text;
		}
	}
	internal sealed class IdentityJoinHook : IMatchmakingCallbacks
	{
		public void OnJoinedRoom()
		{
			Identity.Apply();
		}

		public void OnFriendListUpdate(List<FriendInfo> friendList)
		{
		}

		public void OnCreatedRoom()
		{
		}

		public void OnCreateRoomFailed(short returnCode, string message)
		{
		}

		public void OnJoinRoomFailed(short returnCode, string message)
		{
		}

		public void OnJoinRandomFailed(short returnCode, string message)
		{
		}

		public void OnLeftRoom()
		{
		}
	}
	[BepInPlugin("abuf.identityrename", "Identity Rename", "1.2.0")]
	public sealed class Plugin : BaseUnityPlugin
	{
		public const string Guid = "abuf.identityrename";

		public const string PluginName = "Identity Rename";

		public const string PluginVersion = "1.2.0";

		public static ManualLogSource Log;

		private void Awake()
		{
			Log = ((BaseUnityPlugin)this).Logger;
			Identity.Init(((BaseUnityPlugin)this).Config);
			try
			{
				PhotonNetwork.AddCallbackTarget((object)new IdentityJoinHook());
			}
			catch (Exception ex)
			{
				Log.LogWarning((object)("[Identity] 注册 Photon 回调失败:" + ex.Message));
			}
			Identity.Apply();
			Log.LogInfo((object)("Identity Rename 1.2.0 loaded." + ((Identity.Chosen.Length > 0) ? (" name=" + Identity.Chosen) : " name=Steam")));
		}
	}
}