|
1 | 1 |
|
2 | | -#include <core/helper/magic_enum_wrapper.hpp> |
3 | 2 | #include <core/helper/utils.hpp> |
4 | 3 |
|
5 | 4 | #include "./core.hpp" |
|
18 | 17 | } |
19 | 18 | } |
20 | 19 |
|
21 | | -DiscordInstance::DiscordInstance(discord::Core* core) : m_core{ core }, m_current_user{ nullptr } { } |
| 20 | +DiscordInstance::DiscordInstance() |
| 21 | + : m_current_user{ discordpp::UserHandle::nullobj }, |
| 22 | + m_status{ DiscordStatus::Starting } { |
22 | 23 |
|
23 | | -void DiscordInstance::after_setup() { |
24 | | - m_core->UserManager().OnCurrentUserUpdate.Connect([this]() { |
25 | | - auto* user = new discord::User; // NOLINT(cppcoreguidelines-owning-memory) |
26 | | - this->m_core->UserManager().GetCurrentUser(user); |
27 | | - this->m_current_user.reset(user); |
28 | | - spdlog::info( |
29 | | - "Current user updated: {}#{}", this->m_current_user->GetUsername(), |
30 | | - this->m_current_user->GetDiscriminator() |
31 | | - ); |
32 | | - }); |
33 | | - |
34 | | - auto result = m_core->ActivityManager().RegisterCommand(constants::discord::platform_dependent_launch_arguments); |
35 | | - if (result != discord::Result::Ok) { |
36 | | - spdlog::warn("ActivityManager: Failed to RegisterCommand: {}", magic_enum::enum_name(result)); |
37 | | - }; |
38 | | -} |
39 | | - |
40 | | -[[nodiscard]] helper::expected<DiscordInstance, std::string> DiscordInstance::initialize() { |
41 | | - |
42 | | - discord::Core* core{}; |
43 | | - auto result = discord::Core::Create(constants::discord::client_id, DiscordCreateFlags_Default, &core); |
44 | | - if (core == nullptr) { |
45 | | - return helper::unexpected<std::string>{ |
46 | | - fmt::format("Failed to instantiate discord core: {}", magic_enum::enum_name(result)) |
47 | | - }; |
48 | | - } |
49 | | - |
50 | | - |
51 | | - core->SetLogHook( |
52 | | -#if !defined(NDEBUG) |
53 | | - discord::LogLevel::Debug |
54 | | -#else |
55 | | - discord::LogLevel::Error |
56 | | -#endif |
57 | | - , |
58 | | - [](discord::LogLevel level, const char* message) { |
59 | | - switch (level) { |
60 | | - case discord::LogLevel::Error: |
| 24 | + m_client.AddLogCallback( |
| 25 | + [](std::string message, discordpp::LoggingSeverity severity) -> void { |
| 26 | + switch (severity) { |
| 27 | + case discordpp::LoggingSeverity::Error: |
61 | 28 | spdlog::error("DISCORD SDK: {}", message); |
62 | 29 | break; |
63 | | - case discord::LogLevel::Warn: |
| 30 | + case discordpp::LoggingSeverity::Warning: |
64 | 31 | spdlog::warn("DISCORD SDK: {}", message); |
65 | 32 | break; |
66 | | - case discord::LogLevel::Info: |
| 33 | + case discordpp::LoggingSeverity::Info: |
67 | 34 | spdlog::info("DISCORD SDK: {}", message); |
68 | 35 | break; |
69 | | - case discord::LogLevel::Debug: |
| 36 | + case discordpp::LoggingSeverity::Verbose: |
70 | 37 | spdlog::debug("DISCORD SDK: {}", message); |
71 | 38 | break; |
| 39 | + case discordpp::LoggingSeverity::None: |
| 40 | + break; |
| 41 | + } |
| 42 | + }, |
| 43 | +#if !defined(NDEBUG) |
| 44 | + discordpp::LoggingSeverity::Verbose |
| 45 | +#else |
| 46 | + discordpp::LoggingSeverity::Error |
| 47 | +#endif |
| 48 | + ); |
| 49 | + |
| 50 | + |
| 51 | + m_client.SetStatusChangedCallback( |
| 52 | + [this](discordpp::Client::Status status, discordpp::Client::Error error, int32_t error_detail) -> void { |
| 53 | + if (error != discordpp::Client::Error::None) { |
| 54 | + this->m_status = DiscordStatus::Error; |
| 55 | + spdlog::error( |
| 56 | + "Connection Error: {} - Details: {}", discordpp::Client::ErrorToString(error), error_detail |
| 57 | + ); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + if (status == discordpp::Client::Status::Ready) { |
| 62 | + this->m_status = DiscordStatus::Ok; |
| 63 | + this->after_ready(); |
| 64 | + return; |
72 | 65 | } |
73 | 66 | } |
74 | 67 | ); |
75 | 68 |
|
76 | | - return DiscordInstance{ core }; |
| 69 | + m_client.SetApplicationId(constants::discord::application_id); |
| 70 | + |
| 71 | + m_client.Connect(); |
| 72 | +} |
| 73 | + |
| 74 | +void DiscordInstance::after_ready() { |
| 75 | + |
| 76 | + this->m_client.GetDiscordClientConnectedUser( |
| 77 | + constants::discord::application_id, |
| 78 | + [this](const discordpp::ClientResult& result, std::optional<discordpp::UserHandle> user) -> void { |
| 79 | + if (result.Successful() and user.has_value()) { |
| 80 | + |
| 81 | + auto user_handle = m_client.GetUser(user->Id()); |
| 82 | + if (not user_handle.has_value()) { |
| 83 | + spdlog::error("Current Connected User Error: Can't get userhandle from id: {}", user->Id()); |
| 84 | + |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + this->m_current_user = user_handle.value(); |
| 89 | + spdlog::info("Current user updated: {}", user_handle->Username()); |
| 90 | + |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + spdlog::error("Current Connected User Error: {}", result.ToString()); |
| 95 | + } |
| 96 | + ); |
77 | 97 | } |
78 | 98 |
|
79 | 99 |
|
80 | 100 | DiscordInstance::DiscordInstance(DiscordInstance&& old) noexcept |
81 | | - : m_core{ std::move(old.m_core) }, |
82 | | - m_current_user{ std::move(old.m_current_user) } { |
83 | | - old.m_core = nullptr; |
84 | | - old.m_current_user = nullptr; |
| 101 | + : m_client{ std::move(old.m_client) }, |
| 102 | + m_current_user{ std::move(old.m_current_user) }, |
| 103 | + m_status{ old.m_status } { |
| 104 | + old.m_client = discordpp::Client{}; |
| 105 | + old.m_current_user = discordpp::UserHandle::nullobj; |
| 106 | + old.m_status = DiscordStatus::Error; |
85 | 107 | } |
86 | 108 |
|
87 | 109 |
|
88 | 110 | DiscordInstance& DiscordInstance::operator=(DiscordInstance&& other) noexcept { |
89 | 111 | if (this != &other) { |
90 | 112 |
|
91 | | - m_core = std::move(other.m_core); |
| 113 | + m_client = std::move(other.m_client); |
92 | 114 | m_current_user = std::move(other.m_current_user); |
| 115 | + m_status = other.m_status; |
93 | 116 |
|
94 | | - other.m_core = nullptr; |
95 | | - other.m_current_user = nullptr; |
| 117 | + other.m_client = discordpp::Client{}; |
| 118 | + other.m_current_user = discordpp::UserHandle::nullobj; |
| 119 | + other.m_status = DiscordStatus::Error; |
96 | 120 | } |
97 | 121 | return *this; |
98 | 122 | }; |
99 | 123 |
|
100 | 124 | DiscordInstance::~DiscordInstance() { |
101 | | - if (m_core != nullptr) { |
| 125 | + if (m_client.operator bool()) { |
102 | 126 | clear_activity(); |
| 127 | + m_client.Disconnect(); |
103 | 128 | } |
104 | 129 | } |
105 | 130 |
|
| 131 | +[[nodiscard]] DiscordStatus DiscordInstance::get_status() { |
| 132 | + return m_status; |
| 133 | +} |
| 134 | + |
106 | 135 | void DiscordInstance::update() { |
107 | | - m_core->RunCallbacks(); |
| 136 | + discordpp::RunCallbacks(); |
108 | 137 | } |
109 | 138 |
|
110 | 139 |
|
111 | 140 | void DiscordInstance::set_activity(const DiscordActivityWrapper& activity) { |
112 | 141 |
|
113 | | - m_core->ActivityManager().UpdateActivity(activity.get_raw(), [](discord::Result result) { |
114 | | - spdlog::info("Result to UpdateActivity: {}", magic_enum::enum_name(result)); |
115 | | - }); |
116 | | -} |
| 142 | + const auto& raw_activity = activity.get_raw(); |
117 | 143 |
|
| 144 | + if (not raw_activity.operator bool()) { |
| 145 | + spdlog::error("Tried to set an invalid Discord Activity!"); |
| 146 | + return; |
| 147 | + } |
118 | 148 |
|
119 | | -void DiscordInstance::clear_activity(bool wait) { |
120 | | - bool received_callback = false; |
121 | | - m_core->ActivityManager().ClearActivity([&received_callback](discord::Result result) { |
122 | | - spdlog::info("Result to ClearActivity: {}", magic_enum::enum_name(result)); |
123 | | - received_callback = true; |
| 149 | + // Update rich presence |
| 150 | + m_client.UpdateRichPresence(raw_activity, [](const discordpp::ClientResult& result) { |
| 151 | + if (result.Successful()) { |
| 152 | + spdlog::info("Rich Presence updated successfully"); |
| 153 | + } else { |
| 154 | + spdlog::error("Rich Presence update failed: {}", result.ToString()); |
| 155 | + } |
124 | 156 | }); |
| 157 | +} |
125 | 158 |
|
126 | | - using namespace std::chrono_literals; |
127 | | - |
128 | | - if (wait) { |
129 | | - |
130 | | - constexpr auto max_waittime = 1s; |
131 | | - |
132 | | - const auto start_time = std::chrono::steady_clock::now(); |
133 | 159 |
|
134 | | - while (not received_callback) { |
135 | | - this->update(); |
136 | | - std::this_thread::sleep_for(1ms); |
137 | | - const auto now = std::chrono::steady_clock::now(); |
138 | | - if (now - start_time >= max_waittime) { |
139 | | - break; |
140 | | - } |
141 | | - } |
142 | | - } |
| 160 | +void DiscordInstance::clear_activity() { |
| 161 | + m_client.ClearRichPresence(); |
143 | 162 | } |
144 | 163 |
|
145 | 164 |
|
146 | | -DiscordActivityWrapper::DiscordActivityWrapper(const std::string& details, discord::ActivityType type) { |
147 | | - m_activity.SetDetails(details.c_str()); |
| 165 | +DiscordActivityWrapper::DiscordActivityWrapper(const std::string& details, discordpp::ActivityTypes type) { |
| 166 | + // NOTE: this are partial fields, that are set by the final call, do not set them manually |
| 167 | + // https://discord.com/developers/docs/rich-presence/using-with-the-game-sdk#partial-activity-struct |
| 168 | + // m_activity.SetName(constants::program_name); |
| 169 | + // m_activity.SetApplicationId(constants::application_id); |
| 170 | + |
| 171 | + m_activity.SetDetails(details); |
148 | 172 | m_activity.SetType(type); |
149 | 173 | m_activity.SetSupportedPlatforms(constants::discord::supported_platforms); |
150 | 174 | } |
151 | 175 |
|
152 | 176 |
|
153 | 177 | DiscordActivityWrapper& |
154 | 178 | DiscordActivityWrapper::set_large_image(const std::string& text, constants::discord::ArtAsset asset) { |
155 | | - m_activity.GetAssets().SetLargeText(text.c_str()); |
| 179 | + auto assets = this->get_assets(); |
156 | 180 |
|
157 | 181 | const auto asset_key = constants::discord::get_asset_key(asset); |
158 | | - m_activity.GetAssets().SetLargeImage(asset_key.c_str()); |
| 182 | + |
| 183 | + assets.SetLargeImage(asset_key); |
| 184 | + assets.SetLargeText(text); |
| 185 | + |
| 186 | + m_activity.SetAssets(assets); |
159 | 187 |
|
160 | 188 | return *this; |
161 | 189 | } |
162 | 190 |
|
163 | 191 |
|
164 | 192 | DiscordActivityWrapper& |
165 | 193 | DiscordActivityWrapper::set_small_image(const std::string& text, constants::discord::ArtAsset asset) { |
166 | | - m_activity.GetAssets().SetSmallText(text.c_str()); |
| 194 | + auto assets = this->get_assets(); |
167 | 195 |
|
168 | 196 | const auto asset_key = constants::discord::get_asset_key(asset); |
169 | | - m_activity.GetAssets().SetSmallImage(asset_key.c_str()); |
| 197 | + |
| 198 | + assets.SetSmallImage(asset_key); |
| 199 | + assets.SetSmallText(text); |
| 200 | + |
| 201 | + m_activity.SetAssets(assets); |
| 202 | + |
170 | 203 | return *this; |
171 | 204 | } |
172 | 205 |
|
173 | 206 | DiscordActivityWrapper& DiscordActivityWrapper::set_details(const std::string& text) { |
174 | | - m_activity.SetState(text.c_str()); |
| 207 | + m_activity.SetState(text); |
175 | 208 |
|
176 | 209 | return *this; |
177 | 210 | } |
178 | 211 |
|
179 | 212 |
|
180 | | -[[nodiscard]] const discord::Activity& DiscordActivityWrapper::get_raw() const { |
| 213 | +[[nodiscard]] const discordpp::Activity& DiscordActivityWrapper::get_raw() const { |
181 | 214 | return m_activity; |
182 | 215 | } |
| 216 | + |
| 217 | + |
| 218 | +discordpp::ActivityTimestamps DiscordActivityWrapper::get_timestamps() { |
| 219 | + |
| 220 | + std::optional<discordpp::ActivityTimestamps> timestamps = this->m_activity.Timestamps(); |
| 221 | + |
| 222 | + if (timestamps.has_value()) { |
| 223 | + return timestamps.value(); |
| 224 | + } |
| 225 | + |
| 226 | + return discordpp::ActivityTimestamps(); |
| 227 | +} |
| 228 | + |
| 229 | + |
| 230 | +[[nodiscard]] discordpp::ActivityAssets DiscordActivityWrapper::get_assets() { |
| 231 | + std::optional<discordpp::ActivityAssets> assets = this->m_activity.Assets(); |
| 232 | + |
| 233 | + if (assets.has_value()) { |
| 234 | + return assets.value(); |
| 235 | + } |
| 236 | + |
| 237 | + return discordpp::ActivityAssets(); |
| 238 | +} |
0 commit comments