doodle/cmd/doodad/commands/edit_level.go

166 lines
3.6 KiB
Go
Raw Permalink Normal View History

package commands
import (
"fmt"
"git.kirsle.net/apps/doodle/pkg/level"
"git.kirsle.net/apps/doodle/pkg/log"
"git.kirsle.net/go/render"
2020-11-15 23:20:15 +00:00
"github.com/urfave/cli/v2"
)
// EditLevel allows writing level metadata.
2020-06-05 06:11:03 +00:00
var EditLevel *cli.Command
func init() {
2020-06-05 06:11:03 +00:00
EditLevel = &cli.Command{
Name: "edit-level",
Usage: "update metadata for a Level file",
ArgsUsage: "<filename.level>",
Flags: []cli.Flag{
2020-06-05 06:11:03 +00:00
&cli.BoolFlag{
Name: "quiet",
Aliases: []string{"q"},
Usage: "limit output (don't show doodad data at the end)",
},
2020-06-05 06:11:03 +00:00
&cli.StringFlag{
Name: "title",
Usage: "set the level title",
},
2020-06-05 06:11:03 +00:00
&cli.StringFlag{
Name: "author",
Usage: "set the level author",
},
2020-06-05 06:11:03 +00:00
&cli.StringFlag{
Name: "password",
Usage: "set the level password",
},
2020-06-05 06:11:03 +00:00
&cli.StringFlag{
Name: "type",
Usage: "set the page type. One of: Unbounded, Bounded, NoNegativeSpace, Bordered",
},
2020-06-05 06:11:03 +00:00
&cli.StringFlag{
Name: "max-size",
Usage: "set the page max size (WxH format, like 2550x3300)",
},
2020-06-05 06:11:03 +00:00
&cli.StringFlag{
Name: "wallpaper",
Usage: "set the wallpaper filename",
},
2020-06-05 06:11:03 +00:00
&cli.BoolFlag{
Name: "lock",
Usage: "write-lock the level file",
},
2020-06-05 06:11:03 +00:00
&cli.BoolFlag{
Name: "unlock",
Usage: "remove the write-lock on the level file",
},
},
Action: func(c *cli.Context) error {
if c.NArg() < 1 {
2021-09-04 04:35:12 +00:00
return cli.Exit(
"Usage: doodad edit-level <filename.level>",
1,
)
}
2020-06-05 06:11:03 +00:00
var filenames = c.Args().Slice()
for _, filename := range filenames {
if err := editLevel(c, filename); err != nil {
log.Error("%s: %s", filename, err)
}
}
return nil
},
}
}
func editLevel(c *cli.Context, filename string) error {
var modified bool
lvl, err := level.LoadJSON(filename)
if err != nil {
return fmt.Errorf("Failed to load %s: %s", filename, err)
}
log.Info("File: %s", filename)
/***************************
* Update level properties *
***************************/
if c.String("title") != "" {
lvl.Title = c.String("title")
log.Info("Set title: %s", lvl.Title)
modified = true
}
if c.String("author") != "" {
lvl.Author = c.String("author")
log.Info("Set author: %s", lvl.Author)
modified = true
}
if c.String("password") != "" {
lvl.Password = c.String("password")
log.Info("Updated level password")
modified = true
}
if c.String("max-size") != "" {
w, h, err := render.ParseResolution(c.String("max-size"))
if err != nil {
log.Error("-max-size: %s", err)
} else {
lvl.MaxWidth = int64(w)
lvl.MaxHeight = int64(h)
modified = true
}
}
if c.Bool("lock") {
lvl.Locked = true
log.Info("Write lock enabled.")
modified = true
} else if c.Bool("unlock") {
lvl.Locked = false
log.Info("Write lock disabled.")
modified = true
}
if c.String("type") != "" {
if pageType, ok := level.PageTypeFromString(c.String("type")); ok {
lvl.PageType = pageType
log.Info("Page Type set to %s", pageType)
modified = true
} else {
log.Error("Invalid -type value. Should be like Unbounded, Bounded, NoNegativeSpace, Bordered")
}
}
if c.String("wallpaper") != "" {
lvl.Wallpaper = c.String("wallpaper")
log.Info("Set wallpaper: %s", c.String("wallpaper"))
modified = true
}
/******************************
* Save level changes to disk *
******************************/
if modified {
if err := lvl.WriteFile(filename); err != nil {
2021-09-04 04:35:12 +00:00
return cli.Exit(fmt.Sprintf("Write error: %s", err), 1)
}
} else {
log.Warn("Note: No changes made to level")
}
if c.Bool("quiet") {
return nil
}
return showLevel(c, filename)
}