diff --git a/irsdk/api.go b/irsdk/api.go index 1e6127a..c4476c1 100644 --- a/irsdk/api.go +++ b/irsdk/api.go @@ -11,6 +11,7 @@ import ( "net/http" "os" "reflect" + "regexp" "strings" "syscall" "time" @@ -112,11 +113,9 @@ func (irsdk *Irsdk) WaitForValidData() bool { // //nolint:lll //by design func (irsdk *Irsdk) GetData() bool { - start := time.Now() ret := irsdk.WaitForValidData() - delta := time.Since(start) - if ret || delta > 0 { - // dummy + if !ret { + return false } latestBuf := irsdk.hdr.VarBufs[0] @@ -140,9 +139,15 @@ func (irsdk *Irsdk) GetData() bool { } func (irsdk *Irsdk) GetYaml() (*yaml.IrsdkYaml, error) { - err := goyaml.Unmarshal([]byte(irsdk.GetYamlString()), &irsdk.irsdkYaml) + yamlData := irsdk.GetYamlString() + err := goyaml.Unmarshal([]byte(yamlData), &irsdk.irsdkYaml) if err != nil { - return nil, err + // maybe the yaml is just not valid (see issue #6) + // let's try to fix it and try again + err := goyaml.Unmarshal([]byte(fixYaml(yamlData)), &irsdk.irsdkYaml) + if err != nil { + return nil, err + } } return &irsdk.irsdkYaml, nil } @@ -489,3 +494,13 @@ func isZeroed(buf []byte) bool { } return true } + +// fixYaml replaces the yaml team and user name with a quoted string +func fixYaml(s string) string { + work := s + for _, key := range []string{"TeamName", "UserName"} { + re := regexp.MustCompile(fmt.Sprintf("%s: (.*)", key)) + work = re.ReplaceAllString(work, fmt.Sprintf("%s: \"$1\"", key)) + } + return work +} diff --git a/irsdk/api_test.go b/irsdk/api_test.go new file mode 100644 index 0000000..56ebd58 --- /dev/null +++ b/irsdk/api_test.go @@ -0,0 +1,50 @@ +package irsdk + +import ( + "testing" +) + +func Test_fixYaml(t *testing.T) { + type args struct { + s string + } + tests := []struct { + name string + args args + want string + }{ + // TODO: Add test cases. + { + "standard", + args{` + DriverInfo: + DriverCarIdx: 0 + Drivers: + - CarIdx: 0 + UserName: John Doe + TeamName: @Invalid Yaml team name + - CarIdx: 1 + UserName: Jane Doe + TeamName: Valid Yaml team name + `}, + ` + DriverInfo: + DriverCarIdx: 0 + Drivers: + - CarIdx: 0 + UserName: "John Doe" + TeamName: "@Invalid Yaml team name" + - CarIdx: 1 + UserName: "Jane Doe" + TeamName: "Valid Yaml team name" + `, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := fixYaml(tt.args.s); got != tt.want { + t.Errorf("fixYaml() = %v, want %v", got, tt.want) + } + }) + } +}