2018-04-26 02:13:04 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
2019-10-18 14:39:21 -04:00
|
|
|
|
using System.Linq;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Serilog.Context;
|
|
|
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.IO
|
|
|
|
|
{
|
2020-04-01 15:11:56 -04:00
|
|
|
|
public class GameLogEventDetection
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
private long previousFileSize;
|
|
|
|
|
private readonly Server _server;
|
|
|
|
|
private readonly IGameLogReader _reader;
|
2019-10-18 14:39:21 -04:00
|
|
|
|
private readonly bool _ignoreBots;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
private readonly ILogger _logger;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
public GameLogEventDetection(ILogger<GameLogEventDetection> logger, Server server, Uri[] gameLogUris, IGameLogReaderFactory gameLogReaderFactory)
|
2018-04-28 01:22:18 -04:00
|
|
|
|
{
|
2020-05-04 17:50:02 -04:00
|
|
|
|
_reader = gameLogReaderFactory.CreateGameLogReader(gameLogUris, server.EventParser);
|
2019-05-08 21:34:17 -04:00
|
|
|
|
_server = server;
|
2020-04-01 15:11:56 -04:00
|
|
|
|
_ignoreBots = server?.Manager.GetApplicationSettings().Configuration().IgnoreBots ?? false;
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger = logger;
|
2018-04-28 01:22:18 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-10-25 09:14:39 -04:00
|
|
|
|
public async Task PollForChanges()
|
2018-04-28 01:22:18 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
while (!_server.Manager.CancellationToken.IsCancellationRequested)
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
if (_server.IsInitialized)
|
2018-08-30 21:53:00 -04:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-10-25 09:14:39 -04:00
|
|
|
|
await UpdateLogEvents();
|
2018-08-30 21:53:00 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-08-30 21:53:00 -04:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
using(LogContext.PushProperty("Server", _server.ToString()))
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(e, "Failed to update log event for {endpoint}", _server.EndPoint);
|
|
|
|
|
}
|
2018-08-30 21:53:00 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-11-18 09:08:09 -05:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
await Task.Delay(_reader.UpdateInterval, _server.Manager.CancellationToken);
|
2018-04-28 01:22:18 -04:00
|
|
|
|
}
|
2019-08-24 15:06:23 -04:00
|
|
|
|
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger.LogDebug("Stopped polling for changes");
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 15:11:56 -04:00
|
|
|
|
public async Task UpdateLogEvents()
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
long fileSize = _reader.Length;
|
2018-08-30 21:53:00 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
if (previousFileSize == 0)
|
2019-08-01 20:42:44 -04:00
|
|
|
|
{
|
2019-05-08 21:34:17 -04:00
|
|
|
|
previousFileSize = fileSize;
|
2019-08-01 20:42:44 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
long fileDiff = fileSize - previousFileSize;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-09-06 14:25:58 -04:00
|
|
|
|
// this makes the http log get pulled
|
|
|
|
|
if (fileDiff < 1 && fileSize != -1)
|
2020-04-01 15:11:56 -04:00
|
|
|
|
{
|
|
|
|
|
previousFileSize = fileSize;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
return;
|
2020-04-01 15:11:56 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
var events = await _reader.ReadEventsFromLog(fileDiff, previousFileSize);
|
2018-08-30 21:53:00 -04:00
|
|
|
|
|
2019-10-18 14:39:21 -04:00
|
|
|
|
foreach (var gameEvent in events)
|
2018-08-30 21:53:00 -04:00
|
|
|
|
{
|
2019-10-18 14:39:21 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2019-11-18 09:08:09 -05:00
|
|
|
|
gameEvent.Owner = _server;
|
|
|
|
|
|
2019-10-18 14:39:21 -04:00
|
|
|
|
// we don't want to add the event if ignoreBots is on and the event comes from a bot
|
|
|
|
|
if (!_ignoreBots || (_ignoreBots && !((gameEvent.Origin?.IsBot ?? false) || (gameEvent.Target?.IsBot ?? false))))
|
|
|
|
|
{
|
2020-05-04 17:50:02 -04:00
|
|
|
|
if ((gameEvent.RequiredEntity & GameEvent.EventRequiredEntity.Origin) == GameEvent.EventRequiredEntity.Origin && gameEvent.Origin.NetworkId != Utilities.WORLD_ID)
|
2019-10-18 14:39:21 -04:00
|
|
|
|
{
|
2020-05-04 17:50:02 -04:00
|
|
|
|
gameEvent.Origin = _server.GetClientsAsList().First(_client => _client.NetworkId == gameEvent.Origin?.NetworkId);;
|
2019-10-18 14:39:21 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((gameEvent.RequiredEntity & GameEvent.EventRequiredEntity.Target) == GameEvent.EventRequiredEntity.Target)
|
|
|
|
|
{
|
|
|
|
|
gameEvent.Target = _server.GetClientsAsList().First(_client => _client.NetworkId == gameEvent.Target?.NetworkId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (gameEvent.Origin != null)
|
|
|
|
|
{
|
|
|
|
|
gameEvent.Origin.CurrentServer = _server;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (gameEvent.Target != null)
|
|
|
|
|
{
|
|
|
|
|
gameEvent.Target.CurrentServer = _server;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 17:50:02 -04:00
|
|
|
|
_server.Manager.AddEvent(gameEvent);
|
2019-10-18 14:39:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (InvalidOperationException)
|
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
if (_ignoreBots)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using(LogContext.PushProperty("Server", _server.ToString()))
|
2019-10-18 14:39:21 -04:00
|
|
|
|
{
|
2020-11-11 18:31:26 -05:00
|
|
|
|
_logger.LogError("Could not find client in client list when parsing event line {data}", gameEvent.Data);
|
2019-10-18 14:39:21 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-08-30 21:53:00 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2019-05-08 21:34:17 -04:00
|
|
|
|
previousFileSize = fileSize;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|