diff --git a/cmd/WiiUDownloader/mainwindow.go b/cmd/WiiUDownloader/mainwindow.go index 08c5ace..4751ddd 100644 --- a/cmd/WiiUDownloader/mainwindow.go +++ b/cmd/WiiUDownloader/mainwindow.go @@ -8,13 +8,13 @@ import ( "path/filepath" "strconv" "strings" - "sync" wiiudownloader "github.com/Xpl0itU/WiiUDownloader" "github.com/Xpl0itU/dialog" "github.com/cavaliergopher/grab/v3" "github.com/gotk3/gotk3/glib" "github.com/gotk3/gotk3/gtk" + "golang.org/x/sync/errgroup" ) const ( @@ -736,46 +736,36 @@ func (mw *MainWindow) onDownloadQueueClicked(selectedPath string) error { } queueStatusChan := make(chan bool, 1) - errorChan := make(chan error, 1) - - var wg sync.WaitGroup + defer close(queueStatusChan) + errGroup := errgroup.Group{} queueProcessingLoop: for _, title := range mw.titleQueue { - wg.Add(1) - - go func(title wiiudownloader.TitleEntry, selectedPath string, progressWindow *wiiudownloader.ProgressWindow) { - defer wg.Done() - + errGroup.Go(func() error { tidStr := fmt.Sprintf("%016x", title.TitleID) titlePath := filepath.Join(selectedPath, fmt.Sprintf("%s [%s] [%s]", normalizeFilename(title.Name), wiiudownloader.GetFormattedKind(title.TitleID), tidStr)) - if err := wiiudownloader.DownloadTitle(tidStr, titlePath, mw.decryptContents, progressWindow, mw.getDeleteEncryptedContents(), mw.logger); err != nil { - errorChan <- err - return + if err := wiiudownloader.DownloadTitle(tidStr, titlePath, mw.decryptContents, &mw.progressWindow, mw.getDeleteEncryptedContents(), mw.logger); err != nil { + return err } queueStatusChan <- true - }(title, selectedPath, &mw.progressWindow) + return nil + }) - select { - case err := <-errorChan: + if err := errGroup.Wait(); err != nil { queueStatusChan <- false - wg.Wait() mw.titleQueue = []wiiudownloader.TitleEntry{} if mw.progressWindow.Window.IsVisible() { mw.progressWindow.Window.Close() } mw.updateTitlesInQueue() mw.onSelectionChanged() - close(queueStatusChan) - close(errorChan) return err + } - case queueStatus := <-queueStatusChan: - if !queueStatus { - wg.Wait() - break queueProcessingLoop - } + queueStatus := <-queueStatusChan + if !queueStatus { + break queueProcessingLoop } } @@ -788,8 +778,5 @@ queueProcessingLoop: mw.updateTitlesInQueue() mw.onSelectionChanged() - close(queueStatusChan) - close(errorChan) - return nil } diff --git a/decryption.go b/decryption.go index f44977e..3bdbd9e 100644 --- a/decryption.go +++ b/decryption.go @@ -20,6 +20,7 @@ import ( "github.com/gotk3/gotk3/glib" "github.com/gotk3/gotk3/gtk" + "golang.org/x/sync/errgroup" ) //export callProgressCallback @@ -30,11 +31,13 @@ func callProgressCallback(progress C.int) { var progressChan chan int func DecryptContents(path string, progress *ProgressWindow, deleteEncryptedContents bool) error { - errorChan := make(chan error) progressChan = make(chan int) - defer close(errorChan) - go runDecryption(path, errorChan, deleteEncryptedContents) + errGroup := errgroup.Group{} + + errGroup.Go(func() error { + return runDecryption(path, deleteEncryptedContents) + }) glib.IdleAdd(func() { progress.bar.SetText("Decrypting...") @@ -52,10 +55,10 @@ func DecryptContents(path string, progress *ProgressWindow, deleteEncryptedConte time.Sleep(time.Millisecond * 10) } - return <-errorChan + return errGroup.Wait() } -func runDecryption(path string, errorChan chan<- error, deleteEncryptedContents bool) { +func runDecryption(path string, deleteEncryptedContents bool) error { defer close(progressChan) argv := make([]*C.char, 2) argv[0] = C.CString("WiiUDownloader") @@ -67,13 +70,12 @@ func runDecryption(path string, errorChan chan<- error, deleteEncryptedContents C.set_progress_callback(C.ProgressCallback(C.callProgressCallback)) if int(C.cdecrypt_main(2, (**C.char)(unsafe.Pointer(&argv[0])))) != 0 { - errorChan <- fmt.Errorf("decryption failed") - return + return fmt.Errorf("decryption failed") } if deleteEncryptedContents { doDeleteEncryptedContents(path) } - errorChan <- nil + return nil } diff --git a/go.mod b/go.mod index 06726bc..831ab98 100644 --- a/go.mod +++ b/go.mod @@ -10,4 +10,7 @@ require ( golang.org/x/crypto v0.12.0 ) -require github.com/TheTitanrain/w32 v0.0.0-20200114052255-2654d97dbd3d // indirect +require ( + github.com/TheTitanrain/w32 v0.0.0-20200114052255-2654d97dbd3d // indirect + golang.org/x/sync v0.3.0 // indirect +) diff --git a/go.sum b/go.sum index 41feb1a..2b2dfe1 100644 --- a/go.sum +++ b/go.sum @@ -10,3 +10,5 @@ github.com/gotk3/gotk3 v0.6.2 h1:sx/PjaKfKULJPTPq8p2kn2ZbcNFxpOJqi4VLzMbEOO8= github.com/gotk3/gotk3 v0.6.2/go.mod h1:/hqFpkNa9T3JgNAE2fLvCdov7c5bw//FHNZrZ3Uv9/Q= golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk= golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=