9#include "net/wire/bit_stream.h"
11namespace Vkm::Engine {
20inline constexpr uint32_t MAX_COMPONENT_BYTES = 512;
30bool sameOnWire(
const T& a,
const T& b) {
31 std::vector<uint8_t> first;
32 std::vector<uint8_t> second;
33 BitWriter one(first, MAX_COMPONENT_BYTES);
34 BitWriter two(second, MAX_COMPONENT_BYTES);
39 return one.bitCount() == two.bitCount() && first == second;
50void netKeep(T& held,
const T& incoming) {
94 NetSchema() =
default;
95 ~NetSchema() =
default;
97 NetSchema(
const NetSchema& other) =
delete;
98 NetSchema& operator=(
const NetSchema& other) =
delete;
100 NetSchema(NetSchema && other) =
delete;
101 NetSchema& operator=(NetSchema && other) =
delete;
117 template <
typename T>
121 const std::vector<NetType>&
types()
const {
return m_types; }
123 size_t size()
const {
return m_types.size(); }
165 std::vector<NetType> m_types;
171 type.
name = std::move(name);
176 netEncode(scene.
get<T>(entity), writer);
179 T* held = scene.
tryGet<T>(entity);
182 netDecode(scene.
add<T>(entity, T{}), reader);
183 return !reader.failed();
188 netDecode(incoming, reader);
189 if (reader.failed())
return false;
190 if (sameOnWire(*held, incoming)) netKeep(*held, incoming);
191 else *held = std::move(incoming);
196 add(std::move(type));
Reads back what a BitWriter wrote, in the same order.
Definition bit_stream.h:145
Packs values to the bit into a byte buffer.
Definition bit_stream.h:19
static NetSchema & get()
The registry every end consults. Filled by ScriptModule::setupNetwork, read thereafter.
void clear()
Drops everything.
Definition schema.h:159
static constexpr size_t MAX_TYPES
Most component types one game may replicate.
Definition schema.h:91
const std::vector< NetType > & types() const
Everything registered, in wire order.
Definition schema.h:121
void replicate(std::string name)
Add T to what replicates.
Definition schema.h:169
int indexOf(std::string_view name) const
Wire index of name, or -1.
uint32_t fingerprint() const
A number that differs when the lists differ, or the codecs' layout does.
std::string describe() const
Every name in order, for the log line that follows a refusal.
Central registry managing entities and an open set of component types.
Definition scene.h:29
T * tryGet(EntityId entity)
The entity's T, or null when it has none.
Definition scene.h:206
T & get(EntityId entity)
Get a mutable reference to an entity's component of type T.
Definition scene.h:176
bool has(EntityId entity) const
Check if an entity has a component of type T.
Definition scene.h:162
auto & add(EntityId entity, T &&component)
Give an entity a component it does not have yet.
Definition scene.h:128
void remove(EntityId entity)
Take a component of type T off an entity.
Definition scene.h:145
Names one entity of one Scene, for as long as that slot holds it.
Definition entity.h:23
One replicated component type, reduced to what the wire needs.
Definition schema.h:60
std::string name
The serializable identity, as everywhere else in the engine.
Definition schema.h:61
bool(* has)(const Scene &, EntityId)
Does this entity carry one? Decides its bit in a snapshot's mask.
Definition schema.h:64
bool(* decode)(Scene &, EntityId, BitReader &)
Decode onto this entity, adding the component when it is missing.
Definition schema.h:70
void(* remove)(Scene &, EntityId)
Take this entity's component away: the server holds it no longer.
Definition schema.h:73
void(* encode)(const Scene &, EntityId, BitWriter &)
Encode this entity's component. Asked only of an entity has said yes to.
Definition schema.h:67