command

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: LGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const InvalidID = MaxID + 1

InvalidID is a sentinel id for errors or when we have exceeded the maximum command count.

View Source
const MaxID = math.MaxUint32 - 1

MaxID is the maximum number of command types that can be registered.

Variables

This section is empty.

Functions

func HasCodec added in v0.14.0

func HasCodec(name string) bool

HasCodec reports whether a codec is registered for the command name.

func Marshal added in v0.14.0

func Marshal(p Payload) ([]byte, error)

Marshal encodes a command payload using its registered codec.

func RegisterCodec added in v0.14.0

func RegisterCodec(name string, c Codec)

RegisterCodec registers the wire codec for a command name. Generated code calls this from init(). A name may be registered only once: a duplicate means two generated files claim the same wire name (e.g. a stale commands_wire.gen.go left behind after a command moved packages), so we fail loudly at startup rather than let a last-wins map write silently shadow the correct codec. Registration must happen before the world starts (Seal); a later call panics rather than racing the hot-path reads.

func Seal added in v0.14.0

func Seal()

Seal freezes the codec registry. cardinal calls it when a world starts. Codecs must be registered from generated init() (which runs before any StartGame); registering afterwards would be an unrecoverable concurrent-map write against the command hot path, so a post-seal register panics deterministically instead of racing.

Types

type Codec added in v0.14.0

type Codec interface {
	Marshal(Payload) ([]byte, error)
	Unmarshal([]byte) (Payload, error)
}

Codec encodes and decodes a command payload to and from its wire bytes. Each command type has its own codec (generated from its schema); the engine does not know a command's shape, so it looks the codec up by command name. Unmarshal returns a fresh payload rather than mutating a receiver, so a codec implementation needs no pointer receivers.

type Command

type Command struct {
	Name    string                // The command name
	Address *micro.ServiceAddress // Service address this command is sent to
	Persona string                // Sender's persona
	Payload Payload               // The command payload itself
}

Command represents a command from a player or external system.

type ID

type ID = uint32

ID is a unique identifier for a command type, used for bookkeeping and performance boosts.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages command registration and stores commands received to be passed to the ECS world. Command IDs are mainly used for quick lookup and to check for duplicate WithCommand fields in a system state.

func NewManager

func NewManager() Manager

NewManager creates a new command manager.

func (*Manager) Clear

func (m *Manager) Clear()

Clear discards all pending commands from both queues and buffers.

func (*Manager) Drain

func (m *Manager) Drain() []Command

Drain collects commands from the queues to read-only command buffers. It also returns a list of all commands collected thus far (used by the transaction log). Drain is expected to be called at the start of each tick.

func (*Manager) Enqueue

func (m *Manager) Enqueue(command *iscv1.Command) error

Enqueue stores a command in its corresponding queue. The queues map isn't lock protected, and it is expected that there exists only 1 caller for each command type, therefore each caller reads a different key. This is ok because concurrent reads on Go maps are allowed.

func (*Manager) Get

func (m *Manager) Get(id ID) ([]Command, error)

Get retrieves a slice of commands given the command ID. The ID is returned from Register, and callers are expected to store it for calls to Get. This API is used vs using the command's name as the index as that requires an extra map lookup. We sacrifice extra complexity at the caller to make sure lookups are fast as Get is a hot path as it is called every tick.

func (*Manager) Names added in v0.10.2

func (m *Manager) Names() []string

Names returns the names of all registered command types.

func (*Manager) Register

func (m *Manager) Register(name string, queue Queue) (ID, error)

Register registers the command type with the command manager.

func (*Manager) Zero added in v0.10.2

func (m *Manager) Zero(name string) Payload

Zero returns a zero-value instance of the named command's payload type.

type Payload

type Payload interface {
	schema.Serializable
}

Payload is the interface all command payloads must implement.

type Queue

type Queue interface {
	Enqueue(*iscv1.Command) error
	Drain(target *[]Command)
	Len() int
	Zero() Payload
}

Queue defines the interface for command queuing operations. It provides methods to enqueue commands and drain all queued commands.

func NewQueue

func NewQueue[T Payload]() Queue

NewQueue creates a new command queue with an initial buffer capacity.

Jump to

Keyboard shortcuts

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