From fefde0481d7bbcbe2cea453e8c0ddadc0bfa0b12 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Tue, 11 Jan 2022 17:10:13 -0800 Subject: [PATCH] Use EnumFormatter to generate names for EXI devices This simplifies the code in GameCubePane, and allows us to use the EXIDeviceType enum in error messages. --- Source/Core/Common/EnumFormatter.h | 1 - Source/Core/Core/HW/EXI/EXI_Device.h | 50 +++++++++++++++++++ .../Core/DolphinQt/Settings/GameCubePane.cpp | 36 +++++++------ 3 files changed, 67 insertions(+), 20 deletions(-) diff --git a/Source/Core/Common/EnumFormatter.h b/Source/Core/Common/EnumFormatter.h index 26cc910438..9530013768 100644 --- a/Source/Core/Common/EnumFormatter.h +++ b/Source/Core/Common/EnumFormatter.h @@ -95,7 +95,6 @@ protected: constexpr explicit EnumFormatter(const array_type names) : m_names(std::move(names)) {} -private: const array_type m_names; char format_type = 'u'; }; diff --git a/Source/Core/Core/HW/EXI/EXI_Device.h b/Source/Core/Core/HW/EXI/EXI_Device.h index 6b105c9be4..4425a652c4 100644 --- a/Source/Core/Core/HW/EXI/EXI_Device.h +++ b/Source/Core/Core/HW/EXI/EXI_Device.h @@ -4,7 +4,10 @@ #pragma once #include + +#include "Common/Common.h" #include "Common/CommonTypes.h" +#include "Common/EnumFormatter.h" class PointerWrap; @@ -75,3 +78,50 @@ private: std::unique_ptr EXIDevice_Create(EXIDeviceType device_type, int channel_num, const Memcard::HeaderData& memcard_header_data); } // namespace ExpansionInterface + +template <> +struct fmt::formatter + : EnumFormatter +{ + static constexpr array_type names = { + _trans("Dummy"), + _trans("Memory Card"), + _trans("Mask ROM"), + // i18n: A mysterious debugging/diagnostics peripheral for the GameCube. + _trans("AD16"), + _trans("Microphone"), + _trans("Broadband Adapter (TAP)"), + _trans("Triforce AM Baseboard"), + _trans("USB Gecko"), + _trans("GCI Folder"), + _trans("Advance Game Port"), + _trans("Broadband Adapter (XLink Kai)"), + _trans("Broadband Adapter (tapserver)"), + }; + + constexpr formatter() : EnumFormatter(names) {} + + template + auto format(const ExpansionInterface::EXIDeviceType& e, FormatContext& ctx) + { + if (e != ExpansionInterface::EXIDeviceType::None) + { + return EnumFormatter::format(e, ctx); + } + else + { + // Special-case None since it has a fixed ID (0xff) that is much larger than the rest; we + // don't need 200 nullptr entries in names. We also want to format it specially in the UI. + switch (format_type) + { + default: + case 'u': + return fmt::format_to(ctx.out(), "None"); + case 's': + return fmt::format_to(ctx.out(), "0xffu /* None */"); + case 'n': + return fmt::format_to(ctx.out(), _trans("")); + } + } + } +}; diff --git a/Source/Core/DolphinQt/Settings/GameCubePane.cpp b/Source/Core/DolphinQt/Settings/GameCubePane.cpp index 353199f2fc..b43b5ffef0 100644 --- a/Source/Core/DolphinQt/Settings/GameCubePane.cpp +++ b/Source/Core/DolphinQt/Settings/GameCubePane.cpp @@ -16,6 +16,7 @@ #include #include +#include #include #include "Common/CommonPaths.h" @@ -97,31 +98,28 @@ void GameCubePane::CreateWidgets() } // Add slot devices - for (const auto& entry : {std::make_pair(tr(""), EXIDeviceType::None), - std::make_pair(tr("Dummy"), EXIDeviceType::Dummy), - std::make_pair(tr("Memory Card"), EXIDeviceType::MemoryCard), - std::make_pair(tr("GCI Folder"), EXIDeviceType::MemoryCardFolder), - std::make_pair(tr("USB Gecko"), EXIDeviceType::Gecko), - std::make_pair(tr("Advance Game Port"), EXIDeviceType::AGP), - std::make_pair(tr("Microphone"), EXIDeviceType::Microphone)}) + for (const auto device : {EXIDeviceType::None, EXIDeviceType::Dummy, EXIDeviceType::MemoryCard, + EXIDeviceType::MemoryCardFolder, EXIDeviceType::Gecko, + EXIDeviceType::AGP, EXIDeviceType::Microphone}) { - m_slot_combos[0]->addItem(entry.first, static_cast(entry.second)); - m_slot_combos[1]->addItem(entry.first, static_cast(entry.second)); + const QString name = tr(fmt::format("{:n}", device).c_str()); + const int value = static_cast(device); + m_slot_combos[0]->addItem(name, value); + m_slot_combos[1]->addItem(name, value); } // Add SP1 devices - std::vector> sp1Entries{ - std::make_pair(tr(""), EXIDeviceType::None), - std::make_pair(tr("Dummy"), EXIDeviceType::Dummy), - std::make_pair(tr("Broadband Adapter (TAP)"), EXIDeviceType::Ethernet), - std::make_pair(tr("Broadband Adapter (XLink Kai)"), EXIDeviceType::EthernetXLink)}; -#if defined(__APPLE__) - sp1Entries.emplace_back(std::make_pair(tr("Broadband Adapter (tapserver)"), - ExpansionInterface::EXIDeviceType::EthernetTapServer)); + for (const auto device : { + EXIDeviceType::None, + EXIDeviceType::Dummy, + EXIDeviceType::Ethernet, + EXIDeviceType::EthernetXLink, +#ifdef __APPLE__ + EXIDeviceType::EthernetTapServer, #endif - for (const auto& entry : sp1Entries) + }) { - m_slot_combos[2]->addItem(entry.first, static_cast(entry.second)); + m_slot_combos[2]->addItem(tr(fmt::format("{:n}", device).c_str()), static_cast(device)); } device_layout->addWidget(new QLabel(tr("Slot A:")), 0, 0);