forked from electronicarts/CnC_Generals_Zero_Hour
-
Notifications
You must be signed in to change notification settings - Fork 230
bugfix(network): Prevent LAN lobby hang with long player names #3039
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bobtista
wants to merge
2
commits into
TheSuperHackers:main
Choose a base branch
from
bobtista:bobtista/bugfix/lan-lobby-long-name-hang
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+217
−25
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,9 @@ | |
| #include "GameNetwork/LANAPI.h" // for testing packet size | ||
| #include "GameNetwork/LANAPICallbacks.h" // for testing packet size | ||
| #include "WWLib/strtok_r.h" | ||
| #include "WWMath/wwmath.h" | ||
| #include <algorithm> | ||
| #include <set> | ||
|
|
||
|
|
||
|
|
||
|
|
@@ -891,11 +894,157 @@ Bool GameInfo::isSandbox() | |
|
|
||
| static const char slotListID = 'S'; | ||
|
|
||
| AsciiString GameInfoToAsciiString( const GameInfo *game ) | ||
| struct PlayerNameTruncationInfo | ||
| { | ||
| if (!game) | ||
| return AsciiString::TheEmptyString; | ||
| Int TruncatableByteCount; | ||
| Int PlayerIndex; | ||
| friend bool operator<(const PlayerNameTruncationInfo& lhs, const PlayerNameTruncationInfo& rhs) | ||
| { | ||
| if (lhs.TruncatableByteCount == rhs.TruncatableByteCount) | ||
| { | ||
| return lhs.PlayerIndex < rhs.PlayerIndex; | ||
| } | ||
| return lhs.TruncatableByteCount < rhs.TruncatableByteCount; | ||
| } | ||
| }; | ||
|
|
||
| static void BuildPlayerNames(const GameInfo& game, AsciiString (&playerNames)[MAX_SLOTS]) | ||
| { | ||
| for (Int i = 0; i < MAX_SLOTS; ++i) | ||
| { | ||
| const GameSlot* slot = game.getConstSlot(i); | ||
| if (slot->isHuman()) | ||
| { | ||
| playerNames[i] = WideCharStringToMultiByte(slot->getName().str()).c_str(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static Bool IsUtf8ContinuationByte(Char c) | ||
| { | ||
| return (static_cast<unsigned char>(c) & 0xC0) == 0x80; | ||
| } | ||
|
|
||
| static Int GetFirstUtf8CharacterByteLength(const AsciiString& string) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be an Asciistring function. It does not belong in GameInfo |
||
| { | ||
| const Int stringLength = string.getLength(); | ||
| Int characterLength = std::min(1, stringLength); | ||
| while (characterLength < stringLength && IsUtf8ContinuationByte(string.getCharAt(characterLength))) | ||
| { | ||
| ++characterLength; | ||
| } | ||
| return characterLength; | ||
| } | ||
|
|
||
| static Bool TruncatePlayerNames(AsciiString (&playerNames)[MAX_SLOTS], Int truncateByteCount) | ||
| { | ||
| PlayerNameTruncationInfo truncationInfo[MAX_SLOTS]; | ||
| Int minimumNameLengths[MAX_SLOTS]; | ||
| Int remainingTruncatableByteCount = 0; | ||
|
|
||
| // Build truncatable byte count and player index pairs for the player names. | ||
| for (Int playerIndex = 0; playerIndex < MAX_SLOTS; ++playerIndex) | ||
| { | ||
| minimumNameLengths[playerIndex] = GetFirstUtf8CharacterByteLength(playerNames[playerIndex]); | ||
| truncationInfo[playerIndex].TruncatableByteCount = | ||
| playerNames[playerIndex].getLength() - minimumNameLengths[playerIndex]; | ||
| truncationInfo[playerIndex].PlayerIndex = playerIndex; | ||
| remainingTruncatableByteCount += truncationInfo[playerIndex].TruncatableByteCount; | ||
| } | ||
|
|
||
| if (truncateByteCount > remainingTruncatableByteCount) | ||
| { | ||
| DEBUG_LOG(("TruncatePlayerNames - Requested to truncate %d bytes from player names, but only %d were available for truncation.", | ||
| truncateByteCount, remainingTruncatableByteCount)); | ||
| return false; | ||
| } | ||
|
|
||
| // Sort based on length in ascending order. | ||
| std::sort(truncationInfo, truncationInfo + MAX_SLOTS); | ||
|
|
||
| for (Int i = 0; i < MAX_SLOTS; ++i) | ||
| { | ||
| const Int playerIndex = truncationInfo[i].PlayerIndex; | ||
| const Int playerTruncatableByteCount = truncationInfo[i].TruncatableByteCount; | ||
|
|
||
| // Round the average up, so the final entry (the longest name) accounts for the rounding. | ||
| const Int averageTruncatableByteCount = | ||
| WWMath::Div_Ceil(remainingTruncatableByteCount - truncateByteCount, MAX_SLOTS - i); | ||
| remainingTruncatableByteCount -= playerTruncatableByteCount; | ||
| if (playerTruncatableByteCount <= averageTruncatableByteCount) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| Int playerTruncateByteCount = playerTruncatableByteCount - averageTruncatableByteCount; | ||
| if (i == MAX_SLOTS - 1) | ||
| { | ||
| // ensure we account for rounding errors when truncating the last, longest entry | ||
| playerTruncateByteCount = std::max(truncateByteCount, playerTruncateByteCount); | ||
| } | ||
|
|
||
| // As the name is UTF-8, make sure we don't truncate part of a multibyte character. | ||
| Int truncatedLength = playerNames[playerIndex].getLength() - playerTruncateByteCount; | ||
| while (truncatedLength > minimumNameLengths[playerIndex] | ||
| && IsUtf8ContinuationByte(playerNames[playerIndex].getCharAt(truncatedLength))) | ||
| { | ||
| // Move back to the start of the multibyte character. | ||
| ++playerTruncateByteCount; | ||
| --truncatedLength; | ||
| } | ||
|
|
||
| playerNames[playerIndex].truncateBy(playerTruncateByteCount); | ||
| truncateByteCount -= playerTruncateByteCount; | ||
| if (truncateByteCount <= 0) | ||
| { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| static void ReplaceLastUtf8Character(AsciiString& string, Char replacement) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should not be in GameInfo |
||
| { | ||
| Int characterStart = string.getLength() - 1; | ||
| while (characterStart > 0 && IsUtf8ContinuationByte(string.getCharAt(characterStart))) | ||
| { | ||
| --characterStart; | ||
| } | ||
| string.truncateTo(characterStart); | ||
| string.concat(replacement); | ||
| } | ||
|
|
||
| static void MakeUniquePlayerNames(AsciiString (&playerNames)[MAX_SLOTS]) | ||
| { | ||
| static_assert(MAX_SLOTS < 10, "Name collision avoidance assumes less than 10 players in the game."); | ||
|
|
||
| // ensure there are no duplicates in the list of player names | ||
| std::set<AsciiString> uniqueNames; | ||
| for (Int i = 0; i < MAX_SLOTS; ++i) | ||
| { | ||
| AsciiString& playerName = playerNames[i]; | ||
|
|
||
| if (playerName.isEmpty()) | ||
| { | ||
| continue; | ||
| } | ||
|
|
||
| Int charOffset = -1; | ||
| while (uniqueNames.insert(playerName).second == false) | ||
| { | ||
| // The name already exists, so change the last char to the number index of the player in the game. | ||
| // If that fails to be unique, iterate through 0-9 and change the last char to ensure differentiation. | ||
| // Guaranteed to find a unique name as the number of slots is less than 10. | ||
| char charToTry = '0' + static_cast<char>(charOffset == -1 ? i : charOffset); | ||
| ReplaceLastUtf8Character(playerName, charToTry); | ||
| ++charOffset; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static AsciiString GameInfoToAsciiString(const GameInfo *game, const AsciiString (&playerNames)[MAX_SLOTS]) | ||
| { | ||
| AsciiString mapName = game->getMap(); | ||
| mapName = TheGameState->realMapPathToPortableMapPath(mapName); | ||
| AsciiString newMapName; | ||
|
|
@@ -940,23 +1089,17 @@ AsciiString GameInfoToAsciiString( const GameInfo *game ) | |
| AsciiString str; | ||
| if (slot && slot->isHuman()) | ||
| { | ||
| AsciiString tmp; //all this data goes after name | ||
| tmp.format( ",%X,%d,%c%c,%d,%d,%d,%d,%d:", | ||
| slot->getIP(), slot->getPort(), | ||
| (slot->isAccepted()?'T':'F'), | ||
| (slot->hasMap()?'T':'F'), | ||
| slot->getColor(), slot->getPlayerTemplate(), | ||
| slot->getStartPos(), slot->getTeamNumber(), | ||
| slot->getNATBehavior() ); | ||
| //make sure name doesn't cause overflow of m_lanMaxOptionsLength | ||
| int lenCur = tmp.getLength() + optionsString.getLength() + 2; //+2 for H and trailing ; | ||
| int lenRem = m_lanMaxOptionsLength - lenCur; //length remaining before overflowing | ||
| int lenMax = lenRem / (MAX_SLOTS-i); //share lenRem with all remaining slots | ||
| AsciiString name = WideCharStringToMultiByte(slot->getName().str()).c_str(); | ||
| while( name.getLength() > lenMax ) | ||
| name.removeLastChar(); //what a horrible way to truncate. I hate AsciiString. | ||
|
|
||
| str.format( "H%s%s", name.str(), tmp.str() ); | ||
| str.format( "H%s,%X,%d,%c%c,%d,%d,%d,%d,%d:", | ||
| playerNames[i].str(), | ||
| slot->getIP(), | ||
| slot->getPort(), | ||
| slot->isAccepted() ? 'T' : 'F', | ||
| slot->hasMap() ? 'T' : 'F', | ||
| slot->getColor(), | ||
| slot->getPlayerTemplate(), | ||
| slot->getStartPos(), | ||
| slot->getTeamNumber(), | ||
| slot->getNATBehavior()); | ||
| } | ||
| else if (slot && slot->isAI()) | ||
| { | ||
|
|
@@ -988,13 +1131,40 @@ AsciiString GameInfoToAsciiString( const GameInfo *game ) | |
| } | ||
| optionsString.concat(';'); | ||
|
|
||
| DEBUG_ASSERTCRASH(!TheLAN || (optionsString.getLength() < m_lanMaxOptionsLength), | ||
| ("WARNING: options string is longer than expected! Length is %d, but max is %d!", | ||
| optionsString.getLength(), m_lanMaxOptionsLength)); | ||
|
|
||
| return optionsString; | ||
| } | ||
|
|
||
| AsciiString GameInfoToAsciiString(const GameInfo* game) | ||
| { | ||
| if (!game) | ||
| { | ||
| return AsciiString::TheEmptyString; | ||
| } | ||
|
|
||
| AsciiString playerNames[MAX_SLOTS]; | ||
| BuildPlayerNames(*game, playerNames); | ||
| AsciiString infoString = GameInfoToAsciiString(game, playerNames); | ||
|
|
||
| // TheSuperHackers @bugfix Safely truncate the game info string by | ||
| // stripping characters off of player names if the overall length is too large. | ||
| if (TheLAN && (infoString.getLength() > m_lanMaxOptionsLength)) | ||
| { | ||
| const Int truncateByteCount = infoString.getLength() - m_lanMaxOptionsLength; | ||
| if (!TruncatePlayerNames(playerNames, truncateByteCount)) | ||
| { | ||
| DEBUG_CRASH(("WARNING: options string is longer than expected! Length is %d, but max is %d. " | ||
| "Attempted to truncate player names by %d bytes, but was unsuccessful!", | ||
| infoString.getLength(), m_lanMaxOptionsLength, truncateByteCount)); | ||
| return AsciiString::TheEmptyString; | ||
| } | ||
|
|
||
| MakeUniquePlayerNames(playerNames); | ||
| infoString = GameInfoToAsciiString(game, playerNames); | ||
| } | ||
|
|
||
| return infoString; | ||
| } | ||
|
|
||
| static Int grabHexInt(const char *s) | ||
| { | ||
| char tmp[5] = "0xff"; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be an Asciistring function. It does not belong in GameInfo