2019-05-05 21:03:20 +00:00
|
|
|
package level
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2019-06-27 22:07:34 +00:00
|
|
|
"runtime"
|
2019-05-05 21:03:20 +00:00
|
|
|
"strings"
|
|
|
|
|
2021-07-14 01:02:57 +00:00
|
|
|
"git.kirsle.net/apps/doodle/assets"
|
2019-06-24 00:52:48 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/branding"
|
2019-05-05 21:03:20 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/enum"
|
2019-05-05 22:12:15 +00:00
|
|
|
"git.kirsle.net/apps/doodle/pkg/filesystem"
|
2019-05-05 21:03:20 +00:00
|
|
|
"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 21:03:20 +00:00
|
|
|
)
|
|
|
|
|
2019-06-27 22:07:34 +00:00
|
|
|
// ListSystemLevels returns a list of built-in levels.
|
|
|
|
func ListSystemLevels() ([]string, error) {
|
|
|
|
var names = []string{}
|
|
|
|
|
|
|
|
// Add the levels embedded inside the binary.
|
2021-07-14 01:02:57 +00:00
|
|
|
if levels, err := assets.AssetDir("assets/levels"); err == nil {
|
2019-06-27 22:07:34 +00:00
|
|
|
names = append(names, levels...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WASM
|
|
|
|
if runtime.GOOS == "js" {
|
|
|
|
// Return just the embedded ones, no filesystem access.
|
|
|
|
return names, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read filesystem for system levels.
|
|
|
|
files, err := ioutil.ReadDir(filesystem.SystemLevelsPath)
|
|
|
|
for _, file := range files {
|
|
|
|
name := file.Name()
|
|
|
|
if strings.HasSuffix(strings.ToLower(name), enum.DoodadExt) {
|
|
|
|
names = append(names, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return names, err
|
|
|
|
}
|
|
|
|
|
2019-05-05 21:03:20 +00:00
|
|
|
// LoadFile reads a level file from disk, checking a few locations.
|
|
|
|
func LoadFile(filename string) (*Level, error) {
|
|
|
|
if !strings.HasSuffix(filename, enum.LevelExt) {
|
|
|
|
filename += enum.LevelExt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search the system and user paths for this level.
|
2019-05-05 22:12:15 +00:00
|
|
|
filename, err := filesystem.FindFile(filename)
|
2019-05-05 21:03:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 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 {
|
2021-01-03 23:19:21 +00:00
|
|
|
log.Debug("Level %s: loaded from embedded bindata", filename)
|
2019-06-27 22:07:34 +00:00
|
|
|
return FromJSON(filename, jsonData)
|
|
|
|
}
|
|
|
|
|
2019-06-27 22:59:18 +00:00
|
|
|
// WASM: try the file from localStorage or HTTP ajax request.
|
2019-06-27 22:07:34 +00:00
|
|
|
if runtime.GOOS == "js" {
|
2019-06-27 22:59:18 +00:00
|
|
|
if result, ok := wasm.GetSession(filename); ok {
|
|
|
|
log.Info("recall level data from localStorage")
|
|
|
|
return FromJSON(filename, []byte(result))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ajax request.
|
2019-06-27 22:07:34 +00:00
|
|
|
jsonData, err := wasm.HTTPGet(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return FromJSON(filename, jsonData)
|
|
|
|
}
|
|
|
|
|
2022-01-09 21:16:29 +00:00
|
|
|
// Load as JSON.
|
2019-05-05 21:03:20 +00:00
|
|
|
if level, err := LoadJSON(filename); err == nil {
|
|
|
|
return level, nil
|
|
|
|
} else {
|
|
|
|
log.Warn(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, errors.New("invalid file type")
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteFile saves a level to disk in the user's config directory.
|
|
|
|
func (m *Level) WriteFile(filename string) error {
|
|
|
|
if !strings.HasSuffix(filename, enum.LevelExt) {
|
|
|
|
filename += enum.LevelExt
|
|
|
|
}
|
|
|
|
|
2019-06-24 00:52:48 +00:00
|
|
|
// Set the version information.
|
|
|
|
m.Version = 1
|
|
|
|
m.GameVersion = branding.Version
|
|
|
|
|
2019-07-07 03:31:50 +00:00
|
|
|
// Maintenance functions, clean up cruft before save.
|
2019-07-17 05:10:18 +00:00
|
|
|
m.PruneChunks()
|
2019-07-07 03:31:50 +00:00
|
|
|
m.PruneLinks()
|
|
|
|
|
2019-05-05 21:03:20 +00:00
|
|
|
bin, err := m.ToJSON()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save it to their profile directory.
|
|
|
|
filename = userdir.LevelPath(filename)
|
|
|
|
log.Info("Write Level: %s", filename)
|
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 21:03:20 +00:00
|
|
|
err = ioutil.WriteFile(filename, bin, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("level.WriteFile: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|