summaryrefslogtreecommitdiffstats
path: root/core/io/buffer.cc
diff options
context:
space:
mode:
authoruntodesu <kirill@untode.su>2025-07-01 03:08:39 +0500
committeruntodesu <kirill@untode.su>2025-07-01 03:08:39 +0500
commit458e0005690ea9d579588a0a12368fc2c2c9a93a (patch)
tree588a9ca6cb3c76d9193b5bd4601d64f0e50e8c8c /core/io/buffer.cc
parentc7b0c8e0286a1b2bb7ec55e579137dfc3b22eeb9 (diff)
downloadvoxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.tar.bz2
voxelius-458e0005690ea9d579588a0a12368fc2c2c9a93a.zip
I hyper-focued on refactoring again
- I put a cool-sounding "we are number one" remix on repeat and straight up grinded the entire repository to a better state until 03:09 AM. I guess I have something wrong in my brain that makes me do this shit
Diffstat (limited to 'core/io/buffer.cc')
-rw-r--r--core/io/buffer.cc208
1 files changed, 208 insertions, 0 deletions
diff --git a/core/io/buffer.cc b/core/io/buffer.cc
new file mode 100644
index 0000000..62f981c
--- /dev/null
+++ b/core/io/buffer.cc
@@ -0,0 +1,208 @@
+#include "core/pch.hh"
+
+#include "core/io/buffer.hh"
+
+#include "core/math/constexpr.hh"
+
+io::ReadBuffer::ReadBuffer(const void* data, std::size_t size)
+{
+ reset(data, size);
+}
+
+io::ReadBuffer::ReadBuffer(const ENetPacket* packet)
+{
+ reset(packet);
+}
+
+io::ReadBuffer::ReadBuffer(PHYSFS_File* file)
+{
+ reset(file);
+}
+
+std::size_t io::ReadBuffer::size(void) const
+{
+ return m_vector.size();
+}
+
+const std::byte* io::ReadBuffer::data(void) const
+{
+ return m_vector.data();
+}
+
+void io::ReadBuffer::reset(const void* data, std::size_t size)
+{
+ auto bytes = reinterpret_cast<const std::byte*>(data);
+ m_vector.assign(bytes, bytes + size);
+ m_position = 0U;
+}
+
+void io::ReadBuffer::reset(const ENetPacket* packet)
+{
+ auto bytes_ptr = reinterpret_cast<const std::byte*>(packet->data);
+ m_vector.assign(bytes_ptr, bytes_ptr + packet->dataLength);
+ m_position = 0;
+}
+
+void io::ReadBuffer::reset(PHYSFS_File* file)
+{
+ m_vector.resize(PHYSFS_fileLength(file));
+ m_position = 0;
+
+ PHYSFS_seek(file, 0);
+ PHYSFS_readBytes(file, m_vector.data(), m_vector.size());
+}
+
+float io::ReadBuffer::read_FP32(void)
+{
+ return math::uint32_to_float(read_UI32());
+}
+
+std::uint8_t io::ReadBuffer::read_UI8(void)
+{
+ if((m_position + 1U) <= m_vector.size()) {
+ auto result = static_cast<std::uint8_t>(m_vector[m_position]);
+ m_position += 1U;
+ return result;
+ }
+
+ m_position += 1U;
+ return 0;
+}
+
+std::uint16_t io::ReadBuffer::read_UI16(void)
+{
+ if((m_position + 2U) <= m_vector.size()) {
+ auto result = UINT16_C(0x0000);
+ result |= static_cast<std::uint16_t>(m_vector[m_position + 0U]) << 8U;
+ result |= static_cast<std::uint16_t>(m_vector[m_position + 1U]) << 0U;
+ m_position += 2U;
+ return result;
+ }
+
+ m_position += 2U;
+ return 0;
+}
+
+std::uint32_t io::ReadBuffer::read_UI32(void)
+{
+ if((m_position + 4U) <= m_vector.size()) {
+ auto result = UINT32_C(0x00000000);
+ result |= static_cast<std::uint32_t>(m_vector[m_position + 0U]) << 24U;
+ result |= static_cast<std::uint32_t>(m_vector[m_position + 1U]) << 16U;
+ result |= static_cast<std::uint32_t>(m_vector[m_position + 2U]) << 8U;
+ result |= static_cast<std::uint32_t>(m_vector[m_position + 3U]) << 0U;
+ m_position += 4U;
+ return result;
+ }
+
+ m_position += 4U;
+ return 0;
+}
+
+std::uint64_t io::ReadBuffer::read_UI64(void)
+{
+ if((m_position + 8U) <= m_vector.size()) {
+ auto result = UINT64_C(0x0000000000000000);
+ result |= (0x00000000000000FF & static_cast<std::uint64_t>(m_vector[m_position + 0U])) << 56U;
+ result |= (0x00000000000000FF & static_cast<std::uint64_t>(m_vector[m_position + 1U])) << 48U;
+ result |= (0x00000000000000FF & static_cast<std::uint64_t>(m_vector[m_position + 2U])) << 40U;
+ result |= (0x00000000000000FF & static_cast<std::uint64_t>(m_vector[m_position + 3U])) << 32U;
+ result |= (0x00000000000000FF & static_cast<std::uint64_t>(m_vector[m_position + 4U])) << 24U;
+ result |= (0x00000000000000FF & static_cast<std::uint64_t>(m_vector[m_position + 5U])) << 16U;
+ result |= (0x00000000000000FF & static_cast<std::uint64_t>(m_vector[m_position + 6U])) << 8U;
+ result |= (0x00000000000000FF & static_cast<std::uint64_t>(m_vector[m_position + 7U])) << 0U;
+ m_position += 8U;
+ return result;
+ }
+
+ m_position += 8U;
+ return 0;
+}
+
+std::string io::ReadBuffer::read_string(void)
+{
+ auto size = static_cast<std::size_t>(read_UI16());
+ auto result = std::string();
+
+ for(std::size_t i = 0; i < size; ++i) {
+ if(m_position < m_vector.size()) {
+ result.push_back(static_cast<char>(m_vector[m_position]));
+ }
+
+ m_position += 1U;
+ }
+
+ return result;
+}
+
+std::size_t io::WriteBuffer::size(void) const
+{
+ return m_vector.size();
+}
+
+const std::byte* io::WriteBuffer::data(void) const
+{
+ return m_vector.data();
+}
+
+void io::WriteBuffer::reset(void)
+{
+ m_vector.clear();
+}
+
+void io::WriteBuffer::write_UI8(std::uint8_t value)
+{
+ m_vector.push_back(static_cast<std::byte>(value));
+}
+
+void io::WriteBuffer::write_UI16(std::uint16_t value)
+{
+ m_vector.push_back(static_cast<std::byte>(UINT16_C(0xFF) & ((value & UINT16_C(0xFF00)) >> 8U)));
+ m_vector.push_back(static_cast<std::byte>(UINT16_C(0xFF) & ((value & UINT16_C(0x00FF)) >> 0U)));
+}
+
+void io::WriteBuffer::write_UI32(std::uint32_t value)
+{
+ m_vector.push_back(static_cast<std::byte>(UINT32_C(0xFF) & ((value & UINT32_C(0xFF000000)) >> 24U)));
+ m_vector.push_back(static_cast<std::byte>(UINT32_C(0xFF) & ((value & UINT32_C(0x00FF0000)) >> 16U)));
+ m_vector.push_back(static_cast<std::byte>(UINT32_C(0xFF) & ((value & UINT32_C(0x0000FF00)) >> 8U)));
+ m_vector.push_back(static_cast<std::byte>(UINT32_C(0xFF) & ((value & UINT32_C(0x000000FF)) >> 0U)));
+}
+
+void io::WriteBuffer::write_UI64(std::uint64_t value)
+{
+ m_vector.push_back(static_cast<std::byte>(UINT64_C(0xFF) & ((value & UINT64_C(0xFF00000000000000)) >> 56U)));
+ m_vector.push_back(static_cast<std::byte>(UINT64_C(0xFF) & ((value & UINT64_C(0x00FF000000000000)) >> 48U)));
+ m_vector.push_back(static_cast<std::byte>(UINT64_C(0xFF) & ((value & UINT64_C(0x0000FF0000000000)) >> 40U)));
+ m_vector.push_back(static_cast<std::byte>(UINT64_C(0xFF) & ((value & UINT64_C(0x000000FF00000000)) >> 32U)));
+ m_vector.push_back(static_cast<std::byte>(UINT64_C(0xFF) & ((value & UINT64_C(0x00000000FF000000)) >> 24U)));
+ m_vector.push_back(static_cast<std::byte>(UINT64_C(0xFF) & ((value & UINT64_C(0x0000000000FF0000)) >> 16U)));
+ m_vector.push_back(static_cast<std::byte>(UINT64_C(0xFF) & ((value & UINT64_C(0x000000000000FF00)) >> 8U)));
+ m_vector.push_back(static_cast<std::byte>(UINT64_C(0xFF) & ((value & UINT64_C(0x00000000000000FF)) >> 0U)));
+}
+
+void io::WriteBuffer::write_string(const std::string& value)
+{
+ const std::size_t size = math::min<std::size_t>(UINT16_MAX, value.size());
+
+ write_UI16(static_cast<std::uint16_t>(size));
+
+ for(std::size_t i = 0; i < size; m_vector.push_back(static_cast<std::byte>(value[i++]))) {
+ // empty
+ }
+}
+
+PHYSFS_File* io::WriteBuffer::to_file(const char* path, bool append) const
+{
+ if(auto file = (append ? PHYSFS_openAppend(path) : PHYSFS_openWrite(path))) {
+ PHYSFS_writeBytes(file, m_vector.data(), m_vector.size());
+ return file;
+ }
+
+ return nullptr;
+}
+
+ENetPacket* io::WriteBuffer::to_packet(enet_uint32 flags) const
+{
+ return enet_packet_create(m_vector.data(), m_vector.size(), flags);
+}