risk/tools/tools.go

127 lines
3.0 KiB
Go
Raw Normal View History

2021-01-24 18:12:33 -04:00
package tools
import (
"io/ioutil"
"log"
"os"
"r2go/api"
"r2go/utils"
2021-01-24 18:12:33 -04:00
"regexp"
"strings"
)
// ExposeModString removes user-error surrounding the mod string.
func ExposeModString(input string) string {
reg := regexp.MustCompile(`\-([0-9]+)\.([0-9]+)\.([0-9]+)`)
res := reg.ReplaceAllString(input, "")
return res
}
// DownloadMod gets the download URL and installs the mod in the correct folder
2021-01-30 16:02:49 -04:00
// DownloadMod returns 0 if the download is sucessful, and 1 if it is already installed
// DownlaodMod returns 2 if the mod updated, and 3 if the mod could not be found.
func DownloadMod(depString string) (int, string, []string) {
var status int
var modver string
var deps []string
2021-01-24 18:12:33 -04:00
if depString == "R2API" {
depString = "tristanmcpherson-R2API"
}
sysinfo := utils.GetSysInfo()
2021-01-30 16:02:49 -04:00
status, mod := api.GetModData(depString)
if status != 0 {
return 3, "not_found", deps
2021-01-30 16:02:49 -04:00
}
downloadURL := mod.Versions[0].DownloadURL
2021-01-24 18:12:33 -04:00
2021-01-29 16:42:45 -04:00
modName := sysinfo.ProgDir + "/dl/" + depString + ".zip"
2021-01-24 18:12:33 -04:00
modVersion := mod.Versions[0].VersionNumber
2021-01-24 18:12:33 -04:00
modFolder := sysinfo.PluginDir + "/" + depString + "-" + modVersion
if utils.PathExists(modFolder) {
2021-01-30 16:02:49 -04:00
return 1, modver, deps
}
2021-01-24 18:12:33 -04:00
2021-01-30 16:02:49 -04:00
status, mod = api.GetModData(depString)
if len(mod.Versions) >= 2 {
modVersionOld := mod.Versions[1].VersionNumber
modFolderOld := sysinfo.PluginDir + "/" + depString + "-" + modVersionOld
2021-01-24 18:12:33 -04:00
if utils.PathExists(modFolderOld) {
defer os.RemoveAll(modFolderOld)
status = 2
modver = modVersion
} else {
2021-01-30 16:02:49 -04:00
status = 0
}
}
2021-01-24 18:12:33 -04:00
err := utils.DownloadFile(modName, downloadURL)
utils.CheckErr(err)
2021-01-24 18:12:33 -04:00
unzip, err := utils.Unzip(modName, modFolder)
log.Println(unzip)
utils.CheckErr(err)
for _, dep := range mod.Versions[0].Dependencies {
deps = append(deps, dep)
}
// TODO: Dependencies!
2021-01-31 06:16:34 -04:00
// fmt.Println("pog")
// for _, dep := range mod.Versions[0].Dependencies {
// DownloadMod(dep)
// }
return status, modver, deps
2021-01-24 18:12:33 -04:00
}
// RemoveMod uninstalls a mod
// RemoveMod returns 2 if the mod string was not found, 1 if the mod isn't installed and 0 on success
func RemoveMod(depString string) int {
// fmt.Println(depString)
2021-01-30 16:02:49 -04:00
status, mod := api.GetModData(depString)
// fmt.Println(mod)
2021-01-30 16:02:49 -04:00
if status != 0 {
return 2
2021-01-30 16:02:49 -04:00
}
modFolder := utils.SystemInfo.PluginDir + "/" + depString + "-" + mod.Versions[0].VersionNumber
2021-01-24 18:12:33 -04:00
if !utils.PathExists(modFolder) {
return 1
2021-01-24 18:12:33 -04:00
}
defer os.RemoveAll(modFolder)
return 0
2021-01-24 18:12:33 -04:00
}
// ToggleMods toggles mods on or off.
// TODO: make this not hardcoded as shit, perhaps implement a proper parser
func ToggleMods() bool {
2021-01-24 18:12:33 -04:00
var configFile string = utils.SystemInfo.GameDir + "/doorstop_config.ini"
input, err := ioutil.ReadFile(configFile)
utils.CheckErr(err)
lines := strings.Split(string(input), "\n")
var status bool
2021-01-24 18:12:33 -04:00
if strings.Contains(lines[2], "true") {
status = false
2021-01-24 18:12:33 -04:00
lines[2] = "enabled=false"
} else {
status = true
2021-01-24 18:12:33 -04:00
lines[2] = "enabled=true"
}
output := strings.Join(lines, "\n")
err = ioutil.WriteFile(configFile, []byte(output), 0644)
utils.CheckErr(err)
return status
2021-01-24 18:12:33 -04:00
}