mirror of
https://github.com/leozide/leocad
synced 2025-02-05 08:45:55 +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);
|
||||
|
||||
mNumOfficialPieces = 0;
|
||||
mZipFiles[LC_ZIPFILE_OFFICIAL] = nullptr;
|
||||
mZipFiles[LC_ZIPFILE_UNOFFICIAL] = nullptr;
|
||||
mBuffersDirty = false;
|
||||
mHasUnofficial = false;
|
||||
mCancelLoading = false;
|
||||
|
@ -77,9 +75,7 @@ void lcPiecesLibrary::Unload()
|
|||
mTextures.clear();
|
||||
|
||||
mNumOfficialPieces = 0;
|
||||
delete mZipFiles[LC_ZIPFILE_OFFICIAL];
|
||||
mZipFiles[LC_ZIPFILE_OFFICIAL] = nullptr;
|
||||
delete mZipFiles[LC_ZIPFILE_UNOFFICIAL];
|
||||
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)
|
||||
{
|
||||
lcDiskFile* File = new lcDiskFile(FileName);
|
||||
std::unique_ptr<lcDiskFile> File(new lcDiskFile(FileName));
|
||||
|
||||
if (!File->Open(QIODevice::ReadOnly) || !OpenArchive(File, FileName, ZipFileType))
|
||||
{
|
||||
delete File;
|
||||
if (!File->Open(QIODevice::ReadOnly))
|
||||
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))
|
||||
{
|
||||
delete ZipFile;
|
||||
if (!ZipFile->OpenRead(std::move(File)))
|
||||
return false;
|
||||
}
|
||||
|
||||
mZipFiles[ZipFileType] = ZipFile;
|
||||
|
||||
if (ZipFileType == LC_ZIPFILE_OFFICIAL)
|
||||
mLibraryFileName = FileName;
|
||||
else
|
||||
mUnofficialFileName = FileName;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -1870,14 +1855,11 @@ bool lcPiecesLibrary::LoadBuiltinPieces()
|
|||
if (!Resource.isValid())
|
||||
return false;
|
||||
|
||||
lcMemFile* File = new lcMemFile();
|
||||
std::unique_ptr<lcMemFile> File(new lcMemFile());
|
||||
File->WriteBuffer(Resource.data(), Resource.size());
|
||||
|
||||
if (!OpenArchive(File, "builtin", LC_ZIPFILE_OFFICIAL))
|
||||
{
|
||||
delete File;
|
||||
if (!OpenArchive(std::move(File), "builtin", LC_ZIPFILE_OFFICIAL))
|
||||
return false;
|
||||
}
|
||||
|
||||
lcMemFile PieceFile;
|
||||
|
||||
|
|
|
@ -167,7 +167,7 @@ signals:
|
|||
|
||||
protected:
|
||||
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);
|
||||
void ReadArchiveDescriptions(const QString& OfficialFileName, const QString& UnofficialFileName);
|
||||
void ReadDirectoryDescriptions(const QFileInfoList (&FileLists)[LC_NUM_FOLDERTYPES], bool ShowProgress);
|
||||
|
@ -194,9 +194,7 @@ protected:
|
|||
|
||||
QString mCachePath;
|
||||
qint64 mArchiveCheckSum[4];
|
||||
QString mLibraryFileName;
|
||||
QString mUnofficialFileName;
|
||||
lcZipFile* mZipFiles[LC_NUM_ZIPFILES];
|
||||
std::unique_ptr<lcZipFile> mZipFiles[LC_NUM_ZIPFILES];
|
||||
bool mHasUnofficial;
|
||||
bool mCancelLoading;
|
||||
};
|
||||
|
|
|
@ -14,32 +14,25 @@
|
|||
lcZipFile::lcZipFile()
|
||||
{
|
||||
mModified = false;
|
||||
mFile = nullptr;
|
||||
}
|
||||
|
||||
lcZipFile::~lcZipFile()
|
||||
{
|
||||
delete mFile;
|
||||
}
|
||||
|
||||
bool lcZipFile::OpenRead(const QString& FileName)
|
||||
{
|
||||
lcDiskFile* File = new lcDiskFile(FileName);
|
||||
mFile = File;
|
||||
std::unique_ptr<lcDiskFile> File(new lcDiskFile(FileName));
|
||||
|
||||
if (!File->Open(QIODevice::ReadOnly) || !Open())
|
||||
{
|
||||
delete File;
|
||||
mFile = nullptr;
|
||||
if (!File->Open(QIODevice::ReadOnly))
|
||||
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())
|
||||
{
|
||||
|
@ -52,8 +45,12 @@ bool lcZipFile::OpenRead(lcFile* File)
|
|||
|
||||
bool lcZipFile::OpenWrite(const QString& FileName)
|
||||
{
|
||||
lcDiskFile* File = new lcDiskFile(FileName);
|
||||
mFile = File;
|
||||
std::unique_ptr<lcDiskFile> File(new lcDiskFile(FileName));
|
||||
|
||||
if (!File->Open(QIODevice::WriteOnly))
|
||||
return false;
|
||||
|
||||
mFile = std::move(File);
|
||||
|
||||
mNumEntries = 0;
|
||||
mCentralDirSize = 0;
|
||||
|
@ -61,13 +58,6 @@ bool lcZipFile::OpenWrite(const QString& FileName)
|
|||
mBytesBeforeZipFile = 0;
|
||||
mCentralPos = 0;
|
||||
|
||||
if (!File->Open(QIODevice::WriteOnly))
|
||||
{
|
||||
delete File;
|
||||
mFile = nullptr;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -58,7 +58,7 @@ public:
|
|||
lcZipFile& operator=(lcZipFile&&) = delete;
|
||||
|
||||
bool OpenRead(const QString& FileName);
|
||||
bool OpenRead(lcFile* File);
|
||||
bool OpenRead(std::unique_ptr<lcFile> File);
|
||||
bool OpenWrite(const QString& FileName);
|
||||
|
||||
bool ExtractFile(int FileIndex, lcMemFile& File, quint32 MaxLength = 0xffffffff);
|
||||
|
@ -74,7 +74,7 @@ protected:
|
|||
bool CheckFileCoherencyHeader(int FileIndex, quint32* SizeVar, quint64* OffsetLocalExtraField, quint32* SizeLocalExtraField);
|
||||
|
||||
QMutex mMutex;
|
||||
lcFile* mFile;
|
||||
std::unique_ptr<lcFile> mFile;
|
||||
|
||||
bool mModified;
|
||||
bool mZip64;
|
||||
|
|
Loading…
Add table
Reference in a new issue