From f1c2fa610ac00938113afcaab1e4c78eef60da08 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Sat, 19 Apr 2025 21:56:11 +0200 Subject: [PATCH 01/13] feat: add newer discord-social-sdk as dependency instead of the older game-sdk --- src/meson.build | 2 +- subprojects/discord-social-sdk.wrap | 10 ++ .../discord_social_sdk/meson.build | 103 ++++++++++++++++++ tools/dependencies/meson.build | 72 +++++++----- 4 files changed, 158 insertions(+), 29 deletions(-) create mode 100644 subprojects/discord-social-sdk.wrap create mode 100644 subprojects/packagefiles/discord_social_sdk/meson.build diff --git a/src/meson.build b/src/meson.build index f3f4571a2..c06d91f6c 100644 --- a/src/meson.build +++ b/src/meson.build @@ -13,7 +13,7 @@ if build_application subdir('ui') subdir('lobby') - if have_discord_sdk + if have_discord_social_sdk subdir('discord') endif diff --git a/subprojects/discord-social-sdk.wrap b/subprojects/discord-social-sdk.wrap new file mode 100644 index 000000000..633454dd0 --- /dev/null +++ b/subprojects/discord-social-sdk.wrap @@ -0,0 +1,10 @@ +[wrap-file] +directory = discord_social_sdk +source_url = https://oopetris.totto.lt/static/assets/discord/DiscordSocialSdk-1.1.8318.zip +source_filename = DiscordSocialSdk-1.1.8318.zip +source_hash = e78ac5c6462d74917b74d2511ed88876f59dbe00d31186590a98ea7953099e31 +patch_directory = discord_social_sdk + +[provide] +discord-social-sdk = discord_social_sdk_dep + diff --git a/subprojects/packagefiles/discord_social_sdk/meson.build b/subprojects/packagefiles/discord_social_sdk/meson.build new file mode 100644 index 000000000..a2dd22542 --- /dev/null +++ b/subprojects/packagefiles/discord_social_sdk/meson.build @@ -0,0 +1,103 @@ +project( + 'discord-social-sdk', + 'cpp', + 'c', + version: '1.1.8318', + meson_version: '>=1.4.0', + default_options: { + 'cpp_std': ['c++17'], + }, +) + +inc_dirs = include_directories('include') + +header_files = files('include' / 'cdiscord.h', 'include' / 'discordpp.h') + +install_headers(header_files, subdir: 'discord') + +lib_base_dir = '' + +if host_machine.system() == 'darwin' + if host_machine.cpu_family() in ['aarch64', 'x86_64'] + lib_base_dir = 'lib' + else + error( + 'unsupported architecture for macos: ' + host_machine.cpu_family(), + ) + endif + +elif host_machine.system() == 'linux' + if host_machine.cpu_family() == 'x86_64' + lib_base_dir = 'lib' + + cat = find_program('cat') + + os_release_info = run_command( + cat, + '/etc/os-release', + check: true, + ).stdout().strip().split('\n') + + linux_distro = '' + + foreach line : os_release_info + line_detail = line.split('=') + + if line_detail[0] == 'ID' + linux_distro = line_detail[1] + endif + + endforeach + + if linux_distro == '' + warning('Couldn\'t detect the linux distro') + endif + + if linux_distro == 'alpine' + error('unsupported libc for linux: musl') + endif + + else + error( + 'unsupported architecture for linux: ' + host_machine.cpu_family(), + ) + endif +elif host_machine.system() == 'windows' + if host_machine.cpu_family() == 'x86_64' + lib_base_dir = get_option('default_library') == 'static' ? 'bin' : 'lib' + else + error( + 'unsupported architecture for windows: ' + host_machine.cpu_family(), + ) + endif +else + error('unsupported system: ' + host_machine.system()) +endif + +lib_dir = ( + lib_base_dir / (get_option('buildtype') == 'release' ? 'release' : 'debug') +) + +c = meson.get_compiler('c') + +discord_partner_sdk = c.find_library( + 'discord_partner_sdk', + dirs: [meson.project_source_root() / lib_dir], + required: true, +) + +discord_lib = library( + 'discord_partner_sdk', + header_files, + include_directories: inc_dirs, + dependencies: discord_partner_sdk, + install: true, +) + +discord_social_sdk_dep = declare_dependency( + include_directories: inc_dirs, + version: meson.project_version(), + link_with: discord_lib, +) + +meson.override_dependency('discord-social-sdk', discord_social_sdk_dep) diff --git a/tools/dependencies/meson.build b/tools/dependencies/meson.build index ba0ffbf57..b38986304 100644 --- a/tools/dependencies/meson.build +++ b/tools/dependencies/meson.build @@ -67,7 +67,9 @@ if meson.is_cross_build() foreach native_dependency_tuple : map_native_dependencies native_dep_lib_name = native_dependency_tuple[0] - native_dep_name = native_dependency_tuple.length() == 2 ? native_dependency_tuple[1] : native_dep_lib_name + native_dep_name = ( + native_dependency_tuple.length() == 2 ? native_dependency_tuple[1] : native_dep_lib_name + ) native_dep = cpp.find_library(native_dep_lib_name, required: true) @@ -102,13 +104,12 @@ if meson.is_cross_build() native_dep_with_version = declare_dependency( dependencies: native_dep, - version: major_version - + '.' - + minor_version - + '.' - + patch_version, + version: major_version + '.' + minor_version + '.' + patch_version, + ) + meson.override_dependency( + native_dep_name, + native_dep_with_version, ) - meson.override_dependency(native_dep_name, native_dep_with_version) else meson.override_dependency(native_dep_name, native_dep) @@ -146,7 +147,8 @@ fmt_use_header_only = false if ( meson.is_cross_build() - and (host_machine.system() == 'switch' or host_machine.system() == '3ds') + and (host_machine.system() == 'switch' + or host_machine.system() == '3ds') ) fmt_use_header_only = true endif @@ -350,7 +352,10 @@ if build_application if (meson.is_cross_build() and host_machine.system() == '3ds') graphics_lib += { - 'compile_args': [graphics_lib.get('compile_args'), '-DSPDLOG_NO_TLS'], + 'compile_args': [ + graphics_lib.get('compile_args'), + '-DSPDLOG_NO_TLS', + ], } endif @@ -407,8 +412,16 @@ if build_application endif - if host_machine.system() == 'linux' or (meson.is_cross_build() and host_machine.system() == 'android') - keyutils_dep = dependency('keyutils', required: true, allow_fallback: true) + if ( + host_machine.system() == 'linux' + or (meson.is_cross_build() + and host_machine.system() == 'android') + ) + keyutils_dep = dependency( + 'keyutils', + required: true, + allow_fallback: true, + ) graphics_lib += {'deps': [graphics_lib.get('deps'), keyutils_dep]} elif host_machine.system() == 'windows' c = meson.get_compiler('c') @@ -425,8 +438,8 @@ if build_application if build_installer if get_option('buildtype') != 'release' error( - 'buildtype needs to be \'release\', when building the installer, but was: ' - + get_option('buildtype'), + 'buildtype needs to be \'release\', when building the installer, but was: ' + + get_option('buildtype'), ) endif @@ -435,7 +448,7 @@ if build_application is_flatpak_build = true else error( - 'only support flatpak builds, when building the installer for linux', + 'only support flatpak builds, when building the installer for linux', ) endif elif host_machine.system() == 'windows' @@ -443,8 +456,7 @@ if build_application else # TODO: create a proper installer for macOS : https://mesonbuild.com/Creating-OSX-packages.html error( - 'unsupported system for building the installer: ' - + host_machine.system(), + 'unsupported system for building the installer: ' + host_machine.system(), ) endif @@ -466,7 +478,7 @@ if build_application endif have_file_dialogs = false - have_discord_sdk = false + have_discord_social_sdk = false nfde_dep = dependency( 'nativefiledialog-extended', @@ -489,29 +501,33 @@ if build_application c = meson.get_compiler('c') - discord_dep_required = not meson.is_cross_build() and get_option('build_installer') + discord_dep_required = ( + not meson.is_cross_build() + and get_option('build_installer') + ) - if host_machine.system() == 'linux' and host_machine.cpu_family() == 'aarch64' + if ( + host_machine.system() == 'linux' + and host_machine.cpu_family() == 'aarch64' + ) # not supported on aarch64 linux :( discord_dep_required = false endif - discord_sdk_dep = dependency( - 'discord-game-sdk', + discord_social_sdk_dep = dependency( + 'discord-social-sdk', allow_fallback: true, required: discord_dep_required, - # only with msvc we need a static library, all others work without adding __declspec() everywhere - static: c.get_id() == 'msvc', - default_options: c.get_id() != 'msvc' ? {} : {'default_library': 'static'}, ) - if discord_sdk_dep.found() - have_discord_sdk = true + + if discord_social_sdk_dep.found() + have_discord_social_sdk = true graphics_lib += { 'compile_args': [ graphics_lib.get('compile_args'), - '-D_HAVE_DISCORD_SDK', + '-D_HAVE_DISCORD_SOCIAL_SDK', ], - 'deps': [graphics_lib.get('deps'), discord_sdk_dep], + 'deps': [graphics_lib.get('deps'), discord_social_sdk_dep], } endif From 4f240f4e549bcef107efd6bfafc82fae78c3de6b Mon Sep 17 00:00:00 2001 From: Totto16 Date: Sat, 19 Apr 2025 21:57:14 +0200 Subject: [PATCH 02/13] feat: remove old discord game sdk --- subprojects/discord-game-sdk.wrap | 12 -- .../packagefiles/discord_game_sdk.diff | 12 -- .../discord_game_sdk/cpp/meson.build | 199 ------------------ .../discord_game_sdk/examples/cpp/meson.build | 9 - .../packagefiles/discord_game_sdk/meson.build | 16 -- .../discord_game_sdk/meson.options | 6 - 6 files changed, 254 deletions(-) delete mode 100644 subprojects/discord-game-sdk.wrap delete mode 100644 subprojects/packagefiles/discord_game_sdk.diff delete mode 100644 subprojects/packagefiles/discord_game_sdk/cpp/meson.build delete mode 100644 subprojects/packagefiles/discord_game_sdk/examples/cpp/meson.build delete mode 100644 subprojects/packagefiles/discord_game_sdk/meson.build delete mode 100644 subprojects/packagefiles/discord_game_sdk/meson.options diff --git a/subprojects/discord-game-sdk.wrap b/subprojects/discord-game-sdk.wrap deleted file mode 100644 index c1a3ea0b1..000000000 --- a/subprojects/discord-game-sdk.wrap +++ /dev/null @@ -1,12 +0,0 @@ -[wrap-file] -directory = discord_game_sdk-3.2.1 -lead_directory_missing = true -source_url = https://dl-game-sdk.discordapp.net/3.2.1/discord_game_sdk.zip -source_filename = discord_game_sdk.zip -source_hash = 6757bb4a1f5b42aa7b6707cbf2158420278760ac5d80d40ca708bb01d20ae6b4 -patch_directory = discord_game_sdk -diff_files = discord_game_sdk.diff - -[provide] -discord-game-sdk = discord_dep - diff --git a/subprojects/packagefiles/discord_game_sdk.diff b/subprojects/packagefiles/discord_game_sdk.diff deleted file mode 100644 index 632b18634..000000000 --- a/subprojects/packagefiles/discord_game_sdk.diff +++ /dev/null @@ -1,12 +0,0 @@ -diff --git a/cpp/types.h b/cpp/types.h -index 76c4311..57399d3 100644 ---- a/cpp/types.h -+++ b/cpp/types.h -@@ -2,6 +2,7 @@ - - #include "ffi.h" - #include "event.h" -+#include - #ifdef _WIN32 - #include - #include diff --git a/subprojects/packagefiles/discord_game_sdk/cpp/meson.build b/subprojects/packagefiles/discord_game_sdk/cpp/meson.build deleted file mode 100644 index 0674ef973..000000000 --- a/subprojects/packagefiles/discord_game_sdk/cpp/meson.build +++ /dev/null @@ -1,199 +0,0 @@ -inc_dirs = include_directories('.') - -header_files = files( - 'achievement_manager.h', - 'activity_manager.h', - 'application_manager.h', - 'core.h', - 'discord.h', - 'event.h', - 'ffi.h', - 'image_manager.h', - 'lobby_manager.h', - 'network_manager.h', - 'overlay_manager.h', - 'relationship_manager.h', - 'storage_manager.h', - 'store_manager.h', - 'types.h', - 'user_manager.h', - 'voice_manager.h', -) - -install_headers(header_files, subdir: 'discord') - -src_files = files( - 'achievement_manager.cpp', - 'activity_manager.cpp', - 'application_manager.cpp', - 'core.cpp', - 'image_manager.cpp', - 'lobby_manager.cpp', - 'network_manager.cpp', - 'overlay_manager.cpp', - 'relationship_manager.cpp', - 'storage_manager.cpp', - 'store_manager.cpp', - 'types.cpp', - 'user_manager.cpp', - 'voice_manager.cpp', -) - -lib_dir = '' -dynamic_lib = '' - -if host_machine.system() == 'darwin' - dynamic_lib = 'discord_game_sdk.dylib' - if host_machine.cpu_family() == 'aarch64' - lib_dir = 'lib' / 'aarch64' - elif host_machine.cpu_family() == 'x86_64' - lib_dir = 'lib' / 'x86_64' - else - error('unsupported architecture for macos: ' + host_machine.cpu_family()) - endif - -elif host_machine.system() == 'linux' - dynamic_lib = 'discord_game_sdk.so' - if host_machine.cpu_family() == 'x86_64' - lib_dir = 'lib' / 'x86_64' - - cat = find_program('cat') - - os_release_info = run_command( - cat, - '/etc/os-release', - check: true, - ).stdout().strip().split('\n') - - linux_distro = '' - - foreach line : os_release_info - line_detail = line.split('=') - - if line_detail[0] == 'ID' - linux_distro = line_detail[1] - endif - - endforeach - - if linux_distro == '' - warning('Couldn\'t detect the linux distro') - endif - - if linux_distro == 'alpine' - error('unsupported libc for linux: musl') - endif - - else - error('unsupported architecture for linux: ' + host_machine.cpu_family()) - endif -elif host_machine.system() == 'windows' - dynamic_lib = 'discord_game_sdk.dll' - if host_machine.cpu_family() == 'x86' - lib_dir = 'lib' / 'x86' - elif host_machine.cpu_family() == 'x86_64' - lib_dir = 'lib' / 'x86_64' - else - error('unsupported architecture for windows: ' + host_machine.cpu_family()) - endif -else - error('unsupported system: ' + host_machine.system()) -endif - -c = meson.get_compiler('c') - -lib_deps = [] - -## prefrerring the static variant, if not found we need to copy the danmic lib to the libdir on installation! - -lib_name = 'discord_game_sdk' - -if c.get_id() == 'msvc' - lib_name = 'discord_game_sdk.dll' -endif - -discord_lib_c = c.find_library( - lib_name, - dirs: [meson.project_source_root() / lib_dir], - static: true, - required: false, -) - -if discord_lib_c.found() - lib_deps += discord_lib_c - - if c.get_id() == 'msvc' - - install_data( - meson.project_source_root() / lib_dir / lib_name, - install_dir: get_option('bindir'), - ) - endif - -else - lib_deps += c.find_library( - lib_name, - dirs: [meson.project_source_root() / lib_dir], - ) - - dynamic_lib_rename = dynamic_lib - - if host_machine.system() == 'linux' - dynamic_lib_rename = 'libdiscord_game_sdk_c.so' - - objdump = find_program('objdump') - - objdump_result = run_command( - objdump, - '-p', meson.project_source_root() / lib_dir / dynamic_lib, - check: true, - ).stdout().strip() - - if objdump_result.contains('SONAME') - message('discord game sdk has a SONAME') - - else - message('discord game sdk has no SONAME') - - patchelf = find_program('patchelf') - - #TODO: file issue to discord, to set SONAME - run_command( - patchelf, - '--set-soname', dynamic_lib_rename, - meson.project_source_root() / lib_dir / dynamic_lib, - check: true, - ) - - run_command( - find_program('cp'), - meson.project_source_root() / lib_dir / dynamic_lib, - meson.project_source_root() / lib_dir / dynamic_lib_rename, - check: true, - ) - - endif - endif - - install_data( - meson.project_source_root() / lib_dir / dynamic_lib, - install_dir: get_option('libdir'), - rename: [dynamic_lib_rename], - ) - -endif - -discord_lib = library( - 'discord-game-sdk', - src_files, - header_files, - include_directories: inc_dirs, - dependencies: lib_deps, - install: true, -) - -discord_dep = declare_dependency( - include_directories: inc_dirs, - version: meson.project_version(), - link_with: discord_lib, -) diff --git a/subprojects/packagefiles/discord_game_sdk/examples/cpp/meson.build b/subprojects/packagefiles/discord_game_sdk/examples/cpp/meson.build deleted file mode 100644 index f23b51ffd..000000000 --- a/subprojects/packagefiles/discord_game_sdk/examples/cpp/meson.build +++ /dev/null @@ -1,9 +0,0 @@ -executable( - 'main', - files('main.cpp'), - dependencies: discord_dep, - override_options: { - 'warning_level': '3', - 'werror': true, - }, -) diff --git a/subprojects/packagefiles/discord_game_sdk/meson.build b/subprojects/packagefiles/discord_game_sdk/meson.build deleted file mode 100644 index 6a5d66cc4..000000000 --- a/subprojects/packagefiles/discord_game_sdk/meson.build +++ /dev/null @@ -1,16 +0,0 @@ -project( - 'discord-game-sdk', - 'cpp', - 'c', - version: '3.2.1', - meson_version: '>=1.4.0', - default_options: { - 'cpp_std': ['c++17'], - }, -) - -subdir('cpp') - -if get_option('examples') - subdir('examples/cpp') -endif diff --git a/subprojects/packagefiles/discord_game_sdk/meson.options b/subprojects/packagefiles/discord_game_sdk/meson.options deleted file mode 100644 index fff7c8962..000000000 --- a/subprojects/packagefiles/discord_game_sdk/meson.options +++ /dev/null @@ -1,6 +0,0 @@ -option( - 'examples', - type: 'boolean', - value: false, - description: 'whether to built the examples', -) From 4899e96cc5b42559aac777ced59fe48ed835a21f Mon Sep 17 00:00:00 2001 From: Totto16 Date: Sun, 20 Apr 2025 18:50:42 +0200 Subject: [PATCH 03/13] feat: implement the discord workflows using the new SDK use the new social sdk, to implement the needed things this is still WIP and untested --- src/discord/core.cpp | 225 +++++++++++------- src/discord/core.hpp | 77 +++--- src/executables/game/application.cpp | 29 ++- src/executables/game/application.hpp | 4 +- src/helper/graphic_utils.cpp | 2 +- src/manager/service_provider.hpp | 4 +- src/scenes/main_menu/main_menu.cpp | 4 +- src/scenes/replay_game/replay_game.cpp | 4 +- .../single_player_game/single_player_game.cpp | 4 +- 9 files changed, 203 insertions(+), 150 deletions(-) diff --git a/src/discord/core.cpp b/src/discord/core.cpp index 6a5e32c6b..115e9194c 100644 --- a/src/discord/core.cpp +++ b/src/discord/core.cpp @@ -1,5 +1,4 @@ -#include #include #include "./core.hpp" @@ -18,133 +17,155 @@ } } -DiscordInstance::DiscordInstance(discord::Core* core) : m_core{ core }, m_current_user{ nullptr } { } - -void DiscordInstance::after_setup() { - m_core->UserManager().OnCurrentUserUpdate.Connect([this]() { - auto* user = new discord::User; // NOLINT(cppcoreguidelines-owning-memory) - this->m_core->UserManager().GetCurrentUser(user); - this->m_current_user.reset(user); - spdlog::info( - "Current user updated: {}#{}", this->m_current_user->GetUsername(), - this->m_current_user->GetDiscriminator() - ); - }); - - auto result = m_core->ActivityManager().RegisterCommand(constants::discord::platform_dependent_launch_arguments); - if (result != discord::Result::Ok) { - spdlog::warn("ActivityManager: Failed to RegisterCommand: {}", magic_enum::enum_name(result)); - }; -} - -[[nodiscard]] helper::expected DiscordInstance::initialize() { +DiscordInstance::DiscordInstance() + : m_client{}, + m_current_user{ std::move(discordpp::UserHandle::nullobj) }, + m_status{ DiscordStatus::Starting } { - discord::Core* core{}; - auto result = discord::Core::Create(constants::discord::client_id, DiscordCreateFlags_Default, &core); - if (core == nullptr) { - return helper::unexpected{ - fmt::format("Failed to instantiate discord core: {}", magic_enum::enum_name(result)) - }; - } - - - core->SetLogHook( -#if !defined(NDEBUG) - discord::LogLevel::Debug -#else - discord::LogLevel::Error -#endif - , - [](discord::LogLevel level, const char* message) { - switch (level) { - case discord::LogLevel::Error: + m_client.AddLogCallback( + [](std::string message, discordpp::LoggingSeverity severity) -> void { + switch (severity) { + case discordpp::LoggingSeverity::Error: spdlog::error("DISCORD SDK: {}", message); break; - case discord::LogLevel::Warn: + case discordpp::LoggingSeverity::Warning: spdlog::warn("DISCORD SDK: {}", message); break; - case discord::LogLevel::Info: + case discordpp::LoggingSeverity::Info: spdlog::info("DISCORD SDK: {}", message); break; - case discord::LogLevel::Debug: + case discordpp::LoggingSeverity::Verbose: spdlog::debug("DISCORD SDK: {}", message); break; + case discordpp::LoggingSeverity::None: + break; + } + }, +#if !defined(NDEBUG) + discordpp::LoggingSeverity::Verbose +#else + discordpp::LoggingSeverity::Error +#endif + ); + + + m_client.SetStatusChangedCallback( + [this](discordpp::Client::Status status, discordpp::Client::Error error, int32_t errorDetail) -> void { + if (error != discordpp::Client::Error::None) { + this->m_status = DiscordStatus::Error; + spdlog::error( + "Connection Error: {} - Details: {}", discordpp::Client::ErrorToString(error), errorDetail + ); + return; + } + + if (status == discordpp::Client::Status::Ready) { + this->m_status = DiscordStatus::Ok; + this->after_ready(); + return; } } ); - return DiscordInstance{ core }; + m_client.SetApplicationId(constants::discord::application_id); + + m_client.Connect(); +} + +void DiscordInstance::after_ready() { + + this->m_client.GetDiscordClientConnectedUser( + constants::discord::application_id, + [this](discordpp::ClientResult result, std::optional user) -> void { + if (result.Successful() and user.has_value()) { + + auto user_handle = m_client.GetUser(user->Id()); + if (not user_handle.has_value()) { + spdlog::error("Current Connected User Error: Can't get userhandle from id: {}", user->Id()); + + return; + } + + this->m_current_user = user_handle.value(); + spdlog::info("Current user updated: {}", user_handle->Username()); + + return; + } + + spdlog::error("Current Connected User Error: {}", result.ToString()); + } + ); } DiscordInstance::DiscordInstance(DiscordInstance&& old) noexcept - : m_core{ std::move(old.m_core) }, - m_current_user{ std::move(old.m_current_user) } { - old.m_core = nullptr; - old.m_current_user = nullptr; + : m_client{ std::move(old.m_client) }, + m_current_user{ std::move(old.m_current_user) }, + m_status{ old.m_status } { + old.m_client = discordpp::Client{}; + old.m_current_user = discordpp::UserHandle::nullobj; + old.m_status = DiscordStatus::Error; } DiscordInstance& DiscordInstance::operator=(DiscordInstance&& other) noexcept { if (this != &other) { - m_core = std::move(other.m_core); + m_client = std::move(other.m_client); m_current_user = std::move(other.m_current_user); + m_status = other.m_status; - other.m_core = nullptr; - other.m_current_user = nullptr; + other.m_client = discordpp::Client{}; + other.m_current_user = discordpp::UserHandle::nullobj; + other.m_status = DiscordStatus::Error; } return *this; }; DiscordInstance::~DiscordInstance() { - if (m_core != nullptr) { + if (m_client.operator bool()) { clear_activity(); + m_client.Disconnect(); } } void DiscordInstance::update() { - m_core->RunCallbacks(); + discordpp::RunCallbacks(); } void DiscordInstance::set_activity(const DiscordActivityWrapper& activity) { - m_core->ActivityManager().UpdateActivity(activity.get_raw(), [](discord::Result result) { - spdlog::info("Result to UpdateActivity: {}", magic_enum::enum_name(result)); - }); -} + auto raw_activity = activity.get_raw(); + if (not raw_activity.operator bool()) { + spdlog::error("Tried to set an invalid Discord Activity!"); + return; + } -void DiscordInstance::clear_activity(bool wait) { - bool received_callback = false; - m_core->ActivityManager().ClearActivity([&received_callback](discord::Result result) { - spdlog::info("Result to ClearActivity: {}", magic_enum::enum_name(result)); - received_callback = true; + // Update rich presence + m_client.UpdateRichPresence(raw_activity, [](discordpp::ClientResult result) { + if (result.Successful()) { + spdlog::info("Rich Presence updated successfully"); + } else { + spdlog::error("Rich Presence update failed: {}", result.ToString()); + } }); +} - using namespace std::chrono_literals; - - if (wait) { - - constexpr auto max_waittime = 1s; - - const auto start_time = std::chrono::steady_clock::now(); - while (not received_callback) { - this->update(); - std::this_thread::sleep_for(1ms); - const auto now = std::chrono::steady_clock::now(); - if (now - start_time >= max_waittime) { - break; - } - } - } +void DiscordInstance::clear_activity() { + m_client.ClearRichPresence(); } -DiscordActivityWrapper::DiscordActivityWrapper(const std::string& details, discord::ActivityType type) { - m_activity.SetDetails(details.c_str()); +DiscordActivityWrapper::DiscordActivityWrapper(const std::string& details, discordpp::ActivityTypes type) { + // NOTE: this are partial fields, that are set by the final call, do not set them manually + // https://discord.com/developers/docs/rich-presence/using-with-the-game-sdk#partial-activity-struct + // m_activity.SetName(constants::program_name); + // m_activity.SetApplicationId(constants::application_id); + + m_activity.SetDetails(details); m_activity.SetType(type); m_activity.SetSupportedPlatforms(constants::discord::supported_platforms); } @@ -152,10 +173,14 @@ DiscordActivityWrapper::DiscordActivityWrapper(const std::string& details, disco DiscordActivityWrapper& DiscordActivityWrapper::set_large_image(const std::string& text, constants::discord::ArtAsset asset) { - m_activity.GetAssets().SetLargeText(text.c_str()); + auto assets = this->get_assets(); const auto asset_key = constants::discord::get_asset_key(asset); - m_activity.GetAssets().SetLargeImage(asset_key.c_str()); + + assets.SetLargeImage(asset_key); + assets.SetLargeText(text); + + m_activity.SetAssets(assets); return *this; } @@ -163,20 +188,48 @@ DiscordActivityWrapper::set_large_image(const std::string& text, constants::disc DiscordActivityWrapper& DiscordActivityWrapper::set_small_image(const std::string& text, constants::discord::ArtAsset asset) { - m_activity.GetAssets().SetSmallText(text.c_str()); + auto assets = this->get_assets(); const auto asset_key = constants::discord::get_asset_key(asset); - m_activity.GetAssets().SetSmallImage(asset_key.c_str()); + + assets.SetSmallImage(asset_key); + assets.SetSmallText(text); + + m_activity.SetAssets(assets); + return *this; } DiscordActivityWrapper& DiscordActivityWrapper::set_details(const std::string& text) { - m_activity.SetState(text.c_str()); + m_activity.SetState(text); return *this; } -[[nodiscard]] const discord::Activity& DiscordActivityWrapper::get_raw() const { +[[nodiscard]] const discordpp::Activity& DiscordActivityWrapper::get_raw() const { return m_activity; } + + +discordpp::ActivityTimestamps DiscordActivityWrapper::get_timestamps() { + + std::optional timestamps = m_activity.Timestamps(); + + if (timestamps.has_value()) { + return timestamps.value(); + } + + return discordpp::ActivityTimestamps::nullobj; +} + + +[[nodiscard]] discordpp::ActivityAssets DiscordActivityWrapper::get_assets() { + std::optional assets = m_activity.Assets(); + + if (assets.has_value()) { + return assets.value(); + } + + return discordpp::ActivityAssets::nullobj; +} diff --git a/src/discord/core.hpp b/src/discord/core.hpp index 9d41062c8..47602203c 100644 --- a/src/discord/core.hpp +++ b/src/discord/core.hpp @@ -17,39 +17,22 @@ #endif #endif -#include + +// https://discord.com/developers/docs/discord-social-sdk/getting-started/using-c++ +#include #include #include namespace constants::discord { - constexpr auto client_id = 1220147916371394650ULL; - - //TODO(Totto): this isn't correct for all platforms and needs to be tested -#if defined(__ANDROID__) -#error "Not supported" -#elif defined(__CONSOLE__) -#error "Not supported" -#elif defined(FLATPAK_BUILD) - constexpr const char* platform_dependent_launch_arguments = - "flatpak run io.github.openbrickprotocolfoundation.oopetris --discord"; -#elif defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) - constexpr const char* platform_dependent_launch_arguments = "oopetris.exe --discord"; -#elif defined(__APPLE__) - constexpr const char* platform_dependent_launch_arguments = "TODO"; -#elif defined(__linux__) - constexpr const char* platform_dependent_launch_arguments = "oopetris --discord"; -#else -#error "Unsupported platform" -#endif - + constexpr u64 application_id = 1220147916371394650ULL; #if defined(__ANDROID__) - constexpr const std::uint32_t supported_platforms = DiscordActivitySupportedPlatformFlags_Android; + constexpr const discordpp::ActivityGamePlatforms supported_platforms = discordpp::ActivityGamePlatforms::Android; #elif defined(__CONSOLE__) #error "Not supported" #elif defined(FLATPAK_BUILD) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__) \ || defined(__APPLE__) || defined(__linux__) - constexpr const std::uint32_t supported_platforms = DiscordActivitySupportedPlatformFlags_Desktop; + constexpr const discordpp::ActivityGamePlatforms supported_platforms = discordpp::ActivityGamePlatforms::Desktop; #else #error "Unsupported platform" #endif @@ -65,12 +48,15 @@ namespace constants::discord { struct DiscordActivityWrapper { private: - discord::Activity m_activity{}; + discordpp::Activity m_activity{}; + + [[nodiscard]] discordpp::ActivityTimestamps get_timestamps(); + [[nodiscard]] discordpp::ActivityAssets get_assets(); public: //TODO(Totto): Add support for party and invites / join / invitations / spectate - OOPETRIS_GRAPHICS_EXPORTED DiscordActivityWrapper(const std::string& details, discord::ActivityType type); + OOPETRIS_GRAPHICS_EXPORTED DiscordActivityWrapper(const std::string& details, discordpp::ActivityTypes type); OOPETRIS_GRAPHICS_EXPORTED DiscordActivityWrapper& set_large_image(const std::string& text, constants::discord::ArtAsset asset); @@ -80,14 +66,18 @@ struct DiscordActivityWrapper { OOPETRIS_GRAPHICS_EXPORTED DiscordActivityWrapper& set_details(const std::string& text); + template DiscordActivityWrapper& set_start_timestamp(const std::chrono::time_point& point) { - const auto seconds_since_epoch = static_cast( - std::chrono::duration_cast(point.time_since_epoch()).count() - ); + const auto seconds_since_epoch = + static_cast(std::chrono::duration_cast(point.time_since_epoch()).count() + ); - m_activity.GetTimestamps().SetStart(seconds_since_epoch); + auto timestamps = this->get_timestamps(); + + timestamps.SetStart(seconds_since_epoch); + m_activity.SetTimestamps(timestamps); return *this; } @@ -95,30 +85,36 @@ struct DiscordActivityWrapper { template DiscordActivityWrapper& set_end_timestamp(const std::chrono::time_point& point) { - const auto seconds_since_epoch = static_cast( - std::chrono::duration_cast(point.time_since_epoch()).count() - ); + const auto seconds_since_epoch = + static_cast(std::chrono::duration_cast(point.time_since_epoch()).count() + ); + + auto timestamps = this->get_timestamps(); - m_activity.GetTimestamps().SetEnd(seconds_since_epoch); + timestamps.SetEnd(seconds_since_epoch); + m_activity.SetTimestamps(timestamps); return *this; } - OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] const discord::Activity& get_raw() const; + OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] const discordpp::Activity& get_raw() const; }; +enum class DiscordStatus : u8 { Starting = 0, Ok, Error }; + struct DiscordInstance { private: - std::unique_ptr m_core; - std::unique_ptr m_current_user; + discordpp::Client m_client; + discordpp::UserHandle m_current_user; + DiscordStatus m_status; - explicit DiscordInstance(discord::Core* core); public: - OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] static helper::expected initialize(); + OOPETRIS_GRAPHICS_EXPORTED explicit DiscordInstance(); + + OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] DiscordStatus get_status(); - OOPETRIS_GRAPHICS_EXPORTED void after_setup(); OOPETRIS_GRAPHICS_EXPORTED DiscordInstance(DiscordInstance&& old) noexcept; @@ -130,5 +126,6 @@ struct DiscordInstance { OOPETRIS_GRAPHICS_EXPORTED void set_activity(const DiscordActivityWrapper& activity); private: - void clear_activity(bool wait = true); + void after_ready(); + void clear_activity(); }; diff --git a/src/executables/game/application.cpp b/src/executables/game/application.cpp index 605b6a5eb..cc0fc5d16 100644 --- a/src/executables/game/application.cpp +++ b/src/executables/game/application.cpp @@ -428,10 +428,21 @@ void Application::update() { } } -#if defined(_HAVE_DISCORD_SDK) +#if defined(_HAVE_DISCORD_SOCIAL_SDK) if (m_discord_instance.has_value()) { - m_discord_instance->update(); + + switch (m_discord_instance->get_status()) { + case DiscordStatus::Starting: + break; + case DiscordStatus::Error: + m_discord_instance = std::nullopt; + spdlog::warn("Error initializing the discord instance, it might not be running, destroying client!"); + break; + case DiscordStatus::Ok: + m_discord_instance->update(); + break; + } } #endif @@ -489,17 +500,9 @@ void Application::initialize() { ); #endif -#if defined(_HAVE_DISCORD_SDK) +#if defined(_HAVE_DISCORD_SOCIAL_SDK) if (current_settings.discord) { - auto discord_instance = DiscordInstance::initialize(); - if (not discord_instance.has_value()) { - spdlog::warn( - "Error initializing the discord instance, it might not be running: {}", discord_instance.error() - ); - } else { - m_discord_instance = std::move(discord_instance.value()); - m_discord_instance->after_setup(); - } + m_discord_instance = DiscordInstance{}; } #endif @@ -587,7 +590,7 @@ void Application::load_resources() { } } -#if defined(_HAVE_DISCORD_SDK) +#if defined(_HAVE_DISCORD_SOCIAL_SDK) [[nodiscard]] std::optional& Application::discord_instance() { return m_discord_instance; diff --git a/src/executables/game/application.hpp b/src/executables/game/application.hpp index 6eb3a39da..0d25a0e6c 100644 --- a/src/executables/game/application.hpp +++ b/src/executables/game/application.hpp @@ -120,7 +120,7 @@ struct Application final : public EventListener, public ServiceProvider { std::unique_ptr m_time_info; std::unique_ptr m_loading_info; -#if defined(_HAVE_DISCORD_SDK) +#if defined(_HAVE_DISCORD_SOCIAL_SDK) std::optional m_discord_instance{ std::nullopt }; #endif @@ -196,7 +196,7 @@ struct Application final : public EventListener, public ServiceProvider { [[nodiscard]] const std::unique_ptr& api() const override; -#if defined(_HAVE_DISCORD_SDK) +#if defined(_HAVE_DISCORD_SOCIAL_SDK) [[nodiscard]] std::optional& discord_instance() override; [[nodiscard]] const std::optional& discord_instance() const override; diff --git a/src/helper/graphic_utils.cpp b/src/helper/graphic_utils.cpp index 2bafe4c36..2e7c20952 100644 --- a/src/helper/graphic_utils.cpp +++ b/src/helper/graphic_utils.cpp @@ -21,7 +21,7 @@ std::vector utils::supported_features() { features.emplace_back("file dialogs"); #endif -#if defined(_HAVE_DISCORD_SDK) +#if defined(_HAVE_DISCORD_SOCIAL_SDK) features.emplace_back("discord integration"); #endif diff --git a/src/manager/service_provider.hpp b/src/manager/service_provider.hpp index 6c98aad35..8bb3d375e 100644 --- a/src/manager/service_provider.hpp +++ b/src/manager/service_provider.hpp @@ -3,7 +3,7 @@ #include "manager/event_dispatcher.hpp" -#if defined(_HAVE_DISCORD_SDK) && !defined(_OOPETRIS_RECORDING_UTILITY) +#if defined(_HAVE_DISCORD_SOCIAL_SDK) && !defined(_OOPETRIS_RECORDING_UTILITY) #include "discord/core.hpp" @@ -64,7 +64,7 @@ struct ServiceProvider { [[nodiscard]] virtual const std::unique_ptr& api() const = 0; -#if defined(_HAVE_DISCORD_SDK) && !defined(_OOPETRIS_RECORDING_UTILITY) +#if defined(_HAVE_DISCORD_SOCIAL_SDK) && !defined(_OOPETRIS_RECORDING_UTILITY) [[nodiscard]] virtual std::optional& discord_instance() = 0; [[nodiscard]] virtual const std::optional& discord_instance() const = 0; diff --git a/src/scenes/main_menu/main_menu.cpp b/src/scenes/main_menu/main_menu.cpp index 59aebd0dc..7b6735835 100644 --- a/src/scenes/main_menu/main_menu.cpp +++ b/src/scenes/main_menu/main_menu.cpp @@ -85,11 +85,11 @@ namespace scenes { ); -#if defined(_HAVE_DISCORD_SDK) +#if defined(_HAVE_DISCORD_SOCIAL_SDK) if (auto& discord_instance = service_provider->discord_instance(); discord_instance.has_value()) { discord_instance->set_activity( - DiscordActivityWrapper("Selecting playmode", discord::ActivityType::Playing) + DiscordActivityWrapper("Selecting playmode", discordpp::ActivityTypes::Playing) .set_large_image("Playing OOPetris", constants::discord::ArtAsset::Logo) .set_start_timestamp(std::chrono::system_clock::now()) ); diff --git a/src/scenes/replay_game/replay_game.cpp b/src/scenes/replay_game/replay_game.cpp index 75f0a2df8..440a769bb 100644 --- a/src/scenes/replay_game/replay_game.cpp +++ b/src/scenes/replay_game/replay_game.cpp @@ -53,11 +53,11 @@ namespace scenes { } -#if defined(_HAVE_DISCORD_SDK) +#if defined(_HAVE_DISCORD_SOCIAL_SDK) if (auto& discord_instance = service_provider->discord_instance(); discord_instance.has_value()) { discord_instance->set_activity( - DiscordActivityWrapper("Replaying a game", discord::ActivityType::Playing) + DiscordActivityWrapper("Replaying a game", discordpp::ActivityTypes::Playing) .set_large_image("Playing OOPetris", constants::discord::ArtAsset::Logo) .set_start_timestamp(std::chrono::system_clock::now()) ); diff --git a/src/scenes/single_player_game/single_player_game.cpp b/src/scenes/single_player_game/single_player_game.cpp index 9c35e66ff..afcfd8320 100644 --- a/src/scenes/single_player_game/single_player_game.cpp +++ b/src/scenes/single_player_game/single_player_game.cpp @@ -48,11 +48,11 @@ namespace scenes { ); -#if defined(_HAVE_DISCORD_SDK) +#if defined(_HAVE_DISCORD_SOCIAL_SDK) if (auto& discord_instance = service_provider->discord_instance(); discord_instance.has_value()) { discord_instance->set_activity( - DiscordActivityWrapper("Playing a single-player game", discord::ActivityType::Playing) + DiscordActivityWrapper("Playing a single-player game", discordpp::ActivityTypes::Playing) .set_large_image("Playing OOPetris", constants::discord::ArtAsset::Logo) .set_start_timestamp(std::chrono::system_clock::now()) ); From ae05329b156c7e94a38e3586469aa836fcb91f61 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Thu, 13 Mar 2025 02:07:33 +0100 Subject: [PATCH 04/13] fix: make clang-20 warning dissapear - make a private field pseudo used in normal build, it is really used in other builds e.g. emscripten --- src/manager/settings_manager.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/manager/settings_manager.cpp b/src/manager/settings_manager.cpp index 7e15ca2e5..77a5e7e2a 100644 --- a/src/manager/settings_manager.cpp +++ b/src/manager/settings_manager.cpp @@ -20,6 +20,8 @@ namespace { SettingsManager::SettingsManager(ServiceProvider* service_provider) : m_service_provider{ service_provider } { + // To stop clang about complaining + UNUSED(m_service_provider); #if defined(__EMSCRIPTEN__) const auto content = m_service_provider->web_context().local_storage().get_item(settings_key); From 670ca2c932086a4fb863bd39981b125a67d03cd8 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Sun, 20 Apr 2025 19:01:27 +0200 Subject: [PATCH 05/13] fix: correctly implement the wrap for the discord social sdk - correctly build the cpp library --- subprojects/packagefiles/discord_social_sdk/meson.build | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/subprojects/packagefiles/discord_social_sdk/meson.build b/subprojects/packagefiles/discord_social_sdk/meson.build index a2dd22542..875ef2caa 100644 --- a/subprojects/packagefiles/discord_social_sdk/meson.build +++ b/subprojects/packagefiles/discord_social_sdk/meson.build @@ -86,9 +86,10 @@ discord_partner_sdk = c.find_library( required: true, ) -discord_lib = library( - 'discord_partner_sdk', +discord_social_sdk_lib = library( + 'discord_social_sdk', header_files, + cpp_args: ['-DDISCORDPP_IMPLEMENTATION'], include_directories: inc_dirs, dependencies: discord_partner_sdk, install: true, @@ -97,7 +98,7 @@ discord_lib = library( discord_social_sdk_dep = declare_dependency( include_directories: inc_dirs, version: meson.project_version(), - link_with: discord_lib, + link_with: discord_social_sdk_lib, ) meson.override_dependency('discord-social-sdk', discord_social_sdk_dep) From 3778769e3c96eb43ef8010f4955c63cf38200182 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Mon, 21 Apr 2025 01:19:56 +0200 Subject: [PATCH 06/13] fix: fix clang-tidy warnings in discord code --- src/discord/core.cpp | 7 +++---- src/discord/core.hpp | 8 +++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/discord/core.cpp b/src/discord/core.cpp index 115e9194c..091f2ccce 100644 --- a/src/discord/core.cpp +++ b/src/discord/core.cpp @@ -18,8 +18,7 @@ } DiscordInstance::DiscordInstance() - : m_client{}, - m_current_user{ std::move(discordpp::UserHandle::nullobj) }, + : m_current_user{ discordpp::UserHandle::nullobj }, m_status{ DiscordStatus::Starting } { m_client.AddLogCallback( @@ -214,7 +213,7 @@ DiscordActivityWrapper& DiscordActivityWrapper::set_details(const std::string& t discordpp::ActivityTimestamps DiscordActivityWrapper::get_timestamps() { - std::optional timestamps = m_activity.Timestamps(); + std::optional timestamps = this->m_activity.Timestamps(); if (timestamps.has_value()) { return timestamps.value(); @@ -225,7 +224,7 @@ discordpp::ActivityTimestamps DiscordActivityWrapper::get_timestamps() { [[nodiscard]] discordpp::ActivityAssets DiscordActivityWrapper::get_assets() { - std::optional assets = m_activity.Assets(); + std::optional assets = this->m_activity.Assets(); if (assets.has_value()) { return assets.value(); diff --git a/src/discord/core.hpp b/src/discord/core.hpp index 47602203c..b40141aeb 100644 --- a/src/discord/core.hpp +++ b/src/discord/core.hpp @@ -18,9 +18,7 @@ #endif -// https://discord.com/developers/docs/discord-social-sdk/getting-started/using-c++ #include -#include #include namespace constants::discord { @@ -115,14 +113,18 @@ struct DiscordInstance { OOPETRIS_GRAPHICS_EXPORTED [[nodiscard]] DiscordStatus get_status(); - OOPETRIS_GRAPHICS_EXPORTED DiscordInstance(DiscordInstance&& old) noexcept; OOPETRIS_GRAPHICS_EXPORTED DiscordInstance& operator=(DiscordInstance&& other) noexcept; + OOPETRIS_GRAPHICS_EXPORTED DiscordInstance(DiscordInstance& old) noexcept = delete; + + OOPETRIS_GRAPHICS_EXPORTED DiscordInstance& operator=(const DiscordInstance& other) noexcept = delete; + OOPETRIS_GRAPHICS_EXPORTED ~DiscordInstance(); OOPETRIS_GRAPHICS_EXPORTED void update(); + OOPETRIS_GRAPHICS_EXPORTED void set_activity(const DiscordActivityWrapper& activity); private: From 9db46bf9446ab29a2b223738315be025bbba9a19 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Mon, 21 Apr 2025 01:21:10 +0200 Subject: [PATCH 07/13] fix: remove patchelf relevant code - after migrating to the new discord social SDk, no patchelf is needed anymore, as the SONAME is set correctly --- .github/workflows/musl.yml | 2 +- ...b.openbrickprotocolfoundation.oopetris.yml | 19 ------------------- 2 files changed, 1 insertion(+), 20 deletions(-) diff --git a/.github/workflows/musl.yml b/.github/workflows/musl.yml index 927f47859..70d7f2813 100644 --- a/.github/workflows/musl.yml +++ b/.github/workflows/musl.yml @@ -32,7 +32,7 @@ jobs: - name: Install dependencies run: | apk update - apk add wget meson ca-certificates samurai git openssl-dev openssl pkgconf sdl2 sdl2-dev sdl2_mixer-dev sdl2_ttf-dev sdl2_mixer-dev sdl2_image-dev dbus-dev patchelf ${{ ( matrix.config.use-clang == true ) && 'clang19' || 'gcc g++' }} ${{ ( matrix.config.use-clang == true && matrix.config.use-clang_stdlib == true ) && 'libc++ libc++-dev llvm-libunwind llvm-libunwind-dev ' || '' }} + apk add wget meson ca-certificates samurai git openssl-dev openssl pkgconf sdl2 sdl2-dev sdl2_mixer-dev sdl2_ttf-dev sdl2_mixer-dev sdl2_image-dev dbus-dev ${{ ( matrix.config.use-clang == true ) && 'clang19' || 'gcc g++' }} ${{ ( matrix.config.use-clang == true && matrix.config.use-clang_stdlib == true ) && 'libc++ libc++-dev llvm-libunwind llvm-libunwind-dev ' || '' }} echo "CC=${{ matrix.config.use-clang == true && 'clang-19' || 'gcc' }}" >> "$GITHUB_ENV" echo "CXX=${{ matrix.config.use-clang == true && 'clang++-19' || 'g++' }}" >> "$GITHUB_ENV" diff --git a/io.github.openbrickprotocolfoundation.oopetris.yml b/io.github.openbrickprotocolfoundation.oopetris.yml index 6e318b8e1..414010e34 100644 --- a/io.github.openbrickprotocolfoundation.oopetris.yml +++ b/io.github.openbrickprotocolfoundation.oopetris.yml @@ -4,18 +4,6 @@ runtime-version: '24.08' sdk: org.freedesktop.Sdk command: oopetris modules: - #TODO: remove after discord sets SONAME - - name: patchelf - buildsystem: simple - sources: - - type: archive - url: https://github.com/NixOS/patchelf/releases/download/0.18.0/patchelf-0.18.0.tar.gz - sha256: 64de10e4c6b8b8379db7e87f58030f336ea747c0515f381132e810dbf84a86e7 - build-commands: - - ./configure --prefix=/app - - make - - make install - - name: OOPetris buildsystem: meson config-opts: @@ -42,13 +30,6 @@ cleanup: - /include - /lib/pkgconfig - /share/pkgconfig - ## all for the patchelf executable - #TODO: remove after removing patchelf build - - /bin/patchelf - - /lib/debug - - /share/doc - - /share/man - - /share/zsh finish-args: - --socket=x11 From 836560a180b4d3f512001ab17f4d203febc0e186 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Mon, 21 Apr 2025 01:32:07 +0200 Subject: [PATCH 08/13] fix: fix the discord social sdk c++ library --- subprojects/packagefiles/discord_social_sdk/impl.cpp | 3 +++ subprojects/packagefiles/discord_social_sdk/meson.build | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 subprojects/packagefiles/discord_social_sdk/impl.cpp diff --git a/subprojects/packagefiles/discord_social_sdk/impl.cpp b/subprojects/packagefiles/discord_social_sdk/impl.cpp new file mode 100644 index 000000000..92cec4923 --- /dev/null +++ b/subprojects/packagefiles/discord_social_sdk/impl.cpp @@ -0,0 +1,3 @@ + +#define DISCORDPP_IMPLEMENTATION +#include "./discordpp.h" diff --git a/subprojects/packagefiles/discord_social_sdk/meson.build b/subprojects/packagefiles/discord_social_sdk/meson.build index 875ef2caa..4ac396c9e 100644 --- a/subprojects/packagefiles/discord_social_sdk/meson.build +++ b/subprojects/packagefiles/discord_social_sdk/meson.build @@ -86,10 +86,10 @@ discord_partner_sdk = c.find_library( required: true, ) -discord_social_sdk_lib = library( +## NOTE: this is a static library, as it doesn't export the symbols correctly, so on windows the dll would fail to link +discord_social_sdk_lib = static_library( 'discord_social_sdk', - header_files, - cpp_args: ['-DDISCORDPP_IMPLEMENTATION'], + files('impl.cpp'), include_directories: inc_dirs, dependencies: discord_partner_sdk, install: true, From 794952ef92ea73a45343719635f546dbacbdb304 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Mon, 21 Apr 2025 01:36:47 +0200 Subject: [PATCH 09/13] fix: implement missing method on DiscordInstance --- src/discord/core.cpp | 4 ++++ src/discord/core.hpp | 1 + 2 files changed, 5 insertions(+) diff --git a/src/discord/core.cpp b/src/discord/core.cpp index 091f2ccce..5ca72f895 100644 --- a/src/discord/core.cpp +++ b/src/discord/core.cpp @@ -128,6 +128,10 @@ DiscordInstance::~DiscordInstance() { } } +[[nodiscard]] DiscordStatus DiscordInstance::get_status() { + return m_status; +} + void DiscordInstance::update() { discordpp::RunCallbacks(); } diff --git a/src/discord/core.hpp b/src/discord/core.hpp index b40141aeb..173d3139a 100644 --- a/src/discord/core.hpp +++ b/src/discord/core.hpp @@ -49,6 +49,7 @@ struct DiscordActivityWrapper { discordpp::Activity m_activity{}; [[nodiscard]] discordpp::ActivityTimestamps get_timestamps(); + [[nodiscard]] discordpp::ActivityAssets get_assets(); public: From bf1c56d7fab61fa660f50866c6e57d842bed41a9 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Mon, 21 Apr 2025 02:10:47 +0200 Subject: [PATCH 10/13] fix: fix clang-tidy warnings --- src/discord/core.cpp | 10 +++++----- src/discord/core.hpp | 4 ++-- src/executables/game/application.cpp | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/discord/core.cpp b/src/discord/core.cpp index 5ca72f895..41f83fbba 100644 --- a/src/discord/core.cpp +++ b/src/discord/core.cpp @@ -49,11 +49,11 @@ DiscordInstance::DiscordInstance() m_client.SetStatusChangedCallback( - [this](discordpp::Client::Status status, discordpp::Client::Error error, int32_t errorDetail) -> void { + [this](discordpp::Client::Status status, discordpp::Client::Error error, int32_t error_detail) -> void { if (error != discordpp::Client::Error::None) { this->m_status = DiscordStatus::Error; spdlog::error( - "Connection Error: {} - Details: {}", discordpp::Client::ErrorToString(error), errorDetail + "Connection Error: {} - Details: {}", discordpp::Client::ErrorToString(error), error_detail ); return; } @@ -75,7 +75,7 @@ void DiscordInstance::after_ready() { this->m_client.GetDiscordClientConnectedUser( constants::discord::application_id, - [this](discordpp::ClientResult result, std::optional user) -> void { + [this](const discordpp::ClientResult& result, std::optional user) -> void { if (result.Successful() and user.has_value()) { auto user_handle = m_client.GetUser(user->Id()); @@ -139,7 +139,7 @@ void DiscordInstance::update() { void DiscordInstance::set_activity(const DiscordActivityWrapper& activity) { - auto raw_activity = activity.get_raw(); + const auto& raw_activity = activity.get_raw(); if (not raw_activity.operator bool()) { spdlog::error("Tried to set an invalid Discord Activity!"); @@ -147,7 +147,7 @@ void DiscordInstance::set_activity(const DiscordActivityWrapper& activity) { } // Update rich presence - m_client.UpdateRichPresence(raw_activity, [](discordpp::ClientResult result) { + m_client.UpdateRichPresence(raw_activity, [](const discordpp::ClientResult& result) { if (result.Successful()) { spdlog::info("Rich Presence updated successfully"); } else { diff --git a/src/discord/core.hpp b/src/discord/core.hpp index 173d3139a..c440a4bd2 100644 --- a/src/discord/core.hpp +++ b/src/discord/core.hpp @@ -46,7 +46,7 @@ namespace constants::discord { struct DiscordActivityWrapper { private: - discordpp::Activity m_activity{}; + discordpp::Activity m_activity; [[nodiscard]] discordpp::ActivityTimestamps get_timestamps(); @@ -124,7 +124,7 @@ struct DiscordInstance { OOPETRIS_GRAPHICS_EXPORTED ~DiscordInstance(); - OOPETRIS_GRAPHICS_EXPORTED void update(); + OOPETRIS_GRAPHICS_EXPORTED static void update(); OOPETRIS_GRAPHICS_EXPORTED void set_activity(const DiscordActivityWrapper& activity); diff --git a/src/executables/game/application.cpp b/src/executables/game/application.cpp index cc0fc5d16..34fcacf40 100644 --- a/src/executables/game/application.cpp +++ b/src/executables/game/application.cpp @@ -382,7 +382,8 @@ void Application::update() { }, [this](const scenes::Scene::Push& push) { spdlog::info("pushing back scene {}", magic_enum::enum_name(push.target_scene)); - m_scene_stack.push_back(scenes::create_scene(*this, push.target_scene, push.layout) + m_scene_stack.push_back( + scenes::create_scene(*this, push.target_scene, push.layout) ); }, [this](scenes::Scene::RawPush& raw_push) { @@ -433,14 +434,13 @@ void Application::update() { if (m_discord_instance.has_value()) { switch (m_discord_instance->get_status()) { - case DiscordStatus::Starting: - break; case DiscordStatus::Error: m_discord_instance = std::nullopt; spdlog::warn("Error initializing the discord instance, it might not be running, destroying client!"); break; + case DiscordStatus::Starting: case DiscordStatus::Ok: - m_discord_instance->update(); + DiscordInstance::update(); break; } } From c8e8f83674acb74a571dc0a481755b447c9ee29d Mon Sep 17 00:00:00 2001 From: Totto16 Date: Mon, 21 Apr 2025 02:19:45 +0200 Subject: [PATCH 11/13] fix: fix discord social sdk compilation on mingw --- subprojects/discord-social-sdk.wrap | 1 + .../discord_social_sdk-1.1.8318_mingw.diff | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 subprojects/packagefiles/discord_social_sdk-1.1.8318_mingw.diff diff --git a/subprojects/discord-social-sdk.wrap b/subprojects/discord-social-sdk.wrap index 633454dd0..7f2795fbf 100644 --- a/subprojects/discord-social-sdk.wrap +++ b/subprojects/discord-social-sdk.wrap @@ -4,6 +4,7 @@ source_url = https://oopetris.totto.lt/static/assets/discord/DiscordSocialSdk-1. source_filename = DiscordSocialSdk-1.1.8318.zip source_hash = e78ac5c6462d74917b74d2511ed88876f59dbe00d31186590a98ea7953099e31 patch_directory = discord_social_sdk +diff_files = discord_social_sdk-1.1.8318_mingw.diff [provide] discord-social-sdk = discord_social_sdk_dep diff --git a/subprojects/packagefiles/discord_social_sdk-1.1.8318_mingw.diff b/subprojects/packagefiles/discord_social_sdk-1.1.8318_mingw.diff new file mode 100644 index 000000000..0164a4555 --- /dev/null +++ b/subprojects/packagefiles/discord_social_sdk-1.1.8318_mingw.diff @@ -0,0 +1,14 @@ +diff --git a/include/cdiscord.h b/include/cdiscord.h +index 534a85f..2ba5d7f 100644 +--- a/include/cdiscord.h ++++ b/include/cdiscord.h +@@ -10,7 +10,9 @@ + #ifndef DISCORD_API + #if defined(_WIN32) + #define DISCORD_API __declspec(dllexport) ++#if ! defined(__MINGW32__) + #pragma warning(disable : 4251) ++#endif + #else + #define DISCORD_API __attribute__((visibility("default"))) + #endif From 4d035d4594eaa653e9936421bb94fe03efbdbc66 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Mon, 21 Apr 2025 02:20:58 +0200 Subject: [PATCH 12/13] fix: correctly format application.cpp according to clang-format-19 --- src/executables/game/application.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/executables/game/application.cpp b/src/executables/game/application.cpp index 34fcacf40..600e3f0b0 100644 --- a/src/executables/game/application.cpp +++ b/src/executables/game/application.cpp @@ -382,8 +382,7 @@ void Application::update() { }, [this](const scenes::Scene::Push& push) { spdlog::info("pushing back scene {}", magic_enum::enum_name(push.target_scene)); - m_scene_stack.push_back( - scenes::create_scene(*this, push.target_scene, push.layout) + m_scene_stack.push_back(scenes::create_scene(*this, push.target_scene, push.layout) ); }, [this](scenes::Scene::RawPush& raw_push) { From f27dcd13a1d5825665444a3bbd0166b481219682 Mon Sep 17 00:00:00 2001 From: Totto16 Date: Mon, 21 Apr 2025 02:32:05 +0200 Subject: [PATCH 13/13] fix: fix discord wrappers for getting assets and timestamps --- src/discord/core.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/discord/core.cpp b/src/discord/core.cpp index 41f83fbba..2427367bf 100644 --- a/src/discord/core.cpp +++ b/src/discord/core.cpp @@ -223,7 +223,7 @@ discordpp::ActivityTimestamps DiscordActivityWrapper::get_timestamps() { return timestamps.value(); } - return discordpp::ActivityTimestamps::nullobj; + return discordpp::ActivityTimestamps(); } @@ -234,5 +234,5 @@ discordpp::ActivityTimestamps DiscordActivityWrapper::get_timestamps() { return assets.value(); } - return discordpp::ActivityAssets::nullobj; + return discordpp::ActivityAssets(); }