Skip to content

Commit

Permalink
fix(steam): truncate presence to a valid UTF-8 sequence
Browse files Browse the repository at this point in the history
With a host name such as "Êîðïîðàöèÿ Ìàéêðîñîôò; Âñå ïðàâà çàùèùåíû", we
will try to set a presence name like "test: Êîðïîðàöèÿ Ìàéêðîñîôò; Âñå
ïðàâà çàùèùåíû", assuming `steamname.txt` contains "test".

Steam, internally, truncates app names to fit in a 64-byte buffer with a
null terminator. However, the code which truncates does not ensure the
resulting UTF-8 sequence is valid, which might lead to the update being
rejected further down the line, which is especially likely with a name
consisting solely of multi-byte sequences, like the example name.

In this changeset, we circumvent this by truncating the name to 64 bytes
ourselves, and subsequently chopping off at the first invalid UTF-8
sequence.
  • Loading branch information
blattersturm committed Dec 23, 2023
1 parent 3683eaf commit 9020669
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
3 changes: 3 additions & 0 deletions code/components/steam/component.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
return function()
add_dependencies { "vendor:utfcpp" }
end
13 changes: 13 additions & 0 deletions code/components/steam/src/SteamComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <sstream>
#include <thread>

#include <utf8.h>

struct CfxPresenceState
{
char gameName[512];
Expand Down Expand Up @@ -403,6 +405,17 @@ void SteamComponent::InitializePresence()
productName += fmt::sprintf(": %s", gameData->gameName);
}

// Steam requires the name to fit in a 64-byte buffer, so we try to make sure there's no unfinished UTF-8 sequences in that case
if (productName.length() >= 64)
{
productName = productName.substr(0, 63);

if (auto invalidPos = utf8::find_invalid(productName); invalidPos != std::string::npos)
{
productName = productName.substr(0, invalidPos);
}
}

// set our pipe appid
InterfaceMapper steamUtils(m_clientEngine->GetIClientUtils(m_steamPipe, "CLIENTUTILS_INTERFACE_VERSION001"));

Expand Down

0 comments on commit 9020669

Please sign in to comment.