Pad: Fix crash with mismatched cards in save state

This commit is contained in:
Connor McLaughlin 2021-04-01 02:27:38 +10:00
parent 872a9b95c7
commit 9bf805c2af

View File

@ -71,25 +71,20 @@ bool Pad::DoStateController(StateWrapper& sw, u32 i)
g_host_interface->TranslateString( g_host_interface->TranslateString(
"OSDMessage", "Save state contains controller type %s in port %u, but %s is used. Switching."), "OSDMessage", "Save state contains controller type %s in port %u, but %s is used. Switching."),
Settings::GetControllerTypeName(state_controller_type), i + 1u, Settings::GetControllerTypeName(state_controller_type), i + 1u,
Settings::GetControllerTypeName(controller_type) Settings::GetControllerTypeName(controller_type));
);
} }
else else
{ {
g_host_interface->AddFormattedOSDMessage( g_host_interface->AddFormattedOSDMessage(
10.0f, 10.0f, g_host_interface->TranslateString("OSDMessage", "Ignoring mismatched controller type %s in port %u."),
g_host_interface->TranslateString("OSDMessage", "Ignoring mismatched controller type %s in port %u."), Settings::GetControllerTypeName(state_controller_type), i + 1u);
Settings::GetControllerTypeName(state_controller_type), i + 1u
);
} }
// dev-friendly untranslated console log. // dev-friendly untranslated console log.
Log_DevPrintf("Controller type mismatch in slot %u: state=%s(%u) ui=%s(%u) load_from_state=%s", Log_DevPrintf("Controller type mismatch in slot %u: state=%s(%u) ui=%s(%u) load_from_state=%s", i + 1u,
i + 1u,
Settings::GetControllerTypeName(state_controller_type), state_controller_type, Settings::GetControllerTypeName(state_controller_type), state_controller_type,
Settings::GetControllerTypeName(controller_type), controller_type, Settings::GetControllerTypeName(controller_type), controller_type,
g_settings.load_devices_from_save_states ? "yes" : "no" g_settings.load_devices_from_save_states ? "yes" : "no");
);
if (g_settings.load_devices_from_save_states) if (g_settings.load_devices_from_save_states)
{ {
@ -132,6 +127,16 @@ bool Pad::DoStateMemcard(StateWrapper& sw, u32 i)
sw.Do(&card_present_in_state); sw.Do(&card_present_in_state);
if (card_present_in_state && !m_memory_cards[i] && g_settings.load_devices_from_save_states)
{
g_host_interface->AddFormattedOSDMessage(
20.0f,
g_host_interface->TranslateString(
"OSDMessage", "Memory card %u present in save state but not in system. Creating temporary card."),
i + 1u);
m_memory_cards[i] = MemoryCard::Create();
}
MemoryCard* card_ptr = m_memory_cards[i].get(); MemoryCard* card_ptr = m_memory_cards[i].get();
std::unique_ptr<MemoryCard> card_from_state; std::unique_ptr<MemoryCard> card_from_state;
@ -169,8 +174,7 @@ bool Pad::DoStateMemcard(StateWrapper& sw, u32 i)
20.0f, 20.0f,
g_host_interface->TranslateString( g_host_interface->TranslateString(
"OSDMessage", "Memory card %u from save state does match current card data. Simulating replugging."), "OSDMessage", "Memory card %u from save state does match current card data. Simulating replugging."),
i + 1u i + 1u);
);
// this is a potentially serious issue - some games cache info from memcards and jumping around // this is a potentially serious issue - some games cache info from memcards and jumping around
// with savestates can lead to card corruption on the next save attempts (and may not be obvious // with savestates can lead to card corruption on the next save attempts (and may not be obvious
@ -186,33 +190,21 @@ bool Pad::DoStateMemcard(StateWrapper& sw, u32 i)
{ {
g_host_interface->AddFormattedOSDMessage( g_host_interface->AddFormattedOSDMessage(
20.0f, 20.0f,
g_host_interface->TranslateString( g_host_interface->TranslateString("OSDMessage",
"OSDMessage", "Memory card %u present in save state but not in system. Ignoring card."), "Memory card %u present in save state but not in system. Ignoring card."),
i + 1u i + 1u);
);
} }
return true; return true;
} }
if (card_present_in_state && !m_memory_cards[i]) if (!card_present_in_state && m_memory_cards[i])
{ {
g_host_interface->AddFormattedOSDMessage( g_host_interface->AddFormattedOSDMessage(
20.0f, 20.0f,
g_host_interface->TranslateString( g_host_interface->TranslateString("OSDMessage",
"OSDMessage", "Memory card %u present in save state but not in system. Creating temporary card."), "Memory card %u present in system but not in save state. Removing card."),
i + 1u i + 1u);
);
m_memory_cards[i] = MemoryCard::Create();
}
else if (!card_present_in_state && m_memory_cards[i])
{
g_host_interface->AddFormattedOSDMessage(
20.0f,
g_host_interface->TranslateString(
"OSDMessage", "Memory card %u present in system but not in save state. Removing card."),
i + 1u
);
m_memory_cards[i].reset(); m_memory_cards[i].reset();
} }