From adcde0569336f17cbed5886004c4221d41e46749 Mon Sep 17 00:00:00 2001 From: comp500 Date: Sat, 15 Jun 2019 00:40:56 +0100 Subject: [PATCH] Move locating mods to index --- core/index.go | 15 +++++++++++++++ curseforge/curseforge.go | 25 +++++++++++++++++++++++-- main.go | 15 +++++++++------ 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/core/index.go b/core/index.go index 4619925..e53e1d5 100644 --- a/core/index.go +++ b/core/index.go @@ -8,6 +8,7 @@ import ( "path/filepath" "sort" "time" + "strings" "github.com/BurntSushi/toml" "github.com/vbauerster/mpb/v4" @@ -266,3 +267,17 @@ func (in *Index) RefreshFileWithHash(path, format, hash string, mod bool) error in.resortIndex() return nil } + +// FindMod finds a mod in the index and returns it's path and whether it has been found +func (in Index) FindMod(modName string) (string, bool) { + for _, v := range in.Files { + if v.MetaFile { + _, file := filepath.Split(v.File); + fileTrimmed := strings.TrimSuffix(file, ModExtension) + if fileTrimmed == modName { + return v.File, true + } + } + } + return "", false +} \ No newline at end of file diff --git a/curseforge/curseforge.go b/curseforge/curseforge.go index 17abfdb..945801f 100644 --- a/curseforge/curseforge.go +++ b/curseforge/curseforge.go @@ -29,7 +29,8 @@ func init() { return cmdImport(core.FlagsFromContext(c), c.Args().Get(0)) }, }, { - Name: "open", + Name: "open", + // TODO: change semantics to "project" rather than "mod", as this supports texture packs and misc content as well Usage: "Open the project page for a curseforge mod in your browser", Aliases: []string{"doc"}, Action: func(c *cli.Context) error { @@ -143,7 +144,21 @@ func cmdDoc(flags core.Flags, mod string) error { return cli.NewExitError("You must specify a mod.", 1) } - modData, err := core.LoadMod(core.ResolveMod(mod, flags)) + 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) + } + resolvedMod, ok := index.FindMod(mod) + if !ok { + // TODO: should this auto-refresh??????? + return cli.NewExitError("You don't have this mod installed.", 1) + } + modData, err := core.LoadMod(resolvedMod) if err != nil { return cli.NewExitError(err, 1) } @@ -152,6 +167,7 @@ func cmdDoc(flags core.Flags, mod string) error { return cli.NewExitError("This mod doesn't seem to be a curseforge mod!", 1) } cfUpdateData := updateData.(cfUpdater) + fmt.Println("Opening browser...") url := "https://minecraft.curseforge.com/projects/" + strconv.Itoa(cfUpdateData.ProjectID) err = open.Start(url) if err != nil { @@ -178,6 +194,11 @@ type cfUpdater struct { func (u cfUpdater) DoUpdate(mod core.Mod) (bool, error) { // TODO: implement updating + // modInfoData, err := getModInfo(u.ProjectID) + // if err != nil { + // return false, err + // } + return false, nil } diff --git a/main.go b/main.go index 4a90774..061bcac 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,5 @@ package main + import ( "fmt" "log" @@ -52,11 +53,6 @@ func cmdDelete(flags core.Flags, mod string) error { if len(mod) == 0 { return cli.NewExitError("You must specify a mod.", 1) } - resolvedMod := core.ResolveMod(mod, flags) - err := os.Remove(resolvedMod) - if err != nil { - return cli.NewExitError(err, 1) - } fmt.Println("Loading modpack...") pack, err := core.LoadPack(flags) if err != nil { @@ -66,6 +62,14 @@ func cmdDelete(flags core.Flags, mod string) error { if err != nil { return cli.NewExitError(err, 1) } + resolvedMod, ok := index.FindMod(mod) + if !ok { + return cli.NewExitError("You don't have this mod installed.", 1) + } + err = os.Remove(resolvedMod) + if err != nil { + return cli.NewExitError(err, 1) + } fmt.Println("Removing mod from index...") err = index.RemoveFile(resolvedMod) if err != nil { @@ -117,4 +121,3 @@ func cmdRefresh(flags core.Flags) error { fmt.Println("Index refreshed!") return nil } -