using System; using System.Diagnostics.CodeAnalysis; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace Dashboard.Drawing { public class DbBaseCommands : DrawExtension { public DrawCommand DrawPoint { get; } private DbBaseCommands() : base("DB_base", new[] { BrushExtension.Instance, }) { AddCommand(DrawPoint = new DrawCommand("Point", this, PointCommandArgs.CommandSize)); } public static readonly DbBaseCommands Instance = new DbBaseCommands(); } public struct PointCommandArgs : IParameterSerializer { public Vector3 Position { get; private set; } public float Size { get; private set; } public IBrush? Brush { get; private set; } public PointCommandArgs(Vector3 position, float size, IBrush brush) { Position = position; Brush = brush; Size = size; } public int Serialize(DrawQueue queue, Span bytes) { if (bytes.Length < CommandSize) return CommandSize; Span value = stackalloc Value[] { new Value(Position, Size, queue.RequireResource(Brush!)) }; MemoryMarshal.AsBytes(value).CopyTo(bytes); return CommandSize; } [MemberNotNull(nameof(Brush))] public void Deserialize(DrawQueue queue, ReadOnlySpan bytes) { if (bytes.Length < CommandSize) throw new Exception("Not enough bytes"); Value value = MemoryMarshal.AsRef(bytes); Position = value.Position; Size = value.Size; Brush = (IBrush)queue.Resources[value.BrushIndex]; } private record struct Value(Vector3 Position, float Size, int BrushIndex); public static readonly int CommandSize = Unsafe.SizeOf(); } }