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;
|
2019-07-31 21:15:29 -04:00
|
|
|
|
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;
|
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
|
2019-09-09 18:40:04 -04:00
|
|
|
|
using (FileStream fs = new FileStream(LogFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2019-09-09 18:40:04 -04:00
|
|
|
|
byte[] buff = new byte[fileSizeDiff];
|
|
|
|
|
fs.Seek(startPosition, SeekOrigin.Begin);
|
|
|
|
|
await fs.ReadAsync(buff, 0, (int)fileSizeDiff, server.Manager.CancellationToken);
|
2019-07-31 21:15:29 -04:00
|
|
|
|
var stringBuilder = new StringBuilder();
|
2019-09-09 18:40:04 -04:00
|
|
|
|
char[] charBuff = Utilities.EncodingType.GetChars(buff);
|
|
|
|
|
|
|
|
|
|
foreach (char c in charBuff)
|
2018-04-26 16:26:03 -04:00
|
|
|
|
{
|
2019-07-31 21:15:29 -04:00
|
|
|
|
if (c == '\n')
|
|
|
|
|
{
|
|
|
|
|
logLines.Add(stringBuilder.ToString());
|
|
|
|
|
stringBuilder = new StringBuilder();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (c != '\r')
|
|
|
|
|
{
|
|
|
|
|
stringBuilder.Append(c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stringBuilder.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
logLines.Add(stringBuilder.ToString());
|
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);
|
2019-05-17 10:02:09 -04:00
|
|
|
|
// we don't want to add the event if ignoreBots is on and the event comes from a bot
|
|
|
|
|
if (!ignoreBots.Value || (ignoreBots.Value && !((gameEvent.Origin?.IsBot ?? false) || (gameEvent.Target?.IsBot ?? false))))
|
2019-05-13 11:36:11 -04:00
|
|
|
|
{
|
|
|
|
|
gameEvent.Owner = server;
|
2019-05-29 17:55:35 -04:00
|
|
|
|
|
|
|
|
|
if ((gameEvent.RequiredEntity & GameEvent.EventRequiredEntity.Origin) == GameEvent.EventRequiredEntity.Origin && gameEvent.Origin.NetworkId != 1)
|
|
|
|
|
{
|
|
|
|
|
gameEvent.Origin = server.GetClientsAsList().First(_client => _client.NetworkId == gameEvent.Origin?.NetworkId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2019-05-13 11:36:11 -04:00
|
|
|
|
|
|
|
|
|
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");
|
2019-05-17 10:02:09 -04:00
|
|
|
|
server.Logger.WriteDebug(eventLine);
|
2019-05-13 11:36:11 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|