From 296cb3df2b7956bf4d3d78512a08b906b01925be Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 6 Apr 2020 06:15:50 -0400 Subject: [PATCH] CMemoryCardDriver: Make use of std::array where applicable Stronger typing and also allows eliminating several hardcoded data sizes. --- Runtime/MP1/CMemoryCardDriver.cpp | 55 ++++++++++++++++++------------- Runtime/MP1/CMemoryCardDriver.hpp | 18 ++++++---- 2 files changed, 43 insertions(+), 30 deletions(-) diff --git a/Runtime/MP1/CMemoryCardDriver.cpp b/Runtime/MP1/CMemoryCardDriver.cpp index 8f916ed80..0cf7076e4 100644 --- a/Runtime/MP1/CMemoryCardDriver.cpp +++ b/Runtime/MP1/CMemoryCardDriver.cpp @@ -1,11 +1,13 @@ #include "Runtime/MP1/CMemoryCardDriver.hpp" +#include + #include "Runtime/CCRC32.hpp" #include "Runtime/MP1/MP1.hpp" namespace urde::MP1 { -static const char* SaveFileNames[] = {"MetroidPrime A", "MetroidPrime B"}; +constexpr std::array SaveFileNames{"MetroidPrime A", "MetroidPrime B"}; using ECardResult = kabufuda::ECardResult; using ECardSlot = kabufuda::ECardSlot; @@ -100,19 +102,19 @@ ECardResult CMemoryCardDriver::SFileInfo::GetSaveDataOffset(u32& offOut) const { CMemoryCardDriver::SGameFileSlot::SGameFileSlot() { InitializeFromGameState(); } CMemoryCardDriver::SGameFileSlot::SGameFileSlot(CMemoryInStream& in) { - in.readBytesToBuf(x0_saveBuffer, 940); - x944_fileInfo = CGameState::LoadGameFileState(x0_saveBuffer); + in.readBytesToBuf(x0_saveBuffer.data(), x0_saveBuffer.size()); + x944_fileInfo = CGameState::LoadGameFileState(x0_saveBuffer.data()); } void CMemoryCardDriver::SGameFileSlot::InitializeFromGameState() { - CBitStreamWriter w(x0_saveBuffer, 940); + CBitStreamWriter w(x0_saveBuffer.data(), x0_saveBuffer.size()); g_GameState->PutTo(w); w.Flush(); - x944_fileInfo = CGameState::LoadGameFileState(x0_saveBuffer); + x944_fileInfo = CGameState::LoadGameFileState(x0_saveBuffer.data()); } void CMemoryCardDriver::SGameFileSlot::LoadGameState(u32 idx) { - CBitStreamReader r(x0_saveBuffer, 940); + CBitStreamReader r(x0_saveBuffer.data(), x0_saveBuffer.size()); static_cast(g_Main)->StreamNewGameState(r, idx); } @@ -146,8 +148,9 @@ const CGameState::GameFileStateInfo* CMemoryCardDriver::GetGameFileStateInfo(int CMemoryCardDriver::SSaveHeader CMemoryCardDriver::LoadSaveHeader(CMemoryInStream& in) { SSaveHeader ret; ret.x0_version = in.readUint32Big(); - for (int i = 0; i < 3; ++i) - ret.x4_savePresent[i] = in.readBool(); + for (bool& present : ret.x4_savePresent) { + present = in.readBool(); + } return ret; } @@ -167,27 +170,30 @@ void CMemoryCardDriver::ReadFinished() { x20_fileTime = stat.GetTime(); CMemoryInStream r(fileInfo.x34_saveData.data(), 3004); SSaveHeader header = LoadSaveHeader(r); - r.readBytesToBuf(x30_systemData, 174); + r.readBytesToBuf(x30_systemData.data(), x30_systemData.size()); - for (int i = 0; i < 3; ++i) - if (header.x4_savePresent[i]) + for (size_t i = 0; i < xe4_fileSlots.size(); ++i) { + if (header.x4_savePresent[i]) { xe4_fileSlots[i] = LoadSaveFile(r); + } + } - if (x19d_importPersistent) + if (x19d_importPersistent) { ImportPersistentOptions(); + } } void CMemoryCardDriver::ImportPersistentOptions() { - CBitStreamReader r(x30_systemData, 174); + CBitStreamReader r(x30_systemData.data(), x30_systemData.size()); CPersistentOptions opts(r); g_GameState->ImportPersistentOptions(opts); } void CMemoryCardDriver::ExportPersistentOptions() { - CBitStreamReader r(x30_systemData, 174); + CBitStreamReader r(x30_systemData.data(), x30_systemData.size()); CPersistentOptions opts(r); g_GameState->ExportPersistentOptions(opts); - CBitStreamWriter w(x30_systemData, 174); + CBitStreamWriter w(x30_systemData.data(), x30_systemData.size()); opts.PutTo(w); } @@ -635,7 +641,7 @@ void CMemoryCardDriver::BuildNewFileSlot(u32 saveIdx) { slot = std::make_unique(); slot->LoadGameState(saveIdx); - CBitStreamReader r(x30_systemData, 174); + CBitStreamReader r(x30_systemData.data(), x30_systemData.size()); g_GameState->ReadPersistentOptions(r); ImportPersistentOptions(); g_GameState->SetCardSerial(x28_cardSerial); @@ -653,7 +659,7 @@ void CMemoryCardDriver::BuildExistingFileSlot(u32 saveIdx) { else slot->InitializeFromGameState(); - CBitStreamWriter w(x30_systemData, 174); + CBitStreamWriter w(x30_systemData.data(), x30_systemData.size()); g_GameState->PutTo(w); } @@ -674,15 +680,18 @@ void CMemoryCardDriver::InitializeFileInfo() { CMemoryOutStream w = x198_fileInfo->BeginMemoryOut(3004); SSaveHeader header; - for (int i = 0; i < 3; ++i) - header.x4_savePresent[i] = xe4_fileSlots[i].operator bool(); + for (size_t i = 0; i < xe4_fileSlots.size(); ++i) { + header.x4_savePresent[i] = xe4_fileSlots[i] != nullptr; + } header.DoPut(w); - w.writeBytes(x30_systemData, 174); + w.writeBytes(x30_systemData.data(), x30_systemData.size()); - for (int i = 0; i < 3; ++i) - if (xe4_fileSlots[i]) - xe4_fileSlots[i]->DoPut(w); + for (auto& fileSlot : xe4_fileSlots) { + if (fileSlot) { + fileSlot->DoPut(w); + } + } } void CMemoryCardDriver::WriteBackupBuf() { diff --git a/Runtime/MP1/CMemoryCardDriver.hpp b/Runtime/MP1/CMemoryCardDriver.hpp index 59a161e75..732e39181 100644 --- a/Runtime/MP1/CMemoryCardDriver.hpp +++ b/Runtime/MP1/CMemoryCardDriver.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -87,22 +88,25 @@ private: struct SSaveHeader { u32 x0_version = 0; - bool x4_savePresent[3]; + std::array x4_savePresent{}; + void DoPut(CMemoryOutStream& out) const { out.writeUint32Big(x0_version); - for (int i = 0; i < 3; ++i) - out.writeBool(x4_savePresent[i]); + for (const bool savePresent : x4_savePresent) { + out.writeBool(savePresent); + } } }; struct SGameFileSlot { - u8 x0_saveBuffer[940] = {}; + std::array x0_saveBuffer{}; CGameState::GameFileStateInfo x944_fileInfo; + SGameFileSlot(); explicit SGameFileSlot(CMemoryInStream& in); void InitializeFromGameState(); void LoadGameState(u32 idx); - void DoPut(CMemoryOutStream& w) const { w.writeBytes(x0_saveBuffer, 940); } + void DoPut(CMemoryOutStream& w) const { w.writeBytes(x0_saveBuffer.data(), x0_saveBuffer.size()); } }; enum class EFileState { Unknown, NoFile, File, BadFile }; @@ -117,8 +121,8 @@ private: s32 x1c_cardFreeFiles = 0; u32 x20_fileTime = 0; u64 x28_cardSerial = 0; - u8 x30_systemData[174] = {}; - std::unique_ptr xe4_fileSlots[3]; + std::array x30_systemData{}; + std::array, 3> xe4_fileSlots; std::vector> x100_mcFileInfos; u32 x194_fileIdx = -1; std::unique_ptr x198_fileInfo;