+const char *Packet::Type2String(uint8_t t) noexcept {
+ switch (t) {
+ case Ping::TYPE:
+ return "Ping";
+ case Login::TYPE:
+ return "Login";
+ case Join::TYPE:
+ return "Join";
+ case Part::TYPE:
+ return "Part";
+ case PlayerUpdate::TYPE:
+ return "PlayerUpdate";
+ case SpawnEntity::TYPE:
+ return "SpawnEntity";
+ case DespawnEntity::TYPE:
+ return "DespawnEntity";
+ case EntityUpdate::TYPE:
+ return "EntityUpdate";
+ default:
+ return "Unknown";
+ }
+}
+
+template<class T>
+void Packet::Payload::Write(const T &src, size_t off) noexcept {
+ if ((length - off) < sizeof(T)) {
+ // dismiss out of bounds write
+ return;
+ }
+ *reinterpret_cast<T *>(&data[off]) = src;
+}
+
+template<class T>
+void Packet::Payload::Read(T &dst, size_t off) const noexcept {
+ if ((length - off) < sizeof(T)) {
+ // dismiss out of bounds read
+ return;
+ }
+ dst = *reinterpret_cast<T *>(&data[off]);
+}
+
+void Packet::Payload::WriteString(const string &src, size_t off, size_t maxlen) noexcept {
+ uint8_t *dst = &data[off];
+ size_t len = min(maxlen, length - off);
+ if (src.size() < len) {
+ memset(dst, '\0', len);
+ memcpy(dst, src.c_str(), src.size());
+ } else {
+ memcpy(dst, src.c_str(), len);
+ }
+}
+
+void Packet::Payload::ReadString(string &dst, size_t off, size_t maxlen) const noexcept {
+ size_t len = min(maxlen, length - off);
+ dst.clear();
+ dst.reserve(len);
+ for (size_t i = 0; i < len && data[off + i] != '\0'; ++i) {
+ dst.push_back(data[off + i]);
+ }
+}
+
+
+void Packet::Login::WritePlayerName(const string &name) noexcept {
+ WriteString(name, 0, 32);
+}
+
+void Packet::Login::ReadPlayerName(string &name) const noexcept {
+ ReadString(name, 0, 32);
+}
+
+void Packet::Join::WritePlayer(const Entity &player) noexcept {
+ Write(player.ID(), 0);
+ Write(player.ChunkCoords(), 4);
+ Write(player.Position(), 16);
+ Write(player.Velocity(), 28);
+ Write(player.Orientation(), 40);
+ Write(player.AngularVelocity(), 56);
+}
+
+void Packet::Join::ReadPlayerID(uint32_t &id) const noexcept {
+ Read(id, 0);
+}
+
+void Packet::Join::ReadPlayer(Entity &player) const noexcept {
+ glm::ivec3 chunk_coords(0);
+ glm::vec3 pos;
+ glm::vec3 vel;
+ glm::quat rot;
+ glm::vec3 ang;
+
+ Read(chunk_coords, 4);
+ Read(pos, 16);
+ Read(vel, 28);
+ Read(rot, 40);
+ Read(ang, 56);
+
+ player.Position(chunk_coords, pos);
+ player.Velocity(vel);
+ player.Orientation(rot);
+ player.AngularVelocity(ang);
+}
+
+void Packet::Join::WriteWorldName(const string &name) noexcept {
+ WriteString(name, 68, 32);
+}
+
+void Packet::Join::ReadWorldName(string &name) const noexcept {
+ ReadString(name, 68, 32);
+}
+
+void Packet::PlayerUpdate::WritePlayer(const Entity &player) noexcept {
+ Write(player.ChunkCoords(), 0);
+ Write(player.Position(), 12);
+ Write(player.Velocity(), 24);
+ Write(player.Orientation(), 36);
+ Write(player.AngularVelocity(), 52);
+}
+
+void Packet::PlayerUpdate::ReadPlayer(Entity &player) const noexcept {
+ glm::ivec3 chunk_coords(0);
+ glm::vec3 pos;
+ glm::vec3 vel;
+ glm::quat rot;
+ glm::vec3 ang;
+
+ Read(chunk_coords, 0);
+ Read(pos, 12);
+ Read(vel, 24);
+ Read(rot, 36);
+ Read(ang, 52);
+
+ player.Position(chunk_coords, pos);
+ player.Velocity(vel);
+ player.Orientation(rot);
+ player.AngularVelocity(ang);
+}
+
+void Packet::SpawnEntity::WriteEntity(const Entity &e) noexcept {
+ Write(e.ID(), 0);
+ Write(e.ChunkCoords(), 4);
+ Write(e.Position(), 16);
+ Write(e.Velocity(), 28);
+ Write(e.Orientation(), 40);
+ Write(e.AngularVelocity(), 56);
+ Write(e.Bounds(), 68);
+ uint32_t flags = 0;
+ if (e.WorldCollidable()) {
+ flags |= 1;
+ }
+ Write(flags, 92);
+ WriteString(e.Name(), 96, 32);
+}
+
+void Packet::SpawnEntity::ReadEntityID(uint32_t &id) const noexcept {
+ Read(id, 0);
+}
+
+void Packet::SpawnEntity::ReadEntity(Entity &e) const noexcept {
+ glm::ivec3 chunk_coords(0);
+ glm::vec3 pos;
+ glm::vec3 vel;
+ glm::quat rot;
+ glm::vec3 ang;
+ AABB bounds;
+ uint32_t flags = 0;
+ string name;
+
+ Read(chunk_coords, 4);
+ Read(pos, 16);
+ Read(vel, 28);
+ Read(rot, 40);
+ Read(ang, 56);
+ Read(bounds, 68);
+ Read(flags, 92);
+ ReadString(name, 96, 32);
+
+ e.Position(chunk_coords, pos);
+ e.Velocity(vel);
+ e.Orientation(rot);
+ e.AngularVelocity(ang);
+ e.Bounds(bounds);
+ e.WorldCollidable(flags & 1);
+ e.Name(name);
+}
+
+void Packet::DespawnEntity::WriteEntityID(uint32_t id) noexcept {
+ Write(id, 0);
+}
+
+void Packet::DespawnEntity::ReadEntityID(uint32_t &id) const noexcept {
+ Read(id, 0);
+}
+
+void Packet::EntityUpdate::WriteEntityCount(uint32_t count) noexcept {
+ Write(count, 0);
+}
+
+void Packet::EntityUpdate::ReadEntityCount(uint32_t &count) const noexcept {
+ Read(count, 0);
+}
+
+void Packet::EntityUpdate::WriteEntity(const Entity &entity, uint32_t num) noexcept {
+ uint32_t off = 4 + (num * 64);
+
+ Write(entity.ID(), off);
+ Write(entity.ChunkCoords(), off + 4);
+ Write(entity.Position(), off + 16);
+ Write(entity.Velocity(), off + 28);
+ Write(entity.Orientation(), off + 40);
+ Write(entity.AngularVelocity(), off + 56);