GCMemcard: Let GetDEntry() return std::optional<DEntry>.

This commit is contained in:
Admiral H. Curtiss 2018-12-01 18:17:18 +01:00
parent 860e003b0c
commit 2351a60567
3 changed files with 15 additions and 18 deletions

View File

@ -595,13 +595,12 @@ std::string GCMemcard::GetSaveComment2(u8 index) const
DENTRY_STRLEN); DENTRY_STRLEN);
} }
bool GCMemcard::GetDEntry(u8 index, DEntry& dest) const std::optional<DEntry> GCMemcard::GetDEntry(u8 index) const
{ {
if (!m_valid || index >= DIRLEN) if (!m_valid || index >= DIRLEN)
return false; return std::nullopt;
dest = GetActiveDirectory().m_dir_entries[index]; return GetActiveDirectory().m_dir_entries[index];
return true;
} }
u16 BlockAlloc::GetNextBlock(u16 Block) const u16 BlockAlloc::GetNextBlock(u16 Block) const
@ -803,8 +802,8 @@ u32 GCMemcard::CopyFrom(const GCMemcard& source, u8 index)
if (!m_valid || !source.m_valid) if (!m_valid || !source.m_valid)
return NOMEMCARD; return NOMEMCARD;
DEntry tempDEntry; std::optional<DEntry> tempDEntry = source.GetDEntry(index);
if (!source.GetDEntry(index, tempDEntry)) if (!tempDEntry)
return NOMEMCARD; return NOMEMCARD;
u32 size = source.DEntry_BlockCount(index); u32 size = source.DEntry_BlockCount(index);
@ -821,7 +820,7 @@ u32 GCMemcard::CopyFrom(const GCMemcard& source, u8 index)
return NOMEMCARD; return NOMEMCARD;
default: default:
FixChecksums(); FixChecksums();
return ImportFile(tempDEntry, saveData); return ImportFile(*tempDEntry, saveData);
} }
} }
@ -976,14 +975,12 @@ u32 GCMemcard::ExportGci(u8 index, const std::string& fileName, const std::strin
break; break;
} }
DEntry tempDEntry; std::optional<DEntry> tempDEntry = GetDEntry(index);
if (!GetDEntry(index, tempDEntry)) if (!tempDEntry)
{
return NOMEMCARD; return NOMEMCARD;
}
Gcs_SavConvert(tempDEntry, offset); Gcs_SavConvert(*tempDEntry, offset);
gci.WriteBytes(&tempDEntry, DENTRY_SIZE); gci.WriteBytes(&tempDEntry.value(), DENTRY_SIZE);
u32 size = DEntry_BlockCount(index); u32 size = DEntry_BlockCount(index);
if (size == 0xFFFF) if (size == 0xFFFF)

View File

@ -397,8 +397,9 @@ public:
u32 DEntry_CommentsAddress(u8 index) const; u32 DEntry_CommentsAddress(u8 index) const;
std::string GetSaveComment1(u8 index) const; std::string GetSaveComment1(u8 index) const;
std::string GetSaveComment2(u8 index) const; std::string GetSaveComment2(u8 index) const;
// Copies a DEntry from u8 index to DEntry& data
bool GetDEntry(u8 index, DEntry& dest) const; // Fetches a DEntry from the given file index.
std::optional<DEntry> GetDEntry(u8 index) const;
u32 GetSaveData(u8 index, std::vector<GCMBlock>& saveBlocks) const; u32 GetSaveData(u8 index, std::vector<GCMBlock>& saveBlocks) const;

View File

@ -199,12 +199,11 @@ void GCMemcardManager::UpdateSlotTable(int slot)
auto* icon = new QTableWidgetItem; auto* icon = new QTableWidgetItem;
icon->setData(Qt::DecorationRole, frames[0]); icon->setData(Qt::DecorationRole, frames[0]);
DEntry d; std::optional<DEntry> entry = memcard->GetDEntry(file_index);
memcard->GetDEntry(file_index, d);
// TODO: This is wrong, the animation speed is not static and is already correctly calculated in // TODO: This is wrong, the animation speed is not static and is already correctly calculated in
// GetIconFromSaveFile(), just not returned // GetIconFromSaveFile(), just not returned
const u16 animation_speed = d.m_animation_speed; const u16 animation_speed = entry ? entry->m_animation_speed : 1;
const auto speed = (((animation_speed >> 8) & 1) << 2) + (animation_speed & 1); const auto speed = (((animation_speed >> 8) & 1) << 2) + (animation_speed & 1);
m_slot_active_icons[slot].push_back({speed, frames}); m_slot_active_icons[slot].push_back({speed, frames});