From 98248d59fd04c4e55ee6f192740c2bebf1552799 Mon Sep 17 00:00:00 2001 From: boludoz Date: Wed, 4 Oct 2023 03:08:22 -0300 Subject: [PATCH] Fixed unicode conversion on Windows almost --- src/common/fs/fs_util.cpp | 90 ++++++++------------------------------- src/common/fs/fs_util.h | 9 ---- src/yuzu/main.cpp | 9 +++- 3 files changed, 25 insertions(+), 83 deletions(-) diff --git a/src/common/fs/fs_util.cpp b/src/common/fs/fs_util.cpp index 77c9b01899..cfb625d5aa 100644 --- a/src/common/fs/fs_util.cpp +++ b/src/common/fs/fs_util.cpp @@ -4,20 +4,27 @@ #include #include #include +#ifdef _WIN32 +#include // For MultiByteToWideChar (cannon UTF-8 with Windows) +#endif + +/* TODO: Add support for 'https://github.com/unicode-org/icu' to handle UTF-8. + * This will be necessary for support UTF-8 cannon on Linux and macOS conversions. + */ #include "common/fs/fs_util.h" #include "common/polyfill_ranges.h" namespace Common::FS { -std::u8string ToU8String(std::string_view string) { - return std::u8string{reinterpret_cast(string.data())}; -} - std::u8string ToU8String(std::wstring_view w_string) { return std::u8string{reinterpret_cast(w_string.data())}; } +std::u8string ToU8String(std::string_view string) { + return std::u8string{string.begin(), string.end()}; +} + std::u8string BufferToU8String(std::span buffer) { return std::u8string{buffer.begin(), std::ranges::find(buffer, u8{0})}; } @@ -27,7 +34,14 @@ std::u8string_view BufferToU8StringView(std::span buffer) { } std::wstring ToWString(std::u8string_view utf8_string) { +#ifdef _WIN32 + int size_needed = MultiByteToWideChar(CP_UTF8, 0, reinterpret_cast(utf8_string.data()), -1, NULL, 0); + std::wstring wstr(size_needed, 0); + MultiByteToWideChar(CP_UTF8, 0, reinterpret_cast(utf8_string.data()), -1, &wstr[0], size_needed); + return wstr; +#else return std::wstring{utf8_string.begin(), utf8_string.end()}; +#endif } std::string ToUTF8String(std::u8string_view u8_string) { @@ -46,72 +60,4 @@ std::string PathToUTF8String(const std::filesystem::path& path) { return ToUTF8String(path.u8string()); } -/* -std::u8string UTF8FilenameSantizer(std::u8string u8filename) { - std::u8string u8path_santized = u8filename; - - size_t eSizeSanitized = - u8path_santized.size(); // Cambiado a size_t para coincidir con el tipo de i - - // Special case for ":", for example: 'Pepe: La secuela' --> 'Pepe - La - // secuela' or 'Pepe : La secuela' --> 'Pepe - La secuela' - for (size_t i = 0; i < eSizeSanitized; i++) { - - switch (u8path_santized[i]) { - case u8':': - if (i == 0 || i == eSizeSanitized - 1) { - u8path_santized.replace(i, 1, u8"_"); - } else if (u8path_santized[i - 1] == u8' ') { - u8path_santized.replace(i, 1, u8"-"); - } else { - u8path_santized.replace(i, 1, u8" -"); - eSizeSanitized++; - } - break; - case u8'\\': - [[fallthrough]]; - case u8'/': - [[fallthrough]]; - case u8'*': - [[fallthrough]]; - case u8'?': - [[fallthrough]]; - case u8'\"': - [[fallthrough]]; - case u8'<': - [[fallthrough]]; - case u8'>': - [[fallthrough]]; - case u8'|': - [[fallthrough]]; - case u8'\0': - u8path_santized.replace(i, 1, u8"_"); - break; - default: - break; - } - } - - // Delete duplicated spaces || Delete duplicated dots (MacOS i think) - for (size_t i = 0; i < eSizeSanitized; i++) { - if ((u8path_santized[i] == u8' ' && u8path_santized[i + 1] == u8' ') || - (u8path_santized[i] == u8'.' && u8path_santized[i + 1] == u8'.')) { - u8path_santized.erase(i, 1); - i--; - } - } - - // Delete all spaces and dots at the end (Windows almost) - while (u8path_santized.back() == u8' ' || u8path_santized.back() == u8'.') { - u8path_santized.pop_back(); - } - - if (u8path_santized.empty()) { - return u8""; - } - - return u8path_santized; -} -*/ - } // namespace Common::FS diff --git a/src/common/fs/fs_util.h b/src/common/fs/fs_util.h index afd4de46a4..21ecbeb5e7 100644 --- a/src/common/fs/fs_util.h +++ b/src/common/fs/fs_util.h @@ -99,13 +99,4 @@ concept IsChar = std::same_as; */ [[nodiscard]] std::string PathToUTF8String(const std::filesystem::path& path); -/** - * Fix filename (remove invalid characters) - * @param dirty UTF-8 encoded - * - * @returns UTF-8 encoded fixed - * - */ -// [[nodiscard]] std::u8string UTF8FilenameSantizer(std::u8string &u8filename); - } // namespace Common::FS \ No newline at end of file diff --git a/src/yuzu/main.cpp b/src/yuzu/main.cpp index 5a26830086..53ad453b4c 100644 --- a/src/yuzu/main.cpp +++ b/src/yuzu/main.cpp @@ -3025,9 +3025,14 @@ void GMainWindow::OnGameListCreateShortcut(u64 program_id, const QString& game_p #endif // __linux__ || __FreeBSD__ || _WIN32 #if defined(__linux__) || defined(__FreeBSD__) - std::string arguments = - fmt::format("{}-g \"{:s}\"", (result == QMessageBox::Yes) ? "-f " : "", game_path); + std::string arguments; + if (result == QMessageBox::Yes) { + arguments = fmt::format("-f -g \"{:s}\"", game_path); + + } else { + arguments = fmt::format("-g \"{:s}\"", game_path); + } const std::string comment = tr("Start %1 with the yuzu Emulator").arg(QString::fromStdString(title)).toStdString();