refactor: demodularize bepinex install

This commit is contained in:
jabuxas 2024-09-28 09:55:08 -03:00
parent 464b8ef778
commit ecb4650423
2 changed files with 27 additions and 33 deletions

View File

@ -34,35 +34,37 @@ func DownloadCache() error {
} }
func DownloadBepinex() error { func DownloadBepinex() error {
tmpPath := TMPDIR + "/bepinex.zip" if _, err := os.Stat(GAME_PATH + "/BepInEx"); err != nil {
tmpPath := TMPDIR + "/bepinex.zip"
// download only if it doesnt exist // download only if it doesnt exist
if _, err := os.Stat(tmpPath); err != nil { if _, err := os.Stat(tmpPath); err != nil {
resp, err := http.Get( resp, err := http.Get(
"https://github.com/BepInEx/BepInEx/releases/download/v5.4.23.2/BepInEx_win_x64_5.4.23.2.zip", "https://github.com/BepInEx/BepInEx/releases/download/v5.4.23.2/BepInEx_win_x64_5.4.23.2.zip",
) )
if err != nil { if err != nil {
return fmt.Errorf("could not download bepinex: %w", err) return fmt.Errorf("could not download bepinex: %w", err)
} }
defer resp.Body.Close() defer resp.Body.Close()
out, err := os.Create(tmpPath) out, err := os.Create(tmpPath)
if err != nil { if err != nil {
return fmt.Errorf("could not create file: %w", err) return fmt.Errorf("could not create file: %w", err)
} }
defer out.Close() defer out.Close()
_, err = io.Copy(out, resp.Body) _, err = io.Copy(out, resp.Body)
if err != nil { if err != nil {
return fmt.Errorf("could not save file: %w", err) 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)
}
} }
err := unzip(tmpPath, GAME_PATH)
if err != nil {
return fmt.Errorf("could not extract BepInEx: %w", err)
}
return nil return nil
} }

View File

@ -18,7 +18,7 @@ func main() {
if err := SanitizeInput(&GAME_PATH); err != nil { if err := SanitizeInput(&GAME_PATH); err != nil {
log.Fatal("GAME_PATH is not set") log.Fatal("GAME_PATH is not set")
} }
installBepinex() DownloadBepinex()
} }
func getCache() error { func getCache() error {
@ -35,11 +35,3 @@ func getCache() error {
return nil return nil
} }
func installBepinex() {
if _, err := os.Stat(GAME_PATH + "/BepInEx"); err != nil {
// install bepinex
DownloadBepinex()
}
// else bepinex already installed
}