2018-04-26 02:13:04 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.IO
|
|
|
|
|
{
|
|
|
|
|
class GameLogEvent
|
|
|
|
|
{
|
|
|
|
|
Server Server;
|
|
|
|
|
long PreviousFileSize;
|
|
|
|
|
GameLogReader Reader;
|
|
|
|
|
Timer RefreshInfoTimer;
|
|
|
|
|
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-04-28 01:22:18 -04:00
|
|
|
|
RefreshInfoTimer = new Timer(OnEvent, new EventState()
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2018-04-28 01:22:18 -04:00
|
|
|
|
Log = server.Manager.GetLogger(),
|
|
|
|
|
ServerId = server.ToString()
|
|
|
|
|
}, 0, 100);
|
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|