vkmEngine 1.0.0
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
schema.h
1#pragma once
2
3#include <cstdint>
4#include <string>
5#include <string_view>
6#include <vector>
7
8#include "ecs/scene.h"
9#include "net/wire/bit_stream.h"
10
11namespace Vkm::Engine {
12
18struct NetType {
19 std::string name;
20
22 bool (*has)(const Scene&, EntityId) = nullptr;
23
25 void (*encode)(const Scene&, EntityId, BitWriter&) = nullptr;
26
28 bool (*decode)(Scene&, EntityId, BitReader&) = nullptr;
29};
30
39class NetSchema {
40 public:
46 static constexpr size_t MAX_TYPES = 32;
47
48 public:
49 NetSchema() = default;
50 ~NetSchema() = default;
51
52 NetSchema(const NetSchema& other) = delete;
53 NetSchema& operator=(const NetSchema& other) = delete;
54
55 NetSchema(NetSchema && other) = delete;
56 NetSchema& operator=(NetSchema && other) = delete;
57
58 public:
60 static NetSchema& get();
61
62 public:
72 template <typename T>
73 void replicate(std::string name);
74
76 const std::vector<NetType>& types() const { return m_types; }
77
78 size_t size() const { return m_types.size(); }
79
88 int indexOf(std::string_view name) const;
89
99 uint32_t fingerprint() const;
100
106 std::string describe() const;
107
114 void clear() { m_types.clear(); }
115
116 private:
117 void add(NetType type);
118
119 private:
120 std::vector<NetType> m_types;
121};
122
123template <typename T>
124void NetSchema::replicate(std::string name) {
125 NetType type;
126 type.name = std::move(name);
127
128 // Stateless thunks: plain function pointers, nothing owned.
129 type.has = [](const Scene& scene, EntityId entity) { return scene.has<T>(entity); };
130 type.encode = [](const Scene& scene, EntityId entity, BitWriter& writer) {
131 netEncode(scene.get<T>(entity), writer);
132 };
133 type.decode = [](Scene& scene, EntityId entity, BitReader& reader) {
134 // Added, not required: a client learns of a component by being sent one.
135 T* held = scene.tryGet<T>(entity);
136 if (!held) held = &scene.add<T>(entity, T{});
137 netDecode(*held, reader);
138 return !reader.failed();
139 };
140
141 add(std::move(type));
142}
143
144} // namespace Vkm::Engine
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:114
static constexpr size_t MAX_TYPES
Most component types one game may replicate.
Definition schema.h:46
const std::vector< NetType > & types() const
Everything registered, in wire order.
Definition schema.h:76
void replicate(std::string name)
Add T to what replicates.
Definition schema.h:124
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
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:18
std::string name
The serializable identity, as everywhere else in the engine.
Definition schema.h:19
bool(* has)(const Scene &, EntityId)
Does this entity carry one? Decides its bit in a snapshot's mask.
Definition schema.h:22
bool(* decode)(Scene &, EntityId, BitReader &)
Decode onto this entity, adding the component when it is missing.
Definition schema.h:28
void(* encode)(const Scene &, EntityId, BitWriter &)
Encode this entity's component. Asked only of an entity has said yes to.
Definition schema.h:25