Make backend and frontend independent

This commit is contained in:
Xpl0itU 2023-08-29 20:55:43 +02:00
parent 08505c9eec
commit 99597834ce
8 changed files with 148 additions and 123 deletions

View file

@ -21,7 +21,7 @@ jobs:
- name: Build artifacts
run: |
docker run --rm -v ${PWD}:/project builder python3 grabTitles.py
docker run --rm -v ${PWD}:/project builder go build cmd/WiiUDownloader/main.go cmd/WiiUDownloader/darkMode.go cmd/WiiUDownloader/mainwindow.go cmd/WiiUDownloader/utils.go
docker run --rm -v ${PWD}:/project builder go build cmd/WiiUDownloader/main.go cmd/WiiUDownloader/darkMode.go cmd/WiiUDownloader/mainwindow.go cmd/WiiUDownloader/progressWindow.go cmd/WiiUDownloader/utils.go
- name: Deploy WiiUDownloader
run: |
mv main WiiUDownloader

View file

@ -21,7 +21,7 @@ jobs:
- name: Build
run: |
python3 grabTitles.py
go build cmd/WiiUDownloader/main.go cmd/WiiUDownloader/darkMode.go cmd/WiiUDownloader/mainwindow.go cmd/WiiUDownloader/utils.go
go build cmd/WiiUDownloader/main.go cmd/WiiUDownloader/darkMode.go cmd/WiiUDownloader/mainwindow.go cmd/WiiUDownloader/progressWindow.go cmd/WiiUDownloader/utils.go
- name: Package
run: |
python3 data/create_bundle.py

View file

@ -29,7 +29,7 @@ jobs:
- name: Build
run: |
python3 grabTitles.py
go build -ldflags -H=windowsgui cmd/WiiUDownloader/main.go cmd/WiiUDownloader/darkMode.go cmd/WiiUDownloader/mainwindow.go cmd/WiiUDownloader/utils.go
go build -ldflags -H=windowsgui cmd/WiiUDownloader/main.go cmd/WiiUDownloader/darkMode.go cmd/WiiUDownloader/mainwindow.go cmd/WiiUDownloader/progressWindow.go cmd/WiiUDownloader/utils.go
- name: Deploy WiiUDownloader
run: |
mkdir dist

View file

@ -28,12 +28,12 @@ func getCert(tmdData []byte, id int, numContents uint16) ([]byte, error) {
}
}
func getDefaultCert(progressWindow *ProgressWindow, client *grab.Client) ([]byte, error) {
func getDefaultCert(progressReporter ProgressReporter, client *grab.Client) ([]byte, error) {
if len(cetkData) >= 0x350+0x300 {
return cetkData[0x350 : 0x350+0x300], nil
}
cetkDir := path.Join(os.TempDir(), "cetk")
if err := downloadFile(progressWindow, 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 +51,7 @@ func getDefaultCert(progressWindow *ProgressWindow, client *grab.Client) ([]byte
return nil, fmt.Errorf("failed to download OSv10 cetk, length: %d", len(cetkData))
}
func GenerateCert(tmdData []byte, contentCount uint16, progressWindow *ProgressWindow, client *grab.Client) (bytes.Buffer, error) {
func GenerateCert(tmdData []byte, contentCount uint16, progressReporter ProgressReporter, client *grab.Client) (bytes.Buffer, error) {
cert := bytes.Buffer{}
cert0, err := getCert(tmdData, 0, contentCount)
@ -66,7 +66,7 @@ func GenerateCert(tmdData []byte, contentCount uint16, progressWindow *ProgressW
}
cert.Write(cert1)
defaultCert, err := getDefaultCert(progressWindow, client)
defaultCert, err := getDefaultCert(progressReporter, client)
if err != nil {
return bytes.Buffer{}, err
}

View file

@ -32,7 +32,7 @@ type MainWindow struct {
searchEntry *gtk.Entry
deleteEncryptedContentsCheckbox *gtk.CheckButton
addToQueueButton *gtk.Button
progressWindow wiiudownloader.ProgressWindow
progressWindow *ProgressWindow
lastSearchText string
titleQueue []wiiudownloader.TitleEntry
categoryButtons []*gtk.ToggleButton
@ -200,7 +200,7 @@ func (mw *MainWindow) ShowAll() {
mw.logger.Fatal("Unable to create menu item:", err)
}
decryptContentsMenuItem.Connect("activate", func() {
mw.progressWindow, err = wiiudownloader.CreateProgressWindow(mw.window)
mw.progressWindow, err = createProgressWindow(mw.window)
if err != nil {
return
}
@ -260,7 +260,7 @@ func (mw *MainWindow) ShowAll() {
wiiudownloader.GenerateTicket(filepath.Join(parentDir, "title.tik"), titleID, titleKey, titleVersion)
cert, err := wiiudownloader.GenerateCert(tmdData, contentCount, &mw.progressWindow, grab.NewClient())
cert, err := wiiudownloader.GenerateCert(tmdData, contentCount, mw.progressWindow, grab.NewClient())
if err != nil {
return
}
@ -347,7 +347,7 @@ func (mw *MainWindow) ShowAll() {
if len(mw.titleQueue) == 0 {
return
}
mw.progressWindow, err = wiiudownloader.CreateProgressWindow(mw.window)
mw.progressWindow, err = createProgressWindow(mw.window)
if err != nil {
return
}
@ -483,7 +483,7 @@ func (mw *MainWindow) onCategoryToggled(button *gtk.ToggleButton) {
}
func (mw *MainWindow) onDecryptContentsMenuItemClicked(selectedPath string) error {
err := wiiudownloader.DecryptContents(selectedPath, &mw.progressWindow, false)
err := wiiudownloader.DecryptContents(selectedPath, mw.progressWindow, false)
glib.IdleAdd(func() {
if mw.progressWindow.Window.IsVisible() {
@ -740,7 +740,7 @@ queueProcessingLoop:
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, &mw.progressWindow, mw.getDeleteEncryptedContents(), mw.logger); err != nil {
if err := wiiudownloader.DownloadTitle(tidStr, titlePath, mw.decryptContents, mw.progressWindow, mw.getDeleteEncryptedContents(), mw.logger); err != nil {
return err
}

View file

@ -0,0 +1,112 @@
package main
import (
"fmt"
"github.com/cavaliergopher/grab/v3"
"github.com/dustin/go-humanize"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
type ProgressWindow struct {
Window *gtk.Window
box *gtk.Box
gameLabel *gtk.Label
bar *gtk.ProgressBar
cancelButton *gtk.Button
cancelled bool
}
func (pw *ProgressWindow) SetGameTitle(title string) {
glib.IdleAdd(func() {
pw.gameLabel.SetText(title)
})
for gtk.EventsPending() {
gtk.MainIteration()
}
}
func (pw *ProgressWindow) UpdateDownloadProgress(resp *grab.Response, filePath string) {
glib.IdleAdd(func() {
pw.bar.SetFraction(resp.Progress())
pw.bar.SetText(fmt.Sprintf("Downloading %s (%s/%s) (%s/s)", filePath, humanize.Bytes(uint64(resp.BytesComplete())), humanize.Bytes(uint64(resp.Size())), humanize.Bytes(uint64(resp.BytesPerSecond()))))
})
for gtk.EventsPending() {
gtk.MainIteration()
}
}
func (pw *ProgressWindow) UpdateDecryptionProgress(progress float64) {
glib.IdleAdd(func() {
pw.bar.SetFraction(progress)
pw.bar.SetText(fmt.Sprintf("Decrypting (%.2f%%)", progress*100))
})
for gtk.EventsPending() {
gtk.MainIteration()
}
}
func (pw *ProgressWindow) Cancelled() bool {
return pw.cancelled
}
func createProgressWindow(parent *gtk.ApplicationWindow) (*ProgressWindow, error) {
win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
if err != nil {
return nil, err
}
win.SetTitle("WiiUDownloader - Downloading")
win.SetTransientFor(parent)
box, err := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 5)
if err != nil {
return nil, err
}
win.Add(box)
gameLabel, err := gtk.LabelNew("")
if err != nil {
return nil, err
}
box.PackStart(gameLabel, false, false, 0)
progressBar, err := gtk.ProgressBarNew()
if err != nil {
return nil, err
}
progressBar.SetShowText(true)
box.PackStart(progressBar, false, false, 0)
cancelButton, err := gtk.ButtonNewWithLabel("Cancel")
if err != nil {
return nil, err
}
bottomhBox, err := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 5)
if err != nil {
return nil, err
}
bottomhBox.PackEnd(cancelButton, false, false, 0)
box.SetMarginBottom(5)
box.SetMarginEnd(5)
box.SetMarginStart(5)
box.SetMarginTop(5)
box.PackEnd(bottomhBox, false, false, 0)
progressWindow := ProgressWindow{
Window: win,
box: box,
gameLabel: gameLabel,
bar: progressBar,
cancelButton: cancelButton,
cancelled: false,
}
progressWindow.cancelButton.Connect("clicked", func() {
progressWindow.cancelled = true
})
return &progressWindow, nil
}

View file

@ -18,7 +18,6 @@ import (
"time"
"unsafe"
"github.com/gotk3/gotk3/glib"
"golang.org/x/sync/errgroup"
)
@ -29,7 +28,7 @@ func callProgressCallback(progress C.int) {
var progressChan chan int
func DecryptContents(path string, progress *ProgressWindow, deleteEncryptedContents bool) error {
func DecryptContents(path string, progress ProgressReporter, deleteEncryptedContents bool) error {
progressChan = make(chan int)
errGroup := errgroup.Group{}
@ -38,16 +37,9 @@ func DecryptContents(path string, progress *ProgressWindow, deleteEncryptedConte
return runDecryption(path, deleteEncryptedContents)
})
glib.IdleAdd(func() {
progress.bar.SetText("Decrypting...")
})
for progressInt := range progressChan {
if progressInt > 0 {
glib.IdleAdd(func() {
progress.bar.SetFraction(float64(progressInt) / 100)
})
forceUpdateUI()
progress.UpdateDecryptionProgress(float64(progressInt) / 100)
}
time.Sleep(time.Millisecond * 10)
}

View file

@ -13,9 +13,6 @@ import (
"time"
"github.com/cavaliergopher/grab/v3"
"github.com/dustin/go-humanize"
"github.com/gotk3/gotk3/glib"
"github.com/gotk3/gotk3/gtk"
)
const (
@ -24,70 +21,14 @@ const (
bufferSize = 1048576
)
type ProgressWindow struct {
Window *gtk.Window
box *gtk.Box
gameLabel *gtk.Label
bar *gtk.ProgressBar
cancelButton *gtk.Button
cancelled bool
type ProgressReporter interface {
SetGameTitle(title string)
UpdateDownloadProgress(resp *grab.Response, filePath string)
UpdateDecryptionProgress(progress float64)
Cancelled() bool
}
func CreateProgressWindow(parent *gtk.ApplicationWindow) (ProgressWindow, error) {
win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
if err != nil {
return ProgressWindow{}, err
}
win.SetTitle("WiiUDownloader - Downloading")
win.SetTransientFor(parent)
box, err := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 5)
if err != nil {
return ProgressWindow{}, err
}
win.Add(box)
gameLabel, err := gtk.LabelNew("")
if err != nil {
return ProgressWindow{}, err
}
box.PackStart(gameLabel, false, false, 0)
progressBar, err := gtk.ProgressBarNew()
if err != nil {
return ProgressWindow{}, err
}
progressBar.SetShowText(true)
box.PackStart(progressBar, false, false, 0)
cancelButton, err := gtk.ButtonNewWithLabel("Cancel")
if err != nil {
return ProgressWindow{}, err
}
bottomhBox, err := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 5)
if err != nil {
return ProgressWindow{}, err
}
bottomhBox.PackEnd(cancelButton, false, false, 0)
box.SetMarginBottom(5)
box.SetMarginEnd(5)
box.SetMarginStart(5)
box.SetMarginTop(5)
box.PackEnd(bottomhBox, false, false, 0)
return ProgressWindow{
Window: win,
box: box,
gameLabel: gameLabel,
bar: progressBar,
cancelButton: cancelButton,
cancelled: false,
}, nil
}
func downloadFile(progressWindow *ProgressWindow, client *grab.Client, downloadURL string, dstPath string, doRetries bool) error {
func downloadFile(progressWindow ProgressReporter, client *grab.Client, downloadURL string, dstPath string, doRetries bool) error {
filePath := filepath.Base(dstPath)
for attempt := 1; attempt <= maxRetries; attempt++ {
@ -98,7 +39,7 @@ func downloadFile(progressWindow *ProgressWindow, client *grab.Client, downloadU
req.BufferSize = bufferSize
resp := client.Do(req)
updateProgressWindow(progressWindow, resp, filePath)
progressWindow.UpdateDownloadProgress(resp, filePath)
t := time.NewTicker(500 * time.Millisecond)
defer t.Stop()
@ -107,13 +48,12 @@ func downloadFile(progressWindow *ProgressWindow, client *grab.Client, downloadU
for {
select {
case <-t.C:
updateProgressWindow(progressWindow, resp, filePath)
if progressWindow.cancelled {
progressWindow.UpdateDownloadProgress(resp, filePath)
if progressWindow.Cancelled() {
resp.Cancel()
break Loop
}
case <-resp.Done:
forceUpdateUI()
if err := resp.Err(); err != nil {
if doRetries && attempt < maxRetries {
time.Sleep(retryDelay)
@ -129,30 +69,11 @@ func downloadFile(progressWindow *ProgressWindow, client *grab.Client, downloadU
return nil
}
func forceUpdateUI() {
for gtk.EventsPending() {
gtk.MainIteration()
}
}
func updateProgressWindow(progressWindow *ProgressWindow, resp *grab.Response, filePath string) {
glib.IdleAdd(func() {
progressWindow.bar.SetFraction(resp.Progress())
progressWindow.bar.SetText(fmt.Sprintf("Downloading %s (%s/%s) (%s/s)", filePath, humanize.Bytes(uint64(resp.BytesComplete())), humanize.Bytes(uint64(resp.Size())), humanize.Bytes(uint64(resp.BytesPerSecond()))))
})
forceUpdateUI()
}
func DownloadTitle(titleID string, outputDirectory string, doDecryption bool, progressWindow *ProgressWindow, deleteEncryptedContents bool, logger *Logger) error {
progressWindow.cancelButton.Connect("clicked", func() {
progressWindow.cancelled = true
})
func DownloadTitle(titleID string, outputDirectory string, doDecryption bool, progressReporter ProgressReporter, deleteEncryptedContents bool, logger *Logger) error {
titleEntry := getTitleEntryFromTid(titleID)
glib.IdleAdd(func() {
progressWindow.gameLabel.SetText(titleEntry.Name)
})
progressReporter.SetGameTitle(titleEntry.Name)
outputDir := strings.TrimRight(outputDirectory, "/\\")
baseURL := fmt.Sprintf("http://ccs.cdn.c.shop.nintendowifi.net/ccs/download/%s", titleID)
titleIDBytes, err := hex.DecodeString(titleID)
@ -167,7 +88,7 @@ func DownloadTitle(titleID string, outputDirectory string, doDecryption bool, pr
client := grab.NewClient()
client.BufferSize = bufferSize
tmdPath := filepath.Join(outputDir, "title.tmd")
if err := downloadFile(progressWindow, 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 {
return err
}
@ -182,7 +103,7 @@ func DownloadTitle(titleID string, outputDirectory string, doDecryption bool, pr
}
tikPath := filepath.Join(outputDir, "title.tik")
if err := downloadFile(progressWindow, 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 {
titleKey, err := GenerateKey(titleID)
if err != nil {
return err
@ -202,7 +123,7 @@ func DownloadTitle(titleID string, outputDirectory string, doDecryption bool, pr
return err
}
cert, err := GenerateCert(tmdData, contentCount, progressWindow, client)
cert, err := GenerateCert(tmdData, contentCount, progressReporter, client)
if err != nil {
return err
}
@ -243,13 +164,13 @@ func DownloadTitle(titleID string, outputDirectory string, doDecryption bool, pr
return err
}
filePath := filepath.Join(outputDir, fmt.Sprintf("%08X.app", id))
if err := downloadFile(progressWindow, client, fmt.Sprintf("%s/%08X", baseURL, id), filePath, true); err != nil {
if err := downloadFile(progressReporter, client, fmt.Sprintf("%s/%08X", baseURL, id), filePath, true); err != nil {
return err
}
if tmdData[offset+7]&0x2 == 2 {
filePath = filepath.Join(outputDir, fmt.Sprintf("%08X.h3", id))
if err := downloadFile(progressWindow, client, fmt.Sprintf("%s/%08X.h3", baseURL, id), filePath, true); err != nil {
if err := downloadFile(progressReporter, client, fmt.Sprintf("%s/%08X.h3", baseURL, id), filePath, true); err != nil {
return err
}
content.Hash = tmdData[offset+16 : offset+0x14]
@ -259,19 +180,19 @@ func DownloadTitle(titleID string, outputDirectory string, doDecryption bool, pr
return err
}
if err := checkContentHashes(outputDirectory, content, &cipherHashTree); err != nil {
if progressWindow.cancelled {
if progressReporter.Cancelled() {
break
}
return err
}
}
if progressWindow.cancelled {
if progressReporter.Cancelled() {
break
}
}
if doDecryption && !progressWindow.cancelled {
if err := DecryptContents(outputDir, progressWindow, deleteEncryptedContents); err != nil {
if doDecryption && !progressReporter.Cancelled() {
if err := DecryptContents(outputDir, progressReporter, deleteEncryptedContents); err != nil {
return err
}
}