Actually write mod files

but aaa it doesn't use the struct tags properly so things are bad
This commit is contained in:
comp500
2019-05-11 14:32:14 +01:00
parent 3fdac51d22
commit ef98591d02
4 changed files with 100 additions and 36 deletions

View File

@@ -1,23 +1,27 @@
package core
import (
"errors"
"os"
"github.com/BurntSushi/toml"
)
// Mod stores metadata about a mod. This is written to a TOML file for each mod.
type Mod struct {
metaFilename string // The filename for the metadata file, used as an ID
Name string `toml:"name"`
FileName string `toml:"filename"`
Side string `toml:"side,omitempty"`
Optional bool `toml:"optional,omitempty"`
Download struct {
URL string `toml:"url"`
HashFormat string `toml:"hash-format"`
Hash string `toml:"hash"`
} `toml:"download"`
Update map[string]interface{} `toml:"update"`
metaFile string // The file for the metadata file, used as an ID
Name string `toml:"name"`
FileName string `toml:"filename"`
Side string `toml:"side,omitempty"`
Optional bool `toml:"optional,omitempty"`
Download ModDownload `toml:"download"`
Update map[string]interface{} `toml:"update"`
}
// ModDownload specifies how to download the mod file
type ModDownload struct {
URL string `toml:"url"`
HashFormat string `toml:"hash-format"`
Hash string `toml:"hash"`
}
// The three possible values of Side (the side that the mod is on) are "server", "client", and "both".
@@ -46,10 +50,26 @@ func LoadMod(modFile string) (Mod, error) {
return mod, errors.New("Update plugin " + k + " not found!")
}
}
mod.metaFile = modFile
return mod, nil
}
func (m Mod) Write() {
// SetMetaName sets the mod metadata file from a given file name (to be put in the mods folder)
func (m *Mod) SetMetaName(metaName string, flags Flags) {
m.metaFile = ResolveMod(metaName, flags)
}
// Write saves the mod file
func (m Mod) Write() error {
f, err := os.Create(m.metaFile)
if err != nil {
return err
}
defer f.Close()
enc := toml.NewEncoder(f)
// Disable indentation
enc.Indent = ""
return enc.Encode(m)
}

View File

@@ -94,7 +94,7 @@ func (pack Pack) Write() error {
func (pack Pack) GetMCVersion() (string, error) {
mcVersion, ok := pack.Versions["minecraft"]
if !ok {
return "", errors.New("No Minecraft version specified in modpack!")
return "", errors.New("no minecraft version specified in modpack")
}
return mcVersion, nil
}