From fdc53f80e7b5a4e2944c0a962ef255f3352da86a Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Sun, 27 Oct 2024 15:29:48 +0000 Subject: [PATCH] Formatting and added errors to the var header parser --- diskSubHeader.go | 4 ++-- headers.go | 36 ++++++++++++++++++------------------ irsdk.go | 36 +++++++++++++++++++----------------- sessionInfo.go | 17 ++++++++--------- variables.go | 28 ++++++++++++++-------------- 5 files changed, 61 insertions(+), 60 deletions(-) diff --git a/diskSubHeader.go b/diskSubHeader.go index 7d17898..12b0ad9 100644 --- a/diskSubHeader.go +++ b/diskSubHeader.go @@ -7,7 +7,7 @@ import ( ) const ( - SubHeaderSize = 32 // SubHeaderSize is the size of the subheader + SubHeaderSize = 32 // SubHeaderSize is the size of the subheader ) // DiskSubHeader represents the IBT sub headers @@ -23,7 +23,7 @@ type DiskSubHeader struct { // or nil if an error occurs. In which case the error return value is more // valuable func parseTelemetrySubHeader(buf [SubHeaderSize]byte) (*DiskSubHeader, error) { - dst := DiskSubHeader{} + dst := DiskSubHeader{} err := binary.Read(bytes.NewBuffer(buf[:]), binary.LittleEndian, &dst) if err != nil { return nil, err diff --git a/headers.go b/headers.go index 2980c2d..80c4a95 100644 --- a/headers.go +++ b/headers.go @@ -13,30 +13,30 @@ const ( // TelemetryHeaders struct to hold an IBT file's headers type TelemetryHeaders struct { - Version int32 - // Status of 1 indicates a completed session and status of 0 a live session - Status int32 - // TickRate indicates the frequency of writes (usually 60) - TickRate int32 - // SessionInfoUpdate indicates the number of times the SessionInfo was - // updated. 0 for finished sessions and >1 for active sessions + Version int32 + // Status of 1 indicates a completed session and status of 0 a live session + Status int32 + // TickRate indicates the frequency of writes (usually 60) + TickRate int32 + // SessionInfoUpdate indicates the number of times the SessionInfo was + // updated. 0 for finished sessions and >1 for active sessions SessionInfoUpdate int32 - // SessionInfoLength is the length of the session info buffer + // SessionInfoLength is the length of the session info buffer SessionInfoLength int32 - // SessionInfoOffset is the offset of the session info in the buffer + // SessionInfoOffset is the offset of the session info in the buffer SessionInfoOffset int32 - // NumVars is the number of variables in each input - NumVars int32 - // VarHeaderOffset is the offset of the VarHeader - VarHeaderOffset int32 + // NumVars is the number of variables in each input + NumVars int32 + // VarHeaderOffset is the offset of the VarHeader + VarHeaderOffset int32 // NumBuf will be 1 for static files and 3 for live telemetry files NumBuf int32 - // BufLen is the length for parsing VarHeader values + // BufLen is the length for parsing VarHeader values BufLen int32 - // Padding - Padding [12]byte - // I still don't know what this is: - BufOffset int32 + // Padding + Padding [12]byte + // I still don't know what this is: + BufOffset int32 } // ToString renders a string showing the values of the struct diff --git a/irsdk.go b/irsdk.go index 7d53650..ef12742 100644 --- a/irsdk.go +++ b/irsdk.go @@ -11,7 +11,6 @@ import ( const ( ibtFile = "./telemetryFiles/mx5_2016Okayama_full_2024_10_19_22_02_12.ibt" - // ibtFile = "./telemetryFiles/sample.ibt" ) // Reader is an interface to represent the readable data that can be either @@ -45,7 +44,7 @@ func Init(f Reader) (*IBT, error) { var headerRaw [FileHeaderSize]byte _, err = ibt.File.ReadAt(headerRaw[:], 0) if err != nil { - return nil, fmt.Errorf("Failed to read from file: %v", err) + return nil, fmt.Errorf("Failed to read headers from file: %v", err) } ibt.Headers, err = parseTelemetryHeader(headerRaw) if err != nil { @@ -56,11 +55,11 @@ func Init(f Reader) (*IBT, error) { var subheaderRaw [SubHeaderSize]byte _, err = ibt.File.ReadAt(subheaderRaw[:], 112) if err != nil { - return nil, fmt.Errorf("Failed to read subheader from file: %v", err) + return nil, fmt.Errorf("Failed to read disk subheaders from file: %v", err) } ibt.SubHeaders, err = parseTelemetrySubHeader(subheaderRaw) if err != nil { - return nil, fmt.Errorf("Unable to parse subheaders from file: %v", err) + return nil, fmt.Errorf("Unable to parse disk subheaders from file: %v", err) } // Read session info string @@ -71,11 +70,14 @@ func Init(f Reader) (*IBT, error) { } ibt.SessionInfo, err = parseSessionInfo(sessionInfoStringRaw) if err != nil { - return nil, fmt.Errorf("Unable to parse Session Info from file: %v", err) + return nil, fmt.Errorf("Unable to parse SessionInfoString from file: %v", err) } - // Read the telemetry vars info - ibt.readVariablerHeaders() + // Read the telemetry vars info + err = ibt.readVariablerHeaders() + if err != nil { + return nil, fmt.Errorf("Unable to parser variable headers from file: %v", err) + } return &ibt, nil } @@ -96,19 +98,19 @@ func main() { fmt.Printf("%s\n", ibt.Headers.ToString()) fmt.Printf("%s\n", ibt.SubHeaders.ToString()) - // Display the human readable start date - unixStartDate := time.Unix(ibt.SubHeaders.StartDate, 0) - startDate := unixStartDate.Format("2006/01/02 15:04:05 -0700 MST") + // Display the human readable start date + unixStartDate := time.Unix(ibt.SubHeaders.StartDate, 0) + startDate := unixStartDate.Format("2006/01/02 15:04:05 -0700 MST") fmt.Println("StartDate:", startDate) - // Display the human readable version of start time - unixStartTime := time.Unix(ibt.SubHeaders.StartDate + int64(ibt.SubHeaders.StartTime), 0) - startTime := unixStartTime.Format("2006/01/02 15:04:05 -0700 MST") + // Display the human readable version of start time + unixStartTime := time.Unix(ibt.SubHeaders.StartDate+int64(ibt.SubHeaders.StartTime), 0) + startTime := unixStartTime.Format("2006/01/02 15:04:05 -0700 MST") fmt.Println("StartTime:", startTime) - // Display the human readable version of end time - unixEndTime := time.Unix(ibt.SubHeaders.StartDate + int64(ibt.SubHeaders.EndTime), 0) - endTime := unixEndTime.Format("2006/01/02 15:04:05 -0700 MST") + // Display the human readable version of end time + unixEndTime := time.Unix(ibt.SubHeaders.StartDate+int64(ibt.SubHeaders.EndTime), 0) + endTime := unixEndTime.Format("2006/01/02 15:04:05 -0700 MST") fmt.Println("EndTime: ", endTime) last := time.Now().UnixMilli() @@ -133,5 +135,5 @@ func main() { } } - fmt.Printf("%d\n", ibt.Tick) + fmt.Printf("%d\n", ibt.Tick) } diff --git a/sessionInfo.go b/sessionInfo.go index 12263db..c457af8 100644 --- a/sessionInfo.go +++ b/sessionInfo.go @@ -307,18 +307,17 @@ type Driver struct { // parseSessionInfo will parse the sessionInfo buffer into the SessionInfoYAML // struct func parseSessionInfo(data []byte) (*SessionInfoYAML, error) { - var sessionInfo SessionInfoYAML - err := yaml.Unmarshal(data, &sessionInfo) - if err != nil { - return nil, err - } + var sessionInfo SessionInfoYAML + err := yaml.Unmarshal(data, &sessionInfo) + if err != nil { + return nil, err + } - return &sessionInfo, nil + return &sessionInfo, nil } // ToString will return a readable string of the struct func (s *SessionInfoYAML) ToString() string { - stringified, _ := json.MarshalIndent(s, "", " ") - return string(stringified) + stringified, _ := json.MarshalIndent(s, "", " ") + return string(stringified) } - diff --git a/variables.go b/variables.go index cef6f69..a2ca31f 100644 --- a/variables.go +++ b/variables.go @@ -4,14 +4,14 @@ import ( "bytes" "encoding/binary" "fmt" + "io" "log" + "math" "strings" - "math" - "io" ) const ( - VarSize = 144 + VarHeaderSize = 144 IRSDK_char = 0 IRSDK_bool = 1 IRSDK_int = 2 @@ -88,26 +88,24 @@ type TelemetryVars struct { Vars map[string]Var } -func (i *IBT) readVariablerHeaders() { +func (i *IBT) readVariablerHeaders() error { i.Vars = &TelemetryVars{Vars: make(map[string]Var, i.Headers.NumVars)} var k int32 for k = 0; k < i.Headers.NumVars; k++ { - rbuf := make([]byte, VarSize) + rbuf := make([]byte, VarHeaderSize) - _, err := i.File.ReadAt(rbuf, int64(i.Headers.VarHeaderOffset+k*VarSize)) + _, err := i.File.ReadAt(rbuf, int64(i.Headers.VarHeaderOffset+k*VarHeaderSize)) if err != nil { - log.Fatal(err) + return err } var dst IBTVar err = binary.Read(bytes.NewBuffer(rbuf[:]), binary.LittleEndian, &dst) if err != nil { - log.Fatal(err) + return err } - fmt.Println(dst.Name) - v := Var{ Type: dst.Type, Offset: dst.Offset, @@ -121,13 +119,15 @@ func (i *IBT) readVariablerHeaders() { i.Vars.Vars[v.Name] = v } + + return nil } func (i *IBT) readData() error { - // I think that we can add one extra check or verification here - // The file headers tells us how many data frames there are, we can probably - // cap it at that instead of waiting for the read to fail - // Probably wouldn't work on live data tho + // I think that we can add one extra check or verification here + // The file headers tells us how many data frames there are, we can probably + // cap it at that instead of waiting for the read to fail + // Probably wouldn't work on live data tho start := i.Headers.BufOffset + i.Tick*i.Headers.BufLen buf := make([]byte, i.Headers.BufLen) _, err := i.File.ReadAt(buf, int64(start))