{"modulePath":"github.com/nsgo-dev/game","version":"v0.3.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package devstate\n\nPackage devstate saves and restores the running game scene, fixed-step loop clock, and serialized state across Go rebuilds during development.\n\nIn release builds (-tags release), the package compiles to zero-cost no-op stubs so production binaries link none of the reflection, serialization, or file I/O logic.\n\n## Functions\n\n```go\nfunc ComputeSchemaHash(sample any) string\n```\nComputeSchemaHash reflects the deterministic structural schema of sample (field names, tags, and types recursively) and returns its SHA-256 hex digest.\n\nTop-level pointer indirection is unwrapped so ComputeSchemaHash(T{}) and ComputeSchemaHash(\u0026T{}) yield the exact same hash. Any added, removed, renamed, reordered, or retyped field changes the digest.\n\n## Types\n\n```go\ntype Hook struct {\n\t// contains filtered or unexported fields\n}\n```\nHook binds a Snapshotter to a core.Scope so state is saved automatically on exit, scope disposal, or an optional periodic interval.\n\n```go\nfunc Attach(cx *core.Scope, store *Store, target Snapshotter, opts HookOptions) *Hook\n```\nAttach wires target into cx so that session snapshots are saved automatically when cx exits or is disposed, and periodically when opts.SaveInterval \u003e 0.\n\n```go\nfunc (h *Hook) RestoreInto(state any) (RestoreResult, error)\n```\nRestoreInto loads a saved snapshot into state (a non-nil pointer) and, when successfully restored, forwards it to the attached Snapshotter's Restore method.\n\n```go\nfunc (h *Hook) SaveNow() error\n```\nSaveNow captures a snapshot from the attached Snapshotter and persists it immediately.\n\n```go\nfunc (h *Hook) Stop()\n```\nStop halts periodic checkpointing and prevents automatic save on scope cleanup.\n\n```go\ntype HookOptions struct {\n\t// SceneID returns the identifier of the currently active scene.\n\t// If nil, \"default\" is recorded.\n\tSceneID func() string\n\t// LoopTick returns the current fixed-step simulation tick count.\n\tLoopTick func() uint64\n\t// LoopElapsed returns the total elapsed simulation time.\n\tLoopElapsed func() time.Duration\n\t// SaveInterval periodically checkpoints state while running when \u003e 0.\n\tSaveInterval time.Duration\n\t// Logf receives diagnostic messages such as schema mismatch notifications.\n\t// When nil, the store's logger is used.\n\tLogf func(format string, args ...any)\n}\n```\nHookOptions configures how Attach captures scene metadata and checkpoints state.\n\n```go\ntype RestoreResult struct {\n\tRestored    bool\n\tSceneID     string\n\tLoopTick    uint64\n\tLoopElapsed time.Duration\n\tSkipReason  string\n}\n```\nRestoreResult describes the outcome of Store.Restore.\n\nWhen a saved snapshot cannot be applied because its state struct schema changed across a rebuild, Restored is false, SkipReason explains why, and Restore returns a nil error so the game starts fresh cleanly.\n\n```go\ntype SessionSnapshot struct {\n\tSceneID     string        `json:\"scene_id\"`\n\tSchemaHash  string        `json:\"schema_hash\"`\n\tLoopTick    uint64        `json:\"loop_tick\"`\n\tLoopElapsed time.Duration `json:\"loop_elapsed\"`\n\tPayload     []byte        `json:\"payload\"`\n\tSavedAt     time.Time     `json:\"saved_at\"`\n}\n```\nSessionSnapshot is the persisted state of a running development session.\n\n```go\ntype Snapshotter interface {\n\tSnapshot() (any, error)\n\tRestore(state any) error\n}\n```\nSnapshotter is implemented by scenes or game controllers that participate in automatic dev-session capture and restore via Hook.\n\n```go\ntype Store struct {\n\t// contains filtered or unexported fields\n}\n```\nStore persists and restores SessionSnapshots either on disk or in core.Storage.\n\n```go\nfunc NewStorageStore(st core.Storage, key string) *Store\n```\nNewStorageStore creates a core.Storage-backed dev session store under key.\n\n```go\nfunc NewStore(path string) *Store\n```\nNewStore creates a file-backed dev session store at path.\n\n```go\nfunc (s *Store) Clear() error\n```\nClear removes any persisted session snapshot.\n\n```go\nfunc (s *Store) LoadSnapshot() (snap SessionSnapshot, ok bool, err error)\n```\nLoadSnapshot reads the stored SessionSnapshot without decoding its payload. If no snapshot exists, ok is false and err is nil. A snapshot that does not decode returns a \\*loaderr.Error that names the snapshot's file (or storage key) and line.\n\n```go\nfunc (s *Store) Restore(state any) (RestoreResult, error)\n```\nRestore loads the saved session snapshot into state (which must be a non-nil pointer).\n\nIf no snapshot exists, or if the saved schema hash does not match state's current schema, Restore logs a diagnostic notice and returns Restored: false with a clear SkipReason and a nil error so the app starts fresh without crashing.\n\n```go\nfunc (s *Store) Save(sceneID string, loopTick uint64, elapsed time.Duration, state any) error\n```\nSave marshals state together with its schema hash and loop clock, writing the snapshot atomically.\n\n```go\nfunc (s *Store) SetLogger(logf func(format string, args ...any))\n```\nSetLogger sets the logger used for non-fatal restore notices such as schema mismatches. Passing nil restores the default log.Printf behavior.\n\n","path":"github.com/nsgo-dev/game/pkg/tooling/devstate","name":"devstate","synopsis":"Package devstate saves and restores the running game scene, fixed-step loop clock, and serialized state across Go rebuilds during development.","isRedistributable":true}
