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
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
}

View File

@ -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
}