Documentation
¶
Overview ¶
Package persist holds the rules for the records a mod keeps for each player through the host's persist service: which record types a manifest may declare, and how an update's field operations change a record. A host that serves the service applies updates with Apply, so every host agrees on what an update does and what it refuses.
Index ¶
- Constants
- func Answer(store *Store, manifest *wasm.Manifest, players Players, method string, ...) ([]byte, error)
- func Apply(recordType *persistpb.RecordType, stored *persistpb.StoredRecord, ...) (*persistpb.StoredRecord, bool, error)
- func CheckKey(key string) error
- func Find(manifest *wasm.Manifest, name string) *persistpb.RecordType
- func Validate(manifest *wasm.Manifest) error
- type Players
- type Refusal
- type Store
Constants ¶
const ( // LockFile is the exclusive process lock in a store directory. LockFile = "lock" // HolderFile names the process holding LockFile, without arguments. HolderFile = "holder" )
const ( // TypeLimit bounds the record types one mod declares. TypeLimit = 8 // FieldLimit bounds the fields of one record type. FieldLimit = 32 // RecordBytes bounds one record's encoded values. RecordBytes = 8 << 10 // OpLimit bounds the operations of one update. OpLimit = 32 // ManyLimit bounds the players one GetMany reads. ManyLimit = 16 // RecentKeys is how many update keys a record remembers. RecentKeys = 64 // UpdateKeyBytes bounds one update key. UpdateKeyBytes = 64 // TagLimit bounds the members of one tags field. TagLimit = 256 // TagBytes bounds one tag. TagBytes = 32 // StringBytes is a string field's bound when it declares none. StringBytes = 64 // StringLimit bounds what a string field may declare. StringLimit = 256 )
const Dir = ".modlock/persist"
Dir is the directory of a mod project that a local host keeps the mod's records in, one file per player.
const FileBytes = 6 * (TypeLimit*(RecordBytes+RecentKeys*(UpdateKeyBytes+2)) + 16<<10)
FileBytes bounds one player's JSON file: every type's binary record bound, its recent keys and envelope, allowing six JSON bytes for each source byte when strings and keys contain control characters.
const Service = "persist"
Service is the name mods call the persist service by.
Variables ¶
This section is empty.
Functions ¶
func Answer ¶
func Answer(store *Store, manifest *wasm.Manifest, players Players, method string, payload []byte) ([]byte, error)
Answer answers one call of the persist service from the mod manifest describes: its record types, its players and the store of its records. It returns the reply's bytes, or a Refusal for a call that breaks a rule or a bound. A call reaches a player's record only when it names the generation of the connection in the slot, so a Player a mod kept after its player left cannot reach the next player in the slot.
func Apply ¶
func Apply(recordType *persistpb.RecordType, stored *persistpb.StoredRecord, ops []*persistpb.Op, updateKey string) (*persistpb.StoredRecord, bool, error)
Apply returns stored, a record of recordType as a host keeps it or nil for one never written, changed by ops. An update whose key is among the record's recent keys is left out, and Apply reports it repeated with the record unchanged. It refuses the whole update, changing nothing, when an op breaks its field's rules or the result passes a bound. stored is not modified.
func CheckKey ¶
CheckKey refuses a record key: each player has one record of each type until hosts set a bound for keyed records.
Types ¶
type Players ¶
Players resolves a server slot to the SteamID64 of the player in it and the generation of their connection, and reports false for an empty slot. A host that does not learn generations reports zero, and every call naming that slot is refused: without the slot's generation a request made for a player who left would reach the next one.
type Refusal ¶
type Refusal string
Refusal is the error of a call a host refuses: the call broke a rule of its record type or a bound, and the record is unchanged. Its text tells the mod's author which.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store keeps one mod's records in a directory, one file per player, so they survive a restart of the host. Each update reads the player's file, applies its ops and replaces the file whole, one update at a time, so two updates of one record at once both land.
func Open ¶
Open returns the store of the records kept in dir, creating dir, and holds dir's lock until this process exits. Within one process each directory has one store. It refuses a directory another process holds, naming that process, so two hosts never write one mod's records at once.
func (*Store) Get ¶
Get returns the player steamID's record of recordType, empty for one never written.
func (*Store) Update ¶
func (s *Store) Update(steamID uint64, recordType *persistpb.RecordType, ops []*persistpb.Op, updateKey string) (*persistpb.Record, bool, error)
Update applies ops to the player steamID's record of recordType, as Apply does, and keeps the result. It returns the record after the update and whether the update's key made it a repeat.