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;
|
2018-10-25 09:14:39 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2018-08-30 21:53:00 -04:00
|
|
|
|
public int UpdateInterval => 300;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
public GameLogReader(string logFile, IEventParser parser)
|
|
|
|
|
{
|
|
|
|
|
LogFile = logFile;
|
|
|
|
|
Parser = parser;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-25 09:14:39 -04:00
|
|
|
|
public async Task<ICollection<GameEvent>> ReadEventsFromLog(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-10-25 09:14:39 -04:00
|
|
|
|
// todo: max async
|
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-10-06 12:47:14 -04:00
|
|
|
|
server.Logger.WriteWarning("Could not properly parse event line");
|
|
|
|
|
server.Logger.WriteDebug(e.Message);
|
|
|
|
|
server.Logger.WriteDebug(eventLine);
|
2018-04-26 16:26:03 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return events;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|