From 06c5b08813329081402c007f3ac07e39db588c87 Mon Sep 17 00:00:00 2001 From: comp500 Date: Sun, 16 Jun 2019 16:01:49 +0100 Subject: [PATCH] Add update logic for single mods --- curseforge/curseforge.go | 4 +- main.go | 98 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 4 deletions(-) diff --git a/curseforge/curseforge.go b/curseforge/curseforge.go index 081971f..e778356 100644 --- a/curseforge/curseforge.go +++ b/curseforge/curseforge.go @@ -281,8 +281,8 @@ func (u cfUpdater) DoUpdate(mods []*core.Mod, cachedState []interface{}) error { Hash: strconv.Itoa(modState.fileInfo.Fingerprint), } - v.Update["curseforge"]["ProjectID"] = modState.ID - v.Update["curseforge"]["FileID"] = modState.fileInfo.ID + v.Update["curseforge"]["project-id"] = modState.ID + v.Update["curseforge"]["file-id"] = modState.fileInfo.ID } return nil diff --git a/main.go b/main.go index 061bcac..b5090e9 100644 --- a/main.go +++ b/main.go @@ -25,8 +25,7 @@ func init() { Aliases: []string{"upgrade"}, Usage: "Update a mod (or all mods) in the modpack", Action: func(c *cli.Context) error { - // TODO: implement - return nil + return cmdUpdate(core.FlagsFromContext(c), c.Args().Get(0)) }, }, cli.Command{ Name: "refresh", @@ -121,3 +120,98 @@ func cmdRefresh(flags core.Flags) error { fmt.Println("Index refreshed!") return nil } + +func cmdUpdate(flags core.Flags, mod string) error { + fmt.Println("Loading modpack...") + pack, err := core.LoadPack(flags) + if err != nil { + return cli.NewExitError(err, 1) + } + index, err := pack.LoadIndex() + if err != nil { + return cli.NewExitError(err, 1) + } + mcVersion, err := pack.GetMCVersion() + if err != nil { + return cli.NewExitError(err, 1) + } + + multiple := false + var singleUpdatedName string + if len(mod) == 0 || mod == "*" { + multiple = true + // TODO: implement + return cli.NewExitError("Not implemented yet!", 1) + } else { + modPath, ok := index.FindMod(mod) + if !ok { + return cli.NewExitError("You don't have this mod installed.", 1) + } + modData, err := core.LoadMod(modPath) + if err != nil { + return cli.NewExitError(err, 1) + } + singleUpdatedName = modData.Name + updaterFound := false + for k := range modData.Update { + updater, ok := core.Updaters[k] + if !ok { + continue + } + updaterFound = true + + check, err := updater.CheckUpdate([]core.Mod{modData}, mcVersion) + if err != nil { + return cli.NewExitError(err, 1) + } + if len(check) != 1 { + return cli.NewExitError("Invalid update check response", 1) + } + + if check[0].UpdateAvailable { + fmt.Printf("Update available: %s\n", check[0].UpdateString) + + err = updater.DoUpdate([]*core.Mod{&modData}, []interface{}{check[0].CachedState}) + if err != nil { + return cli.NewExitError(err, 1) + } + + format, hash, err := modData.Write() + if err != nil { + return cli.NewExitError(err, 1) + } + err = index.RefreshFileWithHash(modPath, format, hash, true) + if err != nil { + return cli.NewExitError(err, 1) + } + } else { + fmt.Printf("\"%s\" is already up to date!\n", modData.Name) + return nil + } + + break + } + if !updaterFound { + return cli.NewExitError("A supported update system for this mod cannot be found.", 1) + } + } + + err = index.Write() + if err != nil { + return cli.NewExitError(err, 1) + } + err = pack.UpdateIndexHash() + if err != nil { + return cli.NewExitError(err, 1) + } + err = pack.Write() + if err != nil { + return cli.NewExitError(err, 1) + } + if multiple { + fmt.Println("Mods updated!") + } else { + fmt.Printf("\"%s\" updated!\n", singleUpdatedName) + } + return nil +}