Compare commits

..

3 Commits

2 changed files with 131 additions and 29 deletions

113
helpers.go Normal file
View File

@ -0,0 +1,113 @@
package main
import (
"archive/zip"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
)
func DownloadCache() error {
resp, err := http.Get("https://thunderstore.io/api/v1/package/")
if err != nil {
return fmt.Errorf("couldn't download payload: %w", err)
}
defer resp.Body.Close()
if err := os.MkdirAll(TMPDIR, os.ModePerm); err != nil {
return fmt.Errorf("couldn't create tmpdir: %w", err)
}
out, err := os.Create(PAYLOAD)
if err != nil {
return fmt.Errorf("couldn't create file: %w", err)
}
defer out.Close()
if _, err := io.Copy(out, resp.Body); err != nil {
return fmt.Errorf("couldn't write to file: %w", err)
}
return nil
}
func DownloadBepinex() error {
tmpPath := TMPDIR + "/bepinex.zip"
resp, err := http.Get("https://github.com/BepInEx/BepInEx/releases/download/v5.4.23.2/BepInEx_win_x64_5.4.23.2.zip")
if err != nil {
return fmt.Errorf("could not download bepinex: %w", err)
}
defer resp.Body.Close()
out, err := os.Create(tmpPath)
if err != nil {
return fmt.Errorf("could not create file: %w", err)
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return fmt.Errorf("could not save file: %w", err)
}
err = unzip(tmpPath, GAME_PATH)
if err != nil {
return fmt.Errorf("could not extract BepInEx: %w", err)
}
return nil
}
func unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer r.Close()
for _, f := range r.File {
fpath := filepath.Join(dest, f.Name)
if f.FileInfo().IsDir() {
os.MkdirAll(fpath, os.ModePerm)
continue
}
if err := os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return err
}
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
rc, err := f.Open()
if err != nil {
outFile.Close()
return err
}
_, err = io.Copy(outFile, rc)
outFile.Close()
rc.Close()
if err != nil {
return err
}
}
return nil
}
func SanitizeInput(input *string) error {
if *input == "" {
return fmt.Errorf("%s is not set", *input)
}
if strings.HasSuffix(*input, "/") {
*input = strings.TrimSuffix(*input, "/")
}
return nil
}

View File

@ -2,9 +2,7 @@ package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)
@ -14,18 +12,25 @@ const (
PAYLOAD = TMPDIR + "/payload.json"
)
var GAME_PATH = os.Getenv("GAME_PATH")
func main() {
get_cache()
getCache()
if err := SanitizeInput(&GAME_PATH); err != nil {
log.Fatal("GAME_PATH is not set")
}
installBepinex()
}
func get_cache() error {
func getCache() error {
info, err := os.Stat(PAYLOAD)
if err != nil {
fmt.Println("payload isn't accessible, requesting")
download_cache()
DownloadCache()
} else if !(info.ModTime().After(time.Now().Add(-2 * time.Hour))) {
download_cache()
// download again if payload is older than 2 hours
DownloadCache()
} else {
fmt.Println("payload was updated recently, won't update")
}
@ -33,29 +38,13 @@ func get_cache() error {
return nil
}
func download_cache() error {
resp, err := http.Get("https://thunderstore.io/api/v1/package/")
func installBepinex() {
_, err := os.Stat(GAME_PATH + "/BepInEx")
if err != nil {
log.Fatal("couldn't download thunderstore payload")
return err
// install bepinex
DownloadBepinex()
} else {
fmt.Println("bepinex already installed")
}
defer resp.Body.Close()
if err := os.MkdirAll(TMPDIR, os.ModePerm); err != nil {
log.Fatal("couldn't create tmpdir")
return err
}
out, err := os.Create(PAYLOAD)
if err != nil {
log.Fatal("couldn't create payload file")
return err
}
defer out.Close()
if _, err := io.Copy(out, resp.Body); err != nil {
log.Fatal("couldn't write to payload file")
return err
}
return nil
}