2021-06-13 21:53:21 +00:00
|
|
|
/*
|
|
|
|
Package publishing contains functionality for "publishing" a Level, which
|
|
|
|
involves the writing and reading of custom doodads embedded inside
|
|
|
|
the levels.
|
|
|
|
|
|
|
|
Free tiers of the game will not read or write embedded doodads into
|
|
|
|
levels.
|
|
|
|
*/
|
|
|
|
package publishing
|
|
|
|
|
|
|
|
import (
|
2022-01-18 02:51:11 +00:00
|
|
|
"errors"
|
2021-06-13 21:53:21 +00:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
2022-01-18 02:51:11 +00:00
|
|
|
"strings"
|
2021-06-13 21:53:21 +00:00
|
|
|
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/balance"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/doodads"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/level"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/license"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
|
2021-06-13 21:53:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
Level "Publishing" functions, involving the writing and reading of embedded
|
|
|
|
doodads within level files.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Publish writes a published level file, with embedded doodads included.
|
2022-01-18 02:51:11 +00:00
|
|
|
func Publish(lvl *level.Level) error {
|
|
|
|
// Not embedding doodads?
|
|
|
|
if !lvl.SaveDoodads {
|
|
|
|
if removed := lvl.DeleteFiles(balance.EmbeddedDoodadsBasePath); removed > 0 {
|
|
|
|
log.Info("Note: removed %d attached doodads because SaveDoodads is false", removed)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Registered games only.
|
|
|
|
if !license.IsRegistered() {
|
|
|
|
return errors.New("only registered versions of the game can attach doodads to levels")
|
|
|
|
}
|
|
|
|
|
2021-06-13 21:53:21 +00:00
|
|
|
// Get and embed the doodads.
|
|
|
|
builtins, customs := GetUsedDoodadNames(lvl)
|
2022-01-18 02:51:11 +00:00
|
|
|
var names = map[string]interface{}{}
|
|
|
|
if lvl.SaveBuiltins {
|
2021-06-13 21:53:21 +00:00
|
|
|
log.Debug("including builtins: %+v", builtins)
|
|
|
|
customs = append(customs, builtins...)
|
|
|
|
}
|
|
|
|
for _, filename := range customs {
|
|
|
|
log.Debug("Embed filename: %s", filename)
|
2022-01-18 02:51:11 +00:00
|
|
|
names[filename] = nil
|
|
|
|
|
2021-06-13 21:53:21 +00:00
|
|
|
doodad, err := doodads.LoadFromEmbeddable(filename, lvl)
|
|
|
|
if err != nil {
|
2022-01-18 02:51:11 +00:00
|
|
|
return fmt.Errorf("couldn't load doodad %s: %s", filename, err)
|
2021-06-13 21:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bin, err := doodad.Serialize()
|
|
|
|
if err != nil {
|
2022-01-18 02:51:11 +00:00
|
|
|
return fmt.Errorf("couldn't serialize doodad %s: %s", filename, err)
|
2021-06-13 21:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Embed it.
|
|
|
|
lvl.SetFile(balance.EmbeddedDoodadsBasePath+filename, bin)
|
|
|
|
}
|
|
|
|
|
2022-01-18 02:51:11 +00:00
|
|
|
// Trim any doodads not currently in the level.
|
|
|
|
for _, filename := range lvl.ListFilesAt(balance.EmbeddedDoodadsBasePath) {
|
|
|
|
basename := strings.TrimPrefix(filename, balance.EmbeddedDoodadsBasePath)
|
|
|
|
if _, ok := names[basename]; !ok {
|
|
|
|
log.Debug("Remove embedded doodad %s (cleanup)", basename)
|
|
|
|
lvl.DeleteFile(filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-06-13 21:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUsedDoodadNames returns the lists of doodad filenames in use in a level,
|
|
|
|
// bucketed by built-in or custom user doodads.
|
|
|
|
func GetUsedDoodadNames(lvl *level.Level) (builtin []string, custom []string) {
|
|
|
|
// Collect all the doodad names in use in this level.
|
|
|
|
unique := map[string]interface{}{}
|
|
|
|
names := []string{}
|
|
|
|
if lvl != nil {
|
|
|
|
for _, actor := range lvl.Actors {
|
|
|
|
if _, ok := unique[actor.Filename]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
unique[actor.Filename] = nil
|
|
|
|
names = append(names, actor.Filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Identify which of the doodads are built-ins.
|
|
|
|
// builtin = []string{}
|
|
|
|
builtinMap := map[string]interface{}{}
|
|
|
|
// custom := []string{}
|
|
|
|
if builtins, err := doodads.ListBuiltin(); err == nil {
|
|
|
|
for _, filename := range builtins {
|
|
|
|
if _, ok := unique[filename]; ok {
|
|
|
|
builtin = append(builtin, filename)
|
|
|
|
builtinMap[filename] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, name := range names {
|
|
|
|
if _, ok := builtinMap[name]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
custom = append(custom, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(builtin)
|
|
|
|
sort.Strings(custom)
|
|
|
|
|
|
|
|
return builtin, custom
|
|
|
|
}
|