Add parallel race/motion loading and thread-safe Pack/Pool managers

This commit is contained in:
savis
2026-01-04 17:25:51 +01:00
parent 2550008f6d
commit d5624a8cdd
7 changed files with 102 additions and 24 deletions

View File

@@ -13,7 +13,7 @@ static ZSTD_DCtx* GetThreadLocalZSTDContext()
return g_zstdDCtx;
}
bool CPack::Open(const std::string& path, TPackFileMap& entries)
bool CPack::Load(const std::string& path)
{
std::error_code ec;
m_file.map(path, ec);
@@ -34,13 +34,13 @@ bool CPack::Open(const std::string& path, TPackFileMap& entries)
return false;
}
m_index.resize(m_header.entry_num);
for (size_t i = 0; i < m_header.entry_num; i++) {
TPackFileEntry entry;
TPackFileEntry& entry = m_index[i];
memcpy(&entry, m_file.data() + sizeof(TPackFileHeader) + i * sizeof(TPackFileEntry), sizeof(TPackFileEntry));
m_decryption.ProcessData((CryptoPP::byte*)&entry, (CryptoPP::byte*)&entry, sizeof(TPackFileEntry));
entries[entry.file_name] = std::make_pair(shared_from_this(), entry);
if (file_size < m_header.data_begin + entry.offset + entry.compressed_size) {
return false;
}
@@ -97,4 +97,4 @@ bool CPack::GetFileWithPool(const TPackFileEntry& entry, TPackFile& result, CBuf
}
return true;
}
}

View File

@@ -12,13 +12,16 @@ public:
CPack() = default;
~CPack() = default;
bool Open(const std::string& path, TPackFileMap& entries);
bool Load(const std::string& path);
const std::vector<TPackFileEntry>& GetIndex() const { return m_index; }
bool GetFile(const TPackFileEntry& entry, TPackFile& result);
bool GetFileWithPool(const TPackFileEntry& entry, TPackFile& result, CBufferPool* pPool);
private:
TPackFileHeader m_header;
std::vector<TPackFileEntry> m_index;
mio::mmap_source m_file;
CryptoPP::CTR_Mode<CryptoPP::Camellia>::Decryption m_decryption;
};
};

View File

@@ -23,8 +23,19 @@ bool CPackManager::AddPack(const std::string& path)
{
std::shared_ptr<CPack> pack = std::make_shared<CPack>();
if (!pack->Load(path))
{
return false;
}
std::lock_guard<std::mutex> lock(m_mutex);
return pack->Open(path, m_entries);
const auto& index = pack->GetIndex();
for (const auto& entry : index)
{
m_entries[entry.file_name] = std::make_pair(pack, entry);
}
return true;
}
bool CPackManager::GetFile(std::string_view path, TPackFile& result)