Вот и мы; здесь было на что посмотреть; во-первых, [4]
et c в спецификации относится к фиксированному количеству этих вещей, а не одного из них - посмотрите на температуру шин и т. д .; теперь вы можете сделать это с буфером fixed
в C#, но это очень болезненно использовать, так как для этого требуется код unsafe
, поэтому вместо этого я просто использовал некоторые пользовательские типы квадов; Я также четко изложил все структуры. Вместо того, чтобы читать заголовок дважды, здесь я последовательно анализирую его как 20 экземпляров машины.
Результат: все байты учтены; демонстрационный код.
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Explicit, Pack = 0, Size = 23)]
public struct PacketHeader
{
[FieldOffset(0)]
public ushort m_packetFormat; // 2019
[FieldOffset(2)]
public byte m_gameMajorVersion; // Game major version - "X.00"
[FieldOffset(3)]
public byte m_gameMinorVersion; // Game minor version - "1.XX"
[FieldOffset(4)]
public byte m_packetVersion; // Version of this packet type, all start from 1
[FieldOffset(5)]
public PacketType m_packetId; // Identifier for the packet type, see below
[FieldOffset(6)]
public ulong m_sessionUID; // Unique identifier for the session
[FieldOffset(14)]
public float m_sessionTime; // Session timestamp
[FieldOffset(18)]
public uint m_frameIdentifier; // Identifier for the frame the data was retrieved on
[FieldOffset(22)]
public byte m_playerCarIndex; // Index of player's car in the array
};
public enum PacketType : byte
{
Motion = 0, // Contains all motion data for player’s car – only sent while player is in control
Session = 1,// Data about the session – track, time left
LapData = 2,// Data about all the lap times of cars in the session
Event = 3, // Various notable events that happen during a session
Participants = 4, // List of participants in the session, mostly relevant for multiplayer
CarSetups = 5, // Packet detailing car setups for cars in the race
CarTelemetry = 6, // Telemetry data for all cars
CarStatus = 7 // Status data for all cars such as damage
}
[StructLayout(LayoutKind.Explicit, Pack = 0, Size = 63)]
public struct CarTelemetryData
{
[FieldOffset(0)]
public ushort m_speed; // Speed of car in kilometres per hour
[FieldOffset(2)]
public float m_throttle; // Amount of throttle applied (0.0 to 1.0)
[FieldOffset(6)]
public float m_steer; // Steering (-1.0 (full lock left) to 1.0 (full lock right))
[FieldOffset(10)]
public float m_brake; // Amount of brake applied (0.0 to 1.0)
[FieldOffset(14)]
public byte m_clutch; // Amount of clutch applied (0 to 100)
[FieldOffset(15)]
public sbyte m_gear; // Gear selected (1-8, N=0, R=-1)
[FieldOffset(16)]
public ushort m_engineRPM; // Engine RPM
[FieldOffset(18)]
public byte m_drs; // 0 = off, 1 = on
[FieldOffset(19)]
public byte m_revLightsPercent; // Rev lights indicator (percentage)
[FieldOffset(20)]
public UInt16Quad m_brakesTemperature; // Brakes temperature (celsius)
[FieldOffset(28)]
public UInt16Quad m_tyresSurfaceTemperature; // Tyres surface temperature (celsius)
[FieldOffset(36)]
public UInt16Quad m_tyresInnerTemperature; // Tyres inner temperature (celsius)
[FieldOffset(44)]
public ushort m_engineTemperature; // Engine temperature (celsius)
[FieldOffset(46)]
public SingleQuad tyresPressure; // Tyres pressure (PSI)
[FieldOffset(62)]
public ByteQuad m_surfaceType; // Driving surface, see appendice
};
[StructLayout(LayoutKind.Explicit, Pack = 0, Size = 8)]
public struct UInt16Quad
{
[FieldOffset(0)]
public ushort A;
[FieldOffset(2)]
public ushort B;
[FieldOffset(6)]
public ushort C;
[FieldOffset(6)]
public ushort D;
}
[StructLayout(LayoutKind.Explicit, Pack = 0, Size = 16)]
public struct SingleQuad
{
[FieldOffset(0)]
public float A;
[FieldOffset(4)]
public float B;
[FieldOffset(8)]
public float C;
[FieldOffset(12)]
public float D;
}
[StructLayout(LayoutKind.Explicit, Pack = 0, Size = 4)]
public struct ByteQuad
{
[FieldOffset(0)]
public byte A;
[FieldOffset(1)]
public byte B;
[FieldOffset(2)]
public byte C;
[FieldOffset(3)]
public byte D;
}
static class P
{
static void Main()
{
Console.WriteLine(Unsafe.SizeOf<PacketHeader>());
Console.WriteLine(Unsafe.SizeOf<CarTelemetryData>());
Console.WriteLine(bytes.Length);
ReadOnlySpan<byte> remaining = bytes;
var header = MemoryMarshal.Cast<byte, PacketHeader>(remaining)[0];
remaining = remaining.Slice(Unsafe.SizeOf<PacketHeader>());
Console.WriteLine($"{header.m_packetFormat} {header.m_gameMajorVersion}, {header.m_gameMinorVersion}: {header.m_packetId}");
switch (header.m_packetId)
{
case PacketType.CarTelemetry:
// we expect 20*CarTelemetryData, then a uint of the buttons
// note we *could* use a fixed buffer for this, but... that's really very
// awkward to work with; let's try this instead
int carIndex = 0;
foreach (var telemetry in MemoryMarshal.Cast<byte, CarTelemetryData>(remaining).Slice(0, 20))
{
Console.WriteLine($"car: {carIndex}, speed: {telemetry.m_speed}, throttle: {telemetry.m_throttle}");
var temps = telemetry.m_tyresInnerTemperature;
Console.WriteLine($" type temps: {temps.A}/{temps.B}/{temps.C}/{temps.D}");
}
remaining = remaining.Slice(20 * Unsafe.SizeOf<CarTelemetryData>());
var buttons = MemoryMarshal.Cast<byte, uint>(remaining)[0];
Console.WriteLine($"Buttons: {buttons}");
remaining = remaining.Slice(sizeof(uint));
Console.WriteLine($"Unaccounted for: {remaining.Length}");
break;
}
}
static readonly byte[] bytes = new byte[] {
0xE3, 0x07, 0x01, 0x15, 0x01, 0x06, 0xF8, 0xCE, 0xBE, 0x09, 0x64, 0x7F, 0xEA, 0xD1, 0xDC, 0xA1, 0x16, 0x41, 0x91, 0x01, 0x00, 0x00, 0x13, 0x7B, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x36, 0x53, 0x7E, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xF3, 0x2F, 0x00, 0x64, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x74, 0x00, 0x74, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5B, 0x00, 0x5B, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x1B, 0xF2, 0x3A, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x35, 0x2D, 0x00, 0x22, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x69, 0x00, 0x69, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5A, 0x00, 0x5A, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x7B, 0x00, 0x00, 0x00, 0x80, 0x3F, 0xF3, 0x59, 0xA1, 0xBA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x59, 0x2F, 0x00, 0x64, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x72, 0x00, 0x72, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5A, 0x00, 0x5A, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00, 0x80, 0x3F, 0xFE, 0x5F, 0x3F, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x3D, 0x2E, 0x00, 0x3E, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x71, 0x00, 0x71, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5A, 0x00, 0x5A, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x81, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x89, 0x78, 0x23, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xE0, 0x28, 0x00, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x77, 0x00, 0x78, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5B, 0x00, 0x5B, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x77, 0x00, 0x00, 0x00, 0x80, 0x3F, 0xC3, 0x73, 0x98, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x2F, 0x2E, 0x00, 0x54, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x75, 0x00, 0x75, 0x00, 0x50, 0x00, 0x4F, 0x00, 0x5B, 0x00, 0x5B, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x76, 0x00, 0x00, 0x00, 0x80, 0x3F, 0xF3, 0xF4, 0xBA, 0xBA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x21, 0x2E, 0x00, 0x59, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x75, 0x00, 0x75, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5B, 0x00, 0x5B, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x4D, 0x7D, 0x04, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xFB, 0x2F, 0x00, 0x64, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x70, 0x00, 0x70, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5A, 0x00, 0x5A, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x84, 0x45, 0xAB, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xBF, 0x2D, 0x00, 0x31, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x77, 0x00, 0x77, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5B, 0x00, 0x5B, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x80, 0x3F, 0xB5, 0x8C, 0xBD, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x1B, 0x28, 0x00, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x71, 0x00, 0x71, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5A, 0x00, 0x5A, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x80, 0x3F, 0xF8, 0xD0, 0x87, 0x3A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x63, 0x2C, 0x00, 0x37, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x79, 0x00, 0x79, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5B, 0x00, 0x5B, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x7B, 0x00, 0x00, 0x00, 0x80, 0x3F, 0xFF, 0xA7, 0x60, 0x3A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xDE, 0x2F, 0x00, 0x64, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x6E, 0x00, 0x6E, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5A, 0x00, 0x5A, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x7A, 0x00, 0x00, 0x00, 0x80, 0x3F, 0xB9, 0x2E, 0x2F, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xD1, 0x2F, 0x00, 0x64, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x75, 0x00, 0x75, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5B, 0x00, 0x5B, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x74, 0x00, 0x00, 0x00, 0x80, 0x3F, 0xCD, 0x1E, 0x18, 0x3A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x3F, 0x2D, 0x00, 0x23, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x6F, 0x00, 0x6F, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5A, 0x00, 0x5A, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x73, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x15, 0x0F, 0xAF, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xA5, 0x2B, 0x00, 0x0C, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x70, 0x00, 0x70, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5A, 0x00, 0x5A, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x7A, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x2B, 0xC0, 0x14, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x49, 0x2F, 0x00, 0x64, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x6F, 0x00, 0x6F, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5A, 0x00, 0x5A, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x41, 0xCA, 0xC0, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x85, 0x2B, 0x00, 0x64, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x79, 0x00, 0x79, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5B, 0x00, 0x5B, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x72, 0x00, 0x00, 0x00, 0x80, 0x3F, 0x07, 0x1F, 0xD5, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xDE, 0x2A, 0x00, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x78, 0x00, 0x78, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5B, 0x00, 0x5B, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x71, 0x00, 0x00, 0x00, 0x80, 0x3F, 0xDD, 0xC0, 0x1C, 0x3C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xDD, 0x2A, 0x00, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x6F, 0x00, 0x6F, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x5A, 0x00, 0x5A, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xAC, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0xB8, 0x41, 0x00, 0x00, 0x00, 0x00, 0x31, 0x00, 0x97, 0x96, 0x96, 0x3E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x9B, 0x17, 0x00, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x1D, 0x00, 0x62, 0x00, 0x62, 0x00, 0x4F, 0x00, 0x4F, 0x00, 0x59, 0x00, 0x59, 0x00, 0x58, 0x00, 0x58, 0x00, 0x5A, 0x00, 0xCD, 0xCC, 0xA8, 0x41, 0xCD, 0xCC, 0xA8, 0x41, 0x33, 0x33, 0xBB, 0x41, 0x33, 0x33, 0xBB, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
}