mirror of
https://github.com/Xpl0itU/WiiUDownloader.git
synced 2025-05-12 22:26:15 -04:00
78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
package wiiudownloader
|
|
|
|
/*
|
|
#cgo CFLAGS: -I${SRCDIR}/cdecrypt
|
|
#cgo LDFLAGS: -Wl,-rpath,${SRCDIR}
|
|
#cgo LDFLAGS: -L${SRCDIR}
|
|
#cgo LDFLAGS: -lcdecrypt
|
|
#include <cdecrypt.h>
|
|
#include <ctype.h>
|
|
#include <stdlib.h>
|
|
|
|
// Declare a separate C function that calls the Go function progressCallback
|
|
extern void callProgressCallback(int progress);
|
|
*/
|
|
import "C"
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
"unsafe"
|
|
|
|
"github.com/gotk3/gotk3/gtk"
|
|
)
|
|
|
|
//export callProgressCallback
|
|
func callProgressCallback(progress C.int) {
|
|
progressChan <- int(progress)
|
|
}
|
|
|
|
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)
|
|
|
|
progress.bar.SetText("Decrypting...")
|
|
|
|
for progressInt := range progressChan {
|
|
if progressInt > 0 {
|
|
progress.bar.SetFraction(float64(progressInt) / 100)
|
|
for gtk.EventsPending() {
|
|
gtk.MainIteration()
|
|
}
|
|
}
|
|
time.Sleep(time.Millisecond * 10)
|
|
}
|
|
|
|
if err := <-errorChan; err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func runDecryption(path string, errorChan chan<- error, deleteEncryptedContents bool) {
|
|
argv := make([]*C.char, 2)
|
|
argv[0] = C.CString("WiiUDownloader")
|
|
argv[1] = C.CString(path)
|
|
defer C.free(unsafe.Pointer(argv[0]))
|
|
defer C.free(unsafe.Pointer(argv[1]))
|
|
|
|
// Register the C callback function with C
|
|
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
|
|
}
|
|
|
|
if deleteEncryptedContents {
|
|
doDeleteEncryptedContents(path)
|
|
}
|
|
|
|
close(progressChan) // Indicate the completion of the decryption process
|
|
errorChan <- nil
|
|
}
|