Documentation
¶
Overview ¶
Package assets is experimental: its API and documented formats may change in a minor release; see https://nsgo.games/docs/stability/.
Package assets lays out a game's measured asset groups into the main package, named subpackages and remote files as a project's asset delivery settings place them, writes per-preset package layouts and platform subpackage declarations, and checks the result against the delivery's byte caps, for deterministic export-time package reports.
Index ¶
- Constants
- func MeasureDir(resourcesDir string) (groups []Group, looseMain []File, err error)
- func SupportsSubpackages(platformOrTarget string) bool
- type CapExceededError
- type File
- type Group
- type LayoutOptions
- type Package
- type Report
- func ApplyLayout(pkgDir, target string, delivery *project.AssetDelivery, planned Report, ...) (Report, error)
- func Layout(delivery *project.AssetDelivery, groups []Group) (Report, error)
- func LayoutForPlatform(platform string, delivery *project.AssetDelivery, groups []Group) (Report, error)
- func LayoutWithOptions(opts LayoutOptions) (Report, error)
Examples ¶
Constants ¶
const DefaultLargestFilesLimit = 5
DefaultLargestFilesLimit is the default number of largest files listed per package in formatted reports and cap-exceeded errors.
const SubpackagesDir = "subpackages"
SubpackagesDir is the directory inside a built mini-program or mini-game package where delivery subpackages are staged so they never collide with platform files or NSGo's own nsgo-app module subpackage.
Variables ¶
This section is empty.
Functions ¶
func MeasureDir ¶
MeasureDir scans resourcesDir and groups regular files into named asset groups by top-level subdirectory (for example resources/level1/map.tmx belongs to group "level1" with path "level1/map.tmx"). Regular files directly inside resourcesDir are returned in looseMain so they stay in the main package. Dotfiles, dot-directories, and symlinks are ignored. If resourcesDir does not exist, MeasureDir returns nil, nil, nil.
func SupportsSubpackages ¶
SupportsSubpackages reports whether platformOrTarget supports platform subpackages (currently mini-program / mini-game targets). On targets where it returns false, subpackage mappings collapse into the main package.
Types ¶
type CapExceededError ¶
type CapExceededError struct{ Report Report }
CapExceededError reports package limits with a deterministic report attached.
func (*CapExceededError) Error ¶
func (e *CapExceededError) Error() string
Error lists the report's violations and the largest files per package.
type LayoutOptions ¶
type LayoutOptions struct {
// Platform is the export platform or target name (such as "miniprogram",
// "miniprogram_wechat", "web", or "macos"). When non-empty and
// !SupportsSubpackages(Platform), subpackage groups collapse into Main.
// When empty, subpackages are kept separate.
Platform string
// Delivery is the preset's asset delivery mapping and caps.
Delivery *project.AssetDelivery
// Groups are the measured named asset groups.
Groups []Group
// MainExtras are extra files always shipped in the main package (such as
// loose top-level resources or built runtime/code files) whose sizes count
// toward main and total caps.
MainExtras []File
// ExtraPackages are extra packages produced by the platform build tool
// (such as NSGo's nsgo-app module subpackage) whose sizes count toward the
// total package cap.
ExtraPackages []Package
}
LayoutOptions configures LayoutWithOptions.
type Package ¶
Package is one package in a deterministic delivery report.
func (Package) LargestFiles ¶
LargestFiles returns up to limit files in p sorted descending by byte size, tie-breaking ascending by path so output is deterministic. When limit <= 0, all files are returned. p.Files is not modified.
type Report ¶
type Report struct {
Platform string
Collapsed bool
Main Package
Subpackages []Package
Remote []File
RemoteBytes int64
TotalBytes int64
Violations []string
}
Report is the package layout and cap evaluation for one export preset.
func ApplyLayout ¶
func ApplyLayout(pkgDir, target string, delivery *project.AssetDelivery, planned Report, remoteOutDir, remoteBase string) (Report, error)
ApplyLayout rearranges the built package directory pkgDir for target according to planned:
- On targets that support subpackages (SupportsSubpackages(target)), each subpackage's files are moved from <pkgDir>/resources/<path> to <pkgDir>/subpackages/<name>/resources/<path>, minimal entry files are ensured when absent, and <pkgDir>/game.json or <pkgDir>/app.json is updated with deterministic subPackages declarations.
- On every target, files in planned.Remote are removed from <pkgDir>/resources/<path>; when remoteOutDir is non-empty and remote files exist, they are written under remoteOutDir with content-hashed names and a canonical remote.yaml manifest.
- Finally, ApplyLayout measures the resulting on-disk package sizes (main package plus every subpackage directory, excluding remote files) and evaluates delivery.Caps, returning *CapExceededError with the largest files per package when a cap is exceeded.
func Layout ¶
func Layout(delivery *project.AssetDelivery, groups []Group) (Report, error)
Layout places each measured group according to delivery. Unmentioned groups stay in main. The files of each package and the remote files are sorted by path, and subpackages by name, so reports are stable across filesystem traversal order; the groups passed in are not modified. A subpackage may not be named main.
TotalBytes is the sum of all packaged files (Main plus Subpackages); remote files are served from a remote origin instead of a package and are summed in RemoteBytes.
Example ¶
Layout places measured asset groups into the main package, named subpackages and remote files as the delivery settings say, and returns a deterministic report.
package main
import (
"fmt"
"github.com/nsgo-dev/game/pkg/project"
"github.com/nsgo-dev/game/pkg/tooling/assets"
)
func main() {
delivery := &project.AssetDelivery{
Main: []string{"boot"},
Subpackages: map[string][]string{"chapter": {"level"}},
Remote: []string{"music"},
}
report, err := assets.Layout(delivery, []assets.Group{
{Name: "boot", Files: []assets.File{{Path: "boot.bin", Bytes: 8}}},
{Name: "level", Files: []assets.File{{Path: "z.bin", Bytes: 4}, {Path: "a.bin", Bytes: 5}}},
{Name: "music", Files: []assets.File{{Path: "music.ogg", Bytes: 30}}},
})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(report.Main.Name, report.Main.Bytes, len(report.Subpackages), report.TotalBytes, report.RemoteBytes, len(report.Remote))
}
Output: main 8 1 17 30 1
func LayoutForPlatform ¶
func LayoutForPlatform(platform string, delivery *project.AssetDelivery, groups []Group) (Report, error)
LayoutForPlatform lays out groups for platform according to delivery. On targets that do not support subpackages (such as web and native desktop or mobile platforms), subpackage groups collapse into the main package while still validating delivery names and checking caps.
func LayoutWithOptions ¶
func LayoutWithOptions(opts LayoutOptions) (Report, error)
LayoutWithOptions lays out groups with optional platform collapsing and extra main/subpackage files.