risk/tools/tools.go

120 lines
2.7 KiB
Go

package tools
import (
"fmt"
"io/ioutil"
"log"
"os"
"r2go/api"
"r2go/utils"
"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
// 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) {
var status int
var modver string
if depString == "R2API" {
depString = "tristanmcpherson-R2API"
}
sysinfo := utils.GetSysInfo()
status, mod := api.GetModData(depString)
if status != 0 {
return 3, "not_found"
}
downloadURL := mod.Versions[0].DownloadURL
modName := sysinfo.ProgDir + "/dl/" + depString + ".zip"
modVersion := mod.Versions[0].VersionNumber
modFolder := sysinfo.PluginDir + "/" + depString + "-" + modVersion
if utils.PathExists(modFolder) {
return 1, modver
}
status, mod = api.GetModData(depString)
if len(mod.Versions) >= 2 {
modVersionOld := mod.Versions[1].VersionNumber
modFolderOld := sysinfo.PluginDir + "/" + depString + "-" + modVersionOld
if utils.PathExists(modFolderOld) {
defer os.RemoveAll(modFolderOld)
status = 2
modver = modVersion
} else {
status = 0
}
}
err := utils.DownloadFile(modName, downloadURL)
utils.CheckErr(err)
unzip, err := utils.Unzip(modName, modFolder)
log.Println(unzip)
utils.CheckErr(err)
// TODO: Dependencies!
fmt.Println("pog")
for _, dep := range mod.Versions[0].Dependencies {
DownloadMod(dep)
}
return status, modver
}
// RemoveMod uninstalls a mod
func RemoveMod(depString string) int {
status, mod := api.GetModData(depString)
if status != 0 {
fmt.Println("> Could not find the mod specified.")
}
modFolder := utils.SystemInfo.PluginDir + "/" + depString + "-" + mod.Versions[0].VersionNumber
if !utils.PathExists(modFolder) {
return 1
}
defer os.RemoveAll(modFolder)
return 0
}
// ToggleMods toggles mods on or off.
// TODO: make this not hardcoded as shit, perhaps implement a proper parser
func ToggleMods() bool {
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
if strings.Contains(lines[2], "true") {
status = false
lines[2] = "enabled=false"
} else {
status = true
lines[2] = "enabled=true"
}
output := strings.Join(lines, "\n")
err = ioutil.WriteFile(configFile, []byte(output), 0644)
utils.CheckErr(err)
return status
}