using System;
using System.Collections.Generic;
namespace Microsoft.Xna.Framework.Net
{
///
/// Provides a registry for network message types, allowing messages to be registered and created dynamically.
///
public static class NetworkMessageRegistry
{
private static readonly Dictionary> registry = new();
///
/// Registers a network message type with the specified type identifier.
///
/// The type of the network message to register. Must implement and have a parameterless constructor.
/// The unique identifier for the message type.
public static void Register(byte typeId) where T : INetworkMessage, new()
{
registry[typeId] = () => new T();
}
///
/// Creates a network message instance based on the specified type identifier.
///
/// The unique identifier for the message type.
/// An instance of the network message, or null if the type identifier is not registered.
public static INetworkMessage CreateMessage(byte typeId)
{
return registry.TryGetValue(typeId, out var ctor) ? ctor() : null;
}
}
}