From e9bd34f7da2feb9b8fca6fbc2c0844cad7929cbd Mon Sep 17 00:00:00 2001 From: fearlessTobi Date: Sun, 7 Apr 2019 15:58:42 +0200 Subject: [PATCH] Address remaining review comments --- src/citra_qt/applets/mii_selector.cpp | 22 ++++++++++++++++++---- src/core/file_sys/archive_backend.h | 2 +- src/core/frontend/applets/mii_selector.h | 2 +- src/core/hle/applets/mii_selector.cpp | 19 +++++++++++++++---- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/citra_qt/applets/mii_selector.cpp b/src/citra_qt/applets/mii_selector.cpp index c7723dd42..b51a33fb7 100644 --- a/src/citra_qt/applets/mii_selector.cpp +++ b/src/citra_qt/applets/mii_selector.cpp @@ -9,10 +9,25 @@ #include #include "citra_qt/applets/mii_selector.h" #include "common/file_util.h" +#include "common/string_util.h" #include "core/file_sys/archive_extsavedata.h" #include "core/file_sys/file_backend.h" #include "core/hle/service/ptm/ptm.h" +/** + * Converts a UTF-16 text in a container to a UTF-8 std::string. + */ +template +std::string TextFromBuffer(const T& text) { + const auto text_end = std::find(text.begin(), text.end(), u'\0'); + const std::size_t text_size = std::distance(text.begin(), text_end); + std::u16string buffer(text_size, 0); + std::transform(text.begin(), text_end, buffer.begin(), [](u16_le character) { + return static_cast(static_cast(character)); + }); + return Common::UTF16ToUTF8(buffer); +} + QtMiiSelectorDialog::QtMiiSelectorDialog(QWidget* parent, QtMiiSelector* mii_selector_) : QDialog(parent), mii_selector(mii_selector_) { using namespace Frontend; @@ -28,7 +43,7 @@ QtMiiSelectorDialog::QtMiiSelectorDialog(QWidget* parent, QtMiiSelector* mii_sel setWindowTitle(config.title.empty() || config.title.at(0) == '\x0000' ? tr("Mii Selector") - : QString::fromStdU16String(config.title)); + : QString::fromStdString(config.title)); miis.push_back(HLE::Applets::MiiSelector::GetStandardMiiResult().selected_mii_data); combobox->addItem(tr("Standard Mii")); @@ -56,10 +71,9 @@ QtMiiSelectorDialog::QtMiiSelectorDialog(QWidget* parent, QtMiiSelector* mii_sel file->Read(saved_miis_offset, sizeof(mii), mii_raw.data()); std::memcpy(&mii, mii_raw.data(), sizeof(mii)); if (mii.mii_id != 0) { - std::u16string name(sizeof(mii.mii_name), '\0'); - std::memcpy(name.data(), mii.mii_name.data(), sizeof(mii.mii_name)); + std::string name = TextFromBuffer(mii.mii_name); miis.push_back(mii); - combobox->addItem(QString::fromStdU16String(name)); + combobox->addItem(QString::fromStdString(name)); } saved_miis_offset += sizeof(mii); } diff --git a/src/core/file_sys/archive_backend.h b/src/core/file_sys/archive_backend.h index da05fcb7b..2df4f98c4 100644 --- a/src/core/file_sys/archive_backend.h +++ b/src/core/file_sys/archive_backend.h @@ -41,7 +41,7 @@ public: Path(const char* path) : type(LowPathType::Char), string(path) {} Path(std::vector binary_data) : type(LowPathType::Binary), binary(std::move(binary_data)) {} template - Path(std::array binary_data) + Path(const std::array& binary_data) : type(LowPathType::Binary), binary(binary_data.begin(), binary_data.end()) {} Path(LowPathType type, const std::vector& data); diff --git a/src/core/frontend/applets/mii_selector.h b/src/core/frontend/applets/mii_selector.h index 8cb59d9d2..588bc0c5b 100644 --- a/src/core/frontend/applets/mii_selector.h +++ b/src/core/frontend/applets/mii_selector.h @@ -18,7 +18,7 @@ constexpr char MII_BUTTON_CANCEL[] = "Cancel"; /// later learn is needed can be added here and filled in by the backend HLE applet struct MiiSelectorConfig { bool enable_cancel_button; - std::u16string title; + std::string title; u32 initially_selected_mii_index; }; diff --git a/src/core/hle/applets/mii_selector.cpp b/src/core/hle/applets/mii_selector.cpp index cda28ffc3..596e216c0 100644 --- a/src/core/hle/applets/mii_selector.cpp +++ b/src/core/hle/applets/mii_selector.cpp @@ -19,6 +19,20 @@ namespace HLE::Applets { +/** + * Converts a UTF-16 text in a container to a UTF-8 std::string. + */ +template +std::string TextFromBuffer(const T& text) { + const auto text_end = std::find(text.begin(), text.end(), u'\0'); + const std::size_t text_size = std::distance(text.begin(), text_end); + std::u16string buffer(text_size, 0); + std::transform(text.begin(), text_end, buffer.begin(), [](u16_le character) { + return static_cast(static_cast(character)); + }); + return Common::UTF16ToUTF8(buffer); +} + ResultCode MiiSelector::ReceiveParameter(const Service::APT::MessageParameter& parameter) { if (parameter.signal != Service::APT::SignalType::Request) { LOG_ERROR(Service_APT, "unsupported signal {}", static_cast(parameter.signal)); @@ -142,10 +156,7 @@ MiiResult MiiSelector::GetStandardMiiResult() { Frontend::MiiSelectorConfig MiiSelector::ToFrontendConfig(const MiiConfig& config) const { Frontend::MiiSelectorConfig frontend_config; frontend_config.enable_cancel_button = config.enable_cancel_button == 1; - std::transform(config.title.begin(), config.title.end(), - std::back_inserter(frontend_config.title), [](u16_le character) -> char16_t { - return static_cast(static_cast(character)); - }); + frontend_config.title = TextFromBuffer(config.title); frontend_config.initially_selected_mii_index = config.initially_selected_mii_index; return frontend_config; }