/// <summary> /// Reads the STF file. /// </summary> /// <returns>The statistics.</returns> /// <param name="path">Path.</param> public TrackRecordsEntity Read(string path) { TrackRecordsEntity trackRecordEntity = new TrackRecordsEntity(); using (NfsFileReader reader = new NfsFileReader(path, FileMode.Open)) { reader.ReaderEndianness = Endianness.BigEndian; trackRecordEntity.LapRecords = new LapRecordEntity[31]; for (int i = 0; i < 31; i++) { LapRecordEntity record = new LapRecordEntity(); record.PlayerName = reader.ReadString(9); record.CarId = (short)(reader.ReadInt16() & 0x00FF); record.Time = reader.ReadInt32(Endianness.LittleEndian); record.RaceType = reader.ReadInt16(); reader.ReadBytes(3); // Clean the name int endIndex = record.PlayerName.IndexOf('\0'); if (endIndex > 0) { record.PlayerName = record.PlayerName.Substring(0, endIndex); } record.PlayerName = record.PlayerName.Replace("\t", ""); trackRecordEntity.LapRecords[i] = record; } } return(trackRecordEntity); }
/// <summary> /// Converts the domain model into an entity. /// </summary> /// <returns>The entity.</returns> /// <param name="model">LapRecord.</param> internal static LapRecordEntity ToEntity(this LapRecord model) { LapRecordEntity entity = new LapRecordEntity { PlayerName = model.PlayerName.PadRight(8, '\t').Substring(0, 8), CarId = (short)model.Car, Time = (int)(model.Time * 16384), RaceType = (short)model.RaceType }; return(entity); }
/// <summary> /// Converts the entity into a domain model. /// </summary> /// <returns>The domain model.</returns> /// <param name="lapRecordEntity">LapRecord entity.</param> internal static LapRecord ToDomainModel(this LapRecordEntity lapRecordEntity) { LapRecord lapRecord = new LapRecord { PlayerName = lapRecordEntity.PlayerName, Car = (CarType)lapRecordEntity.CarId, Time = (float)RoundDown((float)lapRecordEntity.Time / 16384, 2), RaceType = (RaceType)lapRecordEntity.RaceType }; return(lapRecord); }
/// <summary> /// Writes the STF file. /// </summary> /// <param name="path">Path.</param> /// <param name="statsFileEntity">Stats file entity.</param> public void Write(string path, TrackRecordsEntity statsFileEntity) { using (NfsFileWriter writer = new NfsFileWriter(path, FileMode.OpenOrCreate)) { writer.WriterEndianness = Endianness.BigEndian; for (int i = 0; i < 31; i++) { LapRecordEntity record = statsFileEntity.LapRecords[i]; writer.WriteString(record.PlayerName, 8, '\t'); writer.WriteInt16(record.CarId); writer.WriteInt32(record.Time, Endianness.LittleEndian); writer.WriteInt16(record.RaceType); writer.WriteBytes(new byte[] { 0, 0, 0 }); } } }