Mod loading, disgusting struct parsing

This commit is contained in:
comp500 2019-04-27 16:54:27 +01:00
parent db7c837bfa
commit 5f92843226
No known key found for this signature in database
GPG Key ID: 214C822FFEC586B5
5 changed files with 87 additions and 7 deletions

15
core/interfaces.go Normal file
View File

@ -0,0 +1,15 @@
package core
// UpdateParsers stores all the update parsers that packwiz can use. Add your own update systems to this map.
var UpdateParsers map[string]UpdateParser = make(map[string]UpdateParser)
// UpdateParser takes an unparsed interface{}, and returns an Updater for a mod file
type UpdateParser interface {
ParseUpdate(interface{}) (Updater, error)
}
// Updater checks for and does updates on a mod
type Updater interface {
// DoUpdate returns true if an update was done, false otherwise
DoUpdate(Mod) (bool, error)
}

View File

@ -1,5 +1,9 @@
package core package core
import "github.com/BurntSushi/toml" import (
"errors"
"github.com/BurntSushi/toml"
)
// Mod stores metadata about a mod. This is written to a TOML file for each mod. // Mod stores metadata about a mod. This is written to a TOML file for each mod.
type Mod struct { type Mod struct {
@ -13,7 +17,7 @@ type Mod struct {
HashFormat string `toml:"hash-format"` HashFormat string `toml:"hash-format"`
Hash string `toml:"hash"` Hash string `toml:"hash"`
} `toml:"download"` } `toml:"download"`
Update map[string]toml.Primitive `toml:"update"` Update map[string]interface{} `toml:"update"`
} }
// The three possible values of Side (the side that the mod is on) are "server", "client", and "both". // The three possible values of Side (the side that the mod is on) are "server", "client", and "both".
@ -23,6 +27,28 @@ const (
UniversalSide = "both" UniversalSide = "both"
) )
// LoadMod attempts to load a mod file from a path
func LoadMod(modFile string) (Mod, error) {
var mod Mod
if _, err := toml.DecodeFile(modFile, &mod); err != nil {
return Mod{}, err
}
// Horrible reflection library to convert to Updaters
for k, v := range mod.Update {
updateParser, ok := UpdateParsers[k]
if ok {
updater, err := updateParser.ParseUpdate(v)
if err != nil {
return mod, err
}
mod.Update[k] = updater
} else {
return mod, errors.New("Update plugin " + k + " not found!")
}
}
return mod, nil
}
func (m Mod) Write() { func (m Mod) Write() {
} }

View File

@ -1,9 +1,9 @@
package curseforge package curseforge
import ( import (
"fmt" "fmt"
"github.com/comp500/packwiz/core" "github.com/comp500/packwiz/core"
"github.com/mitchellh/mapstructure"
"github.com/urfave/cli" "github.com/urfave/cli"
) )
@ -11,9 +11,45 @@ func init() {
core.Commands = append(core.Commands, cli.Command{ core.Commands = append(core.Commands, cli.Command{
Name: "curseforge", Name: "curseforge",
Usage: "Manage curseforge-based mods", Usage: "Manage curseforge-based mods",
Subcommands: []cli.Command{{
Name: "install",
Usage: "Install a mod from a curseforge URL, slug or ID",
Aliases: []string{"add", "get"},
Action: func(c *cli.Context) error {
return cmdInstall(core.FlagsFromContext(c), c.Args().Get(0))
},
}, {
Name: "import",
Usage: "Import an installed curseforge modpack",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
fmt.Println("Not implemented yet!") fmt.Println("Not implemented yet!")
return nil return nil
}, },
}},
}) })
core.UpdateParsers["curseforge"] = cfUpdateParser{}
} }
func cmdInstall(flags core.Flags, mod string) error {
if len(mod) == 0 {
return cli.NewExitError("You must specify a mod.", 1)
}
fmt.Println("Not implemented yet!")
return nil
}
type cfUpdateParser struct{}
func (u cfUpdateParser) ParseUpdate(updateUnparsed interface{}) (core.Updater, error) {
var updater cfUpdater
err := mapstructure.Decode(updateUnparsed, &updater)
return updater, err
}
type cfUpdater struct {
ProjectID int `mapstructure:"project-id"`
}
func (u cfUpdater) DoUpdate(mod core.Mod) (bool, error) {
return false, nil
}

1
go.mod
View File

@ -2,5 +2,6 @@ module github.com/comp500/packwiz
require ( require (
github.com/BurntSushi/toml v0.3.1 github.com/BurntSushi/toml v0.3.1
github.com/mitchellh/mapstructure v1.1.2
github.com/urfave/cli v1.20.0 github.com/urfave/cli v1.20.0
) )

2
go.sum
View File

@ -1,4 +1,6 @@
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw= github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=