From f40d09cb8f712e87691af4912f3630d92d692779 Mon Sep 17 00:00:00 2001 From: untodesu Date: Thu, 11 Dec 2025 15:14:26 +0500 Subject: Shuffle stuff around - Use the new and improved hierarchy I figured out when making Prospero chat - Re-add NSIS scripts, again from Prospero - Update most build and utility scripts with their most recent versions --- src/core/io/buffer.cc | 345 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 345 insertions(+) create mode 100644 src/core/io/buffer.cc (limited to 'src/core/io/buffer.cc') diff --git a/src/core/io/buffer.cc b/src/core/io/buffer.cc new file mode 100644 index 0000000..0f781a0 --- /dev/null +++ b/src/core/io/buffer.cc @@ -0,0 +1,345 @@ +#include "core/pch.hh" + +#include "core/io/buffer.hh" + +#include "core/math/constexpr.hh" + +io::ReadBuffer::ReadBuffer(const ReadBuffer& other) +{ + reset(other.data(), other.size()); +} + +io::ReadBuffer::ReadBuffer(const void* data, std::size_t size) +{ + assert(data); + + reset(data, size); +} + +io::ReadBuffer::ReadBuffer(const ENetPacket* packet) +{ + assert(packet); + + reset(packet); +} + +io::ReadBuffer::ReadBuffer(PHYSFS_File* file) +{ + assert(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) +{ + assert(data); + + auto bytes = reinterpret_cast(data); + m_vector.assign(bytes, bytes + size); + m_position = 0U; +} + +void io::ReadBuffer::reset(const ENetPacket* packet) +{ + assert(packet); + + auto bytes_ptr = reinterpret_cast(packet->data); + m_vector.assign(bytes_ptr, bytes_ptr + packet->dataLength); + m_position = 0; +} + +void io::ReadBuffer::reset(PHYSFS_File* file) +{ + assert(file); + + m_vector.resize(PHYSFS_fileLength(file)); + m_position = 0; + + PHYSFS_seek(file, 0); + PHYSFS_readBytes(file, m_vector.data(), m_vector.size()); +} + +template<> +std::byte io::ReadBuffer::read(void) +{ + if(m_position < m_vector.size()) { + auto result = m_vector[m_position]; + m_position += 1U; + return result; + } + + m_position += 1U; + return static_cast(0x00); +} + +template<> +std::uint8_t io::ReadBuffer::read(void) +{ + if((m_position + 1U) <= m_vector.size()) { + auto result = static_cast(m_vector[m_position]); + m_position += 1U; + return result; + } + + m_position += 1U; + return 0; +} + +template<> +std::uint16_t io::ReadBuffer::read(void) +{ + if((m_position + 2U) <= m_vector.size()) { + auto result = UINT16_C(0x0000); + result |= (UINT16_C(0x00FF) & static_cast(m_vector[m_position + 0U])) << 8U; + result |= (UINT16_C(0x00FF) & static_cast(m_vector[m_position + 1U])) << 0U; + m_position += 2U; + return result; + } + + m_position += 2U; + return 0; +} + +template<> +std::uint32_t io::ReadBuffer::read(void) +{ + if((m_position + 4U) <= m_vector.size()) { + auto result = UINT32_C(0x00000000); + result |= (UINT32_C(0x000000FF) & static_cast(m_vector[m_position + 0U])) << 24U; + result |= (UINT32_C(0x000000FF) & static_cast(m_vector[m_position + 1U])) << 16U; + result |= (UINT32_C(0x000000FF) & static_cast(m_vector[m_position + 2U])) << 8U; + result |= (UINT32_C(0x000000FF) & static_cast(m_vector[m_position + 3U])) << 0U; + m_position += 4U; + return result; + } + + m_position += 4U; + return 0; +} + +template<> +std::uint64_t io::ReadBuffer::read(void) +{ + if((m_position + 8U) <= m_vector.size()) { + auto result = UINT64_C(0x0000000000000000); + result |= (UINT64_C(0x00000000000000FF) & static_cast(m_vector[m_position + 0U])) << 56U; + result |= (UINT64_C(0x00000000000000FF) & static_cast(m_vector[m_position + 1U])) << 48U; + result |= (UINT64_C(0x00000000000000FF) & static_cast(m_vector[m_position + 2U])) << 40U; + result |= (UINT64_C(0x00000000000000FF) & static_cast(m_vector[m_position + 3U])) << 32U; + result |= (UINT64_C(0x00000000000000FF) & static_cast(m_vector[m_position + 4U])) << 24U; + result |= (UINT64_C(0x00000000000000FF) & static_cast(m_vector[m_position + 5U])) << 16U; + result |= (UINT64_C(0x00000000000000FF) & static_cast(m_vector[m_position + 6U])) << 8U; + result |= (UINT64_C(0x00000000000000FF) & static_cast(m_vector[m_position + 7U])) << 0U; + m_position += 8U; + return result; + } + + m_position += 8U; + return 0; +} + +template<> +float io::ReadBuffer::read(void) +{ + return std::bit_cast(read()); +} + +template<> +std::int8_t io::ReadBuffer::read(void) +{ + return std::bit_cast(read()); +} + +template<> +std::int16_t io::ReadBuffer::read(void) +{ + return std::bit_cast(read()); +} + +template<> +std::int32_t io::ReadBuffer::read(void) +{ + return std::bit_cast(read()); +} + +template<> +std::int64_t io::ReadBuffer::read(void) +{ + return std::bit_cast(read()); +} + +template<> +std::string io::ReadBuffer::read(void) +{ + std::string result; + result.resize(read()); + + for(std::size_t i = 0; i < result.size(); ++i) { + if(m_position < m_vector.size()) { + result[i] = static_cast(m_vector[m_position]); + } + + m_position += 1U; + } + + return result; +} + +void io::ReadBuffer::read(void* buffer, std::size_t size) +{ + auto bytes = reinterpret_cast(buffer); + auto amount_to_read = std::min(size, m_vector.size() - m_position); + + if(amount_to_read > 0) { + std::copy(m_vector.cbegin() + m_position, m_vector.cbegin() + m_position + amount_to_read, bytes); + } + + m_position += size; +} + +io::WriteBuffer::WriteBuffer(const WriteBuffer& other) +{ + m_vector = other.m_vector; +} + +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(const WriteBuffer& other) +{ + m_vector.insert(m_vector.end(), other.m_vector.begin(), other.m_vector.end()); +} + +void io::WriteBuffer::write(const void* data, std::size_t size) +{ + assert(data); + + auto bytes = reinterpret_cast(data); + m_vector.insert(m_vector.end(), bytes, bytes + size); +} + +template<> +void io::WriteBuffer::write(const std::byte value) +{ + m_vector.push_back(value); +} + +template<> +void io::WriteBuffer::write(const std::uint8_t value) +{ + m_vector.push_back(static_cast(value)); +} + +template<> +void io::WriteBuffer::write(const std::uint16_t value) +{ + m_vector.push_back(static_cast(UINT16_C(0xFF) & ((value & UINT16_C(0xFF00)) >> 8U))); + m_vector.push_back(static_cast(UINT16_C(0xFF) & ((value & UINT16_C(0x00FF)) >> 0U))); +} + +template<> +void io::WriteBuffer::write(const std::uint32_t value) +{ + m_vector.push_back(static_cast(UINT32_C(0xFF) & ((value & UINT32_C(0xFF000000)) >> 24U))); + m_vector.push_back(static_cast(UINT32_C(0xFF) & ((value & UINT32_C(0x00FF0000)) >> 16U))); + m_vector.push_back(static_cast(UINT32_C(0xFF) & ((value & UINT32_C(0x0000FF00)) >> 8U))); + m_vector.push_back(static_cast(UINT32_C(0xFF) & ((value & UINT32_C(0x000000FF)) >> 0U))); +} + +template<> +void io::WriteBuffer::write(const std::uint64_t value) +{ + m_vector.push_back(static_cast(UINT64_C(0xFF) & ((value & UINT64_C(0xFF00000000000000)) >> 56U))); + m_vector.push_back(static_cast(UINT64_C(0xFF) & ((value & UINT64_C(0x00FF000000000000)) >> 48U))); + m_vector.push_back(static_cast(UINT64_C(0xFF) & ((value & UINT64_C(0x0000FF0000000000)) >> 40U))); + m_vector.push_back(static_cast(UINT64_C(0xFF) & ((value & UINT64_C(0x000000FF00000000)) >> 32U))); + m_vector.push_back(static_cast(UINT64_C(0xFF) & ((value & UINT64_C(0x00000000FF000000)) >> 24U))); + m_vector.push_back(static_cast(UINT64_C(0xFF) & ((value & UINT64_C(0x0000000000FF0000)) >> 16U))); + m_vector.push_back(static_cast(UINT64_C(0xFF) & ((value & UINT64_C(0x000000000000FF00)) >> 8U))); + m_vector.push_back(static_cast(UINT64_C(0xFF) & ((value & UINT64_C(0x00000000000000FF)) >> 0U))); +} + +template<> +void io::WriteBuffer::write(const float value) +{ + write(std::bit_cast(value)); +} + +template<> +void io::WriteBuffer::write(const std::int8_t value) +{ + write(std::bit_cast(value)); +} + +template<> +void io::WriteBuffer::write(const std::int16_t value) +{ + write(std::bit_cast(value)); +} + +template<> +void io::WriteBuffer::write(const std::int32_t value) +{ + write(std::bit_cast(value)); +} + +template<> +void io::WriteBuffer::write(const std::int64_t value) +{ + write(std::bit_cast(value)); +} + +template<> +void io::WriteBuffer::write(const std::string_view value) +{ + write(static_cast(value.size())); + + for(const auto& character : value) { + m_vector.push_back(static_cast(character)); + } +} + +PHYSFS_File* io::WriteBuffer::to_file(const std::string& path, bool append) const +{ + PHYSFS_File* file = nullptr; + + if(append) { + file = PHYSFS_openAppend(path.c_str()); + } + else { + file = PHYSFS_openWrite(path.c_str()); + } + + if(file) { + PHYSFS_writeBytes(file, m_vector.data(), m_vector.size()); + } + + return file; +} + +ENetPacket* io::WriteBuffer::to_packet(enet_uint32 flags) const +{ + return enet_packet_create(m_vector.data(), m_vector.size(), flags); +} -- cgit