From 6cfff2c3f6487a45561ee49617141159745cd6eb Mon Sep 17 00:00:00 2001 From: David Marcec Date: Mon, 3 Aug 2020 20:39:19 +1000 Subject: loader: Make IdentifyFile typesafe Relies on #4465 for concept.h Common::IsBaseOf --- src/core/loader/loader.cpp | 52 ++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index 59ca7091a..c4905550a 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp @@ -3,8 +3,10 @@ // Refer to the license.txt file included. #include +#include #include #include +#include "common/concepts.h" #include "common/file_util.h" #include "common/logging/log.h" #include "common/string_util.h" @@ -21,27 +23,37 @@ namespace Loader { -FileType IdentifyFile(FileSys::VirtualFile file) { - FileType type; - -#define CHECK_TYPE(loader) \ - type = AppLoader_##loader::IdentifyType(file); \ - if (FileType::Error != type) \ - return type; - - CHECK_TYPE(DeconstructedRomDirectory) - CHECK_TYPE(ELF) - CHECK_TYPE(NSO) - CHECK_TYPE(NRO) - CHECK_TYPE(NCA) - CHECK_TYPE(XCI) - CHECK_TYPE(NAX) - CHECK_TYPE(NSP) - CHECK_TYPE(KIP) - -#undef CHECK_TYPE +template T> +std::optional IdentifyFileLoader(FileSys::VirtualFile file) { + const auto file_type = T::IdentifyType(file); + if (file_type != FileType::Error) { + return file_type; + } + return std::nullopt; +} - return FileType::Unknown; +FileType IdentifyFile(FileSys::VirtualFile file) { + if (const auto romdir_type = IdentifyFileLoader(file)) { + return *romdir_type; + } else if (const auto elf_type = IdentifyFileLoader(file)) { + return *elf_type; + } else if (const auto nso_type = IdentifyFileLoader(file)) { + return *nso_type; + } else if (const auto nro_type = IdentifyFileLoader(file)) { + return *nro_type; + } else if (const auto nca_type = IdentifyFileLoader(file)) { + return *nca_type; + } else if (const auto xci_type = IdentifyFileLoader(file)) { + return *xci_type; + } else if (const auto nax_type = IdentifyFileLoader(file)) { + return *nax_type; + } else if (const auto nsp_type = IdentifyFileLoader(file)) { + return *nsp_type; + } else if (const auto kip_type = IdentifyFileLoader(file)) { + return *kip_type; + } else { + return FileType::Unknown; + } } FileType GuessFromFilename(const std::string& name) { -- cgit v1.2.3