public ChunkMChat SendMChat(GhostNetConnection con, GhostNetFrame frame, string text, string tag = null, Color?color = null, bool fillVars = false) { ChunkMChat msg = CreateMChat(frame, text, tag, color, fillVars); con.SendManagement(new GhostNetFrame { new ChunkHHead { PlayerID = uint.MaxValue }, msg }, true); return(msg); }
public ChunkMChat BroadcastMChat(GhostNetFrame frame, string text, string tag = null, Color?color = null, bool fillVars = false) { ChunkMChat msg = CreateMChat(frame, text, tag, color ?? GhostNetModule.Settings.ServerColorBroadcast, fillVars); PropagateM(new GhostNetFrame { new ChunkHHead { PlayerID = uint.MaxValue }, msg }); return(msg); }
public ChunkMChat CreateMChat(GhostNetFrame frame, string text, string tag = null, Color?color = null, bool fillVars = false, uint?id = null) { lock (ChatLog) { ChunkMChat chunk = new ChunkMChat { ID = id ?? (uint)ChatLog.Count, Text = fillVars ? FillVariables(text, frame) : text, Tag = tag ?? "", Color = color ?? GhostNetModule.Settings.ServerColorDefault, Date = DateTime.UtcNow, CreatedByServer = true, Logged = true }; if (id == null) { ChatLog.Add(chunk); } else { ChatLog[(int)id] = chunk; } return(chunk); } }
public ChunkMChat Send(GhostNetFrame frame, string text, string tag = "race", Color?color = null, bool fillVars = false, uint?id = null) { ChunkMChat msg = Manager.Server.CreateMChat(frame, text, tag, color ?? (frame != null ? ColorDefault : ColorBroadcast), fillVars, id); GhostNetFrame frameMsg = new GhostNetFrame { frame?.HHead ?? new ChunkHHead { PlayerID = uint.MaxValue }, msg }; lock (Players) { foreach (uint playerID in Players) { GhostNetConnection con = Manager.Server.Connections[(int)playerID]; if (con == null) { continue; } con.SendManagement(frameMsg, false); } } return(msg); }
public virtual void HandleMChat(GhostNetConnection con, GhostNetFrame frame) { ChunkMChat msg = frame; msg.Text = msg.Text.TrimEnd(); // Logger.Log(LogLevel.Info, "ghostnet-s", $"#{frame.HHead.PlayerID} said: {frame.MChat.Text}"); if (!msg.Logged) { lock (ChatLog) { msg.ID = (uint)ChatLog.Count; ChatLog.Add(msg); } } // Handle commands if necessary. if (msg.Text.StartsWith(GhostNetModule.Settings.ServerCommandPrefix)) { // Echo the chat chunk separately. msg.Color = GhostNetModule.Settings.ServerColorCommand; con.SendManagement(new GhostNetFrame { frame.HHead, msg }, true); GhostNetCommandEnv env = new GhostNetCommandEnv { Server = this, Connection = con, Frame = frame }; string prefix = GhostNetModule.Settings.ServerCommandPrefix; // TODO: This is basically a port of disbot-neo's Handler. string cmdName = env.Text.Substring(prefix.Length); cmdName = cmdName.Split(GhostNetCommand.CommandNameDelimiters)[0].ToLowerInvariant(); if (cmdName.Length == 0) { return; } GhostNetCommand cmd = GetCommand(cmdName); if (cmd != null) { GhostNetFrame cmdFrame = frame; Task.Run(() => { try { cmd.Parse(env); } catch (Exception e) { SendMChat(con, cmdFrame, $"Command {cmdName} failed: {e.Message}", color: GhostNetModule.Settings.ServerColorError, fillVars: false); if (e.GetType() != typeof(Exception)) { Logger.Log(LogLevel.Warn, "ghostnet-s", $"cmd failed: {env.Text}"); e.LogDetailed(); } } }); } else { SendMChat(con, frame, $"Command {cmdName} not found!", color: GhostNetModule.Settings.ServerColorError, fillVars: false); } return; } if (!msg.CreatedByServer) { msg.Text.Replace("\r", "").Replace("\n", ""); if (msg.Text.Length > GhostNetModule.Settings.ServerMaxChatTextLength) { msg.Text = msg.Text.Substring(0, GhostNetModule.Settings.ServerMaxChatTextLength); } msg.Tag = ""; msg.Color = Color.White; } msg.Date = DateTime.UtcNow; frame.PropagateM = true; }