6 Commits

Author SHA1 Message Date
Sotirios Pupakis
7422bcd737 tried 2025-09-18 22:38:24 +02:00
Sotirios Pupakis
93b73d7bea fix 2025-09-08 21:23:20 +02:00
Sotirios Pupakis
110d8c073d ffs 2025-09-08 04:27:09 +02:00
Sotirios Pupakis
2fa2b48611 change 2025-09-08 04:20:15 +02:00
Sotirios Pupakis
8f0fcdca4a Added reference for data 2025-09-06 23:34:53 +02:00
Sotirios Pupakis
345bd44323 Added error hadnling 2025-09-06 23:01:41 +02:00
4 changed files with 48 additions and 14 deletions

2
go.mod
View File

@@ -1,3 +1,5 @@
module gitea.tbdevent.eu/ilbinek/reforger_utils
go 1.25.0
require golang.org/x/net v0.44.0 // indirect

2
go.sum Normal file
View File

@@ -0,0 +1,2 @@
golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I=
golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=

View File

@@ -146,8 +146,8 @@ func parseDirectory(r *bytes.Reader, path string, dataBlockOffset int64) ([]PakE
return entries, totalSize, nil
}
func GetPakFileInfo(data []byte) ([]PakEntry, error) {
r := bytes.NewReader(data)
func GetPakFileInfo(data *[]byte) ([]PakEntry, error) {
r := bytes.NewReader(*data)
// Read FORM header
if err := readString(r, "FORM"); err != nil {

48
util.go
View File

@@ -9,9 +9,25 @@ import (
"path/filepath"
"slices"
"strings"
"golang.org/x/net/proxy"
)
var Debug = false
var Tor = false
var DownThreads = 8
func getHttpClient() (*http.Client, error) {
if Tor {
dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:9050", nil, proxy.Direct)
if err != nil {
return nil, err
}
transport := &http.Transport{Dial: dialer.Dial}
return &http.Client{Transport: transport}, nil
}
return http.DefaultClient, nil
}
func DoAssetsRequest(addonID, version string) (AssetsReply, error) {
url := "https://api-ar-workshop.bistudio.com/workshop-api/api/v3.0/s2s/assets/download-list"
@@ -27,7 +43,11 @@ func DoAssetsRequest(addonID, version string) (AssetsReply, error) {
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)
client, err := getHttpClient()
if err != nil {
return AssetsReply{}, err
}
res, err := client.Do(req)
if err != nil {
return AssetsReply{}, err
}
@@ -37,7 +57,7 @@ func DoAssetsRequest(addonID, version string) (AssetsReply, error) {
// print entire body
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
panic("bad status: " + res.Status)
return AssetsReply{}, fmt.Errorf("bad status: %s", res.Status)
}
// Process response
@@ -57,7 +77,11 @@ func GetManifest(manifestUrl string) (Manifest, error) {
return Manifest{}, err
}
res, err := http.DefaultClient.Do(req)
client, err := getHttpClient()
if err != nil {
return Manifest{}, err
}
res, err := client.Do(req)
if err != nil {
return Manifest{}, err
}
@@ -80,7 +104,7 @@ func Download(manifest Manifest, downloadDir string, filen string) ([]byte, erro
if Debug {
// create the directory if it doesn't exist
if err := os.MkdirAll(downloadDir + "/fragments/" + filen, os.ModePerm); err != nil {
if err := os.MkdirAll(downloadDir+"/fragments/"+filen, os.ModePerm); err != nil {
return nil, err
}
}
@@ -89,7 +113,7 @@ func Download(manifest Manifest, downloadDir string, filen string) ([]byte, erro
downloaded := 0
remContent := make([]byte, 0)
currentRem := 0
if (manifest.Remainder.Size > 0) {
if manifest.Remainder.Size > 0 {
url := transformShaToURL(manifest.Remainder.Sha512, manifest.Remainder.Size)
content, err := getContent(url)
if err != nil {
@@ -109,8 +133,8 @@ func Download(manifest Manifest, downloadDir string, filen string) ([]byte, erro
trackFrag := make([]Frag, 0)
// start downloading chunks
//currentOffset := 0
ret := make([]byte, manifest.Size)
totalMB := float32(manifest.Size) / 1024.0 / 1024.0
for _, fragment := range manifest.Fragments {
url := transformShaToURL(fragment.Sha512, fragment.Size)
content, err := getContent(url)
@@ -131,7 +155,8 @@ func Download(manifest Manifest, downloadDir string, filen string) ([]byte, erro
}
percent := float32(downloaded) / float32(manifest.Size) * 100.0
fmt.Printf("\r\033[32mDownloaded\033[0m %d/%d (\033[36m%.2f%%\033[0m) of \033[33m%s\033[0m", downloaded, manifest.Size, percent, filen)
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)
}
// sort trackFrag by Offset
@@ -141,11 +166,12 @@ func Download(manifest Manifest, downloadDir string, filen string) ([]byte, erro
// check for gaps in trackFrag and fill with remainder content
curPos := 0
for _, frag := range trackFrag {
if frag.Offset > curPos {
// gap detected
gapSize := frag.Offset - curPos
if currentRem + gapSize > len(remContent) {
if currentRem+gapSize > len(remContent) {
panic("not enough remainder content to fill gap")
}
copy(ret[curPos:curPos+gapSize], remContent[currentRem:currentRem+gapSize])
@@ -162,7 +188,11 @@ func Download(manifest Manifest, downloadDir string, filen string) ([]byte, erro
}
func getContent(url string) ([]byte, error) {
resp, err := http.Get(url)
client, err := getHttpClient()
if err != nil {
return nil, err
}
resp, err := client.Get(url)
if err != nil {
return nil, err
}