mirror of
https://github.com/Xpl0itU/WiiUDownloader.git
synced 2025-05-23 11:37:07 -04:00
Use channels for decryption
This commit is contained in:
parent
5612a22027
commit
99887ad529
3 changed files with 51 additions and 26 deletions
|
@ -148,6 +148,18 @@ struct FEntry {
|
|||
uint16_t ContentID;
|
||||
};
|
||||
|
||||
ProgressCallback progressCallback = NULL;
|
||||
|
||||
void set_progress_callback(ProgressCallback cb) {
|
||||
progressCallback = cb;
|
||||
}
|
||||
|
||||
void report_progress(int progress) {
|
||||
if (progressCallback != NULL) {
|
||||
progressCallback(progress);
|
||||
}
|
||||
}
|
||||
|
||||
static bool file_dump(const char *path, void *buf, size_t len) {
|
||||
assert(buf != NULL);
|
||||
assert(len != 0);
|
||||
|
@ -346,7 +358,7 @@ out:
|
|||
}
|
||||
#undef BLOCK_SIZE
|
||||
|
||||
int cdecrypt_main(int argc, char **argv, int *progress) {
|
||||
int cdecrypt_main(int argc, char **argv) {
|
||||
int r = EXIT_FAILURE;
|
||||
char str[PATH_MAX], *tmd_path = NULL, *tik_path = NULL;
|
||||
FILE *src = NULL;
|
||||
|
@ -526,7 +538,7 @@ int cdecrypt_main(int argc, char **argv, int *progress) {
|
|||
uint32_t level = 0;
|
||||
|
||||
for (uint32_t i = 1; i < entries; i++) {
|
||||
*progress = (i * 100) / entries;
|
||||
report_progress((i * 100) / entries);
|
||||
if (level > 0) {
|
||||
while ((level >= 1) && (l_entry[level - 1] == i))
|
||||
level--;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
int cdecrypt_main(int argc, char **argv, int *progress);
|
||||
typedef void (*ProgressCallback)(int progress);
|
||||
void set_progress_callback(ProgressCallback cb);
|
||||
int cdecrypt_main(int argc, char **argv);
|
||||
|
|
|
@ -7,51 +7,62 @@ package wiiudownloader
|
|||
#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"
|
||||
"sync"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var (
|
||||
wg sync.WaitGroup
|
||||
decryptionDone = false
|
||||
decryptionError = false
|
||||
)
|
||||
//export callProgressCallback
|
||||
func callProgressCallback(progress C.int) {
|
||||
progressChan <- int(progress)
|
||||
}
|
||||
|
||||
var progressChan = make(chan int)
|
||||
|
||||
func DecryptContents(path string, progress *ProgressWindow, deleteEncryptedContents bool) error {
|
||||
wg.Add(1)
|
||||
progressInt := 1
|
||||
go runDecryption(path, &progressInt, deleteEncryptedContents)
|
||||
errorChan := make(chan error)
|
||||
defer close(errorChan)
|
||||
|
||||
go runDecryption(path, errorChan, deleteEncryptedContents)
|
||||
|
||||
progress.bar.SetText("Decrypting...")
|
||||
for !decryptionDone {
|
||||
|
||||
for progressInt := range progressChan {
|
||||
progress.bar.SetFraction(float64(progressInt) / 100)
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
if decryptionError {
|
||||
decryptionDone = false
|
||||
decryptionError = false
|
||||
return fmt.Errorf("decryption failed")
|
||||
if err := <-errorChan; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func runDecryption(path string, progress *int, deleteEncryptedContents bool) {
|
||||
defer wg.Done()
|
||||
func runDecryption(path string, errorChan chan<- error, deleteEncryptedContents bool) {
|
||||
argv := make([]*C.char, 2)
|
||||
argv[0] = C.CString("WiiUDownloader")
|
||||
argv[1] = C.CString(path)
|
||||
if int(C.cdecrypt_main(2, (**C.char)(unsafe.Pointer(&argv[0])), (*C.int)(unsafe.Pointer(progress)))) != 0 {
|
||||
decryptionError = true
|
||||
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)
|
||||
}
|
||||
decryptionDone = true
|
||||
|
||||
close(progressChan) // Indicate the completion of the decryption process
|
||||
errorChan <- nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue