2018-04-26 02:13:04 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
2018-08-30 21:53:00 -04:00
|
|
|
|
using System.Collections.Generic;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
using System.IO;
|
2018-08-30 21:53:00 -04:00
|
|
|
|
using System.Threading;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.IO
|
|
|
|
|
{
|
2018-08-28 17:32:59 -04:00
|
|
|
|
class GameLogEventDetection
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
|
|
|
|
Server Server;
|
|
|
|
|
long PreviousFileSize;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
IGameLogReader Reader;
|
2018-08-03 22:11:58 -04:00
|
|
|
|
readonly string GameLogFile;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-04-28 01:22:18 -04:00
|
|
|
|
class EventState
|
|
|
|
|
{
|
|
|
|
|
public ILogger Log { get; set; }
|
|
|
|
|
public string ServerId { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-28 17:32:59 -04:00
|
|
|
|
public GameLogEventDetection(Server server, string gameLogPath, string gameLogName)
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
|
|
|
|
GameLogFile = gameLogPath;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
// todo: abtract this more
|
|
|
|
|
if (gameLogPath.StartsWith("http"))
|
|
|
|
|
{
|
|
|
|
|
Reader = new GameLogReaderHttp(gameLogPath, server.EventParser);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Reader = new GameLogReader(gameLogPath, server.EventParser);
|
|
|
|
|
}
|
2018-05-10 01:34:29 -04:00
|
|
|
|
|
2018-08-30 21:53:00 -04:00
|
|
|
|
Server = server;
|
2018-04-28 01:22:18 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-08-30 21:53:00 -04:00
|
|
|
|
public void PollForChanges()
|
2018-04-28 01:22:18 -04:00
|
|
|
|
{
|
2018-08-30 21:53:00 -04:00
|
|
|
|
while (!Server.Manager.ShutdownRequested())
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2018-08-30 21:53:00 -04:00
|
|
|
|
if ((Server.Manager as ApplicationManager).IsInitialized)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
UpdateLogEvents();
|
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-08-30 21:53:00 -04:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
Server.Logger.WriteWarning($"Failed to update log event for {Server.GetHashCode()}");
|
|
|
|
|
Server.Logger.WriteDebug($"Exception: {e.Message}");
|
|
|
|
|
Server.Logger.WriteDebug($"StackTrace: {e.StackTrace}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Thread.Sleep(Reader.UpdateInterval);
|
2018-04-28 01:22:18 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-30 21:53:00 -04:00
|
|
|
|
private void UpdateLogEvents()
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2018-08-30 21:53:00 -04:00
|
|
|
|
long fileSize = Reader.Length;
|
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
if (PreviousFileSize == 0)
|
2018-04-26 16:26:03 -04:00
|
|
|
|
PreviousFileSize = fileSize;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-04-26 16:26:03 -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)
|
2018-04-26 02:13:04 -04:00
|
|
|
|
return;
|
|
|
|
|
|
2018-04-26 16:26:03 -04:00
|
|
|
|
PreviousFileSize = fileSize;
|
|
|
|
|
|
2018-08-30 21:53:00 -04:00
|
|
|
|
var events = Reader.ReadEventsFromLog(Server, fileDiff, 0);
|
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
foreach (var ev in events)
|
2018-08-30 21:53:00 -04:00
|
|
|
|
{
|
2018-04-26 02:13:04 -04:00
|
|
|
|
Server.Manager.GetEventHandler().AddEvent(ev);
|
2018-08-30 21:53:00 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-04-26 16:26:03 -04:00
|
|
|
|
PreviousFileSize = fileSize;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|