diff --git a/UI/CustomButtonMappingScreen.cpp b/UI/CustomButtonMappingScreen.cpp index 0fc971c36b..f402e03f25 100644 --- a/UI/CustomButtonMappingScreen.cpp +++ b/UI/CustomButtonMappingScreen.cpp @@ -138,6 +138,9 @@ void CustomButtonMappingScreen::CreateDialogViews(UI::ViewGroup *parent) { bool *show = nullptr; memset(array, 0, sizeof(array)); cfg = &g_Config.CustomButton[id_]; + // This screen is reachable from the main menu, so neither GamepadEmu nor the layout screen + // need have sanitized these yet - and everything below indexes the tables with them. + CustomKeyData::Sanitize(*cfg); show = &touch.touchCustom[id_].show; for (int i = 0; i < ARRAY_SIZE(g_customKeyList); i++) array[i] = (0x01 == ((g_Config.CustomButton[id_].key >> i) & 0x01)); diff --git a/UI/GamepadEmu.cpp b/UI/GamepadEmu.cpp index 2ff6505b40..d49edbfdf3 100644 --- a/UI/GamepadEmu.cpp +++ b/UI/GamepadEmu.cpp @@ -1106,12 +1106,7 @@ GamepadEmuView::GamepadEmuView(const TouchControlConfig &config, float xres, flo // Sanitize custom button images, while adding them. for (int i = 0; i < TouchControlConfig::CUSTOM_BUTTON_COUNT; i++) { - if (g_Config.CustomButton[i].shape >= ARRAY_SIZE(CustomKeyData::customKeyShapes)) { - g_Config.CustomButton[i].shape = 0; - } - if (g_Config.CustomButton[i].image >= ARRAY_SIZE(CustomKeyData::customKeyImages)) { - g_Config.CustomButton[i].image = 0; - } + CustomKeyData::Sanitize(g_Config.CustomButton[i]); char temp[64]; snprintf(temp, sizeof(temp), "Custom %d button", i + 1); diff --git a/UI/GamepadEmu.h b/UI/GamepadEmu.h index 6b106f6dae..ce8463c100 100644 --- a/UI/GamepadEmu.h +++ b/UI/GamepadEmu.h @@ -22,6 +22,7 @@ #include "Common/UI/View.h" #include "Common/UI/ViewGroup.h" +#include "Core/ConfigValues.h" #include "Core/CoreParameter.h" #include "Core/HLE/sceCtrl.h" #include "UI/EmuScreen.h" @@ -366,6 +367,17 @@ namespace CustomKeyData { // IMPORTANT: Only add at the end! }; static_assert(ARRAY_SIZE(g_customKeyList) <= 64, "Too many key for a uint64_t bit mask"); + + // image and shape come straight from the ini and index the tables above, so anything that + // reads them has to run this first. + inline void Sanitize(ConfigCustomButton &cfg) { + if (cfg.image < 0 || cfg.image >= (int)ARRAY_SIZE(customKeyImages)) { + cfg.image = 0; + } + if (cfg.shape < 0 || cfg.shape >= (int)ARRAY_SIZE(customKeyShapes)) { + cfg.shape = 0; + } + } }; // Gesture key only have virtual button that can work without constant press diff --git a/UI/TouchControlLayoutScreen.cpp b/UI/TouchControlLayoutScreen.cpp index edf7481d05..b50ad96733 100644 --- a/UI/TouchControlLayoutScreen.cpp +++ b/UI/TouchControlLayoutScreen.cpp @@ -537,12 +537,7 @@ void ControlLayoutView::CreateViews() { for (int i = 0; i < TouchControlConfig::CUSTOM_BUTTON_COUNT; i++) { // Similar to GamepadEmu, we sanitize the images for valid values. - if (g_Config.CustomButton[i].shape >= ARRAY_SIZE(CustomKeyData::customKeyShapes)) { - g_Config.CustomButton[i].shape = 0; - } - if (g_Config.CustomButton[i].image >= ARRAY_SIZE(CustomKeyData::customKeyImages)) { - g_Config.CustomButton[i].image = 0; - } + CustomKeyData::Sanitize(g_Config.CustomButton[i]); char temp[64]; snprintf(temp, sizeof(temp), "Custom %d button", i);