From 754edb9455ef09695f155e15cf2038fae20db033 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 21 Mar 2021 17:26:54 +0100 Subject: [PATCH 1/7] Refactor WriteStringCharacters and WriteString to use string_view --- Shared/sdk/net/bitstream.h | 44 ++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/Shared/sdk/net/bitstream.h b/Shared/sdk/net/bitstream.h index fd12475ad91..62584fc3f97 100644 --- a/Shared/sdk/net/bitstream.h +++ b/Shared/sdk/net/bitstream.h @@ -22,6 +22,7 @@ #ifndef WIN32 #include #endif +#include struct ISyncStructure; class NetBitStreamInterface; @@ -196,13 +197,30 @@ 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) + // 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 (!value.empty()) + Write(value.data(), (int)n); + } + + // Write all characters in `value` (incl. length with with type `SizeType`) + template + void WriteString(std::string_view value) + { + // Write the length + Write(static_cast(value.length())); + + // Write the characters + return WriteStringCharacters(value); } // Read characters into a std::string @@ -224,18 +242,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) @@ -297,7 +303,7 @@ class NetBitStreamInterfaceNoVersion : public CRefCountable } // Write a string (incl. variable size header) - void WriteStr(const std::string& value) + void WriteStr(std::string_view value) { WriteLength(value.length()); return WriteStringCharacters(value, value.length()); From 9249e5f40a8f88dc6b57ce10666d28eed2eacd31 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 21 Mar 2021 17:27:06 +0100 Subject: [PATCH 2/7] Refactor code using these functions --- Server/mods/deathmatch/logic/packets/CConsoleEchoPacket.cpp | 5 ++--- Server/mods/deathmatch/logic/packets/CDebugEchoPacket.cpp | 5 ++--- Server/mods/deathmatch/logic/packets/CLuaEventPacket.cpp | 5 ++--- .../deathmatch/logic/packets/CPlayerChangeNickPacket.cpp | 2 +- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/Server/mods/deathmatch/logic/packets/CConsoleEchoPacket.cpp b/Server/mods/deathmatch/logic/packets/CConsoleEchoPacket.cpp index 6237d04a21b..1cf1a0b3c79 100644 --- a/Server/mods/deathmatch/logic/packets/CConsoleEchoPacket.cpp +++ b/Server/mods/deathmatch/logic/packets/CConsoleEchoPacket.cpp @@ -14,11 +14,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 f98c082ef91..e8a653b6ced 100644 --- a/Server/mods/deathmatch/logic/packets/CDebugEchoPacket.cpp +++ b/Server/mods/deathmatch/logic/packets/CDebugEchoPacket.cpp @@ -24,11 +24,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 d5bcec17d3a..421b50dab43 100644 --- a/Server/mods/deathmatch/logic/packets/CLuaEventPacket.cpp +++ b/Server/mods/deathmatch/logic/packets/CLuaEventPacket.cpp @@ -46,9 +46,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 6505b62c7ee..e7284c299ce 100644 --- a/Server/mods/deathmatch/logic/packets/CPlayerChangeNickPacket.cpp +++ b/Server/mods/deathmatch/logic/packets/CPlayerChangeNickPacket.cpp @@ -25,7 +25,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; } From 436bd88f2685112255e8857889de48f728c2e132 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 21 Mar 2021 20:21:15 +0100 Subject: [PATCH 3/7] Rip net out of GameSA and MultiplayerSA --- Client/game_sa/StdInc.h | 3 --- Client/multiplayer_sa/StdInc.h | 1 - 2 files changed, 4 deletions(-) diff --git a/Client/game_sa/StdInc.h b/Client/game_sa/StdInc.h index 19a73de95be..e3c63d1fded 100644 --- a/Client/game_sa/StdInc.h +++ b/Client/game_sa/StdInc.h @@ -19,13 +19,10 @@ #include // SDK includes -#include #include -#include #include #include #include -#include // Game includes #include "CEntitySA.h" diff --git a/Client/multiplayer_sa/StdInc.h b/Client/multiplayer_sa/StdInc.h index d21dcbb14a2..dfd9e7c2902 100644 --- a/Client/multiplayer_sa/StdInc.h +++ b/Client/multiplayer_sa/StdInc.h @@ -19,7 +19,6 @@ // SDK includes #include -#include #include #include #include From 402301c5e1c8f358c9658e2d011b2dd2d8c9cd37 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 21 Mar 2021 20:53:20 +0100 Subject: [PATCH 4/7] That didnt work. Macro time! --- Shared/sdk/net/bitstream.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Shared/sdk/net/bitstream.h b/Shared/sdk/net/bitstream.h index 62584fc3f97..c5fbc8301a0 100644 --- a/Shared/sdk/net/bitstream.h +++ b/Shared/sdk/net/bitstream.h @@ -22,7 +22,10 @@ #ifndef WIN32 #include #endif + +#ifdef __cpp_lib_string_view #include +#endif struct ISyncStructure; class NetBitStreamInterface; @@ -197,13 +200,16 @@ 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; } + // 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) { if (!value.empty()) Write(value.data(), (int)value.length()); } - // Write `n` characters from `value` void WriteStringCharacters(std::string_view value, size_t n) { @@ -222,6 +228,7 @@ class NetBitStreamInterfaceNoVersion : public CRefCountable // Write the characters return WriteStringCharacters(value); } +#endif // Read characters into a std::string bool ReadStringCharacters(std::string& result, uint uiLength) From f3225adefbfce4af1452dffd53864a8e6254b655 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Sun, 21 Mar 2021 20:53:53 +0100 Subject: [PATCH 5/7] That didnt work. Macro time! Again. --- Shared/sdk/net/bitstream.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Shared/sdk/net/bitstream.h b/Shared/sdk/net/bitstream.h index c5fbc8301a0..2740992ecbc 100644 --- a/Shared/sdk/net/bitstream.h +++ b/Shared/sdk/net/bitstream.h @@ -228,6 +228,13 @@ class NetBitStreamInterfaceNoVersion : public CRefCountable // 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 @@ -309,13 +316,6 @@ class NetBitStreamInterfaceNoVersion : public CRefCountable return true; } - // Write a string (incl. variable size header) - void WriteStr(std::string_view value) - { - WriteLength(value.length()); - return WriteStringCharacters(value, value.length()); - } - // Read a string (incl. variable size header) bool ReadStr(std::string& result) { From 845767f861662acb122d23d137dc9c2cf95c0524 Mon Sep 17 00:00:00 2001 From: Pirulax Date: Mon, 22 Mar 2021 20:25:47 +0100 Subject: [PATCH 6/7] Fix code inconsistency --- Shared/sdk/net/bitstream.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Shared/sdk/net/bitstream.h b/Shared/sdk/net/bitstream.h index 2740992ecbc..fc4e3c04ee4 100644 --- a/Shared/sdk/net/bitstream.h +++ b/Shared/sdk/net/bitstream.h @@ -214,11 +214,11 @@ class NetBitStreamInterfaceNoVersion : public CRefCountable void WriteStringCharacters(std::string_view value, size_t n) { dassert(n <= value.length()); - if (!value.empty()) + if (n) Write(value.data(), (int)n); } - // Write all characters in `value` (incl. length with with type `SizeType`) + // Write all characters in `value` (incl. length as `SizeType`) template void WriteString(std::string_view value) { From 7e58074941f110f6928948bcb77492877b040e7c Mon Sep 17 00:00:00 2001 From: patrikjuvonen <22572159+patrikjuvonen@users.noreply.github.com> Date: Sat, 8 Apr 2023 14:09:58 +0300 Subject: [PATCH 7/7] Update StdInc.h --- Client/game_sa/StdInc.h | 6 ------ 1 file changed, 6 deletions(-) diff --git a/Client/game_sa/StdInc.h b/Client/game_sa/StdInc.h index b3a19dae5e7..d07dc2ec05d 100644 --- a/Client/game_sa/StdInc.h +++ b/Client/game_sa/StdInc.h @@ -24,12 +24,6 @@ #include #include -// SDK includes -#include -#include -#include -#include - // Game includes #include "HookSystem.h" #include "gamesa_init.h"