2019-05-05 22:12:15 +00:00
|
|
|
package doodads
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-06-27 22:07:34 +00:00
|
|
|
"runtime"
|
2019-05-05 22:12:15 +00:00
|
|
|
"strings"
|
|
|
|
|
2019-06-27 22:07:34 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/bindata"
|
2019-06-24 00:52:48 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/branding"
|
2019-05-05 22:12:15 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/enum"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/filesystem"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/log"
|
|
|
|
"git.kirsle.net/apps/doodle/pkg/userdir"
|
2019-06-27 22:07:34 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/wasm"
|
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.
|
|
|
|
if files, err := bindata.AssetDir("assets/doodads"); err == nil {
|
|
|
|
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...)
|
|
|
|
return names, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFile reads a doodad file from disk, checking a few locations.
|
|
|
|
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?
|
|
|
|
if jsonData, err := bindata.Asset(filename); err == nil {
|
|
|
|
return FromJSON(filename, jsonData)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WASM: try the file over HTTP ajax request.
|
|
|
|
if runtime.GOOS == "js" {
|
|
|
|
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
|
|
|
|
|
2019-05-05 22:12:15 +00:00
|
|
|
// bin, err := m.ToBinary()
|
|
|
|
bin, err := d.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save it to their profile directory.
|
|
|
|
filename = userdir.DoodadPath(filename)
|
|
|
|
log.Info("Write Doodad: %s", filename)
|
|
|
|
err = ioutil.WriteFile(filename, bin, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("doodads.WriteFile: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|