2019-05-05 22:12:15 +00:00
|
|
|
package doodads
|
|
|
|
|
|
|
|
import (
|
2021-06-13 21:53:21 +00:00
|
|
|
"errors"
|
2019-05-05 22:12:15 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-06-27 22:07:34 +00:00
|
|
|
"runtime"
|
2019-07-04 04:55:15 +00:00
|
|
|
"sort"
|
2019-05-05 22:12:15 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-09-24 22:17:25 +00:00
|
|
|
"git.kirsle.net/SketchyMaze/doodle/assets"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/branding"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/enum"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/filesystem"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/log"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/userdir"
|
|
|
|
"git.kirsle.net/SketchyMaze/doodle/pkg/wasm"
|
2019-05-05 22:12:15 +00:00
|
|
|
)
|
|
|
|
|
2021-06-13 21:53:21 +00:00
|
|
|
// Errors.
|
|
|
|
var (
|
|
|
|
ErrNotFound = errors.New("file not found")
|
|
|
|
)
|
|
|
|
|
2019-05-05 22:12:15 +00:00
|
|
|
// ListDoodads returns a listing of all available doodads between all locations,
|
|
|
|
// including user doodads.
|
|
|
|
func ListDoodads() ([]string, error) {
|
|
|
|
var names []string
|
|
|
|
|
2019-06-27 22:07:34 +00:00
|
|
|
// List doodads embedded into the binary.
|
2021-07-14 01:02:57 +00:00
|
|
|
if files, err := assets.AssetDir("assets/doodads"); err == nil {
|
2019-06-27 22:07:34 +00:00
|
|
|
names = append(names, files...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WASM
|
|
|
|
if runtime.GOOS == "js" {
|
|
|
|
// Return the array of doodads embedded in the bindata.
|
|
|
|
// TODO: append user doodads to the list.
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
|
2019-05-05 22:12:15 +00:00
|
|
|
// Read system-level doodads first. Ignore errors, if the system path is
|
|
|
|
// empty we still go on to read the user directory.
|
|
|
|
files, _ := ioutil.ReadDir(filesystem.SystemDoodadsPath)
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
name := file.Name()
|
|
|
|
if strings.HasSuffix(strings.ToLower(name), enum.DoodadExt) {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append user doodads.
|
|
|
|
userFiles, err := userdir.ListDoodads()
|
|
|
|
names = append(names, userFiles...)
|
2019-06-28 05:54:46 +00:00
|
|
|
|
|
|
|
// Deduplicate names.
|
|
|
|
var uniq = map[string]interface{}{}
|
|
|
|
var result []string
|
|
|
|
for _, name := range names {
|
|
|
|
if _, ok := uniq[name]; !ok {
|
|
|
|
uniq[name] = nil
|
|
|
|
result = append(result, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 04:55:15 +00:00
|
|
|
sort.Strings(result)
|
|
|
|
|
2019-06-28 05:54:46 +00:00
|
|
|
return result, err
|
2019-05-05 22:12:15 +00:00
|
|
|
}
|
|
|
|
|
WIP Publish Dialog + UI Improvements
* File->Publish Level in the Level Editor opens the Publish window,
where you can embed custom doodads into your level and export a
portable .level file you can share with others.
* Currently does not actually export a level file yet.
* The dialog lists all unique doodad names in use in your level, and
designates which are built-ins and which are custom (paginated).
* A checkbox would let the user embed built-in doodads into their level,
as well, locking it in to those versions and not using updated
versions from future game releases.
UI Improvements:
* Added styling for a "Primary" UI button, rendered in deep blue.
* Pop-up modals (Alert, Confirm) color their Ok button as Primary.
* The Enter key pressed during an Alert or Confirm modal will invoke its
default button and close the modal, corresponding to its Primary
button.
* The developer console is now opened with the tilde/grave key ` instead
of the Enter key, so that the Enter key is free to click through
modals.
* In the "Open/Edit Drawing" window, a "Browse..." button is added to
the level and doodad sections, spawning a native File Open dialog to
pick a .level or .doodad outside the config root.
2021-06-11 05:31:30 +00:00
|
|
|
// ListBuiltin returns a listing of all built-in doodads.
|
|
|
|
// Exactly like ListDoodads() but doesn't return user home folder doodads.
|
|
|
|
func ListBuiltin() ([]string, error) {
|
|
|
|
var names []string
|
|
|
|
|
|
|
|
// List doodads embedded into the binary.
|
2021-07-14 01:02:57 +00:00
|
|
|
if files, err := assets.AssetDir("assets/doodads"); err == nil {
|
WIP Publish Dialog + UI Improvements
* File->Publish Level in the Level Editor opens the Publish window,
where you can embed custom doodads into your level and export a
portable .level file you can share with others.
* Currently does not actually export a level file yet.
* The dialog lists all unique doodad names in use in your level, and
designates which are built-ins and which are custom (paginated).
* A checkbox would let the user embed built-in doodads into their level,
as well, locking it in to those versions and not using updated
versions from future game releases.
UI Improvements:
* Added styling for a "Primary" UI button, rendered in deep blue.
* Pop-up modals (Alert, Confirm) color their Ok button as Primary.
* The Enter key pressed during an Alert or Confirm modal will invoke its
default button and close the modal, corresponding to its Primary
button.
* The developer console is now opened with the tilde/grave key ` instead
of the Enter key, so that the Enter key is free to click through
modals.
* In the "Open/Edit Drawing" window, a "Browse..." button is added to
the level and doodad sections, spawning a native File Open dialog to
pick a .level or .doodad outside the config root.
2021-06-11 05:31:30 +00:00
|
|
|
names = append(names, files...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WASM
|
|
|
|
if runtime.GOOS == "js" {
|
|
|
|
// Return the array of doodads embedded in the bindata.
|
|
|
|
// TODO: append user doodads to the list.
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read system-level doodads first. Ignore errors, if the system path is
|
|
|
|
// empty we still go on to read the user directory.
|
|
|
|
files, _ := ioutil.ReadDir(filesystem.SystemDoodadsPath)
|
|
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
name := file.Name()
|
|
|
|
if strings.HasSuffix(strings.ToLower(name), enum.DoodadExt) {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deduplicate names.
|
|
|
|
var uniq = map[string]interface{}{}
|
|
|
|
var result []string
|
|
|
|
for _, name := range names {
|
|
|
|
if _, ok := uniq[name]; !ok {
|
|
|
|
uniq[name] = nil
|
|
|
|
result = append(result, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(result)
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2019-05-05 22:12:15 +00:00
|
|
|
// LoadFile reads a doodad file from disk, checking a few locations.
|
2021-06-13 21:53:21 +00:00
|
|
|
//
|
|
|
|
// It checks for embedded bindata, system-level doodads on the filesystem,
|
|
|
|
// and then user-owned doodads in their profile folder.
|
2019-05-05 22:12:15 +00:00
|
|
|
func LoadFile(filename string) (*Doodad, error) {
|
|
|
|
if !strings.HasSuffix(filename, enum.DoodadExt) {
|
|
|
|
filename += enum.DoodadExt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search the system and user paths for this level.
|
|
|
|
filename, err := filesystem.FindFile(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("doodads.LoadFile(%s): %s", filename, err)
|
|
|
|
}
|
|
|
|
|
2019-06-27 22:07:34 +00:00
|
|
|
// Do we have the file in bindata?
|
2021-07-14 01:02:57 +00:00
|
|
|
if jsonData, err := assets.Asset(filename); err == nil {
|
2019-06-27 22:07:34 +00:00
|
|
|
return FromJSON(filename, jsonData)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WASM: try the file over HTTP ajax request.
|
|
|
|
if runtime.GOOS == "js" {
|
2019-06-27 22:59:18 +00:00
|
|
|
if result, ok := wasm.GetSession(filename); ok {
|
|
|
|
log.Info("recall doodad data from localStorage")
|
|
|
|
return FromJSON(filename, []byte(result))
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: ajax load for doodads might not work, filesystem.FindFile returns
|
|
|
|
// the base file for WASM but for now force it to system doodads path
|
|
|
|
filename = "assets/doodads/" + filename
|
2019-06-27 22:07:34 +00:00
|
|
|
jsonData, err := wasm.HTTPGet(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return FromJSON(filename, jsonData)
|
2019-05-05 22:12:15 +00:00
|
|
|
}
|
|
|
|
|
2019-06-27 22:07:34 +00:00
|
|
|
// Load the JSON file from the filesystem.
|
|
|
|
return LoadJSON(filename)
|
2019-05-05 22:12:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteFile saves a doodad to disk in the user's config directory.
|
|
|
|
func (d *Doodad) WriteFile(filename string) error {
|
|
|
|
if !strings.HasSuffix(filename, enum.DoodadExt) {
|
|
|
|
filename += enum.DoodadExt
|
|
|
|
}
|
|
|
|
|
2019-06-24 00:52:48 +00:00
|
|
|
// Set the version information.
|
|
|
|
d.Version = 1
|
|
|
|
d.GameVersion = branding.Version
|
|
|
|
|
2024-05-24 22:03:32 +00:00
|
|
|
// Maintenance functions, clean up cruft before save.
|
|
|
|
if err := d.Vacuum(); err != nil {
|
|
|
|
log.Error("Vacuum level %s: %s", filename, err)
|
|
|
|
}
|
|
|
|
|
2019-05-05 22:12:15 +00:00
|
|
|
bin, err := d.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-27 22:59:18 +00:00
|
|
|
// WASM: place in localStorage.
|
|
|
|
if runtime.GOOS == "js" {
|
|
|
|
log.Info("wasm: write %s to localStorage", filename)
|
|
|
|
wasm.SetSession(filename, string(bin))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Desktop: write to disk.
|
2019-05-05 22:12:15 +00:00
|
|
|
filename = userdir.DoodadPath(filename)
|
2019-07-07 06:50:38 +00:00
|
|
|
log.Debug("Write Doodad: %s", filename)
|
2019-05-05 22:12:15 +00:00
|
|
|
err = ioutil.WriteFile(filename, bin, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("doodads.WriteFile: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-06-13 21:53:21 +00:00
|
|
|
|
|
|
|
// Serialize encodes a doodad to bytes and returns them, instead
|
|
|
|
// of writing to a file.
|
|
|
|
func (d *Doodad) Serialize() ([]byte, error) {
|
|
|
|
// Set the version information.
|
|
|
|
d.Version = 1
|
|
|
|
d.GameVersion = branding.Version
|
|
|
|
|
|
|
|
bin, err := d.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bin, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize loads a doodad from its bytes format.
|
|
|
|
func Deserialize(filename string, bin []byte) (*Doodad, error) {
|
|
|
|
return FromJSON(filename, bin)
|
|
|
|
}
|