From b62978b5a101794345551f4b7722397d9d9d785b Mon Sep 17 00:00:00 2001 From: B3n30 Date: Thu, 26 Jul 2018 12:04:17 +0200 Subject: [PATCH] RomFS: add RomFSFile and GetRomFSFile --- src/core/hle/romfs.cpp | 24 +++++++++++++++++++++--- src/core/hle/romfs.h | 21 +++++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/core/hle/romfs.cpp b/src/core/hle/romfs.cpp index 3157df71d..8278b50de 100644 --- a/src/core/hle/romfs.cpp +++ b/src/core/hle/romfs.cpp @@ -53,7 +53,24 @@ static bool MatchName(const u8* buffer, u32 name_length, const std::u16string& n return name == std::u16string(name_buffer.begin(), name_buffer.end()); } +RomFSFile::RomFSFile() : data(nullptr), length(0) {} + +RomFSFile::RomFSFile(const u8* data, u64 length) : data(data), length(length) {} + +const u8* RomFSFile::Data() const { + return data; +} + +u64 RomFSFile::Length() const { + return length; +} + const u8* GetFilePointer(const u8* romfs, const std::vector& path) { + RomFSFile file = GetFile(romfs, path); + return file.Data(); +} + +const RomFSFile GetFile(const u8* romfs, const std::vector& path) { constexpr u32 INVALID_FIELD = 0xFFFFFFFF; // Split path into directory names and file name @@ -73,7 +90,7 @@ const u8* GetFilePointer(const u8* romfs, const std::vector& pat child_dir_offset = dir.first_child_dir_offset; while (true) { if (child_dir_offset == INVALID_FIELD) { - return nullptr; + return RomFSFile(); } const u8* current_child_dir = romfs + header.dir_table_offset + child_dir_offset; std::memcpy(&dir, current_child_dir, sizeof(dir)); @@ -92,11 +109,12 @@ const u8* GetFilePointer(const u8* romfs, const std::vector& pat const u8* current_file = romfs + header.file_table_offset + file_offset; std::memcpy(&file, current_file, sizeof(file)); if (MatchName(current_file + sizeof(file), file.name_length, file_name)) { - return romfs + header.data_offset + file.data_offset; + RomFSFile res(romfs + header.data_offset + file.data_offset, file.data_length); + return res; } file_offset = file.next_file_offset; } - return nullptr; + return RomFSFile(); } } // namespace RomFS diff --git a/src/core/hle/romfs.h b/src/core/hle/romfs.h index ee9f29760..dfe2ee788 100644 --- a/src/core/hle/romfs.h +++ b/src/core/hle/romfs.h @@ -10,6 +10,18 @@ namespace RomFS { +class RomFSFile { +public: + RomFSFile(); + RomFSFile(const u8* data, u64 length); + const u8* Data() const; + u64 Length() const; + +private: + const u8* data; + u64 length; +}; + /** * Gets the pointer to a file in a RomFS image. * @param romfs The pointer to the RomFS image @@ -19,4 +31,13 @@ namespace RomFS { */ const u8* GetFilePointer(const u8* romfs, const std::vector& path); +/** + * Gets a RomFSFile class to a file in a RomFS image. + * @param romfs The pointer to the RomFS image + * @param path A vector containing the directory names and file name of the path to the file + * @return the RomFSFile to the file + * @todo reimplement this with a full RomFS manager + */ +const RomFSFile GetFile(const u8* romfs, const std::vector& path); + } // namespace RomFS