From 2353c3ba37ae67bf748d62a21d704b6591bb1967 Mon Sep 17 00:00:00 2001 From: silentbaws Date: Fri, 29 May 2020 09:52:36 -0400 Subject: [PATCH] Add server messages motd, msg --- XLMultiplayer/MultiplayerController.cs | 13 ++++-- XLMultiplayerServer/FileServer.cs | 2 + XLMultiplayerServer/Server.cs | 64 +++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 6 deletions(-) diff --git a/XLMultiplayer/MultiplayerController.cs b/XLMultiplayer/MultiplayerController.cs index cd89352..76294e4 100644 --- a/XLMultiplayer/MultiplayerController.cs +++ b/XLMultiplayer/MultiplayerController.cs @@ -19,17 +19,14 @@ using XLShredLib; using XLShredLib.UI; -// TODO LIST FOR 0.8.0 - // TODO: Redo the multiplayer texture system // -> Send paths for non-custom gear // -> Send hashes of full size textures for custom gear along with compressed texture // -> Only send hashes/paths from server unless client requests texture data -// TODO: Create 2 connections -> One for gameplay -// -> One for files + // TODO: message of the day -// TODO: Fix dawgs board mod for mp + // TODO: server announce namespace XLMultiplayer { public enum OpCode : byte { @@ -43,6 +40,7 @@ public enum OpCode : byte { MapHash = 7, MapVote = 8, MapList = 9, + ServerMessage = 10, StillAlive = 254, Disconnect = 255 } @@ -749,6 +747,11 @@ private void ProcessMessage(byte[] buffer) { case OpCode.MapVote: Main.utilityMenu.SendImportantChat("MAP VOTING HAS BEGUN. THE MAP WILL CHANGE IN 30 SECONDS", 10000); break; + case OpCode.ServerMessage: + int duration = BitConverter.ToInt32(buffer, 1); + string messageText = ASCIIEncoding.ASCII.GetString(buffer, 5, buffer.Length - 5); + Main.utilityMenu.SendImportantChat(messageText, duration); + break; case OpCode.StillAlive: float sentTime = BitConverter.ToSingle(buffer, 1); pingTimes.Add(Time.time - sentTime); diff --git a/XLMultiplayerServer/FileServer.cs b/XLMultiplayerServer/FileServer.cs index 0750b60..7789173 100644 --- a/XLMultiplayerServer/FileServer.cs +++ b/XLMultiplayerServer/FileServer.cs @@ -38,6 +38,8 @@ public static void StatusCallbackFunction(ref StatusInfo info, IntPtr context) { break; } + if (Server.motdBytes != null) server.SendMessageToConnection(info.connection, Server.motdBytes); + Console.WriteLine("connected on file server"); } break; diff --git a/XLMultiplayerServer/Server.cs b/XLMultiplayerServer/Server.cs index 106961e..db59c2c 100644 --- a/XLMultiplayerServer/Server.cs +++ b/XLMultiplayerServer/Server.cs @@ -27,6 +27,7 @@ public enum OpCode : byte { MapHash = 7, MapVote = 8, MapList = 9, + ServerMessage = 10, StillAlive = 254, Disconnect = 255 } @@ -137,6 +138,7 @@ class Server { private static string currentMapHash = "1"; public static List bannedIPs = new List(); + public static byte[] motdBytes = null; public static int Main(String[] args) { Console.ForegroundColor = ConsoleColor.White; @@ -311,8 +313,68 @@ public static void CommandLoop() { } else { Console.WriteLine("Invalid player ID"); } + } else if (input.ToLower().StartsWith("msg")) { + byte[] messageBytes = ProcessMessageCommand(input); + if (messageBytes != null) { + foreach(Player player in players) { + if(player != null) { + FileServer.server.SendMessageToConnection(player.fileConnection, messageBytes, SendFlags.Reliable); + } + } + } + } else if (input.ToLower().StartsWith("motd")) { + byte[] messageBytes = ProcessMessageCommand(input); + if (messageBytes != null) { + motdBytes = messageBytes; + } + } + } + } + + public static byte[] ProcessMessageCommand(string input) { + //msg:duration:color content of message here + // - duration/color optional parameters so default duration is 10 seconds, default color ff00ff + string[] msgParams = input.Split(new char[] { ' ' }, 2); + + if (msgParams.Length < 2) return null; + + string[] msgSettings = msgParams[0].Split(':'); + + int duration = 10; + string msgColor = "ff00ff"; + + string acceptableCharacters = "abcdef0123456789"; + + if (msgSettings.Length > 1) { + if (Int32.TryParse(msgSettings[1], out duration)) { + duration = Math.Min(duration, 60); + duration = Math.Max(duration, 5); + } else { + duration = 10; } } + if (msgSettings.Length > 2) { + bool validColor = true; + + foreach (char c in msgSettings[2]) { + if (!acceptableCharacters.Contains(c.ToString())) { + validColor = false; + } + } + validColor = validColor && (msgSettings[2].Length == 3 || msgSettings[2].Length == 6); + + if (validColor) { + msgColor = msgSettings[2]; + } + } + + string messageText = $"{msgParams[1]}"; + byte[] messageBytes = new byte[messageText.Length + 5]; + messageBytes[0] = (byte)OpCode.ServerMessage; + Array.Copy(BitConverter.GetBytes(duration * 1000), 0, messageBytes, 1, 4); + Array.Copy(ASCIIEncoding.ASCII.GetBytes(messageText), 0, messageBytes, 5, messageText.Length); + + return messageBytes; } public static void ProcessMessage(byte[] buffer, byte fromID, NetworkingSockets server) { @@ -792,7 +854,7 @@ public static void HandleUsername(object data) { } } - Console.WriteLine("Connection {0}'s username is {1}", fromID, username); + Console.WriteLine("Connection {0}'s username is {1}", fromID, RemoveMarkup(username)); if (players[fromID] != null) { players[fromID].username = username;