Fixed a few bugs, added 'pull' command, optimized API cache, separated the client from the API more or less.

This commit is contained in:
endigma 2021-01-29 13:56:49 -04:00
parent 584c6cd90e
commit ae49350a59
5 changed files with 117 additions and 80 deletions

View File

@ -6,9 +6,6 @@ import (
"os"
"r2mod-go/utils"
"time"
"github.com/fatih/color"
// TODO: eliminate this dependency
)
// Mod contains info about a mod from thunderstore API
@ -44,34 +41,25 @@ type Version struct {
}
// CheckAPICache checks if the cached json is expired
// TODO: REWRITE
func CheckAPICache() {
// TODO: move the printing out, add returns
color.Blue("> Checking API Cache")
func CheckAPICache() int {
if !utils.PathExists(utils.SystemInfo.TmpDir + "/pkg.json") {
color.Red("> API Cache is outdated")
UpdateAPICache()
} else {
info, err := os.Stat(utils.SystemInfo.TmpDir + "/pkg.json")
utils.CheckErr(err)
lastModTime := info.ModTime()
currentTime := time.Now()
if currentTime.Sub(lastModTime).Seconds() > 7200 {
color.Red("> API Cache is outdated")
color.Blue("> Updating API Cache")
UpdateAPICache()
} else {
color.Green("> API Cache is up to date")
}
return 1
}
info, err := os.Stat(utils.SystemInfo.TmpDir + "/pkg.json")
utils.CheckErr(err)
lastModTime := info.ModTime()
currentTime := time.Now()
if currentTime.Sub(lastModTime).Seconds() > 7200 {
return 1
}
return 0
}
// UpdateAPICache gets a new copy of the thunderstore pkgfile
// TODO: ARIA2 IMPLEMENTATION
func UpdateAPICache() {
apiURL := "https://thunderstore.io/api/v1/package/"
@ -79,15 +67,12 @@ func UpdateAPICache() {
utils.CheckErr(err)
}
// GetModData gets object of mod by depString
func GetModData(depString string) Mod {
if depString == "R2API" {
depString = "tristanmcpherson-R2API"
}
func unpackAPI() []Mod {
pkgFile, err := os.Open(utils.SystemInfo.TmpDir + "/pkg.json")
utils.CheckErr(err)
defer pkgFile.Close()
byteValue, err := ioutil.ReadAll(pkgFile)
utils.CheckErr(err)
@ -96,15 +81,24 @@ func GetModData(depString string) Mod {
err = json.Unmarshal(byteValue, &mods)
utils.CheckErr(err)
return mods
}
// PkgList contains all mods in current package cache
var PkgList []Mod = unpackAPI()
// GetModData gets object of mod by depString
func GetModData(depString string) Mod {
if depString == "R2API" {
depString = "tristanmcpherson-R2API"
}
var selectedMod Mod
for i := 0; i < len(mods); i++ {
if mods[i].FullName == depString {
selectedMod = mods[i]
for i := 0; i < len(PkgList); i++ {
if PkgList[i].FullName == depString {
selectedMod = PkgList[i]
}
}
defer pkgFile.Close()
// TODO: export this lol
return selectedMod
}

5
go.mod
View File

@ -2,4 +2,7 @@ module r2mod-go
go 1.15
require github.com/fatih/color v1.10.0
require (
github.com/fatih/color v1.10.0
github.com/ynsgnr/aria2go v0.0.0-20190317155935-c48e59783821
)

2
go.sum
View File

@ -4,6 +4,8 @@ github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/ynsgnr/aria2go v0.0.0-20190317155935-c48e59783821 h1:u7Ccf6Q8RHS5NRtaedudURcUt33+jieRhvi0HgHzqu0=
github.com/ynsgnr/aria2go v0.0.0-20190317155935-c48e59783821/go.mod h1:GIZQz0e6PpgUM6AxkEFlTum/IIXQAO2YsGcj5Aer58c=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

55
main.go
View File

@ -43,6 +43,7 @@ func main() {
> list, ls, li -- list mods
> update, upgrade, up -- update mods and API cache
> toggle, tm, togglemods -- toggle mods
> pull -- forcefully update API cache, not mods
> filter -- remove version numbers using regex from stdin
`)
case "info":
@ -59,14 +60,26 @@ func main() {
case "install", "ins", "i":
api.CheckAPICache()
for _, m := range os.Args[2:] {
fmt.Println(" >", tools.ExposeModString(m))
tools.DownloadMod(tools.ExposeModString(m))
stat, ov := tools.DownloadMod(tools.ExposeModString(m))
if stat == 0 {
color.Green(" > Already installed and up to date")
} else if stat == 1 {
color.Magenta(" > Updating to version %s", ov)
} else {
color.Blue(" > Downloaded")
}
}
case "remove", "rem", "r":
api.CheckAPICache()
for _, m := range os.Args[2:] {
fmt.Println(" >", tools.ExposeModString(m))
tools.RemoveMod(tools.ExposeModString(m))
if tools.RemoveMod(tools.ExposeModString(m)) == 1 {
color.Red(" > Not installed")
} else {
color.Blue(" > Uninstalled")
}
}
case "list", "ls", "li":
files, err := ioutil.ReadDir(utils.SystemInfo.PluginDir)
@ -78,8 +91,14 @@ func main() {
color.Blue("%3v %s", i, white(f.Name()))
}
case "update", "upgrade", "up":
color.Cyan("> Updating")
api.CheckAPICache()
color.Cyan("> Checking API Cache")
if api.CheckAPICache() == 0 {
color.Green("> API Cache is up to date!")
} else {
color.Red("> API Cache out of date!")
api.UpdateAPICache()
}
files, err := ioutil.ReadDir(utils.SystemInfo.PluginDir)
if err != nil {
log.Fatal(err)
@ -87,12 +106,32 @@ func main() {
for _, f := range files {
fmt.Println(" >", tools.ExposeModString(f.Name()))
tools.DownloadMod(tools.ExposeModString(f.Name()))
stat, ov := tools.DownloadMod(tools.ExposeModString(f.Name()))
if stat == 0 {
color.Green(" > Already installed and up to date")
} else if stat == 1 {
color.Magenta(" > Updating to version %s", ov)
} else {
color.Blue(" > Downloaded")
}
}
color.Green("> Complete!")
// color.Green("> Complete!")
case "pull":
color.Cyan("> Checking API Cache")
if api.CheckAPICache() == 0 {
color.Green("> API Cache is up to date!")
} else {
color.Red("> API Cache out of date!")
api.UpdateAPICache()
}
case "toggle", "tm", "togglemods":
tools.ToggleMods()
if tools.ToggleMods() {
color.Cyan("> Mods Enabled")
} else {
color.Cyan("> Mods Enabled")
}
case "filter":
fmt.Println(tools.ExposeModString(os.Args[2]))
default:

View File

@ -8,8 +8,6 @@ import (
"r2mod-go/utils"
"regexp"
"strings"
"github.com/fatih/color"
)
// ExposeModString removes user-error surrounding the mod string.
@ -20,7 +18,7 @@ func ExposeModString(input string) string {
}
// DownloadMod gets the download URL and installs the mod in the correct folder
func DownloadMod(depString string) {
func DownloadMod(depString string) (int, string) {
if depString == "R2API" {
depString = "tristanmcpherson-R2API"
}
@ -34,47 +32,44 @@ func DownloadMod(depString string) {
modFolder := sysinfo.PluginDir + "/" + depString + "-" + modVersion
if utils.PathExists(modFolder) {
color.Green(" > Already installed and up to date")
} else {
if len(api.GetModData(depString).Versions) >= 2 {
modVersionOld := api.GetModData(depString).Versions[1].VersionNumber
modFolderOld := sysinfo.PluginDir + "/" + depString + "-" + modVersionOld
if utils.PathExists(modFolderOld) {
color.Magenta(" > Updating to version %s", modVersion)
defer os.RemoveAll(modFolderOld)
}
}
color.Blue(" > Downloading...")
err := utils.DownloadFile(modName, downloadURL)
utils.CheckErr(err)
color.Blue(" > Unzipping")
unzip, err := utils.Unzip(modName, modFolder)
log.Println(unzip)
utils.CheckErr(err)
return 0, ""
}
if len(api.GetModData(depString).Versions) >= 2 {
modVersionOld := api.GetModData(depString).Versions[1].VersionNumber
modFolderOld := sysinfo.PluginDir + "/" + depString + "-" + modVersionOld
if utils.PathExists(modFolderOld) {
defer os.RemoveAll(modFolderOld)
return 1, modVersion
}
}
err := utils.DownloadFile(modName, downloadURL)
utils.CheckErr(err)
unzip, err := utils.Unzip(modName, modFolder)
log.Println(unzip)
utils.CheckErr(err)
return 2, modVersion
}
// RemoveMod uninstalls a mod
func RemoveMod(depString string) {
func RemoveMod(depString string) int {
modVersion := api.GetModData(depString).Versions[0].VersionNumber
modFolder := utils.SystemInfo.PluginDir + "/" + depString + "-" + modVersion
if !utils.PathExists(modFolder) {
color.Red(" > Not installed")
} else {
defer os.RemoveAll(modFolder)
color.Blue(" > Uninstalled")
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() {
func ToggleMods() bool {
var configFile string = utils.SystemInfo.GameDir + "/doorstop_config.ini"
input, err := ioutil.ReadFile(configFile)
@ -82,15 +77,19 @@ func ToggleMods() {
lines := strings.Split(string(input), "\n")
var status bool
if strings.Contains(lines[2], "true") {
color.Cyan("> Mods Disabled")
status = false
lines[2] = "enabled=false"
} else {
color.Cyan("> Mods Enabled")
status = true
lines[2] = "enabled=true"
}
output := strings.Join(lines, "\n")
err = ioutil.WriteFile(configFile, []byte(output), 0644)
utils.CheckErr(err)
return status
}