Documentation
¶
Overview ¶
Package library is the opt-in standard asset library: every top folder of the asset root is a package (a collection of assets that loads, mounts and packs together), loaded into an asset.Scope the caller owns, with a catalog for file systems that cannot list directories and zip packs that are mounted while a scope that loaded them holds them.
The layout (doc/guide/engine/asset-library.md):
boot/ optional, tiny: mounted first, blocking (a loading screen) global/ required: loaded at boot into the global scope, never released forest/ a package: any name; levels, maps, sprites, prefabs ... temple/ a package other packages use
A package is named by its folder ("forest"). Packages use each other by referencing their files; loading one mounts the packs of the packages it uses. Keys never change: "forest/tiles/trees.tsj" is the same key whether the file is loose, embedded or inside the package's pack.
It is stdlib only plus asset. It knows asset kinds by file name (KindOf) but not their Go types: the engine (or a game) maps kinds to load functions with Library.SetLoader.
Goroutines ¶
Everything runs on the game goroutine except reads through Library.FS and Library.Mounted, which the asset workers may call concurrently; mounting publishes a new mount table atomically, so reads never lock.
Panics and errors ¶
Programmer errors panic with "library: " (unknown package, loading before Library.Attach). Bad files and a failing pack go through the asset pipeline: the asset or the Load fails.
Index ¶
- Constants
- func DiffFiles(want, got fs.FS) (missing, extra []string, err error)
- func Files(fsys fs.FS) ([]string, error)
- func PackageOf(p string) string
- func Scan(fsys fs.FS) (*Catalog, []Finding, error)
- func WritePacks(src fs.FS, out string) (*Catalog, []Finding, error)
- type Catalog
- type Config
- type Finding
- type Ignore
- type IgnoreError
- type IgnoredFile
- type Kind
- type Library
- func (l *Library) Attach(srv *asset.Server)
- func (l *Library) Catalog() *Catalog
- func (l *Library) FS() fs.FS
- func (l *Library) HoldPackage(sc *asset.Scope, name string) *Load
- func (l *Library) LoadPackage(sc *asset.Scope, name string) *Load
- func (l *Library) Mount(prefix string, fsys fs.FS)
- func (l *Library) MountPackage(sc *asset.Scope, name string) error
- func (l *Library) Mounted() []string
- func (l *Library) Package(name string) (*Package, bool)
- func (l *Library) SetLoader(k Kind, fn LoadFunc)
- func (l *Library) Unmount(prefix string, fsys fs.FS) bool
- func (l *Library) Update()
- type Load
- type LoadFunc
- type Pack
- type Package
- type PackageConfig
- type Severity
Constants ¶
const ( // CatalogFile is the catalog written by wpix assets. When it exists, // [Open] reads packages from it instead of listing directories. CatalogFile = "assets.catalog.json" // ConfigFile holds optional overrides (see [Config]). ConfigFile = "library.json" )
File names the library reads at its root.
const ( // GlobalDir is the required global package: loaded at boot into the // global scope and never released. GlobalDir = "global" // BootDir is the optional boot package: mounted before everything else, // blocking, so a startup screen can draw while global/ loads. It may // reference no other package. BootDir = "boot" )
Package folder names with a meaning at the library root. Every other top folder is a package of its own, except the reserved ones (PackageOf).
const CatalogVersion = 0
CatalogVersion is the catalog format Scan writes and Open reads. It stays 0 until the engine is released.
const IgnoreFile = ".assetignore"
IgnoreFile is the name of the files that keep files out of a library: one in the root or in any folder below it, with .gitignore syntax and meaning, relative to the folder that holds it.
const PacksDir = "packs"
PacksDir is the folder of pack files in a packed build.
Variables ¶
This section is empty.
Functions ¶
func DiffFiles ¶
DiffFiles compares the files of two libraries, as Scan lists them (ignored files left out): missing are the files of want that got lacks, extra those of got that want lacks, both sorted. A game's test compares its embedded assets with the folder on disk this way, so a stale generated embed list (wpix assets embed) fails the test.
func Files ¶
Files returns the files of the library in fsys as keys, sorted, the way Scan lists them: every regular file except the reserved top folders (packs/, names starting with "." or "_"), the IgnoreFile files and the files they ignore. It also lists files at the root and files of no known kind. The error is for a file system that cannot be listed; bad ignore lines are skipped (Scan reports them).
func PackageOf ¶
PackageOf returns the package of the file at p (a key's path, without "#sub"): its first folder. "forest/tiles/trees.tsj" is in "forest", "global/fonts/ui.ttf" in "global". A file at the library root, a file in a reserved folder (packs/, or a name starting with "." or "_") and a folder name alone return "".
func Scan ¶
Scan lists fsys and builds its catalog: packages, their entries and the packages they use. It also returns every problem it finds, all of them, sorted by path: a missing global/ package, references to missing files, case mismatches, references that leave the root, global/ or boot/ using another package, references outside a package's fence, cycles between packages (a warning), names outside [a-z0-9._-], unused files of no known kind, and textures over 8192 pixels. Files an IgnoreFile ignores are not part of the catalog; bad ignore lines, references and library.json entries naming ignored files, and ignored library files (library.json, the catalog, global/) are errors. The error is for a file system that cannot be listed or read, or an invalid library.json.
func WritePacks ¶
WritePacks builds a packed library from the loose library src into the directory out: one zip per package under packs/ (named "<package>.<sha256 prefix>.zip", members relative to the package folder), files outside the packages copied loose, and an assets.catalog.json that names the packs. Keys are the same as in src. The packs are deterministic: the same files give the same bytes and names.
It returns the catalog and Scan's findings; it writes even when there are findings (wpix assets pack refuses errors before calling it).
Types ¶
type Catalog ¶
Catalog lists a library's packages. It is a build output of wpix assets (catalog, pack); without one, Open builds it by listing the FS.
func ReadCatalog ¶
ReadCatalog parses a catalog written by Catalog.Encode.
type Config ¶
type Config struct {
// Entries adds keys to a package's entries, by package name: a font
// size to preload ("global/fonts/ui.ttf#16"), a level or a file that is
// also referenced but should load on its own.
Entries map[string][]string `json:"entries,omitempty"`
// Packages holds per-package settings, by package name.
Packages map[string]PackageConfig `json:"packages,omitempty"`
}
Config is the optional library.json at the library root.
type Finding ¶
type Finding struct {
Severity Severity
// Path is the file the finding is about.
Path string
Msg string
}
Finding is one problem Scan found.
type Ignore ¶
type Ignore struct {
// contains filtered or unexported fields
}
Ignore holds the rules of every IgnoreFile of a library. Ignored files are not part of the library: they are not listed, packed, embedded or put in the catalog, and Open refuses to read them from a base it lists itself. The nil *Ignore ignores nothing.
An Ignore is read-only once built and safe for concurrent use.
func ReadIgnore ¶
ReadIgnore reads every IgnoreFile of fsys, skipping those inside ignored folders (they cannot change anything). The error joins one error per bad line ("forest/.assetignore:3: bad pattern ..."); the returned Ignore holds the valid lines even then. A file system that cannot be listed gives the error of the walk and a nil Ignore.
func (*Ignore) Files ¶
Files returns the folders that hold an IgnoreFile with at least one rule, as paths of the ignore files, sorted.
func (*Ignore) Match ¶
Match reports whether the file (or, with isDir, the folder) at p, a path from the library root, is ignored, and names the rule that decided it ("forest/.assetignore:3"), also for a re-include. As in git, a path inside an ignored folder is ignored whatever the rules below say, and of the rules that match a path the one in the deepest file, and there the last line, wins.
type IgnoreError ¶
type IgnoreError struct {
File string // the ignore file: "forest/.assetignore"
Line int // 1-based
Msg string // "bad pattern \"a[\""
}
IgnoreError is a bad line of an IgnoreFile. The line is skipped; the other lines of the file still apply.
func (*IgnoreError) Error ¶
func (e *IgnoreError) Error() string
type IgnoredFile ¶
type IgnoredFile struct {
Path string // the file's key
By string // the deciding rule: "forest/.assetignore:3"
}
IgnoredFile is a file an IgnoreFile keeps out of the library.
func IgnoredFiles ¶
func IgnoredFiles(fsys fs.FS) ([]IgnoredFile, error)
IgnoredFiles lists the files of fsys that ignore files keep out of the library, sorted, with the rule that ignores each (for a file inside an ignored folder, the folder's rule). Reserved top folders and the ignore files themselves are not listed. The error is for a file system that cannot be listed; bad ignore lines are skipped (wpix assets check reports them).
type Kind ¶
type Kind string
Kind names what an asset file is, derived from its name by KindOf.
const ( KindUnknown Kind = "" KindTexture Kind = "texture" // *.png, *.jpg, *.jpeg KindAtlas Kind = "atlas" // *.sheet.json (grid), *.atlas.json (TexturePacker) KindClipSet Kind = "clipset" // *.anim.json, *.aseprite.json, *.ase.json KindTileset Kind = "tileset" // *.tsj KindMap Kind = "map" // *.tmj KindWorld Kind = "world" // *.world KindFont Kind = "font" // *.ttf, *.otf (load as "file.ttf#size") KindShader Kind = "shader" // *.kage KindSound Kind = "sound" // *.wav, *.ogg, *.mp3 outside a music/ folder KindMusic Kind = "music" // *.wav, *.ogg, *.mp3 inside a music/ folder KindBindings Kind = "bindings" // *.bindings.json KindLevel Kind = "level" // *.level.json KindPrefab Kind = "prefab" // *.prefab.json KindEffect Kind = "effect" // *.fx.json KindVoice Kind = "voice" // *.voice.json KindLanguage Kind = "language" // *.lang.json KindDialogue Kind = "dialogue" // *.dialogue.json KindScript Kind = "script" // *.gs )
The kinds the convention knows. KindLevel and KindPrefab are GOD documents: the engine spawns levels itself and loads prefabs as assets (prefab). KindEffect is a particle effect (particles); like a GOD document it names its sprite and shader by key from the asset root. KindVoice, KindLanguage and KindDialogue are the speech files (speech, which sets their loaders with speech.SetLibraryLoaders). KindScript is a goscript file (script, which sets its loader in script.Install).
func KindOf ¶
KindOf returns the kind of the file at p (a key's path, without "#sub") by its name, case-insensitively. Audio is music inside a folder named "music" and a sound anywhere else. Other names (a plain *.json, an *.aseprite source file) are KindUnknown: they load only as dependencies or by explicit key.
type Library ¶
type Library struct {
// contains filtered or unexported fields
}
Library is an asset library opened over a root FS. Create it with Open, hand Library.FS to the asset server, then Library.Attach the server.
func Open ¶
Open opens the library at the root of base. If base has an assets.catalog.json it is read; otherwise base is listed with Scan (its findings are ignored here: run wpix assets check). A file system that cannot list directories (HTTP) needs a catalog. A listed base keeps the rules of its ignore files (IgnoreFile): Library.FS does not serve an ignored file from it, so a loose library behaves like its packed or embedded build. Mounted overlays are not filtered.
It returns an error for an invalid catalog or library.json, or a base without a catalog that cannot be listed.
func (*Library) Attach ¶
Attach registers the pack asset type on srv, whose Options.FS must be Library.FS. Call it once, after the server's other loaders are registered, on the game goroutine.
It panics with "library: Attach called twice", and like asset.Register if srv already has the Pack type.
func (*Library) Catalog ¶
Catalog returns the library's catalog: every package with its files, entries, uses and pack, natively and over HTTP, before anything is downloaded. Don't modify it.
func (*Library) FS ¶
FS returns the file system to read assets from: give it to asset.Options.FS (game.Config.Library does). It serves the base FS plus the mounted packs and overlays, and is safe for concurrent reads.
func (*Library) HoldPackage ¶
HoldPackage mounts the packs of the package name and of the packages it uses into sc, like Library.LoadPackage, but loads no entries: the package's files become readable for loads that name them. The engine calls it before a level of the package spawns. It panics like LoadPackage.
func (*Library) LoadPackage ¶
LoadPackage loads the package name into sc and returns at once: it mounts the packs of the package and of the packages it uses (in a packed build; each pack stays mounted while sc holds it), then loads every entry through the loader of its kind. It is the explicit preload of a package, for assets code spawns or plays by key. Call Library.Update after each Server.Update (the engine does) so the entries start once the packs are mounted; with loose files they start at once.
It panics with "library: unknown package" and with "library: LoadPackage before Attach".
func (*Library) Mount ¶
Mount serves fsys at prefix on top of the library: keys under prefix ("" for the whole root) are read from fsys first and fall through to what was mounted before, then to the packs and the base FS. Later mounts win, and every Mount stays above the packs, even those mounted after it, which makes patches (and later mods) an overlay. Call it on the game goroutine; assets already loaded keep their values until they reload.
func (*Library) MountPackage ¶
MountPackage loads the packs of the package name and of the packages it uses into sc and blocks until they are mounted, so the package's files can be read right away, by LoadSync calls for example. With loose files it does nothing. Use it at boot (game.New mounts boot/ or global/ this way), never in a running level: it drives the server like asset.LoadSync.
It returns the joined errors of the packs that failed, and panics like Library.LoadPackage.
func (*Library) Mounted ¶
Mounted returns the names of the packages whose packs are mounted now, sorted. With loose files it is always empty: nothing needs mounting. Safe to call from any goroutine.
func (*Library) SetLoader ¶
SetLoader makes Library.LoadPackage load entries of kind k with fn. Entries of a kind without a loader are skipped. game.New registers the engine's kinds; a game can replace them or add its own.
func (*Library) Unmount ¶
Unmount removes the newest Library.Mount of fsys at prefix and reports whether there was one.
type Load ¶
type Load struct {
// contains filtered or unexported fields
}
Load is a package being loaded by Library.LoadPackage or held by Library.HoldPackage.
func (*Load) Done ¶
Done reports whether loading has settled: every asset of the scope is Ready or Failed, or a pack failed.
func (*Load) Mounted ¶
Mounted reports whether the package's files are readable: its packs (if any) are mounted and its entries (for LoadPackage) have started loading.
type LoadFunc ¶
LoadFunc loads key into sc, as asset.Load does for one asset type. It must not block.
type Pack ¶
type Pack struct {
// Package is the package the pack serves; empty for the placeholder.
Package string
// Files is the number of files in the pack.
Files int
// contains filtered or unexported fields
}
Pack is a mounted pack file: the files of one package in a zip archive, served at the package's folder while the pack is loaded. Packs are loaded by Library.LoadPackage and Library.HoldPackage; game code rarely sees them.
type Package ¶
type Package struct {
// Name is the folder: "global", "forest".
Name string `json:"name"`
// Files are the package's files as keys (root-relative paths), sorted.
Files []string `json:"files"`
// Entries are the keys [Library.LoadPackage] loads: files of a loadable
// kind that no other file of the package references, plus the entries
// listed in library.json. Their dependencies load with them.
Entries []string `json:"entries"`
// Uses are the other packages the package's files reference. Their
// packs are mounted with the package's own.
Uses []string `json:"uses,omitempty"`
// Pack is the package's pack file (a key, "packs/forest.<hash>.zip") in
// a packed build; empty when the files are loose.
Pack string `json:"pack,omitempty"`
// PackSize and PackSHA256 describe the pack file.
PackSize int64 `json:"packSize,omitempty"`
PackSHA256 string `json:"packSha256,omitempty"`
}
Package is one top folder of the library root: a collection of assets that loads, mounts and packs together. Its lifetime is the scope of whoever loads it.
type PackageConfig ¶
type PackageConfig struct {
// Uses, when set, fences the package: a reference to a package not
// listed here (other than global and boot) is an error. nil leaves the
// package free to use any package.
Uses []string `json:"uses,omitempty"`
}
PackageConfig is one package's settings in library.json.