2021-01-24 18:12:33 -04:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-01-29 16:28:21 -04:00
|
|
|
// SystemInfo contains information about the user's computer.
|
|
|
|
var SystemInfo SysInfo = GetSysInfo()
|
|
|
|
|
2021-01-24 18:12:33 -04:00
|
|
|
// SysInfo holds information about the user's installation
|
|
|
|
type SysInfo struct {
|
|
|
|
SteamDir string
|
|
|
|
HomeDir string
|
|
|
|
FlatpakDir string
|
|
|
|
SteamGameID string
|
|
|
|
GameDir string
|
|
|
|
CompatDir string
|
|
|
|
BepinDir string
|
|
|
|
PluginDir string
|
|
|
|
ConfigDir string
|
2021-01-29 16:42:45 -04:00
|
|
|
ProgDir string
|
2021-01-24 18:12:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckErr checks for an error, prints if it happened
|
|
|
|
func CheckErr(e error) {
|
|
|
|
if e != nil {
|
|
|
|
log.Fatal(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PathExists checks if file or directory exists
|
|
|
|
func PathExists(path string) bool {
|
|
|
|
if _, err := os.Stat(path); err == nil {
|
|
|
|
return true
|
|
|
|
} else if os.IsNotExist(err) {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
log.Fatal(err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSysInfo gathers information about the user's installation
|
|
|
|
func GetSysInfo() SysInfo {
|
|
|
|
|
|
|
|
var steamDir string = "/.local/share/Steam"
|
|
|
|
var homeDir string = os.Getenv("HOME")
|
|
|
|
var flatpakDir string = "/.var/app/com.valvesoftware.Steam" + steamDir
|
|
|
|
var steamGameID string = "632360"
|
|
|
|
|
|
|
|
var gameDir string
|
|
|
|
if os.Getenv("R2MOD_INSTALL_DIR") != "" {
|
|
|
|
gameDir = os.Getenv("R2MOD_INSTALL_DIR")
|
|
|
|
} else if PathExists(homeDir + flatpakDir + "/steamapps/common/Risk of Rain 2") {
|
|
|
|
gameDir = homeDir + flatpakDir + "/steamapps/common/Risk of Rain 2"
|
|
|
|
} else {
|
|
|
|
gameDir = homeDir + steamDir + "/steamapps/common/Risk of Rain 2"
|
|
|
|
}
|
|
|
|
|
|
|
|
var compatDir string
|
|
|
|
if PathExists(homeDir + flatpakDir + "/steamapps/compatdata/" + steamGameID) {
|
|
|
|
compatDir = homeDir + flatpakDir + "/steamapps/compatdata/" + steamGameID
|
|
|
|
} else {
|
|
|
|
compatDir = homeDir + steamDir + "/steamapps/compatdata/" + steamGameID
|
|
|
|
}
|
|
|
|
|
|
|
|
SystemInfo := SysInfo{
|
|
|
|
SteamDir: "/.local/share/Steam",
|
|
|
|
HomeDir: os.Getenv("HOME"),
|
|
|
|
FlatpakDir: "/.var/app/com.valvesoftware.Steam/.local/share/Steam",
|
|
|
|
SteamGameID: "632360",
|
|
|
|
GameDir: gameDir,
|
|
|
|
CompatDir: compatDir,
|
|
|
|
BepinDir: gameDir + "/BepInEx",
|
|
|
|
ConfigDir: gameDir + "/BepInEx/config",
|
|
|
|
PluginDir: gameDir + "/BepInEx/plugins",
|
2021-01-29 16:42:45 -04:00
|
|
|
ProgDir: homeDir + "/.config/r2mod-go",
|
2021-01-24 18:12:33 -04:00
|
|
|
}
|
|
|
|
|
2021-01-29 16:42:45 -04:00
|
|
|
if !PathExists(SystemInfo.ProgDir) {
|
2021-01-24 18:12:33 -04:00
|
|
|
log.Println("temp directory does not exist; creating...")
|
2021-01-29 16:42:45 -04:00
|
|
|
err := os.Mkdir(SystemInfo.ProgDir, 0755)
|
2021-01-24 18:12:33 -04:00
|
|
|
CheckErr(err)
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:42:45 -04:00
|
|
|
if !PathExists(SystemInfo.ProgDir + "/dl") {
|
2021-01-24 18:12:33 -04:00
|
|
|
log.Println("temp dl directory does not exist; creating...")
|
2021-01-29 16:42:45 -04:00
|
|
|
err := os.Mkdir(SystemInfo.ProgDir+"/dl", 0755)
|
2021-01-24 18:12:33 -04:00
|
|
|
CheckErr(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup log file
|
2021-01-29 16:42:45 -04:00
|
|
|
if PathExists(SystemInfo.ProgDir + "/r2mod-go.log") {
|
|
|
|
err := os.Remove(SystemInfo.ProgDir + "/r2mod-go.log")
|
2021-01-24 18:12:33 -04:00
|
|
|
CheckErr(err)
|
|
|
|
}
|
|
|
|
|
2021-01-29 16:42:45 -04:00
|
|
|
logFile, err := os.OpenFile(SystemInfo.ProgDir+"/r2mod-go.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
|
2021-01-24 18:12:33 -04:00
|
|
|
CheckErr(err)
|
|
|
|
|
|
|
|
log.SetOutput(logFile)
|
|
|
|
|
|
|
|
log.Println("System Information : ")
|
|
|
|
log.Println(" gameDir:", SystemInfo.GameDir)
|
|
|
|
log.Println(" compatDir:", SystemInfo.CompatDir)
|
|
|
|
log.Println(" bepinDir:", SystemInfo.BepinDir)
|
|
|
|
log.Println(" configDir:", SystemInfo.ConfigDir)
|
|
|
|
log.Println(" pluginDir:", SystemInfo.PluginDir)
|
2021-01-29 16:42:45 -04:00
|
|
|
log.Println(" ProgDir:", SystemInfo.ProgDir)
|
2021-01-24 18:12:33 -04:00
|
|
|
|
|
|
|
return SystemInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unzip takes input src and dest, unzips file.
|
|
|
|
func Unzip(src string, dest string) ([]string, error) {
|
|
|
|
var filenames []string
|
|
|
|
|
|
|
|
r, err := zip.OpenReader(src)
|
|
|
|
CheckErr(err)
|
|
|
|
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
for _, f := range r.File {
|
|
|
|
fpath := filepath.Join(dest, f.Name)
|
|
|
|
|
|
|
|
if !strings.HasPrefix(fpath, filepath.Clean(dest)+string(os.PathSeparator)) {
|
|
|
|
return filenames, fmt.Errorf("%s: illegal file path", fpath)
|
|
|
|
}
|
|
|
|
|
|
|
|
filenames = append(filenames, fpath)
|
|
|
|
|
|
|
|
if f.FileInfo().IsDir() {
|
|
|
|
os.MkdirAll(fpath, os.ModePerm)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
|
|
|
|
return filenames, err
|
|
|
|
}
|
|
|
|
|
|
|
|
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
|
|
|
|
if err != nil {
|
|
|
|
return filenames, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rc, err := f.Open()
|
|
|
|
if err != nil {
|
|
|
|
return filenames, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(outFile, rc)
|
|
|
|
|
|
|
|
outFile.Close()
|
|
|
|
rc.Close()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return filenames, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filenames, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadFile Downloads a file through http
|
|
|
|
func DownloadFile(filepath string, url string) error {
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
out, err := os.Create(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
return err
|
|
|
|
}
|