- eliminated several pointless string copies in network code.

This commit is contained in:
Christoph Oelckers 2023-08-12 10:56:34 +02:00
parent 46473b45da
commit b695e845e1
2 changed files with 26 additions and 40 deletions

View file

@ -374,14 +374,13 @@ static void ShoveChatStr (const char *str, uint8_t who)
Net_WriteByte (DEM_SAY); Net_WriteByte (DEM_SAY);
Net_WriteByte (who); Net_WriteByte (who);
if (!chat_substitution || !DoSubstitution (substBuff, str)) if (chat_substitution && DoSubstitution (substBuff, str))
{ {
Net_WriteString(MakeUTF8(str)); str = substBuff.GetChars();
}
else
{
Net_WriteString(MakeUTF8(substBuff));
} }
Net_WriteString(CleanseString(const_cast<char*>(MakeUTF8(str))));
} }
//=========================================================================== //===========================================================================

View file

@ -1406,7 +1406,6 @@ struct ArbitrateData
bool DoArbitrate (void *userdata) bool DoArbitrate (void *userdata)
{ {
ArbitrateData *data = (ArbitrateData *)userdata; ArbitrateData *data = (ArbitrateData *)userdata;
char *s;
uint8_t *stream; uint8_t *stream;
int version; int version;
int node; int node;
@ -1468,9 +1467,7 @@ bool DoArbitrate (void *userdata)
NetMode = netbuffer[2]; NetMode = netbuffer[2];
stream = &netbuffer[3]; stream = &netbuffer[3];
s = ReadString (&stream); startmap = ReadStringConst(&stream);
startmap = s;
delete[] s;
rngseed = ReadLong (&stream); rngseed = ReadLong (&stream);
C_ReadCVars (&stream); C_ReadCVars (&stream);
} }
@ -2166,7 +2163,7 @@ static int RemoveClass(FLevelLocals *Level, const PClass *cls)
void Net_DoCommand (int type, uint8_t **stream, int player) void Net_DoCommand (int type, uint8_t **stream, int player)
{ {
uint8_t pos = 0; uint8_t pos = 0;
char *s = NULL; const char* s = nullptr;
int i; int i;
switch (type) switch (type)
@ -2176,8 +2173,7 @@ void Net_DoCommand (int type, uint8_t **stream, int player)
const char *name = players[player].userinfo.GetName(); const char *name = players[player].userinfo.GetName();
uint8_t who = ReadByte (stream); uint8_t who = ReadByte (stream);
s = ReadString (stream); s = ReadStringConst(stream);
CleanseString (s);
if (((who & 1) == 0) || players[player].userinfo.GetTeam() == TEAM_NONE) if (((who & 1) == 0) || players[player].userinfo.GetTeam() == TEAM_NONE)
{ // Said to everyone { // Said to everyone
if (who & 2) if (who & 2)
@ -2206,18 +2202,15 @@ void Net_DoCommand (int type, uint8_t **stream, int player)
break; break;
case DEM_MUSICCHANGE: case DEM_MUSICCHANGE:
s = ReadString (stream); S_ChangeMusic(ReadStringConst(stream));
S_ChangeMusic (s);
break; break;
case DEM_PRINT: case DEM_PRINT:
s = ReadString (stream); Printf("%s", ReadStringConst(stream));
Printf ("%s", s);
break; break;
case DEM_CENTERPRINT: case DEM_CENTERPRINT:
s = ReadString (stream); C_MidPrint(nullptr, ReadStringConst(stream));
C_MidPrint (nullptr, s);
break; break;
case DEM_UINFCHANGED: case DEM_UINFCHANGED:
@ -2233,7 +2226,7 @@ void Net_DoCommand (int type, uint8_t **stream, int player)
break; break;
case DEM_GIVECHEAT: case DEM_GIVECHEAT:
s = ReadString (stream); s = ReadStringConst(stream);
cht_Give (&players[player], s, ReadLong (stream)); cht_Give (&players[player], s, ReadLong (stream));
if (player != consoleplayer) if (player != consoleplayer)
{ {
@ -2245,12 +2238,12 @@ void Net_DoCommand (int type, uint8_t **stream, int player)
break; break;
case DEM_TAKECHEAT: case DEM_TAKECHEAT:
s = ReadString (stream); s = ReadStringConst(stream);
cht_Take (&players[player], s, ReadLong (stream)); cht_Take (&players[player], s, ReadLong (stream));
break; break;
case DEM_SETINV: case DEM_SETINV:
s = ReadString(stream); s = ReadStringConst(stream);
i = ReadLong(stream); i = ReadLong(stream);
cht_SetInv(&players[player], s, i, !!ReadByte(stream)); cht_SetInv(&players[player], s, i, !!ReadByte(stream));
break; break;
@ -2274,7 +2267,7 @@ void Net_DoCommand (int type, uint8_t **stream, int player)
/* intentional fall-through */ /* intentional fall-through */
case DEM_CHANGEMAP: case DEM_CHANGEMAP:
// Change to another map without disconnecting other players // Change to another map without disconnecting other players
s = ReadString (stream); s = ReadStringConst(stream);
// Using LEVEL_NOINTERMISSION tends to throw the game out of sync. // Using LEVEL_NOINTERMISSION tends to throw the game out of sync.
// That was a long time ago. Maybe it works now? // That was a long time ago. Maybe it works now?
primaryLevel->flags |= LEVEL_CHANGEMAPCHEAT; primaryLevel->flags |= LEVEL_CHANGEMAPCHEAT;
@ -2361,7 +2354,7 @@ void Net_DoCommand (int type, uint8_t **stream, int player)
uint8_t special = 0; uint8_t special = 0;
int args[5]; int args[5];
s = ReadString (stream); s = ReadStringConst(stream);
if (type >= DEM_SUMMON2 && type <= DEM_SUMMONFOE2) if (type >= DEM_SUMMON2 && type <= DEM_SUMMONFOE2)
{ {
angle = ReadWord(stream); angle = ReadWord(stream);
@ -2425,12 +2418,12 @@ void Net_DoCommand (int type, uint8_t **stream, int player)
break; break;
case DEM_SPRAY: case DEM_SPRAY:
s = ReadString(stream); s = ReadStringConst(stream);
SprayDecal(players[player].mo, s); SprayDecal(players[player].mo, s);
break; break;
case DEM_MDK: case DEM_MDK:
s = ReadString(stream); s = ReadStringConst(stream);
cht_DoMDK(&players[player], s); cht_DoMDK(&players[player], s);
break; break;
@ -2453,11 +2446,8 @@ void Net_DoCommand (int type, uint8_t **stream, int player)
case DEM_SAVEGAME: case DEM_SAVEGAME:
if (gamestate == GS_LEVEL) if (gamestate == GS_LEVEL)
{ {
s = ReadString (stream); savegamefile = ReadStringConst(stream);
savegamefile = s; savedescription = ReadStringConst(stream);
delete[] s;
s = ReadString (stream);
savedescription = s;
if (player != consoleplayer) if (player != consoleplayer)
{ {
// Paths sent over the network will be valid for the system that sent // Paths sent over the network will be valid for the system that sent
@ -2525,7 +2515,7 @@ void Net_DoCommand (int type, uint8_t **stream, int player)
case DEM_RUNNAMEDSCRIPT: case DEM_RUNNAMEDSCRIPT:
{ {
s = ReadString(stream); s = ReadStringConst(stream);
int argn = ReadByte(stream); int argn = ReadByte(stream);
RunScript(stream, players[player].mo, -FName(s).GetIndex(), argn & 127, (argn & 128) ? ACS_ALWAYS : 0); RunScript(stream, players[player].mo, -FName(s).GetIndex(), argn & 127, (argn & 128) ? ACS_ALWAYS : 0);
@ -2564,8 +2554,8 @@ void Net_DoCommand (int type, uint8_t **stream, int player)
case DEM_MORPHEX: case DEM_MORPHEX:
{ {
s = ReadString (stream); s = ReadStringConst(stream);
FString msg = cht_Morph (players + player, PClass::FindActor (s), false); FString msg = cht_Morph (players + player, PClass::FindActor(s), false);
if (player == consoleplayer) if (player == consoleplayer)
{ {
Printf ("%s\n", msg[0] != '\0' ? msg.GetChars() : "Morph failed."); Printf ("%s\n", msg[0] != '\0' ? msg.GetChars() : "Morph failed.");
@ -2595,7 +2585,7 @@ void Net_DoCommand (int type, uint8_t **stream, int player)
case DEM_KILLCLASSCHEAT: case DEM_KILLCLASSCHEAT:
{ {
s = ReadString (stream); s = ReadStringConst(stream);
int killcount = 0; int killcount = 0;
PClassActor *cls = PClass::FindActor(s); PClassActor *cls = PClass::FindActor(s);
@ -2618,7 +2608,7 @@ void Net_DoCommand (int type, uint8_t **stream, int player)
break; break;
case DEM_REMOVE: case DEM_REMOVE:
{ {
s = ReadString(stream); s = ReadStringConst(stream);
int removecount = 0; int removecount = 0;
PClassActor *cls = PClass::FindActor(s); PClassActor *cls = PClass::FindActor(s);
if (cls != NULL && cls->IsDescendantOf(RUNTIME_CLASS(AActor))) if (cls != NULL && cls->IsDescendantOf(RUNTIME_CLASS(AActor)))
@ -2702,7 +2692,7 @@ void Net_DoCommand (int type, uint8_t **stream, int player)
case DEM_NETEVENT: case DEM_NETEVENT:
{ {
s = ReadString(stream); s = ReadStringConst(stream);
int argn = ReadByte(stream); int argn = ReadByte(stream);
int arg[3] = { 0, 0, 0 }; int arg[3] = { 0, 0, 0 };
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
@ -2720,9 +2710,6 @@ void Net_DoCommand (int type, uint8_t **stream, int player)
I_Error ("Unknown net command: %d", type); I_Error ("Unknown net command: %d", type);
break; break;
} }
if (s)
delete[] s;
} }
// Used by DEM_RUNSCRIPT, DEM_RUNSCRIPT2, and DEM_RUNNAMEDSCRIPT // Used by DEM_RUNSCRIPT, DEM_RUNSCRIPT2, and DEM_RUNNAMEDSCRIPT