Merge pull request #4007 from B3n30/romfs_file

RomFS: add RomFSFile and RomFS::GetFile
This commit is contained in:
Weiyi Wang 2018-07-27 11:24:31 +03:00 committed by GitHub
commit 719bec84cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 11 deletions

View file

@ -53,7 +53,17 @@ static bool MatchName(const u8* buffer, u32 name_length, const std::u16string& n
return name == std::u16string(name_buffer.begin(), name_buffer.end()); return name == std::u16string(name_buffer.begin(), name_buffer.end());
} }
const u8* GetFilePointer(const u8* romfs, const std::vector<std::u16string>& path) { 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 RomFSFile GetFile(const u8* romfs, const std::vector<std::u16string>& path) {
constexpr u32 INVALID_FIELD = 0xFFFFFFFF; constexpr u32 INVALID_FIELD = 0xFFFFFFFF;
// Split path into directory names and file name // Split path into directory names and file name
@ -73,7 +83,7 @@ const u8* GetFilePointer(const u8* romfs, const std::vector<std::u16string>& pat
child_dir_offset = dir.first_child_dir_offset; child_dir_offset = dir.first_child_dir_offset;
while (true) { while (true) {
if (child_dir_offset == INVALID_FIELD) { if (child_dir_offset == INVALID_FIELD) {
return nullptr; return RomFSFile();
} }
const u8* current_child_dir = romfs + header.dir_table_offset + child_dir_offset; const u8* current_child_dir = romfs + header.dir_table_offset + child_dir_offset;
std::memcpy(&dir, current_child_dir, sizeof(dir)); std::memcpy(&dir, current_child_dir, sizeof(dir));
@ -92,11 +102,11 @@ const u8* GetFilePointer(const u8* romfs, const std::vector<std::u16string>& pat
const u8* current_file = romfs + header.file_table_offset + file_offset; const u8* current_file = romfs + header.file_table_offset + file_offset;
std::memcpy(&file, current_file, sizeof(file)); std::memcpy(&file, current_file, sizeof(file));
if (MatchName(current_file + sizeof(file), file.name_length, file_name)) { if (MatchName(current_file + sizeof(file), file.name_length, file_name)) {
return romfs + header.data_offset + file.data_offset; return RomFSFile(romfs + header.data_offset + file.data_offset, file.data_length);
} }
file_offset = file.next_file_offset; file_offset = file.next_file_offset;
} }
return nullptr; return RomFSFile();
} }
} // namespace RomFS } // namespace RomFS

View file

@ -10,13 +10,25 @@
namespace RomFS { namespace RomFS {
class RomFSFile {
public:
RomFSFile() = default;
RomFSFile(const u8* data, u64 length);
const u8* Data() const;
u64 Length() const;
private:
const u8* data = nullptr;
u64 length = 0;
};
/** /**
* Gets the pointer to a file in a RomFS image. * Gets a RomFSFile class to a file in a RomFS image.
* @param romfs The pointer to the 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 * @param path A vector containing the directory names and file name of the path to the file
* @return the pointer to the file * @return the RomFSFile to the file
* @todo reimplement this with a full RomFS manager * @todo reimplement this with a full RomFS manager
*/ */
const u8* GetFilePointer(const u8* romfs, const std::vector<std::u16string>& path); const RomFSFile GetFile(const u8* romfs, const std::vector<std::u16string>& path);
} // namespace RomFS } // namespace RomFS

View file

@ -143,9 +143,9 @@ bool Module::LoadSharedFont() {
const char16_t* file_name[4] = {u"cbf_std.bcfnt.lz", u"cbf_zh-Hans-CN.bcfnt.lz", const char16_t* file_name[4] = {u"cbf_std.bcfnt.lz", u"cbf_zh-Hans-CN.bcfnt.lz",
u"cbf_ko-Hang-KR.bcfnt.lz", u"cbf_zh-Hant-TW.bcfnt.lz"}; u"cbf_ko-Hang-KR.bcfnt.lz", u"cbf_zh-Hant-TW.bcfnt.lz"};
const u8* font_file = const RomFS::RomFSFile font_file =
RomFS::GetFilePointer(romfs_buffer.data(), {file_name[font_region_code - 1]}); RomFS::GetFile(romfs_buffer.data(), {file_name[font_region_code - 1]});
if (font_file == nullptr) if (font_file.Data() == nullptr)
return false; return false;
struct { struct {
@ -159,7 +159,7 @@ bool Module::LoadSharedFont() {
shared_font_header.status = 2; // successfully loaded shared_font_header.status = 2; // successfully loaded
shared_font_header.region = font_region_code; shared_font_header.region = font_region_code;
shared_font_header.decompressed_size = shared_font_header.decompressed_size =
DecompressLZ11(font_file, shared_font_mem->GetPointer(0x80)); DecompressLZ11(font_file.Data(), shared_font_mem->GetPointer(0x80));
std::memcpy(shared_font_mem->GetPointer(), &shared_font_header, sizeof(shared_font_header)); std::memcpy(shared_font_mem->GetPointer(), &shared_font_header, sizeof(shared_font_header));
*shared_font_mem->GetPointer(0x83) = 'U'; // Change the magic from "CFNT" to "CFNU" *shared_font_mem->GetPointer(0x83) = 'U'; // Change the magic from "CFNT" to "CFNU"