public static void LoadDictionary(AsyncUI mainWindow) { String json = "[]"; if (File.Exists(BS(baseDir) + dictFileName)) { json = File.ReadAllText(BS(baseDir) + dictFileName); } Laps = JsonSerializer.Deserialize <List <LapInfo> >(json); int i = Laps.Count; while (i > 0) { i--; if (!File.Exists(BS(baseDir) + BS(Laps[i].CircuitName) + Laps[i].FileName)) { mainWindow.LogLine("Not found:" + Laps[i].FileName); Laps.RemoveAt(i); } } mainProcess = mainWindow; mainWindow.Log($"{Laps.Count} laps loaded"); }
void HandlePacket(Stream fIn, CancellationToken token) { // read Header var packet = new PacketInfo() { sequence = packetCount }; packet.header = PacketHelper.SafeRead <PacketHeader>(fIn, PacketSize.PacketHeaderSize); if (fIn.Position == fIn.Length) { // No more data return; } // callBack.Log($"{packet.header.frameIdentifier}-"); while (packet.header.frameIdentifier == 0 && packet.header.packetFormat != 2021) { // empty packet? packet.header = PacketHelper.SafeRead <PacketHeader>(fIn, PacketSize.PacketHeaderSize); } //if (packet.header.packetFormat != 2020 && packet.header.packetFormat != 2021) //{ // callBack.LogLine($"FATAL: Invalid packet format: last Packet {packet.header.packetFormat}"); // //throw new Exception($"Invalid packet format: last Packet {packet.header.packetFormat}"); //} if (ReaderMode == ReaderMode.rmReplay) { // Simulate time if (Starttijd == DateTime.MinValue) { Starttijd = DateTime.Now.Add(TimeSpan.FromSeconds(-packet.header.sessionTime)); } else { bool inPause = callBack.IsPausing(); DateTime startPause = DateTime.Now; while (!token.IsCancellationRequested && (callBack.IsPausing() || (Starttijd.Add(TimeSpan.FromSeconds(packet.header.sessionTime)) > DateTime.Now))) { Thread.Sleep(1); } if (inPause) { Starttijd = Starttijd.Add(DateTime.Now - startPause); } } } switch (packet.header.packetId) { case 0: // Motion { if (logPackets) { callBack.Log("."); // Motion info } packet.details = PacketHelper.SafeRead <PacketMotionData>(fIn, PacketSize.PacketMotionDataSize); callBack.UpdateMotion(packet.header, (PacketMotionData)packet.details); break; } case 1: // Session { if (logPackets) { callBack.Log($"1S"); // Data about the session – track, time left } packet.details = PacketHelper.SafeRead <PacketSessionData>(fIn, PacketSize.PacketSessionDataSize); callBack.UpdateSession(packet.header, (PacketSessionData)packet.details); break; } case 2: // Lap { if (logPackets) { callBack.Log($"2L"); // Data about all the lap times of cars in the session } packet.details = PacketHelper.SafeRead <PacketLapData>(fIn, PacketSize.PacketLapDataSize); callBack.UpdateLapdata(packet.header, (PacketLapData)packet.details); break; } case 3: // Event { if (logPackets) { callBack.Log($"3E"); } var ped = new PacketEventData() { eventStringCode = PacketHelper.GetEventType(fIn), eventDetails = null }; packet.details = ped; // Depending on type of event the contents may differ. In C++ this is a union. Therefore the largest packetsize is the size. byte[] data = new byte[PacketSize.FlashbackSize]; if (fIn.Read(data, 0, PacketSize.FlashbackSize) != PacketSize.FlashbackSize) { throw new Exception("Size error"); } // Read all the remaining bytes into a memory stream to be able to proces them later MemoryStream ms = new MemoryStream(data); switch (ped.eventStringCode) { case EventType.Unknown: break; case EventType.SessionStarted: callBack.StartSession(packet.header); break; case EventType.SessionEnded: callBack.EndSession(packet.header); SaveData(); break; case EventType.FastestLap: ped.eventDetails = PacketHelper.SafeRead <FastestLap>(ms, PacketSize.FastestLapSize); break; case EventType.Retirement: ped.eventDetails = PacketHelper.SafeRead <Retirement>(ms, PacketSize.RetirementSize); break; case EventType.DRSEnabled: break; case EventType.DRSDisabled: break; case EventType.TeammateInPits: ped.eventDetails = PacketHelper.SafeRead <TeamMateInPits>(ms, PacketSize.TeamMateInPitsSize); break; case EventType.CheckeredFlag: break; case EventType.RaceWinner: ped.eventDetails = PacketHelper.SafeRead <RaceWinner>(ms, PacketSize.RaceWinnerSize); break; case EventType.PenaltyIssued: ped.eventDetails = PacketHelper.SafeRead <Penalty>(ms, PacketSize.PenaltySize); break; case EventType.SpeedTrapTriggered: ped.eventDetails = PacketHelper.SafeRead <SpeedTrap>(ms, PacketSize.SpeedTrapSize); break; default: break; } if (logPackets) { Console.Write("Evt "); // Various notable events that happen during a session } break; } case 4: // Participants { if (logPackets) { callBack.Log($"4P"); /// List of participants in the session, mostly relevant for multiplayer } packet.details = PacketHelper.SafeRead <PacketParticipantsData>(fIn, PacketSize.PacketParticipantsDataSize); Task t = new Task(() => { callBack.UpdatePaticipants((PacketParticipantsData)packet.details); }); t.Start(); break; } case 5: // Car Setup { if (logPackets) { callBack.Log("5C"); } packet.details = PacketHelper.SafeRead <PacketCarSetupData>(fIn, PacketSize.PacketCarSetupDataSize); Console.Write("Setup "); /// Packet detailing car setups for cars in the race callBack.UpdateSetup(packet.header, (PacketCarSetupData)packet.details); break; } case 6: // Car telemetry { if (logPackets) { callBack.Log("6T"); /// Telemetry data for all carsbreak; } packet.details = PacketHelper.SafeRead <PacketCarTelemetryData>(fIn, PacketSize.PacketCarTelemetryDataSize); callBack.UpdateTelemetry(packet.header, (PacketCarTelemetryData)packet.details); break; } case 7: // Car Status { if (logPackets) { callBack.Log("7ST"); } packet.details = PacketHelper.SafeRead <PacketCarStatusData>(fIn, PacketSize.PacketCarStatusDataSize); break; } case 8: // Final classification { if (logPackets) { callBack.Log("8F"); } packet.details = PacketHelper.SafeRead <PacketFinalClassificationData>(fIn, PacketSize.PacketFinalClassificationDataSize); break; } case 9: // Lobby { if (logPackets) { callBack.Log("9L"); } packet.details = PacketHelper.SafeRead <PacketLobbyInfoData>(fIn, PacketSize.PacketLobbyInfoDataSize); break; } case 10: // Car Damage { if (logPackets) { callBack.Log("10L"); } packet.details = PacketHelper.SafeRead <PacketCarDamageData>(fIn, PacketSize.PacketCarDamageDataSize); break; } case 11: // Session history { if (logPackets) { callBack.Log("11L"); } packet.details = PacketHelper.SafeRead <PacketSessionHistoryData>(fIn, PacketSize.PacketSessionHistoryDataSize); break; } default: { Console.Write($"??? Unknown packet type: {packet.header.packetId}"); callBack.LogLine($"??? Unknown packet type: {packet.header.packetId}"); break; } } // notificate UI of packet callBack.AddPacket(packet); lastPacket = packet; }