2019-11-15 15:50:20 -05:00
|
|
|
|
using IW4MAdmin.Application.Misc;
|
|
|
|
|
using SharedLibraryCore;
|
2018-08-30 21:53:00 -04:00
|
|
|
|
using SharedLibraryCore.Events;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2019-11-15 15:50:20 -05:00
|
|
|
|
using System;
|
2019-10-09 16:51:02 -04:00
|
|
|
|
using System.Linq;
|
2018-08-30 21:53:00 -04:00
|
|
|
|
using System.Threading;
|
2020-04-20 11:45:58 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2018-04-26 02:13:04 -04:00
|
|
|
|
|
|
|
|
|
namespace IW4MAdmin.Application
|
|
|
|
|
{
|
|
|
|
|
class GameEventHandler : IEventHandler
|
|
|
|
|
{
|
2019-05-31 11:17:01 -04:00
|
|
|
|
readonly ApplicationManager Manager;
|
2019-11-15 15:50:20 -05:00
|
|
|
|
private readonly EventProfiler _profiler;
|
|
|
|
|
private static readonly GameEvent.EventType[] overrideEvents = new[]
|
2019-10-09 16:51:02 -04:00
|
|
|
|
{
|
|
|
|
|
GameEvent.EventType.Connect,
|
|
|
|
|
GameEvent.EventType.Disconnect,
|
|
|
|
|
GameEvent.EventType.Quit,
|
|
|
|
|
GameEvent.EventType.Stop
|
|
|
|
|
};
|
|
|
|
|
|
2018-04-26 02:13:04 -04:00
|
|
|
|
public GameEventHandler(IManager mgr)
|
|
|
|
|
{
|
2019-05-31 11:17:01 -04:00
|
|
|
|
Manager = (ApplicationManager)mgr;
|
2019-11-15 15:50:20 -05:00
|
|
|
|
_profiler = new EventProfiler(mgr.GetLogger(0));
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-20 11:45:58 -04:00
|
|
|
|
private Task GameEventHandler_GameEventAdded(object sender, GameEventArgs args)
|
2019-11-15 15:50:20 -05:00
|
|
|
|
{
|
2020-04-20 11:45:58 -04:00
|
|
|
|
#if DEBUG
|
2019-11-15 15:50:20 -05:00
|
|
|
|
var start = DateTime.Now;
|
2020-04-20 11:45:58 -04:00
|
|
|
|
#endif
|
2019-12-28 11:07:37 -05:00
|
|
|
|
EventApi.OnGameEvent(sender, args);
|
2020-04-20 11:45:58 -04:00
|
|
|
|
return Manager.ExecuteEvent(args.Event);
|
|
|
|
|
|
2019-11-15 15:50:20 -05:00
|
|
|
|
#if DEBUG
|
|
|
|
|
_profiler.Profile(start, DateTime.Now, args.Event);
|
|
|
|
|
#endif
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-04 13:40:29 -04:00
|
|
|
|
public void AddEvent(GameEvent gameEvent)
|
2018-04-26 02:13:04 -04:00
|
|
|
|
{
|
2019-09-26 17:08:49 -04: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 16:51:02 -04:00
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
if (Manager.Running || overrideEvents.Contains(gameEvent.Type))
|
|
|
|
|
{
|
|
|
|
|
#if DEBUG
|
|
|
|
|
gameEvent.Owner.Logger.WriteDebug($"Adding event with id {gameEvent.Id}");
|
|
|
|
|
#endif
|
2020-04-20 11:45:58 -04:00
|
|
|
|
//GameEventAdded?.Invoke(this, new GameEventArgs(null, false, gameEvent));
|
|
|
|
|
Task.Run(() => GameEventHandler_GameEventAdded(this, new GameEventArgs(null, false, gameEvent)));
|
2019-10-09 16:51:02 -04:00
|
|
|
|
}
|
|
|
|
|
#if DEBUG
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
gameEvent.Owner.Logger.WriteDebug($"Skipping event as we're shutting down {gameEvent.Id}");
|
|
|
|
|
}
|
2019-09-26 17:08:49 -04:00
|
|
|
|
#endif
|
2018-04-26 02:13:04 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|