#nullable enable using System; using System.Collections.Generic; using System.Linq; using Cysharp.Threading.Tasks; using ShrinkEventBus; namespace ShrinkNetwork.Integration { public readonly struct ShrinkNetworkEventRegistration { public ShrinkNetworkEventRegistration(Type eventType, int opcode, string? route, Func dispatch, Func>? requestDispatch) { EventType = eventType ?? throw new ArgumentNullException(nameof(eventType)); Opcode = opcode; Route = route; Dispatch = dispatch ?? throw new ArgumentNullException(nameof(dispatch)); RequestDispatch = requestDispatch; } public Type EventType { get; } public int Opcode { get; } public string? Route { get; } public Func Dispatch { get; } public Func>? RequestDispatch { get; } } public static class ShrinkNetworkEventRegistry { private static readonly object Gate = new(); private static readonly Dictionary Registrations = new(); public static void Register(int opcode, string? route) where TEvent : IShrinkEvent, IShrinkNetworkMessage { var registration = new ShrinkNetworkEventRegistration( typeof(TEvent), opcode, route, static eventData => ShrinkNetworkEventBusBridge.DispatchGeneratedAsync((TEvent)eventData), typeof(IShrinkNetworkRequest).IsAssignableFrom(typeof(TEvent)) ? static (context, eventData) => ShrinkNetworkEventBusBridge.DispatchGeneratedRequestAsync(context, (TEvent)eventData) : null); lock (Gate) Registrations[typeof(TEvent)] = registration; } internal static IReadOnlyList Snapshot() { lock (Gate) return Registrations.Values.ToArray(); } } }