loader: Make IdentifyFile typesafe

Relies on #4465 for concept.h Common::IsBaseOf
This commit is contained in:
David Marcec 2020-08-03 20:39:19 +10:00
parent 9b75481755
commit 6cfff2c3f6

View File

@ -3,8 +3,10 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <memory> #include <memory>
#include <optional>
#include <ostream> #include <ostream>
#include <string> #include <string>
#include "common/concepts.h"
#include "common/file_util.h" #include "common/file_util.h"
#include "common/logging/log.h" #include "common/logging/log.h"
#include "common/string_util.h" #include "common/string_util.h"
@ -21,27 +23,37 @@
namespace Loader { namespace Loader {
template <Common::IsBaseOf<AppLoader> T>
std::optional<FileType> IdentifyFileLoader(FileSys::VirtualFile file) {
const auto file_type = T::IdentifyType(file);
if (file_type != FileType::Error) {
return file_type;
}
return std::nullopt;
}
FileType IdentifyFile(FileSys::VirtualFile file) { FileType IdentifyFile(FileSys::VirtualFile file) {
FileType type; if (const auto romdir_type = IdentifyFileLoader<AppLoader_DeconstructedRomDirectory>(file)) {
return *romdir_type;
#define CHECK_TYPE(loader) \ } else if (const auto elf_type = IdentifyFileLoader<AppLoader_ELF>(file)) {
type = AppLoader_##loader::IdentifyType(file); \ return *elf_type;
if (FileType::Error != type) \ } else if (const auto nso_type = IdentifyFileLoader<AppLoader_NSO>(file)) {
return type; return *nso_type;
} else if (const auto nro_type = IdentifyFileLoader<AppLoader_NRO>(file)) {
CHECK_TYPE(DeconstructedRomDirectory) return *nro_type;
CHECK_TYPE(ELF) } else if (const auto nca_type = IdentifyFileLoader<AppLoader_NCA>(file)) {
CHECK_TYPE(NSO) return *nca_type;
CHECK_TYPE(NRO) } else if (const auto xci_type = IdentifyFileLoader<AppLoader_XCI>(file)) {
CHECK_TYPE(NCA) return *xci_type;
CHECK_TYPE(XCI) } else if (const auto nax_type = IdentifyFileLoader<AppLoader_NAX>(file)) {
CHECK_TYPE(NAX) return *nax_type;
CHECK_TYPE(NSP) } else if (const auto nsp_type = IdentifyFileLoader<AppLoader_NSP>(file)) {
CHECK_TYPE(KIP) return *nsp_type;
} else if (const auto kip_type = IdentifyFileLoader<AppLoader_KIP>(file)) {
#undef CHECK_TYPE return *kip_type;
} else {
return FileType::Unknown; return FileType::Unknown;
}
} }
FileType GuessFromFilename(const std::string& name) { FileType GuessFromFilename(const std::string& name) {