198 lines
4.5 KiB
Go
198 lines
4.5 KiB
Go
package utils
|
|
|
|
import (
|
|
"archive/zip"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// SystemInfo contains information about the user's computer.
|
|
var SystemInfo SysInfo = GetSysInfo()
|
|
|
|
// 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
|
|
ProgDir string
|
|
}
|
|
|
|
// 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",
|
|
ProgDir: homeDir + "/.config/r2mod-go",
|
|
}
|
|
|
|
if !PathExists(SystemInfo.ProgDir) {
|
|
log.Println("temp directory does not exist; creating...")
|
|
err := os.Mkdir(SystemInfo.ProgDir, 0755)
|
|
CheckErr(err)
|
|
}
|
|
|
|
if !PathExists(SystemInfo.ProgDir + "/dl") {
|
|
log.Println("temp dl directory does not exist; creating...")
|
|
err := os.Mkdir(SystemInfo.ProgDir+"/dl", 0755)
|
|
CheckErr(err)
|
|
}
|
|
|
|
// Setup log file
|
|
if PathExists(SystemInfo.ProgDir + "/r2mod-go.log") {
|
|
err := os.Remove(SystemInfo.ProgDir + "/r2mod-go.log")
|
|
CheckErr(err)
|
|
}
|
|
|
|
logFile, err := os.OpenFile(SystemInfo.ProgDir+"/r2mod-go.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)
|
|
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)
|
|
log.Println(" ProgDir:", SystemInfo.ProgDir)
|
|
|
|
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
|
|
}
|
|
|
|
// ExistsInArray checks if the given string exists in a string array
|
|
func ExistsInArray(array []string, search string) bool {
|
|
for _, i := range array {
|
|
if i == search {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|