using System; namespace Dashboard.Drawing { public interface IDrawCommand { /// /// Name of the command. /// string Name { get; } /// /// The draw extension that defines this command. /// IDrawExtension Extension { get; } /// /// The length of the command data segment, in bytes. /// /// /// Must be 0 for simple commands. For commands that are variadic, the /// value must be less than 0. Any other positive value, otherwise. /// int Length { get; } /// /// Get the parameters object for this command. /// /// The parameter array. /// The parameters object. object? GetParams(DrawQueue queue, ReadOnlySpan param); int WriteParams(DrawQueue queue, object? obj, Span param); } public interface IDrawCommand : IDrawCommand { /// /// Get the parameters object for this command. /// /// The parameter array. /// The parameters object. new T? GetParams(DrawQueue queue, ReadOnlySpan param); new int WriteParams(DrawQueue queue, T? obj, Span param); } public sealed class DrawCommand : IDrawCommand { public string Name { get; } public IDrawExtension Extension { get; } public int Length { get; } = 0; public DrawCommand(string name, IDrawExtension extension) { Name = name; Extension = extension; } public object? GetParams(DrawQueue queue, ReadOnlySpan param) { return null; } public int WriteParams(DrawQueue queue, object? obj, Span param) { return 0; } } public sealed class DrawCommand : IDrawCommand where T : IParameterSerializer, new() { public string Name { get; } public IDrawExtension Extension { get; } public int Length { get; } public DrawCommand(string name, IDrawExtension extension, int length) { Name = name; Extension = extension; Length = length; } public T? GetParams(DrawQueue queue, ReadOnlySpan param) { T t = new T(); t.Deserialize(queue, param); return t; } public int WriteParams(DrawQueue queue, T? obj, Span param) { return obj!.Serialize(queue, param); } int IDrawCommand.WriteParams(DrawQueue queue, object? obj, Span param) { return WriteParams(queue, (T?)obj, param); } object? IDrawCommand.GetParams(DrawQueue queue, ReadOnlySpan param) { return GetParams(queue, param); } } }