diff --git a/irsdk.go b/irsdk.go index 97be156..7422bab 100644 --- a/irsdk.go +++ b/irsdk.go @@ -7,8 +7,8 @@ import ( "os" "github.com/ESilva15/goirsdk/logger" + "github.com/ESilva15/goirsdk/mmaputils" "github.com/ESilva15/goirsdk/sharedMem" - "github.com/ESilva15/goirsdk/winutils" "gopkg.in/yaml.v3" ) @@ -52,7 +52,7 @@ type IBT struct { // IBTExportPath string // Path for IBT export // YAMLExport *os.File // If set, it will export the session YAML to the file // YAMLExportPath string // Path for YAML export - winUtils *winutils.IRacingWinUtils // WinUtils gives access to the system utilities + winUtils *mmaputils.IRacingWinUtils // WinUtils gives access to the system utilities // TODO: fragment this struct a little bit, for now I want to actually get // stuff done so its enough to work as is @@ -117,30 +117,31 @@ func (i *IBT) openSource() error { switch i.Opts.SourceType { case SharedMemoryFile: // User is requesting us to read live data - present in the mem map file - i.File, err = winutils.OpenMemMap(IRSDK_MEMMAPFILENAME, fileMapSize) + i.File, err = mmaputils.OpenMemMap(MEMMAPFILENAME, fileMapSize) if err != nil { - return fmt.Errorf("failed to open memory mapped file: %v", err) + return fmt.Errorf("failed to open memory mapped file: %+v", err) } // To use our windows interface we need to initialize it first // it will return a struct with a pointer to the windows handles // if, for some reason, we need to stub out this to run in on Linux its easier - i.winUtils, err = winutils.Init() + i.winUtils, err = mmaputils.Init() if err != nil { return err } + // I don't believe we need this on windows either, but I'll have to check // We need to open the windows event thing - err = i.winUtils.OpenWinEvent(IRSDK_DATAVALIDEVENTNAME) - if err != nil { - return err - } + // err = i.winUtils.OpenWinEvent(IRSDK_DATAVALIDEVENTNAME) + // if err != nil { + // return err + // } // We need to open the broadcast channel - err = i.winUtils.OpenBroadcastChannel(IRSDK_BROADCASTMSGNAME) - if err != nil { - return err - } + // err = i.winUtils.OpenBroadcastChannel(IRSDK_BROADCASTMSGNAME) + // if err != nil { + // return err + // } case IBTFile: i.File, err = os.Open(i.Opts.SourcePath) if err != nil { diff --git a/winutils/winutils.go b/mmaputils/mmaputils.go similarity index 69% rename from winutils/winutils.go rename to mmaputils/mmaputils.go index 6f829fc..d810893 100644 --- a/winutils/winutils.go +++ b/mmaputils/mmaputils.go @@ -2,10 +2,9 @@ // is interface some windows stuff that we need for the: // - Broadcast Channel // - Valid Data Event windows thing -package winutils +package mmaputils import ( - "github.com/ESilva15/goirsdk/sharedMem" "io" "time" ) @@ -32,23 +31,12 @@ func (u *IRacingWinUtils) Close() { u.Utils.Close() } -// OpenMemMap returns a Reader interface that can be used to read the data -// No need to encapsulate it -func OpenMemMap(path string, size uint32) (Reader, error) { - file, err := sharedMem.Open(path, size) - if err != nil { - return nil, err - } - - return file, nil -} - // OpenWinEvent will open the named windows event func (u *IRacingWinUtils) OpenWinEvent(name string) error { return u.Utils.OpenEvent(name) } -// OpenWinEvent will open the broadcast channel +// OpenBroadcastChannel will open the broadcast channel func (u *IRacingWinUtils) OpenBroadcastChannel(name string) error { return u.Utils.OpenBroadcastChannel(name) } diff --git a/winutils/winutils_linux.go b/mmaputils/mmaputils_linux.go similarity index 82% rename from winutils/winutils_linux.go rename to mmaputils/mmaputils_linux.go index ff6002c..e88f4ee 100644 --- a/winutils/winutils_linux.go +++ b/mmaputils/mmaputils_linux.go @@ -1,12 +1,14 @@ //go:build (linux && cgo) || (darwin && cgo) -package winutils +package mmaputils import ( "fmt" "net" "os" "time" + + "github.com/ESilva15/goirsdk/sharedMem" ) type utils struct { @@ -25,6 +27,17 @@ func (u *utils) Close() { } } +// OpenMemMap returns a Reader interface that can be used to read the data +// No need to encapsulate it +func OpenMemMap(name string, size uint32) (Reader, error) { + file, err := sharedMem.Open(name, size) + if err != nil { + return nil, fmt.Errorf("failed to open `%s` with err: %+v", name, err) + } + + return file, nil +} + // OpenEvent creates or connects to a Unix socket for event signaling on Linux func (u *utils) OpenEvent(eventName string) error { u.socketPath = fmt.Sprintf("/tmp/iracing_%s.sock", eventName) diff --git a/winutils/winutils_windows.go b/mmaputils/mmaputils_windows.go similarity index 84% rename from winutils/winutils_windows.go rename to mmaputils/mmaputils_windows.go index 55c12bc..b9c89a7 100644 --- a/winutils/winutils_windows.go +++ b/mmaputils/mmaputils_windows.go @@ -1,12 +1,13 @@ // go:build windows -package winutils +package mmaputils import ( "sync" "time" "unsafe" + "github.com/ESilva15/goirsdk/sharedMem" "golang.org/x/sys/windows" ) @@ -36,6 +37,17 @@ func (u *utils) Close() { // Do we need to close the broadcast channel ??? } +// OpenMemMap returns a Reader interface that can be used to read the data +// No need to encapsulate it +func OpenMemMap(name string, size uint32) (Reader, error) { + file, err := sharedMem.Open("Local\\"+name, size) + if err != nil { + return nil, err + } + + return file, nil +} + // openEvent opens a windows.Handle for a given event func (u *utils) OpenEvent(eventName string) error { name, err := windows.UTF16PtrFromString(eventName)