Merge pull request #4466 from ogniK5377/loader-type-safe

loader: Make IdentifyFile typesafe
This commit is contained in:
bunnei 2020-08-05 12:34:25 -04:00 committed by GitHub
commit 07691f994a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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,41 @@
namespace Loader { namespace Loader {
namespace {
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;
}
} // namespace
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) {