Skip to content

Commit

Permalink
System: Load discord-rpc dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Jul 4, 2024
1 parent 00d2d86 commit f6d3a79
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 32 deletions.
1 change: 1 addition & 0 deletions scripts/appimage/make-appimage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ APPDIRNAME=DuckStation.AppDir
STRIP=strip

declare -a MANUAL_LIBS=(
"libdiscord-rpc.so"
"libshaderc_shared.so"
"libspirv-cross-c-shared.so"
)
Expand Down
5 changes: 0 additions & 5 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,6 @@ if(CPU_ARCH_RISCV64)
message(STATUS "Building RISC-V 64-bit recompiler.")
endif()

if(NOT ANDROID)
target_compile_definitions(core PUBLIC -DENABLE_DISCORD_PRESENCE=1)
target_link_libraries(core PRIVATE DiscordRPC::discord-rpc)
endif()

# Copy the provided data directory to the output directory. Borrowed from PCSX2.
function(add_resources target path basedir)
get_filename_component(dir ${path} DIRECTORY)
Expand Down
4 changes: 1 addition & 3 deletions src/core/achievements.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,11 +801,9 @@ void Achievements::UpdateRichPresence(std::unique_lock<std::recursive_mutex>& lo
INFO_LOG("Rich presence updated: {}", s_rich_presence_string);
Host::OnAchievementsRefreshed();

#ifdef ENABLE_DISCORD_PRESENCE
lock.unlock();
System::UpdateDiscordPresence(false);
System::UpdateRichPresence(false);
lock.lock();
#endif
}

void Achievements::GameChanged(const std::string& path, CDImage* image)
Expand Down
1 change: 0 additions & 1 deletion src/core/core.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>ENABLE_DISCORD_PRESENCE=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="('$(Platform)'!='ARM64')">ENABLE_RAINTEGRATION=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="('$(Platform)'=='x64' Or '$(Platform)'=='ARM' Or '$(Platform)'=='ARM64')">ENABLE_RECOMPILER=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="('$(Platform)'=='x64' Or '$(Platform)'=='ARM64')">ENABLE_MMAP_FASTMEM=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
Expand Down
2 changes: 0 additions & 2 deletions src/core/fullscreen_ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3148,12 +3148,10 @@ void FullscreenUI::DrawInterfaceSettingsPage()
}
}

#ifdef ENABLE_DISCORD_PRESENCE
MenuHeading(FSUI_CSTR("Integration"));
DrawToggleSetting(bsi, FSUI_ICONSTR(ICON_FA_CHARGING_STATION, "Enable Discord Presence"),
FSUI_CSTR("Shows the game you are currently playing as part of your profile in Discord."), "Main",
"EnableDiscordPresence", false);
#endif

MenuHeading(FSUI_CSTR("On-Screen Display"));
DrawIntSpinBoxSetting(bsi, FSUI_ICONSTR(ICON_FA_SEARCH, "OSD Scale"),
Expand Down
100 changes: 83 additions & 17 deletions src/core/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "util/state_wrapper.h"

#include "common/align.h"
#include "common/dynamic_library.h"
#include "common/error.h"
#include "common/file_system.h"
#include "common/log.h"
Expand Down Expand Up @@ -77,11 +78,8 @@ Log_SetChannel(System);
#include <objbase.h>
#endif

#ifdef ENABLE_DISCORD_PRESENCE
#include "discord_rpc.h"
#endif

#ifndef __ANDROID__
#define ENABLE_DISCORD_PRESENCE 1
#define ENABLE_PINE_SERVER 1
#define ENABLE_GDB_SERVER 1
#define ENABLE_SOCKET_MULTIPLEXER 1
Expand Down Expand Up @@ -1957,9 +1955,7 @@ void System::ClearRunningGame()

Achievements::GameChanged(s_running_game_path, nullptr);

#ifdef ENABLE_DISCORD_PRESENCE
UpdateDiscordPresence(true);
#endif
UpdateRichPresence(true);
}

void System::Execute()
Expand Down Expand Up @@ -3791,9 +3787,7 @@ void System::UpdateRunningGame(const char* path, CDImage* image, bool booting)
else
SaveStateSelectorUI::ClearList();

#ifdef ENABLE_DISCORD_PRESENCE
UpdateDiscordPresence(booting);
#endif
UpdateRichPresence(booting);

Host::OnGameChanged(s_running_game_path, s_running_game_serial, s_running_game_title);
}
Expand Down Expand Up @@ -5467,29 +5461,95 @@ void System::ReleaseSocketMultiplexer()

#ifdef ENABLE_DISCORD_PRESENCE

#include "discord_rpc.h"

#define DISCORD_RPC_FUNCTIONS(X) \
X(Discord_Initialize) \
X(Discord_Shutdown) \
X(Discord_RunCallbacks) \
X(Discord_UpdatePresence) \
X(Discord_ClearPresence)

namespace dyn_libs {
static bool OpenDiscordRPC(Error* error);
static void CloseDiscordRPC();

static DynamicLibrary s_discord_rpc_library;

#define ADD_FUNC(F) static decltype(&::F) F;
DISCORD_RPC_FUNCTIONS(ADD_FUNC)
#undef ADD_FUNC
} // namespace dyn_libs

bool dyn_libs::OpenDiscordRPC(Error* error)
{
if (s_discord_rpc_library.IsOpen())
return true;

const std::string libname = DynamicLibrary::GetVersionedFilename("discord-rpc");
if (!s_discord_rpc_library.Open(libname.c_str(), error))
{
Error::AddPrefix(error, "Failed to load discord-rpc: ");
return false;
}

#define LOAD_FUNC(F) \
if (!s_discord_rpc_library.GetSymbol(#F, &F)) \
{ \
Error::SetStringFmt(error, "Failed to find function {}", #F); \
CloseDiscordRPC(); \
return false; \
}
DISCORD_RPC_FUNCTIONS(LOAD_FUNC)
#undef LOAD_FUNC

return true;
}

void dyn_libs::CloseDiscordRPC()
{
if (!s_discord_rpc_library.IsOpen())
return;

#define UNLOAD_FUNC(F) F = nullptr;
DISCORD_RPC_FUNCTIONS(UNLOAD_FUNC)
#undef UNLOAD_FUNC

s_discord_rpc_library.Close();
}

void System::InitializeDiscordPresence()
{
if (s_discord_presence_active)
return;

Error error;
if (!dyn_libs::OpenDiscordRPC(&error))
{
ERROR_LOG("Failed to open discord-rpc: {}", error.GetDescription());
return;
}

DiscordEventHandlers handlers = {};
Discord_Initialize("705325712680288296", &handlers, 0, nullptr);
dyn_libs::Discord_Initialize("705325712680288296", &handlers, 0, nullptr);
s_discord_presence_active = true;

UpdateDiscordPresence(true);
UpdateRichPresence(true);
}

void System::ShutdownDiscordPresence()
{
if (!s_discord_presence_active)
return;

Discord_ClearPresence();
Discord_Shutdown();
dyn_libs::Discord_ClearPresence();
dyn_libs::Discord_Shutdown();
dyn_libs::CloseDiscordRPC();

s_discord_presence_active = false;
}

void System::UpdateDiscordPresence(bool update_session_time)
void System::UpdateRichPresence(bool update_session_time)
{
if (!s_discord_presence_active)
return;
Expand Down Expand Up @@ -5525,15 +5585,21 @@ void System::UpdateDiscordPresence(bool update_session_time)
rp.state = state_string.c_str();
}

Discord_UpdatePresence(&rp);
dyn_libs::Discord_UpdatePresence(&rp);
}

void System::PollDiscordPresence()
{
if (!s_discord_presence_active)
return;

Discord_RunCallbacks();
dyn_libs::Discord_RunCallbacks();
}

#else

void System::UpdateRichPresence(bool update_session_time)
{
}

#endif
4 changes: 1 addition & 3 deletions src/core/system.h
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,8 @@ void SetRunaheadReplayFlag();
SocketMultiplexer* GetSocketMultiplexer();
void ReleaseSocketMultiplexer();

#ifdef ENABLE_DISCORD_PRESENCE
/// Called when rich presence changes.
void UpdateDiscordPresence(bool update_session_time);
#endif
void UpdateRichPresence(bool update_session_time);

namespace Internal {
/// Performs mandatory hardware checks.
Expand Down
2 changes: 1 addition & 1 deletion src/util/util.props
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(DepsIncludeDir)SDL2</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<AdditionalDependencies>%(AdditionalDependencies);cpuinfo.lib;discord-rpc.lib;freetype.lib;libjpeg.lib;libpng16.lib;libwebp.lib;SDL2.lib;zlib.lib;zstd.lib</AdditionalDependencies>
<AdditionalDependencies>%(AdditionalDependencies);cpuinfo.lib;freetype.lib;libjpeg.lib;libpng16.lib;libwebp.lib;SDL2.lib;zlib.lib;zstd.lib</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down

0 comments on commit f6d3a79

Please sign in to comment.