From a49bb23cff285bcbf293bfc1e45f72c8569016a7 Mon Sep 17 00:00:00 2001 From: Vincent Date: Fri, 13 Sep 2024 17:38:09 +0100 Subject: [PATCH] refactor: DataIO and StringByteOutput class --- .../bedrock/common/util/bytes_data_output.h | 126 ------------------ include/bedrock/common/util/data_io.h | 19 +++ .../util/string_byte_output.h} | 20 ++- include/bedrock/nbt/tag.h | 1 - src/endstone_devtools/devtools.cpp | 2 +- src/endstone_devtools/vanilla_data.cpp | 2 +- .../bedrock/common/util/data_io.cpp | 57 ++++++++ .../common/util/string_byte_output.cpp | 68 ++++++++++ 8 files changed, 162 insertions(+), 133 deletions(-) delete mode 100644 include/bedrock/common/util/bytes_data_output.h rename include/bedrock/{core/utility/print_stream.h => common/util/string_byte_output.h} (54%) create mode 100644 src/endstone_runtime/bedrock/common/util/data_io.cpp create mode 100644 src/endstone_runtime/bedrock/common/util/string_byte_output.cpp diff --git a/include/bedrock/common/util/bytes_data_output.h b/include/bedrock/common/util/bytes_data_output.h deleted file mode 100644 index f56575486..000000000 --- a/include/bedrock/common/util/bytes_data_output.h +++ /dev/null @@ -1,126 +0,0 @@ -// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#pragma once - -#include "bedrock/common/util/data_io.h" - -class BytesDataOutput : public IDataOutput { - -public: - ~BytesDataOutput() override = default; - void writeString(std::string_view v) override - { - writeShort(static_cast(v.size() & 0x7fff)); - writeBytes(v.data(), v.size() & 0x7fff); - } - - void writeLongString(std::string_view v) override - { - writeInt(static_cast(v.size())); - writeBytes(v.data(), v.size()); - } - - void writeFloat(float v) override - { - writeBytes(&v, sizeof(float)); - } - - void writeDouble(double v) override - { - writeBytes(&v, sizeof(double)); - } - - void writeByte(std::uint8_t v) override - { - writeBytes(&v, sizeof(std::uint8_t)); - } - - void writeShort(std::int16_t v) override - { - writeBytes(&v, sizeof(std::int16_t)); - } - - void writeInt(std::int32_t v) override - { - writeBytes(&v, sizeof(std::int32_t)); - } - - void writeLongLong(std::int64_t v) override - { - writeBytes(&v, sizeof(std::int64_t)); - } -}; - -class StringByteOutput : public BytesDataOutput { -public: - ~StringByteOutput() override = default; - void writeBytes(const void *data, std::uint64_t bytes) override - { - buffer.append(static_cast(data), bytes); - } - std::string buffer; -}; - -class BigEndianStringByteOutput : public StringByteOutput { -public: - ~BigEndianStringByteOutput() override = default; - void writeFloat(float v) override - { - auto p = reinterpret_cast(v); - writeByte((p >> 24) & 0xff); - writeByte((p >> 16) & 0xff); - writeByte((p >> 8) & 0xff); - writeByte(p & 0xff); - } - - void writeDouble(double v) override - { - auto p = reinterpret_cast(v); - writeByte((p >> 56) & 0xff); - writeByte((p >> 48) & 0xff); - writeByte((p >> 40) & 0xff); - writeByte((p >> 32) & 0xff); - writeByte((p >> 24) & 0xff); - writeByte((p >> 16) & 0xff); - writeByte((p >> 8) & 0xff); - writeByte(p & 0xff); - } - - void writeShort(std::int16_t v) override - { - writeByte((v >> 8) & 0xff); - writeByte(v & 0xff); - } - - void writeInt(std::int32_t v) override - { - writeByte((v >> 24) & 0xff); - writeByte((v >> 16) & 0xff); - writeByte((v >> 8) & 0xff); - writeByte(v & 0xff); - } - - void writeLongLong(std::int64_t v) override - { - writeByte((v >> 56) & 0xff); - writeByte((v >> 48) & 0xff); - writeByte((v >> 40) & 0xff); - writeByte((v >> 32) & 0xff); - writeByte((v >> 24) & 0xff); - writeByte((v >> 16) & 0xff); - writeByte((v >> 8) & 0xff); - writeByte(v & 0xff); - } -}; diff --git a/include/bedrock/common/util/data_io.h b/include/bedrock/common/util/data_io.h index 66d3365ee..2e7e8dd7a 100644 --- a/include/bedrock/common/util/data_io.h +++ b/include/bedrock/common/util/data_io.h @@ -47,3 +47,22 @@ class IDataOutput { virtual void writeLongLong(std::int64_t v) = 0; virtual void writeBytes(void const *data, std::uint64_t bytes) = 0; }; + +class BytesDataOutput : public IDataOutput { +public: + ~BytesDataOutput() override = default; + void writeString(std::string_view v) override; + void writeLongString(std::string_view v) override; + void writeFloat(float v) override; + void writeDouble(double v) override; + void writeByte(std::uint8_t v) override; + void writeShort(std::int16_t v) override; + void writeInt(std::int32_t v) override; + void writeLongLong(std::int64_t v) override; +}; + +class PrintStream { +public: + virtual ~PrintStream() = 0; + virtual void print(std::string const &) = 0; +}; diff --git a/include/bedrock/core/utility/print_stream.h b/include/bedrock/common/util/string_byte_output.h similarity index 54% rename from include/bedrock/core/utility/print_stream.h rename to include/bedrock/common/util/string_byte_output.h index c3a539b9c..d023612dd 100644 --- a/include/bedrock/core/utility/print_stream.h +++ b/include/bedrock/common/util/string_byte_output.h @@ -14,10 +14,22 @@ #pragma once -#include +#include "bedrock/common/util/data_io.h" -class PrintStream { +class StringByteOutput : public BytesDataOutput { public: - virtual ~PrintStream() = 0; - virtual void print(std::string const &) = 0; + ~StringByteOutput() override = default; + void writeBytes(const void *data, std::uint64_t bytes) override; + + std::string buffer; +}; + +class BigEndianStringByteOutput : public StringByteOutput { +public: + ~BigEndianStringByteOutput() override = default; + void writeFloat(float v) override; + void writeDouble(double v) override; + void writeShort(std::int16_t v) override; + void writeInt(std::int32_t v) override; + void writeLongLong(std::int64_t v) override; }; diff --git a/include/bedrock/nbt/tag.h b/include/bedrock/nbt/tag.h index 2d6965ea1..0d269fbfe 100644 --- a/include/bedrock/nbt/tag.h +++ b/include/bedrock/nbt/tag.h @@ -16,7 +16,6 @@ #include "bedrock/common/util/data_io.h" #include "bedrock/core/result.h" -#include "bedrock/core/utility/print_stream.h" class Tag { public: diff --git a/src/endstone_devtools/devtools.cpp b/src/endstone_devtools/devtools.cpp index 1233288ac..99006f427 100644 --- a/src/endstone_devtools/devtools.cpp +++ b/src/endstone_devtools/devtools.cpp @@ -30,7 +30,7 @@ #include #include -#include "bedrock/common/util/bytes_data_output.h" +#include "bedrock/common/util/string_byte_output.h" #include "bedrock/nbt/nbt_io.h" #include "endstone/color_format.h" #include "endstone/detail/devtools/imgui/imgui_json.h" diff --git a/src/endstone_devtools/vanilla_data.cpp b/src/endstone_devtools/vanilla_data.cpp index 7f1196f9b..1944ca1d2 100644 --- a/src/endstone_devtools/vanilla_data.cpp +++ b/src/endstone_devtools/vanilla_data.cpp @@ -16,7 +16,7 @@ #include -#include "bedrock/common/util/bytes_data_output.h" +#include "bedrock/common/util/string_byte_output.h" #include "bedrock/nbt/nbt_io.h" #include "bedrock/network/packet/crafting_data_packet.h" #include "bedrock/world/item/registry/creative_item_registry.h" diff --git a/src/endstone_runtime/bedrock/common/util/data_io.cpp b/src/endstone_runtime/bedrock/common/util/data_io.cpp new file mode 100644 index 000000000..904e04501 --- /dev/null +++ b/src/endstone_runtime/bedrock/common/util/data_io.cpp @@ -0,0 +1,57 @@ +// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "bedrock/common/util/data_io.h" + +void BytesDataOutput::writeString(std::string_view v) +{ + writeShort(static_cast(v.size() & 0x7fff)); + writeBytes(v.data(), v.size() & 0x7fff); +} + +void BytesDataOutput::writeLongString(std::string_view v) +{ + writeInt(static_cast(v.size())); + writeBytes(v.data(), v.size()); +} + +void BytesDataOutput::writeFloat(float v) +{ + writeBytes(&v, sizeof(float)); +} + +void BytesDataOutput::writeDouble(double v) +{ + writeBytes(&v, sizeof(double)); +} + +void BytesDataOutput::writeByte(std::uint8_t v) +{ + writeBytes(&v, sizeof(std::uint8_t)); +} + +void BytesDataOutput::writeShort(std::int16_t v) +{ + writeBytes(&v, sizeof(std::int16_t)); +} + +void BytesDataOutput::writeInt(std::int32_t v) +{ + writeBytes(&v, sizeof(std::int32_t)); +} + +void BytesDataOutput::writeLongLong(std::int64_t v) +{ + writeBytes(&v, sizeof(std::int64_t)); +} diff --git a/src/endstone_runtime/bedrock/common/util/string_byte_output.cpp b/src/endstone_runtime/bedrock/common/util/string_byte_output.cpp new file mode 100644 index 000000000..39fd0b271 --- /dev/null +++ b/src/endstone_runtime/bedrock/common/util/string_byte_output.cpp @@ -0,0 +1,68 @@ +// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "bedrock/common/util/string_byte_output.h" + +void StringByteOutput::writeBytes(const void *data, std::uint64_t bytes) +{ + buffer.append(static_cast(data), bytes); +} + +void BigEndianStringByteOutput::writeFloat(float v) +{ + auto p = reinterpret_cast(v); + writeByte((p >> 24) & 0xff); + writeByte((p >> 16) & 0xff); + writeByte((p >> 8) & 0xff); + writeByte(p & 0xff); +} + +void BigEndianStringByteOutput::writeDouble(double v) +{ + auto p = reinterpret_cast(v); + writeByte((p >> 56) & 0xff); + writeByte((p >> 48) & 0xff); + writeByte((p >> 40) & 0xff); + writeByte((p >> 32) & 0xff); + writeByte((p >> 24) & 0xff); + writeByte((p >> 16) & 0xff); + writeByte((p >> 8) & 0xff); + writeByte(p & 0xff); +} + +void BigEndianStringByteOutput::writeShort(std::int16_t v) +{ + writeByte((v >> 8) & 0xff); + writeByte(v & 0xff); +} + +void BigEndianStringByteOutput::writeInt(std::int32_t v) +{ + writeByte((v >> 24) & 0xff); + writeByte((v >> 16) & 0xff); + writeByte((v >> 8) & 0xff); + writeByte(v & 0xff); +} + +void BigEndianStringByteOutput::writeLongLong(std::int64_t v) +{ + writeByte((v >> 56) & 0xff); + writeByte((v >> 48) & 0xff); + writeByte((v >> 40) & 0xff); + writeByte((v >> 32) & 0xff); + writeByte((v >> 24) & 0xff); + writeByte((v >> 16) & 0xff); + writeByte((v >> 8) & 0xff); + writeByte(v & 0xff); +}