La referencia de API proviene de los comentarios de documentación de Go en el código fuente y está redactada en inglés.
package statedump
import "github.com/nsgo-dev/game/pkg/statedump"Resumen
Package statedump runs a game headless for a fixed number of steps and writes its entities, components and scene state as deterministic YAML, so an agent or a test can diff one run against another.
A Dump holds the run's seed, rate and step count, the scene stack (Scene), named game-wide values outside the entity world (Values), and every live entity with its hierarchy parent and its codec-registered components, as entities.World.Snapshot records them.
Encode writes a Dump as version 1 YAML: fixed key order, entities by ascending slot index, components by ascending codec name, values by ascending byte order of their names, and component and value data turned from their JSON encoding into YAML mappings and sequences that keep the JSON field order and the exact number text. Two runs with the same seed and inputs on the same CPU architecture therefore write byte-identical files (a game's own float math can differ between arm64 and amd64, where the compiler may fuse a multiply and an add), and a change shows as a few changed lines in a line diff. Parse reads such a file back, refusing unknown fields, other versions, trailing documents and data JSON cannot hold, and Encode(Parse(b)) returns b for every b that Encode wrote.
Run plays a replay.Recording against a simulation without a window and captures the Dump at the end; Capture takes one at any point; Diff prints a unified line diff of two dumps for a test failure or an agent's report.
Índice
- Constantes
- func Diff(a, b []byte) string
- func Encode(d *Dump) ([]byte, error)
- func Validate(d *Dump) error
- func WriteFile(path string, d *Dump) error
- type Dump
- func Capture(src Source) (*Dump, error)
- func Parse(data []byte) (*Dump, error)
- func ReadFile(path string) (*Dump, error)
- func Run(rec *replay.Recording, makeSim func(seed uint64, src replay.ActionSource) (Sim, error)) (*Dump, error)
- func (d *Dump) SetValue(name string, v any) error
- func (d *Dump) Snapshot() entities.WorldSnapshot
- type SceneState
- type Sim
- type Source
Constantes
const FormatVersion = 1FormatVersion is the schema version Encode writes and Parse accepts.
Funciones
func Diff(a, b []byte) stringDiff returns a unified line diff from a to b, labelled "a" and "b", or "" when they are equal.
It is meant for dumps that Encode wrote, where one changed field is one changed line, and prints each change with three unchanged lines around it.
func Encode(d *Dump) ([]byte, error)Encode validates d and writes it as deterministic 2-space YAML.
func Validate(d *Dump) errorValidate checks that d uses FormatVersion, has non-negative counts, a finite scene progress that is 0 unless a transition runs, non-empty value names with valid JSON, and entities in strictly ascending slot order with non-zero generations, parents present in the dump, and components with non-empty names in strictly ascending order holding valid JSON.
func WriteFile(path string, d *Dump) errorWriteFile encodes d and writes it to path.
Tipos
type Dump struct {
// Version is the schema version; it must equal FormatVersion.
Version int
// Seed is the random seed the run started from.
Seed uint64
// Rate is the run's fixed steps per second, or 0 when unknown.
Rate int
// Steps is how many fixed steps the run took before the dump.
Steps int
// Scene is the scene stack's state, or nil when the game has no scene.Manager.
Scene *SceneState
// Values holds game-wide state outside the entity world (score, level, RNG position), each as its JSON encoding, keyed by a non-empty name.
Values map[string]json.RawMessage
// Entities are the live entities in ascending slot index, as entities.World.Snapshot records them.
Entities []entities.EntitySnapshot
}Dump is the state of a game after a number of fixed steps.
func Capture(src Source) (*Dump, error)Capture returns the state src names as a Dump with FormatVersion and no seed, rate or step count; the caller fills those in when it knows them.
func Parse(data []byte) (*Dump, error)Parse reads a version 1 YAML dump, refusing unknown fields, trailing documents, and data JSON cannot hold (aliases, non-string keys, binary, NaN and infinities), then runs Validate.
func ReadFile(path string) (*Dump, error)ReadFile reads and parses the dump at path.
func Run(rec *replay.Recording, makeSim func(seed uint64, src replay.ActionSource) (Sim, error)) (*Dump, error)Run plays rec headless against the simulation makeSim builds and returns the state after rec.TotalSteps steps. It returns an error when the steps did not advance the ActionSource once each, because a sim that never reads the recording would otherwise dump the same state for any input.
makeSim receives rec.Seed and a *replay.Player as its ActionSource, as with replay.Run, but Run needs no testing.TB, so a command or an agent's tool can call it. Each step passes rec.StepDuration(), the fixed dt of a game.Loop at rec.Rate.
func (d *Dump) SetValue(name string, v any) errorSetValue stores v's JSON encoding in d.Values under name.
func (d *Dump) Snapshot() entities.WorldSnapshotSnapshot returns d's entities as an entities.WorldSnapshot that entities.World.Restore loads.
type SceneState struct {
// Depth is the number of scenes on the stack.
Depth int `yaml:"depth"`
// Current names the scene at the top of the stack, or is empty when the game does not name its scenes.
Current string `yaml:"current,omitempty"`
// Transitioning reports whether a Switch transition is running.
Transitioning bool `yaml:"transitioning,omitempty"`
// Progress is the running transition's eased progress, as scene.Manager.TransitionProgress reports it; it is 0 when no transition runs. It usually lies in [0, 1], but a curve that overshoots, such as an ease-out-back, takes it past either end for part of the transition.
Progress float64 `yaml:"progress,omitempty"`
}SceneState is the state of a scene.Manager's stack.
type Sim struct {
Source
// Step advances the simulation by dt; like a game.Loop's Update, it must call the ActionSource's Step once, or Run returns an error.
Step func(dt time.Duration)
}Sim is a headless simulation that Run steps: Step advances it by one fixed step and Source says what to dump afterwards.
type Source struct {
// World is the entity world; only components registered with entities.RegisterCodec appear in the dump.
World *entities.World
// Scenes is the game's scene stack.
Scenes *scene.Manager
// SceneName names a scene controller for SceneState.Current; nil leaves Current empty.
SceneName func(*scene.Controller) string
// Values returns game-wide state outside the world, each value encoded with encoding/json.
Values func() map[string]any
}Source names the parts of a game that Capture reads. Every field is optional.