Справочник API собран из Go doc-комментариев в исходном коде и написан на английском языке.
package library
import "github.com/nsgo-dev/game/pkg/tooling/library"Обзор
Package library finds shared asset packs, editor plugin packs and templates in a library and installs them into a game project.
The Index
A library is a text index anyone can add a pack to with a pull request: a versioned YAML file (index.yaml, IndexFileName) listing each pack's name, kind, version, description, tags, license, source and digest. ParseIndex refuses unknown fields, other versions, duplicate names and sources that leave the library, and EncodeIndex writes the deterministic form ParseIndex reads back unchanged. Index.Search finds packs by kind and by words in their names, descriptions and tags.
Packs
A pack is a directory holding a manifest, pack.yaml (ManifestFileName), and the files it lists. The manifest names the pack, gives its kind (asset, plugin or template, as project.PackKinds names them), its version and license, the project directory its files go to (into, by default packs/<name>), and the references installing it adds to game.yaml: autoload scenes and resources, and, for a plugin pack, the editor plugins it turns on. A pack's digest (Digest) is the SHA-256 of a sha256sum-style listing of its files, pack.yaml included, so the index pins the exact bytes of every file whatever form they were fetched in.
Fetching
Open reads an index from a directory, a file or an http(s) URL, and Library.Fetch fetches a pack from its source, relative to the index or an absolute URL, file by file as its manifest lists them, and refuses one whose digest, name, kind or version differs from its index entry. Plain http is accepted only for a loopback host, for the index and for a pack's absolute source alike; anything else must use https, every file is limited to MaxFileSize bytes, and without a Library.Client a request gives up on a server that stalls.
Installing
PlanInstall works out what installing a fetched pack does to a project, without writing anything: the files to write and remove, and the project.File afterwards, which records the pack under packs with the files it wrote and the references it added. Installing a pack again updates it, replacing its files and references with the new version's; a file that is already in the project and is not the pack's is refused rather than overwritten. A pack's files and the directory they go to must stay inside the project, outside hidden directories such as .github, with names every platform can hold and no two differing only in case, and may not be Go source or another file the Go toolchain builds. PlanRemove undoes an install. Change.Apply then writes the files and game.yaml, restoring what it changed when a write fails. Install and Remove do both for a project directory, and the game library command and the scene editor's library panel run them.
Указатель
- Константы
- func DefaultDir(name string) string
- func Digest(files map[string][]byte) string
- func EncodeIndex(idx *Index) ([]byte, error)
- func EncodeManifest(m *Manifest) ([]byte, error)
- func ValidateIndex(idx *Index) error
- func ValidateManifest(m *Manifest) error
- type Autoload
- type Change
- func Install(projectDir string, p *Pack, dir string) (*Change, error)
- func PlanInstall(fsys fs.FS, f *project.File, p *Pack, dir string) (*Change, error)
- func PlanRemove(fsys fs.FS, f *project.File, name string) (*Change, error)
- func Remove(projectDir, name string) (*Change, error)
- func (c *Change) Apply(projectDir string) error
- func (c *Change) ApplyFiles(projectDir string) (undo func() error, err error)
- func (c *Change) NoOp() bool
- type Entry
- type Index
- type Library
- type Manifest
- type Pack
Константы
const DefaultIndex = "https://raw.githubusercontent.com/nsgo-dev/game/main/library/index.yaml"DefaultIndex is the shared library's index, in this repository's library directory, which the game library command reads unless told otherwise.
const IndexFileName = "index.yaml"IndexFileName is the index's file name, which Open reads from a directory.
const IndexVersion = 1IndexVersion is the index schema version this package reads and writes.
const ManifestFileName = "pack.yaml"ManifestFileName is a pack's manifest, at the root of its directory.
const ManifestVersion = 1ManifestVersion is the manifest schema version this package reads and writes.
Функции
func DefaultDir(name string) stringDefaultDir is the project directory a pack called name installs into when neither its manifest nor the installer names one.
func Digest(files map[string][]byte) stringDigest returns the digest of a pack's files: the SHA-256, in lower-case hexadecimal, of one line per file sorted by path, each the file's own SHA-256 in lower-case hexadecimal, two spaces and its slash-separated path, ended by a newline. The listing is what sha256sum prints for the files named in byte order, so the digest can be checked without this package; the game library digest command prints it for a pack's directory.
func EncodeIndex(idx *Index) ([]byte, error)EncodeIndex validates idx and serializes it as deterministic 2-space YAML that ParseIndex reads back unchanged.
func EncodeManifest(m *Manifest) ([]byte, error)EncodeManifest validates m and serializes it as deterministic 2-space YAML that ParseManifest reads back unchanged.
func ValidateIndex(idx *Index) errorValidateIndex checks that idx conforms to the version 1 schema.
func ValidateManifest(m *Manifest) errorValidateManifest checks that m conforms to the version 1 schema.
Типы
type Autoload struct {
// Scenes are scene files to autoload; each path is one of Manifest.Files, ending in .yaml or .yml.
Scenes []project.Entry `yaml:"scenes,omitempty"`
// Resources are other files to autoload; each path is one of Manifest.Files.
Resources []project.Entry `yaml:"resources,omitempty"`
}Autoload lists the pack files installing a pack autoloads.
type Change struct {
// Project is the project file afterwards.
Project *project.File
// Pack is the pack's record: the one Project holds after an install, or the one it held before a removal.
Pack project.Pack
// Write maps each file to write, by its slash-separated path from the project root, to its content.
Write map[string][]byte
// Remove lists the files to remove, by their slash-separated paths from the project root, sorted.
Remove []string
// contains filtered or unexported fields
}Change is what installing or removing a pack does to a project, worked out by PlanInstall or PlanRemove and carried out by Apply.
func Install(projectDir string, p *Pack, dir string) (*Change, error)Install installs p into the Go project in projectDir, whose game.yaml it reads, as PlanInstall and Change.Apply describe, and returns what it did. An install that changes nothing, such as the same release of a pack installed again, writes nothing.
func PlanInstall(fsys fs.FS, f *project.File, p *Pack, dir string) (*Change, error)PlanInstall works out what installing p into the project at the root of fsys, whose project file is f, does, without writing anything. The pack's files go under dir; when dir is empty, an update keeps them in the directory the installed version went to, and a first install puts them in the manifest's into or DefaultDir. The directory must stay inside the project, and none of its elements may be hidden (start with '.') or be a name some platform cannot hold.
When f already records the pack, the install updates it: the files of the old version the new one lacks are removed, the others replaced, and the autoload entries and plugins it added are replaced by the new version's, keeping their places. A file the project already has is refused unless the old version wrote it or it has the content the pack would write, and so is a file another pack wrote; an autoload scene or resource of the pack's name that the project already has is refused too. A plugin the project already turns on is left as it is and not recorded as the pack's.
f is not changed; the returned Change's Project is a validated copy.
func PlanRemove(fsys fs.FS, f *project.File, name string) (*Change, error)PlanRemove works out what removing the installed pack called name from the project at the root of fsys, whose project file is f, does, without writing anything: its files are removed, edited or not, and so are the autoload entries and editor plugins it added. A main scene that was the pack's is unset.
func Remove(projectDir, name string) (*Change, error)Remove removes the installed pack called name from the project in projectDir, as PlanRemove and Change.Apply describe, and returns what it did.
func (c *Change) Apply(projectDir string) errorApply carries out c in the project directory projectDir: it removes and writes c's files, deleting the directories a removal leaves empty inside the pack's directory (and packs, for a pack in its DefaultDir), then writes c.Project to game.yaml in the canonical form project.Encode writes, which keeps no comments. When a step fails, it restores the files it had changed and returns the error.
func (c *Change) ApplyFiles(projectDir string) (undo func() error, err error)ApplyFiles removes and writes c's files in the project directory projectDir, leaving game.yaml to the caller, such as an editor that saves its open project. It removes first, so that an update may put a file where the old version had a directory or the other way round, and deletes the directories a removal leaves empty inside the pack's directory (and packs, for a pack in its DefaultDir), never above it. It returns undo, which restores every file it changed, for when saving the project then fails. When ApplyFiles itself fails, it has already restored them.
func (c *Change) NoOp() boolNoOp reports whether applying c would change nothing: no file to write or remove, and the same project file.
type Entry struct {
// Name identifies the pack, as project.ValidatePackName accepts it; it is unique in the index and equals the name in the pack's manifest.
Name string `yaml:"name"`
// Kind is one of project.PackKinds and equals the manifest's.
Kind string `yaml:"kind"`
// PackVersion is the version the index offers, which equals the manifest's.
PackVersion string `yaml:"packVersion"`
// Description says in one line what the pack holds.
Description string `yaml:"description"`
// Tags are lower-case words a search also matches, such as 2d or tiles.
Tags []string `yaml:"tags,omitempty,flow"`
// License is the license of the pack's files, as an SPDX identifier such as CC0-1.0 or MIT.
License string `yaml:"license"`
// Source is the directory holding the pack's pack.yaml: a slash-separated path relative to the index's directory that stays inside it, or an absolute https URL.
Source string `yaml:"source"`
// SHA256 is the pack's digest (Digest), 64 lower-case hexadecimal digits; a fetched pack whose digest differs is refused.
SHA256 string `yaml:"sha256"`
}Entry is one pack of a library.
type Index struct {
// Version is the schema version; it must equal IndexVersion (1).
Version int `yaml:"version"`
// Packs are the library's packs, in the order the index lists them.
Packs []Entry `yaml:"packs,omitempty"`
}Index is a library's versioned index file.
func ParseIndex(data []byte) (*Index, error)ParseIndex decodes a YAML index and validates it. Unknown fields and trailing YAML documents are refused.
func (idx *Index) Find(name string) (Entry, bool)Find returns the entry called name, or false.
func (idx *Index) Search(kind, query string) []EntrySearch returns the entries of kind, or of every kind when kind is empty, whose name, description or tags contain every word of query, ignoring case, sorted by name. An empty query matches every entry.
type Library struct {
// Index is the library's parsed index.
Index *Index
// Location is where the index was read from: a URL, a file path, or the name of the index in an fs.FS.
Location string
// Client fetches http(s) indexes and packs; nil uses a client that gives up on a server that does not connect within 30 seconds or answer within a minute, and on any one file after 10 minutes.
Client *http.Client
// contains filtered or unexported fields
}Library is an index together with where it was read from, so that its entries' relative sources resolve.
func Open(ctx context.Context, location string) (*Library, error)Open reads the index at location: an http(s) URL of an index file, a local index file, or a local directory holding index.yaml. Plain http is accepted only for a loopback host, such as a test server's.
func OpenFS(fsys fs.FS, name string) (*Library, error)OpenFS reads the index called name in fsys, whose relative sources resolve from name's directory in fsys.
func (l *Library) Fetch(ctx context.Context, name string) (*Pack, error)Fetch fetches the pack called name from its source and checks it against its index entry: its digest, name, kind and version must all be the entry's.
func (l *Library) Remote() boolRemote reports whether fetching from l, or from any of its packs, may reach the network, so that a caller with a user interface fetches off its event loop.
type Manifest struct {
// Version is the schema version; it must equal ManifestVersion (1).
Version int `yaml:"version"`
// Name identifies the pack, as project.ValidatePackName accepts it.
Name string `yaml:"name"`
// Kind is one of project.PackKinds.
Kind string `yaml:"kind"`
// PackVersion is the pack's own version, such as 1.2.0.
PackVersion string `yaml:"packVersion"`
// Description says in one line what the pack holds.
Description string `yaml:"description"`
// License is the license of the pack's files, as an SPDX identifier such as CC0-1.0.
License string `yaml:"license"`
// Into is the slash-separated project directory the pack's files are written to; empty is DefaultDir(Name).
Into string `yaml:"into,omitempty"`
// Files are the slash-separated paths of the pack's files relative to its directory, pack.yaml aside; no path may be another's directory.
Files []string `yaml:"files,omitempty"`
// Autoload lists the pack's files that installing it adds to game.yaml's autoload section; nil adds none.
Autoload *Autoload `yaml:"autoload,omitempty"`
// Plugins names the editor plugins a plugin pack turns on under game.yaml's editor.plugins; a plugin pack names at least one, and the other kinds none.
Plugins []string `yaml:"plugins,omitempty,flow"`
}Manifest is a pack's versioned pack.yaml.
func ParseManifest(data []byte) (*Manifest, error)ParseManifest decodes a YAML pack manifest and validates it. Unknown fields and trailing YAML documents are refused.
type Pack struct {
// Manifest is the parsed pack.yaml.
Manifest *Manifest
// Files maps each file's slash-separated path in the pack to its content, pack.yaml included.
Files map[string][]byte
}Pack is a pack read into memory: its manifest and the content of every file it holds.
func ReadPack(fsys fs.FS, dir string) (*Pack, error)ReadPack reads the pack whose directory is dir in fsys: its pack.yaml and every file the manifest lists. A file the manifest does not list is not part of the pack.
func (p *Pack) Digest() stringDigest returns p's digest, as the package function Digest computes it from p.Files.
func (p *Pack) Names() []stringNames returns the paths of p's files, pack.yaml included, sorted.