2018-09-06 14:25:58 -04:00
|
|
|
|
using IW4MAdmin.Application.API.GameLogServer;
|
|
|
|
|
using RestEase;
|
|
|
|
|
using SharedLibraryCore;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2019-05-13 11:36:11 -04:00
|
|
|
|
using System.Linq;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
using System.Net.Http;
|
2018-10-25 09:14:39 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2018-09-06 14:25:58 -04:00
|
|
|
|
using static SharedLibraryCore.Utilities;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application.IO
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// provides capibility of reading log files over HTTP
|
|
|
|
|
/// </summary>
|
|
|
|
|
class GameLogReaderHttp : IGameLogReader
|
|
|
|
|
{
|
|
|
|
|
readonly IEventParser Parser;
|
2018-09-06 14:25:58 -04:00
|
|
|
|
readonly IGameLogServer Api;
|
2019-02-09 16:35:13 -05:00
|
|
|
|
readonly string logPath;
|
2019-05-13 11:36:11 -04:00
|
|
|
|
private bool? ignoreBots;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
2019-02-09 16:35:13 -05:00
|
|
|
|
public GameLogReaderHttp(Uri gameLogServerUri, string logPath, IEventParser parser)
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
2019-02-09 16:35:13 -05:00
|
|
|
|
this.logPath = logPath.ToBase64UrlSafeString(); ;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
Parser = parser;
|
2019-02-09 16:35:13 -05:00
|
|
|
|
Api = RestClient.For<IGameLogServer>(gameLogServerUri);
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 14:25:58 -04:00
|
|
|
|
public long Length => -1;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
2018-11-25 21:00:36 -05:00
|
|
|
|
public int UpdateInterval => 350;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
2018-10-25 09:14:39 -04:00
|
|
|
|
public async Task<ICollection<GameEvent>> ReadEventsFromLog(Server server, long fileSizeDiff, long startPosition)
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
2018-08-30 21:53:00 -04:00
|
|
|
|
#if DEBUG == true
|
2018-11-25 21:00:36 -05:00
|
|
|
|
server.Logger.WriteDebug($"Begin reading from http log");
|
2018-08-30 21:53:00 -04:00
|
|
|
|
#endif
|
2019-05-13 11:36:11 -04:00
|
|
|
|
|
|
|
|
|
if (!ignoreBots.HasValue)
|
|
|
|
|
{
|
|
|
|
|
ignoreBots = server.Manager.GetApplicationSettings().Configuration().IgnoreBots;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 14:25:58 -04:00
|
|
|
|
var events = new List<GameEvent>();
|
2019-02-09 16:35:13 -05:00
|
|
|
|
string b64Path = logPath;
|
2018-10-25 09:14:39 -04:00
|
|
|
|
var response = await Api.Log(b64Path);
|
2018-09-06 14:25:58 -04:00
|
|
|
|
|
|
|
|
|
if (!response.Success)
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
2019-02-09 16:35:13 -05:00
|
|
|
|
server.Logger.WriteError($"Could not get log server info of {logPath}/{b64Path} ({server.LogPath})");
|
2018-10-25 09:14:39 -04:00
|
|
|
|
return events;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parse each line
|
2018-09-06 14:25:58 -04:00
|
|
|
|
foreach (string eventLine in response.Data.Split(Environment.NewLine))
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
|
|
|
|
if (eventLine.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
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-08-30 21:53:00 -04:00
|
|
|
|
#if DEBUG == true
|
2019-05-13 11:36:11 -04:00
|
|
|
|
server.Logger.WriteDebug($"Parsed event with id {gameEvent.Id} from http");
|
2018-08-30 21:53:00 -04:00
|
|
|
|
#endif
|
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-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2019-05-13 11:36:11 -04:00
|
|
|
|
server.Logger.WriteWarning("Could not properly parse remote event line");
|
2018-10-06 12:47:14 -04:00
|
|
|
|
server.Logger.WriteDebug(e.Message);
|
|
|
|
|
server.Logger.WriteDebug(eventLine);
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return events;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|