using ProtoBuf.Meta;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Quasar.Common.Messages
{
public static class TypeRegistry
{
///
/// The internal index of the message type.
///
private static int _typeIndex;
///
/// Adds a Type to the serializer so a message can be properly serialized.
///
/// The parent type, i.e.: IMessage
/// Type to be added
public static void AddTypeToSerializer(Type parent, Type type)
{
if (type == null || parent == null)
throw new ArgumentNullException();
bool isAlreadyAdded = RuntimeTypeModel.Default[parent].GetSubtypes().Any(subType => subType.DerivedType.Type == type);
if (!isAlreadyAdded)
RuntimeTypeModel.Default[parent].AddSubType(++_typeIndex, type);
}
///
/// Adds Types to the serializer.
///
/// The parent type, i.e.: IMessage
/// Types to add.
public static void AddTypesToSerializer(Type parent, params Type[] types)
{
foreach (Type type in types)
AddTypeToSerializer(parent, type);
}
public static IEnumerable GetPacketTypes(Type type)
{
return AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p) && !p.IsInterface);
}
}
}