2019-07-07 06:28:11 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-01-03 06:12:20 +00:00
|
|
|
"os"
|
2021-09-04 04:35:12 +00:00
|
|
|
"strconv"
|
2020-01-03 06:12:20 +00:00
|
|
|
"strings"
|
2019-07-07 06:28:11 +00:00
|
|
|
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/doodads"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/log"
|
2021-09-04 04:35:12 +00:00
|
|
|
"git.kirsle.net/go/render"
|
2020-11-15 23:20:15 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2019-07-07 06:28:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// EditDoodad allows writing doodad metadata.
|
2020-06-05 06:11:03 +00:00
|
|
|
var EditDoodad *cli.Command
|
2019-07-07 06:28:11 +00:00
|
|
|
|
|
|
|
func init() {
|
2020-06-05 06:11:03 +00:00
|
|
|
EditDoodad = &cli.Command{
|
2019-07-07 06:28:11 +00:00
|
|
|
Name: "edit-doodad",
|
|
|
|
Usage: "update metadata for a Doodad file",
|
|
|
|
ArgsUsage: "<filename.doodad>",
|
|
|
|
Flags: []cli.Flag{
|
2020-06-05 06:11:03 +00:00
|
|
|
&cli.BoolFlag{
|
2020-06-05 07:26:48 +00:00
|
|
|
Name: "quiet",
|
|
|
|
Aliases: []string{"q"},
|
|
|
|
Usage: "limit output (don't show doodad data at the end)",
|
2019-07-07 06:50:38 +00:00
|
|
|
},
|
2020-06-05 06:11:03 +00:00
|
|
|
&cli.StringFlag{
|
2019-07-07 06:28:11 +00:00
|
|
|
Name: "title",
|
|
|
|
Usage: "set the doodad title",
|
|
|
|
},
|
2020-06-05 06:11:03 +00:00
|
|
|
&cli.StringFlag{
|
2019-07-07 06:28:11 +00:00
|
|
|
Name: "author",
|
|
|
|
Usage: "set the doodad author",
|
|
|
|
},
|
2021-09-04 04:35:12 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "hitbox",
|
|
|
|
Usage: "set the doodad hitbox (X,Y,W,H or W,H format)",
|
|
|
|
},
|
2020-06-05 06:11:03 +00:00
|
|
|
&cli.StringSliceFlag{
|
2020-06-05 07:26:48 +00:00
|
|
|
Name: "tag",
|
|
|
|
Aliases: []string{"t"},
|
|
|
|
Usage: "set a key/value tag on the doodad, in key=value format. Empty value deletes the tag.",
|
2020-01-03 06:12:20 +00:00
|
|
|
},
|
2020-06-05 06:11:03 +00:00
|
|
|
&cli.BoolFlag{
|
2019-07-07 06:28:11 +00:00
|
|
|
Name: "hide",
|
|
|
|
Usage: "Hide the doodad from the palette",
|
|
|
|
},
|
2020-06-05 06:11:03 +00:00
|
|
|
&cli.BoolFlag{
|
2019-07-07 06:28:11 +00:00
|
|
|
Name: "unhide",
|
|
|
|
Usage: "Unhide the doodad from the palette",
|
|
|
|
},
|
2020-06-05 06:11:03 +00:00
|
|
|
&cli.BoolFlag{
|
2019-07-07 06:28:11 +00:00
|
|
|
Name: "lock",
|
|
|
|
Usage: "write-lock the level file",
|
|
|
|
},
|
2020-06-05 06:11:03 +00:00
|
|
|
&cli.BoolFlag{
|
2019-07-07 06:28:11 +00:00
|
|
|
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(
|
2019-07-07 06:28:11 +00:00
|
|
|
"Usage: doodad edit-doodad <filename.doodad>",
|
|
|
|
1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-06-05 06:11:03 +00:00
|
|
|
var filenames = c.Args().Slice()
|
2019-07-07 06:28:11 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-07-07 06:50:38 +00:00
|
|
|
log.Info("Edit Doodad: %s", filename)
|
2019-07-07 06:28:11 +00:00
|
|
|
|
|
|
|
/***************************
|
|
|
|
* 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
|
|
|
|
}
|
|
|
|
|
2021-09-04 04:35:12 +00:00
|
|
|
if c.String("hitbox") != "" {
|
|
|
|
// Setting a hitbox, parse it out.
|
|
|
|
parts := strings.Split(c.String("hitbox"), ",")
|
|
|
|
var ints []int
|
|
|
|
for _, part := range parts {
|
|
|
|
a, err := strconv.Atoi(strings.TrimSpace(part))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ints = append(ints, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ints) == 2 {
|
|
|
|
dd.Hitbox = render.NewRect(ints[0], ints[1])
|
|
|
|
modified = true
|
|
|
|
} else if len(ints) == 4 {
|
|
|
|
dd.Hitbox = render.Rect{
|
|
|
|
X: ints[0],
|
|
|
|
Y: ints[1],
|
|
|
|
W: ints[2],
|
|
|
|
H: ints[3],
|
|
|
|
}
|
|
|
|
modified = true
|
|
|
|
} else {
|
|
|
|
return cli.Exit("Hitbox should be in X,Y,W,H or just W,H format, 2 or 4 numbers.", 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 06:12:20 +00:00
|
|
|
// Tags.
|
|
|
|
tags := c.StringSlice("tag")
|
|
|
|
if len(tags) > 0 {
|
|
|
|
for _, tag := range tags {
|
|
|
|
parts := strings.SplitN(tag, "=", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
log.Error("--tag: must be in format `key=value`. Value may be blank to delete a tag. len=%d", len(parts))
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
key = parts[0]
|
|
|
|
value = parts[1]
|
|
|
|
)
|
|
|
|
if value == "" {
|
|
|
|
log.Debug("Delete tag '%s'", key)
|
|
|
|
delete(dd.Tags, key)
|
|
|
|
} else {
|
|
|
|
log.Debug("Set tag '%s' to '%s'", key, value)
|
|
|
|
dd.Tags[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
modified = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-07 06:28:11 +00:00
|
|
|
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 {
|
2020-01-03 06:12:20 +00:00
|
|
|
if err := dd.WriteJSON(filename); err != nil {
|
2021-09-04 04:35:12 +00:00
|
|
|
return cli.Exit(fmt.Sprintf("Write error: %s", err), 1)
|
2019-07-07 06:28:11 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Warn("Note: No changes made to level")
|
|
|
|
}
|
|
|
|
|
2019-07-07 06:50:38 +00:00
|
|
|
if c.Bool("quiet") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-07 06:28:11 +00:00
|
|
|
return showDoodad(c, filename)
|
|
|
|
}
|