From 8f13b57ae811c04762aa90cbb2738f9815c54679 Mon Sep 17 00:00:00 2001 From: Xpl0itU <24777100+Xpl0itU@users.noreply.github.com> Date: Fri, 8 Sep 2023 17:30:57 +0200 Subject: [PATCH] Gocritic changes --- cmd/WiiUDownloader/main.go | 2 +- cmd/WiiUDownloader/mainwindow.go | 5 ++--- cmd/WiiUDownloader/utils.go | 7 ++++--- downloader.go | 12 ++++++------ hash.go | 5 ++--- keygen.go | 2 +- logger.go | 2 +- 7 files changed, 17 insertions(+), 18 deletions(-) diff --git a/cmd/WiiUDownloader/main.go b/cmd/WiiUDownloader/main.go index 71aa827..279c4fc 100644 --- a/cmd/WiiUDownloader/main.go +++ b/cmd/WiiUDownloader/main.go @@ -25,7 +25,7 @@ func main() { return } - bundlePath := filepath.Join(filepath.Dir(filepath.Dir(execPath))) + bundlePath := filepath.Dir(filepath.Dir(execPath)) filePath := filepath.Join(bundlePath, "Resources", "lib", "share", "glib-schemas") if _, err := os.Stat(filePath); os.IsNotExist(err) { logger.Warning("glib-schemas not found") diff --git a/cmd/WiiUDownloader/mainwindow.go b/cmd/WiiUDownloader/mainwindow.go index 91ae845..e6bf620 100644 --- a/cmd/WiiUDownloader/mainwindow.go +++ b/cmd/WiiUDownloader/mainwindow.go @@ -583,7 +583,7 @@ func (mw *MainWindow) isTitleInQueue(title wiiudownloader.TitleEntry) bool { return false } -func (mw *MainWindow) addToQueue(tid string, name string) { +func (mw *MainWindow) addToQueue(tid, name string) { titleID, err := strconv.ParseUint(tid, 16, 64) if err != nil { mw.logger.Fatal("Unable to parse title ID:", err) @@ -743,7 +743,6 @@ func (mw *MainWindow) onDownloadQueueClicked(selectedPath string) error { defer close(queueStatusChan) errGroup := errgroup.Group{} -queueProcessingLoop: for _, title := range mw.titleQueue { errGroup.Go(func() error { tidStr := fmt.Sprintf("%016x", title.TitleID) @@ -761,7 +760,7 @@ queueProcessingLoop: } if !<-queueStatusChan { - break queueProcessingLoop + break } } diff --git a/cmd/WiiUDownloader/utils.go b/cmd/WiiUDownloader/utils.go index c65cfc3..dc9ea84 100644 --- a/cmd/WiiUDownloader/utils.go +++ b/cmd/WiiUDownloader/utils.go @@ -7,17 +7,18 @@ func normalizeFilename(filename string) string { shouldAppend := true for _, c := range filename { - if c == '_' { + switch { + case c == '_': if shouldAppend { out.WriteRune('_') shouldAppend = false } - } else if c == ' ' { + case c == ' ': if shouldAppend { out.WriteRune(' ') shouldAppend = false } - } else if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') { + case (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'): out.WriteRune(c) shouldAppend = true } diff --git a/downloader.go b/downloader.go index 8e4b93b..234a74e 100644 --- a/downloader.go +++ b/downloader.go @@ -28,9 +28,12 @@ type ProgressReporter interface { Cancelled() bool } -func downloadFile(progressReporter ProgressReporter, client *grab.Client, downloadURL string, dstPath string, doRetries bool) error { +func downloadFile(progressReporter ProgressReporter, client *grab.Client, downloadURL, dstPath string, doRetries bool) error { filePath := filepath.Base(dstPath) + t := time.NewTicker(500 * time.Millisecond) + defer t.Stop() + for attempt := 1; attempt <= maxRetries; attempt++ { req, err := grab.NewRequest(dstPath, downloadURL) if err != nil { @@ -41,9 +44,6 @@ func downloadFile(progressReporter ProgressReporter, client *grab.Client, downlo resp := client.Do(req) progressReporter.UpdateDownloadProgress(resp, filePath) - t := time.NewTicker(500 * time.Millisecond) - defer t.Stop() - Loop: for { select { @@ -69,7 +69,7 @@ func downloadFile(progressReporter ProgressReporter, client *grab.Client, downlo return nil } -func DownloadTitle(titleID string, outputDirectory string, doDecryption bool, progressReporter ProgressReporter, deleteEncryptedContents bool, logger *Logger) error { +func DownloadTitle(titleID, outputDirectory string, doDecryption bool, progressReporter ProgressReporter, deleteEncryptedContents bool, logger *Logger) error { titleEntry := getTitleEntryFromTid(titleID) progressReporter.SetGameTitle(titleEntry.Name) @@ -179,7 +179,7 @@ func DownloadTitle(titleID string, outputDirectory string, doDecryption bool, pr if err := binary.Read(tmdDataReader, binary.BigEndian, &content.Size); err != nil { return err } - if err := checkContentHashes(outputDirectory, content, &cipherHashTree); err != nil { + if err := checkContentHashes(outputDirectory, content, cipherHashTree); err != nil { if progressReporter.Cancelled() { break } diff --git a/hash.go b/hash.go index 08476fc..95f7e6d 100644 --- a/hash.go +++ b/hash.go @@ -13,8 +13,7 @@ import ( var commonKey = []byte{0xD7, 0xB0, 0x04, 0x02, 0x65, 0x9B, 0xA2, 0xAB, 0xD2, 0xCB, 0x0D, 0xB2, 0x7F, 0xA2, 0xB6, 0x56} -func checkContentHashes(path string, content contentInfo, cipherHashTree *cipher.Block) error { - cHashTree := *cipherHashTree +func checkContentHashes(path string, content contentInfo, cipherHashTree cipher.Block) error { h3Data, err := os.ReadFile(filepath.Join(path, fmt.Sprintf("%s.h3", content.ID))) if err != nil { return fmt.Errorf("failed to read H3 hash tree file: %w", err) @@ -42,7 +41,7 @@ func checkContentHashes(path string, content contentInfo, cipherHashTree *cipher iv := make([]byte, aes.BlockSize) for chunkNum := 0; chunkNum < chunkCount; chunkNum++ { encryptedFile.Read(buffer) - cipher.NewCBCDecrypter(cHashTree, iv).CryptBlocks(decryptedContent, buffer) + cipher.NewCBCDecrypter(cipherHashTree, iv).CryptBlocks(decryptedContent, buffer) h0Hashes := decryptedContent[0:0x140] h1Hashes := decryptedContent[0x140:0x280] diff --git a/keygen.go b/keygen.go index 2070d51..e3efe0b 100644 --- a/keygen.go +++ b/keygen.go @@ -17,7 +17,7 @@ var ( keygen_pw = []byte{0x6d, 0x79, 0x70, 0x61, 0x73, 0x73} ) -func encryptAES(data []byte, key []byte, iv []byte) ([]byte, error) { +func encryptAES(data, key, iv []byte) ([]byte, error) { block, err := aes.NewCipher(key) if err != nil { return nil, err diff --git a/logger.go b/logger.go index 6562b1c..aedf204 100644 --- a/logger.go +++ b/logger.go @@ -39,7 +39,7 @@ func NewLogger(logFilePath string) (*Logger, error) { } // Open the log file for writing, truncating it if it exists - logFile, err = os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666) + logFile, err = os.OpenFile(logFilePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o666) if err != nil { // If unable to open the log file, log the error to stdout log.New(os.Stdout, "", log.Ldate|log.Ltime).Printf("[Error] Unable to open log file: %v\n", err)