diff --git a/Client/game_sa/StdInc.h b/Client/game_sa/StdInc.h index 7f6c2701cd5..d07dc2ec05d 100644 --- a/Client/game_sa/StdInc.h +++ b/Client/game_sa/StdInc.h @@ -24,9 +24,6 @@ #include #include -// SDK includes -#include - // Game includes #include "HookSystem.h" #include "gamesa_init.h" diff --git a/Client/multiplayer_sa/StdInc.h b/Client/multiplayer_sa/StdInc.h index 1694174cae2..3103d4a8d38 100644 --- a/Client/multiplayer_sa/StdInc.h +++ b/Client/multiplayer_sa/StdInc.h @@ -19,7 +19,6 @@ // SDK includes #include -#include #include #include #include diff --git a/Server/mods/deathmatch/logic/packets/CConsoleEchoPacket.cpp b/Server/mods/deathmatch/logic/packets/CConsoleEchoPacket.cpp index 7f1f2a434b7..93297b26f1f 100644 --- a/Server/mods/deathmatch/logic/packets/CConsoleEchoPacket.cpp +++ b/Server/mods/deathmatch/logic/packets/CConsoleEchoPacket.cpp @@ -15,11 +15,10 @@ bool CConsoleEchoPacket::Write(NetBitStreamInterface& BitStream) const { // Not too short? - size_t sizeMessage = m_strMessage.length(); - if (sizeMessage >= MIN_CONSOLEECHO_LENGTH) + if (m_strMessage.length() >= MIN_CONSOLEECHO_LENGTH) { // Write the string - BitStream.WriteStringCharacters(m_strMessage, sizeMessage); + BitStream.WriteStringCharacters(m_strMessage); return true; } diff --git a/Server/mods/deathmatch/logic/packets/CDebugEchoPacket.cpp b/Server/mods/deathmatch/logic/packets/CDebugEchoPacket.cpp index e84caff7765..958740dbe78 100644 --- a/Server/mods/deathmatch/logic/packets/CDebugEchoPacket.cpp +++ b/Server/mods/deathmatch/logic/packets/CDebugEchoPacket.cpp @@ -25,11 +25,10 @@ bool CDebugEchoPacket::Write(NetBitStreamInterface& BitStream) const } // Too short? - size_t sizeMessage = m_strMessage.length(); - if (sizeMessage >= MIN_DEBUGECHO_LENGTH) + if (m_strMessage.length() >= MIN_DEBUGECHO_LENGTH) { // Write the string - BitStream.WriteStringCharacters(m_strMessage, sizeMessage); + BitStream.WriteStringCharacters(m_strMessage); return true; } diff --git a/Server/mods/deathmatch/logic/packets/CLuaEventPacket.cpp b/Server/mods/deathmatch/logic/packets/CLuaEventPacket.cpp index ae8edea14d3..0bb42c76203 100644 --- a/Server/mods/deathmatch/logic/packets/CLuaEventPacket.cpp +++ b/Server/mods/deathmatch/logic/packets/CLuaEventPacket.cpp @@ -47,9 +47,8 @@ bool CLuaEventPacket::Read(NetBitStreamInterface& BitStream) bool CLuaEventPacket::Write(NetBitStreamInterface& BitStream) const { - unsigned short usNameLength = static_cast(m_strName.length()); - BitStream.WriteCompressed(usNameLength); - BitStream.WriteStringCharacters(m_strName, usNameLength); + BitStream.WriteCompressed(static_cast(m_strName.length())); + BitStream.WriteStringCharacters(m_strName); BitStream.Write(m_ElementID); m_pArguments->WriteToBitStream(BitStream); diff --git a/Server/mods/deathmatch/logic/packets/CPlayerChangeNickPacket.cpp b/Server/mods/deathmatch/logic/packets/CPlayerChangeNickPacket.cpp index 7f83c4e4439..9ff323961cd 100644 --- a/Server/mods/deathmatch/logic/packets/CPlayerChangeNickPacket.cpp +++ b/Server/mods/deathmatch/logic/packets/CPlayerChangeNickPacket.cpp @@ -27,7 +27,7 @@ bool CPlayerChangeNickPacket::Write(NetBitStreamInterface& BitStream) const BitStream.Write(ID); // Write the nick - BitStream.WriteStringCharacters(m_strNewNick, m_strNewNick.length()); + BitStream.WriteStringCharacters(m_strNewNick); return true; } diff --git a/Shared/sdk/net/bitstream.h b/Shared/sdk/net/bitstream.h index 9f14a345373..de21542907a 100644 --- a/Shared/sdk/net/bitstream.h +++ b/Shared/sdk/net/bitstream.h @@ -23,6 +23,10 @@ #include #endif +#ifdef __cpp_lib_string_view +#include +#endif + struct ISyncStructure; class NetBitStreamInterface; @@ -196,15 +200,43 @@ class NetBitStreamInterfaceNoVersion : public CRefCountable // Return true if enough bytes left in the bitstream bool CanReadNumberOfBytes(int iLength) const { return iLength >= 0 && iLength <= (GetNumberOfUnreadBits() + 7) / 8; } - // Write characters from a std::string - void WriteStringCharacters(const std::string& value, uint uiLength) + // For whatever stupid reason sdk/net gets included in Multiplayer SA and Game SA + // And since those projects are c++14 we need this stuff. + // TODO: Rip sdk/net out of these projects... +#ifdef __cpp_lib_string_view + // Write characters in `value` + void WriteStringCharacters(std::string_view value) { - dassert(uiLength <= value.length()); - // Send the data - if (uiLength) - Write(&value.at(0), uiLength); + if (!value.empty()) + Write(value.data(), (int)value.length()); + } + // Write `n` characters from `value` + void WriteStringCharacters(std::string_view value, size_t n) + { + dassert(n <= value.length()); + if (n) + Write(value.data(), (int)n); + } + + // Write all characters in `value` (incl. length as `SizeType`) + template + void WriteString(std::string_view value) + { + // Write the length + Write(static_cast(value.length())); + + // Write the characters + return WriteStringCharacters(value); } + // Write a string (incl. variable size header) + void WriteStr(std::string_view value) + { + WriteLength(value.length()); + return WriteStringCharacters(value, value.length()); + } +#endif + // Read characters into a std::string bool ReadStringCharacters(std::string& result, uint uiLength) { @@ -224,18 +256,6 @@ class NetBitStreamInterfaceNoVersion : public CRefCountable return true; } - // Write a string (incl. ushort size header) - template - void WriteString(const std::string& value) - { - // Write the length - auto length = static_cast(value.length()); - Write(length); - - // Write the characters - return WriteStringCharacters(value, length); - } - // Read a string (incl. ushort size header) template bool ReadString(std::string& result) @@ -296,13 +316,6 @@ class NetBitStreamInterfaceNoVersion : public CRefCountable return true; } - // Write a string (incl. variable size header) - void WriteStr(const std::string& value) - { - WriteLength(value.length()); - return WriteStringCharacters(value, value.length()); - } - // Read a string (incl. variable size header) bool ReadStr(std::string& result) {