From 3f0b23ed2bf431e7cd7262c60afebc293d8ff806 Mon Sep 17 00:00:00 2001 From: Pokechu22 Date: Sun, 30 Aug 2020 13:43:45 -0700 Subject: [PATCH] Create Slot enum class for EXI slots --- Source/Core/Core/Config/MainSettings.cpp | 18 +++++--- Source/Core/Core/Config/MainSettings.h | 5 ++- .../ConfigLoaders/NetPlayConfigLoader.cpp | 6 +-- Source/Core/Core/HW/EXI/EXI.cpp | 44 ++++++++++++++++--- Source/Core/Core/HW/EXI/EXI.h | 23 +++++++++- Source/Core/Core/NetPlayClient.cpp | 5 ++- Source/Core/Core/NetPlayProto.h | 4 +- Source/Core/Core/NetPlayServer.cpp | 40 +++++++++++------ Source/Core/DolphinQt/GameList/GameList.cpp | 18 +++++--- .../Core/DolphinQt/Settings/GameCubePane.cpp | 7 +-- 10 files changed, 125 insertions(+), 45 deletions(-) diff --git a/Source/Core/Core/Config/MainSettings.cpp b/Source/Core/Core/Config/MainSettings.cpp index c520f06b4a..023f0ebf5b 100644 --- a/Source/Core/Core/Config/MainSettings.cpp +++ b/Source/Core/Core/Config/MainSettings.cpp @@ -10,11 +10,13 @@ #include "AudioCommon/AudioCommon.h" #include "Common/CommonPaths.h" #include "Common/Config/Config.h" +#include "Common/EnumMap.h" #include "Common/Logging/Log.h" #include "Common/MathUtil.h" #include "Common/StringUtil.h" #include "Common/Version.h" #include "Core/Config/DefaultLocale.h" +#include "Core/HW/EXI/EXI.h" #include "Core/HW/EXI/EXI_Device.h" #include "Core/HW/Memmap.h" #include "Core/HW/SI/SI_Device.h" @@ -62,14 +64,16 @@ const Info MAIN_SLOT_B{{System::Main, "Core", const Info MAIN_SERIAL_PORT_1{ {System::Main, "Core", "SerialPort1"}, ExpansionInterface::EXIDeviceType::None}; -const Info& GetInfoForEXIDevice(int channel) +const Info& GetInfoForEXIDevice(ExpansionInterface::Slot slot) { - static constexpr std::array*, 3> infos{ - &MAIN_SLOT_A, - &MAIN_SLOT_B, - &MAIN_SERIAL_PORT_1, - }; - return *infos[channel]; + static constexpr Common::EnumMap*, + ExpansionInterface::MAX_SLOT> + infos{ + &MAIN_SLOT_A, + &MAIN_SLOT_B, + &MAIN_SERIAL_PORT_1, + }; + return *infos[slot]; } const Info MAIN_BBA_MAC{{System::Main, "Core", "BBA_MAC"}, ""}; diff --git a/Source/Core/Core/Config/MainSettings.h b/Source/Core/Core/Config/MainSettings.h index 49e2a98e8d..23c3017c68 100644 --- a/Source/Core/Core/Config/MainSettings.h +++ b/Source/Core/Core/Config/MainSettings.h @@ -34,7 +34,8 @@ enum class DPL2Quality; namespace ExpansionInterface { enum class EXIDeviceType : int; -} +enum class Slot : int; +} // namespace ExpansionInterface namespace SerialInterface { @@ -72,7 +73,7 @@ extern const Info MAIN_GCI_FOLDER_B_PATH_OVERRIDE; extern const Info MAIN_SLOT_A; extern const Info MAIN_SLOT_B; extern const Info MAIN_SERIAL_PORT_1; -const Info& GetInfoForEXIDevice(int channel); +const Info& GetInfoForEXIDevice(ExpansionInterface::Slot slot); extern const Info MAIN_BBA_MAC; extern const Info MAIN_BBA_XLINK_IP; extern const Info MAIN_BBA_XLINK_CHAT_OSD; diff --git a/Source/Core/Core/ConfigLoaders/NetPlayConfigLoader.cpp b/Source/Core/Core/ConfigLoaders/NetPlayConfigLoader.cpp index cd6f6d8829..92dd1a9cfc 100644 --- a/Source/Core/Core/ConfigLoaders/NetPlayConfigLoader.cpp +++ b/Source/Core/Core/ConfigLoaders/NetPlayConfigLoader.cpp @@ -15,6 +15,7 @@ #include "Core/Config/MainSettings.h" #include "Core/Config/SYSCONFSettings.h" #include "Core/Config/SessionSettings.h" +#include "Core/HW/EXI/EXI.h" #include "Core/NetPlayProto.h" namespace ConfigLoaders @@ -37,9 +38,8 @@ public: layer->Set(Config::MAIN_DSP_HLE, m_settings.m_DSPHLE); layer->Set(Config::MAIN_OVERCLOCK_ENABLE, m_settings.m_OCEnable); layer->Set(Config::MAIN_OVERCLOCK, m_settings.m_OCFactor); - layer->Set(Config::MAIN_SLOT_A, m_settings.m_EXIDevice[0]); - layer->Set(Config::MAIN_SLOT_B, m_settings.m_EXIDevice[1]); - layer->Set(Config::MAIN_SERIAL_PORT_1, m_settings.m_EXIDevice[2]); + for (ExpansionInterface::Slot slot : ExpansionInterface::SLOTS) + layer->Set(Config::GetInfoForEXIDevice(slot), m_settings.m_EXIDevice[slot]); layer->Set(Config::SESSION_SAVE_DATA_WRITABLE, m_settings.m_WriteToMemcard); layer->Set(Config::MAIN_RAM_OVERRIDE_ENABLE, m_settings.m_RAMOverrideEnable); layer->Set(Config::MAIN_MEM1_SIZE, m_settings.m_Mem1Size); diff --git a/Source/Core/Core/HW/EXI/EXI.cpp b/Source/Core/Core/HW/EXI/EXI.cpp index f40d321bb5..7ffd639b2d 100644 --- a/Source/Core/Core/HW/EXI/EXI.cpp +++ b/Source/Core/Core/HW/EXI/EXI.cpp @@ -46,7 +46,8 @@ void AddMemoryCards(int i) { if (Movie::IsUsingMemcard(i)) { - if (Config::Get(Config::GetInfoForEXIDevice(i)) == EXIDeviceType::MemoryCardFolder) + if (Config::Get(Config::GetInfoForEXIDevice(static_cast(i))) == + EXIDeviceType::MemoryCardFolder) memorycard_device = EXIDeviceType::MemoryCardFolder; else memorycard_device = EXIDeviceType::MemoryCard; @@ -58,13 +59,45 @@ void AddMemoryCards(int i) } else { - memorycard_device = Config::Get(Config::GetInfoForEXIDevice(i)); + memorycard_device = Config::Get(Config::GetInfoForEXIDevice(static_cast(i))); } g_Channels[i]->AddDevice(memorycard_device, 0); } } // namespace +u8 SlotToEXIChannel(Slot slot) +{ + switch (slot) + { + case Slot::A: + return 0; + case Slot::B: + return 1; + case Slot::SP1: + return 0; + default: + PanicAlertFmt("Unhandled slot {}", slot); + return 0; + } +} + +u8 SlotToEXIDevice(Slot slot) +{ + switch (slot) + { + case Slot::A: + return 0; + case Slot::B: + return 0; + case Slot::SP1: + return 2; + default: + PanicAlertFmt("Unhandled slot {}", slot); + return 0; + } +} + void Init() { if (!g_SRAM_netplay_initialized) @@ -98,11 +131,12 @@ void Init() } } - for (int i = 0; i < MAX_MEMORYCARD_SLOTS; i++) - AddMemoryCards(i); + for (Slot slot : MEMCARD_SLOTS) + AddMemoryCards(static_cast(slot)); g_Channels[0]->AddDevice(EXIDeviceType::MaskROM, 1); - g_Channels[0]->AddDevice(Config::Get(Config::MAIN_SERIAL_PORT_1), 2); + g_Channels[SlotToEXIChannel(Slot::SP1)]->AddDevice(Config::Get(Config::MAIN_SERIAL_PORT_1), + SlotToEXIDevice(Slot::SP1)); g_Channels[2]->AddDevice(EXIDeviceType::AD16, 0); changeDevice = CoreTiming::RegisterEvent("ChangeEXIDevice", ChangeDeviceCallback); diff --git a/Source/Core/Core/HW/EXI/EXI.h b/Source/Core/Core/HW/EXI/EXI.h index d898f7ea02..4484205044 100644 --- a/Source/Core/Core/HW/EXI/EXI.h +++ b/Source/Core/Core/HW/EXI/EXI.h @@ -3,7 +3,10 @@ #pragma once +#include + #include "Common/CommonTypes.h" +#include "Common/EnumFormatter.h" #include "Core/CoreTiming.h" class PointerWrap; @@ -25,10 +28,22 @@ enum class EXIDeviceType : int; enum { - MAX_MEMORYCARD_SLOTS = 2, MAX_EXI_CHANNELS = 3 }; +enum class Slot : int +{ + A, + B, + SP1, +}; +static constexpr auto SLOTS = {Slot::A, Slot::B, Slot::SP1}; +static constexpr auto MAX_SLOT = Slot::SP1; +static constexpr auto MEMCARD_SLOTS = {Slot::A, Slot::B}; + +u8 SlotToEXIChannel(Slot slot); +u8 SlotToEXIDevice(Slot slot); + void Init(); void Shutdown(); void DoState(PointerWrap& p); @@ -47,3 +62,9 @@ CEXIChannel* GetChannel(u32 index); IEXIDevice* FindDevice(EXIDeviceType device_type, int customIndex = -1); } // namespace ExpansionInterface + +template <> +struct fmt::formatter : EnumFormatter +{ + constexpr formatter() : EnumFormatter({"Slot A", "Slot B", "Serial Port 1"}) {} +}; diff --git a/Source/Core/Core/NetPlayClient.cpp b/Source/Core/Core/NetPlayClient.cpp index dc2a56efdc..02bdcd7219 100644 --- a/Source/Core/Core/NetPlayClient.cpp +++ b/Source/Core/Core/NetPlayClient.cpp @@ -39,6 +39,7 @@ #include "Core/Config/SessionSettings.h" #include "Core/ConfigManager.h" #include "Core/GeckoCode.h" +#include "Core/HW/EXI/EXI.h" #include "Core/HW/EXI/EXI_DeviceIPL.h" #ifdef HAS_LIBMGBA #include "Core/HW/GBACore.h" @@ -818,8 +819,8 @@ void NetPlayClient::OnStartGame(sf::Packet& packet) packet >> m_net_settings.m_OCEnable; packet >> m_net_settings.m_OCFactor; - for (auto& device : m_net_settings.m_EXIDevice) - packet >> device; + for (auto slot : ExpansionInterface::SLOTS) + packet >> m_net_settings.m_EXIDevice[slot]; for (u32& value : m_net_settings.m_SYSCONFSettings) packet >> value; diff --git a/Source/Core/Core/NetPlayProto.h b/Source/Core/Core/NetPlayProto.h index 9c63ed0e6c..ccb147b256 100644 --- a/Source/Core/Core/NetPlayProto.h +++ b/Source/Core/Core/NetPlayProto.h @@ -8,7 +8,9 @@ #include #include "Common/CommonTypes.h" +#include "Common/EnumMap.h" #include "Core/Config/SYSCONFSettings.h" +#include "Core/HW/EXI/EXI.h" #include "Core/HW/EXI/EXI_Device.h" namespace DiscIO @@ -44,7 +46,7 @@ struct NetSettings bool m_CopyWiiSave = false; bool m_OCEnable = false; float m_OCFactor = 0; - std::array m_EXIDevice{}; + Common::EnumMap m_EXIDevice{}; std::array m_SYSCONFSettings{}; diff --git a/Source/Core/Core/NetPlayServer.cpp b/Source/Core/Core/NetPlayServer.cpp index 8ef91a459b..40e480f6a1 100644 --- a/Source/Core/Core/NetPlayServer.cpp +++ b/Source/Core/Core/NetPlayServer.cpp @@ -40,6 +40,8 @@ #include "Core/ConfigManager.h" #include "Core/GeckoCode.h" #include "Core/GeckoCodeConfig.h" +#include "Core/HW/EXI/EXI.h" +#include "Core/HW/EXI/EXI_Device.h" #ifdef HAS_LIBMGBA #include "Core/HW/GBACore.h" #endif @@ -1306,10 +1308,21 @@ bool NetPlayServer::SetupNetSettings() settings.m_CopyWiiSave = Config::Get(Config::NETPLAY_LOAD_WII_SAVE); settings.m_OCEnable = Config::Get(Config::MAIN_OVERCLOCK_ENABLE); settings.m_OCFactor = Config::Get(Config::MAIN_OVERCLOCK); - settings.m_EXIDevice[0] = Config::Get(Config::MAIN_SLOT_A); - settings.m_EXIDevice[1] = Config::Get(Config::MAIN_SLOT_B); - // There's no way the BBA is going to sync, disable it - settings.m_EXIDevice[2] = ExpansionInterface::EXIDeviceType::None; + + for (ExpansionInterface::Slot slot : ExpansionInterface::SLOTS) + { + ExpansionInterface::EXIDeviceType device; + if (slot == ExpansionInterface::Slot::SP1) + { + // There's no way the BBA is going to sync, disable it + device = ExpansionInterface::EXIDeviceType::None; + } + else + { + device = Config::Get(Config::GetInfoForEXIDevice(slot)); + } + settings.m_EXIDevice[slot] = device; + } for (size_t i = 0; i < Config::SYSCONF_SETTINGS.size(); ++i) { @@ -1494,8 +1507,8 @@ bool NetPlayServer::StartGame() spac << m_settings.m_OCEnable; spac << m_settings.m_OCFactor; - for (auto& device : m_settings.m_EXIDevice) - spac << static_cast(device); + for (auto slot : ExpansionInterface::SLOTS) + spac << static_cast(m_settings.m_EXIDevice[slot]); for (u32 value : m_settings.m_SYSCONFSettings) spac << value; @@ -1592,11 +1605,10 @@ bool NetPlayServer::SyncSaveData() u8 save_count = 0; - constexpr int exi_device_count = 2; - for (int i = 0; i < exi_device_count; ++i) + for (ExpansionInterface::Slot slot : ExpansionInterface::MEMCARD_SLOTS) { - if (m_settings.m_EXIDevice[i] == ExpansionInterface::EXIDeviceType::MemoryCard || - Config::Get(Config::GetInfoForEXIDevice(i)) == + if (m_settings.m_EXIDevice[slot] == ExpansionInterface::EXIDeviceType::MemoryCard || + Config::Get(Config::GetInfoForEXIDevice(slot)) == ExpansionInterface::EXIDeviceType::MemoryCardFolder) { save_count++; @@ -1652,11 +1664,11 @@ bool NetPlayServer::SyncSaveData() const std::string region = SConfig::GetDirectoryForRegion(SConfig::ToGameCubeRegion(game->GetRegion())); - for (int i = 0; i < exi_device_count; ++i) + for (ExpansionInterface::Slot slot : ExpansionInterface::MEMCARD_SLOTS) { - const bool is_slot_a = i == 0; + const bool is_slot_a = slot == ExpansionInterface::Slot::A; - if (m_settings.m_EXIDevice[i] == ExpansionInterface::EXIDeviceType::MemoryCard) + if (m_settings.m_EXIDevice[slot] == ExpansionInterface::EXIDeviceType::MemoryCard) { std::string path = is_slot_a ? Config::Get(Config::MAIN_MEMCARD_A_PATH) : Config::Get(Config::MAIN_MEMCARD_B_PATH); @@ -1693,7 +1705,7 @@ bool NetPlayServer::SyncSaveData() SendChunkedToClients(std::move(pac), 1, fmt::format("Memory Card {} Synchronization", is_slot_a ? 'A' : 'B')); } - else if (Config::Get(Config::GetInfoForEXIDevice(i)) == + else if (Config::Get(Config::GetInfoForEXIDevice(slot)) == ExpansionInterface::EXIDeviceType::MemoryCardFolder) { const std::string path = File::GetUserPath(D_GCUSER_IDX) + region + DIR_SEP + diff --git a/Source/Core/DolphinQt/GameList/GameList.cpp b/Source/Core/DolphinQt/GameList/GameList.cpp index 40b786261c..e5170fc938 100644 --- a/Source/Core/DolphinQt/GameList/GameList.cpp +++ b/Source/Core/DolphinQt/GameList/GameList.cpp @@ -47,6 +47,7 @@ #include "Core/ConfigManager.h" #include "Core/Core.h" #include "Core/HW/DVD/DVDInterface.h" +#include "Core/HW/EXI/EXI.h" #include "Core/HW/EXI/EXI_Device.h" #include "Core/HW/WiiSave.h" #include "Core/WiiUtils.h" @@ -661,21 +662,24 @@ void GameList::OpenGCSaveFolder() bool found = false; - for (int i = 0; i < 2; i++) + using ExpansionInterface::Slot; + + for (Slot slot : ExpansionInterface::MEMCARD_SLOTS) { QUrl url; const ExpansionInterface::EXIDeviceType current_exi_device = - Config::Get(Config::GetInfoForEXIDevice(i)); + Config::Get(Config::GetInfoForEXIDevice(slot)); switch (current_exi_device) { case ExpansionInterface::EXIDeviceType::MemoryCardFolder: { std::string path = StringFromFormat("%s/%s/%s", File::GetUserPath(D_GCUSER_IDX).c_str(), SConfig::GetDirectoryForRegion(game->GetRegion()), - i == 0 ? "Card A" : "Card B"); + slot == Slot::A ? "Card A" : "Card B"); - std::string override_path = i == 0 ? Config::Get(Config::MAIN_GCI_FOLDER_A_PATH_OVERRIDE) : - Config::Get(Config::MAIN_GCI_FOLDER_B_PATH_OVERRIDE); + std::string override_path = slot == Slot::A ? + Config::Get(Config::MAIN_GCI_FOLDER_A_PATH_OVERRIDE) : + Config::Get(Config::MAIN_GCI_FOLDER_B_PATH_OVERRIDE); if (!override_path.empty()) path = override_path; @@ -693,8 +697,8 @@ void GameList::OpenGCSaveFolder() } case ExpansionInterface::EXIDeviceType::MemoryCard: { - std::string memcard_path = i == 0 ? Config::Get(Config::MAIN_MEMCARD_A_PATH) : - Config::Get(Config::MAIN_MEMCARD_B_PATH); + std::string memcard_path = slot == Slot::A ? Config::Get(Config::MAIN_MEMCARD_A_PATH) : + Config::Get(Config::MAIN_MEMCARD_B_PATH); std::string memcard_dir; diff --git a/Source/Core/DolphinQt/Settings/GameCubePane.cpp b/Source/Core/DolphinQt/Settings/GameCubePane.cpp index b43b5ffef0..d3ee4b3055 100644 --- a/Source/Core/DolphinQt/Settings/GameCubePane.cpp +++ b/Source/Core/DolphinQt/Settings/GameCubePane.cpp @@ -464,7 +464,7 @@ void GameCubePane::LoadSettings() { QSignalBlocker blocker(m_slot_combos[i]); const ExpansionInterface::EXIDeviceType exi_device = - Config::Get(Config::GetInfoForEXIDevice(i)); + Config::Get(Config::GetInfoForEXIDevice(static_cast(i))); m_slot_combos[i]->setCurrentIndex(m_slot_combos[i]->findData(static_cast(exi_device))); UpdateButton(i); } @@ -494,7 +494,7 @@ void GameCubePane::SaveSettings() const auto dev = static_cast(m_slot_combos[i]->currentData().toInt()); const ExpansionInterface::EXIDeviceType current_exi_device = - Config::Get(Config::GetInfoForEXIDevice(i)); + Config::Get(Config::GetInfoForEXIDevice(static_cast(i))); if (Core::IsRunning() && current_exi_device != dev) { @@ -507,7 +507,8 @@ void GameCubePane::SaveSettings() (i == 2) ? 2 : 0); } - Config::SetBaseOrCurrent(Config::GetInfoForEXIDevice(i), dev); + Config::SetBaseOrCurrent(Config::GetInfoForEXIDevice(static_cast(i)), + dev); } #ifdef HAS_LIBMGBA