Skip to content

Commit

Permalink
refactor: DataIO and StringByteOutput class
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Sep 13, 2024
1 parent 32b1de9 commit a49bb23
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 133 deletions.
126 changes: 0 additions & 126 deletions include/bedrock/common/util/bytes_data_output.h

This file was deleted.

19 changes: 19 additions & 0 deletions include/bedrock/common/util/data_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,22 @@

#pragma once

#include <string>
#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;
};
1 change: 0 additions & 1 deletion include/bedrock/nbt/tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/endstone_devtools/devtools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <GLFW/glfw3.h>
#include <entt/entt.hpp>

#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"
Expand Down
2 changes: 1 addition & 1 deletion src/endstone_devtools/vanilla_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#include <magic_enum/magic_enum.hpp>

#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"
Expand Down
57 changes: 57 additions & 0 deletions src/endstone_runtime/bedrock/common/util/data_io.cpp
Original file line number Diff line number Diff line change
@@ -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<std::int16_t>(v.size() & 0x7fff));
writeBytes(v.data(), v.size() & 0x7fff);
}

void BytesDataOutput::writeLongString(std::string_view v)
{
writeInt(static_cast<std::int32_t>(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));
}
68 changes: 68 additions & 0 deletions src/endstone_runtime/bedrock/common/util/string_byte_output.cpp
Original file line number Diff line number Diff line change
@@ -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<const char *>(data), bytes);
}

void BigEndianStringByteOutput::writeFloat(float v)
{
auto p = reinterpret_cast<std::uint32_t &>(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<std::uint64_t &>(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);
}

0 comments on commit a49bb23

Please sign in to comment.