From ee79fe5cc25acaee04eb3bfc0807f3b00b1b4dd7 Mon Sep 17 00:00:00 2001 From: Makinolo Date: Tue, 14 Dec 2021 20:24:03 -0700 Subject: [PATCH] Allows sending messages only to a group of users In some circumstances (like proximity, missions...) we may want to target messages to only a sub set of all the users connected to the server. Adding this optional parameter to SendModPacketToAll and SendChatMessageToAll allows that while being backwards compatible with the API Fixes some problems introduced in the non interactive mode Changes the SetLocalTraffic parameter to make it more readable so now SetLocalTraffic(true) means to ENABLE local traffic while SetLocalTraffic(false) means DISABLE local traffic --- Client/COOPAPI.cs | 6 +++--- Client/Main.cs | 2 ++ Client/Menus/Sub/Servers.cs | 4 ++-- Server/Server.cs | 5 +++-- Server/ServerScript.cs | 26 ++++++++++++++++++++++---- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/Client/COOPAPI.cs b/Client/COOPAPI.cs index 5a9feca..1ea8676 100644 --- a/Client/COOPAPI.cs +++ b/Client/COOPAPI.cs @@ -266,10 +266,10 @@ namespace CoopClient /// /// Enable or disable the local traffic for this player /// - /// - public static void SetLocalTraffic(bool stop) + /// + public static void SetLocalTraffic(bool enable) { - Main.DisableTraffic = stop; + Main.DisableTraffic = !enable; } #if DEBUG diff --git a/Client/Main.cs b/Client/Main.cs index 6e35e2f..1a7d25c 100644 --- a/Client/Main.cs +++ b/Client/Main.cs @@ -67,7 +67,9 @@ namespace CoopClient MainSettings = Util.ReadSettings(); MainNetworking = new Networking(); +#if !NON_INTERACTIVE MainMenu = new MenusMain(); +#endif MainChat = new Chat(); Players = new Dictionary(); NPCs = new Dictionary(); diff --git a/Client/Menus/Sub/Servers.cs b/Client/Menus/Sub/Servers.cs index 901ce75..86b98d4 100644 --- a/Client/Menus/Sub/Servers.cs +++ b/Client/Menus/Sub/Servers.cs @@ -105,11 +105,11 @@ namespace CoopClient.Menus.Sub MainMenu.Visible = false; Main.MainNetworking.DisConnectFromServer(address); - +#if !NON_INTERACTIVE Main.MainMenu.ServerIpItem.AltTitle = address; Main.MainMenu.MainMenu.Visible = true; - +#endif Main.MainSettings.LastServerAddress = address; Util.SaveSettings(); } diff --git a/Server/Server.cs b/Server/Server.cs index 018ecba..a17d8a8 100644 --- a/Server/Server.cs +++ b/Server/Server.cs @@ -166,9 +166,10 @@ namespace CoopServer { try { - Logging.Info("Loading resource..."); + string resourcepath = AppDomain.CurrentDomain.BaseDirectory + "resources" + Path.DirectorySeparatorChar + MainSettings.Resource + ".dll"; + Logging.Info($"Loading resource {resourcepath}..."); - Assembly asm = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "resources" + Path.DirectorySeparatorChar + MainSettings.Resource + ".dll"); + Assembly asm = Assembly.LoadFrom(resourcepath); Type[] types = asm.GetExportedTypes(); IEnumerable validTypes = types.Where(t => !t.IsInterface && !t.IsAbstract).Where(t => typeof(ServerScript).IsAssignableFrom(t)); Type[] enumerable = validTypes as Type[] ?? validTypes.ToArray(); diff --git a/Server/ServerScript.cs b/Server/ServerScript.cs index 23f0d81..b434a29 100644 --- a/Server/ServerScript.cs +++ b/Server/ServerScript.cs @@ -207,10 +207,19 @@ namespace CoopServer #endregion #region FUNCTIONS - public static void SendModPacketToAll(string mod, byte customID, byte[] bytes) + public static void SendModPacketToAll(string mod, byte customID, byte[] bytes, List playerIdList = null) { try { + List connections; + if (playerIdList == null) + { + connections = Server.MainNetServer.Connections; + } + else + { + connections = Server.MainNetServer.Connections.FindAll(c => playerIdList.Contains(c.RemoteUniqueIdentifier)); + } NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage(); new ModPacket() { @@ -220,7 +229,7 @@ namespace CoopServer CustomPacketID = customID, Bytes = bytes }.PacketToNetOutGoingMessage(outgoingMessage); - Server.MainNetServer.SendMessage(outgoingMessage, Server.MainNetServer.Connections, NetDeliveryMethod.ReliableOrdered, (int)ConnectionChannel.Mod); + Server.MainNetServer.SendMessage(outgoingMessage, connections, NetDeliveryMethod.ReliableOrdered, (int)ConnectionChannel.Mod); Server.MainNetServer.FlushSendQueue(); } catch (Exception e) @@ -285,7 +294,7 @@ namespace CoopServer return Server.Clients.Find(x => x.Player.Username.ToLower() == username.ToLower()); } - public static void SendChatMessageToAll(string message, string username = "Server") + public static void SendChatMessageToAll(string message, string username = "Server", List playerIdList = null) { try { @@ -294,13 +303,22 @@ namespace CoopServer return; } + List connections; + if (playerIdList == null) + { + connections = Server.MainNetServer.Connections; + } + else + { + connections = Server.MainNetServer.Connections.FindAll(c => playerIdList.Contains(c.RemoteUniqueIdentifier)); + } NetOutgoingMessage outgoingMessage = Server.MainNetServer.CreateMessage(); new ChatMessagePacket() { Username = username, Message = message }.PacketToNetOutGoingMessage(outgoingMessage); - Server.MainNetServer.SendMessage(outgoingMessage, Server.MainNetServer.Connections, NetDeliveryMethod.ReliableOrdered, (int)ConnectionChannel.Chat); + Server.MainNetServer.SendMessage(outgoingMessage, connections, NetDeliveryMethod.ReliableOrdered, (int)ConnectionChannel.Chat); } catch (Exception e) {