mirror of
https://github.com/leozide/leocad
synced 2025-02-10 20:48:03 +01:00
Merge pull request #559 from j6t/master
Small code modernization around file handling during parts loading.
This commit is contained in:
commit
228d17bd28
4 changed files with 25 additions and 55 deletions
|
@ -44,8 +44,6 @@ lcPiecesLibrary::lcPiecesLibrary()
|
||||||
Dir.mkpath(mCachePath);
|
Dir.mkpath(mCachePath);
|
||||||
|
|
||||||
mNumOfficialPieces = 0;
|
mNumOfficialPieces = 0;
|
||||||
mZipFiles[LC_ZIPFILE_OFFICIAL] = nullptr;
|
|
||||||
mZipFiles[LC_ZIPFILE_UNOFFICIAL] = nullptr;
|
|
||||||
mBuffersDirty = false;
|
mBuffersDirty = false;
|
||||||
mHasUnofficial = false;
|
mHasUnofficial = false;
|
||||||
mCancelLoading = false;
|
mCancelLoading = false;
|
||||||
|
@ -77,9 +75,7 @@ void lcPiecesLibrary::Unload()
|
||||||
mTextures.clear();
|
mTextures.clear();
|
||||||
|
|
||||||
mNumOfficialPieces = 0;
|
mNumOfficialPieces = 0;
|
||||||
delete mZipFiles[LC_ZIPFILE_OFFICIAL];
|
|
||||||
mZipFiles[LC_ZIPFILE_OFFICIAL] = nullptr;
|
mZipFiles[LC_ZIPFILE_OFFICIAL] = nullptr;
|
||||||
delete mZipFiles[LC_ZIPFILE_UNOFFICIAL];
|
|
||||||
mZipFiles[LC_ZIPFILE_UNOFFICIAL] = nullptr;
|
mZipFiles[LC_ZIPFILE_UNOFFICIAL] = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -307,33 +303,20 @@ bool lcPiecesLibrary::Load(const QString& LibraryPath, bool ShowProgress)
|
||||||
|
|
||||||
bool lcPiecesLibrary::OpenArchive(const QString& FileName, lcZipFileType ZipFileType)
|
bool lcPiecesLibrary::OpenArchive(const QString& FileName, lcZipFileType ZipFileType)
|
||||||
{
|
{
|
||||||
lcDiskFile* File = new lcDiskFile(FileName);
|
std::unique_ptr<lcDiskFile> File(new lcDiskFile(FileName));
|
||||||
|
|
||||||
if (!File->Open(QIODevice::ReadOnly) || !OpenArchive(File, FileName, ZipFileType))
|
if (!File->Open(QIODevice::ReadOnly))
|
||||||
{
|
|
||||||
delete File;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return OpenArchive(std::move(File), FileName, ZipFileType);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool lcPiecesLibrary::OpenArchive(lcFile* File, const QString& FileName, lcZipFileType ZipFileType)
|
bool lcPiecesLibrary::OpenArchive(std::unique_ptr<lcFile> File, const QString& FileName, lcZipFileType ZipFileType)
|
||||||
{
|
{
|
||||||
lcZipFile* ZipFile = new lcZipFile();
|
std::unique_ptr<lcZipFile> ZipFile(new lcZipFile());
|
||||||
|
|
||||||
if (!ZipFile->OpenRead(File))
|
if (!ZipFile->OpenRead(std::move(File)))
|
||||||
{
|
|
||||||
delete ZipFile;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
mZipFiles[ZipFileType] = ZipFile;
|
|
||||||
|
|
||||||
if (ZipFileType == LC_ZIPFILE_OFFICIAL)
|
|
||||||
mLibraryFileName = FileName;
|
|
||||||
else
|
|
||||||
mUnofficialFileName = FileName;
|
|
||||||
|
|
||||||
for (int FileIdx = 0; FileIdx < ZipFile->mFiles.GetSize(); FileIdx++)
|
for (int FileIdx = 0; FileIdx < ZipFile->mFiles.GetSize(); FileIdx++)
|
||||||
{
|
{
|
||||||
|
@ -432,6 +415,8 @@ bool lcPiecesLibrary::OpenArchive(lcFile* File, const QString& FileName, lcZipFi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mZipFiles[ZipFileType] = std::move(ZipFile);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1870,14 +1855,11 @@ bool lcPiecesLibrary::LoadBuiltinPieces()
|
||||||
if (!Resource.isValid())
|
if (!Resource.isValid())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
lcMemFile* File = new lcMemFile();
|
std::unique_ptr<lcMemFile> File(new lcMemFile());
|
||||||
File->WriteBuffer(Resource.data(), Resource.size());
|
File->WriteBuffer(Resource.data(), Resource.size());
|
||||||
|
|
||||||
if (!OpenArchive(File, "builtin", LC_ZIPFILE_OFFICIAL))
|
if (!OpenArchive(std::move(File), "builtin", LC_ZIPFILE_OFFICIAL))
|
||||||
{
|
|
||||||
delete File;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
lcMemFile PieceFile;
|
lcMemFile PieceFile;
|
||||||
|
|
||||||
|
|
|
@ -167,7 +167,7 @@ signals:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool OpenArchive(const QString& FileName, lcZipFileType ZipFileType);
|
bool OpenArchive(const QString& FileName, lcZipFileType ZipFileType);
|
||||||
bool OpenArchive(lcFile* File, const QString& FileName, lcZipFileType ZipFileType);
|
bool OpenArchive(std::unique_ptr<lcFile> File, const QString& FileName, lcZipFileType ZipFileType);
|
||||||
bool OpenDirectory(const QDir& LibraryDir, bool ShowProgress);
|
bool OpenDirectory(const QDir& LibraryDir, bool ShowProgress);
|
||||||
void ReadArchiveDescriptions(const QString& OfficialFileName, const QString& UnofficialFileName);
|
void ReadArchiveDescriptions(const QString& OfficialFileName, const QString& UnofficialFileName);
|
||||||
void ReadDirectoryDescriptions(const QFileInfoList (&FileLists)[LC_NUM_FOLDERTYPES], bool ShowProgress);
|
void ReadDirectoryDescriptions(const QFileInfoList (&FileLists)[LC_NUM_FOLDERTYPES], bool ShowProgress);
|
||||||
|
@ -194,9 +194,7 @@ protected:
|
||||||
|
|
||||||
QString mCachePath;
|
QString mCachePath;
|
||||||
qint64 mArchiveCheckSum[4];
|
qint64 mArchiveCheckSum[4];
|
||||||
QString mLibraryFileName;
|
std::unique_ptr<lcZipFile> mZipFiles[LC_NUM_ZIPFILES];
|
||||||
QString mUnofficialFileName;
|
|
||||||
lcZipFile* mZipFiles[LC_NUM_ZIPFILES];
|
|
||||||
bool mHasUnofficial;
|
bool mHasUnofficial;
|
||||||
bool mCancelLoading;
|
bool mCancelLoading;
|
||||||
};
|
};
|
||||||
|
|
|
@ -14,32 +14,25 @@
|
||||||
lcZipFile::lcZipFile()
|
lcZipFile::lcZipFile()
|
||||||
{
|
{
|
||||||
mModified = false;
|
mModified = false;
|
||||||
mFile = nullptr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
lcZipFile::~lcZipFile()
|
lcZipFile::~lcZipFile()
|
||||||
{
|
{
|
||||||
delete mFile;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool lcZipFile::OpenRead(const QString& FileName)
|
bool lcZipFile::OpenRead(const QString& FileName)
|
||||||
{
|
{
|
||||||
lcDiskFile* File = new lcDiskFile(FileName);
|
std::unique_ptr<lcDiskFile> File(new lcDiskFile(FileName));
|
||||||
mFile = File;
|
|
||||||
|
|
||||||
if (!File->Open(QIODevice::ReadOnly) || !Open())
|
if (!File->Open(QIODevice::ReadOnly))
|
||||||
{
|
|
||||||
delete File;
|
|
||||||
mFile = nullptr;
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return OpenRead(std::move(File));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool lcZipFile::OpenRead(lcFile* File)
|
bool lcZipFile::OpenRead(std::unique_ptr<lcFile> File)
|
||||||
{
|
{
|
||||||
mFile = File;
|
mFile = std::move(File);
|
||||||
|
|
||||||
if (!Open())
|
if (!Open())
|
||||||
{
|
{
|
||||||
|
@ -52,8 +45,12 @@ bool lcZipFile::OpenRead(lcFile* File)
|
||||||
|
|
||||||
bool lcZipFile::OpenWrite(const QString& FileName)
|
bool lcZipFile::OpenWrite(const QString& FileName)
|
||||||
{
|
{
|
||||||
lcDiskFile* File = new lcDiskFile(FileName);
|
std::unique_ptr<lcDiskFile> File(new lcDiskFile(FileName));
|
||||||
mFile = File;
|
|
||||||
|
if (!File->Open(QIODevice::WriteOnly))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
mFile = std::move(File);
|
||||||
|
|
||||||
mNumEntries = 0;
|
mNumEntries = 0;
|
||||||
mCentralDirSize = 0;
|
mCentralDirSize = 0;
|
||||||
|
@ -61,13 +58,6 @@ bool lcZipFile::OpenWrite(const QString& FileName)
|
||||||
mBytesBeforeZipFile = 0;
|
mBytesBeforeZipFile = 0;
|
||||||
mCentralPos = 0;
|
mCentralPos = 0;
|
||||||
|
|
||||||
if (!File->Open(QIODevice::WriteOnly))
|
|
||||||
{
|
|
||||||
delete File;
|
|
||||||
mFile = nullptr;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -58,7 +58,7 @@ public:
|
||||||
lcZipFile& operator=(lcZipFile&&) = delete;
|
lcZipFile& operator=(lcZipFile&&) = delete;
|
||||||
|
|
||||||
bool OpenRead(const QString& FileName);
|
bool OpenRead(const QString& FileName);
|
||||||
bool OpenRead(lcFile* File);
|
bool OpenRead(std::unique_ptr<lcFile> File);
|
||||||
bool OpenWrite(const QString& FileName);
|
bool OpenWrite(const QString& FileName);
|
||||||
|
|
||||||
bool ExtractFile(int FileIndex, lcMemFile& File, quint32 MaxLength = 0xffffffff);
|
bool ExtractFile(int FileIndex, lcMemFile& File, quint32 MaxLength = 0xffffffff);
|
||||||
|
@ -74,7 +74,7 @@ protected:
|
||||||
bool CheckFileCoherencyHeader(int FileIndex, quint32* SizeVar, quint64* OffsetLocalExtraField, quint32* SizeLocalExtraField);
|
bool CheckFileCoherencyHeader(int FileIndex, quint32* SizeVar, quint64* OffsetLocalExtraField, quint32* SizeLocalExtraField);
|
||||||
|
|
||||||
QMutex mMutex;
|
QMutex mMutex;
|
||||||
lcFile* mFile;
|
std::unique_ptr<lcFile> mFile;
|
||||||
|
|
||||||
bool mModified;
|
bool mModified;
|
||||||
bool mZip64;
|
bool mZip64;
|
||||||
|
|
Loading…
Add table
Reference in a new issue