2018-04-26 02:13:04 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.IO
|
|
|
|
|
{
|
2018-08-28 17:32:59 -04:00
|
|
|
|
class GameLogReader : IGameLogReader
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
|
|
|
|
IEventParser Parser;
|
2018-08-03 22:11:58 -04:00
|
|
|
|
readonly string LogFile;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-08-28 17:32:59 -04:00
|
|
|
|
public long Length => new FileInfo(LogFile).Length;
|
|
|
|
|
|
|
|
|
|
public int UpdateInterval => 100;
|
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
public GameLogReader(string logFile, IEventParser parser)
|
|
|
|
|
{
|
|
|
|
|
LogFile = logFile;
|
|
|
|
|
Parser = parser;
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 16:26:03 -04:00
|
|
|
|
public ICollection<GameEvent> EventsFromLog(Server server, long fileSizeDiff, long startPosition)
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
|
|
|
|
// allocate the bytes for the new log lines
|
2018-04-26 16:26:03 -04:00
|
|
|
|
List<string> logLines = new List<string>();
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
|
|
|
|
// open the file as a stream
|
2018-04-26 16:26:03 -04:00
|
|
|
|
using (var rd = new StreamReader(new FileStream(LogFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Utilities.EncodingType))
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2018-04-26 16:26:03 -04:00
|
|
|
|
// take the old start position and go back the number of new characters
|
|
|
|
|
rd.BaseStream.Seek(-fileSizeDiff, SeekOrigin.End);
|
2018-04-26 02:13:04 -04:00
|
|
|
|
// the difference should be in the range of a int :P
|
2018-04-26 16:26:03 -04:00
|
|
|
|
string newLine;
|
|
|
|
|
while (!String.IsNullOrEmpty(newLine = rd.ReadLine()))
|
|
|
|
|
{
|
2018-05-10 01:34:29 -04:00
|
|
|
|
logLines.Add(newLine);
|
2018-04-26 16:26:03 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
List<GameEvent> events = new List<GameEvent>();
|
|
|
|
|
|
|
|
|
|
// parse each line
|
|
|
|
|
foreach (string eventLine in logLines)
|
|
|
|
|
{
|
|
|
|
|
if (eventLine.Length > 0)
|
2018-04-26 16:26:03 -04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// todo: catch elsewhere
|
|
|
|
|
events.Add(Parser.GetEvent(server, eventLine));
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-14 13:55:10 -04:00
|
|
|
|
catch (Exception e)
|
2018-04-26 16:26:03 -04:00
|
|
|
|
{
|
2018-05-14 13:55:10 -04:00
|
|
|
|
Program.ServerManager.GetLogger().WriteWarning("Could not properly parse event line");
|
|
|
|
|
Program.ServerManager.GetLogger().WriteDebug(e.Message);
|
|
|
|
|
Program.ServerManager.GetLogger().WriteDebug(eventLine);
|
2018-04-26 16:26:03 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return events;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|