Skip to content

Commit

Permalink
fix(network): fix methods in BinaryStream
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Aug 6, 2024
1 parent 41b2584 commit 4ce6d82
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
14 changes: 7 additions & 7 deletions include/bedrock/core/utility/binary_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class ReadOnlyBinaryStream {
virtual Bedrock::Result<void> read(void *, std::uint64_t);

private:
std::size_t read_pointer_;
bool has_overflowed_;
std::string owned_buffer_;
std::string *buffer_;
std::size_t read_pointer_; // +8
bool has_overflowed_; // +16
std::string owned_buffer_; // +24
std::string *buffer_; // +56
};

class BinaryStream : public ReadOnlyBinaryStream {
Expand All @@ -40,10 +40,10 @@ class BinaryStream : public ReadOnlyBinaryStream {
void writeVarInt64(std::int64_t value);
void writeUnsignedVarInt(std::uint32_t value);
void writeUnsignedVarInt64(std::uint64_t value);
void writeString(const std::string &value);
void writeString(std::string_view value);
void writeFloat(float value);

private:
std::string owned_buffer_;
std::string *buffer_;
std::string owned_buffer_; // +64
std::string *buffer_; // +96
};
4 changes: 3 additions & 1 deletion src/endstone_core/network/packet_codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <stdexcept>

#include <endstone/network/spawn_particle_effect_packet.h>
#include <fmt/format.h>

namespace endstone::detail {

Expand All @@ -25,8 +26,9 @@ void PacketCodec::encode(BinaryStream &stream, Packet &packet)
switch (packet.getType()) {
case PacketType::SpawnParticleEffect:
encode(stream, static_cast<SpawnParticleEffectPacket &>(packet));
break;
default:
throw std::runtime_error("Not supported");
throw std::runtime_error(fmt::format("Packet type {} is not supported.", static_cast<int>(packet.getType())));
}
}
} // namespace endstone::detail
1 change: 1 addition & 0 deletions src/endstone_python/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void init_network(py::module_ &m)

py::class_<SpawnParticleEffectPacket, Packet>(m, "SpawnParticleEffectPacket",
"Represents a packet for spawning a particle effect.")
.def(py::init<>())
.def_readwrite("dimension_id", &SpawnParticleEffectPacket::dimension_id)
.def_readwrite("actor_id", &SpawnParticleEffectPacket::actor_id)
.def_readwrite("position", &SpawnParticleEffectPacket::position)
Expand Down
12 changes: 7 additions & 5 deletions src/endstone_runtime/bedrock/core/utility/binary_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@

void BinaryStream::write(const void *data, std::size_t size)
{
buffer_->append(static_cast<const char *>(data), size);
if (size > 0) {
buffer_->append(static_cast<const char *>(data), size);
}
}

void BinaryStream::writeUnsignedChar(std::uint8_t value)
{
buffer_ += static_cast<char>(value);
write(&value, sizeof(std::uint8_t));
}

void BinaryStream::writeByte(std::uint8_t value)
{
buffer_ += static_cast<char>(value);
write(&value, sizeof(std::uint8_t));
}

void BinaryStream::writeBool(bool value)
Expand Down Expand Up @@ -74,10 +76,10 @@ void BinaryStream::writeUnsignedVarInt64(std::uint64_t value)
} while (value);
}

void BinaryStream::writeString(const std::string &value)
void BinaryStream::writeString(std::string_view value)
{
writeUnsignedVarInt(value.length());
buffer_->append(value);
write(value.data(), value.size());
}

void BinaryStream::writeFloat(float value)
Expand Down

0 comments on commit 4ce6d82

Please sign in to comment.