quantum-space-buddies/QSB/Events/QSBEventManager.cs

123 lines
3.3 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2021-11-30 16:57:04 -08:00
using OWML.Common;
2020-11-03 21:11:10 +00:00
using QSB.Utility;
2020-08-12 21:58:29 +02:00
2020-12-14 16:24:52 +00:00
namespace QSB.Events
{
2020-12-02 21:23:01 +00:00
public static class QSBEventManager
{
public static bool Ready { get; private set; }
2020-08-09 14:26:33 +01:00
private static readonly Type[] _types = typeof(IQSBEvent).GetDerivedTypes().ToArray();
internal static readonly List<IQSBEvent> _eventList = new();
2020-08-15 20:32:58 +01:00
2020-12-02 21:23:01 +00:00
public static void Init()
{
foreach (var type in _types)
2020-12-02 21:23:01 +00:00
{
_eventList.Add((IQSBEvent)Activator.CreateInstance(type));
}
2020-08-15 20:32:58 +01:00
2021-02-12 13:29:01 +00:00
if (UnitTestDetector.IsInUnitTest)
{
return;
}
2020-12-02 21:23:01 +00:00
_eventList.ForEach(ev => ev.SetupListener());
2020-08-18 20:47:17 +02:00
2020-12-02 21:23:01 +00:00
Ready = true;
2020-11-03 21:11:10 +00:00
2020-12-02 21:23:01 +00:00
DebugLog.DebugWrite("Event Manager ready.", MessageType.Success);
}
2020-08-15 20:32:58 +01:00
2020-12-02 21:23:01 +00:00
public static void Reset()
{
Ready = false;
_eventList.ForEach(ev => ev.CloseListener());
_eventList.Clear();
2020-12-02 21:23:01 +00:00
}
2021-02-10 19:34:41 +00:00
public static void FireEvent(string eventName)
{
if (!QSBCore.IsInMultiplayer)
{
2021-12-11 19:02:34 -08:00
DebugLog.ToConsole($"Warning - Tried to send event {eventName} while not connected to/hosting server.", MessageType.Warning);
2021-02-10 19:34:41 +00:00
return;
}
2021-06-18 22:38:32 +01:00
2021-02-10 19:34:41 +00:00
GlobalMessenger.FireEvent(eventName);
}
public static void FireEvent<T>(string eventName, T arg)
2021-02-10 19:34:41 +00:00
{
if (!QSBCore.IsInMultiplayer)
{
DebugLog.ToConsole($"Warning - Tried to send event {eventName} while not connected to/hosting server.", MessageType.Warning);
return;
}
2021-06-18 22:38:32 +01:00
GlobalMessenger<T>.FireEvent(eventName, arg);
2021-02-10 19:34:41 +00:00
}
public static void FireEvent<T, U>(string eventName, T arg1, U arg2)
2021-02-10 19:34:41 +00:00
{
if (!QSBCore.IsInMultiplayer)
{
DebugLog.ToConsole($"Warning - Tried to send event {eventName} while not connected to/hosting server.", MessageType.Warning);
return;
}
2021-06-18 22:38:32 +01:00
GlobalMessenger<T, U>.FireEvent(eventName, arg1, arg2);
2021-02-10 19:34:41 +00:00
}
public static void FireEvent<T, U, V>(string eventName, T arg1, U arg2, V arg3)
2021-02-10 19:34:41 +00:00
{
if (!QSBCore.IsInMultiplayer)
{
DebugLog.ToConsole($"Warning - Tried to send event {eventName} while not connected to/hosting server.", MessageType.Warning);
return;
}
2021-06-18 22:38:32 +01:00
GlobalMessenger<T, U, V>.FireEvent(eventName, arg1, arg2, arg3);
2021-02-10 19:34:41 +00:00
}
2021-02-25 13:52:49 +00:00
public static void FireEvent<T, U, V, W>(string eventName, T arg1, U arg2, V arg3, W arg4)
{
if (!QSBCore.IsInMultiplayer)
{
DebugLog.ToConsole($"Warning - Tried to send event {eventName} while not connected to/hosting server.", MessageType.Warning);
return;
}
2021-06-18 22:38:32 +01:00
2021-02-25 13:52:49 +00:00
GlobalMessenger<T, U, V, W>.FireEvent(eventName, arg1, arg2, arg3, arg4);
}
public static void FireEvent<T, U, V, W, X>(string eventName, T arg1, U arg2, V arg3, W arg4, X arg5)
{
if (!QSBCore.IsInMultiplayer)
{
DebugLog.ToConsole($"Warning - Tried to send event {eventName} while not connected to/hosting server.", MessageType.Warning);
return;
}
2021-06-18 22:38:32 +01:00
2021-02-25 13:52:49 +00:00
GlobalMessenger<T, U, V, W, X>.FireEvent(eventName, arg1, arg2, arg3, arg4, arg5);
}
public static void FireEvent<T, U, V, W, X, Y>(string eventName, T arg1, U arg2, V arg3, W arg4, X arg5, Y arg6)
{
if (!QSBCore.IsInMultiplayer)
{
DebugLog.ToConsole($"Warning - Tried to send event {eventName} while not connected to/hosting server.", MessageType.Warning);
return;
}
2021-06-18 22:38:32 +01:00
2021-02-25 13:52:49 +00:00
GlobalMessenger<T, U, V, W, X, Y>.FireEvent(eventName, arg1, arg2, arg3, arg4, arg5, arg6);
}
2021-12-06 01:57:56 -08:00
/// used to force set ForId for every sent event
public static uint ForIdOverride = uint.MaxValue;
2020-12-02 21:23:01 +00:00
}
2021-11-09 18:38:19 -08:00
}