| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "UICommon/DiscordPresence.h" |
| 5 | |
| 6 | #include "Core/Config/NetplaySettings.h" |
| 7 | #include "Core/Config/UISettings.h" |
| 8 | #include "Core/ConfigManager.h" |
| 9 | |
| 10 | #ifdef USE_DISCORD_PRESENCE |
| 11 | |
| 12 | #include <ctime> |
| 13 | #include <string> |
| 14 | |
| 15 | #include <discord_rpc.h> |
| 16 | #include <fmt/format.h> |
| 17 | |
| 18 | #include "Common/Hash.h" |
| 19 | |
| 20 | #include "Core/AchievementManager.h" |
| 21 | #include "Core/Config/AchievementSettings.h" |
| 22 | #include "Core/System.h" |
| 23 | |
| 24 | #endif |
| 25 | |
| 26 | namespace Discord |
| 27 | { |
| 28 | #ifdef USE_DISCORD_PRESENCE |
| 29 | static bool s_using_custom_client = false; |
| 30 | |
| 31 | namespace |
| 32 | { |
| 33 | Handler* event_handler = nullptr; |
| 34 | const char* username = ""; |
| 35 | static int64_t s_start_timestamp = std::chrono::duration_cast<std::chrono::seconds>( |
| 36 | std::chrono::system_clock::now().time_since_epoch()) |
| 37 | .count(); |
| 38 | |
| 39 | void HandleDiscordReady(const DiscordUser* user) |
| 40 | { |
| 41 | username = user->username; |
| 42 | } |
| 43 | |
| 44 | void HandleDiscordJoinRequest(const DiscordUser* user) |
| 45 | { |
| 46 | if (event_handler == nullptr) |
| 47 | return; |
| 48 | |
| 49 | const std::string discord_tag = fmt::format("{}#{}", user->username, user->discriminator); |
| 50 | event_handler->DiscordJoinRequest(user->userId, discord_tag, user->avatar); |
| 51 | } |
| 52 | |
| 53 | void HandleDiscordJoin(const char* join_secret) |
| 54 | { |
| 55 | if (event_handler == nullptr) |
| 56 | return; |
| 57 | |
| 58 | if (Config::IsDefaultValue(Config::NETPLAY_NICKNAME)) |
| 59 | Config::SetCurrent(Config::NETPLAY_NICKNAME, username); |
| 60 | |
| 61 | std::string secret(join_secret); |
| 62 | |
| 63 | std::string type = secret.substr(0, secret.find('\n')); |
| 64 | size_t offset = type.length() + 1; |
| 65 | |
| 66 | switch (static_cast<SecretType>(std::stol(type))) |
| 67 | { |
| 68 | default: |
| 69 | case SecretType::Empty: |
| 70 | return; |
| 71 | |
| 72 | case SecretType::IPAddress: |
| 73 | { |
| 74 | // SetBaseOrCurrent will save the ip address, which isn't what's wanted in this situation |
| 75 | Config::SetCurrent(Config::NETPLAY_TRAVERSAL_CHOICE, "direct"); |
| 76 | |
| 77 | std::string host = secret.substr(offset, secret.find_last_of(':') - offset); |
| 78 | Config::SetCurrent(Config::NETPLAY_ADDRESS, host); |
| 79 | |
| 80 | offset += host.length(); |
| 81 | if (secret[offset] == ':') |
| 82 | Config::SetCurrent(Config::NETPLAY_CONNECT_PORT, std::stoul(secret.substr(offset + 1))); |
| 83 | } |
| 84 | break; |
| 85 | |
| 86 | case SecretType::RoomID: |
| 87 | { |
| 88 | Config::SetCurrent(Config::NETPLAY_TRAVERSAL_CHOICE, "traversal"); |
| 89 | |
| 90 | Config::SetCurrent(Config::NETPLAY_HOST_CODE, secret.substr(offset)); |
| 91 | } |
| 92 | break; |
| 93 | } |
| 94 | |
| 95 | event_handler->DiscordJoin(); |
| 96 | } |
| 97 | |
| 98 | std::string ArtworkForGameId() |
| 99 | { |
| 100 | const DiscIO::Region region = SConfig::GetInstance().m_region; |
| 101 | const bool is_wii = Core::System::GetInstance().IsWii(); |
| 102 | const std::string region_code = SConfig::GetInstance().GetGameTDBImageRegionCode(is_wii, region); |
| 103 | |
| 104 | static constexpr char cover_url[] = "https://discord.dolphin-emu.org/cover-art/{}/{}.png"; |
| 105 | return fmt::format(cover_url, region_code, SConfig::GetInstance().GetGameTDBID()); |
| 106 | } |
| 107 | |
| 108 | } // namespace |
| 109 | #endif |
| 110 | |
| 111 | Discord::Handler::~Handler() = default; |
| 112 | |
| 113 | void Init() |
| 114 | { |
| 115 | #ifdef USE_DISCORD_PRESENCE |
| 116 | if (!Config::Get(Config::MAIN_USE_DISCORD_PRESENCE)) |
| 117 | return; |
| 118 | |
| 119 | DiscordEventHandlers handlers = {}; |
| 120 | |
| 121 | handlers.ready = HandleDiscordReady; |
| 122 | handlers.joinRequest = HandleDiscordJoinRequest; |
| 123 | handlers.joinGame = HandleDiscordJoin; |
| 124 | Discord_Initialize(DEFAULT_CLIENT_ID.c_str(), &handlers, 1, nullptr); |
| 125 | UpdateDiscordPresence(); |
| 126 | #endif |
| 127 | } |
| 128 | |
| 129 | void UpdateClientID(const std::string& new_client) |
| 130 | { |
| 131 | #ifdef USE_DISCORD_PRESENCE |
| 132 | if (!Config::Get(Config::MAIN_USE_DISCORD_PRESENCE)) |
| 133 | return; |
| 134 | |
| 135 | s_using_custom_client = new_client.empty() || new_client.compare(DEFAULT_CLIENT_ID) != 0; |
| 136 | |
| 137 | Shutdown(); |
| 138 | if (s_using_custom_client) |
| 139 | Discord_Initialize(new_client.c_str(), nullptr, 0, nullptr); |
| 140 | else // if initialising dolphin's client ID, make sure to restore event handlers |
| 141 | Init(); |
| 142 | #endif |
| 143 | } |
| 144 | |
| 145 | void CallPendingCallbacks() |
| 146 | { |
| 147 | #ifdef USE_DISCORD_PRESENCE |
| 148 | if (!Config::Get(Config::MAIN_USE_DISCORD_PRESENCE)) |
| 149 | return; |
| 150 | |
| 151 | Discord_RunCallbacks(); |
| 152 | |
| 153 | #endif |
| 154 | } |
| 155 | |
| 156 | void InitNetPlayFunctionality(Handler& handler) |
| 157 | { |
| 158 | #ifdef USE_DISCORD_PRESENCE |
| 159 | event_handler = &handler; |
| 160 | #endif |
| 161 | } |
| 162 | |
| 163 | bool UpdateDiscordPresenceRaw(const std::string& details, const std::string& state, |
| 164 | const std::string& large_image_key, |
| 165 | const std::string& large_image_text, |
| 166 | const std::string& small_image_key, |
| 167 | const std::string& small_image_text, const int64_t start_timestamp, |
| 168 | const int64_t end_timestamp, const int party_size, |
| 169 | const int party_max) |
| 170 | { |
| 171 | #ifdef USE_DISCORD_PRESENCE |
| 172 | if (!Config::Get(Config::MAIN_USE_DISCORD_PRESENCE)) |
| 173 | return false; |
| 174 | |
| 175 | // only /dev/dolphin sets this, don't let homebrew change official client ID raw presence |
| 176 | if (!s_using_custom_client) |
| 177 | return false; |
| 178 | |
| 179 | DiscordRichPresence discord_presence = {}; |
| 180 | discord_presence.details = details.c_str(); |
| 181 | discord_presence.state = state.c_str(); |
| 182 | discord_presence.largeImageKey = large_image_key.c_str(); |
| 183 | discord_presence.largeImageText = large_image_text.c_str(); |
| 184 | discord_presence.smallImageKey = small_image_key.c_str(); |
| 185 | discord_presence.smallImageText = small_image_text.c_str(); |
| 186 | discord_presence.startTimestamp = start_timestamp; |
| 187 | discord_presence.endTimestamp = end_timestamp; |
| 188 | discord_presence.partySize = party_size; |
| 189 | discord_presence.partyMax = party_max; |
| 190 | Discord_UpdatePresence(&discord_presence); |
| 191 | |
| 192 | return true; |
| 193 | #else |
| 194 | return false; |
| 195 | #endif |
| 196 | } |
| 197 | |
| 198 | void UpdateDiscordPresence(int party_size, SecretType type, const std::string& secret, |
| 199 | const std::string& current_game, bool reset_timer) |
| 200 | { |
| 201 | #ifdef USE_DISCORD_PRESENCE |
| 202 | if (!Config::Get(Config::MAIN_USE_DISCORD_PRESENCE)) |
| 203 | return; |
| 204 | |
| 205 | // reset the client ID if running homebrew has changed it |
| 206 | if (s_using_custom_client) |
| 207 | UpdateClientID(DEFAULT_CLIENT_ID); |
| 208 | |
| 209 | const std::string& title = |
| 210 | current_game.empty() ? SConfig::GetInstance().GetTitleDescription() : current_game; |
| 211 | std::string game_artwork = |
| 212 | SConfig::GetInstance().GetGameTDBID().empty() ? "" : ArtworkForGameId(); |
| 213 | |
| 214 | DiscordRichPresence discord_presence = {}; |
| 215 | if (game_artwork.empty()) |
| 216 | { |
| 217 | discord_presence.largeImageKey = "dolphin_logo"; |
| 218 | discord_presence.largeImageText = "Dolphin is an emulator for the GameCube and the Wii."; |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | discord_presence.largeImageKey = game_artwork.c_str(); |
| 223 | discord_presence.largeImageText = title.c_str(); |
| 224 | discord_presence.smallImageKey = "dolphin_logo"; |
| 225 | discord_presence.smallImageText = "Dolphin is an emulator for the GameCube and the Wii."; |
| 226 | } |
| 227 | discord_presence.details = title.empty() ? "Not in-game" : title.c_str(); |
| 228 | if (reset_timer) |
| 229 | { |
| 230 | s_start_timestamp = std::chrono::duration_cast<std::chrono::seconds>( |
| 231 | std::chrono::system_clock::now().time_since_epoch()) |
| 232 | .count(); |
| 233 | } |
| 234 | discord_presence.startTimestamp = s_start_timestamp; |
| 235 | |
| 236 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 237 | std::string state_string; |
| 238 | #endif // USE_RETRO_ACHIEVEMENTS |
| 239 | if (party_size > 0) |
| 240 | { |
| 241 | if (party_size < 4) |
| 242 | { |
| 243 | discord_presence.state = "In a party"; |
| 244 | discord_presence.partySize = party_size; |
| 245 | discord_presence.partyMax = 4; |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | // others can still join to spectate |
| 250 | discord_presence.state = "In a full party"; |
| 251 | discord_presence.partySize = party_size; |
| 252 | // Note: joining still works without partyMax |
| 253 | } |
| 254 | } |
| 255 | #ifdef USE_RETRO_ACHIEVEMENTS |
| 256 | else if (Config::Get(Config::RA_ENABLED) && Config::Get(Config::RA_DISCORD_PRESENCE_ENABLED)) |
| 257 | { |
| 258 | state_string = AchievementManager::GetInstance().GetRichPresence().data(); |
| 259 | if (state_string.length() >= 128) |
| 260 | { |
| 261 | // 124 characters + 3 dots + null terminator - thanks to Stenzek for format |
| 262 | state_string.resize(124); |
| 263 | state_string += "..."; |
| 264 | } |
| 265 | discord_presence.state = state_string.c_str(); |
| 266 | } |
| 267 | #endif // USE_RETRO_ACHIEVEMENTS |
| 268 | |
| 269 | std::string party_id; |
| 270 | std::string secret_final; |
| 271 | if (type != SecretType::Empty) |
| 272 | { |
| 273 | // Declaring party_id or secret_final here will deallocate the variable before passing the |
| 274 | // values over to Discord_UpdatePresence. |
| 275 | |
| 276 | const size_t secret_length = secret.length(); |
| 277 | party_id = std::to_string( |
| 278 | Common::HashAdler32(reinterpret_cast<const u8*>(secret.c_str()), secret_length)); |
| 279 | |
| 280 | const std::string secret_type = std::to_string(static_cast<int>(type)); |
| 281 | secret_final.reserve(secret_type.length() + 1 + secret_length); |
| 282 | secret_final += secret_type; |
| 283 | secret_final += '\n'; |
| 284 | secret_final += secret; |
| 285 | } |
| 286 | discord_presence.partyId = party_id.c_str(); |
| 287 | discord_presence.joinSecret = secret_final.c_str(); |
| 288 | |
| 289 | Discord_UpdatePresence(&discord_presence); |
| 290 | #endif |
| 291 | } |
| 292 | |
| 293 | std::string CreateSecretFromIPAddress(const std::string& ip_address, int port) |
| 294 | { |
| 295 | const std::string port_string = std::to_string(port); |
| 296 | std::string secret; |
| 297 | secret.reserve(ip_address.length() + 1 + port_string.length()); |
| 298 | secret += ip_address; |
| 299 | secret += ':'; |
| 300 | secret += port_string; |
| 301 | return secret; |
| 302 | } |
| 303 | |
| 304 | void Shutdown() |
| 305 | { |
| 306 | #ifdef USE_DISCORD_PRESENCE |
| 307 | if (!Config::Get(Config::MAIN_USE_DISCORD_PRESENCE)) |
| 308 | return; |
| 309 | |
| 310 | Discord_ClearPresence(); |
| 311 | Discord_Shutdown(); |
| 312 | #endif |
| 313 | } |
| 314 | |
| 315 | void SetDiscordPresenceEnabled(bool enabled) |
| 316 | { |
| 317 | if (Config::Get(Config::MAIN_USE_DISCORD_PRESENCE) == enabled) |
| 318 | return; |
| 319 | |
| 320 | if (Config::Get(Config::MAIN_USE_DISCORD_PRESENCE)) |
| 321 | Discord::Shutdown(); |
| 322 | |
| 323 | Config::SetBase(Config::MAIN_USE_DISCORD_PRESENCE, enabled); |
| 324 | |
| 325 | if (Config::Get(Config::MAIN_USE_DISCORD_PRESENCE)) |
| 326 | Discord::Init(); |
| 327 | } |
| 328 | |
| 329 | } // namespace Discord |