Client LuaCsForBarotrauma
BarotraumaShared/SharedSource/Networking/ServerLog.cs
1 using Microsoft.Xna.Framework;
2 using System;
3 using System.Collections.Generic;
4 using Barotrauma.IO;
5 using System.Linq;
6 
7 namespace Barotrauma.Networking
8 {
9  public partial class ServerLog
10  {
11  private struct LogMessage
12  {
13  public readonly RichString Text;
14  public readonly MessageType Type;
15 
16  public LogMessage(string text, MessageType type)
17  {
18  if (type.HasFlag(MessageType.Chat))
19  {
20  text = $"[{DateTime.Now}]\n {text}";
21  }
22  else
23  {
24  text = $"[{DateTime.Now}]\n {TextManager.GetServerMessage(text)}";
25  }
26  Text = RichString.Rich(text);
27  Type = type;
28  }
29  }
30 
31  public enum MessageType
32  {
33  Chat,
34  ItemInteraction,
35  Inventory,
36  Attack,
37  Spawning,
38  Wiring,
39  ServerMessage,
40  ConsoleUsage,
41  Money,
42  DoSProtection,
43  Karma,
44  Talent,
45  Traitors,
46  Error,
47  }
48 
49  private readonly Dictionary<MessageType, Color> messageColor = new Dictionary<MessageType, Color>
50  {
51  { MessageType.Chat, Color.LightBlue },
52  { MessageType.ItemInteraction, new Color(205, 205, 180) },
53  { MessageType.Inventory, new Color(255, 234, 85) },
54  { MessageType.Attack, new Color(204, 74, 78) },
55  { MessageType.Spawning, new Color(163, 73, 164) },
56  { MessageType.Wiring, new Color(255, 157, 85) },
57  { MessageType.ServerMessage, new Color(157, 225, 160) },
58  { MessageType.ConsoleUsage, new Color(0, 162, 232) },
59  { MessageType.Money, Color.Green },
60  { MessageType.DoSProtection, Color.OrangeRed },
61  { MessageType.Karma, new Color(75, 88, 255) },
62  { MessageType.Talent, new Color(125, 125, 255) },
63  { MessageType.Traitors, new Color(107, 69, 158) },
64  { MessageType.Error, Color.Red }
65  };
66 
67  private readonly Dictionary<MessageType, string> messageTypeName = new Dictionary<MessageType, string>
68  {
69  { MessageType.Chat, "ChatMessage" },
70  { MessageType.ItemInteraction, "ItemInteraction" },
71  { MessageType.Inventory, "InventoryUsage" },
72  { MessageType.Attack, "AttackDeath" },
73  { MessageType.Spawning, "Spawning" },
74  { MessageType.Wiring, "Wiring" },
75  { MessageType.ServerMessage, "ServerMessage" },
76  { MessageType.ConsoleUsage, "ConsoleUsage" },
77  { MessageType.Money, "Money" },
78  { MessageType.DoSProtection, "DoSProtection" },
79  { MessageType.Karma, "Karma" },
80  { MessageType.Talent, "Talent" },
81  { MessageType.Traitors, "Traitors" },
82  { MessageType.Error, "Error" }
83  };
84 
85  private int linesPerFile = 800;
86 
87  public const string SavePath = "ServerLogs";
88 
89  private readonly Queue<LogMessage> lines;
90  private readonly Queue<LogMessage> unsavedLines;
91 
92  private readonly bool[] msgTypeHidden = new bool[Enum.GetValues(typeof(MessageType)).Length];
93 
94  public int LinesPerFile
95  {
96  get { return linesPerFile; }
97  set { linesPerFile = Math.Max(value, 10); }
98  }
99 
100  public string ServerName;
101 
102  public ServerLog(string serverName)
103  {
104  ServerName = serverName;
105  lines = new Queue<LogMessage>();
106  unsavedLines = new Queue<LogMessage>();
107 
108  foreach (MessageType messageType in Enum.GetValues(typeof(MessageType)))
109  {
110  System.Diagnostics.Debug.Assert(messageColor.ContainsKey(messageType));
111  System.Diagnostics.Debug.Assert(messageTypeName.ContainsKey(messageType));
112  }
113  }
114 
115  public void WriteLine(string line, MessageType messageType, bool logToConsole = true)
116  {
117  //string logLine = "[" + DateTime.Now.ToLongTimeString() + "] " + line;
118 
119  var newText = new LogMessage(line, messageType);
120 
121 #if SERVER
122  if (logToConsole)
123  {
124  DebugConsole.NewMessage(newText.Text.SanitizedValue, messageColor[messageType]); //TODO: REMOVE
125  }
126 #endif
127 
128  lines.Enqueue(newText);
129  unsavedLines.Enqueue(newText);
130 
131 #if CLIENT
132  if (listBox != null)
133  {
134  AddLine(newText);
135  listBox.UpdateScrollBarSize();
136  }
137 #endif
138  if (unsavedLines.Count >= LinesPerFile)
139  {
140  Save();
141  unsavedLines.Clear();
142  }
143 
144  while (lines.Count > LinesPerFile)
145  {
146  lines.Dequeue();
147  }
148 
149 #if CLIENT
150  while (listBox != null && listBox.Content.CountChildren > LinesPerFile)
151  {
152  listBox.Content.RemoveChild(!reverseOrder ? listBox.Content.Children.First() : listBox.Content.Children.Last());
153  }
154 #endif
155  }
156 
157  public void Save()
158  {
159  if (!Directory.Exists(SavePath))
160  {
161  try
162  {
163  Directory.CreateDirectory(SavePath);
164  }
165  catch (Exception e)
166  {
167  DebugConsole.ThrowError("Failed to create a folder for server logs", e);
168  return;
169  }
170  }
171 
172  string fileName = ServerName + "_" + DateTime.Now.ToString("yyyy-MM-dd_HH:mm");
173  fileName = ToolBox.RemoveInvalidFileNameChars(fileName);
174 
175  string filePath = Path.Combine(SavePath, fileName + ".txt");
176  int i = 2;
177  while (File.Exists(filePath))
178  {
179  filePath = Path.Combine(SavePath, fileName + " (" + i + ").txt");
180  i++;
181  }
182 
183  try
184  {
185  File.WriteAllLines(filePath, unsavedLines.Select(l => l.Text.SanitizedValue));
186  }
187  catch (Exception e)
188  {
189  DebugConsole.ThrowError("Saving the server log to " + filePath + " failed", e);
190  return;
191  }
192  }
193  }
194 }
Attacks are used to deal damage to characters, structures and items. They can be defined in the weapo...
virtual void RemoveChild(GUIComponent child)
Definition: GUIComponent.cs:87
IEnumerable< GUIComponent > Children
Definition: GUIComponent.cs:29
GUIFrame Content
A frame that contains the contents of the listbox. The frame itself is not rendered.
Definition: GUIListBox.cs:33
void WriteLine(string line, MessageType messageType, bool logToConsole=true)
static RichString Rich(LocalizedString str, Func< string, string >? postProcess=null)
Definition: RichString.cs:67