doodle/pkg/campaign/files.go
Noah Petherbridge 38614ee280 Tighten Doodad JavaScript API, User Documentation
* Tightens up the surface area of API methods available to the
  JavaScript VMs for doodads. Variables and functions are carefully
  passed in one-by-one so the doodad script can only access intended
  functions and not snoop on undocumented APIs.
* Wrote tons of user documentation for Doodad Scripts: documented the
  full surface area of the exposed JavaScript API now that the surface
  area is known and limited.
* Early WIP code for the Campaign JSON
2020-04-21 23:50:45 -07:00

61 lines
1.5 KiB
Go

// Package campaign contains types and functions for the single player campaigns.
package campaign
import (
"io/ioutil"
"path/filepath"
"runtime"
"sort"
"git.kirsle.net/apps/doodle/pkg/bindata"
"git.kirsle.net/apps/doodle/pkg/filesystem"
"git.kirsle.net/apps/doodle/pkg/userdir"
)
// List returns the list of available campaign JSONs.
//
// It searches in:
// - The embedded bindata for built-in scenarios
// - Scenarios on disk at the assets/campaigns folder.
// - User-made scenarios at ~/doodle/campaigns.
func List() ([]string, error) {
var names []string
// List built-in bindata campaigns.
if files, err := bindata.AssetDir("assets/campaigns"); err == nil {
names = append(names, files...)
}
// WASM: only built-in campaigns, no filesystem access.
if runtime.GOOS == "js" {
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.SystemCampaignsPath)
for _, file := range files {
name := file.Name()
if filepath.Ext(name) == ".json" {
names = append(names, name)
}
}
// Append user campaigns.
userFiles, err := userdir.ListCampaigns()
names = append(names, userFiles...)
// 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, err
}