/// <summary> /// Decodes a byte array into a Device. /// </summary> /// <param name="buffer"></param> /// <param name="protocol"></param> /// <param name="client">The owning OpenRGBClient for this device.</param> /// <param name="deviceId">The device ID under the owning client.</param> internal static Device Decode(byte[] buffer, uint protocol, IOpenRGBClient client, int deviceId) { var dev = new Device { ID = deviceId, Client = client }; using (var reader = new BinaryReader(new MemoryStream(buffer))) { var duplicatePacketLength = reader.ReadUInt32(); var deviceType = reader.ReadInt32(); if (Enum.IsDefined(typeof(DeviceType), deviceType)) { dev.Type = (DeviceType)deviceType; } else { dev.Type = DeviceType.Unknown; } dev.Name = reader.ReadLengthAndString(); if (protocol >= 1) { dev.Vendor = reader.ReadLengthAndString(); } else { dev.Vendor = null; } dev.Description = reader.ReadLengthAndString(); dev.Version = reader.ReadLengthAndString(); dev.Serial = reader.ReadLengthAndString(); dev.Location = reader.ReadLengthAndString(); var modeCount = reader.ReadUInt16(); dev.ActiveModeIndex = reader.ReadInt32(); dev.Modes = Mode.Decode(reader, modeCount); var zoneCount = reader.ReadUInt16(); dev.Zones = Zone.Decode(reader, zoneCount, client, deviceId); var ledCount = reader.ReadUInt16(); dev.Leds = Led.Decode(reader, ledCount); var colorCount = reader.ReadUInt16(); dev.Colors = Color.Decode(reader, colorCount); return(dev); } }
/// <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[] Decode(BinaryReader reader, ushort ledCount) { var leds = new Led[ledCount]; for (int i = 0; i < ledCount; i++) { leds[i] = new Led { Name = reader.ReadLengthAndString(), Value = reader.ReadUInt32() }; } return(leds); }
/// <summary> /// Decodes a byte array into a LED array. /// Increments the offset accordingly. /// </summary> /// <param name="buffer"></param> /// <param name="offset"></param> /// <param name="ledCount"></param> /// <returns></returns> internal static Led[] Decode(byte[] buffer, ref int offset, ushort ledCount) { var leds = new Led[ledCount]; for (int i = 0; i < ledCount; i++) { leds[i] = new Led { Name = buffer.GetString(ref offset), Value = buffer.GetUInt32(ref offset) }; } return(leds); }
/// <summary> /// Decodes a byte array into a Device. /// </summary> /// <param name="buffer"></param> /// <param name="protocol"></param> internal static Device Decode(byte[] buffer, uint protocol) { var dev = new Device(); using (var reader = new BinaryReader(new MemoryStream(buffer))) { var duplicatePacketLength = reader.ReadUInt32(); dev.Type = (DeviceType)reader.ReadInt32(); dev.Name = reader.ReadLengthAndString(); if (protocol >= 1) { dev.Vendor = reader.ReadLengthAndString(); } else { dev.Vendor = null; } dev.Description = reader.ReadLengthAndString(); dev.Version = reader.ReadLengthAndString(); dev.Serial = reader.ReadLengthAndString(); dev.Location = reader.ReadLengthAndString(); var modeCount = reader.ReadUInt16(); dev.ActiveModeIndex = reader.ReadInt32(); dev.Modes = Mode.Decode(reader, modeCount); var zoneCount = reader.ReadUInt16(); dev.Zones = Zone.Decode(reader, zoneCount); var ledCount = reader.ReadUInt16(); dev.Leds = Led.Decode(reader, ledCount); var colorCount = reader.ReadUInt16(); dev.Colors = Color.Decode(reader, colorCount); return(dev); } }
/// <summary> /// Decodes a byte array into a Device. /// </summary> /// <param name="buffer"></param> /// <returns></returns> internal static Device Decode(byte[] buffer) { var dev = new Device(); int offset = sizeof(uint); dev.Type = (DeviceType)buffer.GetInt32(ref offset); dev.Name = buffer.GetString(ref offset); dev.Description = buffer.GetString(ref offset); dev.Version = buffer.GetString(ref offset); dev.Serial = buffer.GetString(ref offset); dev.Location = buffer.GetString(ref offset); var modeCount = buffer.GetUInt16(ref offset); dev.ActiveModeIndex = buffer.GetInt32(ref offset); dev.Modes = Mode.Decode(buffer, ref offset, modeCount); var zoneCount = buffer.GetUInt16(ref offset); dev.Zones = Zone.Decode(buffer, ref offset, zoneCount); var ledCount = buffer.GetUInt16(ref offset); dev.Leds = Led.Decode(buffer, ref offset, ledCount); var colorCount = buffer.GetUInt16(ref offset); dev.Colors = Color.Decode(buffer, ref offset, colorCount); return(dev); }