| 986 | } |
| 987 | |
| 988 | bool FileMcd_CreateNewCard(const std::string_view name, MemoryCardType type, MemoryCardFileType file_type) |
| 989 | { |
| 990 | const std::string full_path(Path::Combine(EmuFolders::MemoryCards, name)); |
| 991 | |
| 992 | if (type == MemoryCardType::Folder) |
| 993 | { |
| 994 | Console.WriteLn("(FileMcd) Creating new PS2 folder memory card: '%.*s'", static_cast<int>(name.size()), name.data()); |
| 995 | |
| 996 | Error error; |
| 997 | if (!FileSystem::CreateDirectoryPath(full_path.c_str(), false, &error)) |
| 998 | { |
| 999 | Host::ReportErrorAsync("Memory Card Creation Failed", |
| 1000 | fmt::format("Failed to create directory. The error was:\n{}", error.GetDescription())); |
| 1001 | return false; |
| 1002 | } |
| 1003 | |
| 1004 | // write the superblock |
| 1005 | auto fp = FileSystem::OpenManagedCFile(Path::Combine(full_path, s_folder_mem_card_id_file).c_str(), "wb", &error); |
| 1006 | if (!fp) |
| 1007 | { |
| 1008 | Host::ReportErrorAsync("Memory Card Creation Failed", fmt::format("Failed to create superblock. The error was:\n{}", error.GetDescription())); |
| 1009 | return false; |
| 1010 | } |
| 1011 | |
| 1012 | return true; |
| 1013 | } |
| 1014 | |
| 1015 | if (type == MemoryCardType::File) |
| 1016 | { |
| 1017 | if (file_type <= MemoryCardFileType::Unknown || file_type >= MemoryCardFileType::MaxCount) |
| 1018 | return false; |
| 1019 | |
| 1020 | static constexpr std::array<u32, static_cast<size_t>(MemoryCardFileType::MaxCount)> sizes = {{0, 8 * MC2_MBSIZE, 16 * MC2_MBSIZE, 32 * MC2_MBSIZE, 64 * MC2_MBSIZE, MCD_SIZE}}; |
| 1021 | |
| 1022 | const bool isPSX = (type == MemoryCardType::File && file_type == MemoryCardFileType::PS1); |
| 1023 | const u32 size = sizes[static_cast<u32>(file_type)]; |
| 1024 | if (!isPSX && size == 0) |
| 1025 | return false; |
| 1026 | |
| 1027 | Error error; |
| 1028 | auto fp = FileSystem::OpenManagedCFile(full_path.c_str(), "wb", &error); |
| 1029 | if (!fp) |
| 1030 | { |
| 1031 | Host::ReportErrorAsync(TRANSLATE_SV("MemoryCard", "Memory Card Creation Failed"), |
| 1032 | fmt::format(TRANSLATE_FS("MemoryCard", "Failed to create memory card. The error was:\n{}"), error.GetDescription())); |
| 1033 | return false; |
| 1034 | } |
| 1035 | |
| 1036 | if (!isPSX) |
| 1037 | { |
| 1038 | Console.WriteLn("(FileMcd) Creating new PS2 %uMB memory card: '%s'", size / MC2_MBSIZE, full_path.c_str()); |
| 1039 | |
| 1040 | // PS2 Memory Card |
| 1041 | u8 buf[MC2_ERASE_SIZE]; |
| 1042 | std::memset(buf, 0xff, sizeof(buf)); |
| 1043 | |
| 1044 | const u32 count = size / sizeof(buf); |
| 1045 | for (uint i = 0; i < count; i++) |