Files
LuaCsForBarotraumaEP/Subsurface/Source/Networking/ServerLog.cs
Regalis cc4ada952f - File transfer improvements (switching the sub selection during transfer works, max transfer duration, waiting for transfers to finish before starting the round)
- Firesource changes (more particles with shorter lifetimes, combining bugfix)
- StatusEffects can target hulls and always be active
- Cyrillic character support
- Saving server settings
- Swapping items in inventory by dropping an item to a non-free slot
2016-02-27 21:01:10 +02:00

132 lines
3.9 KiB
C#

using Microsoft.Xna.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace Barotrauma.Networking
{
class ServerLog
{
private int linesPerFile = 800;
public const string SavePath = "ServerLogs";
private string serverName;
public GUIFrame LogFrame;
private GUIListBox listBox;
private Queue<ColoredText> lines;
public int LinesPerFile
{
get { return linesPerFile; }
set { linesPerFile = Math.Max(10, linesPerFile); }
}
public ServerLog(string serverName)
{
this.serverName = serverName;
lines = new Queue<ColoredText>();
}
public void WriteLine(string line, Color? color)
{
string logLine = "[" + DateTime.Now.ToLongTimeString() + "] " + line;
var newText = new ColoredText(logLine, color == null ? Color.White : (Color)color);
lines.Enqueue(newText);
if (LogFrame != null)
{
AddLine(newText);
listBox.UpdateScrollBarSize();
}
if (lines.Count >= LinesPerFile)
{
Save();
lines.Clear();
}
}
public void CreateLogFrame()
{
LogFrame = new GUIFrame(new Rectangle(0, 0, GameMain.GraphicsWidth, GameMain.GraphicsHeight), Color.Black * 0.5f);
GUIFrame innerFrame = new GUIFrame(new Rectangle(0,0,400, 400), null, Alignment.Center, GUI.Style, LogFrame);
innerFrame.Padding = new Vector4(10.0f, 10.0f, 10.0f, 10.0f);
listBox = new GUIListBox(new Rectangle(0,0,0,355), GUI.Style, innerFrame);
var currLines = lines.ToList();
foreach (ColoredText line in currLines)
{
AddLine(line);
}
listBox.UpdateScrollBarSize();
if (listBox.BarScroll==0.0f || listBox.BarScroll==1.0f) listBox.BarScroll = 1.0f;
GUIButton closeButton = new GUIButton(new Rectangle(0,0,100, 15), "Close", Alignment.BottomRight, GUI.Style, innerFrame);
closeButton.OnClicked = (GUIButton button, object userData) =>
{
LogFrame = null;
return true;
};
}
private void AddLine(ColoredText line)
{
var textBlock = new GUITextBlock(new Rectangle(0, 0, 0, 0), line.Text, GUI.Style, Alignment.TopLeft, Alignment.TopLeft, listBox, true, GUI.SmallFont);
textBlock.Rect = new Rectangle(textBlock.Rect.X, textBlock.Rect.Y, textBlock.Rect.Width, Math.Max(13, textBlock.Rect.Height));
textBlock.TextColor = line.Color;
textBlock.CanBeFocused = false;
}
public void Save()
{
if (!Directory.Exists(SavePath))
{
try
{
Directory.CreateDirectory(SavePath);
}
catch (Exception e)
{
DebugConsole.ThrowError("Failed to create a folder for server logs", e);
return;
}
}
string fileName = serverName+"_"+DateTime.Now.ToShortDateString()+"_"+DateTime.Now.ToShortTimeString()+".txt";
fileName = fileName.Replace(":", "");
fileName = fileName.Replace("../", "");
fileName = fileName.Replace("/", "");
string filePath = Path.Combine(SavePath, fileName);
try
{
File.WriteAllLines(filePath, lines.Select(l => l.Text));
}
catch (Exception e)
{
DebugConsole.ThrowError("Saving the server log to " + filePath + " failed", e);
}
lines.Clear();
}
}
}