From 9496c04ac5ad39816f81ff94bc33de2c4fc11dec Mon Sep 17 00:00:00 2001 From: Gliniak Date: Sun, 3 Sep 2023 21:31:17 +0200 Subject: [PATCH] [VFS] Zarchive cleanup & style changes --- src/xenia/vfs/devices/disc_zarchive_device.cc | 69 ++++++++++++------- src/xenia/vfs/devices/disc_zarchive_file.cc | 5 +- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/src/xenia/vfs/devices/disc_zarchive_device.cc b/src/xenia/vfs/devices/disc_zarchive_device.cc index ff31d660e..d5181c889 100644 --- a/src/xenia/vfs/devices/disc_zarchive_device.cc +++ b/src/xenia/vfs/devices/disc_zarchive_device.cc @@ -32,12 +32,13 @@ DiscZarchiveDevice::DiscZarchiveDevice(const std::string_view mount_path, DiscZarchiveDevice::~DiscZarchiveDevice() { ZArchiveReader* reader = static_cast(opaque_); - if (reader != nullptr) delete reader; + if (reader != nullptr) { + delete reader; + } }; bool DiscZarchiveDevice::Initialize() { - ZArchiveReader* reader = nullptr; - reader = ZArchiveReader::OpenFromFile(host_path_); + ZArchiveReader* reader = ZArchiveReader::OpenFromFile(host_path_); if (!reader) { XELOGE("Disc ZArchive could not be opened"); @@ -45,10 +46,10 @@ bool DiscZarchiveDevice::Initialize() { } opaque_ = static_cast(reader); - bool result = false; - - result = reader->IsFile(reader->LookUp("default.xex", true, false)); - if (!result) XELOGE("Failed to verify disc ZArchive (no default.xex)"); + bool result = reader->IsFile(reader->LookUp("default.xex", true, false)); + if (!result) { + XELOGE("Failed to verify disc ZArchive (no default.xex)"); + } const std::string root_path = std::string("/"); ZArchiveNodeHandle handle = reader->LookUp(root_path); @@ -75,12 +76,14 @@ Entry* DiscZarchiveDevice::ResolvePath(const std::string_view path) { XELOGFS("DiscZarchiveDevice::ResolvePath({})", path); ZArchiveReader* reader = static_cast(opaque_); - if (!reader) return nullptr; + if (!reader) { + return nullptr; + } ZArchiveNodeHandle handle = reader->LookUp(path); - bool result = (handle != ZARCHIVE_INVALID_NODE); - - if (!result) return nullptr; + if (handle == ZARCHIVE_INVALID_NODE) { + return nullptr; + } return root_entry_->ResolvePath(path); } @@ -90,30 +93,55 @@ bool DiscZarchiveDevice::ReadAllEntries(void* opaque, const std::string& path, DiscZarchiveEntry* parent) { ZArchiveReader* reader = static_cast(opaque); ZArchiveNodeHandle handle = node->handle_; - if (handle == ZARCHIVE_INVALID_NODE) return false; + + if (handle == ZARCHIVE_INVALID_NODE) { + return false; + } + + if (reader->IsFile(handle)) { + auto entry = new DiscZarchiveEntry(this, parent, path, opaque); + entry->attributes_ = kFileAttributeReadOnly; + entry->handle_ = static_cast(handle); + entry->parent_ = parent; + entry->children_.push_back(std::unique_ptr(entry)); + return true; + } + if (reader->IsDirectory(handle)) { - uint32_t count = reader->GetDirEntryCount(handle); + const uint32_t count = reader->GetDirEntryCount(handle); + for (uint32_t i = 0; i < count; i++) { ZArchiveReader::DirEntry dirEntry; - if (!reader->GetDirEntry(handle, i, dirEntry)) return false; - std::string full_path = path + std::string(dirEntry.name); + if (!reader->GetDirEntry(handle, i, dirEntry)) { + XELOGE("Invalid ZArchive directory! Skipping loading"); + return false; + } + + const std::string full_path = path + std::string(dirEntry.name); ZArchiveNodeHandle fileHandle = reader->LookUp(full_path); - if (handle == ZARCHIVE_INVALID_NODE) return false; + if (handle == ZARCHIVE_INVALID_NODE) { + return false; + } + auto entry = new DiscZarchiveEntry(this, parent, full_path, opaque); entry->handle_ = static_cast(fileHandle); entry->data_offset_ = 0; + // Set to January 1, 1970 (UTC) in 100-nanosecond intervals entry->create_timestamp_ = 10000 * 11644473600000LL; entry->access_timestamp_ = 10000 * 11644473600000LL; entry->write_timestamp_ = 10000 * 11644473600000LL; entry->parent_ = node; + + if (dirEntry.isDirectory) { entry->data_size_ = 0; entry->size_ = dirEntry.size; entry->attributes_ = kFileAttributeDirectory | kFileAttributeReadOnly; node->children_.push_back(std::unique_ptr(entry)); - if (!ReadAllEntries(reader, full_path + "\\", entry, node)) + if (!ReadAllEntries(reader, full_path + "\\", entry, node)) { return false; + } } else if (dirEntry.isFile) { entry->data_size_ = entry->size_ = reader->GetFileSize(fileHandle); entry->attributes_ = kFileAttributeReadOnly; @@ -123,13 +151,6 @@ bool DiscZarchiveDevice::ReadAllEntries(void* opaque, const std::string& path, } } return true; - } else if (reader->IsFile(handle)) { - auto entry = new DiscZarchiveEntry(this, parent, path, opaque); - entry->attributes_ = kFileAttributeReadOnly; - entry->handle_ = static_cast(handle); - entry->parent_ = parent; - entry->children_.push_back(std::unique_ptr(entry)); - return true; } return false; diff --git a/src/xenia/vfs/devices/disc_zarchive_file.cc b/src/xenia/vfs/devices/disc_zarchive_file.cc index 39cc745f1..5c149f168 100644 --- a/src/xenia/vfs/devices/disc_zarchive_file.cc +++ b/src/xenia/vfs/devices/disc_zarchive_file.cc @@ -33,10 +33,9 @@ X_STATUS DiscZarchiveFile::ReadSync(void* buffer, size_t buffer_length, return X_STATUS_END_OF_FILE; } ZArchiveReader* reader = static_cast(entry_->opaque_); - uint64_t bytes_read = + const uint64_t bytes_read = reader->ReadFromFile(entry_->handle_, byte_offset, buffer_length, buffer); - size_t real_offset = entry_->data_offset() + byte_offset; - size_t real_length = + const size_t real_length = std::min(buffer_length, entry_->data_size() - byte_offset); *out_bytes_read = real_length; return X_STATUS_SUCCESS;