diff --git a/certificate.go b/certificate.go index f682acd..ae9d1ac 100644 --- a/certificate.go +++ b/certificate.go @@ -2,7 +2,6 @@ package wiiudownloader import ( "bytes" - "context" "fmt" "net/http" "os" @@ -28,12 +27,12 @@ func getCert(tmdData []byte, id int, numContents uint16) ([]byte, error) { } } -func getDefaultCert(cancelCtx context.Context, progressReporter ProgressReporter, client *http.Client) ([]byte, error) { +func getDefaultCert(progressReporter ProgressReporter, client *http.Client) ([]byte, error) { if len(cetkData) >= 0x350+0x300 { return cetkData[0x350 : 0x350+0x300], nil } cetkDir := path.Join(os.TempDir(), "cetk") - if err := downloadFile(cancelCtx, progressReporter, client, "http://ccs.cdn.c.shop.nintendowifi.net/ccs/download/000500101000400a/cetk", cetkDir, true); err != nil { + if err := downloadFile(progressReporter, client, "http://ccs.cdn.c.shop.nintendowifi.net/ccs/download/000500101000400a/cetk", cetkDir, true); err != nil { return nil, err } cetkData, err := os.ReadFile(cetkDir) @@ -51,7 +50,7 @@ func getDefaultCert(cancelCtx context.Context, progressReporter ProgressReporter return nil, fmt.Errorf("failed to download OSv10 cetk, length: %d", len(cetkData)) } -func GenerateCert(tmdData []byte, contentCount uint16, progressReporter ProgressReporter, client *http.Client, cancelCtx context.Context) (bytes.Buffer, error) { +func GenerateCert(tmdData []byte, contentCount uint16, progressReporter ProgressReporter, client *http.Client) (bytes.Buffer, error) { cert := bytes.Buffer{} cert0, err := getCert(tmdData, 0, contentCount) @@ -66,7 +65,7 @@ func GenerateCert(tmdData []byte, contentCount uint16, progressReporter Progress } cert.Write(cert1) - defaultCert, err := getDefaultCert(cancelCtx, progressReporter, client) + defaultCert, err := getDefaultCert(progressReporter, client) if err != nil { return bytes.Buffer{}, err } diff --git a/cmd/WiiUDownloader/mainwindow.go b/cmd/WiiUDownloader/mainwindow.go index effa2e3..f166a00 100644 --- a/cmd/WiiUDownloader/mainwindow.go +++ b/cmd/WiiUDownloader/mainwindow.go @@ -265,7 +265,7 @@ func (mw *MainWindow) ShowAll() { wiiudownloader.GenerateTicket(filepath.Join(parentDir, "title.tik"), titleID, titleKey, titleVersion) - cert, err := wiiudownloader.GenerateCert(tmdData, contentCount, mw.progressWindow, http.DefaultClient, context.Background()) + cert, err := wiiudownloader.GenerateCert(tmdData, contentCount, mw.progressWindow, http.DefaultClient) if err != nil { return } @@ -746,21 +746,15 @@ func (mw *MainWindow) onDownloadQueueClicked(selectedPath string) error { defer close(queueStatusChan) errGroup := errgroup.Group{} - queueCtx, cancel := context.WithCancel(context.Background()) - mw.progressWindow.cancelFunc = cancel - defer mw.progressWindow.cancelFunc() - for _, title := range mw.titleQueue { errGroup.Go(func() error { - select { - case <-queueCtx.Done(): + if mw.progressWindow.cancelled { queueStatusChan <- true return nil - default: } 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(queueCtx, tidStr, titlePath, mw.decryptContents, mw.progressWindow, mw.getDeleteEncryptedContents(), mw.logger, mw.client); err != nil && err != context.Canceled { + if err := wiiudownloader.DownloadTitle(tidStr, titlePath, mw.decryptContents, mw.progressWindow, mw.getDeleteEncryptedContents(), mw.logger, mw.client); err != nil && err != context.Canceled { return err } diff --git a/downloader.go b/downloader.go index 11b02d9..e6b4014 100644 --- a/downloader.go +++ b/downloader.go @@ -2,7 +2,6 @@ package wiiudownloader import ( "bytes" - "context" "encoding/binary" "fmt" "io" @@ -29,7 +28,7 @@ type ProgressReporter interface { AddToTotalDownloaded(toAdd int64) } -func downloadFile(ctx context.Context, progressReporter ProgressReporter, client *http.Client, downloadURL, dstPath string, doRetries bool) error { +func downloadFile(progressReporter ProgressReporter, client *http.Client, downloadURL, dstPath string, doRetries bool) error { filePath := filepath.Base(dstPath) for attempt := 1; attempt <= maxRetries; attempt++ { @@ -39,7 +38,6 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client } req.Header.Set("User-Agent", "WiiUDownloader") - req = req.WithContext(ctx) resp, err := client.Do(req) if err != nil { @@ -84,7 +82,7 @@ func downloadFile(ctx context.Context, progressReporter ProgressReporter, client return nil } -func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, doDecryption bool, progressReporter ProgressReporter, deleteEncryptedContents bool, logger *Logger, client *http.Client) error { +func DownloadTitle(titleID, outputDirectory string, doDecryption bool, progressReporter ProgressReporter, deleteEncryptedContents bool, logger *Logger, client *http.Client) error { tEntry := getTitleEntryFromTid(titleID) progressReporter.SetTotalDownloaded(0) @@ -98,7 +96,7 @@ func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, d } tmdPath := filepath.Join(outputDir, "title.tmd") - if err := downloadFile(cancelCtx, progressReporter, client, fmt.Sprintf("%s/%s", baseURL, "tmd"), tmdPath, true); err != nil { + if err := downloadFile(progressReporter, client, fmt.Sprintf("%s/%s", baseURL, "tmd"), tmdPath, true); err != nil { if progressReporter.Cancelled() { return nil } @@ -116,7 +114,7 @@ func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, d } tikPath := filepath.Join(outputDir, "title.tik") - if err := downloadFile(cancelCtx, progressReporter, client, fmt.Sprintf("%s/%s", baseURL, "cetk"), tikPath, false); err != nil { + if err := downloadFile(progressReporter, client, fmt.Sprintf("%s/%s", baseURL, "cetk"), tikPath, false); err != nil { if progressReporter.Cancelled() { return nil } @@ -150,7 +148,7 @@ func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, d progressReporter.SetDownloadSize(int64(titleSize)) - cert, err := GenerateCert(tmdData, contentCount, progressReporter, client, cancelCtx) + cert, err := GenerateCert(tmdData, contentCount, progressReporter, client) if err != nil { if progressReporter.Cancelled() { return nil @@ -179,7 +177,7 @@ func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, d return err } filePath := filepath.Join(outputDir, fmt.Sprintf("%08X.app", content.ID)) - if err := downloadFile(cancelCtx, progressReporter, client, fmt.Sprintf("%s/%08X", baseURL, content.ID), filePath, true); err != nil { + if err := downloadFile(progressReporter, client, fmt.Sprintf("%s/%08X", baseURL, content.ID), filePath, true); err != nil { if progressReporter.Cancelled() { break } @@ -189,7 +187,7 @@ func DownloadTitle(cancelCtx context.Context, titleID, outputDirectory string, d if tmdData[offset+7]&0x2 == 2 { filePath = filepath.Join(outputDir, fmt.Sprintf("%08X.h3", content.ID)) - if err := downloadFile(cancelCtx, progressReporter, client, fmt.Sprintf("%s/%08X.h3", baseURL, content.ID), filePath, true); err != nil { + if err := downloadFile(progressReporter, client, fmt.Sprintf("%s/%08X.h3", baseURL, content.ID), filePath, true); err != nil { if progressReporter.Cancelled() { break }