diff --git a/src/Patches/PlayersManager_Patch.cs b/src/Patches/PlayersManager_Patch.cs new file mode 100644 index 0000000..26c4853 --- /dev/null +++ b/src/Patches/PlayersManager_Patch.cs @@ -0,0 +1,149 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Game; +using Game.Messages; +using Game.State; +using HarmonyLib; +using Network; +using Network.Client; +using Serilog; + +namespace tostilities.Patches; + +[HarmonyPatch(typeof(PlayersManager))] +[HarmonyPatch(nameof(PlayersManager.NotifyOfNewPlayers))] +public class PlayersManager_NotifyOfNewPlayers_Patch +{ + private static bool Prefix(ref PlayersManager __instance, Dictionary players) + { + if (!Main.MySettings.EnableWelcomeMessage && !Main.MySettings.ConsoleTimeStamps) + { + return true; //execute original function + } + + // ============= unchanged ============= + + HashSet playerIdSet = new HashSet(__instance._remotePlayers.Keys); + HashSet playerSet = new HashSet(); + foreach (KeyValuePair player1 in players) + { + PlayerId key = new PlayerId(player1.Key); + playerIdSet.Remove(key); + if (!__instance._remotePlayers.ContainsKey(key) && key != PlayersManager.PlayerId) + { + Snapshot.Player player2 = player1.Value; + playerSet.Add(player2); + } + } + if (playerSet.Any()) + { + Log.Information("Connected: {players}", playerSet); + string playerNames = string.Join(", ", playerSet.OrderBy(p => p.Name).Select(p => p.Name)); + bool flag = playerSet.Count != 1; + if (__instance._hasNotifiedOfPlayers) + { + // ============= changed ============= + + var timeString = Main.MySettings.ConsoleTimeStamps ? "" : DateTime.Now.ToString("HH:mm")+" "; + Console.Log($"{timeString}{playerNames} has connected."); + + if (Main.MySettings.EnableWelcomeMessage) + { + var message = Main.MySettings.WelcomeMessage.Replace(Settings.playername_replaceo, playerNames); + Multiplayer.Broadcast(message); + } + + // ============= unchanged ============= + } + else + { + Console.Log(playerNames + " " + (flag ? "are" : "is") + " connected."); + } + + __instance._hasNotifiedOfPlayers = true; + } + foreach (PlayerId playerId in playerIdSet) + { + RemotePlayer remotePlayer; + if (!__instance._remotePlayers.TryGetValue(playerId, out remotePlayer)) + { + Log.Error("Couldn't find name of disconnected player {playerId}", playerId); + } + else + { + Log.Information("{name} {playerId} has disconnected", remotePlayer.playerName, remotePlayer.playerId); + + // ============= changed ============= + + var timeString = Main.MySettings.ConsoleTimeStamps ? "" : DateTime.Now.ToString("HH:mm")+" "; + Console.Log($"{timeString}{remotePlayer.playerName} has disconnected."); + + // ============= unchanged ============= + } + } + + return false; //skip original function + } +} + +/* + +[HarmonyPatch(typeof(PlayersManager))] +[HarmonyPatch(nameof(PlayersManager.NotifyOfNewPlayers))] +public class PlayersManager_NotifyOfNewPlayers_Patch +{ + private static void Prefix(out List __state, PlayersManager __instance) + { + __state = __instance._remotePlayers.Keys.ToList(); + + foreach (var aaa in __instance._remotePlayers) + { + Console.Log("old:"+aaa.Value.Name); + } + } + + private static void Postfix(List __state, PlayersManager __instance) + { + // _hasNotifiedOfPlayers is false at server startup + if (!Main.MySettings.EnableWelcomeMessage || !__instance._hasNotifiedOfPlayers) + { + return; + } + + foreach (var aaa in __instance._remotePlayers) + { + Console.Log("new:"+aaa.Value.Name); + } + + if (__instance._remotePlayers.Keys.ToList() != __state) + { + Console.Log("player list changed!"); + } + + List newPlayerNames = new(); + + foreach (var aPlayerNow in __instance._remotePlayers) + { + Console.Log(aPlayerNow.Value.Name); + if (!__state.Contains(aPlayerNow.Key)) + { + Console.Log("new"); + //new player + newPlayerNames.Add(aPlayerNow.Value.Name); + } + else + { + Console.Log("newn't"); + } + } + + foreach (var playerName in newPlayerNames) + { + var message = Main.MySettings.WelcomeMessage.Replace(Settings.playername_replaceo, playerName); + Multiplayer.Broadcast(message); + } + } +} + +*/ \ No newline at end of file diff --git a/src/Settings.cs b/src/Settings.cs index 513ec16..8cc0aa9 100644 --- a/src/Settings.cs +++ b/src/Settings.cs @@ -6,6 +6,7 @@ namespace tostilities { public class Settings : UnityModManager.ModSettings { + public const string playername_replaceo = ""; private const int SPACE = 15; public float PushForceMultiplier = 10; @@ -16,6 +17,9 @@ public class Settings : UnityModManager.ModSettings public bool EnableBunnyHopping = false; public bool ReportDamage = false; public bool ConsoleTimeStamps = false; + + public bool EnableWelcomeMessage = false; + public string WelcomeMessage = ", welcome to the server!"; public void Setup() { @@ -39,6 +43,14 @@ public void Draw(UnityModManager.ModEntry modEntry) DisableDerailing = GUILayout.Toggle(DisableDerailing, "Disable derailing"); DisableDamage = GUILayout.Toggle(DisableDamage, "Disable damage to rolling stock"); ReportDamage = GUILayout.Toggle(ReportDamage, "Report damage to rolling stock and derailments in the console"); + + EnableWelcomeMessage = GUILayout.Toggle(EnableWelcomeMessage, "Show a customizable message in the console when a new player joins"); + + if (EnableWelcomeMessage) + { + GUILayout.Label($"Welcome message text. {playername_replaceo} will be replaced with the name of the player."); + WelcomeMessage = GUILayout.TextArea(WelcomeMessage); + } } private void DrawFloatInput(string descriptionText, ref string fieldText, ref float number)