doodle/pkg/doodads/json.go

70 lines
1.5 KiB
Go
Raw Normal View History

package doodads
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
)
// ToJSON serializes the doodad as JSON.
func (d *Doodad) ToJSON() ([]byte, error) {
out := bytes.NewBuffer([]byte{})
encoder := json.NewEncoder(out)
encoder.SetIndent("", "\t")
err := encoder.Encode(d)
return out.Bytes(), err
}
Bindata: Embedding Doodads and Levels (for WASM) * Use `go-bindata` to embed built-in doodads and levels directly into the Doodle binary. `make bindata` produces the bindata source file. * Add `FromJSON()` method to Levels and Doodads to load objects from JSON strings in memory (for bindata built-ins or WASM ajax requests) * Update file loading functions to check the embedded bindata files. * pkg/config.go#EditFile: * Supports editing a level from bindata (TODO: remove this support) * If the "assets/levels/%(simple-name.level)" exists in bindata, edits that drawing. * No such support for editing built-in doodads. * WASM has no filesystem access to edit files except built-in levels (yet) * pkg/doodads#ListDoodads: * Prepends built-in doodads from bindata to the returned list. * WASM: no filesystem access so gets only the built-ins. * pkg/doodads#LoadFile: * Checks built-in bindata store first for doodad files. * WASM: tries an HTTP request if not found in bindata but can go no further if not found (no filesystem access) * pkg/filesystem#FindFile: * This function finds a level/doodad by checking all the places. * If the level or doodad exists in bindata built-in, always returns its system path like "assets/doodads/test.doodad" * WASM: always returns the built-in candidate path even if not found in bindata so that ajax GET can be attempted. * pkg/level#ListSystemLevels: * New function that lists the system level files, similar to the equivalent doodads function. * Prepends the bindata built-in level files. * WASM: only returns the built-ins (no filesystem support) * Desktop: also lists and returns the assets/levels/ directory. * pkg/level#LoadFile: * Like the doodads.LoadFile, tries from built-in bindata first, then ajax request (WASM) before accessing the filesystem (desktop) * Menu Scene: TODO, list the built-in levels in the Load Level menu. This feature will soon go away when WASM gets its own storage for user levels (localStorage instead of filesystem)
2019-06-27 22:07:34 +00:00
// FromJSON loads a doodad from JSON string.
func FromJSON(filename string, data []byte) (*Doodad, error) {
var doodad = &Doodad{}
err := json.Unmarshal(data, doodad)
// Inflate the chunk metadata to map the pixels to their palette indexes.
doodad.Filename = filepath.Base(filename)
doodad.Inflate()
return doodad, err
}
// WriteJSON writes a Doodad to JSON on disk.
func (d *Doodad) WriteJSON(filename string) error {
json, err := d.ToJSON()
if err != nil {
return fmt.Errorf("Doodad.WriteJSON: JSON encode error: %s", err)
}
err = ioutil.WriteFile(filename, json, 0755)
if err != nil {
return fmt.Errorf("Doodad.WriteJSON: WriteFile error: %s", err)
}
d.Filename = filepath.Base(filename)
return nil
}
// LoadJSON loads a map from JSON file.
func LoadJSON(filename string) (*Doodad, error) {
fh, err := os.Open(filename)
if err != nil {
return nil, err
}
defer fh.Close()
// Decode the JSON file from disk.
d := New(0)
decoder := json.NewDecoder(fh)
err = decoder.Decode(&d)
if err != nil {
return d, fmt.Errorf("doodad.LoadJSON: JSON decode error: %s", err)
}
// Inflate the chunk metadata to map the pixels to their palette indexes.
d.Filename = filepath.Base(filename)
d.Inflate()
return d, err
}