Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor OpenRGBClient #33

Merged
merged 12 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/OpenRGB.NET.Example/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
client.UpdateLeds(index, slice);
}

Thread.Sleep(1000 / fps);
Thread.Sleep(1000 / fps);
}
});

Expand Down
19 changes: 19 additions & 0 deletions src/OpenRGB.NET.Tests/BufferReadTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.IO;
using OpenRGB.NET.Utils;
using Xunit;

namespace OpenRGB.NET.Test;

public class BufferReadTests
{
[Fact]
public void Test_Read_Device()
{
var buffer = File.ReadAllBytes(Path.Combine("TestData", "08-Receive-RequestControllerData.bin"));
var spanReader = new SpanReader(buffer);
var device = DeviceReader.ReadFrom(ref spanReader, ProtocolVersion.V4, 0);

Assert.Equal(0, device.Index);
Assert.Equal("Full 104 key ", device.Name);
}
}
4 changes: 4 additions & 0 deletions src/OpenRGB.NET.Tests/OpenRGB.NET.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@
<ItemGroup>
<ProjectReference Include="..\OpenRGB.NET\OpenRGB.NET.csproj" />
</ItemGroup>

<ItemGroup>
<Content Include="TestData\*.bin" CopyToOutputDirectory="PreserveNewest"/>
</ItemGroup>

</Project>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion src/OpenRGB.NET/IOpenRGBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public interface IOpenRgbClient
/// The maximum protocol version this implementation supports
/// </summary>
ProtocolVersion MaxSupportedProtocolVersion { get; }

/// <summary>
/// The protocol version to be used by this instance of <see cref="IOpenRgbClient" />
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions src/OpenRGB.NET/ISpanReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using OpenRGB.NET.Utils;

namespace OpenRGB.NET;

internal interface ISpanReader<out T>
{
static abstract T ReadFrom(ref SpanReader reader, ProtocolVersion? protocolVersion = default, int? index = default, int? outerCount = default);
}
9 changes: 9 additions & 0 deletions src/OpenRGB.NET/ISpanWritable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using OpenRGB.NET.Utils;

namespace OpenRGB.NET;

internal interface ISpanWritable
{
int Length { get; }
void WriteTo(ref SpanWriter writer);
}
22 changes: 22 additions & 0 deletions src/OpenRGB.NET/IsExternalInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#if NETSTANDARD2_0 || NETSTANDARD2_1 || NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2 || NETCOREAPP3_0 || NETCOREAPP3_1 || NET45 || NET451 || NET452 || NET46 || NET461 || NET462 || NET47 || NET471 || NET472 || NET48

using System.ComponentModel;

// ReSharper disable once CheckNamespace
namespace System.Runtime.CompilerServices
{
/// <summary>
/// Reserved to be used by the compiler for tracking metadata.
/// This class should not be used by developers in source code.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal static class IsExternalInit
{
}
}

#endif
39 changes: 4 additions & 35 deletions src/OpenRGB.NET/Models/Color.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using OpenRGB.NET.Utils;
using System.Runtime.InteropServices;

namespace OpenRGB.NET;

Expand All @@ -9,6 +8,7 @@ namespace OpenRGB.NET;
/// <param name="R">The Red component</param>
/// <param name="G">The Green component</param>
/// <param name="B">The Blue component</param>
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public readonly record struct Color(byte R = 0, byte G = 0, byte B = 0)
{
/// <summary>
Expand All @@ -26,37 +26,6 @@ public readonly record struct Color(byte R = 0, byte G = 0, byte B = 0)
/// </summary>
public byte B { get; } = B;

private static Color ReadFrom(ref SpanReader reader)
{
var r = reader.ReadByte();
var g = reader.ReadByte();
var b = reader.ReadByte();
var a = reader.ReadByte();

return new Color(r, g, b);
}

internal static Color[] ReadManyFrom(ref SpanReader reader, int count)
{
if (count < 0)
throw new ArgumentOutOfRangeException(nameof(count), "Count must be greater than or equal to 0.");

if (count == 0)
return Array.Empty<Color>();

var colors = new Color[count];

for (var i = 0; i < count; i++)
colors[i] = ReadFrom(ref reader);

return colors;
}

internal void WriteTo(ref SpanWriter writer)
{
writer.WriteByte(R);
writer.WriteByte(G);
writer.WriteByte(B);
writer.WriteByte(0);
}
//Added for padding, so Color fits within 4 bytes.
private readonly byte UnusedAlpha = 0;
}
33 changes: 2 additions & 31 deletions src/OpenRGB.NET/Models/Device.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;
using OpenRGB.NET.Utils;

namespace OpenRGB.NET;

/// <summary>
/// Device class containing all the info present in an OpenRGB RGBController
/// </summary>
public class Device
{
private Device(int index, DeviceType type, string name, string? vendor,
internal Device(int index, DeviceType type, string name, string? vendor,
string description, string version, string serial, string location, int activeModeIndex,
Mode[] modes, Zone[] zones, Led[] leds, Color[] colors)
{
Expand Down Expand Up @@ -97,35 +94,9 @@ private Device(int index, DeviceType type, string name, string? vendor,
/// </summary>
public Mode ActiveMode => Modes[ActiveModeIndex];

internal static Device ReadFrom(ref SpanReader reader, ProtocolVersion protocol, int deviceIndex)
{
var duplicatePacketLength = reader.ReadUInt32();

var deviceType = reader.ReadInt32();
var name = reader.ReadLengthAndString();
var vendor = protocol.SupportsVendorString ? reader.ReadLengthAndString() : null;
var description = reader.ReadLengthAndString();
var version = reader.ReadLengthAndString();
var serial = reader.ReadLengthAndString();
var location = reader.ReadLengthAndString();
var modeCount = reader.ReadUInt16();
var activeMode = reader.ReadInt32();
var modes = Mode.ReadManyFrom(ref reader, modeCount, protocol);
var zoneCount = reader.ReadUInt16();
var zones = Zone.ReadManyFrom(ref reader, zoneCount, deviceIndex, protocol);
var ledCount = reader.ReadUInt16();
var leds = Led.ReadManyFrom(ref reader, ledCount);
var colorCount = reader.ReadUInt16();
var colors = Color.ReadManyFrom(ref reader, colorCount);

return new Device(deviceIndex, (DeviceType)deviceType,
name, vendor, description, version, serial, location,
activeMode, modes, zones, leds, colors);
}

/// <inheritdoc />
public override string ToString()
{
return $"{Type}: {Name}";
}
}
}
28 changes: 1 addition & 27 deletions src/OpenRGB.NET/Models/Led.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using OpenRGB.NET.Utils;

namespace OpenRGB.NET;

/// <summary>
/// Led class containing the name of the LED
/// </summary>
public class Led
{
private Led(int index, string name, uint value)
internal Led(int index, string name, uint value)
{
Index = index;
Name = name;
Expand All @@ -29,30 +27,6 @@ private Led(int index, string name, uint value)
/// </summary>
public uint Value { get; }

private static Led ReadFrom(ref SpanReader reader, int index)
{
var name = reader.ReadLengthAndString();
var value = reader.ReadUInt32();

return new Led(index, name, value);
}

/// <summary>
/// Decodes a byte array into a LED array.
/// Increments the offset accordingly.
/// </summary>
/// <param name="reader"></param>
/// <param name="ledCount"></param>
internal static Led[] ReadManyFrom(ref SpanReader reader, ushort ledCount)
{
var leds = new Led[ledCount];

for (var i = 0; i < ledCount; i++)
leds[i] = ReadFrom(ref reader, i);

return leds;
}

/// <inheritdoc />
public override string ToString()
{
Expand Down
35 changes: 1 addition & 34 deletions src/OpenRGB.NET/Models/MatrixMap.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using OpenRGB.NET.Utils;

namespace OpenRGB.NET;

/// <summary>
/// Matrix Map class for the matrix Zone type
/// </summary>
public class MatrixMap
{
private MatrixMap(uint height, uint width, uint[,] matrix)
internal MatrixMap(uint height, uint width, uint[,] matrix)
{
Height = height;
Width = width;
Expand All @@ -28,35 +26,4 @@ private MatrixMap(uint height, uint width, uint[,] matrix)
/// The matrix.
/// </summary>
public uint[,] Matrix { get; }

internal uint Length => Height * Width * 4 + 8;

/// <summary>
/// Decodes a byte array into a matrix map
/// </summary>
/// <param name="reader"></param>
internal static MatrixMap ReadFrom(ref SpanReader reader)
{
var height = reader.ReadUInt32();

var width = reader.ReadUInt32();

var matrix = new uint[height, width];

for (var i = 0; i < height; i++)
for (var j = 0; j < width; j++)
matrix[i, j] = reader.ReadUInt32();

return new MatrixMap(height, width, matrix);
}

internal void WriteTo(ref SpanWriter writer)
{
writer.WriteUInt32(Height);
writer.WriteUInt32(Width);

for (var i = 0; i < Height; i++)
for (var j = 0; j < Width; j++)
writer.WriteUInt32(Matrix[i, j]);
}
}
79 changes: 1 addition & 78 deletions src/OpenRGB.NET/Models/Mode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using OpenRGB.NET.Utils;

namespace OpenRGB.NET;

Expand All @@ -8,7 +7,7 @@ namespace OpenRGB.NET;
/// </summary>
public class Mode
{
private Mode(ProtocolVersion protocolVersion, int index, string name, int value, ModeFlags flags, uint speedMin,
internal Mode(ProtocolVersion protocolVersion, int index, string name, int value, ModeFlags flags, uint speedMin,
uint speedMax, uint brightnessMin, uint brightnessMax, uint colorMin, uint colorMax, uint speed,
uint brightness, Direction direction, ColorMode colorMode, Color[] colors)
{
Expand Down Expand Up @@ -165,80 +164,4 @@ public void SetColors(Color[] newColors)
{
Colors = newColors;
}

internal static Mode ReadFrom(ref SpanReader reader, ProtocolVersion protocolVersion, int index)
{
var name = reader.ReadLengthAndString();
var modeValue = reader.ReadInt32();
var modeFlags = (ModeFlags)reader.ReadUInt32();
var speedMin = reader.ReadUInt32();
var speedMax = reader.ReadUInt32();
var brightMin = protocolVersion.SupportsBrightnessAndSaveMode ? reader.ReadUInt32() : 0;
var brightMax = protocolVersion.SupportsBrightnessAndSaveMode ? reader.ReadUInt32() : 0;
var colorMin = reader.ReadUInt32();
var colorMax = reader.ReadUInt32();
var speed = reader.ReadUInt32();
var brightness = protocolVersion.SupportsBrightnessAndSaveMode ? reader.ReadUInt32() : 0;
var direction = reader.ReadUInt32();
var colorMode = (ColorMode)reader.ReadUInt32();
var colorCount = reader.ReadUInt16();
var colors = Color.ReadManyFrom(ref reader, colorCount);

return new Mode(protocolVersion, index, name, modeValue, modeFlags, speedMin, speedMax,
brightMin, brightMax, colorMin, colorMax, speed, brightness,
(Direction)direction, colorMode, colors);
}

internal static Mode[] ReadManyFrom(ref SpanReader reader, ushort numModes, ProtocolVersion protocolVersion)
{
var modes = new Mode[numModes];

for (var i = 0; i < numModes; i++)
modes[i] = ReadFrom(ref reader, protocolVersion, i);

return modes;
}

internal void WriteTo(ref SpanWriter writer)
{
writer.WriteLengthAndString(Name);
writer.WriteInt32(Value);
writer.WriteUInt32((uint)Flags);
writer.WriteUInt32(SpeedMin);
writer.WriteUInt32(SpeedMax);

if (ProtocolVersion.SupportsBrightnessAndSaveMode)
{
writer.WriteUInt32(BrightnessMin);
writer.WriteUInt32(BrightnessMax);
}

writer.WriteUInt32(ColorMin);
writer.WriteUInt32(ColorMax);
writer.WriteUInt32(Speed);

if (ProtocolVersion.SupportsBrightnessAndSaveMode)
writer.WriteUInt32(Brightness);

writer.WriteUInt32((uint)Direction);
writer.WriteUInt32((uint)ColorMode);
writer.WriteUInt16((ushort)Colors.Length);

foreach (var color in Colors)
color.WriteTo(ref writer);
}

internal int GetLength()
{
var size = (
sizeof(int) * 2 +
sizeof(uint) * 9 +
sizeof(ushort) * 2 +
sizeof(uint) * Colors.Length +
Name.Length + 1);

if (ProtocolVersion.SupportsBrightnessAndSaveMode) size += sizeof(uint) * 3;

return size;
}
}
Loading
Loading