doodle/pkg/level/filesystem.go
Noah Petherbridge c5e3fc297c Manage Embedded Files In Levels
In the Level Editor, the "Level->Attached files" menu opens the
FileSystem Window, which shows a paginated list of attached files and a
"Delete" button to remove them.

- Custom doodads which also exist locally can be deleted from the
  level's filesystem at any time.
- If a custom doodad does NOT exist locally, and one of them is still
  placed somewhere within the level, you can not delete it.
- You can't delete the custom wallpaper image IF the level is still
  using it. Change to a default wallpaper and then you can delete the
  custom wallpaper image.
2021-06-13 16:03:32 -07:00

82 lines
1.5 KiB
Go

package level
import (
"errors"
"sort"
"strings"
)
// 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 == nil || l.Files == nil {
return files
}
for name := range l.Files {
files = append(files, name)
}
sort.Strings(files)
return files
}
// ListFilesAt returns the list of files having a common prefix.
func (l *Level) ListFilesAt(prefix string) []string {
var (
files = l.ListFiles()
match = []string{}
)
for _, name := range files {
if strings.HasPrefix(name, prefix) {
match = append(match, name)
}
}
return match
}