mirror of https://github.com/quasar/Quasar.git
50 lines
1.6 KiB
C#
50 lines
1.6 KiB
C#
|
using ProtoBuf.Meta;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
|
|||
|
namespace Quasar.Common.Messages
|
|||
|
{
|
|||
|
public static class TypeRegistry
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// The internal index of the message type.
|
|||
|
/// </summary>
|
|||
|
private static int _typeIndex;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Adds a Type to the serializer so a message can be properly serialized.
|
|||
|
/// </summary>
|
|||
|
/// <param name="parent">The parent type, i.e.: IMessage</param>
|
|||
|
/// <param name="type">Type to be added</param>
|
|||
|
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);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Adds Types to the serializer.
|
|||
|
/// </summary>
|
|||
|
/// <param name="parent">The parent type, i.e.: IMessage</param>
|
|||
|
/// <param name="types">Types to add.</param>
|
|||
|
public static void AddTypesToSerializer(Type parent, params Type[] types)
|
|||
|
{
|
|||
|
foreach (Type type in types)
|
|||
|
AddTypeToSerializer(parent, type);
|
|||
|
}
|
|||
|
|
|||
|
public static IEnumerable<Type> GetPacketTypes(Type type)
|
|||
|
{
|
|||
|
return AppDomain.CurrentDomain.GetAssemblies()
|
|||
|
.SelectMany(s => s.GetTypes())
|
|||
|
.Where(p => type.IsAssignableFrom(p) && !p.IsInterface);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|