2626#include " ../src/binary.hpp"
2727#include " ../src/Path.hpp"
2828
29- bool FileManager::backupGameDir (const QString &gameDir ) {
29+ bool FileManager::backupGameDir (Path path ) {
3030 bool status = false ;
31- const QString source = QString ( " %1%2 " )
32- . arg (m_gameDir. absolutePath (), gameDir. chopped ( 1 )) ;
33- const QString destination = QString ( " %1%2 " ). arg (source, " .old " );
34- QDir directory;
35- if (directory. rename (source, destination) == true ) {
36- qDebug () << " Directory renamed successfully. New path: " << destination ;
31+ Path backupPath = path;
32+ backupPath << " .old " ;
33+
34+ if ( QDir (). rename (path. get (), backupPath. get ()) == true ) {
35+ qDebug () << " Directory renamed successfully. New path: "
36+ << backupPath. get () ;
3737 status = true ;
3838 } else {
39- qWarning () << " Failed to rename directory:" << source ;
39+ qWarning () << " Failed to rename directory:" << path. get () ;
4040 status = false ;
4141 }
4242 return status;
4343}
4444
45- const QString FileManager::calculateMD5 (
46- const QString& fileName, bool lookGameDir) {
47- const QString path = FileManager::getFullPath (fileName, lookGameDir);
48- QFileInfo fileInfo (path);
45+ const QString FileManager::calculateMD5 (Path path) {
4946 QString result;
5047
51- if (fileInfo .exists () && !fileInfo .isFile ()) {
52- qDebug () << " Error: The path is not a regular file." << path;
48+ if (path .exists () && !path .isFile ()) {
49+ qDebug () << " Error: The path is not a regular file." << path. get () ;
5350 } else {
54- QFile file (path);
51+ QFile file (path. get () );
5552 bool status = file.open (QIODevice::ReadOnly); // flawfinder: ignore
5653 if (!status) {
5754 qDebug ()
@@ -69,36 +66,32 @@ const QString FileManager::calculateMD5(
6966 return result;
7067}
7168
72-
73- int FileManager::checkFileInfo (const QString& file, bool lookGameDir) {
69+ int FileManager::checkFileInfo (Path path) {
7470 int status = 0 ;
75- const QString path = FileManager::getFullPath (file, lookGameDir);
76- QFileInfo fileInfo (path);
77- if (fileInfo.isDir () == true ) {
71+
72+ if (path.exists () == true ) {
7873 qDebug () << " The path is a directory." ;
79- if (fileInfo .isSymLink () == true ) {
74+ if (path .isSymLink () == true ) {
8075 qDebug () << " return value 1:The path is a symbolic link." ;
8176 status = 1 ;
8277 } else {
8378 qDebug () << " return value 2:The path is not a symbolic link." ;
8479 status = 2 ;
8580 }
86- } else {
87- qDebug () << " value 3:The path is not a directory." ;
88- qDebug () << " filePath " << path;
89- status = 3 ;
81+ if (!path.isDir ()) {
82+ qDebug () << " value 3:The path is not a directory." ;
83+ qDebug () << " filePath " << path.get ();
84+ status = 3 ;
85+ }
9086 }
9187 return status;
9288}
9389
94- int FileManager::cleanWorkingDir (const QString &levelDir ) {
90+ int FileManager::cleanWorkingDir (Path path ) {
9591 int status = 0 ;
96- const QString directoryPath = QString (" %1%2%3" )
97- .arg (m_levelDir.absolutePath (), m_sep, levelDir);
9892
99- QDir directory (directoryPath);
100- if (directory.exists () == true ) {
101- if (directory.removeRecursively () == true ) {
93+ if (path.isDir () == true ) {
94+ if (QDir (path.get ()).removeRecursively () == true ) {
10295 qDebug () << " Working Directory removed successfully." ;
10396 status = 0 ;
10497 } else {
@@ -111,48 +104,39 @@ int FileManager::cleanWorkingDir(const QString &levelDir) {
111104 return status;
112105}
113106
114- int FileManager::copyFile (const QString &gameFile,
115- const QString &levelFile, bool fromGameDir) {
107+ int FileManager::copyFile (Path from, Path to) {
116108 int status = 0 ;
117109
118- const QString gamePath = QString (" %1%2" )
119- .arg (m_gameDir.absolutePath (), gameFile);
120-
121- const QString levelPath = QString (" %1%2" )
122- .arg (m_levelDir.absolutePath (), levelFile);
123-
124- const QString sourceFile = fromGameDir ? gamePath : levelPath;
125- const QString destinationFile = fromGameDir ? levelPath : gamePath;
126-
127110 // Ensure the destination directory exists
128- const QFileInfo destinationFileInfo (destinationFile);
129- QDir destinationDir (destinationFileInfo.absolutePath ());
130- if (!destinationDir.exists () == true ) {
131- if (!QDir ().mkpath (destinationDir.absolutePath ())) {
132- qDebug () << " Error creating destination directory." ;
111+ if (!QDir (to.getDir ()).exists () == true ) {
112+ if (!QDir ().mkpath (to.getDir ())) {
113+ qDebug () << " FileManager: Error creating destination directory." ;
133114 status = 1 ;
134115 }
135- } else if (QFile::copy (sourceFile, destinationFile) == true ) {
136- qDebug () << " File copy to " << destinationFile << " successfully." ;
137- status = 0 ;
116+ }
117+
118+ // Ensure the destination file/dir dose NOT exists
119+ if ((status == 0 ) && to.exists () == true ) {
120+ qDebug () << " FileManager: Target file already exist." ;
121+ status = 2 ;
122+ }
123+
124+ if ((status == 0 ) && (QFile::copy (from.get (), to.get ()) == true )) {
125+ qDebug () << " File copy to " << to.get () << " successfully." ;
138126 } else {
139- if (QFile::exists (destinationFile) == true ) {
140- qDebug () << " Target file already exist." ;
141- status = 2 ;
142- } else {
143- qDebug () << " Failed to copy file: " << destinationFile;
144- status = 3 ;
145- }
127+ qDebug () << " FileManager Failed to copy the file." ;
128+ status = 3 ;
146129 }
130+
147131 return status;
148132}
149133
150- int FileManager::createDirectory (const QString &file, bool gameDir ) {
134+ int FileManager::createDirectory (Path path ) {
151135 qint64 status = 0 ;
152- const QString path = FileManager::getFullPath (file, gameDir);
136+
153137 // Create the directory if it doesn't exist
154- if (!QDir ( path) .exists ()) {
155- if (QDir ().mkpath (path) == true ) {
138+ if (!path.exists ()) {
139+ if (QDir ().mkpath (path. get () ) == true ) {
156140 qDebug () << " Directory created successfully." ;
157141 status = 0 ;
158142 } else {
@@ -166,26 +150,23 @@ int FileManager::createDirectory(const QString &file, bool gameDir) {
166150 return status;
167151}
168152
169- void FileManager::createLinkToExe (
170- const QString& path,
171- const QString& expectedFileName) {
172- const QString pathToKnownPattern = getExtraPathToExe (path);
173- const QString pathToExpectedExe = QString (" %1/%2" )
174- .arg (pathToKnownPattern, expectedFileName);
153+ void FileManager::createLinkToExe (Path path, const QString& expectedFileName) {
154+ path = getExtraPathToExe (path);
155+ path << expectedFileName;
175156
176- qDebug () << " pathToExpectedExe :" << pathToExpectedExe ;
177- qDebug () << " pathToKnownPattern :" << pathToKnownPattern ;
157+ qDebug () << " path :" << path. get () ;
158+ qDebug () << " pathDir :" << path. getDir () ;
178159
179- if (!QFile (pathToExpectedExe) .exists ()) {
180- QDir dir (pathToKnownPattern );
160+ if (!path .exists ()) {
161+ QDir dir (path. getDir () );
181162 const QString finalExe = decideExe (dir);
182163 qDebug () << " finalExe :" << finalExe;
183164
184165 if (finalExe.isEmpty () == true ) {
185166 qDebug () << " error: could not find the exe file for the game!" ;
186167 } else {
187168 (void )makeRelativeLink (
188- pathToKnownPattern ,
169+ path ,
189170 finalExe,
190171 expectedFileName);
191172 }
@@ -201,6 +182,8 @@ bool FileManager::extractZip(
201182 QString (" %1%2%3" ).arg (m_levelDir.absolutePath (), m_sep, zipFilename);
202183 const QString& outputPath =
203184 QString (" %1%2%3" ).arg (m_levelDir.absolutePath (), m_sep, outputFolder);
185+ Path oPath (Path::resource);
186+ oPath << outputFolder;
204187
205188 qDebug () << " Unzipping file" << zipFilename << " to" << outputPath;
206189
@@ -267,7 +250,7 @@ bool FileManager::extractZip(
267250 }
268251 lastPrintedPercent = currentPercent;
269252 if (currentPercent == gotoPercent) {
270- createLinkToExe (outputPath , executable);
253+ createLinkToExe (oPath , executable);
271254 status = true ;
272255 }
273256 }
@@ -281,55 +264,47 @@ bool FileManager::extractZip(
281264 return status;
282265}
283266
284- QString FileManager::getExtraPathToExe (const QString& levelPath) {
285- qDebug () << " levelPath :" << levelPath;
267+ Path FileManager::getExtraPathToExe (Path path) {
268+ const QString s = path.get ();
269+ qDebug () << " levelPath :" << s;
270+
286271 StaticTrees staticTrees;
287- QDir dir (levelPath );
272+ QDir dir (s );
288273 GameFileTree tree (dir);
289274 tree.printTree (1 );
290- QString extraPath;
275+
276+ QStringList extraPath;
291277
292278 for (const GameFileTree* stree : staticTrees.data ) {
293279 extraPath = tree.matchesFromAnyNode (stree);
294- if ((extraPath != QString (" \0 " )) && (!extraPath.isEmpty ())) {
295- QTextStream (stdout)
296- << " game tree matches: " << extraPath << Qt::endl;
280+ if (!extraPath.isEmpty ()) {
281+ QString joined = extraPath.join (QDir::separator ());
282+ QTextStream (stdout) << " game tree matches: " << joined << Qt::endl;
283+
284+ for (int i = extraPath.size () - 1 ; i >= 0 ; --i) {
285+ path << extraPath[i];
286+ }
287+
297288 break ;
298289 }
299290 }
300- return levelPath + extraPath;
301- }
302291
303- inline const QString FileManager::getFullPath (
304- const QString& file,
305- bool lookGameDir) {
306- QString path;
307- if (lookGameDir) {
308- path = QString (" %1%2%3" ).arg (m_gameDir.absolutePath (), m_sep, file);
309- } else {
310- path = QString (" %1%2%3" ).arg (m_levelDir.absolutePath (), m_sep, file);
311- }
312292 return path;
313293}
314294
315- bool FileManager::linkGameDir (const QString& levelDir, const QString& gameDir ) {
295+ bool FileManager::linkGameDir (Path from, Path to ) {
316296 bool status = false ;
317- const QString gamePath = QString (" %1/%2" )
318- .arg (m_gameDir.absolutePath (), gameDir);
319- const QString levelPath = getExtraPathToExe (
320- m_levelDir.absolutePath () + levelDir);
321297
322- qDebug () << " levelPath: " << levelPath ;
323- qDebug () << " gamePath: " << gamePath ;
298+ qDebug () << " levelPath: " << from. get () ;
299+ qDebug () << " gamePath: " << to. get () ;
324300
325- if (QFile::link (levelPath, gamePath ) == true ) {
301+ if (QFile::link (from. get (), to. get () ) == true ) {
326302 qDebug () << " Symbolic link created successfully." ;
327303 status = true ;
328304 } else {
329- QFileInfo fileInfo (gamePath);
330- if (fileInfo.isSymLink () == true ) {
331- (void )QFile::remove (gamePath);
332- if (QFile::link (levelPath, gamePath) == true ) {
305+ if (to.isSymLink () == true ) {
306+ (void )QFile::remove (to.get ());
307+ if (QFile::link (from.get (), to.get ()) == true ) {
333308 qDebug () << " Symbolic link created successfully." ;
334309 status = true ;
335310 } else {
@@ -345,29 +320,22 @@ bool FileManager::linkGameDir(const QString& levelDir, const QString& gameDir) {
345320}
346321
347322bool FileManager::makeRelativeLink (
348- const QString & levelDir,
323+ const Path & levelDir,
349324 const QString& from,
350325 const QString& to) {
351326 bool status = false ;
352327
353- const QString fromPath = QString (" %1/%2" )
354- .arg (levelDir, from);
355- const QString toPath = QString (" %1/%2" )
356- .arg (levelDir, to);
328+ Path fromPath = levelDir;
329+ fromPath << from;
330+ Path toPath = levelDir;
357331
358- qDebug () << " calling makeRelativeLink" ;
359- qDebug () << " levelDir :" << levelDir;
360- qDebug () << " fromPath :" << fromPath;
361- qDebug () << " toPath :" << toPath;
362-
363- if (QFile::link (fromPath, toPath) == true ) {
332+ if (QFile::link (fromPath.get (), to) == true ) {
364333 qDebug () << " Symbolic link created successfully." ;
365334 status = true ;
366335 } else {
367- QFileInfo i (toPath);
368- if (i.isSymLink () == true ) {
369- (void )QFile::remove (toPath);
370- if (QFile::link (fromPath, to) == true ) {
336+ if (toPath.isSymLink () == true ) {
337+ (void )QFile::remove (toPath.get ());
338+ if (QFile::link (fromPath.get (), to) == true ) {
371339 qDebug () << " Symbolic link created successfully." ;
372340 status = true ;
373341 } else {
@@ -382,36 +350,30 @@ bool FileManager::makeRelativeLink(
382350 return status;
383351}
384352
385- qint64 FileManager::removeFileOrDirectory (
386- const QString &fileName,
387- bool lookGameDir) {
388- qint64 status = 0 ;
389- const QString path = FileManager::getFullPath (fileName, lookGameDir);
390- QDir dir (path);
391- if (dir.exists () == true ) {
353+ qint64 FileManager::removeFileOrDirectory (Path path) {
354+ qint64 status = 2 ;
355+
356+ if (path.isDir () == true ) {
392357 // Remove directory and its contents
393- if (dir .removeRecursively () == true ) {
394- qDebug () << " Directory removed successfully:" << path;
358+ if (QDir (path. get ()) .removeRecursively () == true ) {
359+ qDebug () << " Directory removed successfully:" << path. get () ;
395360 status = 0 ;
396361 } else {
397- qWarning () << " Failed to remove directory:" << path;
362+ qWarning () << " Failed to remove directory:" << path. get () ;
398363 status = 1 ;
399364 }
400- } else {
401- QFile file (path);
365+ } else if (path. isFile ()) {
366+ QFile file (path. get () );
402367 // Check if the file exists before attempting to remove it
403- if (file.exists () == true ) {
404- if (file.remove () == true ) {
405- qDebug () << " File removed successfully:" << path;
406- status = 0 ;
407- } else {
408- qWarning () << " Failed to remove file:" << path;
409- status = 1 ;
410- }
368+ if (file.remove () == true ) {
369+ qDebug () << " File removed successfully:" << path.get ();
370+ status = 0 ;
411371 } else {
412- qDebug () << " File or directory does not exist :" << path;
413- status = 2 ;
372+ qWarning () << " Failed to remove file :" << path. get () ;
373+ status = 1 ;
414374 }
375+ } else {
376+ qDebug () << " File or directory does not exist:" << path.get ();
415377 }
416378 return status;
417379}
0 commit comments