Added check if mod exists

This commit is contained in:
rainyuu 2021-01-30 21:02:49 +01:00
parent 2b19b57541
commit 3933f84535
4 changed files with 44 additions and 21 deletions

View File

@ -113,16 +113,16 @@ func SearchMods(inp string) fuzzy.Ranks {
} }
// GetModData gets object of mod by depString // GetModData gets object of mod by depString
func GetModData(depString string) Mod { func GetModData(depString string) (int, Mod) {
if depString == "R2API" { if depString == "R2API" {
depString = "tristanmcpherson-R2API" depString = "tristanmcpherson-R2API"
} }
for i := 0; i < len(PkgList); i++ { for i := 0; i < len(PkgList); i++ {
if PkgList[i].FullName == depString { if PkgList[i].FullName == depString {
return PkgList[i] return 0, PkgList[i]
} }
} }
panic("Mod not found") return 1, Mod{}
} }

View File

@ -15,22 +15,28 @@ func InstallMod(pkg string) {
green := color.New(color.FgGreen).SprintFunc() green := color.New(color.FgGreen).SprintFunc()
magenta := color.New(color.FgMagenta).SprintFunc() magenta := color.New(color.FgMagenta).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
blue := color.New(color.FgBlue).SprintFunc() blue := color.New(color.FgBlue).SprintFunc()
if stat == 1 { if stat == 1 {
buffer += green("up to date!") buffer += green("up to date!")
} else if stat == 2 { } else if stat == 2 {
buffer += fmt.Sprintf("%s %s", magenta("updating to version"), ov) buffer += fmt.Sprintf("%s %s", magenta("updating to version"), ov)
} else if stat == 3 {
buffer += fmt.Sprintf("%s", red("could not find the mod specified."))
} else { } else {
buffer += blue("downloaded") buffer += blue("downloaded")
} }
mod := api.GetModData(tools.ExposeModString(pkg)) if stat != 3 {
for _, dep := range mod.Versions[0].Dependencies { _, mod := api.GetModData(tools.ExposeModString(pkg))
depString := tools.ExposeModString(dep) for _, dep := range mod.Versions[0].Dependencies {
if depString != "tristanmcpherson-R2API" && depString != "bbepis-BepInExPack" { depString := tools.ExposeModString(dep)
InstallMod(depString) if depString != "tristanmcpherson-R2API" && depString != "bbepis-BepInExPack" {
InstallMod(depString)
}
} }
} }
fmt.Println(buffer) fmt.Println(buffer)

23
main.go
View File

@ -55,9 +55,12 @@ func main() {
case "info": case "info":
api.InitAPI() api.InitAPI()
if len(os.Args) <= 2 { if len(os.Args) <= 2 {
fmt.Printf("Usage: %s info <dependency string>\n", os.Args[0]) fmt.Println("Usage: r2go info <dependency string>")
} else { } else {
var selectedmod api.Mod = api.GetModData(os.Args[2]) status, selectedmod := api.GetModData(os.Args[2])
if status != 0 {
fmt.Println("> Could not find the mod specified.")
}
color.Cyan("Mod Info: %s", os.Args[2]) color.Cyan("Mod Info: %s", os.Args[2])
fmt.Println(" Name: " + selectedmod.Versions[0].FullName) fmt.Println(" Name: " + selectedmod.Versions[0].FullName)
fmt.Println(" Desc: " + selectedmod.Versions[0].Description) fmt.Println(" Desc: " + selectedmod.Versions[0].Description)
@ -106,9 +109,14 @@ func main() {
color.Green("> Complete!") color.Green("> Complete!")
case "pull": case "pull":
color.Red("> Forcefully updating API Cache")
api.InitAPI() api.InitAPI()
api.UpdateAPICache() 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": case "toggle", "tm", "togglemods":
if tools.ToggleMods() { if tools.ToggleMods() {
color.Cyan("> Mods Enabled") color.Cyan("> Mods Enabled")
@ -118,9 +126,8 @@ func main() {
case "filter": case "filter":
fmt.Println(tools.ExposeModString(os.Args[2])) fmt.Println(tools.ExposeModString(os.Args[2]))
case "version", "ver", "v": case "version", "ver", "v":
color.Magenta("> r2go") color.Magenta("> r2go", version)
fmt.Println(">", version) fmt.Println(" > github.com/endigma442")
fmt.Println("> git.cya.cx/endigma/r2mod-go")
case "search", "s", "find": case "search", "s", "find":
api.InitAPI() api.InitAPI()
@ -134,6 +141,6 @@ func main() {
fmt.Println(" -", mod.Target) fmt.Println(" -", mod.Target)
} }
default: default:
fmt.Printf("Unknown command, use '%s help' for a list of commands.\n", os.Args[0]) fmt.Println("Unknown command, use 'r2go help' for a list of commands.")
} }
} }

View File

@ -19,6 +19,8 @@ func ExposeModString(input string) string {
} }
// DownloadMod gets the download URL and installs the mod in the correct folder // 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) { func DownloadMod(depString string) (int, string) {
var status int var status int
var modver string var modver string
@ -28,7 +30,10 @@ func DownloadMod(depString string) (int, string) {
} }
sysinfo := utils.GetSysInfo() sysinfo := utils.GetSysInfo()
mod := api.GetModData(depString) status, mod := api.GetModData(depString)
if status != 0 {
return 3, "not_found"
}
downloadURL := mod.Versions[0].DownloadURL downloadURL := mod.Versions[0].DownloadURL
modName := sysinfo.ProgDir + "/dl/" + depString + ".zip" modName := sysinfo.ProgDir + "/dl/" + depString + ".zip"
@ -37,10 +42,12 @@ func DownloadMod(depString string) (int, string) {
modFolder := sysinfo.PluginDir + "/" + depString + "-" + modVersion modFolder := sysinfo.PluginDir + "/" + depString + "-" + modVersion
if utils.PathExists(modFolder) { if utils.PathExists(modFolder) {
return 1, modver return 1, modver
} }
if len(api.GetModData(depString).Versions) >= 2 { status, mod = api.GetModData(depString)
if len(mod.Versions) >= 2 {
modVersionOld := mod.Versions[1].VersionNumber modVersionOld := mod.Versions[1].VersionNumber
modFolderOld := sysinfo.PluginDir + "/" + depString + "-" + modVersionOld modFolderOld := sysinfo.PluginDir + "/" + depString + "-" + modVersionOld
@ -49,7 +56,7 @@ func DownloadMod(depString string) (int, string) {
status = 2 status = 2
modver = modVersion modver = modVersion
} else { } else {
status = 3 status = 0
} }
} }
@ -71,8 +78,11 @@ func DownloadMod(depString string) (int, string) {
// RemoveMod uninstalls a mod // RemoveMod uninstalls a mod
func RemoveMod(depString string) int { func RemoveMod(depString string) int {
modVersion := api.GetModData(depString).Versions[0].VersionNumber status, mod := api.GetModData(depString)
modFolder := utils.SystemInfo.PluginDir + "/" + depString + "-" + modVersion if status != 0 {
fmt.Println("> Could not find the mod specified.")
}
modFolder := utils.SystemInfo.PluginDir + "/" + depString + "-" + mod.Versions[0].VersionNumber
if !utils.PathExists(modFolder) { if !utils.PathExists(modFolder) {
return 1 return 1