Skip to content

Commit

Permalink
Merge branch 'pr-2324'
Browse files Browse the repository at this point in the history
  • Loading branch information
nihonium committed Feb 1, 2024
2 parents cc84048 + 9020669 commit 88a0a6c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 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
36 changes: 26 additions & 10 deletions code/components/steam/src/SteamComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@
#include <sstream>
#include <thread>

#include <utf8.h>

struct CfxPresenceState
{
char gameName[512];
volatile bool needRefresh;
int childPid = 0;

CfxPresenceState()
: needRefresh(0)
{
memset(gameName, 0, sizeof(gameName));
}
Expand Down Expand Up @@ -389,22 +390,32 @@ void SteamComponent::InitializePresence()

if (GetFileAttributes(namePath.c_str()) != INVALID_FILE_ATTRIBUTES)
{
std::wifstream nameFile(namePath);
std::wstringstream nameStream;
std::ifstream nameFile(namePath);
std::stringstream nameStream;
nameStream << nameFile.rdbuf();
nameFile.close();

productName = ToNarrow(nameStream.str());
productName = nameStream.str();
}

static HostSharedData<CfxPresenceState> gameData("PresenceState");
gameData->needRefresh = false;

if (gameData->gameName[0])
{
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 Expand Up @@ -492,6 +503,11 @@ bool SteamComponent::RunPresenceDummy()

trace("game parent PID: %d\n", parentPid);

HostSharedData<CfxPresenceState> gameData("PresenceState");

auto currentPid = GetCurrentProcessId();
gameData->childPid = currentPid;

// open a handle to the parent process with SYNCHRONIZE rights
HANDLE processHandle = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, parentPid);

Expand All @@ -512,9 +528,7 @@ bool SteamComponent::RunPresenceDummy()
break;
}

static HostSharedData<CfxPresenceState> gameData("PresenceState");

if (gameData->needRefresh)
if (gameData->childPid != currentPid)
{
return true;
}
Expand Down Expand Up @@ -651,9 +665,11 @@ void SteamComponent::SetRichPresenceValue(int idx, const std::string& value)
if (updateGameName)
{
static HostSharedData<CfxPresenceState> gameData("PresenceState");
gameData->needRefresh = true;
strcpy_s(gameData->gameName, value.c_str());

// reset the child PID to make the current process exit early if it can
gameData->childPid = 0;

RunChildLauncher();
}
}
Expand Down

0 comments on commit 88a0a6c

Please sign in to comment.