|
| 1 | +/* |
| 2 | +
|
| 3 | +MIT License |
| 4 | +
|
| 5 | +Copyright (c) 2025 PCSX-Redux authors |
| 6 | +
|
| 7 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +of this software and associated documentation files (the "Software"), to deal |
| 9 | +in the Software without restriction, including without limitation the rights |
| 10 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +copies of the Software, and to permit persons to whom the Software is |
| 12 | +furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | +The above copyright notice and this permission notice shall be included in all |
| 15 | +copies or substantial portions of the Software. |
| 16 | +
|
| 17 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +SOFTWARE. |
| 24 | +
|
| 25 | +*/ |
| 26 | + |
| 27 | +#include "psyqo-paths/archive-manager.hh" |
| 28 | + |
| 29 | +#include "EASTL/algorithm.h" |
| 30 | +#include "lz4/lz4.h" |
| 31 | +#include "psyqo/kernel.hh" |
| 32 | +#include "psyqo/utility-polyfill.h" |
| 33 | +#include "ucl-demo/n2e-d.h" |
| 34 | + |
| 35 | +eastl::array<void (psyqo::paths::ArchiveManager::*)(const psyqo::paths::ArchiveManager::IndexEntry*), 3> |
| 36 | + psyqo::paths::ArchiveManager::s_decompressors = {nullptr}; |
| 37 | + |
| 38 | +void psyqo::paths::ArchiveManager::setupInitQueue(eastl::string_view archiveName, ISO9660Parser& parser, |
| 39 | + eastl::function<void(bool)>&& callback) { |
| 40 | + setupInitQueue(0, *parser.getCDRom(), eastl::move(callback)); |
| 41 | + m_queueInitFilename.reset(); |
| 42 | + m_queueInitFilename.startWith([](auto task) { task->resolve(); }); |
| 43 | + if (!parser.initialized()) { |
| 44 | + m_queueInitFilename.then(parser.scheduleInitialize()); |
| 45 | + } |
| 46 | + m_queueInitFilename.then(parser.scheduleGetDirentry(archiveName, &m_archiveDirentry)) |
| 47 | + .then([this](auto task) { |
| 48 | + m_index.resize(2048 / sizeof(IndexEntry)); |
| 49 | + m_request.LBA = m_archiveDirentry.LBA; |
| 50 | + m_request.count = 1; |
| 51 | + m_request.buffer = m_index.data(); |
| 52 | + task->resolve(); |
| 53 | + }) |
| 54 | + .then(m_queue.schedule()); |
| 55 | +} |
| 56 | + |
| 57 | +void psyqo::paths::ArchiveManager::setupInitQueue(uint32_t LBA, CDRom& device, eastl::function<void(bool)>&& callback) { |
| 58 | + Kernel::assert(!m_pending, "Only one action can be performed at a time"); |
| 59 | + m_queue.reset(); |
| 60 | + m_pending = true; |
| 61 | + m_success = false; |
| 62 | + m_initCallback = eastl::move(callback); |
| 63 | + m_request.LBA = LBA; |
| 64 | + m_request.count = 1; |
| 65 | + m_request.buffer = m_index.data(); |
| 66 | + m_queue.startWith(device.scheduleReadRequest(&m_request)) |
| 67 | + .then([this](auto task) { |
| 68 | + eastl::string_view signature = {reinterpret_cast<const char*>(m_index.data()), 8}; |
| 69 | + if (signature != "PSX-ARC1") { |
| 70 | + task->reject(); |
| 71 | + return; |
| 72 | + } |
| 73 | + m_index.resize(getIndexSectorCount() * 2048 / sizeof(IndexEntry)); |
| 74 | + m_request.count = getIndexSectorCount(); |
| 75 | + m_request.buffer = m_index.data(); |
| 76 | + task->resolve(); |
| 77 | + }) |
| 78 | + .then(device.scheduleReadRequest(&m_request)) |
| 79 | + .then([this](auto task) { |
| 80 | + m_success = true; |
| 81 | + m_index.resize(getIndexCount() + 1); |
| 82 | + task->resolve(); |
| 83 | + }) |
| 84 | + .finally([this](auto queue) { |
| 85 | + m_pending = false; |
| 86 | + auto callback = eastl::move(m_initCallback); |
| 87 | + m_initCallback = nullptr; |
| 88 | + callback(m_success); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +const psyqo::paths::ArchiveManager::IndexEntry* psyqo::paths::ArchiveManager::getIndexEntry( |
| 93 | + eastl::string_view path) const { |
| 94 | + uint64_t hash = djb::hash<uint64_t>(path.data(), path.size()); |
| 95 | + return getIndexEntry(hash); |
| 96 | +} |
| 97 | + |
| 98 | +const psyqo::paths::ArchiveManager::IndexEntry* psyqo::paths::ArchiveManager::getIndexEntry(uint64_t hash) const { |
| 99 | + const IndexEntry* first = &m_index[1]; |
| 100 | + const IndexEntry* last = first + getIndexCount(); |
| 101 | + const IndexEntry* entry = |
| 102 | + eastl::lower_bound(first, last, hash, [](const IndexEntry& e, uint64_t hash) { return e.hash < hash; }); |
| 103 | + if (entry != last && entry->hash == hash) { |
| 104 | + return entry; |
| 105 | + } |
| 106 | + return nullptr; |
| 107 | +} |
| 108 | + |
| 109 | +void psyqo::paths::ArchiveManager::setupQueue(const IndexEntry* entry, CDRom& device, |
| 110 | + eastl::function<void(Buffer<uint8_t>&&)>&& callback) { |
| 111 | + Kernel::assert(!m_pending, "Only one action can be performed at a time"); |
| 112 | + m_queue.reset(); |
| 113 | + m_pending = true; |
| 114 | + m_callback = eastl::move(callback); |
| 115 | + if (!entry) { |
| 116 | + m_queue.startWith([this](auto task) { |
| 117 | + m_data.clear(); |
| 118 | + m_pending = false; |
| 119 | + auto callback = eastl::move(m_callback); |
| 120 | + m_callback = nullptr; |
| 121 | + callback(eastl::move(m_data)); |
| 122 | + }); |
| 123 | + return; |
| 124 | + } |
| 125 | + const auto method = entry->getCompressionMethod(); |
| 126 | + const uint32_t sectorCount = entry->getCompressedSize(); |
| 127 | + const uint32_t decompSize = entry->getDecompSize(); |
| 128 | + m_request.LBA = m_archiveDirentry.LBA + entry->getSectorOffset(); |
| 129 | + m_request.count = sectorCount; |
| 130 | + if (method == IndexEntry::Method::NONE) { |
| 131 | + m_data.resize(sectorCount * 2048); |
| 132 | + m_request.buffer = m_data.data(); |
| 133 | + } else { |
| 134 | + uint32_t actualSize = eastl::max<uint32_t>(((decompSize + 3) & ~3) + 16, sectorCount * 2048); |
| 135 | + m_data.resize(actualSize); |
| 136 | + m_request.buffer = m_data.data() + actualSize - sectorCount * 2048; |
| 137 | + } |
| 138 | + m_queue.startWith(device.scheduleReadRequest(&m_request)) |
| 139 | + .then([this, entry](auto task) { |
| 140 | + auto decompress = s_decompressors[toUnderlying(entry->getCompressionMethod())]; |
| 141 | + if (decompress) (this->*decompress)(entry); |
| 142 | + uint32_t decompSize = entry->getDecompSize(); |
| 143 | + if (decompSize) m_data.resize(decompSize); |
| 144 | + task->resolve(); |
| 145 | + }) |
| 146 | + .butCatch([this](auto task) { m_data.resize(0); }) |
| 147 | + .finally([this, decompSize](auto queue) { |
| 148 | + m_pending = false; |
| 149 | + auto callback = eastl::move(m_callback); |
| 150 | + m_callback = nullptr; |
| 151 | + callback(eastl::move(m_data)); |
| 152 | + }); |
| 153 | +} |
| 154 | + |
| 155 | +void psyqo::paths::ArchiveManager::decompressUCL_NRV2E(const IndexEntry* entry) { |
| 156 | + uint32_t padding = entry->getPadding(); |
| 157 | + n2e_decompress(reinterpret_cast<uint8_t*>(m_request.buffer) + padding, m_data.data()); |
| 158 | +} |
| 159 | + |
| 160 | +void psyqo::paths::ArchiveManager::decompressLZ4(const IndexEntry* entry) { |
| 161 | + uint32_t padding = entry->getPadding(); |
| 162 | + uint32_t srcSize = entry->getCompressedSize() * 2048 - padding; |
| 163 | + uint8_t* src = reinterpret_cast<uint8_t*>(m_request.buffer) + padding; |
| 164 | + lz4_decompress_block(src, src + srcSize, m_data.data()); |
| 165 | +} |
0 commit comments