| 893 | } |
| 894 | |
| 895 | bool FolderMemoryCard::ReadFromFile(u8* dest, u32 adr, u32 dataLength) |
| 896 | { |
| 897 | const u32 page = adr / PageSizeRaw; |
| 898 | const u32 offset = adr % PageSizeRaw; |
| 899 | const u32 cluster = adr / ClusterSizeRaw; |
| 900 | const u32 fatCluster = cluster - m_superBlock.data.alloc_offset; |
| 901 | |
| 902 | // if the cluster is unused according to FAT, just return |
| 903 | if ((m_fat.data[0][0][fatCluster] & DataClusterInUseMask) == 0) |
| 904 | { |
| 905 | return false; |
| 906 | } |
| 907 | |
| 908 | // figure out which file to read from |
| 909 | auto it = m_fileMetadataQuickAccess.find(fatCluster); |
| 910 | if (it != m_fileMetadataQuickAccess.end()) |
| 911 | { |
| 912 | const u32 clusterNumber = it->second.consecutiveCluster; |
| 913 | std::FILE* file = m_lastAccessedFile.ReOpen(m_folderName, &it->second); |
| 914 | if (file) |
| 915 | { |
| 916 | const u32 clusterOffset = (page % 2) * PageSize + offset; |
| 917 | const u32 fileOffset = clusterNumber * ClusterSize + clusterOffset; |
| 918 | |
| 919 | size_t bytesRead = 0; |
| 920 | if (fileOffset == FileSystem::FTell64(file) || FileSystem::FSeek64(file, fileOffset, SEEK_SET) == 0) |
| 921 | bytesRead = std::fread(dest, 1, dataLength, file); |
| 922 | |
| 923 | // if more bytes were requested than actually exist, fill the rest with 0xFF |
| 924 | if (bytesRead < dataLength) |
| 925 | { |
| 926 | memset(&dest[bytesRead], 0xFF, dataLength - bytesRead); |
| 927 | } |
| 928 | |
| 929 | return bytesRead > 0; |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | return false; |
| 934 | } |
| 935 | |
| 936 | s32 FolderMemoryCard::Read(u8* dest, u32 adr, int size) |
| 937 | { |