@@ -338,43 +338,60 @@ void FileMemoryCard::Open()
338338 }
339339
340340 if (!m_file[slot])
341+ goto memoryCardOpenFailed;
342+
343+ m_fileSize[slot] = FileSystem::FSize64 (m_file[slot]);
344+
345+ m_mapping_handles[slot] = HostSys::CreateMappingFromFile (m_file[slot]);
346+ if (!m_mapping_handles[slot])
341347 {
342- Host::ReportErrorAsync (TRANSLATE_SV (" MemoryCard" , " Memory Card Read Failed" ),
343- fmt::format (TRANSLATE_FS (" MemoryCard" , " Unable to access memory card:\n\n {}\n\n "
344- " Another instance of PCSX2 may be using this memory card "
345- " or the memory card is stored in a write-protected folder.\n "
346- " Close any other instances of PCSX2, or restart your computer.\n " ),
347- fname));
348+ Console.Warning (" MemoryCardFile: CreateMappingFromFile failed!" );
349+ goto memoryCardOpenFailed;
348350 }
349- else // Load memory map and checksum
351+
352+ m_mappings[slot] = static_cast <u8 *>(HostSys::MapMapping (m_mapping_handles[slot], m_fileSize[slot], PageAccess_ReadWrite ()));
353+ if (!m_mappings[slot] || reinterpret_cast <intptr_t >(m_mappings[slot]) < 0 )
350354 {
351- m_fileSize[slot] = FileSystem::FSize64 (m_file[slot]);
355+ Console.Warning (" MemoryCardFile: MapSharedMemory failed! %d. %s" , errno, strerror (errno));
356+ goto memoryCardOpenFailed;
357+ }
352358
353- m_mapping_handles[slot] = HostSys::CreateMappingFromFile (m_file[slot]);
354- if (!m_mapping_handles[slot])
355- {
356- Console.Warning (" CreateMappingFromFile failed!" );
357- }
359+ Console.WriteLnFmt (Color_Green, " McdSlot {} [File]: {} [{} MB, {}]" , slot, Path::GetFileName (fname),
360+ (m_fileSize[slot] + (MCD_SIZE + 1 )) / MC2_MBSIZE ,
361+ FileMcd_IsMemoryCardFormatted (m_file[slot]) ? " Formatted" : " UNFORMATTED" );
358362
359- m_mappings[slot] = static_cast <u8 *>(HostSys::MapMapping (m_mapping_handles[slot], m_fileSize[slot], PageAccess_ReadWrite ()));
360- if (!m_mappings[slot])
361- {
362- Console.Warning (" MapSharedMemory failed! %d. %s" , errno, strerror (errno));
363- }
363+ m_filenames[slot] = std::move (fname);
364+ m_ispsx[slot] = m_fileSize[slot] == 0x20000 ;
365+ m_chkaddr = 0x210 ;
366+
367+ if (!m_ispsx[slot])
368+ {
369+ std::memcpy (&m_chksum[slot], m_mappings[slot] + m_chkaddr, sizeof (m_chksum[slot]));
370+ }
364371
365- Console.WriteLnFmt (Color_Green, " McdSlot {} [File]: {} [{} MB, {}]" , slot, Path::GetFileName (fname),
366- (m_fileSize[slot] + (MCD_SIZE + 1 )) / MC2_MBSIZE ,
367- FileMcd_IsMemoryCardFormatted (m_file[slot]) ? " Formatted" : " UNFORMATTED" );
372+ continue ;
368373
369- m_filenames[slot] = std::move (fname);
370- m_ispsx[slot] = m_fileSize[slot] == 0x20000 ;
371- m_chkaddr = 0x210 ;
374+ memoryCardOpenFailed:
375+ Host::ReportErrorAsync (TRANSLATE_SV (" MemoryCard" , " Memory Card Read Failed" ),
376+ fmt::format (TRANSLATE_FS (" MemoryCard" , " Unable to access memory card:\n\n {}\n\n "
377+ " Another instance of PCSX2 may be using this memory card "
378+ " or the memory card is stored in a write-protected folder.\n "
379+ " Close any other instances of PCSX2, or restart your computer.\n " ),
380+ fname));
372381
373- if (!m_ispsx[slot])
374- {
375- std::memcpy (&m_chksum[slot], m_mappings[slot] + m_chkaddr, sizeof (m_chksum[slot]));
376- }
382+ if (m_mapping_handles[slot])
383+ {
384+ HostSys::DestroyMapping (m_mapping_handles[slot]);
377385 }
386+
387+ if (m_file[slot])
388+ {
389+ std::fclose (m_file[slot]);
390+ m_file[slot] = nullptr ;
391+ }
392+
393+ m_filenames[slot] = {};
394+ m_fileSize[slot] = -1 ;
378395 }
379396}
380397
0 commit comments