summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/yuzu/CMakeLists.txt2
-rw-r--r--src/yuzu/discord_impl.cpp67
2 files changed, 61 insertions, 8 deletions
diff --git a/src/yuzu/CMakeLists.txt b/src/yuzu/CMakeLists.txt
index dfc675cc8..06d982d9b 100644
--- a/src/yuzu/CMakeLists.txt
+++ b/src/yuzu/CMakeLists.txt
@@ -353,7 +353,7 @@ if (USE_DISCORD_PRESENCE)
353 discord_impl.cpp 353 discord_impl.cpp
354 discord_impl.h 354 discord_impl.h
355 ) 355 )
356 target_link_libraries(yuzu PRIVATE DiscordRPC::discord-rpc) 356 target_link_libraries(yuzu PRIVATE DiscordRPC::discord-rpc httplib::httplib)
357 target_compile_definitions(yuzu PRIVATE -DUSE_DISCORD_PRESENCE) 357 target_compile_definitions(yuzu PRIVATE -DUSE_DISCORD_PRESENCE)
358endif() 358endif()
359 359
diff --git a/src/yuzu/discord_impl.cpp b/src/yuzu/discord_impl.cpp
index c351e9b83..de0c307d4 100644
--- a/src/yuzu/discord_impl.cpp
+++ b/src/yuzu/discord_impl.cpp
@@ -4,7 +4,10 @@
4#include <chrono> 4#include <chrono>
5#include <string> 5#include <string>
6#include <discord_rpc.h> 6#include <discord_rpc.h>
7#include <fmt/format.h>
8#include <httplib.h>
7#include "common/common_types.h" 9#include "common/common_types.h"
10#include "common/string_util.h"
8#include "core/core.h" 11#include "core/core.h"
9#include "core/loader/loader.h" 12#include "core/loader/loader.h"
10#include "yuzu/discord_impl.h" 13#include "yuzu/discord_impl.h"
@@ -14,7 +17,6 @@ namespace DiscordRPC {
14 17
15DiscordImpl::DiscordImpl(Core::System& system_) : system{system_} { 18DiscordImpl::DiscordImpl(Core::System& system_) : system{system_} {
16 DiscordEventHandlers handlers{}; 19 DiscordEventHandlers handlers{};
17
18 // The number is the client ID for yuzu, it's used for images and the 20 // The number is the client ID for yuzu, it's used for images and the
19 // application name 21 // application name
20 Discord_Initialize("712465656758665259", &handlers, 1, nullptr); 22 Discord_Initialize("712465656758665259", &handlers, 1, nullptr);
@@ -29,23 +31,74 @@ void DiscordImpl::Pause() {
29 Discord_ClearPresence(); 31 Discord_ClearPresence();
30} 32}
31 33
34static std::string GetGameString(const std::string& title) {
35 // Convert to lowercase
36 std::string icon_name = Common::ToLower(title);
37
38 // Replace spaces with dashes
39 std::replace(icon_name.begin(), icon_name.end(), ' ', '-');
40
41 // Remove non-alphanumeric characters but keep dashes
42 std::erase_if(icon_name, [](char c) { return !std::isalnum(c) && c != '-'; });
43
44 // Remove dashes from the start and end of the string
45 icon_name.erase(icon_name.begin(), std::find_if(icon_name.begin(), icon_name.end(),
46 [](int ch) { return ch != '-'; }));
47 icon_name.erase(
48 std::find_if(icon_name.rbegin(), icon_name.rend(), [](int ch) { return ch != '-'; }).base(),
49 icon_name.end());
50
51 // Remove double dashes
52 icon_name.erase(std::unique(icon_name.begin(), icon_name.end(),
53 [](char a, char b) { return a == '-' && b == '-'; }),
54 icon_name.end());
55
56 return icon_name;
57}
58
32void DiscordImpl::Update() { 59void DiscordImpl::Update() {
33 s64 start_time = std::chrono::duration_cast<std::chrono::seconds>( 60 s64 start_time = std::chrono::duration_cast<std::chrono::seconds>(
34 std::chrono::system_clock::now().time_since_epoch()) 61 std::chrono::system_clock::now().time_since_epoch())
35 .count(); 62 .count();
63 const std::string default_text = "yuzu is an emulator for the Nintendo Switch";
64 const std::string default_image = "yuzu_logo";
65 std::string game_cover_url = "https://yuzu-emu.org";
36 std::string title; 66 std::string title;
37 if (system.IsPoweredOn()) { 67
38 system.GetAppLoader().ReadTitle(title);
39 }
40 DiscordRichPresence presence{}; 68 DiscordRichPresence presence{};
41 presence.largeImageKey = "yuzu_logo"; 69
42 presence.largeImageText = "yuzu is an emulator for the Nintendo Switch";
43 if (system.IsPoweredOn()) { 70 if (system.IsPoweredOn()) {
71 system.GetAppLoader().ReadTitle(title);
72
73 // Used to format Icon URL for yuzu website game compatibility page
74 std::string icon_name = GetGameString(title);
75
76 // New Check for game cover
77 httplib::Client cli(game_cover_url);
78
79 if (auto res = cli.Head(fmt::format("/images/game/boxart/{}.png", icon_name).c_str())) {
80 if (res->status == 200) {
81 game_cover_url += fmt::format("/images/game/boxart/{}.png", icon_name);
82 } else {
83 game_cover_url = "yuzu_logo";
84 }
85 } else {
86 game_cover_url = "yuzu_logo";
87 }
88
89 presence.largeImageKey = game_cover_url.c_str();
90 presence.largeImageText = title.c_str();
91
92 presence.smallImageKey = default_image.c_str();
93 presence.smallImageText = default_text.c_str();
44 presence.state = title.c_str(); 94 presence.state = title.c_str();
45 presence.details = "Currently in game"; 95 presence.details = "Currently in game";
46 } else { 96 } else {
47 presence.details = "Not in game"; 97 presence.largeImageKey = default_image.c_str();
98 presence.largeImageText = default_text.c_str();
99 presence.details = "Currently not in game";
48 } 100 }
101
49 presence.startTimestamp = start_time; 102 presence.startTimestamp = start_time;
50 Discord_UpdatePresence(&presence); 103 Discord_UpdatePresence(&presence);
51} 104}