diff options
Diffstat (limited to '')
-rw-r--r-- | src/ByteBuffer.h | 5 | ||||
-rw-r--r-- | src/ClientHandle.h | 2 | ||||
-rw-r--r-- | src/Enchantments.h | 2 | ||||
-rw-r--r-- | src/Protocol/ChunkDataSerializer.cpp | 24 | ||||
-rw-r--r-- | src/Protocol/ChunkDataSerializer.h | 8 | ||||
-rw-r--r-- | src/Protocol/Packetizer.h | 2 | ||||
-rw-r--r-- | src/Protocol/Protocol.h | 4 | ||||
-rw-r--r-- | src/Protocol/Protocol17x.cpp | 58 | ||||
-rw-r--r-- | src/Protocol/Protocol17x.h | 6 | ||||
-rw-r--r-- | src/Protocol/Protocol18x.cpp | 60 | ||||
-rw-r--r-- | src/Protocol/Protocol18x.h | 10 | ||||
-rw-r--r-- | src/Protocol/ProtocolRecognizer.cpp | 14 | ||||
-rw-r--r-- | src/Protocol/ProtocolRecognizer.h | 4 | ||||
-rw-r--r-- | src/StringCompression.h | 2 | ||||
-rw-r--r-- | src/StringUtils.h | 5 | ||||
-rw-r--r-- | src/WorldStorage/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/WorldStorage/EnchantmentSerializer.cpp | 10 | ||||
-rw-r--r-- | src/WorldStorage/EnchantmentSerializer.h | 2 | ||||
-rw-r--r-- | src/WorldStorage/FastNBT.cpp | 115 | ||||
-rw-r--r-- | src/WorldStorage/FastNBT.h | 120 | ||||
-rw-r--r-- | src/WorldStorage/FireworksSerializer.cpp | 23 | ||||
-rw-r--r-- | src/WorldStorage/FireworksSerializer.h | 2 | ||||
-rw-r--r-- | src/WorldStorage/MapSerializer.cpp | 24 |
23 files changed, 256 insertions, 247 deletions
diff --git a/src/ByteBuffer.h b/src/ByteBuffer.h index cec85a404..e6cb777ab 100644 --- a/src/ByteBuffer.h +++ b/src/ByteBuffer.h @@ -108,11 +108,14 @@ public: /** Reads a_Count bytes into a_String; returns true if successful */ bool ReadString(AString & a_String, size_t a_Count); + /** Reads a_Count bytes into a_String; returns true if successful */ + bool ReadByteString(std::basic_string<Byte> & a_String, size_t a_Count); + /** Skips reading by a_Count bytes; returns false if not enough bytes in the ringbuffer */ bool SkipRead(size_t a_Count); /** Reads all available data into a_Data */ - void ReadAll(AString & a_Data); + void ReadAll(std::basic_string<Byte> & a_Data); /** Reads the specified number of bytes and writes it into the destinatio bytebuffer. Returns true on success. */ bool ReadToByteBuffer(cByteBuffer & a_Dst, size_t a_NumBytes); diff --git a/src/ClientHandle.h b/src/ClientHandle.h index 22d052f22..9a6276b09 100644 --- a/src/ClientHandle.h +++ b/src/ClientHandle.h @@ -351,7 +351,7 @@ public: // tolua_export */ bool HandleLogin(UInt32 a_ProtocolVersion, const AString & a_Username); - void SendData(const char * a_Data, size_t a_Size); + void SendData(const Byte * a_Data, size_t a_Size); /** Called when the player moves into a different world. Sends an UnloadChunk packet for each loaded chunk and resets the streamed chunks. */ diff --git a/src/Enchantments.h b/src/Enchantments.h index 8c08e7a93..7b39569e9 100644 --- a/src/Enchantments.h +++ b/src/Enchantments.h @@ -141,7 +141,7 @@ public: friend void EnchantmentSerializer::WriteToNBTCompound(const cEnchantments & a_Enchantments, cFastNBTWriter & a_Writer, const AString & a_ListTagName); /** Reads the enchantments from the specified NBT list tag (ench or StoredEnchantments) */ - friend void EnchantmentSerializer::ParseFromNBT(cEnchantments & a_Enchantments, const cParsedNBT & a_NBT, int a_EnchListTagIdx); + friend void EnchantmentSerializer::ParseFromNBT(cEnchantments & a_Enchantments, const cParsedNBT & a_NBT, size_t a_EnchListTagIdx); protected: /** Maps enchantment ID -> enchantment level */ diff --git a/src/Protocol/ChunkDataSerializer.cpp b/src/Protocol/ChunkDataSerializer.cpp index 8c5569cc7..757038f99 100644 --- a/src/Protocol/ChunkDataSerializer.cpp +++ b/src/Protocol/ChunkDataSerializer.cpp @@ -32,7 +32,7 @@ cChunkDataSerializer::cChunkDataSerializer( -const AString & cChunkDataSerializer::Serialize(int a_Version, int a_ChunkX, int a_ChunkZ) +const std::basic_string<Byte> & cChunkDataSerializer::Serialize(int a_Version, int a_ChunkX, int a_ChunkZ) { Serializations::const_iterator itr = m_Serializations.find(a_Version); if (itr != m_Serializations.end()) @@ -40,7 +40,7 @@ const AString & cChunkDataSerializer::Serialize(int a_Version, int a_ChunkX, int return itr->second; } - AString data; + std::basic_string<Byte> data; switch (a_Version) { case RELEASE_1_3_2: Serialize39(data); break; @@ -64,7 +64,7 @@ const AString & cChunkDataSerializer::Serialize(int a_Version, int a_ChunkX, int -void cChunkDataSerializer::Serialize39(AString & a_Data) +void cChunkDataSerializer::Serialize39(std::basic_string<Byte> & a_Data) { // TODO: Do not copy data and then compress it; rather, compress partial blocks of data (zlib can stream) @@ -88,7 +88,7 @@ void cChunkDataSerializer::Serialize39(AString & a_Data) // In order not to use allocation, use a fixed-size buffer, with the size // that uses the same calculation as compressBound(): const uLongf CompressedMaxSize = DataSize + (DataSize >> 12) + (DataSize >> 14) + (DataSize >> 25) + 16; - char CompressedBlockData[CompressedMaxSize]; + Byte CompressedBlockData[CompressedMaxSize]; uLongf CompressedSize = compressBound(DataSize); @@ -106,11 +106,11 @@ void cChunkDataSerializer::Serialize39(AString & a_Data) // Also, no endian flipping is needed because of the const values unsigned short BitMap1 = 0xffff; unsigned short BitMap2 = 0; - a_Data.append(reinterpret_cast<const char *>(&BitMap1), sizeof(short)); - a_Data.append(reinterpret_cast<const char *>(&BitMap2), sizeof(short)); + a_Data.append(reinterpret_cast<const Byte *>(&BitMap1), sizeof(short)); + a_Data.append(reinterpret_cast<const Byte *>(&BitMap2), sizeof(short)); UInt32 CompressedSizeBE = htonl(static_cast<UInt32>(CompressedSize)); - a_Data.append(reinterpret_cast<const char *>(&CompressedSizeBE), sizeof(CompressedSizeBE)); + a_Data.append(reinterpret_cast<const Byte *>(&CompressedSizeBE), sizeof(CompressedSizeBE)); // Unlike 29, 39 doesn't have the "unused" int @@ -121,7 +121,7 @@ void cChunkDataSerializer::Serialize39(AString & a_Data) -void cChunkDataSerializer::Serialize47(AString & a_Data, int a_ChunkX, int a_ChunkZ) +void cChunkDataSerializer::Serialize47(std::basic_string<Byte> & a_Data, int a_ChunkX, int a_ChunkZ) { // This function returns the fully compressed packet (including packet size), not the raw packet! @@ -157,7 +157,7 @@ void cChunkDataSerializer::Serialize47(AString & a_Data, int a_ChunkX, int a_Chu Packet.WriteBuf(m_BlockSkyLight, sizeof(m_BlockSkyLight)); Packet.WriteBuf(m_BiomeData, BiomeDataSize); - AString PacketData; + std::basic_string<Byte> PacketData; Packet.ReadAll(PacketData); Packet.CommitRead(); @@ -173,7 +173,7 @@ void cChunkDataSerializer::Serialize47(AString & a_Data, int a_ChunkX, int a_Chu } else { - AString PostData; + std::basic_string<Byte> PostData; Buffer.WriteVarInt32(static_cast<UInt32>(Packet.GetUsedSpace() + 1)); Buffer.WriteVarInt32(0); Buffer.ReadAll(PostData); @@ -181,8 +181,8 @@ void cChunkDataSerializer::Serialize47(AString & a_Data, int a_ChunkX, int a_Chu a_Data.clear(); a_Data.reserve(PostData.size() + PacketData.size()); - a_Data.append(PostData.data(), PostData.size()); - a_Data.append(PacketData.data(), PacketData.size()); + a_Data.append(PostData); + a_Data.append(PacketData); } } diff --git a/src/Protocol/ChunkDataSerializer.h b/src/Protocol/ChunkDataSerializer.h index 6acc1544b..a53347eb2 100644 --- a/src/Protocol/ChunkDataSerializer.h +++ b/src/Protocol/ChunkDataSerializer.h @@ -18,12 +18,12 @@ protected: const cChunkDef::BlockNibbles & m_BlockSkyLight; const unsigned char * m_BiomeData; - typedef std::map<int, AString> Serializations; + typedef std::map<int, std::basic_string<Byte>> Serializations; Serializations m_Serializations; - void Serialize39(AString & a_Data); // Release 1.3.1 to 1.7.10 - void Serialize47(AString & a_Data, int a_ChunkX, int a_ChunkZ); // Release 1.8 + void Serialize39(std::basic_string<Byte> & a_Data); // Release 1.3.1 to 1.7.10 + void Serialize47(std::basic_string<Byte> & a_Data, int a_ChunkX, int a_ChunkZ); // Release 1.8 public: enum @@ -40,7 +40,7 @@ public: const unsigned char * a_BiomeData ); - const AString & Serialize(int a_Version, int a_ChunkX, int a_ChunkZ); // Returns one of the internal m_Serializations[] + const std::basic_string<Byte> & Serialize(int a_Version, int a_ChunkX, int a_ChunkZ); // Returns one of the internal m_Serializations[] } ; diff --git a/src/Protocol/Packetizer.h b/src/Protocol/Packetizer.h index efed9c7a9..13a79bd7d 100644 --- a/src/Protocol/Packetizer.h +++ b/src/Protocol/Packetizer.h @@ -112,7 +112,7 @@ public: } - inline void WriteBuf(const char * a_Data, size_t a_Size) + inline void WriteBuf(const Byte * a_Data, size_t a_Size) { VERIFY(m_Out.Write(a_Data, a_Size)); } diff --git a/src/Protocol/Protocol.h b/src/Protocol/Protocol.h index af0485a78..0f9136d2b 100644 --- a/src/Protocol/Protocol.h +++ b/src/Protocol/Protocol.h @@ -61,7 +61,7 @@ public: virtual ~cProtocol() {} /** Called when client sends some data */ - virtual void DataReceived(const char * a_Data, size_t a_Size) = 0; + virtual void DataReceived(const Byte * a_Data, size_t a_Size) = 0; // Sending stuff to clients (alphabetically sorted): virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity * a_Vehicle) = 0; @@ -165,7 +165,7 @@ protected: cByteBuffer m_OutPacketLenBuffer; /** A generic data-sending routine, all outgoing packet data needs to be routed through this so that descendants may override it. */ - virtual void SendData(const char * a_Data, size_t a_Size) = 0; + virtual void SendData(const Byte * a_Data, size_t a_Size) = 0; /** Sends a single packet contained within the cPacketizer class. The cPacketizer's destructor calls this to send the contained packet; protocol may transform the data (compression in 1.8 etc). */ diff --git a/src/Protocol/Protocol17x.cpp b/src/Protocol/Protocol17x.cpp index 1c8c81bbd..108aa243c 100644 --- a/src/Protocol/Protocol17x.cpp +++ b/src/Protocol/Protocol17x.cpp @@ -136,7 +136,7 @@ cProtocol172::cProtocol172(cClientHandle * a_Client, const AString & a_ServerAdd -void cProtocol172::DataReceived(const char * a_Data, size_t a_Size) +void cProtocol172::DataReceived(const Byte * a_Data, size_t a_Size) { if (m_IsEncrypted) { @@ -144,8 +144,8 @@ void cProtocol172::DataReceived(const char * a_Data, size_t a_Size) while (a_Size > 0) { size_t NumBytes = (a_Size > sizeof(Decrypted)) ? sizeof(Decrypted) : a_Size; - m_Decryptor.ProcessData(Decrypted, reinterpret_cast<const Byte *>(a_Data), NumBytes); - AddReceivedData(reinterpret_cast<const char *>(Decrypted), NumBytes); + m_Decryptor.ProcessData(Decrypted, a_Data, NumBytes); + AddReceivedData(Decrypted, NumBytes); a_Size -= NumBytes; a_Data += NumBytes; } @@ -286,7 +286,7 @@ void cProtocol172::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerialize // Serialize first, before creating the Packetizer (the packetizer locks a CS) // This contains the flags and bitmasks, too - const AString & ChunkData = a_Serializer.Serialize(cChunkDataSerializer::RELEASE_1_3_2, a_ChunkX, a_ChunkZ); + const auto & ChunkData = a_Serializer.Serialize(cChunkDataSerializer::RELEASE_1_3_2, a_ChunkX, a_ChunkZ); cPacketizer Pkt(*this, 0x21); // Chunk Data packet Pkt.WriteBEInt32(a_ChunkX); @@ -694,7 +694,7 @@ void cProtocol172::SendMapData(const cMap & a_Map, int a_DataStartX, int a_DataS Pkt.WriteBEUInt8(static_cast<Byte>(a_DataStartX)); Pkt.WriteBEUInt8(static_cast<Byte>(a_DataStartX)); - Pkt.WriteBuf(reinterpret_cast<const char *>(a_Map.GetData().data()), a_Map.GetData().size()); + Pkt.WriteBuf(reinterpret_cast<const Byte *>(a_Map.GetData().data()), a_Map.GetData().size()); } { @@ -981,7 +981,7 @@ void cProtocol172::SendPluginMessage(const AString & a_Channel, const AString & cPacketizer Pkt(*this, 0x3f); Pkt.WriteString(a_Channel); Pkt.WriteBEUInt16(static_cast<UInt16>(a_Message.size())); - Pkt.WriteBuf(a_Message.data(), a_Message.size()); + Pkt.WriteBuf(reinterpret_cast<const Byte *>(a_Message.data()), a_Message.size()); } @@ -1544,14 +1544,14 @@ void cProtocol172::SendWindowProperty(const cWindow & a_Window, short a_Property -void cProtocol172::AddReceivedData(const char * a_Data, size_t a_Size) +void cProtocol172::AddReceivedData(const Byte * a_Data, size_t a_Size) { // Write the incoming data into the comm log file: if (g_ShouldLogCommIn) { if (m_ReceivedData.GetReadableSpace() > 0) { - AString AllData; + std::basic_string<Byte> AllData; size_t OldReadableSpace = m_ReceivedData.GetReadableSpace(); m_ReceivedData.ReadAll(AllData); m_ReceivedData.ResetRead(); @@ -1611,7 +1611,7 @@ void cProtocol172::AddReceivedData(const char * a_Data, size_t a_Size) // Log the packet info into the comm log file: if (g_ShouldLogCommIn) { - AString PacketData; + std::basic_string<Byte> PacketData; bb.ReadAll(PacketData); bb.ResetRead(); bb.ReadVarInt(PacketType); // We have already read the packet type once, it will be there again. @@ -1632,7 +1632,7 @@ void cProtocol172::AddReceivedData(const char * a_Data, size_t a_Size) #ifdef _DEBUG // Dump the packet contents into the log: bb.ResetRead(); - AString Packet; + std::basic_string<Byte> Packet; bb.ReadAll(Packet); Packet.resize(Packet.size() - 1); // Drop the final NUL pushed there for over-read detection AString Out; @@ -1673,7 +1673,7 @@ void cProtocol172::AddReceivedData(const char * a_Data, size_t a_Size) // Log any leftover bytes into the logfile: if (g_ShouldLogCommIn && (m_ReceivedData.GetReadableSpace() > 0)) { - AString AllData; + std::basic_string<Byte> AllData; size_t OldReadableSpace = m_ReceivedData.GetReadableSpace(); m_ReceivedData.ReadAll(AllData); m_ReceivedData.ResetRead(); @@ -1932,7 +1932,7 @@ void cProtocol172::HandlePacketLoginStart(cByteBuffer & a_ByteBuffer) Pkt.WriteString(Server->GetServerID()); const AString & PubKeyDer = Server->GetPublicKeyDER(); Pkt.WriteBEUInt16(static_cast<UInt16>(PubKeyDer.size())); - Pkt.WriteBuf(PubKeyDer.data(), PubKeyDer.size()); + Pkt.WriteBuf(reinterpret_cast<const Byte *>(PubKeyDer.data()), PubKeyDer.size()); Pkt.WriteBEInt16(4); Pkt.WriteBEUInt32(static_cast<UInt32>(reinterpret_cast<uintptr_t>(this))); // Using 'this' as the cryptographic nonce, so that we don't have to generate one each time :) m_Client->SetUsername(Username); @@ -2431,7 +2431,7 @@ void cProtocol172::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const -void cProtocol172::SendData(const char * a_Data, size_t a_Size) +void cProtocol172::SendData(const Byte * a_Data, size_t a_Size) { if (m_IsEncrypted) { @@ -2440,7 +2440,7 @@ void cProtocol172::SendData(const char * a_Data, size_t a_Size) { size_t NumBytes = (a_Size > sizeof(Encrypted)) ? sizeof(Encrypted) : a_Size; m_Encryptor.ProcessData(Encrypted, reinterpret_cast<const Byte *>(a_Data), NumBytes); - m_Client->SendData(reinterpret_cast<const char *>(Encrypted), NumBytes); + m_Client->SendData(reinterpret_cast<const Byte *>(Encrypted), NumBytes); a_Size -= NumBytes; a_Data += NumBytes; } @@ -2457,7 +2457,7 @@ void cProtocol172::SendData(const char * a_Data, size_t a_Size) void cProtocol172::SendPacket(cPacketizer & a_Packet) { - AString DataToSend; + std::basic_string<Byte> DataToSend; // Send the packet length UInt32 PacketLen = static_cast<UInt32>(m_OutPacketBuffer.GetUsedSpace()); @@ -2527,7 +2527,7 @@ bool cProtocol172::ReadItem(cByteBuffer & a_ByteBuffer, cItem & a_Item) void cProtocol172::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata) { // Uncompress the GZIPped data: - AString Uncompressed; + std::basic_string<Byte> Uncompressed; if (UncompressStringGZIP(a_Metadata.data(), a_Metadata.size(), Uncompressed) != Z_OK) { AString HexDump; @@ -2537,7 +2537,7 @@ void cProtocol172::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata) } // Parse into NBT: - cParsedNBT NBT(Uncompressed.data(), Uncompressed.size()); + cParsedNBT NBT(Uncompressed); if (!NBT.IsValid()) { AString HexDump; @@ -2547,16 +2547,17 @@ void cProtocol172::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata) } // Load enchantments and custom display names from the NBT data: - for (int tag = NBT.GetFirstChild(NBT.GetRoot()); tag >= 0; tag = NBT.GetNextSibling(tag)) + for (auto tag = NBT.GetFirstChild(NBT.GetRoot()); tag.HasValue(); tag = NBT.GetNextSibling(tag.GetValue())) { - AString TagName = NBT.GetName(tag); - switch (NBT.GetType(tag)) + auto tagnum = tag.GetValue(); + AString TagName = NBT.GetName(tagnum); + switch (NBT.GetType(tagnum)) { case TAG_List: { if ((TagName == "ench") || (TagName == "StoredEnchantments")) // Enchantments tags { - EnchantmentSerializer::ParseFromNBT(a_Item.m_Enchantments, NBT, tag); + EnchantmentSerializer::ParseFromNBT(a_Item.m_Enchantments, NBT, tagnum); } break; } @@ -2564,8 +2565,9 @@ void cProtocol172::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata) { if (TagName == "display") // Custom name and lore tag { - for (int displaytag = NBT.GetFirstChild(tag); displaytag >= 0; displaytag = NBT.GetNextSibling(displaytag)) + for (auto maybedisplaytag = NBT.GetFirstChild(tagnum); maybedisplaytag.HasValue(); maybedisplaytag = NBT.GetNextSibling(maybedisplaytag.GetValue())) { + auto displaytag = maybedisplaytag.GetValue(); if ((NBT.GetType(displaytag) == TAG_String) && (NBT.GetName(displaytag) == "Name")) // Custon name tag { a_Item.m_CustomName = NBT.GetString(displaytag); @@ -2574,9 +2576,9 @@ void cProtocol172::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata) { AString Lore; - for (int loretag = NBT.GetFirstChild(displaytag); loretag >= 0; loretag = NBT.GetNextSibling(loretag)) // Loop through array of strings + for (auto loretag = NBT.GetFirstChild(displaytag); loretag.HasValue(); loretag = NBT.GetNextSibling(loretag.GetValue())) // Loop through array of strings { - AppendPrintf(Lore, "%s`", NBT.GetString(loretag).c_str()); // Append the lore with a grave accent / backtick, used internally by MCS to display a new line in the client; don't forget to c_str ;) + AppendPrintf(Lore, "%s`", NBT.GetString(loretag.GetValue()).c_str()); // Append the lore with a grave accent / backtick, used internally by MCS to display a new line in the client; don't forget to c_str ;) } a_Item.m_Lore = Lore; @@ -2589,7 +2591,7 @@ void cProtocol172::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata) } else if ((TagName == "Fireworks") || (TagName == "Explosion")) { - cFireworkItem::ParseFromNBT(a_Item.m_FireworkItem, NBT, tag, static_cast<ENUM_ITEM_ID>(a_Item.m_ItemType)); + cFireworkItem::ParseFromNBT(a_Item.m_FireworkItem, NBT, tagnum, static_cast<ENUM_ITEM_ID>(a_Item.m_ItemType)); } break; } @@ -2597,7 +2599,7 @@ void cProtocol172::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata) { if (TagName == "RepairCost") { - a_Item.m_RepairCost = NBT.GetInt(tag); + a_Item.m_RepairCost = NBT.GetInt(tagnum); } } default: LOGD("Unimplemented NBT data when parsing!"); break; @@ -2726,7 +2728,7 @@ void cProtocol172::WriteItem(cPacketizer & a_Pkt, const cItem & a_Item) } Writer.Finish(); - AString Compressed; + std::basic_string<Byte> Compressed; CompressStringGZIP(Writer.GetResult().data(), Writer.GetResult().size(), Compressed); a_Pkt.WriteBEUInt16(static_cast<UInt16>(Compressed.size())); a_Pkt.WriteBuf(Compressed.data(), Compressed.size()); @@ -2821,7 +2823,7 @@ void cProtocol172::WriteBlockEntity(cPacketizer & a_Pkt, const cBlockEntity & a_ Writer.Finish(); - AString Compressed; + std::basic_string<Byte> Compressed; CompressStringGZIP(Writer.GetResult().data(), Writer.GetResult().size(), Compressed); a_Pkt.WriteBEUInt16(static_cast<UInt16>(Compressed.size())); a_Pkt.WriteBuf(Compressed.data(), Compressed.size()); diff --git a/src/Protocol/Protocol17x.h b/src/Protocol/Protocol17x.h index 747ffe186..f26c6d59d 100644 --- a/src/Protocol/Protocol17x.h +++ b/src/Protocol/Protocol17x.h @@ -58,7 +58,7 @@ public: cProtocol172(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); /** Called when client sends some data: */ - virtual void DataReceived(const char * a_Data, size_t a_Size) override; + virtual void DataReceived(const Byte * a_Data, size_t a_Size) override; /** Sending stuff to clients (alphabetically sorted): */ virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity * a_Vehicle) override; @@ -172,7 +172,7 @@ protected: /** Adds the received (unencrypted) data to m_ReceivedData, parses complete packets */ - void AddReceivedData(const char * a_Data, size_t a_Size); + void AddReceivedData(const Byte * a_Data, size_t a_Size); /** Reads and handles the packet. The packet length and type have already been read. Returns true if the packet was understood, false if it was an unknown packet @@ -217,7 +217,7 @@ protected: void HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const AString & a_Channel, UInt16 a_PayloadLength); /** Sends the data to the client, encrypting them if needed. */ - virtual void SendData(const char * a_Data, size_t a_Size) override; + virtual void SendData(const Byte * a_Data, size_t a_Size) override; /** Sends the packet to the client. Called by the cPacketizer's destructor. */ virtual void SendPacket(cPacketizer & a_Packet) override; diff --git a/src/Protocol/Protocol18x.cpp b/src/Protocol/Protocol18x.cpp index c80907ed8..59ce21f50 100644 --- a/src/Protocol/Protocol18x.cpp +++ b/src/Protocol/Protocol18x.cpp @@ -147,7 +147,7 @@ cProtocol180::cProtocol180(cClientHandle * a_Client, const AString & a_ServerAdd -void cProtocol180::DataReceived(const char * a_Data, size_t a_Size) +void cProtocol180::DataReceived(const Byte * a_Data, size_t a_Size) { if (m_IsEncrypted) { @@ -155,8 +155,8 @@ void cProtocol180::DataReceived(const char * a_Data, size_t a_Size) while (a_Size > 0) { size_t NumBytes = (a_Size > sizeof(Decrypted)) ? sizeof(Decrypted) : a_Size; - m_Decryptor.ProcessData(Decrypted, reinterpret_cast<const Byte *>(a_Data), NumBytes); - AddReceivedData(reinterpret_cast<const char *>(Decrypted), NumBytes); + m_Decryptor.ProcessData(Decrypted, a_Data, NumBytes); + AddReceivedData(Decrypted, NumBytes); a_Size -= NumBytes; a_Data += NumBytes; } @@ -281,7 +281,7 @@ void cProtocol180::SendChunkData(int a_ChunkX, int a_ChunkZ, cChunkDataSerialize // Serialize first, before creating the Packetizer (the packetizer locks a CS) // This contains the flags and bitmasks, too - const AString & ChunkData = a_Serializer.Serialize(cChunkDataSerializer::RELEASE_1_8_0, a_ChunkX, a_ChunkZ); + const auto & ChunkData = a_Serializer.Serialize(cChunkDataSerializer::RELEASE_1_8_0, a_ChunkX, a_ChunkZ); cCSLock Lock(m_CSPacket); SendData(ChunkData.data(), ChunkData.size()); @@ -1038,7 +1038,7 @@ void cProtocol180::SendPluginMessage(const AString & a_Channel, const AString & cPacketizer Pkt(*this, 0x3f); Pkt.WriteString(a_Channel); - Pkt.WriteBuf(a_Message.data(), a_Message.size()); + Pkt.WriteBuf(reinterpret_cast<const Byte *>(a_Message.data()), a_Message.size()); } @@ -1638,10 +1638,10 @@ void cProtocol180::SendWindowProperty(const cWindow & a_Window, short a_Property -bool cProtocol180::CompressPacket(const AString & a_Packet, AString & a_CompressedData) +bool cProtocol180::CompressPacket(const std::basic_string<Byte> & a_Packet, std::basic_string<Byte> & a_CompressedData) { // Compress the data: - char CompressedData[MAX_COMPRESSED_PACKET_LEN]; + Byte CompressedData[MAX_COMPRESSED_PACKET_LEN]; uLongf CompressedSize = compressBound(static_cast<uLongf>(a_Packet.size())); if (CompressedSize >= MAX_COMPRESSED_PACKET_LEN) @@ -1659,7 +1659,7 @@ bool cProtocol180::CompressPacket(const AString & a_Packet, AString & a_Compress return false; } - AString LengthData; + std::basic_string<Byte> LengthData; cByteBuffer Buffer(20); Buffer.WriteVarInt32(static_cast<UInt32>(a_Packet.size())); Buffer.ReadAll(LengthData); @@ -1782,14 +1782,14 @@ void cProtocol180::FixItemFramePositions(int a_ObjectData, double & a_PosX, doub -void cProtocol180::AddReceivedData(const char * a_Data, size_t a_Size) +void cProtocol180::AddReceivedData(const Byte * a_Data, size_t a_Size) { // Write the incoming data into the comm log file: if (g_ShouldLogCommIn && m_CommLogFile.IsOpen()) { if (m_ReceivedData.GetReadableSpace() > 0) { - AString AllData; + std::basic_string<Byte> AllData; size_t OldReadableSpace = m_ReceivedData.GetReadableSpace(); m_ReceivedData.ReadAll(AllData); m_ReceivedData.ResetRead(); @@ -1896,7 +1896,7 @@ void cProtocol180::AddReceivedData(const char * a_Data, size_t a_Size) // Log the packet info into the comm log file: if (g_ShouldLogCommIn && m_CommLogFile.IsOpen()) { - AString PacketData; + std::basic_string<Byte> PacketData; bb.ReadAll(PacketData); bb.ResetRead(); bb.ReadVarInt(PacketType); // We have already read the packet type once, it will be there again @@ -1917,7 +1917,7 @@ void cProtocol180::AddReceivedData(const char * a_Data, size_t a_Size) #ifdef _DEBUG // Dump the packet contents into the log: bb.ResetRead(); - AString Packet; + std::basic_string<Byte> Packet; bb.ReadAll(Packet); Packet.resize(Packet.size() - 1); // Drop the final NUL pushed there for over-read detection AString Out; @@ -1959,7 +1959,7 @@ void cProtocol180::AddReceivedData(const char * a_Data, size_t a_Size) // Log any leftover bytes into the logfile: if (g_ShouldLogCommIn && (m_ReceivedData.GetReadableSpace() > 0) && m_CommLogFile.IsOpen()) { - AString AllData; + std::basic_string<Byte> AllData; size_t OldReadableSpace = m_ReceivedData.GetReadableSpace(); m_ReceivedData.ReadAll(AllData); m_ReceivedData.ResetRead(); @@ -2206,7 +2206,7 @@ void cProtocol180::HandlePacketLoginStart(cByteBuffer & a_ByteBuffer) Pkt.WriteString(Server->GetServerID()); const AString & PubKeyDer = Server->GetPublicKeyDER(); Pkt.WriteVarInt32(static_cast<UInt32>(PubKeyDer.size())); - Pkt.WriteBuf(PubKeyDer.data(), PubKeyDer.size()); + Pkt.WriteBuf(reinterpret_cast<const Byte *>(PubKeyDer.data()), PubKeyDer.size()); Pkt.WriteVarInt32(4); Pkt.WriteBEInt32(static_cast<int>(reinterpret_cast<intptr_t>(this))); // Using 'this' as the cryptographic nonce, so that we don't have to generate one each time :) m_Client->SetUsername(Username); @@ -2734,7 +2734,7 @@ void cProtocol180::HandleVanillaPluginMessage(cByteBuffer & a_ByteBuffer, const -void cProtocol180::SendData(const char * a_Data, size_t a_Size) +void cProtocol180::SendData(const Byte * a_Data, size_t a_Size) { if (m_IsEncrypted) { @@ -2742,8 +2742,8 @@ void cProtocol180::SendData(const char * a_Data, size_t a_Size) while (a_Size > 0) { size_t NumBytes = (a_Size > sizeof(Encrypted)) ? sizeof(Encrypted) : a_Size; - m_Encryptor.ProcessData(Encrypted, reinterpret_cast<Byte *>(const_cast<char*>(a_Data)), NumBytes); - m_Client->SendData(reinterpret_cast<const char *>(Encrypted), NumBytes); + m_Encryptor.ProcessData(Encrypted, a_Data, NumBytes); + m_Client->SendData(Encrypted, NumBytes); a_Size -= NumBytes; a_Data += NumBytes; } @@ -2778,8 +2778,8 @@ bool cProtocol180::ReadItem(cByteBuffer & a_ByteBuffer, cItem & a_Item, size_t a a_Item.Empty(); } - AString Metadata; - if (!a_ByteBuffer.ReadString(Metadata, a_ByteBuffer.GetReadableSpace() - a_KeepRemainingBytes - 1) || (Metadata.size() == 0) || (Metadata[0] == 0)) + std::basic_string<Byte> Metadata; + if (!a_ByteBuffer.ReadByteString(Metadata, a_ByteBuffer.GetReadableSpace() - a_KeepRemainingBytes - 1) || (Metadata.size() == 0) || (Metadata[0] == 0)) { // No metadata return true; @@ -2793,10 +2793,10 @@ bool cProtocol180::ReadItem(cByteBuffer & a_ByteBuffer, cItem & a_Item, size_t a -void cProtocol180::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata) +void cProtocol180::ParseItemMetadata(cItem & a_Item, const std::basic_string<Byte> & a_Metadata) { // Parse into NBT: - cParsedNBT NBT(a_Metadata.data(), a_Metadata.size()); + cParsedNBT NBT(a_Metadata); if (!NBT.IsValid()) { AString HexDump; @@ -2806,8 +2806,9 @@ void cProtocol180::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata) } // Load enchantments and custom display names from the NBT data: - for (int tag = NBT.GetFirstChild(NBT.GetRoot()); tag >= 0; tag = NBT.GetNextSibling(tag)) + for (auto maybetag = NBT.GetFirstChild(NBT.GetRoot()); maybetag.HasValue(); maybetag = NBT.GetNextSibling(maybetag.GetValue())) { + auto tag = maybetag.GetValue(); AString TagName = NBT.GetName(tag); switch (NBT.GetType(tag)) { @@ -2823,8 +2824,9 @@ void cProtocol180::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata) { if (TagName == "display") // Custom name and lore tag { - for (int displaytag = NBT.GetFirstChild(tag); displaytag >= 0; displaytag = NBT.GetNextSibling(displaytag)) + for (auto maybedisplaytag = NBT.GetFirstChild(tag); maybedisplaytag.HasValue(); maybedisplaytag = NBT.GetNextSibling(maybedisplaytag.GetValue())) { + auto displaytag = maybedisplaytag.GetValue(); if ((NBT.GetType(displaytag) == TAG_String) && (NBT.GetName(displaytag) == "Name")) // Custon name tag { a_Item.m_CustomName = NBT.GetString(displaytag); @@ -2833,9 +2835,9 @@ void cProtocol180::ParseItemMetadata(cItem & a_Item, const AString & a_Metadata) { AString Lore; - for (int loretag = NBT.GetFirstChild(displaytag); loretag >= 0; loretag = NBT.GetNextSibling(loretag)) // Loop through array of strings + for (auto loretag = NBT.GetFirstChild(displaytag); loretag.HasValue(); loretag = NBT.GetNextSibling(loretag.GetValue())) // Loop through array of strings { - AppendPrintf(Lore, "%s`", NBT.GetString(loretag).c_str()); // Append the lore with a grave accent / backtick, used internally by MCS to display a new line in the client; don't forget to c_str ;) + AppendPrintf(Lore, "%s`", NBT.GetString(loretag.GetValue()).c_str()); // Append the lore with a grave accent / backtick, used internally by MCS to display a new line in the client; don't forget to c_str ;) } a_Item.m_Lore = Lore; @@ -2916,7 +2918,7 @@ eBlockFace cProtocol180::FaceIntToBlockFace(Int8 a_BlockFace) void cProtocol180::SendPacket(cPacketizer & a_Pkt) { UInt32 PacketLen = static_cast<UInt32>(m_OutPacketBuffer.GetUsedSpace()); - AString PacketData, CompressedPacket; + std::basic_string<Byte> PacketData, CompressedPacket; m_OutPacketBuffer.ReadAll(PacketData); m_OutPacketBuffer.CommitRead(); @@ -2933,7 +2935,7 @@ void cProtocol180::SendPacket(cPacketizer & a_Pkt) // The packet is not compressed, indicate this in the packet header: m_OutPacketLenBuffer.WriteVarInt32(PacketLen + 1); m_OutPacketLenBuffer.WriteVarInt32(0); - AString LengthData; + std::basic_string<Byte> LengthData; m_OutPacketLenBuffer.ReadAll(LengthData); SendData(LengthData.data(), LengthData.size()); } @@ -2941,7 +2943,7 @@ void cProtocol180::SendPacket(cPacketizer & a_Pkt) { // Compression doesn't apply to this state, send raw data: m_OutPacketLenBuffer.WriteVarInt32(PacketLen); - AString LengthData; + std::basic_string<Byte> LengthData; m_OutPacketLenBuffer.ReadAll(LengthData); SendData(LengthData.data(), LengthData.size()); } @@ -3048,7 +3050,7 @@ void cProtocol180::WriteItem(cPacketizer & a_Pkt, const cItem & a_Item) } Writer.Finish(); - AString Result = Writer.GetResult(); + auto Result = Writer.GetResult(); if (Result.size() == 0) { a_Pkt.WriteBEInt8(0); diff --git a/src/Protocol/Protocol18x.h b/src/Protocol/Protocol18x.h index 8b5b7ffa2..76bf6e7e7 100644 --- a/src/Protocol/Protocol18x.h +++ b/src/Protocol/Protocol18x.h @@ -57,7 +57,7 @@ public: cProtocol180(cClientHandle * a_Client, const AString & a_ServerAddress, UInt16 a_ServerPort, UInt32 a_State); /** Called when client sends some data: */ - virtual void DataReceived(const char * a_Data, size_t a_Size) override; + virtual void DataReceived(const Byte * a_Data, size_t a_Size) override; /** Sending stuff to clients (alphabetically sorted): */ virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity * a_Vehicle) override; @@ -146,7 +146,7 @@ public: /** Compress the packet. a_Packet must be without packet length. a_Compressed will be set to the compressed packet includes packet length and data length. If compression fails, the function returns false. */ - static bool CompressPacket(const AString & a_Packet, AString & a_Compressed); + static bool CompressPacket(const std::basic_string<Byte> & a_Packet, std::basic_string<Byte> & a_Compressed); /** The 1.8 protocol use a particle id instead of a string. This function converts the name to the id. If the name is incorrect, it returns 0. */ static int GetParticleID(const AString & a_ParticleName); @@ -182,7 +182,7 @@ protected: /** Adds the received (unencrypted) data to m_ReceivedData, parses complete packets */ - void AddReceivedData(const char * a_Data, size_t a_Size); + void AddReceivedData(const Byte * a_Data, size_t a_Size); /** Reads and handles the packet. The packet length and type have already been read. Returns true if the packet was understood, false if it was an unknown packet @@ -228,7 +228,7 @@ protected: /** Sends the data to the client, encrypting them if needed. */ - virtual void SendData(const char * a_Data, size_t a_Size) override; + virtual void SendData(const Byte * a_Data, size_t a_Size) override; /** Sends the packet to the client. Called by the cPacketizer's destructor. */ virtual void SendPacket(cPacketizer & a_Packet) override; @@ -241,7 +241,7 @@ protected: virtual bool ReadItem(cByteBuffer & a_ByteBuffer, cItem & a_Item, size_t a_KeepRemainingBytes = 0); /** Parses item metadata as read by ReadItem(), into the item enchantments. */ - void ParseItemMetadata(cItem & a_Item, const AString & a_Metadata); + void ParseItemMetadata(cItem & a_Item, const std::basic_string<Byte> & a_Metadata); void StartEncryption(const Byte * a_Key); diff --git a/src/Protocol/ProtocolRecognizer.cpp b/src/Protocol/ProtocolRecognizer.cpp index c88bd8639..4754b335e 100644 --- a/src/Protocol/ProtocolRecognizer.cpp +++ b/src/Protocol/ProtocolRecognizer.cpp @@ -57,7 +57,7 @@ AString cProtocolRecognizer::GetVersionTextFromInt(int a_ProtocolVersion) -void cProtocolRecognizer::DataReceived(const char * a_Data, size_t a_Size) +void cProtocolRecognizer::DataReceived(const Byte * a_Data, size_t a_Size) { if (m_Protocol == nullptr) { @@ -73,7 +73,7 @@ void cProtocolRecognizer::DataReceived(const char * a_Data, size_t a_Size) } // The protocol has just been recognized, dump the whole m_Buffer contents into it for parsing: - AString Dump; + std::basic_string<Byte> Dump; m_Buffer.ResetRead(); m_Buffer.ReadAll(Dump); m_Protocol->DataReceived(Dump.data(), Dump.size()); @@ -197,13 +197,13 @@ void cProtocolRecognizer::SendDisconnect(const AString & a_Reason) else { // This is used when the client sends a server-ping, respond with the default packet: - static const int Packet = 0xff; // PACKET_DISCONNECT - SendData(reinterpret_cast<const char *>(&Packet), 1); // WriteByte() + static const Byte Packet = 0xff; // PACKET_DISCONNECT + SendData(&Packet, 1); // WriteByte() auto UTF16 = UTF8ToRawBEUTF16(a_Reason); static const u_short Size = htons(static_cast<u_short>(UTF16.size())); - SendData(reinterpret_cast<const char *>(&Size), 2); // WriteShort() - SendData(reinterpret_cast<const char *>(UTF16.data()), UTF16.size() * sizeof(char16_t)); // WriteString() + SendData(reinterpret_cast<const Byte *>(&Size), 2); // WriteShort() + SendData(reinterpret_cast<const Byte *>(UTF16.data()), UTF16.size() * sizeof(char16_t)); // WriteString() } } @@ -909,7 +909,7 @@ AString cProtocolRecognizer::GetAuthServerID(void) -void cProtocolRecognizer::SendData(const char * a_Data, size_t a_Size) +void cProtocolRecognizer::SendData(const Byte * a_Data, size_t a_Size) { // This is used only when handling the server ping m_Client->SendData(a_Data, a_Size); diff --git a/src/Protocol/ProtocolRecognizer.h b/src/Protocol/ProtocolRecognizer.h index c548ad5ba..4533a2a89 100644 --- a/src/Protocol/ProtocolRecognizer.h +++ b/src/Protocol/ProtocolRecognizer.h @@ -45,7 +45,7 @@ public: static AString GetVersionTextFromInt(int a_ProtocolVersion); /** Called when client sends some data: */ - virtual void DataReceived(const char * a_Data, size_t a_Size) override; + virtual void DataReceived(const Byte * a_Data, size_t a_Size) override; /** Sending stuff to clients (alphabetically sorted): */ virtual void SendAttachEntity (const cEntity & a_Entity, const cEntity * a_Vehicle) override; @@ -131,7 +131,7 @@ public: virtual AString GetAuthServerID(void) override; - virtual void SendData(const char * a_Data, size_t a_Size) override; + virtual void SendData(const Byte * a_Data, size_t a_Size) override; protected: /** The recognized protocol */ diff --git a/src/StringCompression.h b/src/StringCompression.h index 10c00a804..d9ff6da32 100644 --- a/src/StringCompression.h +++ b/src/StringCompression.h @@ -17,9 +17,11 @@ extern int UncompressString(const char * a_Data, size_t a_Length, AString & a_Un /** Compresses a_Data into a_Compressed using GZIP; returns Z_OK for success or Z_XXX error constants same as zlib */ extern int CompressStringGZIP(const char * a_Data, size_t a_Length, AString & a_Compressed); +extern int CompressStringGZIP(const Byte * a_Data, size_t a_Length, std::basic_string<Byte> & a_Compressed); /** Uncompresses a_Data into a_Uncompressed using GZIP; returns Z_OK for success or Z_XXX error constants same as zlib */ extern int UncompressStringGZIP(const char * a_Data, size_t a_Length, AString & a_Uncompressed); +extern int UncompressStringGZIP(const char * a_Data, size_t a_Length, std::basic_string<Byte> & a_Uncompressed); /** Uncompresses a_Data into a_Uncompressed using Inflate; returns Z_OK for success or Z_XXX error constants same as zlib */ extern int InflateString(const char * a_Data, size_t a_Length, AString & a_Uncompressed); diff --git a/src/StringUtils.h b/src/StringUtils.h index 8c1925115..bd41c86af 100644 --- a/src/StringUtils.h +++ b/src/StringUtils.h @@ -111,13 +111,14 @@ extern AString Base64Decode(const AString & a_Base64String); // Exported manual extern AString Base64Encode(const AString & a_Input); // Exported manually due to embedded NULs and extra parameter /** Reads two bytes from the specified memory location and interprets them as BigEndian short */ -extern short GetBEShort(const char * a_Mem); +extern short GetBEShort(const Byte * a_Mem); /** Reads four bytes from the specified memory location and interprets them as BigEndian int */ -extern int GetBEInt(const char * a_Mem); +extern int GetBEInt(const Byte * a_Mem); /** Writes four bytes to the specified memory location so that they interpret as BigEndian int */ extern void SetBEInt(char * a_Mem, Int32 a_Value); +extern void SetBEInt(std::basic_string<Byte> & a_Mem, size_t a_Offset, Int32 a_Value); /** Splits a string that has embedded \0 characters, on those characters. a_Output is first cleared and then each separate string is pushed back into a_Output. diff --git a/src/WorldStorage/CMakeLists.txt b/src/WorldStorage/CMakeLists.txt index 1d8b60140..2b268af8a 100644 --- a/src/WorldStorage/CMakeLists.txt +++ b/src/WorldStorage/CMakeLists.txt @@ -31,7 +31,6 @@ SET (HDRS ) if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") - set_source_files_properties(FastNBT.cpp PROPERTIES COMPILE_FLAGS "-Wno-error=sign-conversion ") set_source_files_properties(FireworksSerializer.cpp PROPERTIES COMPILE_FLAGS "-Wno-error=switch-enum ") set_source_files_properties(NBTChunkSerializer.cpp PROPERTIES COMPILE_FLAGS "-Wno-error=switch-enum -Wno-error=sign-conversion -Wno-error=conversion ") set_source_files_properties(SchematicFileSerializer.cpp PROPERTIES COMPILE_FLAGS "-Wno-error=shadow -Wno-error=conversion -Wno-error=global-constructors") diff --git a/src/WorldStorage/EnchantmentSerializer.cpp b/src/WorldStorage/EnchantmentSerializer.cpp index b28e16b1d..c88d2749f 100644 --- a/src/WorldStorage/EnchantmentSerializer.cpp +++ b/src/WorldStorage/EnchantmentSerializer.cpp @@ -25,7 +25,7 @@ void EnchantmentSerializer::WriteToNBTCompound(const cEnchantments & a_Enchantme -void EnchantmentSerializer::ParseFromNBT(cEnchantments & a_Enchantments, const cParsedNBT & a_NBT, int a_EnchListTagIdx) +void EnchantmentSerializer::ParseFromNBT(cEnchantments & a_Enchantments, const cParsedNBT & a_NBT, size_t a_EnchListTagIdx) { // Read the enchantments from the specified NBT list tag (ench or StoredEnchantments) @@ -52,15 +52,19 @@ void EnchantmentSerializer::ParseFromNBT(cEnchantments & a_Enchantments, const c a_Enchantments.Clear(); // Iterate over all the compound children, parse an enchantment from each: - for (int tag = a_NBT.GetFirstChild(a_EnchListTagIdx); tag >= 0; tag = a_NBT.GetNextSibling(tag)) + for (auto maybetag = a_NBT.GetFirstChild(a_EnchListTagIdx); maybetag.HasValue(); maybetag = a_NBT.GetNextSibling(maybetag.GetValue())) { + auto tag = maybetag.GetValue(); + // tag is the compound inside the "ench" list tag ASSERT(a_NBT.GetType(tag) == TAG_Compound); // Search for the id and lvl tags' values: int id = -1, lvl = -1; - for (int ch = a_NBT.GetFirstChild(tag); ch >= 0; ch = a_NBT.GetNextSibling(ch)) + for (auto maybech = a_NBT.GetFirstChild(tag); maybech.GetValue(); maybech = a_NBT.GetNextSibling(maybech.GetValue())) { + auto ch = maybech.GetValue(); + if (a_NBT.GetType(ch) != TAG_Short) { continue; diff --git a/src/WorldStorage/EnchantmentSerializer.h b/src/WorldStorage/EnchantmentSerializer.h index 5c12e01bc..91ab4893f 100644 --- a/src/WorldStorage/EnchantmentSerializer.h +++ b/src/WorldStorage/EnchantmentSerializer.h @@ -16,7 +16,7 @@ namespace EnchantmentSerializer void WriteToNBTCompound(const cEnchantments & a_Enchantments, cFastNBTWriter & a_Writer, const AString & a_ListTagName); /** Reads the enchantments from the specified NBT list tag (ench or StoredEnchantments) */ - void ParseFromNBT(cEnchantments & a_Enchantments, const cParsedNBT & a_NBT, int a_EnchListTagIdx); + void ParseFromNBT(cEnchantments & a_Enchantments, const cParsedNBT & a_NBT, size_t a_EnchListTagIdx); }; diff --git a/src/WorldStorage/FastNBT.cpp b/src/WorldStorage/FastNBT.cpp index d4e781af1..93c6a177c 100644 --- a/src/WorldStorage/FastNBT.cpp +++ b/src/WorldStorage/FastNBT.cpp @@ -45,9 +45,9 @@ static const int MAX_LIST_ITEMS = 10000; -cParsedNBT::cParsedNBT(const char * a_Data, size_t a_Length) : - m_Data(a_Data), - m_Length(a_Length), +cParsedNBT::cParsedNBT(const std::basic_string<Byte> & a_Data) : + m_Data(a_Data.c_str()), + m_Length(a_Data.length()), m_Pos(0) { m_IsValid = Parse(); @@ -79,7 +79,7 @@ bool cParsedNBT::Parse(void) m_Tags.reserve(NBT_RESERVE_SIZE); - m_Tags.push_back(cFastNBTTag(TAG_Compound, -1)); + m_Tags.push_back(cFastNBTTag(TAG_Compound, Option<size_t>::None())); m_Pos = 1; @@ -113,12 +113,12 @@ bool cParsedNBT::ReadCompound(void) // Reads the latest tag as a compound size_t ParentIdx = m_Tags.size() - 1; - int PrevSibling = -1; + Option<size_t> PrevSibling = Option<size_t>::None(); for (;;) { NEEDBYTES(1); - const char TagTypeNum = m_Data[m_Pos]; - if ((TagTypeNum < TAG_Min) || (TagTypeNum > TAG_Max)) + const auto TagTypeNum = m_Data[m_Pos]; + if (TagTypeNum > TAG_Max) { return false; } @@ -128,16 +128,16 @@ bool cParsedNBT::ReadCompound(void) { break; } - m_Tags.push_back(cFastNBTTag(TagType, static_cast<int>(ParentIdx), PrevSibling)); - if (PrevSibling >= 0) + m_Tags.push_back(cFastNBTTag(TagType, ParentIdx, PrevSibling)); + if (PrevSibling.HasValue()) { - m_Tags[static_cast<size_t>(PrevSibling)].m_NextSibling = static_cast<int>(m_Tags.size()) - 1; + m_Tags[PrevSibling.GetValue()].m_NextSibling = m_Tags.size() - 1; } else { - m_Tags[ParentIdx].m_FirstChild = static_cast<int>(m_Tags.size()) - 1; + m_Tags[ParentIdx].m_FirstChild = m_Tags.size() - 1; } - PrevSibling = static_cast<int>(m_Tags.size()) - 1; + PrevSibling = m_Tags.size() - 1; RETURN_FALSE_IF_FALSE(ReadString(m_Tags.back().m_NameStart, m_Tags.back().m_NameLength)); RETURN_FALSE_IF_FALSE(ReadTag()); } // while (true) @@ -165,19 +165,19 @@ bool cParsedNBT::ReadList(eTagType a_ChildrenType) // Read items: ASSERT(m_Tags.size() > 0); size_t ParentIdx = m_Tags.size() - 1; - int PrevSibling = -1; + Option<size_t> PrevSibling = Option<size_t>::None(); for (int i = 0; i < Count; i++) { - m_Tags.push_back(cFastNBTTag(a_ChildrenType, static_cast<int>(ParentIdx), PrevSibling)); - if (PrevSibling >= 0) + m_Tags.push_back(cFastNBTTag(a_ChildrenType, ParentIdx, PrevSibling)); + if (PrevSibling.HasValue()) { - m_Tags[static_cast<size_t>(PrevSibling)].m_NextSibling = static_cast<int>(m_Tags.size()) - 1; + m_Tags[PrevSibling.GetValue()].m_NextSibling = m_Tags.size() - 1; } else { - m_Tags[ParentIdx].m_FirstChild = static_cast<int>(m_Tags.size()) - 1; + m_Tags[ParentIdx].m_FirstChild = m_Tags.size() - 1; } - PrevSibling = static_cast<int>(m_Tags.size()) - 1; + PrevSibling = m_Tags.size() - 1; RETURN_FALSE_IF_FALSE(ReadTag()); } // for (i) m_Tags[ParentIdx].m_LastChild = PrevSibling; @@ -281,64 +281,57 @@ bool cParsedNBT::ReadTag(void) -int cParsedNBT::FindChildByName(int a_Tag, const char * a_Name, size_t a_NameLength) const +Option<size_t> cParsedNBT::FindChildByName(size_t a_Tag, const char * a_Name, size_t a_NameLength) const { - if (a_Tag < 0) + if (m_Tags[a_Tag].m_Type != TAG_Compound) { - return -1; - } - if (m_Tags[static_cast<size_t>(a_Tag)].m_Type != TAG_Compound) - { - return -1; + return Option<size_t>::None(); } if (a_NameLength == 0) { a_NameLength = strlen(a_Name); } - for (int Child = m_Tags[static_cast<size_t>(a_Tag)].m_FirstChild; Child != -1; Child = m_Tags[static_cast<size_t>(Child)].m_NextSibling) + for (auto Child = m_Tags[a_Tag].m_FirstChild; Child.HasValue(); Child = m_Tags[Child.GetValue()].m_NextSibling) { if ( - (m_Tags[static_cast<size_t>(Child)].m_NameLength == a_NameLength) && - (memcmp(m_Data + m_Tags[static_cast<size_t>(Child)].m_NameStart, a_Name, a_NameLength) == 0) + (m_Tags[Child.GetValue()].m_NameLength == a_NameLength) && + (memcmp(m_Data + m_Tags[Child.GetValue()].m_NameStart, a_Name, a_NameLength) == 0) ) { - return Child; + return Child.GetValue(); } } // for Child - children of a_Tag - return -1; + return Option<size_t>::None(); } -int cParsedNBT::FindTagByPath(int a_Tag, const AString & a_Path) const +Option<size_t> cParsedNBT::FindTagByPath(size_t a_Tag, const AString & a_Path) const { - if (a_Tag < 0) - { - return -1; - } size_t Begin = 0; size_t Length = a_Path.length(); - int Tag = a_Tag; + size_t Tag = a_Tag; for (size_t i = 0; i < Length; i++) { if (a_Path[i] != '\\') { continue; } - Tag = FindChildByName(Tag, a_Path.c_str() + Begin, i - Begin); - if (Tag < 0) + auto MaybeTag = FindChildByName(Tag, a_Path.c_str() + Begin, i - Begin); + if (!MaybeTag.HasValue()) { - return -1; + return Tag; } + Tag = MaybeTag.GetValue(); Begin = i + 1; } // for i - a_Path[] if (Begin < Length) { - Tag = FindChildByName(Tag, a_Path.c_str() + Begin, Length - Begin); + return FindChildByName(Tag, a_Path.c_str() + Begin, Length - Begin); } return Tag; } @@ -404,12 +397,12 @@ void cFastNBTWriter::BeginList(const AString & a_Name, eTagType a_ChildrenType) TagCommon(a_Name, TAG_List); - m_Result.push_back(static_cast<char>(a_ChildrenType)); + m_Result.push_back(static_cast<Byte>(a_ChildrenType)); m_Result.append(4, static_cast<char>(0)); ++m_CurrentStack; m_Stack[m_CurrentStack].m_Type = TAG_List; - m_Stack[m_CurrentStack].m_Pos = static_cast<int>(m_Result.size()) - 4; + m_Stack[m_CurrentStack].m_Pos = m_Result.size() - 4; m_Stack[m_CurrentStack].m_Count = 0; m_Stack[m_CurrentStack].m_ItemType = a_ChildrenType; } @@ -424,7 +417,7 @@ void cFastNBTWriter::EndList(void) ASSERT(m_Stack[m_CurrentStack].m_Type == TAG_List); // Update the list count: - SetBEInt(const_cast<char *>(m_Result.c_str() + m_Stack[m_CurrentStack].m_Pos), m_Stack[m_CurrentStack].m_Count); + SetBEInt(m_Result, m_Stack[m_CurrentStack].m_Pos, m_Stack[m_CurrentStack].m_Count); --m_CurrentStack; } @@ -436,7 +429,7 @@ void cFastNBTWriter::EndList(void) void cFastNBTWriter::AddByte(const AString & a_Name, unsigned char a_Value) { TagCommon(a_Name, TAG_Byte); - m_Result.push_back(static_cast<char>(a_Value)); + m_Result.push_back(a_Value); } @@ -446,8 +439,8 @@ void cFastNBTWriter::AddByte(const AString & a_Name, unsigned char a_Value) void cFastNBTWriter::AddShort(const AString & a_Name, Int16 a_Value) { TagCommon(a_Name, TAG_Short); - UInt16 Value = htons(a_Value); - m_Result.append(reinterpret_cast<const char *>(&Value), 2); + UInt16 Value = htons(static_cast<UInt16>(a_Value)); + m_Result.append(reinterpret_cast<const Byte *>(&Value), 2); } @@ -457,8 +450,8 @@ void cFastNBTWriter::AddShort(const AString & a_Name, Int16 a_Value) void cFastNBTWriter::AddInt(const AString & a_Name, Int32 a_Value) { TagCommon(a_Name, TAG_Int); - UInt32 Value = htonl(a_Value); - m_Result.append(reinterpret_cast<const char *>(&Value), 4); + UInt32 Value = htonl(static_cast<UInt32>(a_Value)); + m_Result.append(reinterpret_cast<const Byte *>(&Value), 4); } @@ -469,7 +462,7 @@ void cFastNBTWriter::AddLong(const AString & a_Name, Int64 a_Value) { TagCommon(a_Name, TAG_Long); UInt64 Value = HostToNetwork8(&a_Value); - m_Result.append(reinterpret_cast<const char *>(&Value), 8); + m_Result.append(reinterpret_cast<const Byte *>(&Value), 8); } @@ -480,7 +473,7 @@ void cFastNBTWriter::AddFloat(const AString & a_Name, float a_Value) { TagCommon(a_Name, TAG_Float); UInt32 Value = HostToNetwork4(&a_Value); - m_Result.append(reinterpret_cast<const char *>(&Value), 4); + m_Result.append(reinterpret_cast<const Byte *>(&Value), 4); } @@ -491,7 +484,7 @@ void cFastNBTWriter::AddDouble(const AString & a_Name, double a_Value) { TagCommon(a_Name, TAG_Double); UInt64 Value = HostToNetwork8(&a_Value); - m_Result.append(reinterpret_cast<const char *>(&Value), 8); + m_Result.append(reinterpret_cast<const Byte *>(&Value), 8); } @@ -501,20 +494,20 @@ void cFastNBTWriter::AddDouble(const AString & a_Name, double a_Value) void cFastNBTWriter::AddString(const AString & a_Name, const AString & a_Value) { TagCommon(a_Name, TAG_String); - UInt16 len = htons(static_cast<short>(a_Value.size())); - m_Result.append(reinterpret_cast<const char *>(&len), 2); - m_Result.append(a_Value.c_str(), a_Value.size()); + UInt16 len = htons(static_cast<UInt16>(a_Value.size())); + m_Result.append(reinterpret_cast<const Byte *>(&len), 2); + m_Result.append(reinterpret_cast<const Byte *>(a_Value.c_str()), a_Value.size()); } -void cFastNBTWriter::AddByteArray(const AString & a_Name, const char * a_Value, size_t a_NumElements) +void cFastNBTWriter::AddByteArray(const AString & a_Name, const Byte * a_Value, size_t a_NumElements) { TagCommon(a_Name, TAG_ByteArray); u_int len = htonl(static_cast<u_int>(a_NumElements)); - m_Result.append(reinterpret_cast<const char *>(&len), 4); + m_Result.append(reinterpret_cast<const Byte *>(&len), 4); m_Result.append(a_Value, a_NumElements); } @@ -532,11 +525,11 @@ void cFastNBTWriter::AddIntArray(const AString & a_Name, const int * a_Value, si { m_Result.reserve(size + 4 + (a_NumElements * 4)); } - m_Result.append(reinterpret_cast<const char *>(&len), 4); + m_Result.append(reinterpret_cast<const Byte *>(&len), 4); for (size_t i = 0; i < a_NumElements; i++) { - UInt32 Element = htonl(a_Value[i]); - m_Result.append(reinterpret_cast<const char *>(&Element), 4); + UInt32 Element = htonl(static_cast<UInt32>(a_Value[i])); + m_Result.append(reinterpret_cast<const Byte *>(&Element), 4); } } @@ -557,8 +550,8 @@ void cFastNBTWriter::Finish(void) void cFastNBTWriter::WriteString(const char * a_Data, UInt16 a_Length) { UInt16 Len = htons(a_Length); - m_Result.append(reinterpret_cast<const char *>(&Len), 2); - m_Result.append(a_Data, a_Length); + m_Result.append(reinterpret_cast<const Byte *>(&Len), 2); + m_Result.append(reinterpret_cast<const Byte *>(a_Data), a_Length); } diff --git a/src/WorldStorage/FastNBT.h b/src/WorldStorage/FastNBT.h index a23dc7af2..a2fe1cd51 100644 --- a/src/WorldStorage/FastNBT.h +++ b/src/WorldStorage/FastNBT.h @@ -20,6 +20,7 @@ It directly outputs a string containing the serialized NBT data. #pragma once #include "../Endianness.h" +#include "../Option.h" @@ -66,29 +67,29 @@ public: size_t m_DataStart; size_t m_DataLength; - // The following members are indices into the array returned; -1 if not valid + // The following members are indices into the array returned; none if not valid // They must not be pointers, because pointers would not survive std::vector reallocation - int m_Parent; - int m_PrevSibling; - int m_NextSibling; - int m_FirstChild; - int m_LastChild; + Option<size_t> m_Parent; + Option<size_t> m_PrevSibling; + Option<size_t> m_NextSibling; + Option<size_t> m_FirstChild; + Option<size_t> m_LastChild; - cFastNBTTag(eTagType a_Type, int a_Parent) : + cFastNBTTag(eTagType a_Type, Option<size_t> a_Parent) : m_Type(a_Type), m_NameStart(0), m_NameLength(0), m_DataStart(0), m_DataLength(0), m_Parent(a_Parent), - m_PrevSibling(-1), - m_NextSibling(-1), - m_FirstChild(-1), - m_LastChild(-1) + m_PrevSibling(Option<size_t>::None()), + m_NextSibling(Option<size_t>::None()), + m_FirstChild(Option<size_t>::None()), + m_LastChild(Option<size_t>::None()) { } - cFastNBTTag(eTagType a_Type, int a_Parent, int a_PrevSibling) : + cFastNBTTag(eTagType a_Type, Option<size_t> a_Parent, Option<size_t> a_PrevSibling) : m_Type(a_Type), m_NameStart(0), m_NameLength(0), @@ -96,9 +97,9 @@ public: m_DataLength(0), m_Parent(a_Parent), m_PrevSibling(a_PrevSibling), - m_NextSibling(-1), - m_FirstChild(-1), - m_LastChild(-1) + m_NextSibling(Option<size_t>::None()), + m_FirstChild(Option<size_t>::None()), + m_LastChild(Option<size_t>::None()) { } } ; @@ -118,80 +119,81 @@ Each primitive tag also stores the length of the contained data, in bytes. class cParsedNBT { public: - cParsedNBT(const char * a_Data, size_t a_Length); + explicit cParsedNBT(const std::basic_string<Byte> & data); bool IsValid(void) const {return m_IsValid; } /** Returns the root tag of the hierarchy. */ - int GetRoot(void) const {return 0; } + size_t GetRoot(void) const {return 0; } - /** Returns the first child of the specified tag, or -1 if none / not applicable. */ - int GetFirstChild (int a_Tag) const { return m_Tags[static_cast<size_t>(a_Tag)].m_FirstChild; } + /** Returns the first child of the specified tag, or none if none / not applicable. */ + Option<size_t> GetFirstChild (size_t a_Tag) const { return m_Tags[a_Tag].m_FirstChild; } - /** Returns the last child of the specified tag, or -1 if none / not applicable. */ - int GetLastChild (int a_Tag) const { return m_Tags[static_cast<size_t>(a_Tag)].m_LastChild; } + /** Returns the last child of the specified tag, or none if none / not applicable. */ + Option<size_t> GetLastChild (size_t a_Tag) const { return m_Tags[a_Tag].m_LastChild; } - /** Returns the next sibling of the specified tag, or -1 if none. */ - int GetNextSibling(int a_Tag) const { return m_Tags[static_cast<size_t>(a_Tag)].m_NextSibling; } + /** Returns the next sibling of the specified tag, or none. */ + Option<size_t> GetNextSibling(size_t a_Tag) const { return m_Tags[a_Tag].m_NextSibling; } - /** Returns the previous sibling of the specified tag, or -1 if none. */ - int GetPrevSibling(int a_Tag) const { return m_Tags[static_cast<size_t>(a_Tag)].m_PrevSibling; } + /** Returns the previous sibling of the specified tag, or none if none. */ + Option<size_t> GetPrevSibling(size_t a_Tag) const { return m_Tags[a_Tag].m_PrevSibling; } /** Returns the length of the tag's data, in bytes. Not valid for Compound or List tags! */ - size_t GetDataLength (int a_Tag) const + size_t GetDataLength (size_t a_Tag) const { - ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type != TAG_List); - ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type != TAG_Compound); - return m_Tags[static_cast<size_t>(a_Tag)].m_DataLength; + ASSERT(m_Tags[a_Tag].m_Type != TAG_List); + ASSERT(m_Tags[a_Tag].m_Type != TAG_Compound); + return m_Tags[a_Tag].m_DataLength; } /** Returns the data stored in this tag. Not valid for Compound or List tags! */ - const char * GetData(int a_Tag) const + const Byte * GetData(size_t a_Tag) const { ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type != TAG_List); ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type != TAG_Compound); return m_Data + m_Tags[static_cast<size_t>(a_Tag)].m_DataStart; } - /** Returns the direct child tag of the specified name, or -1 if no such tag. */ - int FindChildByName(int a_Tag, const AString & a_Name) const + /** Returns the direct child tag of the specified name, or none if no such tag. */ + Option<size_t> FindChildByName(size_t a_Tag, const AString & a_Name) const + { return FindChildByName(a_Tag, a_Name.c_str(), a_Name.length()); } - /** Returns the direct child tag of the specified name, or -1 if no such tag. */ - int FindChildByName(int a_Tag, const char * a_Name, size_t a_NameLength = 0) const; + /** Returns the direct child tag of the specified name, or none if no such tag. */ + Option<size_t> FindChildByName(size_t a_Tag, const char * a_Name, size_t a_NameLength = 0) const; - /** Returns the child tag of the specified path (Name1 / Name2 / Name3...), or -1 if no such tag. */ - int FindTagByPath(int a_Tag, const AString & a_Path) const; + /** Returns the child tag of the specified path (Name1 / Name2 / Name3...), or none if no such tag. */ + Option<size_t> FindTagByPath(size_t a_Tag, const AString & a_Path) const; - eTagType GetType(int a_Tag) const { return m_Tags[static_cast<size_t>(a_Tag)].m_Type; } + eTagType GetType(size_t a_Tag) const { return m_Tags[static_cast<size_t>(a_Tag)].m_Type; } /** Returns the children type for a List tag; undefined on other tags. If list empty, returns TAG_End. */ - eTagType GetChildrenType(int a_Tag) const + eTagType GetChildrenType(size_t a_Tag) const { ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type == TAG_List); - return (m_Tags[static_cast<size_t>(a_Tag)].m_FirstChild < 0) ? TAG_End : m_Tags[static_cast<size_t>(m_Tags[static_cast<size_t>(a_Tag)].m_FirstChild)].m_Type; + return m_Tags[static_cast<size_t>(a_Tag)].m_FirstChild.To<eTagType>([this](size_t FirstChildValue) { return m_Tags[FirstChildValue].m_Type; }, TAG_End); } /** Returns the value stored in a Byte tag. Not valid for any other tag type. */ - inline unsigned char GetByte(int a_Tag) const + inline unsigned char GetByte(size_t a_Tag) const { ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type == TAG_Byte); return static_cast<unsigned char>(m_Data[static_cast<size_t>(m_Tags[static_cast<size_t>(a_Tag)].m_DataStart)]); } /** Returns the value stored in a Short tag. Not valid for any other tag type. */ - inline Int16 GetShort(int a_Tag) const + inline Int16 GetShort(size_t a_Tag) const { - ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type == TAG_Short); - return GetBEShort(m_Data + m_Tags[static_cast<size_t>(a_Tag)].m_DataStart); + ASSERT(m_Tags[a_Tag].m_Type == TAG_Short); + return GetBEShort(m_Data + m_Tags[a_Tag].m_DataStart); } /** Returns the value stored in an Int tag. Not valid for any other tag type. */ - inline Int32 GetInt(int a_Tag) const + inline Int32 GetInt(size_t a_Tag) const { ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type == TAG_Int); return GetBEInt(m_Data + m_Tags[static_cast<size_t>(a_Tag)].m_DataStart); @@ -237,24 +239,28 @@ public: } /** Returns the value stored in a String tag. Not valid for any other tag type. */ - inline AString GetString(int a_Tag) const + inline AString GetString(size_t a_Tag) const { ASSERT(m_Tags[static_cast<size_t>(a_Tag)].m_Type == TAG_String); - AString res; - res.assign(m_Data + m_Tags[static_cast<size_t>(a_Tag)].m_DataStart, static_cast<size_t>(m_Tags[static_cast<size_t>(a_Tag)].m_DataLength)); + size_t length = m_Tags[static_cast<size_t>(a_Tag)].m_DataLength; + const Byte * DataStart = m_Data + m_Tags[a_Tag].m_DataStart; + AString res(length, 0); + std::transform(DataStart, DataStart + length, res.begin(), [](Byte c){ return static_cast<char>(c); }); return res; } /** Returns the tag's name. For tags that are not named, returns an empty string. */ - inline AString GetName(int a_Tag) const + inline AString GetName(size_t a_Tag) const { - AString res; - res.assign(m_Data + m_Tags[static_cast<size_t>(a_Tag)].m_NameStart, static_cast<size_t>(m_Tags[static_cast<size_t>(a_Tag)].m_NameLength)); + size_t length = m_Tags[a_Tag].m_NameLength; + const Byte * NameStart = m_Data + m_Tags[a_Tag].m_NameStart; + AString res(length, 0); + std::transform(NameStart, NameStart + length, res.begin(), [](Byte c) { return static_cast<char>(c); }); return res; } protected: - const char * m_Data; + const Byte * m_Data; size_t m_Length; std::vector<cFastNBTTag> m_Tags; bool m_IsValid; // True if parsing succeeded @@ -291,15 +297,15 @@ public: void AddFloat (const AString & a_Name, float a_Value); void AddDouble (const AString & a_Name, double a_Value); void AddString (const AString & a_Name, const AString & a_Value); - void AddByteArray(const AString & a_Name, const char * a_Value, size_t a_NumElements); + void AddByteArray(const AString & a_Name, const Byte * a_Value, size_t a_NumElements); void AddIntArray (const AString & a_Name, const int * a_Value, size_t a_NumElements); - void AddByteArray(const AString & a_Name, const AString & a_Value) + void AddByteArray(const AString & a_Name, const std::basic_string<Byte> & a_Value) { AddByteArray(a_Name, a_Value.data(), a_Value.size()); } - const AString & GetResult(void) const {return m_Result; } + const std::basic_string<Byte> & GetResult(void) const {return m_Result; } void Finish(void); @@ -308,7 +314,7 @@ protected: struct sParent { int m_Type; // TAG_Compound or TAG_List - int m_Pos; // for TAG_List, the position of the list count + size_t m_Pos; // for TAG_List, the position of the list count int m_Count; // for TAG_List, the element count eTagType m_ItemType; // for TAG_List, the element type } ; @@ -319,7 +325,7 @@ protected: sParent m_Stack[MAX_STACK]; int m_CurrentStack; - AString m_Result; + std::basic_string<Byte> m_Result; bool IsStackTopCompound(void) const { return (m_Stack[m_CurrentStack].m_Type == TAG_Compound); } @@ -333,7 +339,7 @@ protected: if (IsStackTopCompound()) { // Compound: add the type and name: - m_Result.push_back(static_cast<char>(a_Type)); + m_Result.push_back(static_cast<Byte>(a_Type)); WriteString(a_Name.c_str(), static_cast<UInt16>(a_Name.length())); } else diff --git a/src/WorldStorage/FireworksSerializer.cpp b/src/WorldStorage/FireworksSerializer.cpp index 0398f4da8..4840bf05a 100644 --- a/src/WorldStorage/FireworksSerializer.cpp +++ b/src/WorldStorage/FireworksSerializer.cpp @@ -58,19 +58,15 @@ void cFireworkItem::WriteToNBTCompound(const cFireworkItem & a_FireworkItem, cFa -void cFireworkItem::ParseFromNBT(cFireworkItem & a_FireworkItem, const cParsedNBT & a_NBT, int a_TagIdx, const ENUM_ITEM_ID a_Type) +void cFireworkItem::ParseFromNBT(cFireworkItem & a_FireworkItem, const cParsedNBT & a_NBT, size_t a_TagIdx, const ENUM_ITEM_ID a_Type) { - if (a_TagIdx < 0) - { - return; - } - switch (a_Type) { case E_ITEM_FIREWORK_STAR: { - for (int explosiontag = a_NBT.GetFirstChild(a_TagIdx); explosiontag >= 0; explosiontag = a_NBT.GetNextSibling(explosiontag)) + for (auto maybeexplosiontag = a_NBT.GetFirstChild(a_TagIdx); maybeexplosiontag.HasValue(); maybeexplosiontag = a_NBT.GetNextSibling(maybeexplosiontag.GetValue())) { + auto explosiontag = maybeexplosiontag.GetValue(); eTagType TagType = a_NBT.GetType(explosiontag); if (TagType == TAG_Byte) // Custon name tag { @@ -104,7 +100,7 @@ void cFireworkItem::ParseFromNBT(cFireworkItem & a_FireworkItem, const cParsedNB continue; } - const char * ColourData = (a_NBT.GetData(explosiontag)); + const Byte * ColourData = (a_NBT.GetData(explosiontag)); for (size_t i = 0; i < DataLength; i += 4) { a_FireworkItem.m_Colours.push_back(GetBEInt(ColourData + i)); @@ -120,7 +116,7 @@ void cFireworkItem::ParseFromNBT(cFireworkItem & a_FireworkItem, const cParsedNB continue; } - const char * FadeColourData = (a_NBT.GetData(explosiontag)); + const Byte * FadeColourData = (a_NBT.GetData(explosiontag)); for (size_t i = 0; i < DataLength; i += 4) { a_FireworkItem.m_FadeColours.push_back(GetBEInt(FadeColourData + i)); @@ -132,8 +128,9 @@ void cFireworkItem::ParseFromNBT(cFireworkItem & a_FireworkItem, const cParsedNB } case E_ITEM_FIREWORK_ROCKET: { - for (int fireworkstag = a_NBT.GetFirstChild(a_TagIdx); fireworkstag >= 0; fireworkstag = a_NBT.GetNextSibling(fireworkstag)) + for (auto maybefireworkstag = a_NBT.GetFirstChild(a_TagIdx); maybefireworkstag.HasValue(); maybefireworkstag = a_NBT.GetNextSibling(maybefireworkstag.GetValue())) { + auto fireworkstag = maybefireworkstag.GetValue(); eTagType TagType = a_NBT.GetType(fireworkstag); if (TagType == TAG_Byte) // Custon name tag { @@ -144,10 +141,10 @@ void cFireworkItem::ParseFromNBT(cFireworkItem & a_FireworkItem, const cParsedNB } else if ((TagType == TAG_List) && (a_NBT.GetName(fireworkstag) == "Explosions")) { - int ExplosionsChild = a_NBT.GetFirstChild(fireworkstag); - if ((a_NBT.GetType(ExplosionsChild) == TAG_Compound) && (a_NBT.GetName(ExplosionsChild).empty())) + auto ExplosionsChild = a_NBT.GetFirstChild(fireworkstag); + if ((a_NBT.GetType(ExplosionsChild.GetValue()) == TAG_Compound) && (a_NBT.GetName(ExplosionsChild.GetValue()).empty())) { - ParseFromNBT(a_FireworkItem, a_NBT, ExplosionsChild, E_ITEM_FIREWORK_STAR); + ParseFromNBT(a_FireworkItem, a_NBT, ExplosionsChild.GetValue(), E_ITEM_FIREWORK_STAR); } } } diff --git a/src/WorldStorage/FireworksSerializer.h b/src/WorldStorage/FireworksSerializer.h index 59f1b09b0..ad9569e3a 100644 --- a/src/WorldStorage/FireworksSerializer.h +++ b/src/WorldStorage/FireworksSerializer.h @@ -66,7 +66,7 @@ public: static void WriteToNBTCompound(const cFireworkItem & a_FireworkItem, cFastNBTWriter & a_Writer, const ENUM_ITEM_ID a_Type); /** Reads NBT data from a NBT object and populates a FireworkItem with it */ - static void ParseFromNBT(cFireworkItem & a_FireworkItem, const cParsedNBT & a_NBT, int a_TagIdx, const ENUM_ITEM_ID a_Type); + static void ParseFromNBT(cFireworkItem & a_FireworkItem, const cParsedNBT & a_NBT, size_t a_TagIdx, const ENUM_ITEM_ID a_Type); /** Converts the firework's vector of colours into a string of values separated by a semicolon */ static AString ColoursToString(const cFireworkItem & a_FireworkItem); diff --git a/src/WorldStorage/MapSerializer.cpp b/src/WorldStorage/MapSerializer.cpp index 8acfa4696..5b8a44c95 100644 --- a/src/WorldStorage/MapSerializer.cpp +++ b/src/WorldStorage/MapSerializer.cpp @@ -38,7 +38,7 @@ bool cMapSerializer::Load(void) return false; } - AString Uncompressed; + std::basic_string<Byte> Uncompressed; int res = UncompressStringGZIP(Data.data(), Data.size(), Uncompressed); if (res != Z_OK) @@ -47,7 +47,7 @@ bool cMapSerializer::Load(void) } // Parse the NBT data: - cParsedNBT NBT(Uncompressed.data(), Uncompressed.size()); + cParsedNBT NBT(Uncompressed); if (!NBT.IsValid()) { // NBT Parsing failed @@ -70,7 +70,7 @@ bool cMapSerializer::Save(void) Writer.Finish(); #ifdef _DEBUG - cParsedNBT TestParse(Writer.GetResult().data(), Writer.GetResult().size()); + cParsedNBT TestParse(Writer.GetResult()); ASSERT(TestParse.IsValid()); #endif // _DEBUG @@ -80,7 +80,7 @@ bool cMapSerializer::Save(void) return false; } - AString Compressed; + std::basic_string<Byte> Compressed; int res = CompressStringGZIP(Writer.GetResult().data(), Writer.GetResult().size(), Compressed); if (res != Z_OK) @@ -112,7 +112,7 @@ void cMapSerializer::SaveMapToNBT(cFastNBTWriter & a_Writer) a_Writer.AddInt("zCenter", m_Map->GetCenterZ()); const cMap::cColorList & Data = m_Map->GetData(); - a_Writer.AddByteArray("colors", reinterpret_cast<const char *>(Data.data()), Data.size()); + a_Writer.AddByteArray("colors", reinterpret_cast<const Byte *>(Data.data()), Data.size()); a_Writer.EndCompound(); } @@ -123,21 +123,21 @@ void cMapSerializer::SaveMapToNBT(cFastNBTWriter & a_Writer) bool cMapSerializer::LoadMapFromNBT(const cParsedNBT & a_NBT) { - int Data = a_NBT.FindChildByName(0, "data"); - if (Data < 0) + auto Data = a_NBT.FindChildByName(0, "data"); + if (!Data.HasValue()) { return false; } - int CurrLine = a_NBT.FindChildByName(Data, "scale"); - if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_Byte)) + auto CurrLine = a_NBT.FindChildByName(Data.GetValue(), "scale"); + if ((CurrLine.HasValue()) && (a_NBT.GetType(CurrLine.GetValue()) == TAG_Byte)) { - unsigned int Scale = static_cast<unsigned int>(a_NBT.GetByte(CurrLine)); + unsigned int Scale = static_cast<unsigned int>(a_NBT.GetByte(CurrLine.GetValue())); m_Map->SetScale(Scale); } - CurrLine = a_NBT.FindChildByName(Data, "dimension"); - if ((CurrLine >= 0) && (a_NBT.GetType(CurrLine) == TAG_Byte)) + CurrLine = a_NBT.FindChildByName(Data.GetValue(), "dimension"); + if ((CurrLine.HasValue()) && (a_NBT.GetType(CurrLine.GetValue()) == TAG_Byte)) { eDimension Dimension = static_cast<eDimension>(a_NBT.GetByte(CurrLine)); |