From e1c2df57670bf40868372b49119ad50f47c07230 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Mon, 19 Oct 2026 22:51:03 -0700 Subject: [PATCH] Centralize ZIP filename lookup Add FindFileIndex to resolve the first case-insensitive filename match without extracting it. Reuse the lookup in named extraction and library include resolution, removing the duplicate archive-entry scan while preserving archive order and source fallback behavior. Validated with a warning-free Qt 6.10 Debug build and 51 offscreen lifecycle groups; the final API rename also builds without warnings. --- common/lc_library.cpp | 19 ++++++++----------- common/lc_zipfile.cpp | 15 +++++++++++---- common/lc_zipfile.h | 3 +++ 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/common/lc_library.cpp b/common/lc_library.cpp index 66e1b9ad..c9f9a68f 100644 --- a/common/lc_library.cpp +++ b/common/lc_library.cpp @@ -1809,20 +1809,17 @@ lcResult> lcPiecesLibrary::OpenPieceDataFile(const char* { const QByteArray FileName = (Prefix + Folder + QString::fromLatin1(PieceName)).toLatin1(); - for (quint32 Index = 0; Index < Archive->mFiles.size(); Index++) - { - if (qstricmp(Archive->mFiles[Index].file_name, FileName.constData())) - continue; + const std::optional FileIndex = Archive->FindFileIndex(FileName.constData()); - lcFileSource Source; - Source.ArchiveType = static_cast(Type); - Source.ArchiveIndex = Index; + if (!FileIndex) + continue; - if (lcResult> File = OpenFile(Source)) - return File; + lcFileSource Source; + Source.ArchiveType = static_cast(Type); + Source.ArchiveIndex = *FileIndex; - break; - } + if (lcResult> File = OpenFile(Source)) + return File; } } diff --git a/common/lc_zipfile.cpp b/common/lc_zipfile.cpp index 8709ffbf..fb7353a0 100644 --- a/common/lc_zipfile.cpp +++ b/common/lc_zipfile.cpp @@ -567,17 +567,24 @@ bool lcZipFile::ReadCentralDir() return true; } -bool lcZipFile::ExtractFile(const char* FileName, lcMemFile& File, quint32 MaxLength) +std::optional lcZipFile::FindFileIndex(const char* FileName) const { for (quint32 FileIdx = 0; FileIdx < mFiles.size(); FileIdx++) { - lcZipFileInfo& FileInfo = mFiles[FileIdx]; + const lcZipFileInfo& FileInfo = mFiles[FileIdx]; if (!qstricmp(FileInfo.file_name, FileName)) - return ExtractFile(FileIdx, File, MaxLength); + return FileIdx; } - return false; + return std::nullopt; +} + +bool lcZipFile::ExtractFile(const char* FileName, lcMemFile& File, quint32 MaxLength) +{ + const std::optional FileIndex = FindFileIndex(FileName); + + return FileIndex && ExtractFile(*FileIndex, File, MaxLength); } bool lcZipFile::ExtractFile(quint32 FileIndex, lcMemFile& File, quint32 MaxLength) diff --git a/common/lc_zipfile.h b/common/lc_zipfile.h index c1d5349d..1413e247 100644 --- a/common/lc_zipfile.h +++ b/common/lc_zipfile.h @@ -59,6 +59,9 @@ public: bool OpenRead(std::unique_ptr File); bool OpenWrite(const QString& FileName); + // Returns the first case-insensitive filename match in archive order. + std::optional FindFileIndex(const char* FileName) const; + bool ExtractFile(quint32 FileIndex, lcMemFile& File, quint32 MaxLength = 0xffffffff); bool ExtractFile(const char* FileName, lcMemFile& File, quint32 MaxLength = 0xffffffff);