diff --git a/asm/Kyoto/DolphinCMemoryCardSys.s b/asm/Kyoto/DolphinCMemoryCardSys.s index ed952241..9c6874c7 100644 --- a/asm/Kyoto/DolphinCMemoryCardSys.s +++ b/asm/Kyoto/DolphinCMemoryCardSys.s @@ -838,8 +838,8 @@ Close__13CCardFileInfoFv: /* 8034DF40 0034AEA0 38 21 00 10 */ addi r1, r1, 0x10 /* 8034DF44 0034AEA4 4E 80 00 20 */ blr -.global Close__19SMemoryCardFileInfo -Close__19SMemoryCardFileInfo: +.global Close__19SMemoryCardFileInfoFv +Close__19SMemoryCardFileInfoFv: /* 8034DF48 0034AEA8 94 21 FF F0 */ stwu r1, -0x10(r1) /* 8034DF4C 0034AEAC 7C 08 02 A6 */ mflr r0 /* 8034DF50 0034AEB0 90 01 00 14 */ stw r0, 0x14(r1) diff --git a/asm/MetroidPrime/CMemoryCardDriver.s b/asm/MetroidPrime/CMemoryCardDriver.s index 134a7edd..6d2f8f95 100644 --- a/asm/MetroidPrime/CMemoryCardDriver.s +++ b/asm/MetroidPrime/CMemoryCardDriver.s @@ -1904,7 +1904,7 @@ lbl_8024DB90: /* 8024DB98 0024AAF8 48 00 01 60 */ b lbl_8024DCF8 lbl_8024DB9C: /* 8024DB9C 0024AAFC 38 7E 00 04 */ addi r3, r30, 4 -/* 8024DBA0 0024AB00 48 10 03 A9 */ bl Close__19SMemoryCardFileInfo +/* 8024DBA0 0024AB00 48 10 03 A9 */ bl Close__19SMemoryCardFileInfoFv /* 8024DBA4 0024AB04 2C 03 FF FD */ cmpwi r3, -3 /* 8024DBA8 0024AB08 40 82 00 1C */ bne lbl_8024DBC4 /* 8024DBAC 0024AB0C 7F E3 FB 78 */ mr r3, r31 @@ -2535,7 +2535,7 @@ UpdateFileRead__17CMemoryCardDriverF11ECardResult: /* 8024E3D4 0024B334 1C 60 00 48 */ mulli r3, r0, 0x48 /* 8024E3D8 0024B338 38 63 00 04 */ addi r3, r3, 4 /* 8024E3DC 0024B33C 7C 7D 1A 14 */ add r3, r29, r3 -/* 8024E3E0 0024B340 48 0F FB 69 */ bl Close__19SMemoryCardFileInfo +/* 8024E3E0 0024B340 48 0F FB 69 */ bl Close__19SMemoryCardFileInfoFv /* 8024E3E4 0024B344 2C 03 00 00 */ cmpwi r3, 0 /* 8024E3E8 0024B348 41 82 00 10 */ beq lbl_8024E3F8 /* 8024E3EC 0024B34C 7F E3 FB 78 */ mr r3, r31 diff --git a/include/MetroidPrime/CMemoryCardDriver.hpp b/include/MetroidPrime/CMemoryCardDriver.hpp index 3ba58f55..af31b12b 100644 --- a/include/MetroidPrime/CMemoryCardDriver.hpp +++ b/include/MetroidPrime/CMemoryCardDriver.hpp @@ -117,7 +117,7 @@ private: rstl::reserved_vector< u8, 174 > x30_systemData; rstl::reserved_vector< rstl::auto_ptr< SGameFileSlot >, 3 > xe4_fileSlots; rstl::reserved_vector< rstl::pair< EFileState, SMemoryCardFileInfo >, 2 > x100_mcFileInfos; - u32 x194_fileIdx; + int x194_fileIdx; rstl::single_ptr< CMemoryCardSys::CCardFileInfo > x198_fileInfo; bool x19c_; bool x19d_importPersistent; @@ -154,6 +154,8 @@ public: void NoCardFound(); void IndexFiles(); void StartFileDeleteBad(); + void StartFileDeleteAlt(); + void StartFileRead(); void StartFileCreate(); void StartFileWrite(); void StartFileCreateTransactional(); diff --git a/src/MetroidPrime/CMemoryCardDriver.cpp b/src/MetroidPrime/CMemoryCardDriver.cpp index a8d2d168..b75098ec 100644 --- a/src/MetroidPrime/CMemoryCardDriver.cpp +++ b/src/MetroidPrime/CMemoryCardDriver.cpp @@ -6,40 +6,33 @@ bool CMemoryCardDriver::IsCardBusy(EState) { return false; } bool CMemoryCardDriver::IsCardWriting(EState) { return false; } -CMemoryCardDriver::CMemoryCardDriver(CMemoryCardSys::EMemoryCardPort cardPort, CAssetId saveBanner, CAssetId saveIcon0, CAssetId saveIcon1, - bool importPersistent) - : x0_cardPort(cardPort) - , x4_saveBanner(saveBanner) - , x8_saveIcon0(saveIcon0) - , xc_saveIcon1(saveIcon1) - , x10_state(kS_Initial) - , x14_error(kE_OK) - , x18_cardFreeBytes(0) - , x1c_cardFreeFiles(0) - , x20_fileTime(0) - , x28_cardSerial(0) +CMemoryCardDriver::CMemoryCardDriver(CMemoryCardSys::EMemoryCardPort cardPort, CAssetId saveBanner, + CAssetId saveIcon0, CAssetId saveIcon1, bool importPersistent) +: x0_cardPort(cardPort) +, x4_saveBanner(saveBanner) +, x8_saveIcon0(saveIcon0) +, xc_saveIcon1(saveIcon1) +, x10_state(kS_Initial) +, x14_error(kE_OK) +, x18_cardFreeBytes(0) +, x1c_cardFreeFiles(0) +, x20_fileTime(0) +, x28_cardSerial(0) - , x30_systemData(0) - , xe4_fileSlots(nullptr) - , x100_mcFileInfos() - , x194_fileIdx(-1) - , x198_fileInfo(nullptr) - , x19c_(false) - , x19d_importPersistent(importPersistent) -{ +, x30_systemData(0) +, xe4_fileSlots(nullptr) +, x100_mcFileInfos() +, x194_fileIdx(-1) +, x198_fileInfo(nullptr) +, x19c_(false) +, x19d_importPersistent(importPersistent) { x100_mcFileInfos.push_back(rstl::pair< EFileState, SMemoryCardFileInfo >( - kFS_Unknown, - SMemoryCardFileInfo(x0_cardPort, rstl::string_l("MetroidPrime A")) - )); + kFS_Unknown, SMemoryCardFileInfo(x0_cardPort, rstl::string_l("MetroidPrime A")))); x100_mcFileInfos.push_back(rstl::pair< EFileState, SMemoryCardFileInfo >( - kFS_Unknown, - SMemoryCardFileInfo(x0_cardPort, rstl::string_l("MetroidPrime B")) - )); + kFS_Unknown, SMemoryCardFileInfo(x0_cardPort, rstl::string_l("MetroidPrime B")))); } -void CMemoryCardDriver::ClearFileInfo() { - x198_fileInfo = nullptr; -} +void CMemoryCardDriver::ClearFileInfo() { x198_fileInfo = nullptr; } CMemoryCardDriver::~CMemoryCardDriver() {} @@ -155,13 +148,47 @@ void CMemoryCardDriver::UpdateCardCheck(ECardResult result) { } else if (result == kCR_BROKEN) { x10_state = kS_CardCheckFailed; x14_error = kE_CardBroken; - + } else { HandleCardError(result, kS_CardCheckFailed); } } -void CMemoryCardDriver::UpdateFileRead(ECardResult) {} +void CMemoryCardDriver::UpdateFileRead(ECardResult result) { + if (result == kCR_READY) { + ECardResult readRes = x100_mcFileInfos[x194_fileIdx].second.TryFileRead(); + if (x100_mcFileInfos[x194_fileIdx].second.Close() != kCR_READY) { + NoCardFound(); + return; + } + + int altFileIdx = !bool(x194_fileIdx); + if (readRes == kCR_READY) { + x10_state = kS_Ready; + ReadFinished(); + EFileState fileSt = x100_mcFileInfos[altFileIdx].first; + if (fileSt != kFS_NoFile) { + StartFileDeleteAlt(); + } else { + CheckCardCapacity(); + } + return; + } + + if (readRes == kCR_CRC_MISMATCH) { + x100_mcFileInfos[x194_fileIdx].first = kFS_BadFile; + if (x100_mcFileInfos[altFileIdx].first == kFS_File) { + x10_state = kS_CardCheckDone; + IndexFiles(); + } else { + x10_state = kS_FileBad; + x14_error = kE_FileCorrupted; + } + } + } else { + HandleCardError(result, kS_FileBad); + } +} void CMemoryCardDriver::UpdateFileDeleteAlt(ECardResult) {} @@ -203,6 +230,10 @@ void CMemoryCardDriver::IndexFiles() {} void CMemoryCardDriver::StartFileDeleteBad() {} +void CMemoryCardDriver::StartFileDeleteAlt() {} + +void CMemoryCardDriver::StartFileRead() {} + void CMemoryCardDriver::StartFileCreate() {} void CMemoryCardDriver::StartFileWrite() {}