From b695e845e1791de3e07a5b6b48512274115a8ab1 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 12 Aug 2023 10:56:34 +0200 Subject: [PATCH] - eliminated several pointless string copies in network code. --- src/ct_chat.cpp | 11 +++++----- src/d_net.cpp | 55 +++++++++++++++++++------------------------------ 2 files changed, 26 insertions(+), 40 deletions(-) diff --git a/src/ct_chat.cpp b/src/ct_chat.cpp index 5f16a872a..1ced34702 100644 --- a/src/ct_chat.cpp +++ b/src/ct_chat.cpp @@ -374,14 +374,13 @@ static void ShoveChatStr (const char *str, uint8_t who) Net_WriteByte (DEM_SAY); Net_WriteByte (who); - if (!chat_substitution || !DoSubstitution (substBuff, str)) + if (chat_substitution && DoSubstitution (substBuff, str)) { - Net_WriteString(MakeUTF8(str)); - } - else - { - Net_WriteString(MakeUTF8(substBuff)); + str = substBuff.GetChars(); } + + Net_WriteString(CleanseString(const_cast(MakeUTF8(str)))); + } //=========================================================================== diff --git a/src/d_net.cpp b/src/d_net.cpp index 80f43e1a2..4f86f0da4 100644 --- a/src/d_net.cpp +++ b/src/d_net.cpp @@ -1406,7 +1406,6 @@ struct ArbitrateData bool DoArbitrate (void *userdata) { ArbitrateData *data = (ArbitrateData *)userdata; - char *s; uint8_t *stream; int version; int node; @@ -1468,9 +1467,7 @@ bool DoArbitrate (void *userdata) NetMode = netbuffer[2]; stream = &netbuffer[3]; - s = ReadString (&stream); - startmap = s; - delete[] s; + startmap = ReadStringConst(&stream); rngseed = ReadLong (&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) { uint8_t pos = 0; - char *s = NULL; + const char* s = nullptr; int i; switch (type) @@ -2176,8 +2173,7 @@ void Net_DoCommand (int type, uint8_t **stream, int player) const char *name = players[player].userinfo.GetName(); uint8_t who = ReadByte (stream); - s = ReadString (stream); - CleanseString (s); + s = ReadStringConst(stream); if (((who & 1) == 0) || players[player].userinfo.GetTeam() == TEAM_NONE) { // Said to everyone if (who & 2) @@ -2206,18 +2202,15 @@ void Net_DoCommand (int type, uint8_t **stream, int player) break; case DEM_MUSICCHANGE: - s = ReadString (stream); - S_ChangeMusic (s); + S_ChangeMusic(ReadStringConst(stream)); break; case DEM_PRINT: - s = ReadString (stream); - Printf ("%s", s); + Printf("%s", ReadStringConst(stream)); break; case DEM_CENTERPRINT: - s = ReadString (stream); - C_MidPrint (nullptr, s); + C_MidPrint(nullptr, ReadStringConst(stream)); break; case DEM_UINFCHANGED: @@ -2233,7 +2226,7 @@ void Net_DoCommand (int type, uint8_t **stream, int player) break; case DEM_GIVECHEAT: - s = ReadString (stream); + s = ReadStringConst(stream); cht_Give (&players[player], s, ReadLong (stream)); if (player != consoleplayer) { @@ -2245,12 +2238,12 @@ void Net_DoCommand (int type, uint8_t **stream, int player) break; case DEM_TAKECHEAT: - s = ReadString (stream); + s = ReadStringConst(stream); cht_Take (&players[player], s, ReadLong (stream)); break; case DEM_SETINV: - s = ReadString(stream); + s = ReadStringConst(stream); i = ReadLong(stream); cht_SetInv(&players[player], s, i, !!ReadByte(stream)); break; @@ -2274,7 +2267,7 @@ void Net_DoCommand (int type, uint8_t **stream, int player) /* intentional fall-through */ case DEM_CHANGEMAP: // 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. // That was a long time ago. Maybe it works now? primaryLevel->flags |= LEVEL_CHANGEMAPCHEAT; @@ -2361,7 +2354,7 @@ void Net_DoCommand (int type, uint8_t **stream, int player) uint8_t special = 0; int args[5]; - s = ReadString (stream); + s = ReadStringConst(stream); if (type >= DEM_SUMMON2 && type <= DEM_SUMMONFOE2) { angle = ReadWord(stream); @@ -2425,12 +2418,12 @@ void Net_DoCommand (int type, uint8_t **stream, int player) break; case DEM_SPRAY: - s = ReadString(stream); + s = ReadStringConst(stream); SprayDecal(players[player].mo, s); break; case DEM_MDK: - s = ReadString(stream); + s = ReadStringConst(stream); cht_DoMDK(&players[player], s); break; @@ -2453,11 +2446,8 @@ void Net_DoCommand (int type, uint8_t **stream, int player) case DEM_SAVEGAME: if (gamestate == GS_LEVEL) { - s = ReadString (stream); - savegamefile = s; - delete[] s; - s = ReadString (stream); - savedescription = s; + savegamefile = ReadStringConst(stream); + savedescription = ReadStringConst(stream); if (player != consoleplayer) { // 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: { - s = ReadString(stream); + s = ReadStringConst(stream); int argn = ReadByte(stream); 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: { - s = ReadString (stream); - FString msg = cht_Morph (players + player, PClass::FindActor (s), false); + s = ReadStringConst(stream); + FString msg = cht_Morph (players + player, PClass::FindActor(s), false); if (player == consoleplayer) { 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: { - s = ReadString (stream); + s = ReadStringConst(stream); int killcount = 0; PClassActor *cls = PClass::FindActor(s); @@ -2618,7 +2608,7 @@ void Net_DoCommand (int type, uint8_t **stream, int player) break; case DEM_REMOVE: { - s = ReadString(stream); + s = ReadStringConst(stream); int removecount = 0; PClassActor *cls = PClass::FindActor(s); 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: { - s = ReadString(stream); + s = ReadStringConst(stream); int argn = ReadByte(stream); int arg[3] = { 0, 0, 0 }; 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); break; } - - if (s) - delete[] s; } // Used by DEM_RUNSCRIPT, DEM_RUNSCRIPT2, and DEM_RUNNAMEDSCRIPT