From 77c684c1140f6bf3fb7d4560d06d2efb1a2ee5e2 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Fri, 6 Jul 2018 10:51:32 -0400 Subject: Virtual Filesystem (#597) * Add VfsFile and VfsDirectory classes * Finish abstract Vfs classes * Implement RealVfsFile (computer fs backend) * Finish RealVfsFile and RealVfsDirectory * Finished OffsetVfsFile * More changes * Fix import paths * Major refactor * Remove double const * Use experimental/filesystem or filesystem depending on compiler * Port partition_filesystem * More changes * More Overhaul * FSP_SRV fixes * Fixes and testing * Try to get filesystem to compile * Filesystem on linux * Remove std::filesystem and document/test * Compile fixes * Missing include * Bug fixes * Fixes * Rename v_file and v_dir * clang-format fix * Rename NGLOG_* to LOG_* * Most review changes * Fix TODO * Guess 'main' to be Directory by filename --- src/core/loader/loader.cpp | 63 ++++++++++++++++++---------------------------- 1 file changed, 25 insertions(+), 38 deletions(-) (limited to 'src/core/loader/loader.cpp') diff --git a/src/core/loader/loader.cpp b/src/core/loader/loader.cpp index 8831d8e83..1574345a1 100644 --- a/src/core/loader/loader.cpp +++ b/src/core/loader/loader.cpp @@ -6,6 +6,7 @@ #include #include "common/logging/log.h" #include "common/string_util.h" +#include "core/file_sys/vfs_real.h" #include "core/hle/kernel/process.h" #include "core/loader/deconstructed_rom_directory.h" #include "core/loader/elf.h" @@ -21,11 +22,11 @@ const std::initializer_list default_address_mappings = { {0x1F000000, 0x600000, false}, // entire VRAM }; -FileType IdentifyFile(FileUtil::IOFile& file, const std::string& filepath) { +FileType IdentifyFile(FileSys::VirtualFile file) { FileType type; #define CHECK_TYPE(loader) \ - type = AppLoader_##loader::IdentifyType(file, filepath); \ + type = AppLoader_##loader::IdentifyType(file); \ if (FileType::Error != type) \ return type; @@ -41,25 +42,22 @@ FileType IdentifyFile(FileUtil::IOFile& file, const std::string& filepath) { } FileType IdentifyFile(const std::string& file_name) { - FileUtil::IOFile file(file_name, "rb"); - if (!file.IsOpen()) { - LOG_ERROR(Loader, "Failed to load file {}", file_name); - return FileType::Unknown; - } - - return IdentifyFile(file, file_name); + return IdentifyFile(FileSys::VirtualFile(std::make_shared(file_name))); } -FileType GuessFromExtension(const std::string& extension_) { - std::string extension = Common::ToLower(extension_); +FileType GuessFromFilename(const std::string& name) { + if (name == "main") + return FileType::DeconstructedRomDirectory; - if (extension == ".elf") + const std::string extension = Common::ToLower(FileUtil::GetExtensionFromFilename(name)); + + if (extension == "elf") return FileType::ELF; - else if (extension == ".nro") + if (extension == "nro") return FileType::NRO; - else if (extension == ".nso") + if (extension == "nso") return FileType::NSO; - else if (extension == ".nca") + if (extension == "nca") return FileType::NCA; return FileType::Unknown; @@ -93,58 +91,47 @@ const char* GetFileTypeString(FileType type) { * @param filepath the file full path (with name) * @return std::unique_ptr a pointer to a loader object; nullptr for unsupported type */ -static std::unique_ptr GetFileLoader(FileUtil::IOFile&& file, FileType type, - const std::string& filename, - const std::string& filepath) { +static std::unique_ptr GetFileLoader(FileSys::VirtualFile file, FileType type) { switch (type) { // Standard ELF file format. case FileType::ELF: - return std::make_unique(std::move(file), filename); + return std::make_unique(std::move(file)); // NX NSO file format. case FileType::NSO: - return std::make_unique(std::move(file), filepath); + return std::make_unique(std::move(file)); // NX NRO file format. case FileType::NRO: - return std::make_unique(std::move(file), filepath); + return std::make_unique(std::move(file)); // NX NCA file format. case FileType::NCA: - return std::make_unique(std::move(file), filepath); + return std::make_unique(std::move(file)); // NX deconstructed ROM directory. case FileType::DeconstructedRomDirectory: - return std::make_unique(std::move(file), filepath); + return std::make_unique(std::move(file)); default: return nullptr; } } -std::unique_ptr GetLoader(const std::string& filename) { - FileUtil::IOFile file(filename, "rb"); - if (!file.IsOpen()) { - LOG_ERROR(Loader, "Failed to load file {}", filename); - return nullptr; - } - - std::string filename_filename, filename_extension; - Common::SplitPath(filename, nullptr, &filename_filename, &filename_extension); - - FileType type = IdentifyFile(file, filename); - FileType filename_type = GuessFromExtension(filename_extension); +std::unique_ptr GetLoader(FileSys::VirtualFile file) { + FileType type = IdentifyFile(file); + FileType filename_type = GuessFromFilename(file->GetName()); if (type != filename_type) { - LOG_WARNING(Loader, "File {} has a different type than its extension.", filename); + LOG_WARNING(Loader, "File {} has a different type than its extension.", file->GetName()); if (FileType::Unknown == type) type = filename_type; } - LOG_DEBUG(Loader, "Loading file {} as {}...", filename, GetFileTypeString(type)); + LOG_DEBUG(Loader, "Loading file {} as {}...", file->GetName(), GetFileTypeString(type)); - return GetFileLoader(std::move(file), type, filename_filename, filename); + return GetFileLoader(std::move(file), type); } } // namespace Loader -- cgit v1.2.3