2019-11-15 14:50:20 -06:00
|
|
|
|
using IW4MAdmin.Application.Misc;
|
2020-05-04 16:50:02 -05:00
|
|
|
|
using Newtonsoft.Json;
|
2019-11-15 14:50:20 -06:00
|
|
|
|
using SharedLibraryCore;
|
2018-08-30 20:53:00 -05:00
|
|
|
|
using SharedLibraryCore.Events;
|
2018-04-26 01:13:04 -05:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2019-11-15 14:50:20 -06:00
|
|
|
|
using System;
|
2020-05-04 16:50:02 -05:00
|
|
|
|
using System.Collections.Generic;
|
2019-10-09 15:51:02 -05:00
|
|
|
|
using System.Linq;
|
2018-08-30 20:53:00 -05:00
|
|
|
|
using System.Threading;
|
2020-04-20 10:45:58 -05:00
|
|
|
|
using System.Threading.Tasks;
|
2018-04-26 01:13:04 -05:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application
|
|
|
|
|
{
|
2020-05-04 16:50:02 -05:00
|
|
|
|
public class GameEventHandler : IEventHandler
|
2018-04-26 01:13:04 -05:00
|
|
|
|
{
|
2020-05-04 16:50:02 -05:00
|
|
|
|
private readonly EventLog _eventLog;
|
2019-11-15 14:50:20 -06:00
|
|
|
|
private static readonly GameEvent.EventType[] overrideEvents = new[]
|
2019-10-09 15:51:02 -05:00
|
|
|
|
{
|
|
|
|
|
GameEvent.EventType.Connect,
|
|
|
|
|
GameEvent.EventType.Disconnect,
|
|
|
|
|
GameEvent.EventType.Quit,
|
|
|
|
|
GameEvent.EventType.Stop
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-04 16:50:02 -05:00
|
|
|
|
public GameEventHandler()
|
2018-04-26 01:13:04 -05:00
|
|
|
|
{
|
2020-05-04 16:50:02 -05:00
|
|
|
|
_eventLog = new EventLog();
|
2019-11-15 14:50:20 -06:00
|
|
|
|
}
|
|
|
|
|
|
2020-05-04 16:50:02 -05:00
|
|
|
|
public void HandleEvent(IManager manager, GameEvent gameEvent)
|
2018-04-26 01:13:04 -05:00
|
|
|
|
{
|
2019-09-26 16:08:49 -05:00
|
|
|
|
#if DEBUG
|
|
|
|
|
ThreadPool.GetMaxThreads(out int workerThreads, out int n);
|
|
|
|
|
ThreadPool.GetAvailableThreads(out int availableThreads, out int m);
|
|
|
|
|
gameEvent.Owner.Logger.WriteDebug($"There are {workerThreads - availableThreads} active threading tasks");
|
2019-10-09 15:51:02 -05:00
|
|
|
|
|
|
|
|
|
#endif
|
2020-05-04 16:50:02 -05:00
|
|
|
|
if (manager.IsRunning || overrideEvents.Contains(gameEvent.Type))
|
2019-10-09 15:51:02 -05:00
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
gameEvent.Owner.Logger.WriteDebug($"Adding event with id {gameEvent.Id}");
|
|
|
|
|
#endif
|
2020-05-04 16:50:02 -05:00
|
|
|
|
|
|
|
|
|
EventApi.OnGameEvent(gameEvent);
|
|
|
|
|
Task.Factory.StartNew(() => manager.ExecuteEvent(gameEvent));
|
2019-10-09 15:51:02 -05:00
|
|
|
|
}
|
|
|
|
|
#if DEBUG
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
gameEvent.Owner.Logger.WriteDebug($"Skipping event as we're shutting down {gameEvent.Id}");
|
|
|
|
|
}
|
2019-09-26 16:08:49 -05:00
|
|
|
|
#endif
|
2018-04-26 01:13:04 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|