assets

package
v0.4.0-rc.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 4, 2026 License: Apache-2.0 Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

Examples

Constants

View Source
const DefaultLargestFilesLimit = 5

DefaultLargestFilesLimit is the default number of largest files listed per package in formatted reports and cap-exceeded errors.

View Source
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

func MeasureDir(resourcesDir string) (groups []Group, looseMain []File, err error)

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

func SupportsSubpackages(platformOrTarget string) bool

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 File

type File struct {
	Path  string
	Bytes int64
}

File is one shipped asset with its byte size.

type Group

type Group struct {
	Name  string
	Files []File
}

Group is the measured output of one named asset group.

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

type Package struct {
	Name  string
	Files []File
	Bytes int64
}

Package is one package in a deterministic delivery report.

func (Package) LargestFiles

func (p Package) LargestFiles(limit int) []File

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.

func (Report) Format

func (r Report) Format(maxFiles int) string

Format renders a deterministic human-readable package layout and size report, including up to maxFiles largest files per package (defaulting to DefaultLargestFilesLimit when maxFiles <= 0).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL