feat: add bepinex installation

This commit is contained in:
jabuxas 2024-09-20 15:06:43 -03:00
parent c4cc5e184d
commit 908e6b656c
2 changed files with 76 additions and 14 deletions

View File

@ -1,39 +1,102 @@
package main package main
import ( import (
"archive/zip"
"fmt"
"io" "io"
"log"
"net/http" "net/http"
"os" "os"
"path/filepath"
) )
func DownloadCache() error { func DownloadCache() error {
resp, err := http.Get("https://thunderstore.io/api/v1/package/") resp, err := http.Get("https://thunderstore.io/api/v1/package/")
if err != nil { if err != nil {
log.Fatal("couldn't download thunderstore payload") return fmt.Errorf("couldn't download payload: %w", err)
return err
} }
defer resp.Body.Close() defer resp.Body.Close()
if err := os.MkdirAll(TMPDIR, os.ModePerm); err != nil { if err := os.MkdirAll(TMPDIR, os.ModePerm); err != nil {
log.Fatal("couldn't create tmpdir") return fmt.Errorf("couldn't create tmpdir: %w", err)
return err
} }
out, err := os.Create(PAYLOAD) out, err := os.Create(PAYLOAD)
if err != nil { if err != nil {
log.Fatal("couldn't create payload file") return fmt.Errorf("couldn't create file: %w", err)
return err
} }
defer out.Close() defer out.Close()
if _, err := io.Copy(out, resp.Body); err != nil { if _, err := io.Copy(out, resp.Body); err != nil {
log.Fatal("couldn't write to payload file") return fmt.Errorf("couldn't write to file: %w", err)
return err
} }
return nil return nil
} }
func DownloadBepinex() error { 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_LOCATION)
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
} }

View File

@ -15,6 +15,7 @@ var GAME_LOCATION = os.Getenv("GAME_PATH")
func main() { func main() {
getCache() getCache()
installBepinex()
} }
func getCache() error { func getCache() error {
@ -33,15 +34,13 @@ func getCache() error {
return nil return nil
} }
func installBepinex() error { func installBepinex() {
_, err := os.Stat(GAME_LOCATION + "/BepInEx") _, err := os.Stat(GAME_LOCATION + "/BepInEx")
if err != nil { if err != nil {
// install bepinex // install bepinex
DownloadBepinex()
} else { } else {
// bepinex already installed fmt.Print("bepinex already installed")
} }
return nil
} }