Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6d725b9f5f | ||
|
|
93b73d7bea |
@@ -61,11 +61,13 @@ type Manifest struct {
|
|||||||
Size int `json:"size"`
|
Size int `json:"size"`
|
||||||
Offsets []int `json:"offsets"`
|
Offsets []int `json:"offsets"`
|
||||||
} `json:"remainder"`
|
} `json:"remainder"`
|
||||||
Fragments []struct {
|
Fragments []Fragment `json:"fragments"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Fragment struct {
|
||||||
Sha512 string `json:"sha512"`
|
Sha512 string `json:"sha512"`
|
||||||
Size int `json:"size"`
|
Size int `json:"size"`
|
||||||
Offsets []int `json:"offsets"`
|
Offsets []int `json:"offsets"`
|
||||||
} `json:"fragments"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type RDBHeader struct {
|
type RDBHeader struct {
|
||||||
|
|||||||
80
util.go
80
util.go
@@ -9,9 +9,11 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Debug = false
|
var Debug = false
|
||||||
|
var DownThreads = 8
|
||||||
|
|
||||||
func DoAssetsRequest(addonID, version string) (AssetsReply, error) {
|
func DoAssetsRequest(addonID, version string) (AssetsReply, error) {
|
||||||
url := "https://api-ar-workshop.bistudio.com/workshop-api/api/v3.0/s2s/assets/download-list"
|
url := "https://api-ar-workshop.bistudio.com/workshop-api/api/v3.0/s2s/assets/download-list"
|
||||||
@@ -21,7 +23,11 @@ func DoAssetsRequest(addonID, version string) (AssetsReply, error) {
|
|||||||
return AssetsReply{}, err
|
return AssetsReply{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// ffs
|
req.Header.Add("x-client-id", "$edb1b7862bba5cade1f6e06bfdeac2c")
|
||||||
|
req.Header.Add("x-client-secret", "$8b415ea2aa11bd51f2f5b5a9dcb8476")
|
||||||
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
req.Header.Add("user-agent", "Arma Reforger/1.4.0.53 (Headless; Windows)")
|
||||||
|
req.Header.Add("content-length", fmt.Sprintf("%d", len(body)))
|
||||||
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
res, err := http.DefaultClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -105,32 +111,75 @@ func Download(manifest Manifest, downloadDir string, filen string) ([]byte, erro
|
|||||||
trackFrag := make([]Frag, 0)
|
trackFrag := make([]Frag, 0)
|
||||||
|
|
||||||
// start downloading chunks
|
// start downloading chunks
|
||||||
//currentOffset := 0
|
|
||||||
ret := make([]byte, manifest.Size)
|
ret := make([]byte, manifest.Size)
|
||||||
totalMB := float32(manifest.Size) / 1024.0 / 1024.0
|
totalMB := float32(manifest.Size) / 1024.0 / 1024.0
|
||||||
for _, fragment := range manifest.Fragments {
|
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
var mu sync.Mutex
|
||||||
|
|
||||||
|
fragmentsChan := make(chan Fragment, len(manifest.Fragments))
|
||||||
|
errChan := make(chan error, DownThreads)
|
||||||
|
|
||||||
|
// Start worker goroutines
|
||||||
|
for i := 0; i < DownThreads; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
for fragment := range fragmentsChan {
|
||||||
url := transformShaToURL(fragment.Sha512, fragment.Size)
|
url := transformShaToURL(fragment.Sha512, fragment.Size)
|
||||||
content, err := getContent(url)
|
content, err := getContent(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
// Handle error, maybe by sending it to an error channel
|
||||||
|
// For now, we'll just skip and print.
|
||||||
|
fmt.Printf("Error downloading fragment %s: %v\n", fragment.Sha512, err)
|
||||||
|
errChan <- err
|
||||||
}
|
}
|
||||||
downloaded += len(content) * len(fragment.Offsets)
|
|
||||||
|
mu.Lock()
|
||||||
|
// Safely write to the shared slice and update progress
|
||||||
for _, offset := range fragment.Offsets {
|
for _, offset := range fragment.Offsets {
|
||||||
trackFrag = append(trackFrag, Frag{Offset: offset, Size: len(content)})
|
if offset+len(content) > len(ret) {
|
||||||
|
// Handle case where fragment is too large for the buffer
|
||||||
|
fmt.Printf("Fragment too large for buffer at offset %d\n", offset)
|
||||||
|
continue
|
||||||
|
}
|
||||||
copy(ret[offset:offset+len(content)], content)
|
copy(ret[offset:offset+len(content)], content)
|
||||||
}
|
}
|
||||||
|
downloaded += len(content) * len(fragment.Offsets)
|
||||||
|
|
||||||
if Debug {
|
// Print progress
|
||||||
// save fragment to file in the fragments/ directory
|
|
||||||
if err := os.WriteFile(filepath.Join(downloadDir, "fragments", filen, fmt.Sprintf("%d.%s.%d.bytes", fragment.Offsets[0], fragment.Sha512, fragment.Size)), content, os.ModePerm); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
percent := float32(downloaded) / float32(manifest.Size) * 100.0
|
|
||||||
downedMB := float32(downloaded) / 1024.0 / 1024.0
|
downedMB := float32(downloaded) / 1024.0 / 1024.0
|
||||||
fmt.Printf("\r\033[32mDownloaded\033[0m %fMB/%fMB (\033[36m%.2f%%\033[0m) of \033[33m%s\033[0m", downedMB, totalMB, percent, filen)
|
percent := float32(downloaded) / float32(manifest.Size) * 100.0
|
||||||
|
fmt.Printf("\r\033[32mDownloaded\033[0m %.2fMB/%.2fMB (\033[36m%.2f%%\033[0m) of \033[33m%s\033[0m", downedMB, totalMB, percent, filen)
|
||||||
|
|
||||||
|
mu.Unlock()
|
||||||
|
|
||||||
|
// The Debug section can be safely moved here as well
|
||||||
|
if os.Getenv("DEBUG") == "true" { // Using env var for a cleaner check
|
||||||
|
mu.Lock() // Re-lock for file write
|
||||||
|
if err := os.WriteFile(filepath.Join(downloadDir, "fragments", filen, fmt.Sprintf("%d.%s.%d.bytes", fragment.Offsets[0], fragment.Sha512, fragment.Size)), content, os.ModePerm); err != nil {
|
||||||
|
fmt.Printf("Error saving file: %v\n", err)
|
||||||
}
|
}
|
||||||
|
mu.Unlock()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send fragments to the channel
|
||||||
|
for _, fragment := range manifest.Fragments {
|
||||||
|
fragmentsChan <- fragment
|
||||||
|
}
|
||||||
|
close(fragmentsChan) // Close the channel to signal workers no more tasks are coming
|
||||||
|
|
||||||
|
// Wait for all workers to finish
|
||||||
|
wg.Wait()
|
||||||
|
|
||||||
|
// Check for errors
|
||||||
|
if len(errChan) > 0 {
|
||||||
|
return nil, <-errChan // Return the first error encountered
|
||||||
|
}
|
||||||
|
close(errChan)
|
||||||
|
|
||||||
// sort trackFrag by Offset
|
// sort trackFrag by Offset
|
||||||
slices.SortFunc(trackFrag, func(a, b Frag) int {
|
slices.SortFunc(trackFrag, func(a, b Frag) int {
|
||||||
@@ -139,6 +188,7 @@ func Download(manifest Manifest, downloadDir string, filen string) ([]byte, erro
|
|||||||
|
|
||||||
// check for gaps in trackFrag and fill with remainder content
|
// check for gaps in trackFrag and fill with remainder content
|
||||||
curPos := 0
|
curPos := 0
|
||||||
|
|
||||||
for _, frag := range trackFrag {
|
for _, frag := range trackFrag {
|
||||||
if frag.Offset > curPos {
|
if frag.Offset > curPos {
|
||||||
// gap detected
|
// gap detected
|
||||||
|
|||||||
Reference in New Issue
Block a user