Documentation
¶
Overview ¶
Package devstate saves and restores the running game scene, fixed-step loop clock, and serialized state across Go rebuilds during development.
In 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.
Index ¶
- func ComputeSchemaHash(sample any) string
- type Hook
- type HookOptions
- type RestoreResult
- type SessionSnapshot
- type Snapshotter
- type Store
- func (s *Store) Clear() error
- func (s *Store) LoadSnapshot() (snap SessionSnapshot, ok bool, err error)
- func (s *Store) Restore(state any) (RestoreResult, error)
- func (s *Store) Save(sceneID string, loopTick uint64, elapsed time.Duration, state any) error
- func (s *Store) SetLogger(logf func(format string, args ...any))
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ComputeSchemaHash ¶
ComputeSchemaHash reflects the deterministic structural schema of sample (field names, tags, and types recursively) and returns its SHA-256 hex digest.
Top-level pointer indirection is unwrapped so ComputeSchemaHash(T{}) and ComputeSchemaHash(&T{}) yield the exact same hash. Any added, removed, renamed, reordered, or retyped field changes the digest.
Types ¶
type Hook ¶
type Hook struct {
// contains filtered or unexported fields
}
Hook binds a Snapshotter to a core.Scope so state is saved automatically on exit, scope disposal, or an optional periodic interval.
func Attach ¶
func Attach(cx *core.Scope, store *Store, target Snapshotter, opts HookOptions) *Hook
Attach wires target into cx so that session snapshots are saved automatically when cx exits or is disposed, and periodically when opts.SaveInterval > 0.
func (*Hook) RestoreInto ¶
func (h *Hook) RestoreInto(state any) (RestoreResult, error)
RestoreInto loads a saved snapshot into state (a non-nil pointer) and, when successfully restored, forwards it to the attached Snapshotter's Restore method.
type HookOptions ¶
type HookOptions struct {
// SceneID returns the identifier of the currently active scene.
// If nil, "default" is recorded.
SceneID func() string
// LoopTick returns the current fixed-step simulation tick count.
LoopTick func() uint64
// LoopElapsed returns the total elapsed simulation time.
LoopElapsed func() time.Duration
// SaveInterval periodically checkpoints state while running when > 0.
SaveInterval time.Duration
// Logf receives diagnostic messages such as schema mismatch notifications.
// When nil, the store's logger is used.
Logf func(format string, args ...any)
}
HookOptions configures how Attach captures scene metadata and checkpoints state.
type RestoreResult ¶
type RestoreResult struct {
Restored bool
SceneID string
LoopTick uint64
LoopElapsed time.Duration
SkipReason string
}
RestoreResult describes the outcome of Store.Restore.
When 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.
type SessionSnapshot ¶
type SessionSnapshot struct {
SceneID string `json:"scene_id"`
SchemaHash string `json:"schema_hash"`
LoopTick uint64 `json:"loop_tick"`
LoopElapsed time.Duration `json:"loop_elapsed"`
Payload []byte `json:"payload"`
SavedAt time.Time `json:"saved_at"`
}
SessionSnapshot is the persisted state of a running development session.
type Snapshotter ¶
Snapshotter is implemented by scenes or game controllers that participate in automatic dev-session capture and restore via Hook.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store persists and restores SessionSnapshots either on disk or in core.Storage.
Example ¶
A Store keeps the running scene's state across a rebuild during development; Restore brings it back only when the state's shape has not changed since it was saved.
//go:build !release
package main
import (
"fmt"
"time"
"github.com/nsgo-dev/game/pkg/tooling/devstate"
"github.com/nsgo-dev/nsgo/core"
)
type levelState struct {
PlayerX, PlayerY float32
Coins int
}
// A Store keeps the running scene's state across a rebuild during development; Restore brings it back only when the state's shape has not changed since it was saved.
func main() {
root := core.NewRoot(core.RootOptions{})
defer root.Dispose()
store := devstate.NewStorageStore(root.Storage(), "devstate")
store.SetLogger(func(string, ...any) {})
if err := store.Save("level-2", 900, 15*time.Second, levelState{PlayerX: 120, PlayerY: 48, Coins: 7}); err != nil {
fmt.Println(err)
return
}
// After the rebuild:
var st levelState
res, err := store.Restore(&st)
fmt.Println(err, res.Restored, res.SceneID, res.LoopTick, res.LoopElapsed, st)
// A changed state type is skipped rather than half restored.
var other struct{ Lives int }
res, _ = store.Restore(&other)
fmt.Println(res.Restored, res.SkipReason != "")
}
Output: <nil> true level-2 900 15s {120 48 7} false true
func NewStorageStore ¶
NewStorageStore creates a core.Storage-backed dev session store under key.
func (*Store) LoadSnapshot ¶
func (s *Store) LoadSnapshot() (snap SessionSnapshot, ok bool, err error)
LoadSnapshot 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.
func (*Store) Restore ¶
func (s *Store) Restore(state any) (RestoreResult, error)
Restore loads the saved session snapshot into state (which must be a non-nil pointer).
If 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.