2018-04-26 02:13:04 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2019-05-13 11:36:11 -04:00
|
|
|
|
using System.Linq;
|
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;
|
2019-05-13 11:36:11 -04:00
|
|
|
|
private bool? ignoreBots;
|
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
|
|
|
|
{
|
2019-05-13 11:36:11 -04:00
|
|
|
|
if (!ignoreBots.HasValue)
|
|
|
|
|
{
|
|
|
|
|
ignoreBots = server.Manager.GetApplicationSettings().Configuration().IgnoreBots;
|
|
|
|
|
}
|
|
|
|
|
|
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);
|
2019-05-13 11:36:11 -04:00
|
|
|
|
|
2018-04-26 16:26:03 -04:00
|
|
|
|
string newLine;
|
2019-03-09 11:28:04 -05:00
|
|
|
|
while (!string.IsNullOrEmpty(newLine = await rd.ReadLineAsync()))
|
2018-04-26 16:26:03 -04:00
|
|
|
|
{
|
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
|
|
|
|
|
{
|
2019-05-13 11:36:11 -04:00
|
|
|
|
var gameEvent = Parser.GenerateGameEvent(eventLine);
|
|
|
|
|
// we don't want to add the even if ignoreBots is on and the event comes froma bot
|
|
|
|
|
if (!ignoreBots.Value || (ignoreBots.Value && (gameEvent.Origin.NetworkId != -1 || gameEvent.Target.NetworkId != -1)))
|
|
|
|
|
{
|
|
|
|
|
gameEvent.Owner = server;
|
|
|
|
|
// we need to pull the "live" versions of the client (only if the client id isn't IW4MAdmin
|
|
|
|
|
gameEvent.Origin = gameEvent.Origin.ClientId == 1 ? gameEvent.Origin : server.GetClientsAsList().First(_client => _client.NetworkId == gameEvent.Origin.NetworkId);
|
|
|
|
|
gameEvent.Target = gameEvent.Target.ClientId == 1 ? gameEvent.Target : server.GetClientsAsList().First(_client => _client.NetworkId == gameEvent.Target.NetworkId);
|
|
|
|
|
|
|
|
|
|
events.Add(gameEvent);
|
|
|
|
|
}
|
2018-04-26 16:26:03 -04:00
|
|
|
|
}
|
2019-05-13 11:36:11 -04:00
|
|
|
|
|
|
|
|
|
catch (InvalidOperationException)
|
|
|
|
|
{
|
|
|
|
|
if (!ignoreBots.Value)
|
|
|
|
|
{
|
|
|
|
|
server.Logger.WriteWarning("Could not find client in client list when parsing event line");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|