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