2018-04-26 02:13:04 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.IO
|
|
|
|
|
{
|
|
|
|
|
class GameLogEvent
|
|
|
|
|
{
|
|
|
|
|
Server Server;
|
|
|
|
|
long PreviousFileSize;
|
|
|
|
|
GameLogReader Reader;
|
|
|
|
|
string GameLogFile;
|
|
|
|
|
|
2018-04-28 01:22:18 -04:00
|
|
|
|
class EventState
|
|
|
|
|
{
|
|
|
|
|
public ILogger Log { get; set; }
|
|
|
|
|
public string ServerId { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
public GameLogEvent(Server server, string gameLogPath, string gameLogName)
|
|
|
|
|
{
|
|
|
|
|
GameLogFile = gameLogPath;
|
|
|
|
|
Reader = new GameLogReader(gameLogPath, server.EventParser);
|
|
|
|
|
Server = server;
|
2018-05-10 01:34:29 -04:00
|
|
|
|
|
|
|
|
|
Task.Run(async () =>
|
|
|
|
|
{
|
|
|
|
|
while (!server.Manager.ShutdownRequested())
|
|
|
|
|
{
|
2018-07-04 22:09:42 -04:00
|
|
|
|
if ((server.Manager as ApplicationManager).IsInitialized)
|
2018-05-10 01:34:29 -04:00
|
|
|
|
{
|
2018-07-04 22:09:42 -04:00
|
|
|
|
OnEvent(new EventState()
|
|
|
|
|
{
|
|
|
|
|
Log = server.Manager.GetLogger(),
|
|
|
|
|
ServerId = server.ToString()
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-05-10 01:34:29 -04:00
|
|
|
|
await Task.Delay(100);
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-04-28 01:22:18 -04:00
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-04-28 01:22:18 -04:00
|
|
|
|
private void OnEvent(object state)
|
|
|
|
|
{
|
|
|
|
|
long newLength = new FileInfo(GameLogFile).Length;
|
2018-07-04 22:09:42 -04:00
|
|
|
|
|
2018-04-28 01:22:18 -04:00
|
|
|
|
try
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2018-04-28 01:22:18 -04:00
|
|
|
|
UpdateLogEvents(newLength);
|
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
2018-04-28 01:22:18 -04:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
((EventState)state).Log.WriteWarning($"Failed to update log event for {((EventState)state).ServerId}");
|
|
|
|
|
((EventState)state).Log.WriteDebug($"Exception: {e.Message}");
|
|
|
|
|
((EventState)state).Log.WriteDebug($"StackTrace: {e.StackTrace}");
|
|
|
|
|
}
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 16:26:03 -04:00
|
|
|
|
private void UpdateLogEvents(long fileSize)
|
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
|
|
|
|
|
|
|
|
|
if (fileDiff < 1)
|
|
|
|
|
return;
|
|
|
|
|
|
2018-04-26 16:26:03 -04:00
|
|
|
|
PreviousFileSize = fileSize;
|
|
|
|
|
|
|
|
|
|
var events = Reader.EventsFromLog(Server, fileDiff, 0);
|
2018-04-26 02:13:04 -04:00
|
|
|
|
foreach (var ev in events)
|
|
|
|
|
Server.Manager.GetEventHandler().AddEvent(ev);
|
|
|
|
|
|
2018-04-26 16:26:03 -04:00
|
|
|
|
PreviousFileSize = fileSize;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|