Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 66ff543

Browse files
authored
Refactor BitStream to use std::string_view (PR #2143)
1 parent 9944aeb commit 66ff543

File tree

7 files changed

+45
-39
lines changed

7 files changed

+45
-39
lines changed

‎Client/game_sa/StdInc.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
#include <string>
2525
#include <vector>
2626

27-
// SDK includes
28-
#include <ijsify.h>
29-
3027
// Game includes
3128
#include "HookSystem.h"
3229
#include "gamesa_init.h"

‎Client/multiplayer_sa/StdInc.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
// SDK includes
2121
#include <core/CCoreInterface.h>
22-
#include <net/CNet.h>
2322
#include <game/CGame.h>
2423
#include <CMatrix_Pad.h>
2524
#include <version.h>

‎Server/mods/deathmatch/logic/packets/CConsoleEchoPacket.cpp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
bool CConsoleEchoPacket::Write(NetBitStreamInterface& BitStream) const
1616
{
1717
// Not too short?
18-
size_t sizeMessage = m_strMessage.length();
19-
if (sizeMessage >= MIN_CONSOLEECHO_LENGTH)
18+
if (m_strMessage.length() >= MIN_CONSOLEECHO_LENGTH)
2019
{
2120
// Write the string
22-
BitStream.WriteStringCharacters(m_strMessage, sizeMessage);
21+
BitStream.WriteStringCharacters(m_strMessage);
2322
return true;
2423
}
2524

‎Server/mods/deathmatch/logic/packets/CDebugEchoPacket.cpp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ bool CDebugEchoPacket::Write(NetBitStreamInterface& BitStream) const
2525
}
2626

2727
// Too short?
28-
size_t sizeMessage = m_strMessage.length();
29-
if (sizeMessage >= MIN_DEBUGECHO_LENGTH)
28+
if (m_strMessage.length() >= MIN_DEBUGECHO_LENGTH)
3029
{
3130
// Write the string
32-
BitStream.WriteStringCharacters(m_strMessage, sizeMessage);
31+
BitStream.WriteStringCharacters(m_strMessage);
3332
return true;
3433
}
3534

‎Server/mods/deathmatch/logic/packets/CLuaEventPacket.cpp‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,8 @@ bool CLuaEventPacket::Read(NetBitStreamInterface& BitStream)
4747

4848
bool CLuaEventPacket::Write(NetBitStreamInterface& BitStream) const
4949
{
50-
unsigned short usNameLength = static_cast<unsigned short>(m_strName.length());
51-
BitStream.WriteCompressed(usNameLength);
52-
BitStream.WriteStringCharacters(m_strName, usNameLength);
50+
BitStream.WriteCompressed(static_cast<unsigned short>(m_strName.length()));
51+
BitStream.WriteStringCharacters(m_strName);
5352
BitStream.Write(m_ElementID);
5453
m_pArguments->WriteToBitStream(BitStream);
5554

‎Server/mods/deathmatch/logic/packets/CPlayerChangeNickPacket.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ bool CPlayerChangeNickPacket::Write(NetBitStreamInterface& BitStream) const
2727
BitStream.Write(ID);
2828

2929
// Write the nick
30-
BitStream.WriteStringCharacters(m_strNewNick, m_strNewNick.length());
30+
BitStream.WriteStringCharacters(m_strNewNick);
3131
return true;
3232
}
3333

‎Shared/sdk/net/bitstream.h‎

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
#include <alloca.h>
2424
#endif
2525

26+
#ifdef __cpp_lib_string_view
27+
#include <string_view>
28+
#endif
29+
2630
struct ISyncStructure;
2731
class NetBitStreamInterface;
2832

@@ -196,15 +200,43 @@ class NetBitStreamInterfaceNoVersion : public CRefCountable
196200
// Return true if enough bytes left in the bitstream
197201
bool CanReadNumberOfBytes(int iLength) const { return iLength >= 0 && iLength <= (GetNumberOfUnreadBits() + 7) / 8; }
198202

199-
// Write characters from a std::string
200-
void WriteStringCharacters(const std::string& value, uint uiLength)
203+
// For whatever stupid reason sdk/net gets included in Multiplayer SA and Game SA
204+
// And since those projects are c++14 we need this stuff.
205+
// TODO: Rip sdk/net out of these projects...
206+
#ifdef __cpp_lib_string_view
207+
// Write characters in `value`
208+
void WriteStringCharacters(std::string_view value)
201209
{
202-
dassert(uiLength <= value.length());
203-
// Send the data
204-
if (uiLength)
205-
Write(&value.at(0), uiLength);
210+
if (!value.empty())
211+
Write(value.data(), (int)value.length());
212+
}
213+
// Write `n` characters from `value`
214+
void WriteStringCharacters(std::string_view value, size_t n)
215+
{
216+
dassert(n <= value.length());
217+
if (n)
218+
Write(value.data(), (int)n);
219+
}
220+
221+
// Write all characters in `value` (incl. length as `SizeType`)
222+
template<typename SizeType = unsigned short>
223+
void WriteString(std::string_view value)
224+
{
225+
// Write the length
226+
Write(static_cast<SizeType>(value.length()));
227+
228+
// Write the characters
229+
return WriteStringCharacters(value);
206230
}
207231

232+
// Write a string (incl. variable size header)
233+
void WriteStr(std::string_view value)
234+
{
235+
WriteLength(value.length());
236+
return WriteStringCharacters(value, value.length());
237+
}
238+
#endif
239+
208240
// Read characters into a std::string
209241
bool ReadStringCharacters(std::string& result, uint uiLength)
210242
{
@@ -224,18 +256,6 @@ class NetBitStreamInterfaceNoVersion : public CRefCountable
224256
return true;
225257
}
226258

227-
// Write a string (incl. ushort size header)
228-
template <typename SizeType = unsigned short>
229-
void WriteString(const std::string& value)
230-
{
231-
// Write the length
232-
auto length = static_cast<SizeType>(value.length());
233-
Write(length);
234-
235-
// Write the characters
236-
return WriteStringCharacters(value, length);
237-
}
238-
239259
// Read a string (incl. ushort size header)
240260
template <typename SizeType = unsigned short>
241261
bool ReadString(std::string& result)
@@ -296,13 +316,6 @@ class NetBitStreamInterfaceNoVersion : public CRefCountable
296316
return true;
297317
}
298318

299-
// Write a string (incl. variable size header)
300-
void WriteStr(const std::string& value)
301-
{
302-
WriteLength(value.length());
303-
return WriteStringCharacters(value, value.length());
304-
}
305-
306319
// Read a string (incl. variable size header)
307320
bool ReadStr(std::string& result)
308321
{

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /