System: Load discord-rpc dynamically

This commit is contained in:
Stenzek 2024-07-04 14:40:16 +10:00
parent 00d2d862c2
commit f6d3a7987d
No known key found for this signature in database
8 changed files with 87 additions and 32 deletions

View File

@ -56,6 +56,7 @@ APPDIRNAME=DuckStation.AppDir
STRIP=strip STRIP=strip
declare -a MANUAL_LIBS=( declare -a MANUAL_LIBS=(
"libdiscord-rpc.so"
"libshaderc_shared.so" "libshaderc_shared.so"
"libspirv-cross-c-shared.so" "libspirv-cross-c-shared.so"
) )

View File

@ -182,11 +182,6 @@ if(CPU_ARCH_RISCV64)
message(STATUS "Building RISC-V 64-bit recompiler.") message(STATUS "Building RISC-V 64-bit recompiler.")
endif() 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. # Copy the provided data directory to the output directory. Borrowed from PCSX2.
function(add_resources target path basedir) function(add_resources target path basedir)
get_filename_component(dir ${path} DIRECTORY) get_filename_component(dir ${path} DIRECTORY)

View File

@ -801,11 +801,9 @@ void Achievements::UpdateRichPresence(std::unique_lock<std::recursive_mutex>& lo
INFO_LOG("Rich presence updated: {}", s_rich_presence_string); INFO_LOG("Rich presence updated: {}", s_rich_presence_string);
Host::OnAchievementsRefreshed(); Host::OnAchievementsRefreshed();
#ifdef ENABLE_DISCORD_PRESENCE
lock.unlock(); lock.unlock();
System::UpdateDiscordPresence(false); System::UpdateRichPresence(false);
lock.lock(); lock.lock();
#endif
} }
void Achievements::GameChanged(const std::string& path, CDImage* image) void Achievements::GameChanged(const std::string& path, CDImage* image)

View File

@ -4,7 +4,6 @@
<ItemDefinitionGroup> <ItemDefinitionGroup>
<ClCompile> <ClCompile>
<PreprocessorDefinitions>ENABLE_DISCORD_PRESENCE=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="('$(Platform)'!='ARM64')">ENABLE_RAINTEGRATION=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)'=='ARM' Or '$(Platform)'=='ARM64')">ENABLE_RECOMPILER=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions Condition="('$(Platform)'=='x64' Or '$(Platform)'=='ARM64')">ENABLE_MMAP_FASTMEM=1;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions Condition="('$(Platform)'=='x64' Or '$(Platform)'=='ARM64')">ENABLE_MMAP_FASTMEM=1;%(PreprocessorDefinitions)</PreprocessorDefinitions>

View File

@ -3148,12 +3148,10 @@ void FullscreenUI::DrawInterfaceSettingsPage()
} }
} }
#ifdef ENABLE_DISCORD_PRESENCE
MenuHeading(FSUI_CSTR("Integration")); MenuHeading(FSUI_CSTR("Integration"));
DrawToggleSetting(bsi, FSUI_ICONSTR(ICON_FA_CHARGING_STATION, "Enable Discord Presence"), 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", FSUI_CSTR("Shows the game you are currently playing as part of your profile in Discord."), "Main",
"EnableDiscordPresence", false); "EnableDiscordPresence", false);
#endif
MenuHeading(FSUI_CSTR("On-Screen Display")); MenuHeading(FSUI_CSTR("On-Screen Display"));
DrawIntSpinBoxSetting(bsi, FSUI_ICONSTR(ICON_FA_SEARCH, "OSD Scale"), DrawIntSpinBoxSetting(bsi, FSUI_ICONSTR(ICON_FA_SEARCH, "OSD Scale"),

View File

@ -47,6 +47,7 @@
#include "util/state_wrapper.h" #include "util/state_wrapper.h"
#include "common/align.h" #include "common/align.h"
#include "common/dynamic_library.h"
#include "common/error.h" #include "common/error.h"
#include "common/file_system.h" #include "common/file_system.h"
#include "common/log.h" #include "common/log.h"
@ -77,11 +78,8 @@ Log_SetChannel(System);
#include <objbase.h> #include <objbase.h>
#endif #endif
#ifdef ENABLE_DISCORD_PRESENCE
#include "discord_rpc.h"
#endif
#ifndef __ANDROID__ #ifndef __ANDROID__
#define ENABLE_DISCORD_PRESENCE 1
#define ENABLE_PINE_SERVER 1 #define ENABLE_PINE_SERVER 1
#define ENABLE_GDB_SERVER 1 #define ENABLE_GDB_SERVER 1
#define ENABLE_SOCKET_MULTIPLEXER 1 #define ENABLE_SOCKET_MULTIPLEXER 1
@ -1957,9 +1955,7 @@ void System::ClearRunningGame()
Achievements::GameChanged(s_running_game_path, nullptr); Achievements::GameChanged(s_running_game_path, nullptr);
#ifdef ENABLE_DISCORD_PRESENCE UpdateRichPresence(true);
UpdateDiscordPresence(true);
#endif
} }
void System::Execute() void System::Execute()
@ -3791,9 +3787,7 @@ void System::UpdateRunningGame(const char* path, CDImage* image, bool booting)
else else
SaveStateSelectorUI::ClearList(); SaveStateSelectorUI::ClearList();
#ifdef ENABLE_DISCORD_PRESENCE UpdateRichPresence(booting);
UpdateDiscordPresence(booting);
#endif
Host::OnGameChanged(s_running_game_path, s_running_game_serial, s_running_game_title); Host::OnGameChanged(s_running_game_path, s_running_game_serial, s_running_game_title);
} }
@ -5467,16 +5461,80 @@ void System::ReleaseSocketMultiplexer()
#ifdef ENABLE_DISCORD_PRESENCE #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() void System::InitializeDiscordPresence()
{ {
if (s_discord_presence_active) if (s_discord_presence_active)
return; return;
Error error;
if (!dyn_libs::OpenDiscordRPC(&error))
{
ERROR_LOG("Failed to open discord-rpc: {}", error.GetDescription());
return;
}
DiscordEventHandlers handlers = {}; DiscordEventHandlers handlers = {};
Discord_Initialize("705325712680288296", &handlers, 0, nullptr); dyn_libs::Discord_Initialize("705325712680288296", &handlers, 0, nullptr);
s_discord_presence_active = true; s_discord_presence_active = true;
UpdateDiscordPresence(true); UpdateRichPresence(true);
} }
void System::ShutdownDiscordPresence() void System::ShutdownDiscordPresence()
@ -5484,12 +5542,14 @@ void System::ShutdownDiscordPresence()
if (!s_discord_presence_active) if (!s_discord_presence_active)
return; return;
Discord_ClearPresence(); dyn_libs::Discord_ClearPresence();
Discord_Shutdown(); dyn_libs::Discord_Shutdown();
dyn_libs::CloseDiscordRPC();
s_discord_presence_active = false; s_discord_presence_active = false;
} }
void System::UpdateDiscordPresence(bool update_session_time) void System::UpdateRichPresence(bool update_session_time)
{ {
if (!s_discord_presence_active) if (!s_discord_presence_active)
return; return;
@ -5525,7 +5585,7 @@ void System::UpdateDiscordPresence(bool update_session_time)
rp.state = state_string.c_str(); rp.state = state_string.c_str();
} }
Discord_UpdatePresence(&rp); dyn_libs::Discord_UpdatePresence(&rp);
} }
void System::PollDiscordPresence() void System::PollDiscordPresence()
@ -5533,7 +5593,13 @@ void System::PollDiscordPresence()
if (!s_discord_presence_active) if (!s_discord_presence_active)
return; return;
Discord_RunCallbacks(); dyn_libs::Discord_RunCallbacks();
}
#else
void System::UpdateRichPresence(bool update_session_time)
{
} }
#endif #endif

View File

@ -495,10 +495,8 @@ void SetRunaheadReplayFlag();
SocketMultiplexer* GetSocketMultiplexer(); SocketMultiplexer* GetSocketMultiplexer();
void ReleaseSocketMultiplexer(); void ReleaseSocketMultiplexer();
#ifdef ENABLE_DISCORD_PRESENCE
/// Called when rich presence changes. /// Called when rich presence changes.
void UpdateDiscordPresence(bool update_session_time); void UpdateRichPresence(bool update_session_time);
#endif
namespace Internal { namespace Internal {
/// Performs mandatory hardware checks. /// Performs mandatory hardware checks.

View File

@ -27,7 +27,7 @@
<AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(DepsIncludeDir)SDL2</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>%(AdditionalIncludeDirectories);$(DepsIncludeDir)SDL2</AdditionalIncludeDirectories>
</ClCompile> </ClCompile>
<Link> <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> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup> <ItemGroup>