2021-01-24 18:12:33 -04:00
|
|
|
package tools
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
2021-01-29 16:28:21 -04:00
|
|
|
"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-29 13:56:49 -04:00
|
|
|
func DownloadMod(depString string) (int, string) {
|
2021-01-29 16:28:21 -04:00
|
|
|
var status int
|
|
|
|
var modver string
|
|
|
|
|
2021-01-24 18:12:33 -04:00
|
|
|
if depString == "R2API" {
|
|
|
|
depString = "tristanmcpherson-R2API"
|
|
|
|
}
|
|
|
|
|
|
|
|
sysinfo := utils.GetSysInfo()
|
2021-01-29 16:28:21 -04:00
|
|
|
mod := api.GetModData(depString)
|
|
|
|
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
|
|
|
|
2021-01-29 16:28:21 -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-29 16:28:21 -04:00
|
|
|
return 1, modver
|
2021-01-29 13:56:49 -04:00
|
|
|
}
|
2021-01-24 18:12:33 -04:00
|
|
|
|
2021-01-29 13:56:49 -04:00
|
|
|
if len(api.GetModData(depString).Versions) >= 2 {
|
2021-01-29 16:28:21 -04:00
|
|
|
modVersionOld := mod.Versions[1].VersionNumber
|
2021-01-29 13:56:49 -04:00
|
|
|
modFolderOld := sysinfo.PluginDir + "/" + depString + "-" + modVersionOld
|
2021-01-24 18:12:33 -04:00
|
|
|
|
2021-01-29 13:56:49 -04:00
|
|
|
if utils.PathExists(modFolderOld) {
|
|
|
|
defer os.RemoveAll(modFolderOld)
|
2021-01-29 16:28:21 -04:00
|
|
|
status = 2
|
|
|
|
modver = modVersion
|
|
|
|
} else {
|
|
|
|
status = 3
|
2021-01-29 13:56:49 -04:00
|
|
|
}
|
|
|
|
}
|
2021-01-24 18:12:33 -04:00
|
|
|
|
2021-01-29 13:56:49 -04:00
|
|
|
err := utils.DownloadFile(modName, downloadURL)
|
|
|
|
utils.CheckErr(err)
|
2021-01-24 18:12:33 -04:00
|
|
|
|
2021-01-29 13:56:49 -04:00
|
|
|
unzip, err := utils.Unzip(modName, modFolder)
|
|
|
|
log.Println(unzip)
|
|
|
|
utils.CheckErr(err)
|
|
|
|
|
2021-01-29 16:28:21 -04:00
|
|
|
// TODO: Dependencies!
|
|
|
|
// for i, dep := range {
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
return status, modver
|
2021-01-24 18:12:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveMod uninstalls a mod
|
2021-01-29 13:56:49 -04:00
|
|
|
func RemoveMod(depString string) int {
|
2021-01-24 18:12:33 -04:00
|
|
|
modVersion := api.GetModData(depString).Versions[0].VersionNumber
|
|
|
|
modFolder := utils.SystemInfo.PluginDir + "/" + depString + "-" + modVersion
|
|
|
|
|
|
|
|
if !utils.PathExists(modFolder) {
|
2021-01-29 13:56:49 -04:00
|
|
|
return 1
|
2021-01-24 18:12:33 -04:00
|
|
|
}
|
2021-01-29 13:56:49 -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
|
2021-01-29 13:56:49 -04:00
|
|
|
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")
|
|
|
|
|
2021-01-29 13:56:49 -04:00
|
|
|
var status bool
|
|
|
|
|
2021-01-24 18:12:33 -04:00
|
|
|
if strings.Contains(lines[2], "true") {
|
2021-01-29 13:56:49 -04:00
|
|
|
status = false
|
2021-01-24 18:12:33 -04:00
|
|
|
lines[2] = "enabled=false"
|
|
|
|
} else {
|
2021-01-29 13:56:49 -04:00
|
|
|
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)
|
2021-01-29 13:56:49 -04:00
|
|
|
|
|
|
|
return status
|
2021-01-24 18:12:33 -04:00
|
|
|
}
|