Skip to content

Commit d58b029

Browse files
feat: update packets to support for F1 25 (#11)
1 parent 7d16201 commit d58b029

36 files changed

+543
-98
lines changed

F1Game.UDP.Benchmarks/Helpers/PacketGenerator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ static int GetPacketSize(PacketType packetType)
121121
PacketType.TyreSets => GetSize<TyreSetsDataPacket>(),
122122
PacketType.MotionEx => GetSize<MotionExDataPacket>(),
123123
PacketType.TimeTrial => GetSize<TimeTrialDataPacket>(),
124+
PacketType.LapPositions => GetSize<LapPositionsDataPacket>(),
124125
_ => throw new ArgumentOutOfRangeException(nameof(packetType), packetType, null)
125126
};
126127
}

F1Game.UDP.Benchmarks/InternalBenchmark.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public class InternalBenchmark
2424
PacketType.Session,
2525
PacketType.SessionHistory,
2626
PacketType.TyreSets,
27-
PacketType.TimeTrial
27+
PacketType.TimeTrial,
28+
PacketType.LapPositions
2829
])]
2930
public PacketType Type { get; set; }
3031

F1Game.UDP.Benchmarks/ThirdPartyComparisonBenchmark.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public class ThirdPartyComparisonBenchmark
2626
PacketType.SessionHistory,
2727
PacketType.TyreSets,
2828
PacketType.Event,
29-
PacketType.TimeTrial
29+
PacketType.TimeTrial,
30+
PacketType.LapPositions
3031
])]
3132
public PacketType Type { get; set; }
3233

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using BenchmarkDotNet.Attributes;
2+
3+
using F1Game.UDP.Benchmarks.Helpers;
4+
using F1Game.UDP.Enums;
5+
using F1Game.UDP.Packets;
6+
7+
namespace F1Game.UDP.Benchmarks;
8+
9+
[MemoryDiagnoser(true)]
10+
public class ToPacketBenchmark
11+
{
12+
[Params([
13+
PacketType.CarDamage,
14+
PacketType.CarSetups,
15+
PacketType.CarStatus,
16+
PacketType.CarTelemetry,
17+
PacketType.Event,
18+
PacketType.FinalClassification,
19+
PacketType.LapData,
20+
PacketType.LobbyInfo,
21+
PacketType.Motion,
22+
PacketType.MotionEx,
23+
PacketType.Participants,
24+
PacketType.Session,
25+
PacketType.SessionHistory,
26+
PacketType.TyreSets,
27+
PacketType.TimeTrial,
28+
PacketType.LapPositions
29+
])]
30+
public PacketType Type { get; set; }
31+
32+
byte[] data = [];
33+
34+
[GlobalSetup]
35+
public void GlobalSetup()
36+
{
37+
data = PacketGenerator.GeneratePacket(Type);
38+
}
39+
40+
[Benchmark(Baseline = true)]
41+
public UnionPacket ReadPacket()
42+
{
43+
return data.ToPacket();
44+
}
45+
}

F1Game.UDP.Tests/PacketReaderFixture.cs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public void ReadEventDataPacket(EventType eventType)
165165
EventType.Overtake => fixture.Create<OvertakeEvent>(),
166166
EventType.SafetyCar => fixture.Create<SafetyCarEvent>(),
167167
EventType.Collision => fixture.Create<CollisionEvent>(),
168+
EventType.DRSDisabled => fixture.Create<DrsDisabledEvent>(),
168169
_ => new EventDetails { EventType = eventType },
169170
};
170171

@@ -221,7 +222,7 @@ public void ReadLapDataPacket()
221222
public void ReadLobbyInfoDataPacket()
222223
{
223224
var lobbyPlayers = fixture.Build<LobbyInfoData>()
224-
.With(x => x.NameBytes, fixture.Create<string>().AsArray48Bytes())
225+
.With(x => x.NameBytes, fixture.Create<string>().AsArray32Bytes())
225226
.CreateMany(22)
226227
.ToArray();
227228

@@ -270,7 +271,8 @@ public void ReadMotionExDataPacket()
270271
public void ReadParticipantsDataPacket()
271272
{
272273
var participants = fixture.Build<ParticipantData>()
273-
.With(x => x.NameBytes, fixture.Create<string>().AsArray48Bytes())
274+
.With(x => x.NameBytes, fixture.Create<string>().AsArray32Bytes())
275+
.With(x => x.LiveryColors, fixture.CreateMany<LiveryColor>(4).ToArray())
274276
.CreateMany(22)
275277
.ToArray();
276278

@@ -347,6 +349,25 @@ public void ReadTimeTrialDataPacket()
347349
bytes.ToPacketWithReader().Should().BeEquivalentTo(packet, Configure);
348350
}
349351

352+
[Test]
353+
public void ReadLapPositionsDataPacket()
354+
{
355+
var positionsPerLapForVehicle = Enumerable.Range(0, 50)
356+
.Select(_ => Array22<byte>.Create(fixture.CreateMany<byte>(22).ToArray()))
357+
.ToArray();
358+
359+
UnionPacket packet = BuildPacket<LapPositionsDataPacket>()
360+
.With(x => x.PositionsPerLapForVehicle, positionsPerLapForVehicle)
361+
.Create();
362+
363+
var bytes = new byte[GetSize<LapPositionsDataPacket>()];
364+
var writer = new BytesWriter(bytes);
365+
writer.Write(packet);
366+
367+
bytes.ToPacket().Should().BeEquivalentTo(packet, Configure);
368+
bytes.ToPacketWithReader().Should().BeEquivalentTo(packet, Configure);
369+
}
370+
350371
IPostprocessComposer<T> BuildPacket<T>() where T : IHaveHeader, new()
351372
{
352373
var packetType = new T() switch
@@ -366,6 +387,7 @@ public void ReadTimeTrialDataPacket()
366387
SessionHistoryDataPacket => PacketType.SessionHistory,
367388
TyreSetsDataPacket => PacketType.TyreSets,
368389
TimeTrialDataPacket => PacketType.TimeTrial,
390+
LapPositionsDataPacket => PacketType.LapPositions,
369391
_ => throw new NotImplementedException()
370392
};
371393

F1Game.UDP/Data/Arrays.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace F1Game.UDP.Data;
44

5+
[AutoInlineArray(4)]
6+
public partial struct Array4<T>;
7+
58
[AutoInlineArray(8)]
69
public partial struct Array8<T>;
710

@@ -17,8 +20,11 @@ public partial struct Array21<T>;
1720
[AutoInlineArray(22)]
1821
public partial struct Array22<T>;
1922

20-
[AutoInlineArray(48)]
21-
public partial struct Array48<T>;
23+
[AutoInlineArray(32)]
24+
public partial struct Array32<T>;
25+
26+
[AutoInlineArray(50)]
27+
public partial struct Array50<T>;
2228

2329
[AutoInlineArray(64)]
2430
public partial struct Array64<T>;

F1Game.UDP/Data/CarDamageData.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[StructLayout(LayoutKind.Sequential, Pack = 1)]
44
public readonly record struct CarDamageData() : IByteParsable<CarDamageData>, IByteWritable, ISizeable
55
{
6-
static int ISizeable.Size => 42;
6+
static int ISizeable.Size => 46;
77

88
/// <summary>
99
/// Tyre wear (percentage).
@@ -18,6 +18,10 @@ public readonly record struct CarDamageData() : IByteParsable<CarDamageData>, IB
1818
/// </summary>
1919
public Tyres<byte> BrakesDamage { get; init; }
2020
/// <summary>
21+
/// Tyre blisters value (percentage)
22+
/// </summary>
23+
public Tyres<byte> TyresBlisters { get; init; }
24+
/// <summary>
2125
/// Front left wing damage (percentage).
2226
/// </summary>
2327
public byte FrontLeftWingDamage { get; init; }
@@ -97,6 +101,7 @@ static CarDamageData IByteParsable<CarDamageData>.Parse(ref BytesReader reader)
97101
TyresWear = reader.GetNextTyresFloat(),
98102
TyresDamage = reader.GetNextTyresByte(),
99103
BrakesDamage = reader.GetNextTyresByte(),
104+
TyresBlisters = reader.GetNextTyresByte(),
100105
FrontLeftWingDamage = reader.GetNextByte(),
101106
FrontRightWingDamage = reader.GetNextByte(),
102107
RearWingDamage = reader.GetNextByte(),
@@ -123,6 +128,7 @@ void IByteWritable.WriteBytes(ref BytesWriter writer)
123128
writer.WriteTyresFloat(TyresWear);
124129
writer.WriteTyresByte(TyresDamage);
125130
writer.WriteTyresByte(BrakesDamage);
131+
writer.WriteTyresByte(TyresBlisters);
126132
writer.Write(FrontLeftWingDamage);
127133
writer.Write(FrontRightWingDamage);
128134
writer.Write(RearWingDamage);

F1Game.UDP/Data/FinalClassificationData.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace F1Game.UDP.Data;
55
[StructLayout(LayoutKind.Sequential, Pack = 1)]
66
public readonly record struct FinalClassificationData() : IByteParsable<FinalClassificationData>, IByteWritable, ISizeable
77
{
8-
static int ISizeable.Size => 45;
8+
static int ISizeable.Size => 46;
99

1010
/// <summary>
1111
/// The finishing position.
@@ -32,6 +32,10 @@ public readonly record struct FinalClassificationData() : IByteParsable<FinalCla
3232
/// </summary>
3333
public ResultStatus ResultStatus { get; init; }
3434
/// <summary>
35+
/// The reason for the result, such as DNF or DSQ.
36+
/// </summary>
37+
public ResultReason ResultReason { get; init; }
38+
/// <summary>
3539
/// The best lap time of the session in milliseconds.
3640
/// </summary>
3741
public uint BestLapTimeInMS { get; init; }
@@ -74,6 +78,7 @@ static FinalClassificationData IByteParsable<FinalClassificationData>.Parse(ref
7478
Points = reader.GetNextByte(),
7579
NumPitStops = reader.GetNextByte(),
7680
ResultStatus = reader.GetNextEnum<ResultStatus>(),
81+
ResultReason = reader.GetNextEnum<ResultReason>(),
7782
BestLapTimeInMS = reader.GetNextUInt(),
7883
TotalRaceTime = reader.GetNextDouble(),
7984
PenaltiesTime = reader.GetNextByte(),
@@ -93,6 +98,7 @@ void IByteWritable.WriteBytes(ref BytesWriter writer)
9398
writer.Write(Points);
9499
writer.Write(NumPitStops);
95100
writer.WriteEnum(ResultStatus);
101+
writer.WriteEnum(ResultReason);
96102
writer.Write(BestLapTimeInMS);
97103
writer.Write(TotalRaceTime);
98104
writer.Write(PenaltiesTime);

F1Game.UDP/Data/LiveryColor.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace F1Game.UDP.Data;
2+
3+
[StructLayout(LayoutKind.Sequential, Pack = 1)]
4+
public readonly record struct LiveryColor() : IByteParsable<LiveryColor>, IByteWritable, ISizeable
5+
{
6+
static int ISizeable.Size => 3;
7+
8+
/// <summary>
9+
/// The red component of the livery color.
10+
/// </summary>
11+
public byte Red { get; init; }
12+
/// <summary>
13+
/// The green component of the livery color.
14+
/// </summary>
15+
public byte Green { get; init; }
16+
/// <summary>
17+
/// The blue component of the livery color.
18+
/// </summary>
19+
public byte Blue { get; init; }
20+
21+
static LiveryColor IByteParsable<LiveryColor>.Parse(ref BytesReader reader)
22+
{
23+
return new()
24+
{
25+
Red = reader.GetNextByte(),
26+
Green = reader.GetNextByte(),
27+
Blue = reader.GetNextByte()
28+
};
29+
}
30+
31+
void IByteWritable.WriteBytes(ref BytesWriter writer)
32+
{
33+
writer.Write(Red);
34+
writer.Write(Green);
35+
writer.Write(Blue);
36+
}
37+
}

F1Game.UDP/Data/LobbyInfoData.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace F1Game.UDP.Data;
55
[StructLayout(LayoutKind.Sequential, Pack = 1)]
66
public readonly record struct LobbyInfoData() : IByteParsable<LobbyInfoData>, IByteWritable, ISizeable
77
{
8-
static int ISizeable.Size => 58;
8+
static int ISizeable.Size => 42;
99

1010
/// <summary>
1111
/// Gets whether the vehicle is AI or Human.
@@ -24,13 +24,13 @@ public readonly record struct LobbyInfoData() : IByteParsable<LobbyInfoData>, IB
2424
/// </summary>
2525
public Platform Platform { get; init; }
2626
/// <summary>
27-
/// The name of participant in UTF-8 bytes – null terminated. Will be truncated with ... (U+2026) if too long; 48 chars.
27+
/// The name of participant in UTF-8 bytes – null terminated. Will be truncated with ... (U+2026) if too long; 32 bytes maximum.
2828
/// </summary>
29-
public Array48<byte> NameBytes { get; init; }
29+
public Array32<byte> NameBytes { get; init; }
3030
/// <summary>
31-
/// The name of participant in UTF-8 format – null terminated. Will be truncated with ... (U+2026) if too long; 48 chars.
31+
/// The name of participant in UTF-8 format – null terminated. Will be truncated with ... (U+2026) if too long; 32 bytes maximum.
3232
/// </summary>
33-
public string Name { get => NameBytes.AsString(); init => NameBytes = value.AsArray48Bytes(); }
33+
public string Name { get => NameBytes.AsString(); init => NameBytes = value.AsArray32Bytes(); }
3434
/// <summary>
3535
/// Car number of the player.
3636
/// </summary>
@@ -60,7 +60,7 @@ static LobbyInfoData IByteParsable<LobbyInfoData>.Parse(ref BytesReader reader)
6060
Team = reader.GetNextEnum<Team>(),
6161
Nationality = reader.GetNextEnum<Nationality>(),
6262
Platform = reader.GetNextEnum<Platform>(),
63-
NameBytes = reader.GetNextBytes(48),
63+
NameBytes = reader.GetNextBytes(32),
6464
CarNumber = reader.GetNextByte(),
6565
IsTelemetryPublic = reader.GetNextBoolean(),
6666
ShowOnlineNames = reader.GetNextBoolean(),

0 commit comments

Comments
 (0)