InputCommon: Refactor away InputConfig::LoadConfig's switch case

By having getters for this information, other code that needs access to
the same information can call the getters instead of duplicating the
information.
This commit is contained in:
JosJuice 2024-02-04 15:29:36 +01:00
parent d96d2cd68c
commit 2bcf70af3f
8 changed files with 68 additions and 46 deletions

View File

@ -312,7 +312,8 @@ void FreeLookController::UpdateInput(CameraControllerInput* camera_controller)
namespace FreeLook
{
static InputConfig s_config("FreeLookController", _trans("FreeLook"), "FreeLookController");
static InputConfig s_config("FreeLookController", _trans("FreeLook"), "FreeLookController",
InputConfig::InputClass::GC);
InputConfig* GetInputConfig()
{
return &s_config;
@ -336,12 +337,12 @@ void Initialize()
FreeLook::GetConfig().Refresh();
s_config.LoadConfig(InputConfig::InputClass::GC);
s_config.LoadConfig();
}
void LoadInputConfig()
{
s_config.LoadConfig(InputConfig::InputClass::GC);
s_config.LoadConfig();
}
bool IsInitialized()

View File

@ -10,7 +10,7 @@
namespace Pad
{
static InputConfig s_config("GBA", _trans("Pad"), "GBA");
static InputConfig s_config("GBA", _trans("Pad"), "GBA", InputConfig::InputClass::GBA);
InputConfig* GetGBAConfig()
{
return &s_config;
@ -34,12 +34,12 @@ void InitializeGBA()
s_config.RegisterHotplugCallback();
// Load the saved controller config
s_config.LoadConfig(InputConfig::InputClass::GBA);
s_config.LoadConfig();
}
void LoadGBAConfig()
{
s_config.LoadConfig(InputConfig::InputClass::GBA);
s_config.LoadConfig();
}
bool IsGBAInitialized()

View File

@ -17,7 +17,7 @@
namespace Keyboard
{
static InputConfig s_config("GCKeyNew", _trans("Keyboard"), "GCKey");
static InputConfig s_config("GCKeyNew", _trans("Keyboard"), "GCKey", InputConfig::InputClass::GC);
InputConfig* GetConfig()
{
return &s_config;
@ -41,12 +41,12 @@ void Initialize()
s_config.RegisterHotplugCallback();
// Load the saved controller config
s_config.LoadConfig(InputConfig::InputClass::GC);
s_config.LoadConfig();
}
void LoadConfig()
{
s_config.LoadConfig(InputConfig::InputClass::GC);
s_config.LoadConfig();
}
ControllerEmu::ControlGroup* GetGroup(int port, KeyboardGroup group)

View File

@ -14,7 +14,7 @@
namespace Pad
{
static InputConfig s_config("GCPadNew", _trans("Pad"), "GCPad");
static InputConfig s_config("GCPadNew", _trans("Pad"), "GCPad", InputConfig::InputClass::GC);
InputConfig* GetConfig()
{
return &s_config;
@ -38,12 +38,12 @@ void Initialize()
s_config.RegisterHotplugCallback();
// Load the saved controller config
s_config.LoadConfig(InputConfig::InputClass::GC);
s_config.LoadConfig();
}
void LoadConfig()
{
s_config.LoadConfig(InputConfig::InputClass::GC);
s_config.LoadConfig();
}
bool IsInitialized()

View File

@ -97,7 +97,8 @@ HIDWiimote* GetHIDWiimoteSource(unsigned int index)
namespace Wiimote
{
static InputConfig s_config(WIIMOTE_INI_NAME, _trans("Wii Remote"), "Wiimote");
static InputConfig s_config(WIIMOTE_INI_NAME, _trans("Wii Remote"), "Wiimote",
InputConfig::InputClass::Wii);
InputConfig* GetConfig()
{
@ -206,7 +207,7 @@ void ResetAllWiimotes()
void LoadConfig()
{
s_config.LoadConfig(InputConfig::InputClass::Wii);
s_config.LoadConfig();
s_last_connect_request_counter.fill(0);
}

View File

@ -207,7 +207,7 @@ static std::array<u32, NUM_HOTKEY_GROUPS> s_hotkey_down;
static HotkeyStatus s_hotkey;
static bool s_enabled;
static InputConfig s_config("Hotkeys", _trans("Hotkeys"), "Hotkeys");
static InputConfig s_config("Hotkeys", _trans("Hotkeys"), "Hotkeys", InputConfig::InputClass::GC);
InputConfig* GetConfig()
{
@ -304,7 +304,7 @@ void Initialize()
void LoadConfig()
{
s_config.LoadConfig(InputConfig::InputClass::GC);
s_config.LoadConfig();
LoadLegacyConfig(s_config.GetController(0));
}

View File

@ -20,57 +20,40 @@
#include "InputCommon/InputProfile.h"
InputConfig::InputConfig(const std::string& ini_name, const std::string& gui_name,
const std::string& profile_name)
: m_ini_name(ini_name), m_gui_name(gui_name), m_profile_name(profile_name)
const std::string& profile_name, InputClass input_class)
: m_ini_name(ini_name), m_gui_name(gui_name), m_profile_name(profile_name),
m_input_class(input_class)
{
}
InputConfig::~InputConfig() = default;
bool InputConfig::LoadConfig(InputClass type)
bool InputConfig::LoadConfig()
{
Common::IniFile inifile;
bool useProfile[MAX_BBMOTES] = {false, false, false, false, false};
static constexpr std::array<std::string_view, MAX_BBMOTES> num = {"1", "2", "3", "4", "BB"};
std::string profile[MAX_BBMOTES];
std::string path;
m_dynamic_input_tex_config_manager.Load();
if (SConfig::GetInstance().GetGameID() != "00000000")
{
std::string type_str;
switch (type)
{
case InputClass::GBA:
type_str = "GBA";
path = "Profiles/GBA/";
break;
case InputClass::Wii:
type_str = "Wiimote";
path = "Profiles/Wiimote/";
break;
case InputClass::GC:
default:
type_str = "Pad";
path = "Profiles/GCPad/";
break;
}
const std::string profile_directory = GetProfileDirectoryPath();
Common::IniFile game_ini = SConfig::GetInstance().LoadGameIni();
auto* control_section = game_ini.GetOrCreateSection("Controls");
for (int i = 0; i < 4; i++)
{
const auto profile_name = fmt::format("{}Profile{}", type_str, num[i]);
const auto profile_name = fmt::format("{}Profile{}", GetProfileKey(), num[i]);
if (control_section->Exists(profile_name))
{
std::string profile_setting;
if (control_section->Get(profile_name, &profile_setting))
{
auto profiles = InputProfile::GetProfilesFromSetting(
profile_setting, File::GetUserPath(D_CONFIG_IDX) + path);
auto profiles = InputProfile::GetProfilesFromSetting(profile_setting, profile_directory);
if (profiles.empty())
{
@ -176,6 +159,39 @@ bool InputConfig::ControllersNeedToBeCreated() const
return m_controllers.empty();
}
std::string InputConfig::GetProfileKey() const
{
switch (m_input_class)
{
case InputClass::GBA:
return "GBA";
case InputClass::Wii:
return "Wiimote";
case InputClass::GC:
default:
return "Pad";
}
}
std::string InputConfig::GetProfileDirectoryName() const
{
switch (m_input_class)
{
case InputClass::GBA:
return "GBA";
case InputClass::Wii:
return "Wiimote";
case InputClass::GC:
default:
return "GCPad";
}
}
std::string InputConfig::GetProfileDirectoryPath() const
{
return fmt::format("{}Profiles/{}/", File::GetUserPath(D_CONFIG_IDX), GetProfileDirectoryName());
}
int InputConfig::GetControllerCount() const
{
return static_cast<int>(m_controllers.size());

View File

@ -24,11 +24,6 @@ class EmulatedController;
class InputConfig
{
public:
InputConfig(const std::string& ini_name, const std::string& gui_name,
const std::string& profile_name);
~InputConfig();
enum class InputClass
{
GC,
@ -36,7 +31,12 @@ public:
GBA,
};
bool LoadConfig(InputClass type);
InputConfig(const std::string& ini_name, const std::string& gui_name,
const std::string& profile_name, InputClass input_class);
~InputConfig();
bool LoadConfig();
void SaveConfig();
template <typename T, typename... Args>
@ -52,6 +52,9 @@ public:
std::string GetGUIName() const { return m_gui_name; }
std::string GetProfileName() const { return m_profile_name; }
std::string GetProfileKey() const;
std::string GetProfileDirectoryName() const;
std::string GetProfileDirectoryPath() const;
int GetControllerCount() const;
// These should be used after creating all controllers and before clearing them, respectively.
@ -66,5 +69,6 @@ private:
const std::string m_ini_name;
const std::string m_gui_name;
const std::string m_profile_name;
const InputClass m_input_class;
InputCommon::DynamicInputTextureManager m_dynamic_input_tex_config_manager;
};