diff --git a/Barotrauma/BarotraumaClient/Source/Networking/ChatMessage.cs b/Barotrauma/BarotraumaClient/Source/Networking/ChatMessage.cs index 75f22a273..4427bd23b 100644 --- a/Barotrauma/BarotraumaClient/Source/Networking/ChatMessage.cs +++ b/Barotrauma/BarotraumaClient/Source/Networking/ChatMessage.cs @@ -32,18 +32,26 @@ namespace Barotrauma.Networking if (NetIdUtils.IdMoreRecent(ID, LastID)) { - if (type == ChatMessageType.MessageBox) + switch (type) { - new GUIMessageBox("", txt); - } - else if (type == ChatMessageType.Console) - { - DebugConsole.NewMessage(txt, MessageColor[(int)ChatMessageType.Console]); - } - else - { - GameMain.Client.AddChatMessage(txt, type, senderName, senderCharacter); + case ChatMessageType.MessageBox: + new GUIMessageBox("", txt); + break; + case ChatMessageType.Console: + DebugConsole.NewMessage(txt, MessageColor[(int)ChatMessageType.Console]); + break; + case ChatMessageType.ServerLog: + if (!Enum.TryParse(senderName, out ServerLog.MessageType messageType)) + { + return; + } + GameMain.Client.ServerLog?.WriteLine(txt, messageType); + break; + default: + GameMain.Client.AddChatMessage(txt, type, senderName, senderCharacter); + break; } + LastID = ID; } } diff --git a/Barotrauma/BarotraumaClient/Source/Networking/GameClient.cs b/Barotrauma/BarotraumaClient/Source/Networking/GameClient.cs index cdfcd3de6..85779a7ce 100644 --- a/Barotrauma/BarotraumaClient/Source/Networking/GameClient.cs +++ b/Barotrauma/BarotraumaClient/Source/Networking/GameClient.cs @@ -113,6 +113,8 @@ namespace Barotrauma.Networking otherClients = new List(); + ServerLog = new ServerLog(""); + ChatMessage.LastID = 0; GameMain.NetLobbyScreen = new NetLobbyScreen(); } @@ -470,6 +472,9 @@ namespace Barotrauma.Networking #if DEBUG if (PlayerInput.GetKeyboardState.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.P)) return; #endif +#if CLIENT + if (ServerLog.LogFrame != null) ServerLog.LogFrame.Update(deltaTime); +#endif base.Update(deltaTime); @@ -670,9 +675,11 @@ namespace Barotrauma.Networking new GUITextBlock(new Rectangle(0, 0, 0, 15), permittedCommand, "", commandList, GUI.SmallFont).CanBeFocused = false; } } - + GameMain.NetLobbyScreen.SubList.Enabled = Voting.AllowSubVoting || HasPermission(ClientPermissions.SelectSub); GameMain.NetLobbyScreen.ModeList.Enabled = Voting.AllowModeVoting || HasPermission(ClientPermissions.SelectMode); + GameMain.NetLobbyScreen.InfoFrame.FindChild("showlog").Visible = HasPermission(ClientPermissions.ServerLog); + showLogButton.Visible = HasPermission(ClientPermissions.ServerLog); endRoundButton.Visible = HasPermission(ClientPermissions.EndRound); } @@ -929,6 +936,8 @@ namespace Barotrauma.Networking { GameMain.NetLobbyScreen.LastUpdateID = updateID; + ServerLog.ServerName = serverName; + GameMain.NetLobbyScreen.ServerName = serverName; GameMain.NetLobbyScreen.ServerMessage.Text = serverText; @@ -1398,6 +1407,12 @@ namespace Barotrauma.Networking public override void Disconnect() { client.Shutdown(""); + + if (HasPermission(ClientPermissions.ServerLog)) + { + ServerLog?.Save(); + } + GameMain.NetworkMember = null; } diff --git a/Barotrauma/BarotraumaClient/Source/Networking/GameServer.cs b/Barotrauma/BarotraumaClient/Source/Networking/GameServer.cs index 87919d6b8..2f5048fee 100644 --- a/Barotrauma/BarotraumaClient/Source/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaClient/Source/Networking/GameServer.cs @@ -9,45 +9,22 @@ namespace Barotrauma.Networking { private NetStats netStats; - private GUIButton showLogButton; - private GUIScrollBar clientListScrollBar; void InitProjSpecific() { - //---------------------------------------- - var endRoundButton = new GUIButton(new Rectangle(GameMain.GraphicsWidth - 170, 20, 150, 20), "End round", Alignment.TopLeft, "", inGameHUD); endRoundButton.OnClicked = (btn, userdata) => { EndGame(); return true; }; - - showLogButton = new GUIButton(new Rectangle(GameMain.GraphicsWidth - 170 - 170, 20, 150, 20), "Server Log", Alignment.TopLeft, "", inGameHUD); - showLogButton.OnClicked = (GUIButton button, object userData) => - { - if (log.LogFrame == null) - { - log.CreateLogFrame(); - } - else - { - log.LogFrame = null; - GUIComponent.KeyboardDispatcher.Subscriber = null; - } - return true; - }; - + GUIButton settingsButton = new GUIButton(new Rectangle(GameMain.GraphicsWidth - 170 - 170 - 170, 20, 150, 20), "Settings", Alignment.TopLeft, "", inGameHUD); settingsButton.OnClicked = ToggleSettingsFrame; settingsButton.UserData = "settingsButton"; - - //---------------------------------------- } public override void AddToGUIUpdateList() { - if (started) base.AddToGUIUpdateList(); - + base.AddToGUIUpdateList(); if (settingsFrame != null) settingsFrame.AddToGUIUpdateList(); - if (log.LogFrame != null) log.LogFrame.AddToGUIUpdateList(); } public override void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch) @@ -58,9 +35,9 @@ namespace Barotrauma.Networking { settingsFrame.Draw(spriteBatch); } - else if (log.LogFrame != null) + else if (ServerLog.LogFrame != null) { - log.LogFrame.Draw(spriteBatch); + ServerLog.LogFrame.Draw(spriteBatch); } if (Screen.Selected == GameMain.GameScreen && !GUI.DisableHUD) diff --git a/Barotrauma/BarotraumaClient/Source/Networking/NetworkMember.cs b/Barotrauma/BarotraumaClient/Source/Networking/NetworkMember.cs index 1bdd6110b..c6c126bc4 100644 --- a/Barotrauma/BarotraumaClient/Source/Networking/NetworkMember.cs +++ b/Barotrauma/BarotraumaClient/Source/Networking/NetworkMember.cs @@ -24,6 +24,7 @@ namespace Barotrauma.Networking protected GUIFrame inGameHUD; protected GUIListBox chatBox; protected GUITextBox chatMsgBox; + protected GUIButton showLogButton; public GUIFrame InGameHUD { @@ -35,6 +36,23 @@ namespace Barotrauma.Networking inGameHUD = new GUIFrame(new Rectangle(0, 0, 0, 0), null, null); inGameHUD.CanBeFocused = false; + showLogButton = new GUIButton(new Rectangle(GameMain.GraphicsWidth - 170 - 170, 20, 150, 20), "Server Log", Alignment.TopLeft, "", inGameHUD) + { + OnClicked = (GUIButton button, object userData) => + { + if (ServerLog.LogFrame == null) + { + ServerLog.CreateLogFrame(); + } + else + { + ServerLog.LogFrame = null; + GUIComponent.KeyboardDispatcher.Subscriber = null; + } + return true; + } + }; + int width = (int)MathHelper.Clamp(GameMain.GraphicsWidth * 0.35f, 350, 500); int height = (int)MathHelper.Clamp(GameMain.GraphicsHeight * 0.15f, 100, 200); chatBox = new GUIListBox(new Rectangle( @@ -109,39 +127,44 @@ namespace Barotrauma.Networking { inGameHUD.AddToGUIUpdateList(); } + if (ServerLog.LogFrame != null) ServerLog.LogFrame.AddToGUIUpdateList(); } public virtual void Draw(Microsoft.Xna.Framework.Graphics.SpriteBatch spriteBatch) { - if (!gameStarted || Screen.Selected != GameMain.GameScreen || GUI.DisableHUD) return; - - GameMain.GameSession.CrewManager.Draw(spriteBatch); - - inGameHUD.Draw(spriteBatch); - - if (respawnManager != null) + if (GUI.DisableHUD) return; + + if (gameStarted && Screen.Selected == GameMain.GameScreen) { - string respawnInfo = ""; + GameMain.GameSession.CrewManager.Draw(spriteBatch); - if (respawnManager.CurrentState == RespawnManager.State.Waiting && - respawnManager.CountdownStarted) - { - respawnInfo = respawnManager.UsingShuttle ? "Respawn Shuttle dispatching in " : "Respawning players in "; - respawnInfo = respawnManager.RespawnTimer <= 0.0f ? "" : respawnInfo + ToolBox.SecondsToReadableTime(respawnManager.RespawnTimer); - } - else if (respawnManager.CurrentState == RespawnManager.State.Transporting) - { - respawnInfo = respawnManager.TransportTimer <= 0.0f ? "" : "Shuttle leaving in " + ToolBox.SecondsToReadableTime(respawnManager.TransportTimer); - } + inGameHUD.Draw(spriteBatch); - if (!string.IsNullOrEmpty(respawnInfo)) + if (respawnManager != null) { - GUI.DrawString(spriteBatch, - new Vector2(120.0f, 10), - respawnInfo, Color.White, null, 0, GUI.SmallFont); - } + string respawnInfo = ""; + if (respawnManager.CurrentState == RespawnManager.State.Waiting && + respawnManager.CountdownStarted) + { + respawnInfo = respawnManager.UsingShuttle ? "Respawn Shuttle dispatching in " : "Respawning players in "; + respawnInfo = respawnManager.RespawnTimer <= 0.0f ? "" : respawnInfo + ToolBox.SecondsToReadableTime(respawnManager.RespawnTimer); + } + else if (respawnManager.CurrentState == RespawnManager.State.Transporting) + { + respawnInfo = respawnManager.TransportTimer <= 0.0f ? "" : "Shuttle leaving in " + ToolBox.SecondsToReadableTime(respawnManager.TransportTimer); + } + + if (!string.IsNullOrEmpty(respawnInfo)) + { + GUI.DrawString(spriteBatch, + new Vector2(120.0f, 10), + respawnInfo, Color.White, null, 0, GUI.SmallFont); + } + } } + + ServerLog.LogFrame?.Draw(spriteBatch); } public virtual bool SelectCrewCharacter(Character character, GUIComponent characterFrame) diff --git a/Barotrauma/BarotraumaClient/Source/Screens/NetLobbyScreen.cs b/Barotrauma/BarotraumaClient/Source/Screens/NetLobbyScreen.cs index 4240e64ea..f52552cf1 100644 --- a/Barotrauma/BarotraumaClient/Source/Screens/NetLobbyScreen.cs +++ b/Barotrauma/BarotraumaClient/Source/Screens/NetLobbyScreen.cs @@ -379,13 +379,13 @@ namespace Barotrauma showLogButton.UserData = "showlog"; showLogButton.OnClicked = (GUIButton button, object userData) => { - if (GameMain.Server.ServerLog.LogFrame == null) + if (GameMain.NetworkMember.ServerLog.LogFrame == null) { - GameMain.Server.ServerLog.CreateLogFrame(); + GameMain.NetworkMember.ServerLog.CreateLogFrame(); } else { - GameMain.Server.ServerLog.LogFrame = null; + GameMain.NetworkMember.ServerLog.LogFrame = null; GUIComponent.KeyboardDispatcher.Subscriber = null; } return true; @@ -435,7 +435,9 @@ namespace Barotrauma infoFrame.RemoveChild(infoFrame.children.Find(c => c.UserData as string == "settingsButton")); infoFrame.RemoveChild(infoFrame.children.Find(c => c.UserData as string == "spectateButton")); - InfoFrame.FindChild("showlog").Visible = GameMain.Server != null; + InfoFrame.FindChild("showlog").Visible = + GameMain.Server != null || + (GameMain.Client != null && GameMain.Client.HasPermission(ClientPermissions.ServerLog)); if (campaignViewButton == null) { diff --git a/Barotrauma/BarotraumaShared/Data/permissionpresets.xml b/Barotrauma/BarotraumaShared/Data/permissionpresets.xml index e909124d0..ab3f2b98d 100644 --- a/Barotrauma/BarotraumaShared/Data/permissionpresets.xml +++ b/Barotrauma/BarotraumaShared/Data/permissionpresets.xml @@ -7,8 +7,8 @@ + description="Allowed to manage round settings, kick players and access server logs." + permissions="EndRound,Kick,SelectSub,SelectMode,ManageCampaign,ConsoleCommands,ServerLog"> @@ -21,8 +21,8 @@ + description="Allowed to ban and kick players, manage round settings, access server logs and use nearly all console commands." + permissions="EndRound,Kick,Ban,SelectSub,SelectMode,ManageCampaign,ConsoleCommands,ServerLog"> diff --git a/Barotrauma/BarotraumaShared/Source/Networking/ChatMessage.cs b/Barotrauma/BarotraumaShared/Source/Networking/ChatMessage.cs index 688697f0b..6a28e95dd 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/ChatMessage.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/ChatMessage.cs @@ -7,7 +7,7 @@ namespace Barotrauma.Networking { enum ChatMessageType { - Default, Error, Dead, Server, Radio, Private, Console, MessageBox + Default, Error, Dead, Server, Radio, Private, Console, MessageBox, ServerLog } partial class ChatMessage diff --git a/Barotrauma/BarotraumaShared/Source/Networking/ClientPermissions.cs b/Barotrauma/BarotraumaShared/Source/Networking/ClientPermissions.cs index 262503795..ae4edc2a9 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/ClientPermissions.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/ClientPermissions.cs @@ -23,7 +23,9 @@ namespace Barotrauma.Networking [Description("Manage campaign")] ManageCampaign = 32, [Description("Console commands")] - ConsoleCommands = 64 + ConsoleCommands = 64, + [Description("Access server log")] + ServerLog = 128 } class PermissionPreset diff --git a/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs b/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs index f10433eba..1f5e1f4e5 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/GameServer.cs @@ -32,9 +32,7 @@ namespace Barotrauma.Networking private RestClient restClient; private bool masterServerResponded; private IRestResponse masterServerResponse; - - private ServerLog log; - + private bool initiatedStartGame; private CoroutineHandle startGameCoroutine; @@ -57,12 +55,7 @@ namespace Barotrauma.Networking { get { return entityEventManager; } } - - public ServerLog ServerLog - { - get { return log; } - } - + public TimeSpan UpdateInterval { get { return updateInterval; } @@ -115,7 +108,7 @@ namespace Barotrauma.Networking config.EnableMessageType(NetIncomingMessageType.ConnectionApproval); - log = new ServerLog(name); + ServerLog = new ServerLog(name); InitProjSpecific(); @@ -342,9 +335,9 @@ namespace Barotrauma.Networking #if CLIENT if (ShowNetStats) netStats.Update(deltaTime); if (settingsFrame != null) settingsFrame.Update(deltaTime); - if (log.LogFrame != null) log.LogFrame.Update(deltaTime); + if (ServerLog.LogFrame != null) ServerLog.LogFrame.Update(deltaTime); #endif - + if (!started) return; base.Update(deltaTime); @@ -1503,7 +1496,7 @@ namespace Barotrauma.Networking GameMain.NetLobbyScreen.LastUpdateID++; } - if (SaveServerLogs) log.Save(); + if (SaveServerLogs) ServerLog.Save(); Character.Controlled = null; @@ -1951,7 +1944,7 @@ namespace Barotrauma.Networking msg.Write((byte)transfer.SequenceChannel); server.SendMessage(msg, transfer.Connection, NetDeliveryMethod.ReliableOrdered, transfer.SequenceChannel); } - + public void UpdateVoteStatus() { if (server.Connections.Count == 0|| connectedClients.Count == 0) return; @@ -2260,7 +2253,15 @@ namespace Barotrauma.Networking { if (GameMain.Server == null || !GameMain.Server.SaveServerLogs) return; - GameMain.Server.log.WriteLine(line, messageType); + GameMain.Server.ServerLog.WriteLine(line, messageType); + + foreach (Client client in GameMain.Server.ConnectedClients) + { + if (!client.HasPermission(ClientPermissions.ServerLog)) continue; + //use sendername as the message type + GameMain.Server.SendChatMessage( + ChatMessage.Create(messageType.ToString(), line, ChatMessageType.ServerLog, null), client); + } } public override void Disconnect() @@ -2281,7 +2282,7 @@ namespace Barotrauma.Networking if (SaveServerLogs) { Log("Shutting down the server...", ServerLog.MessageType.ServerMessage); - log.Save(); + ServerLog.Save(); } GameAnalyticsManager.AddDesignEvent("GameServer:ShutDown"); diff --git a/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs b/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs index ae3c37d1c..f670bdb78 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/GameServerSettings.cs @@ -151,11 +151,11 @@ namespace Barotrauma.Networking { get { - return log.LinesPerFile; + return ServerLog.LinesPerFile; } set { - log.LinesPerFile = value; + ServerLog.LinesPerFile = value; } } diff --git a/Barotrauma/BarotraumaShared/Source/Networking/NetworkMember.cs b/Barotrauma/BarotraumaShared/Source/Networking/NetworkMember.cs index 742f6cbab..2aad99fba 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/NetworkMember.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/NetworkMember.cs @@ -122,6 +122,12 @@ namespace Barotrauma.Networking get { return respawnManager; } } + public ServerLog ServerLog + { + get; + protected set; + } + public NetworkMember() { InitProjSpecific(); diff --git a/Barotrauma/BarotraumaShared/Source/Networking/ServerLog.cs b/Barotrauma/BarotraumaShared/Source/Networking/ServerLog.cs index 8d26d9caf..a215eb251 100644 --- a/Barotrauma/BarotraumaShared/Source/Networking/ServerLog.cs +++ b/Barotrauma/BarotraumaShared/Source/Networking/ServerLog.cs @@ -59,9 +59,7 @@ namespace Barotrauma.Networking private int linesPerFile = 800; public const string SavePath = "ServerLogs"; - - private string serverName; - + private readonly Queue lines; private int unsavedLineCount; @@ -75,10 +73,11 @@ namespace Barotrauma.Networking set { linesPerFile = Math.Max(value, 10); } } + public string ServerName; + public ServerLog(string serverName) { - this.serverName = serverName; - + ServerName = serverName; lines = new Queue(); } @@ -139,7 +138,7 @@ namespace Barotrauma.Networking } } - string fileName = serverName + "_" + DateTime.Now.ToString("yyyy-MM-dd_HH:mm") + ".txt"; + string fileName = ServerName + "_" + DateTime.Now.ToString("yyyy-MM-dd_HH:mm") + ".txt"; var invalidChars = Path.GetInvalidFileNameChars(); foreach (char invalidChar in invalidChars) @@ -156,6 +155,7 @@ namespace Barotrauma.Networking catch (Exception e) { DebugConsole.ThrowError("Saving the server log to " + filePath + " failed", e); + return; } } }