diff --git a/src/core/hle/romfs.cpp b/src/core/hle/romfs.cpp index 3157df71d0..8278b50dec 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 ee9f297606..dfe2ee788d 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