Decompiled source of JoinNotice v1.5.2

BepInEx/plugins/MaoNu.JoinNotice.dll

Decompiled a week ago
using System;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Security;
using System.Security.Permissions;
using BepInEx;
using BepInEx.Configuration;
using FishNet.Object;
using HarmonyLib;
using Microsoft.CodeAnalysis;
using UnityEngine;

[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: SecurityPermission(SecurityAction.RequestMinimum, SkipVerification = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[module: UnverifiableCode]
[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 JoinNotice
{
	public static class PluginInfo
	{
		public const string PLUGIN_GUID = "MaoNu.JoinNotice";

		public const string PLUGIN_NAME = "JoinNotice";

		public const string PLUGIN_VERSION = "1.5.0";
	}
	[BepInPlugin("MaoNu.JoinNotice", "JoinNotice", "1.5.0")]
	public class Plugin : BaseUnityPlugin
	{
		private ConfigEntry<bool> isJoinLeaveNoticeEnabled;

		private ConfigEntry<string> languageMode;

		private ConfigEntry<bool> debugLogging;

		private Harmony _harmony;

		public static Plugin Instance { get; private set; }

		public bool IsJoinLeaveNoticeEnabled
		{
			get
			{
				if (isJoinLeaveNoticeEnabled != null)
				{
					return isJoinLeaveNoticeEnabled.Value;
				}
				return false;
			}
		}

		public bool DebugLogging
		{
			get
			{
				if (debugLogging != null)
				{
					return debugLogging.Value;
				}
				return false;
			}
		}

		public string LanguageMode
		{
			get
			{
				if (languageMode == null)
				{
					return "auto";
				}
				return languageMode.Value;
			}
		}

		public void LogInfo(string msg)
		{
			((BaseUnityPlugin)this).Logger.LogInfo((object)msg);
		}

		public void LogWarning(string msg)
		{
			((BaseUnityPlugin)this).Logger.LogWarning((object)msg);
		}

		public void LogError(string msg)
		{
			((BaseUnityPlugin)this).Logger.LogError((object)msg);
		}

		public void LogDebug(string msg)
		{
			if (DebugLogging)
			{
				((BaseUnityPlugin)this).Logger.LogInfo((object)msg);
			}
		}

		private void Awake()
		{
			//IL_0082: Unknown result type (might be due to invalid IL or missing references)
			//IL_008c: Expected O, but got Unknown
			//IL_00c3: Unknown result type (might be due to invalid IL or missing references)
			//IL_00cd: Expected O, but got Unknown
			//IL_0116: Unknown result type (might be due to invalid IL or missing references)
			//IL_0123: Expected O, but got Unknown
			//IL_01c5: Unknown result type (might be due to invalid IL or missing references)
			//IL_01d2: Expected O, but got Unknown
			Instance = this;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"[JoinNotice] v1.5.0 loaded!");
			isJoinLeaveNoticeEnabled = ((BaseUnityPlugin)this).Config.Bind<bool>("提示设置", "玩家加入退出提示", true, "是否在聊天窗显示玩家加入/退出提示");
			languageMode = ((BaseUnityPlugin)this).Config.Bind<string>("提示设置", "语言", "auto", new ConfigDescription("提示语言:auto=跟随客户端语言,zh=强制简体中文,zh-tw=强制繁体中文,en=强制英文", (AcceptableValueBase)(object)new AcceptableValueList<string>(new string[4] { "auto", "zh", "zh-tw", "en" }), Array.Empty<object>()));
			debugLogging = ((BaseUnityPlugin)this).Config.Bind<bool>("调试设置", "详细日志", true, "输出详细调试日志(排查问题用)");
			L.ForcedLang = LanguageMode;
			_harmony = new Harmony("MaoNu.JoinNotice.Patches");
			try
			{
				MethodInfo methodInfo = AccessTools.Method(typeof(PlayerManager), "AddPlayer", (Type[])null, (Type[])null);
				if (methodInfo != null)
				{
					MethodInfo methodInfo2 = AccessTools.Method(typeof(PlayerJoinLeavePatch), "AddPlayerPostfix", (Type[])null, (Type[])null);
					if (methodInfo2 != null)
					{
						_harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, new HarmonyMethod(methodInfo2), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
						((BaseUnityPlugin)this).Logger.LogInfo((object)"[玩家加入提示] PlayerManager.AddPlayer 补丁安装成功");
					}
					else
					{
						((BaseUnityPlugin)this).Logger.LogError((object)"[玩家加入提示] 找不到 AddPlayerPostfix 方法");
					}
				}
				else
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)"[玩家加入提示] 找不到 PlayerManager.AddPlayer");
				}
			}
			catch (Exception ex)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("[玩家加入提示] " + ex.Message));
			}
			try
			{
				MethodInfo methodInfo3 = AccessTools.Method(typeof(PlayerManager), "RemovePlayer", (Type[])null, (Type[])null);
				if (methodInfo3 != null)
				{
					MethodInfo methodInfo4 = AccessTools.Method(typeof(PlayerJoinLeavePatch), "RemovePlayerPostfix", (Type[])null, (Type[])null);
					if (methodInfo4 != null)
					{
						_harmony.Patch((MethodBase)methodInfo3, (HarmonyMethod)null, new HarmonyMethod(methodInfo4), (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
						((BaseUnityPlugin)this).Logger.LogInfo((object)"[玩家退出提示] PlayerManager.RemovePlayer 补丁安装成功");
					}
					else
					{
						((BaseUnityPlugin)this).Logger.LogError((object)"[玩家退出提示] 找不到 RemovePlayerPostfix 方法");
					}
				}
				else
				{
					((BaseUnityPlugin)this).Logger.LogWarning((object)"[玩家退出提示] 找不到 PlayerManager.RemovePlayer");
				}
			}
			catch (Exception ex2)
			{
				((BaseUnityPlugin)this).Logger.LogError((object)("[玩家退出提示] " + ex2.Message));
			}
			try
			{
				Server.OnServerStarted += delegate
				{
					PlayerJoinLeavePatch.ClearState();
					((BaseUnityPlugin)this).Logger.LogInfo((object)"[玩家加入提示] 服务端启动,已清空状态");
				};
			}
			catch (Exception ex3)
			{
				((BaseUnityPlugin)this).Logger.LogWarning((object)("[玩家加入提示] 挂钩 OnServerStarted 失败: " + ex3.Message));
			}
		}

		private void OnDestroy()
		{
			try
			{
				Harmony harmony = _harmony;
				if (harmony != null)
				{
					harmony.UnpatchSelf();
				}
			}
			catch
			{
			}
		}
	}
	internal static class L
	{
		public static string ForcedLang = "auto";

		public static bool IsSimplifiedChinese()
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_0049: Unknown result type (might be due to invalid IL or missing references)
			//IL_004a: Unknown result type (might be due to invalid IL or missing references)
			//IL_004c: Invalid comparison between Unknown and I4
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_0051: Invalid comparison between Unknown and I4
			if (ForcedLang == "zh")
			{
				return true;
			}
			if (ForcedLang == "zh-tw" || ForcedLang == "en")
			{
				return false;
			}
			try
			{
				return LocalizationManager.CurLanguage == 2;
			}
			catch
			{
				SystemLanguage systemLanguage = Application.systemLanguage;
				return (int)systemLanguage == 6 || (int)systemLanguage == 40;
			}
		}

		public static bool IsTraditionalChinese()
		{
			//IL_0044: Unknown result type (might be due to invalid IL or missing references)
			//IL_004b: Invalid comparison between Unknown and I4
			if (ForcedLang == "zh-tw")
			{
				return true;
			}
			if (ForcedLang == "zh" || ForcedLang == "en")
			{
				return false;
			}
			try
			{
				return LocalizationManager.CurLanguage == 3;
			}
			catch
			{
				return (int)Application.systemLanguage == 41;
			}
		}

		public static string Join(string name)
		{
			if (IsTraditionalChinese())
			{
				return "【加入提示】" + name + " 加入了遊戲!";
			}
			if (IsSimplifiedChinese())
			{
				return "【加入提示】" + name + " 加入了游戏!";
			}
			return "[Join] " + name + " joined the game!";
		}

		public static string Leave(string name)
		{
			if (IsTraditionalChinese())
			{
				return "【退出提示】" + name + " 退出了遊戲!";
			}
			if (IsSimplifiedChinese())
			{
				return "【退出提示】" + name + " 退出了游戏!";
			}
			return "[Leave] " + name + " left the game!";
		}
	}
	internal static class PlayerJoinLeavePatch
	{
		public static void ClearState()
		{
		}

		public static void AddPlayerPostfix(Player player)
		{
			if ((Object)(object)Plugin.Instance == (Object)null)
			{
				return;
			}
			Plugin.Instance.LogDebug("[玩家加入] AddPlayerPostfix 被调用!player=" + (((Object)(object)player == (Object)null) ? "null" : (player.SteamName ?? "(SteamName为空)")));
			if (!((Object)(object)player == (Object)null) && Plugin.Instance.IsJoinLeaveNoticeEnabled)
			{
				bool flag = (Object)(object)Server.Instance != (Object)null && ((NetworkBehaviour)Server.Instance).IsServerInitialized;
				Plugin.Instance.LogDebug($"[玩家加入] isServer={flag}");
				if (flag)
				{
					((MonoBehaviour)Plugin.Instance).StartCoroutine(SendJoinMessageDelayed(player));
				}
			}
		}

		public static void RemovePlayerPostfix(Player player)
		{
			if ((Object)(object)Plugin.Instance == (Object)null)
			{
				return;
			}
			Plugin.Instance.LogDebug("[玩家退出] RemovePlayerPostfix 被调用!player=" + (((Object)(object)player == (Object)null) ? "null" : (player.SteamName ?? "(SteamName为空)")));
			if ((Object)(object)player == (Object)null || !Plugin.Instance.IsJoinLeaveNoticeEnabled)
			{
				return;
			}
			bool flag = (Object)(object)Server.Instance != (Object)null && ((NetworkBehaviour)Server.Instance).IsServerInitialized;
			Plugin.Instance.LogDebug($"[玩家退出] isServer={flag}");
			if (!flag)
			{
				return;
			}
			try
			{
				string name = (string.IsNullOrEmpty(player.SteamName) ? "未知玩家" : player.SteamName);
				ulong steamID = 0uL;
				try
				{
					steamID = player.SteamID;
				}
				catch
				{
				}
				SendSystemMessage(steamID, L.Leave(name));
			}
			catch (Exception ex)
			{
				Plugin.Instance.LogWarning("[玩家退出提示] " + ex.Message);
			}
		}

		private static IEnumerator SendJoinMessageDelayed(Player player)
		{
			if ((Object)(object)Plugin.Instance == (Object)null)
			{
				yield break;
			}
			for (int i = 0; i < 30; i++)
			{
				yield return null;
			}
			if ((Object)(object)player == (Object)null || (Object)(object)Server.Instance == (Object)null || !((NetworkBehaviour)Server.Instance).IsServerInitialized)
			{
				yield break;
			}
			if (string.IsNullOrEmpty(player.SteamName))
			{
				for (int i = 0; i < 60; i++)
				{
					yield return null;
					if ((Object)(object)player == (Object)null)
					{
						yield break;
					}
					if (!string.IsNullOrEmpty(player.SteamName))
					{
						break;
					}
				}
			}
			if (!((Object)(object)player == (Object)null))
			{
				string text = (string.IsNullOrEmpty(player.SteamName) ? "未知玩家" : player.SteamName);
				ulong num = 0uL;
				try
				{
					num = player.SteamID;
				}
				catch
				{
				}
				Plugin.Instance.LogDebug($"[玩家加入] 准备发送消息 steamID={num} name={text}");
				SendSystemMessage(num, L.Join(text));
			}
		}

		private static void SendSystemMessage(ulong steamID, string message)
		{
			if ((Object)(object)Plugin.Instance == (Object)null)
			{
				return;
			}
			try
			{
				OnlineChatManager instance = OnlineChatManager.Instance;
				if ((Object)(object)instance != (Object)null)
				{
					instance.SendChatMessage(steamID, message);
					Plugin.Instance.LogDebug("[系统消息] 已通过 OnlineChatManager 发送: " + message);
				}
				else
				{
					Plugin.Instance.LogWarning("[系统消息] OnlineChatManager.Instance 为 null(聊天系统未就绪)");
				}
			}
			catch (Exception ex)
			{
				Plugin.Instance.LogWarning("[系统消息] OnlineChatManager 调用失败: " + ex.Message);
			}
		}
	}
}