From 6758c77c39b094555ab2797b0419217de40e202e Mon Sep 17 00:00:00 2001 From: Starsam80 Date: Wed, 29 Dec 2021 17:49:16 -0700 Subject: [PATCH] NANDImporter: Reduce recursion in `ProcessEntry` It also simplifies the code flow, as it no longer goes backwards through the filesystem chain. --- Source/Core/DiscIO/NANDImporter.cpp | 46 +++++++++++++---------------- Source/Core/DiscIO/NANDImporter.h | 4 +-- 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/Source/Core/DiscIO/NANDImporter.cpp b/Source/Core/DiscIO/NANDImporter.cpp index 32dafdd58d..322dd548d7 100644 --- a/Source/Core/DiscIO/NANDImporter.cpp +++ b/Source/Core/DiscIO/NANDImporter.cpp @@ -129,44 +129,38 @@ std::string NANDImporter::GetPath(const NANDFSTEntry& entry, const std::string& void NANDImporter::ProcessEntry(u16 entry_number, const std::string& parent_path) { NANDFSTEntry entry; - memcpy(&entry, &m_nand[m_nand_fst_offset + sizeof(NANDFSTEntry) * entry_number], - sizeof(NANDFSTEntry)); + while (entry_number != 0xffff) + { + memcpy(&entry, &m_nand[m_nand_fst_offset + sizeof(NANDFSTEntry) * entry_number], + sizeof(NANDFSTEntry)); - if (entry.sib != 0xffff) - ProcessEntry(entry.sib, parent_path); + const std::string path = GetPath(entry, parent_path); + INFO_LOG_FMT(DISCIO, "Entry: {} Path: {}", entry, path); + m_update_callback(); - Type type = static_cast(entry.mode & 3); - if (type == Type::File) - ProcessFile(entry, parent_path); - else if (type == Type::Directory) - ProcessDirectory(entry, parent_path); - else - ERROR_LOG_FMT(DISCIO, "Ignoring unknown entry type for {}", entry); + Type type = static_cast(entry.mode & 3); + if (type == Type::File) + ProcessFile(entry, path); + else if (type == Type::Directory) + ProcessDirectory(entry, path); + else + ERROR_LOG_FMT(DISCIO, "Ignoring unknown entry type for {}", entry); + + entry_number = entry.sib; + } } -void NANDImporter::ProcessDirectory(const NANDFSTEntry& entry, const std::string& parent_path) +void NANDImporter::ProcessDirectory(const NANDFSTEntry& entry, const std::string& path) { - m_update_callback(); - INFO_LOG_FMT(DISCIO, "Path: {}", FormatDebugString(entry)); - - const std::string path = GetPath(entry, parent_path); File::CreateDir(m_nand_root + path); - - if (entry.sub != 0xffff) - ProcessEntry(entry.sub, path); - - INFO_LOG_FMT(DISCIO, "Path: {}", parent_path); + ProcessEntry(entry.sub, path); } -void NANDImporter::ProcessFile(const NANDFSTEntry& entry, const std::string& parent_path) +void NANDImporter::ProcessFile(const NANDFSTEntry& entry, const std::string& path) { constexpr size_t NAND_AES_KEY_OFFSET = 0x158; constexpr size_t NAND_FAT_BLOCK_SIZE = 0x4000; - m_update_callback(); - INFO_LOG_FMT(DISCIO, "File: {}", FormatDebugString(entry)); - - const std::string path = GetPath(entry, parent_path); File::IOFile file(m_nand_root + path, "wb"); std::array key{}; std::copy(&m_nand_keys[NAND_AES_KEY_OFFSET], &m_nand_keys[NAND_AES_KEY_OFFSET + key.size()], diff --git a/Source/Core/DiscIO/NANDImporter.h b/Source/Core/DiscIO/NANDImporter.h index 7c266d6840..eeb12c87bc 100644 --- a/Source/Core/DiscIO/NANDImporter.h +++ b/Source/Core/DiscIO/NANDImporter.h @@ -55,8 +55,8 @@ private: std::string GetPath(const NANDFSTEntry& entry, const std::string& parent_path); std::string FormatDebugString(const NANDFSTEntry& entry); void ProcessEntry(u16 entry_number, const std::string& parent_path); - void ProcessFile(const NANDFSTEntry& entry, const std::string& parent_path); - void ProcessDirectory(const NANDFSTEntry& entry, const std::string& parent_path); + void ProcessFile(const NANDFSTEntry& entry, const std::string& path); + void ProcessDirectory(const NANDFSTEntry& entry, const std::string& path); void ExportKeys(); std::string m_nand_root;