Rise
The Vieneo Province
Loading...
Searching...
No Matches
DiscordSocialSdk.cpp
Go to the documentation of this file.
3
4#if defined(_WIN64)
5#ifndef WIN32_LEAN_AND_MEAN
6#define WIN32_LEAN_AND_MEAN
7#endif
8#ifndef NOMINMAX
9#define NOMINMAX
10#endif
11#include <windows.h>
12
13// This is the only C++20 translation unit that implements the official wrapper.
14#define DISCORDPP_IMPLEMENTATION
15#define DISCORD_API __declspec(dllimport)
16#include <discordpp.h>
17#include <optional>
18#include <utility>
19
20namespace
21{
22 std::optional<std::string> Nonempty(const std::string& value)
23 {
24 return value.empty() ? std::nullopt : std::optional<std::string>(value);
25 }
26
27 discordpp::Activity BuildSocialActivity(const RiseDiscord::Activity& fields)
28 {
29 discordpp::Activity activity;
31 activity.SetState(Nonempty(fields.state));
32 activity.SetDetails(Nonempty(fields.details));
33 const char* vehicleUrl = RiseDiscord::VehicleWikiUrl(fields.smallImageKey);
34 const char* locationUrl = RiseDiscord::LocationWikiUrl(fields.largeImageKey);
35 // In the simulator the details describe the simulator, not the vehicle.
36 if (!fields.details.empty() && fields.largeImageKey != "simulator")
37 {
38 const char* detailsUrl = vehicleUrl;
39 if (fields.smallImageKey == "type20") detailsUrl = RiseDiscord::LocationWikiUrl("apartment");
40 else if (fields.smallImageKey == "type26") detailsUrl = RiseDiscord::LocationWikiUrl("controltower");
41 else if (fields.smallImageKey == "type30") detailsUrl = RiseDiscord::LocationWikiUrl("hangar");
42 else if (fields.smallImageKey == "type31") detailsUrl = RiseDiscord::LocationWikiUrl("estate");
43 if (detailsUrl) activity.SetDetailsUrl(detailsUrl);
44 }
45 if (!fields.state.empty() && locationUrl) activity.SetStateUrl(locationUrl);
46 discordpp::ActivityButton steamButton;
47 steamButton.SetLabel("View on Steam");
48 steamButton.SetUrl("https://store.steampowered.com/app/795990/");
49 activity.AddButton(steamButton);
50 discordpp::ActivityButton discordButton;
51 discordButton.SetLabel("Join our Discord");
52 discordButton.SetUrl("https://discord.gg/BH6zvaw");
53 activity.AddButton(discordButton);
54 if (!fields.largeImageKey.empty() || !fields.smallImageKey.empty())
55 {
57 assets.SetLargeImage(Nonempty(fields.largeImageKey));
58 assets.SetLargeText(Nonempty(fields.largeImageText));
59 assets.SetSmallImage(Nonempty(fields.smallImageKey));
60 assets.SetSmallText(Nonempty(fields.smallImageText));
61 if (!fields.largeImageKey.empty() && locationUrl) assets.SetLargeUrl(locationUrl);
62 if (!fields.smallImageKey.empty() && vehicleUrl) assets.SetSmallUrl(vehicleUrl);
63 activity.SetAssets(assets);
64 }
65 if (fields.startMilliseconds)
66 {
68 timestamps.SetStart(fields.startMilliseconds);
69 activity.SetTimestamps(timestamps);
70 }
71 return activity;
72 }
73
74 RiseDiscord::Result ConvertResult(const discordpp::ClientResult& result)
75 {
76 return { result.Successful(), static_cast<int>(result.Type()), result.ErrorCode() };
77 }
78
79 class DesktopSocialSdk final : public RiseDiscord::SocialSdk
80 {
81 HMODULE module;
82 std::unique_ptr<discordpp::Client> client;
83 public:
84 explicit DesktopSocialSdk(HMODULE loaded, const RiseDiscord::Log& log) : module(loaded)
85 {
86 client = std::make_unique<discordpp::Client>();
87 client->SetApplicationId(RiseDiscord::ApplicationId);
88 // No Connect/Authorize/token flow: optional desktop Rich Presence only.
89 if (!client->RegisterLaunchSteamApplication(RiseDiscord::ApplicationId,
91 log("Could not register the Steam launch command", true);
92 }
93 ~DesktopSocialSdk() override
94 {
95 client.reset();
96 FreeLibrary(module);
97 }
98 void Pump() override { discordpp::RunCallbacks(); }
99 void Clear() override { client->ClearRichPresence(); }
100 void Update(const RiseDiscord::Activity& fields, Completion completion) override
101 {
102 client->UpdateRichPresence(BuildSocialActivity(fields),
103 [completion = std::move(completion)](const discordpp::ClientResult& result) {
104 completion(ConvertResult(result));
105 });
106 }
107 void QueryUser(UserCompletion completion) override
108 {
109 client->GetDiscordClientConnectedUser(RiseDiscord::ApplicationId,
110 [completion = std::move(completion)](const discordpp::ClientResult& result,
111 std::optional<discordpp::UserHandle> user) {
112 completion(ConvertResult(result), user ? user->Id() : 0);
113 });
114 }
115 };
116}
117#endif
118
119std::unique_ptr<RiseDiscord::SocialSdk> RiseDiscord::CreateSocialSdk(const Log& log)
120{
121#if defined(_WIN64)
122 // The SDK is delay-loaded so a missing runtime cannot prevent Rise startup.
123 // Resolve only beside this executable; never search the working directory.
124 wchar_t path[32768];
125 const auto length = GetModuleFileNameW(nullptr, path, static_cast<DWORD>(std::size(path)));
126 if (!length || length >= std::size(path)) return {};
127 auto separator = wcsrchr(path, L'\\');
128 if (!separator) return {};
129 *(separator + 1) = L'\0';
130 wcscat_s(path, L"discord_partner_sdk.dll");
131 HMODULE module = LoadLibraryExW(path, nullptr,
132 LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
133 if (!module)
134 {
135 if (log) log("Social SDK DLL unavailable; continuing without Discord", true);
136 return {};
137 }
138 using Version = std::int32_t (*)();
139 const auto major = reinterpret_cast<Version>(GetProcAddress(module, "Discord_Client_GetVersionMajor"));
140 const auto minor = reinterpret_cast<Version>(GetProcAddress(module, "Discord_Client_GetVersionMinor"));
141 const auto patch = reinterpret_cast<Version>(GetProcAddress(module, "Discord_Client_GetVersionPatch"));
142 // Ship one verified ABI/header pair. An old DLL left by an incomplete update
143 // is optional too, rather than a delay-load exception in the game's main loop.
144 if (!major || !minor || !patch || major() != 1 || minor() != 10 || patch() != 19337)
145 {
146 if (log) log("Social SDK DLL version mismatch; expected 1.10.19337", true);
147 FreeLibrary(module);
148 return {};
149 }
150 auto sdk = std::make_unique<DesktopSocialSdk>(module, log);
151 if (log) log("Optional Social SDK 1.10.19337 initialized", false);
152 return sdk;
153#else
154 if (log) log("Discord Social SDK requires a 64-bit client; continuing without Discord", false);
155 return {};
156#endif
157}
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
Struct which controls what your rich presence looks like in the Discord client. If you don't specify ...
Definition discordpp.h:827
void SetLargeImage(std::optional< std::string > LargeImage)
Setter for ActivityAssets::LargeImage.
void SetLargeText(std::optional< std::string > LargeText)
Setter for ActivityAssets::LargeText.
void SetLargeUrl(std::optional< std::string > LargeUrl)
Setter for ActivityAssets::LargeUrl.
void SetSmallImage(std::optional< std::string > SmallImage)
Setter for ActivityAssets::SmallImage.
void SetSmallText(std::optional< std::string > SmallText)
Setter for ActivityAssets::SmallText.
void SetSmallUrl(std::optional< std::string > SmallUrl)
Setter for ActivityAssets::SmallUrl.
void SetLabel(std::string Label)
Setter for ActivityButton::Label.
void SetUrl(std::string Url)
Setter for ActivityButton::Url.
An Activity represents one "thing" a user is doing on Discord and is part of their rich presence.
Definition discordpp.h:1299
void SetDetailsUrl(std::optional< std::string > DetailsUrl)
Setter for Activity::DetailsUrl.
void SetStateUrl(std::optional< std::string > StateUrl)
Setter for Activity::StateUrl.
void SetDetails(std::optional< std::string > Details)
Setter for Activity::Details.
void SetType(discordpp::ActivityTypes Type)
Setter for Activity::Type.
void SetTimestamps(std::optional< discordpp::ActivityTimestamps > Timestamps)
Setter for Activity::Timestamps.
void SetAssets(std::optional< discordpp::ActivityAssets > Assets)
Setter for Activity::Assets.
void AddButton(discordpp::ActivityButton button)
Adds a custom button to the rich presence.
void SetState(std::optional< std::string > State)
Setter for Activity::State.
void SetStart(uint64_t Start)
Setter for ActivityTimestamps::Start.
Struct that stores information about the result of an SDK function call.
Definition discordpp.h:1455
discordpp::ErrorType Type() const
The type of error that occurred. See ErrorType for more information.
int32_t ErrorCode() const
A more detailed error code for this failure. Currently the only use of this is when an API request is...
bool Successful() const
Equivalent to type == ErrorType::None.
float Update(Component *components, std::size_t count, bool viewerInside)
const char * VehicleWikiUrl(std::string_view imageKey)
const char * LocationWikiUrl(std::string_view imageKey)
constexpr std::uint64_t ApplicationId
constexpr std::uint32_t SteamApplicationId
std::unique_ptr< SocialSdk > CreateSocialSdk(const Log &log)
std::function< void(const char *, bool warning)> Log
void RunCallbacks()
Definition discordpp.h:29