From 908e6b656c1db3b4b98b38b299451ea4e9134405 Mon Sep 17 00:00:00 2001 From: jabuxas Date: Fri, 20 Sep 2024 15:06:43 -0300 Subject: [PATCH] feat: add bepinex installation --- helpers.go | 81 ++++++++++++++++++++++++++++++++++++++++++++++++------ spire.go | 9 +++--- 2 files changed, 76 insertions(+), 14 deletions(-) diff --git a/helpers.go b/helpers.go index d9feccb..d3b1835 100644 --- a/helpers.go +++ b/helpers.go @@ -1,39 +1,102 @@ package main import ( + "archive/zip" + "fmt" "io" - "log" "net/http" "os" + "path/filepath" ) func DownloadCache() error { resp, err := http.Get("https://thunderstore.io/api/v1/package/") if err != nil { - log.Fatal("couldn't download thunderstore payload") - return err + return fmt.Errorf("couldn't download payload: %w", err) } defer resp.Body.Close() if err := os.MkdirAll(TMPDIR, os.ModePerm); err != nil { - log.Fatal("couldn't create tmpdir") - return err + return fmt.Errorf("couldn't create tmpdir: %w", err) } out, err := os.Create(PAYLOAD) if err != nil { - log.Fatal("couldn't create payload file") - return err + return fmt.Errorf("couldn't create file: %w", err) } defer out.Close() if _, err := io.Copy(out, resp.Body); err != nil { - log.Fatal("couldn't write to payload file") - return err + 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_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 } diff --git a/spire.go b/spire.go index 5f5a255..19fe3bd 100644 --- a/spire.go +++ b/spire.go @@ -15,6 +15,7 @@ var GAME_LOCATION = os.Getenv("GAME_PATH") func main() { getCache() + installBepinex() } func getCache() error { @@ -33,15 +34,13 @@ func getCache() error { return nil } -func installBepinex() error { +func installBepinex() { _, err := os.Stat(GAME_LOCATION + "/BepInEx") if err != nil { // install bepinex - + DownloadBepinex() } else { - // bepinex already installed + fmt.Print("bepinex already installed") } - - return nil }