doodle/pkg/level/filesystem.go
Noah Petherbridge 640e75ba4d Custom Wallpapers for Levels
* You can now browse for a custom wallpaper image to use with your
  levels. A platform-native file picker dialog is used (no WASM support)
* In the New/Edit Level Properties dialog, the Wallpaper drop-down
  includes an option to browse for a custom map.
* When editing an existing level: the wallpaper takes effect immediately
  in your level once the file is picked. For NEW levels, the wallpaper
  will appear once the "Continue" button is pressed.
* All common image types supported: png, jpeg, gif.
* The wallpaper is embedded in the level using the filepath
  "assets/wallpapers/custom.b64img" as a Base64-encoded blob of the
  image data.
* The `doodad show` command will list the names and sizes of files
  embedded in levels. `doodad show --attachment <name>` will get an
  attachment and print it to the console window.
* To extract a wallpaper image from a level:
  `doodad show -a assets/wallpapers/custom.b64img | base64 -d > out.png`
2021-06-06 18:59:04 -07:00

66 lines
1.2 KiB
Go

package level
import (
"errors"
"sort"
)
// FileSystem embeds a map of files inside a parent drawing.
type FileSystem map[string]File
// File holds details about a file in the FileSystem.
type File struct {
Data []byte `json:"data"`
}
// SetFile sets a file's data in the level.
func (l *Level) SetFile(filename string, data []byte) {
if l.Files == nil {
l.Files = map[string]File{}
}
l.Files[filename] = File{
Data: data,
}
}
// GetFile looks up an embedded file.
func (l *Level) GetFile(filename string) ([]byte, error) {
if l.Files == nil {
l.Files = map[string]File{}
}
if result, ok := l.Files[filename]; ok {
return result.Data, nil
}
return []byte{}, errors.New("not found")
}
// DeleteFile removes an embedded file.
func (l *Level) DeleteFile(filename string) bool {
if l.Files == nil {
l.Files = map[string]File{}
}
if _, ok := l.Files[filename]; ok {
delete(l.Files, filename)
return true
}
return false
}
// ListFiles returns the list of all embedded file names, alphabetically.
func (l *Level) ListFiles() []string {
var files []string
if l.Files == nil {
return files
}
for name := range l.Files {
files = append(files, name)
}
sort.Strings(files)
return files
}