Add utility for custom commands into command lists.
This commit is contained in:
parent
23fc485b2a
commit
dadd9f137a
@ -170,5 +170,63 @@ namespace Dashboard.CommandMachine
|
||||
iterator.Dequeue();
|
||||
}
|
||||
}
|
||||
|
||||
private static readonly Dictionary<Type, ICommandListSerializer> s_serializers = new Dictionary<Type, ICommandListSerializer>();
|
||||
|
||||
/// <summary>
|
||||
/// Add a custom serializer to the command engine.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type object type.</typeparam>
|
||||
/// <param name="serializer">The serializer.</param>
|
||||
/// <param name="overwrite">True to allow overwriting.</param>
|
||||
public static void AddSerializer<T>(ICommandListSerializer serializer, bool overwrite = false)
|
||||
{
|
||||
if (overwrite)
|
||||
{
|
||||
s_serializers[typeof(T)] = serializer;
|
||||
}
|
||||
else
|
||||
{
|
||||
s_serializers.Add(typeof(T), serializer);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Add a custom serializer to the command engine.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type object type.</typeparam>
|
||||
/// <param name="serializer">The serializer.</param>
|
||||
/// <param name="overwrite">True to allow overwriting.</param>
|
||||
public static void AddSerializer<T>(ICommandListSerializer<T> serializer, bool overwrite = false)
|
||||
=> AddSerializer<T>((ICommandListSerializer)serializer, overwrite);
|
||||
|
||||
/// <summary>
|
||||
/// Get a serializer for the given object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The object type.</typeparam>
|
||||
/// <param name="value">Required parameter for the C# type inference to work.</param>
|
||||
/// <returns>The serializer.</returns>
|
||||
public static ICommandListSerializer<T> GetSerializer<T>(ICommandListSerializable<T>? value)
|
||||
where T : ICommandListSerializable<T>, new()
|
||||
{
|
||||
if (!s_serializers.TryGetValue(typeof(T), out var serializer))
|
||||
{
|
||||
serializer = new CommandListSerializableSerializer<T>();
|
||||
AddSerializer<T>(serializer);
|
||||
}
|
||||
|
||||
return (ICommandListSerializer<T>)serializer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get a serializer for the given object.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The object type.</typeparam>
|
||||
/// <param name="value">Required parameter for the C# type inference to work.</param>
|
||||
/// <returns>The serializer.</returns>
|
||||
public static ICommandListSerializer<T> GetSerializer<T>(T? value)
|
||||
{
|
||||
return (ICommandListSerializer<T>)s_serializers[typeof(T)];
|
||||
}
|
||||
}
|
||||
}
|
@ -3,7 +3,6 @@ using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Dashboard.CommandMachine
|
||||
{
|
||||
@ -316,6 +315,27 @@ namespace Dashboard.CommandMachine
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize an object into the command list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the value to serialize.</typeparam>
|
||||
/// <param name="value">What to write into the command list.</param>
|
||||
public void Write<T>(T value)
|
||||
{
|
||||
CommandEngine.GetSerializer(value).Serialize(value, this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Serialize an object into the command list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the value to serialize.</typeparam>
|
||||
/// <param name="value">What to write into the command list.</param>
|
||||
public void Write<T>(ICommandListSerializable<T> value)
|
||||
where T : ICommandListSerializable<T>, new()
|
||||
{
|
||||
CommandEngine.GetSerializer(value).Serialize((T)value, this);
|
||||
}
|
||||
|
||||
public CommandQueue GetEnumerator() => new CommandQueue(_frames);
|
||||
IEnumerator<Frame> IEnumerable<Frame>.GetEnumerator() => GetEnumerator();
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
@ -372,6 +392,28 @@ namespace Dashboard.CommandMachine
|
||||
|
||||
public Frame Peek() => TryPeek(out Frame frame) ? frame : throw new Exception("No more frames left.");
|
||||
|
||||
/// <summary>
|
||||
/// Deserialize an object from the command queue.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the object to deserialize.</typeparam>
|
||||
/// <param name="value">The deserialized value.</param>
|
||||
public void Read<T>([NotNull] out T? value)
|
||||
{
|
||||
value = CommandEngine.GetSerializer(default(T)).Deserialize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Deserialize an object from the command queue.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the object to deserialize.</typeparam>
|
||||
/// <param name="value">The deserialized value.</param>
|
||||
public void Read<T>([NotNull] out ICommandListSerializable<T>? value)
|
||||
where T : ICommandListSerializable<T>, new()
|
||||
{
|
||||
value = CommandEngine.GetSerializer(value = null).Deserialize(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (_current + 1 < _frames.Count)
|
||||
@ -382,6 +424,7 @@ namespace Dashboard.CommandMachine
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Reset()
|
||||
{
|
||||
_current = -1;
|
||||
|
@ -1,17 +1,68 @@
|
||||
namespace Dashboard.CommandMachine
|
||||
{
|
||||
/// <summary>
|
||||
/// Enumeration of command types in the Dashboard command lists.
|
||||
/// </summary>
|
||||
public enum FrameType
|
||||
{
|
||||
/// <summary>
|
||||
/// A null value.
|
||||
/// </summary>
|
||||
None,
|
||||
|
||||
/// <summary>
|
||||
/// A command frame.
|
||||
/// </summary>
|
||||
Command,
|
||||
|
||||
/// <summary>
|
||||
/// An integer frame.
|
||||
/// </summary>
|
||||
IVec1,
|
||||
|
||||
/// <summary>
|
||||
/// A two dimensional integer vector frame.
|
||||
/// </summary>
|
||||
IVec2,
|
||||
|
||||
/// <summary>
|
||||
/// A three dimensional integer vector frame.
|
||||
/// </summary>
|
||||
IVec3,
|
||||
|
||||
/// <summary>
|
||||
/// A four dimensional integer vector frame.
|
||||
/// </summary>
|
||||
IVec4,
|
||||
|
||||
/// <summary>
|
||||
/// A floating point frame.
|
||||
/// </summary>
|
||||
Vec1,
|
||||
|
||||
/// <summary>
|
||||
/// A two dimensional floating point vector frame.
|
||||
/// </summary>
|
||||
Vec2,
|
||||
|
||||
/// <summary>
|
||||
/// A three dimensional floating point vector frame.
|
||||
/// </summary>
|
||||
Vec3,
|
||||
|
||||
/// <summary>
|
||||
/// A four dimensional floating point vector frame.
|
||||
/// </summary>
|
||||
Vec4,
|
||||
|
||||
/// <summary>
|
||||
/// A serialized object frame.
|
||||
/// </summary>
|
||||
Serialized,
|
||||
|
||||
/// <summary>
|
||||
/// A .Net object frame.
|
||||
/// </summary>
|
||||
Object,
|
||||
}
|
||||
}
|
69
Dashboard/CommandMachine/Serializers.cs
Normal file
69
Dashboard/CommandMachine/Serializers.cs
Normal file
@ -0,0 +1,69 @@
|
||||
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Threading;
|
||||
|
||||
namespace Dashboard.CommandMachine
|
||||
{
|
||||
public interface ICommandListSerializable { }
|
||||
|
||||
/// <summary>
|
||||
/// Interface for objects that can be serialized into the Dashboard command stream.
|
||||
/// </summary>
|
||||
public interface ICommandListSerializable<T> : ICommandListSerializable
|
||||
{
|
||||
/// <summary>
|
||||
/// Seralize object.
|
||||
/// </summary>
|
||||
/// <param name="list">The object to serialize into.</param>
|
||||
void Serialize(CommandList list);
|
||||
|
||||
/// <summary>
|
||||
/// Deserialize object.
|
||||
/// </summary>
|
||||
/// <param name="queue">The command queue to deserialize from.</param>
|
||||
void Deserialize(CommandQueue queue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Base interface for all Command List serializers.
|
||||
/// </summary>
|
||||
public interface ICommandListSerializer { }
|
||||
|
||||
public interface ICommandListSerializer<T> : ICommandListSerializer
|
||||
{
|
||||
/// <summary>
|
||||
/// Serialize an object into the command list.
|
||||
/// </summary>
|
||||
/// <param name="value">The object to serialize.</param>
|
||||
/// <param name="list">The command list to serialize into.</param>
|
||||
void Serialize(T value, CommandList list);
|
||||
|
||||
/// <summary>
|
||||
/// Deserialize an object from the command queue.
|
||||
/// </summary>
|
||||
/// <param name="queue">The command queue.</param>
|
||||
/// <returns>The object deserialized from the command queue.</returns>
|
||||
[return: NotNull]
|
||||
T Deserialize(CommandQueue queue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Class for automatic serialization of <see cref="ICommandListSerializable"/> objects.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The object type to convert.</typeparam>
|
||||
internal class CommandListSerializableSerializer<T> : ICommandListSerializer<T>
|
||||
where T : ICommandListSerializable<T>, new()
|
||||
{
|
||||
public T Deserialize(CommandQueue queue)
|
||||
{
|
||||
T value = new T();
|
||||
value.Deserialize(queue);
|
||||
return value;
|
||||
}
|
||||
|
||||
public void Serialize(T value, CommandList list)
|
||||
{
|
||||
value.Serialize(list);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user