doodle/cmd/doodad/commands/edit_doodad.go
Noah Petherbridge fd649b7ab1 Doodad CLI Tool Features; Write Lock and Hidden
* The `doodad` CLI tool got a lot of new commands:
  * `doodad show` to verbosely print details about Levels and Doodads.
  * `edit-level` and `edit-doodad` to update details about Levels and
    Doodads, such as their Title, Author, page type and size, etc.
* Doodads gain a `Hidden bool` that hides them from the palette in
  Editor Mode. The player character (Blue Azulian) is Hidden.
* Add some boolProps to the balance/ package and made a dynamic system
  to easily configure these with the in-game dev console.
  * Command: `boolProp list` returns available balance.boolProps
  * `boolProp <name>` returns the current value.
  * `boolProp <name> <true or false>` sets the value.
* The new boolProps are:
  * showAllDoodads: enable Hidden doodads on the palette UI (NOTE:
    reload the editor to take effect)
  * writeLockOverride: edit files that are write locked anyway
  * prettyJSON: pretty-format the JSON files saved by the game.
2019-07-06 23:28:11 -07:00

125 lines
2.5 KiB
Go

package commands
import (
"fmt"
"git.kirsle.net/apps/doodle/pkg/doodads"
"git.kirsle.net/apps/doodle/pkg/log"
"github.com/urfave/cli"
)
// EditDoodad allows writing doodad metadata.
var EditDoodad cli.Command
func init() {
EditDoodad = cli.Command{
Name: "edit-doodad",
Usage: "update metadata for a Doodad file",
ArgsUsage: "<filename.doodad>",
Flags: []cli.Flag{
cli.StringFlag{
Name: "title",
Usage: "set the doodad title",
},
cli.StringFlag{
Name: "author",
Usage: "set the doodad author",
},
cli.BoolFlag{
Name: "hide",
Usage: "Hide the doodad from the palette",
},
cli.BoolFlag{
Name: "unhide",
Usage: "Unhide the doodad from the palette",
},
cli.BoolFlag{
Name: "lock",
Usage: "write-lock the level file",
},
cli.BoolFlag{
Name: "unlock",
Usage: "remove the write-lock on the level file",
},
},
Action: func(c *cli.Context) error {
if c.NArg() < 1 {
return cli.NewExitError(
"Usage: doodad edit-doodad <filename.doodad>",
1,
)
}
var filenames = c.Args()
for _, filename := range filenames {
if err := editDoodad(c, filename); err != nil {
log.Error("%s: %s", filename, err)
}
}
return nil
},
}
}
func editDoodad(c *cli.Context, filename string) error {
var modified bool
dd, err := doodads.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") != "" {
dd.Title = c.String("title")
log.Info("Set title: %s", dd.Title)
modified = true
}
if c.String("author") != "" {
dd.Author = c.String("author")
log.Info("Set author: %s", dd.Author)
modified = true
}
if c.Bool("hide") {
dd.Hidden = true
log.Info("Marked doodad Hidden")
modified = true
} else if c.Bool("unhide") {
dd.Hidden = false
log.Info("Doodad is no longer Hidden")
modified = true
}
if c.Bool("lock") {
dd.Locked = true
log.Info("Write lock enabled.")
modified = true
} else if c.Bool("unlock") {
dd.Locked = false
log.Info("Write lock disabled.")
modified = true
}
/******************************
* Save level changes to disk *
******************************/
if modified {
if err := dd.WriteFile(filename); err != nil {
return cli.NewExitError(fmt.Sprintf("Write error: %s", err), 1)
}
} else {
log.Warn("Note: No changes made to level")
}
return showDoodad(c, filename)
}