From d82a6afd9e8be53d60043c8ab49223ac9470ad46 Mon Sep 17 00:00:00 2001 From: Ash Date: Mon, 4 Nov 2024 02:33:40 +1100 Subject: Overhaul endian handling in ByteBuffer and FastNBT (#5543) * Overhaul endian handling in ByteBuffer and FastNBT Rather than juggling "swapped" and "unswapped" versions of integers, different library functions, #defines, etc., simply always read everything byte-by-byte. This works regardless of host CPU endian, got optimised down to either a normal load or a byteswap on every compiler I tested - only 1 instruction on most CPU architectures. This commit introduces a "Bytes" array type to keep endian-sensitive data seperate from host data, alongside the needed C++ template machinery for it to work seamlessly. This approach is a little bit safer as well since you get length- and type-checking for most callsites. * Remove remaining references to old-style endianness conversion, remove functions themselves. --------- Co-authored-by: Alexander Harkness --- Tools/QtBiomeVisualiser/ChunkSource.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'Tools') diff --git a/Tools/QtBiomeVisualiser/ChunkSource.cpp b/Tools/QtBiomeVisualiser/ChunkSource.cpp index ea3346f04..120b72474 100644 --- a/Tools/QtBiomeVisualiser/ChunkSource.cpp +++ b/Tools/QtBiomeVisualiser/ChunkSource.cpp @@ -5,6 +5,7 @@ #include "src/StringCompression.h" #include "src/WorldStorage/FastNBT.h" #include "src/IniFile.h" +#include "src/Endianness.h" @@ -143,7 +144,7 @@ public: // Get the real data size: const char * chunkData = m_FileData.data() + chunkOffset * 4096; - UInt32 chunkSize = GetBEInt(chunkData); + UInt32 chunkSize = NetworkBufToHost(chunkData); if ((chunkSize < 2) || (chunkSize / 4096 > numChunkSectors)) { // Bad data, bail out @@ -181,7 +182,7 @@ protected: const char * hdr = m_FileData.data(); for (size_t i = 0; i < ARRAYCOUNT(m_Header); i++) { - m_Header[i] = GetBEInt(hdr + 4 * i); + m_Header[i] = NetworkBufToHost(hdr + 4 * i); } m_IsValid = true; } @@ -241,7 +242,7 @@ void AnvilSource::getChunkBiomes(int a_ChunkX, int a_ChunkZ, Chunk & a_DestChunk const char * beBiomes = nbt.GetData(mcsBiomes); for (size_t i = 0; i < ARRAYCOUNT(biomeMap); i++) { - biomeMap[i] = (EMCSBiome)GetBEInt(beBiomes + 4 * i); + biomeMap[i] = (EMCSBiome)NetworkBufToHost(beBiomes + 4 * i); } a_DestChunk.setBiomes(biomeMap); return; -- cgit v1.2.3