From 0e0737c53cd47390eb6444ec9f612d51196661c9 Mon Sep 17 00:00:00 2001 From: EinTim23 Date: Mon, 4 Nov 2024 15:32:46 +0100 Subject: [PATCH] fixed unicode support --- src/backends/windows.cpp | 17 +++++++++-------- src/main.cpp | 1 + src/utils.hpp | 34 +++++++++++----------------------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/backends/windows.cpp b/src/backends/windows.cpp index 248f070..0cd1e1c 100644 --- a/src/backends/windows.cpp +++ b/src/backends/windows.cpp @@ -4,6 +4,7 @@ #include #include +#include #include "../backend.hpp" #include "../utils.hpp" @@ -11,11 +12,11 @@ using namespace winrt; using namespace Windows::Media::Control; using namespace Windows::Storage::Streams; - -// a winrt::hstring is just an extended std::wstring, so we can use std::strings built in conversion. +#define EM_DASH "\xE2\x80\x94" +// codecvt is deprecated, but there is no good portable way to do this, I could technically use the winapi as this is the windows backend tho std::string toStdString(winrt::hstring in) { - std::string converted = std::string(in.begin(), in.end()); - return converted; + std::wstring_convert> converter; + return converter.to_bytes(in.c_str()); } std::shared_ptr backend::getMediaInformation() { @@ -54,9 +55,9 @@ std::shared_ptr backend::getMediaInformation() { if (artist == "") artist = toStdString(mediaProperties.AlbumArtist()); // Needed for some apps - if (artist.find("\x14") != std::string::npos) { - albumName = artist.substr(artist.find("\x14") + 1); - artist = artist.substr(0, artist.find("\x14")); + if (artist.find(EM_DASH) != std::string::npos) { + albumName = artist.substr(artist.find(EM_DASH) + 3); + artist = artist.substr(0, artist.find(EM_DASH)); utils::trim(artist); utils::trim(albumName); } @@ -66,5 +67,5 @@ std::shared_ptr backend::getMediaInformation() { toStdString(mediaProperties.Title()), artist, albumName, toStdString(currentSession.SourceAppUserModelId()), thumbnailData, endTime, elapsedTime); } - +#undef EM_DASH #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 54516c4..8b1673c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -30,6 +30,7 @@ void handleRPCTasks() { break; std::this_thread::sleep_for(std::chrono::seconds(1)); } + Discord_Shutdown(); handleRPCTasks(); // this could theoretically cause a stack overflow if discord is restarted often enough } diff --git a/src/utils.hpp b/src/utils.hpp index 812cd7d..a80ab5a 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -53,30 +53,18 @@ namespace utils { } inline std::string urlEncode(std::string str) { - std::string new_str = ""; - char c; - int ic; - const char* chars = str.c_str(); - char bufHex[10]; - int len = strlen(chars); + std::ostringstream encoded; + encoded << std::hex << std::uppercase; - for (int i = 0; i < len; i++) { - c = chars[i]; - ic = c; - if (c == ' ') - new_str += '+'; - else if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') - new_str += c; - else { - snprintf(bufHex, sizeof(bufHex), "%X", c); - if (ic < 16) - new_str += "%0"; - else - new_str += "%"; - new_str += bufHex; + for (unsigned char c : str) { + if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') { + encoded << c; + } else { + encoded << '%' << std::setw(2) << std::setfill('0') << static_cast(c); } } - return new_str; + + return encoded.str(); } inline size_t curlWriteCallback(char* contents, size_t size, size_t nmemb, void* userp) { @@ -144,8 +132,8 @@ namespace utils { inline App getApp(std::string processName) { auto apps = getAllApps(); for (auto app : apps) { - for(auto procName : app.processNames) { - if(procName == processName) + for (auto procName : app.processNames) { + if (procName == processName) return app; } }