2020#include < QtCore>
2121#include < QByteArray>
2222#include < QDataStream>
23- #include " gameTree .hpp"
23+ #include " GameFileTree .hpp"
2424#include " miniz.h"
2525#include " miniz_zip.h"
2626
@@ -88,6 +88,7 @@ const QString FileManager::calculateMD5(
8888bool FileManager::extractZip (
8989 const QString& zipFilename,
9090 const QString& outputFolder) {
91+ bool status = false ;
9192 const QString& zipPath =
9293 QString (" %1%2%3" ).arg (m_levelDir.absolutePath (), m_sep, zipFilename);
9394 const QString& outputPath =
@@ -103,77 +104,72 @@ bool FileManager::extractZip(
103104
104105 // Open the zip file
105106 mz_zip_archive zip;
106- memset (&zip, 0 , sizeof (zip));
107- mz_bool result =
108- mz_zip_reader_init_file (&zip, zipPath.toUtf8 ().constData (), 0 );
109- if (!result) {
110- qWarning () << " Failed to open zip file" << zipPath;
111- return false ;
112- }
113-
114- // Extract each file in the zip archive
115- mz_uint numFiles = mz_zip_reader_get_num_files (&zip);
116- qDebug () << " Zip file contains" << numFiles << " files" ;
117-
118- unsigned int gotoPercent = 50 ; // Percentage of total work
119- unsigned int lastPrintedPercent = 0 ; // Last printed percentage
120-
121- for (uint i = 0 ; i < numFiles; i++) {
122- mz_zip_archive_file_stat file_stat;
123- if (!mz_zip_reader_file_stat (&zip, i, &file_stat)) {
124- qWarning () << " Failed to get file info for file" << i
125- << " in zip file" << zipPath;
126- mz_zip_reader_end (&zip);
127- return false ;
128- }
107+ (void )memset (&zip, 0 , sizeof (zip));
108+ if (mz_zip_reader_init_file (
109+ &zip, zipPath.toUtf8 ().constData (), 0 ) == true ) {
110+ // Extract each file in the zip archive
111+ quint64 numFiles = mz_zip_reader_get_num_files (&zip);
112+ qDebug () << " Zip file contains" << numFiles << " files" ;
113+
114+ quint64 gotoPercent = 50 ; // Percentage of total work
115+ quint64 lastPrintedPercent = 0 ; // Last printed percentage
116+
117+ for (quint64 i = 0 ; i < numFiles; i++) {
118+ mz_zip_archive_file_stat file_stat;
119+ if (!mz_zip_reader_file_stat (&zip, i, &file_stat)) {
120+ qWarning () << " Failed to get file info for file" << i
121+ << " in zip file" << zipPath;
122+ mz_zip_reader_end (&zip);
123+ break ;
124+ }
129125
130- QString filename = QString::fromUtf8 (file_stat.m_filename );
131- if (filename.endsWith (' /' ) == true ) {
132- continue ; // Skip directories
133- }
126+ QString filename = QString::fromUtf8 (file_stat.m_filename );
127+ if (filename.endsWith (' /' ) == true ) {
128+ continue ; // Skip directories
129+ }
134130
135- QString outFile = outputPath + " / " + filename;
136- qDebug () << " Extracting" << filename;
131+ QString outFile = QString ( " %1/%2 " ). arg (outputPath, filename) ;
132+ qDebug () << " Extracting" << filename;
137133
138- if (!QDir ().mkpath (QFileInfo (outFile).path ())) {
139- qWarning () << " Failed to create directory for file" << outFile;
140- mz_zip_reader_end (&zip);
141- return false ;
142- }
143-
144- if (!mz_zip_reader_extract_to_file (
145- &zip,
146- i,
147- outFile.toUtf8 ().constData (),
148- 0 )) {
149- qWarning () << " Failed to extract file" << filename
150- << " from zip file" << zipPath;
151- mz_zip_reader_end (&zip);
152- return false ;
153- }
134+ if (!QDir ().mkpath (QFileInfo (outFile).path ())) {
135+ qWarning () << " Failed to create directory for file" << outFile;
136+ mz_zip_reader_end (&zip);
137+ break ;
138+ }
154139
155- unsigned int currentPercent = ((i + 1 ) * gotoPercent) / numFiles;
140+ if (!mz_zip_reader_extract_to_file (
141+ &zip,
142+ i,
143+ outFile.toUtf8 ().constData (),
144+ 0 )) {
145+ qWarning () << " Failed to extract file" << filename
146+ << " from zip file" << zipPath;
147+ mz_zip_reader_end (&zip);
148+ break ;
149+ }
156150
157- if (currentPercent != lastPrintedPercent) {
158- for (unsigned int j = lastPrintedPercent + 1 ;
159- j <= currentPercent; j++) {
160- emit this ->fileWorkTickSignal ();
161- QCoreApplication::processEvents ();
151+ quint64 currentPercent =
152+ ((i + quint64 (1 )) * gotoPercent) / numFiles;
153+
154+ if (currentPercent != lastPrintedPercent) {
155+ for (quint64 j = lastPrintedPercent + quint64 (1 );
156+ j <= currentPercent; j++) {
157+ emit this ->fileWorkTickSignal ();
158+ QCoreApplication::processEvents ();
159+ }
160+ lastPrintedPercent = currentPercent;
161+ if (currentPercent == gotoPercent) {
162+ status = true ;
163+ }
162164 }
163- lastPrintedPercent = currentPercent;
164165 }
166+ } else {
167+ qWarning () << " Failed to open zip file" << zipPath;
165168 }
166-
167- // Ensure any remaining progress is emitted
168- for (unsigned int j = lastPrintedPercent + 1 ; j <= gotoPercent; j++) {
169- emit this ->fileWorkTickSignal ();
170- QCoreApplication::processEvents ();
171- }
172-
173169 // Clean up
174170 mz_zip_reader_end (&zip);
175171 qDebug () << " Unzip complete" ;
176- return true ;
172+ return status ;
177173}
178174
179175/* *
@@ -219,15 +215,17 @@ bool FileManager::linkGameDir(const QString& levelDir, const QString& gameDir) {
219215 const QString gamePath = QString (" %1%2" )
220216 .arg (m_gameDir.absolutePath (), gameDir);
221217
222- test (levelPath); // here we just output the directory tree for now..
218+ QDir dir (levelPath);
219+ GameFileTree test (dir);
220+ test.printTree (1 );
223221
224222 if (QFile::link (levelPath, gamePath) == true ) {
225223 qDebug () << " Symbolic link created successfully." ;
226224 status = true ;
227225 } else {
228226 QFileInfo fileInfo (gamePath);
229227 if (fileInfo.isSymLink () == true ) {
230- QFile::remove (gamePath);
228+ ( void ) QFile::remove (gamePath);
231229 if (QFile::link (levelPath, gamePath) == true ) {
232230 qDebug () << " Symbolic link created successfully." ;
233231 status = true ;
@@ -261,7 +259,7 @@ bool FileManager::makeRelativeLink(
261259 } else {
262260 QFileInfo i (toPath);
263261 if (i.isSymLink () == true ) {
264- QFile::remove (toPath);
262+ ( void ) QFile::remove (toPath);
265263 if (QFile::link (fromPath, toPath) == true ) {
266264 qDebug () << " Symbolic link created successfully." ;
267265 status = true ;
@@ -419,10 +417,10 @@ bool FileManager::moveFilesToDirectory(
419417 dir.entryList (QDir::Files | QDir::NoDotAndDotDot);
420418
421419 // Move files and recursively move directories
422- bool allMoved = std::all_of (
420+ return std::all_of (
423421 entryFileList.cbegin (),
424422 entryFileList.cend (),
425- [&](const QString& entry) {
423+ [&](const QString& entry) -> bool {
426424 bool status = true ;
427425
428426 QString entryPath = QString (" %1%2%3" )
@@ -435,10 +433,9 @@ bool FileManager::moveFilesToDirectory(
435433 << entryPath << " to:" << destinationPath;
436434 status = false ;
437435 }
438-
436+ // cppcheck-suppress misra-c2012-15.5
439437 return status;
440438 });
441- return allMoved;
442439}
443440
444441bool FileManager::moveFilesToParentDirectory (
0 commit comments