Skip to content

Commit f0558dd

Browse files
committed
fix some paths problems in FileManager
1 parent 9b2ecec commit f0558dd

File tree

2 files changed

+44
-56
lines changed

2 files changed

+44
-56
lines changed

src/FileManager.cpp

Lines changed: 42 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,20 @@ bool FileManager::setUpCamp(const QString& levelDir, const QString& gameDir) {
4444
return true;
4545
}
4646

47-
const QString FileManager::calculateMD5(const QString& file, bool lookGameDir) {
48-
const QString& path = lookGameDir ?
49-
m_gameDir.absolutePath() + QDir::separator()+file :
50-
m_levelDir.absolutePath() + QDir::separator()+file;
47+
inline const QString FileManager::lookGameDir(
48+
const QString& file,
49+
bool lookGameDir) {
50+
QString path;
51+
if (lookGameDir) {
52+
path = QString("%1%2%3").arg(m_gameDir.absolutePath(), m_sep, file);
53+
} else {
54+
path = QString("%1%2%3").arg(m_levelDir.absolutePath(), m_sep, file);
55+
}
56+
return path;
57+
}
5158

59+
const QString FileManager::calculateMD5(const QString& file, bool lookGameDir) {
60+
const QString path = FileManager::lookGameDir(file, lookGameDir);
5261
QFileInfo fileInfo(path);
5362

5463
if (fileInfo.exists() && !fileInfo.isFile()) {
@@ -79,10 +88,9 @@ bool FileManager::extractZip(
7988
const QString& zipFilename,
8089
const QString& outputFolder) {
8190
const QString& zipPath =
82-
m_levelDir.absolutePath() + QDir::separator() + zipFilename;
83-
91+
QString("%1%2%3").arg(m_levelDir.absolutePath(), m_sep, zipFilename);
8492
const QString& outputPath =
85-
m_levelDir.absolutePath() + QDir::separator() + outputFolder;
93+
QString("%1%2%3").arg(m_levelDir.absolutePath(), m_sep, outputFolder);
8694

8795
qDebug() << "Unzipping file" << zipFilename << "to" << outputPath;
8896

@@ -171,36 +179,23 @@ bool FileManager::extractZip(
171179
*
172180
*/
173181
bool FileManager::checkDir(const QString& file, bool lookGameDir ) {
174-
QString path;
175-
if (!lookGameDir) {
176-
path = m_levelDir.absolutePath() + QDir::separator() + file;
177-
} else {
178-
path = m_gameDir.absolutePath() + QDir::separator() + file;
179-
}
182+
const QString path = FileManager::lookGameDir(file, lookGameDir);
180183
QDir directory(path);
181184
return directory.exists();
182185
}
183186

184187
bool FileManager::checkFile(const QString& file, bool lookGameDir ) {
185-
QString path;
186-
if (!lookGameDir) {
187-
path = m_levelDir.absolutePath() + QDir::separator() + file;
188-
} else {
189-
path = m_gameDir.absolutePath() + QDir::separator() + file;
190-
}
188+
const QString path = FileManager::lookGameDir(file, lookGameDir);
191189
QFile fFile(path);
192190
return fFile.exists();
193191
}
194192

195193
int FileManager::checkFileInfo(const QString& file, bool lookGameDir) {
196-
const QString& path = lookGameDir ?
197-
m_gameDir.absolutePath() + QDir::separator()+file :
198-
m_levelDir.absolutePath() + QDir::separator()+file;
199-
194+
const QString path = FileManager::lookGameDir(file, lookGameDir);
200195
QFileInfo fileInfo(path);
201196
if (fileInfo.isDir()) {
202197
qDebug() << "The path is a directory.";
203-
if (fileInfo.isSymLink()) {
198+
if (fileInfo.isSymLink() == true) {
204199
qDebug() << "return value 1:The path is a symbolic link.";
205200
return 1;
206201
} else {
@@ -215,19 +210,21 @@ int FileManager::checkFileInfo(const QString& file, bool lookGameDir) {
215210
}
216211

217212
bool FileManager::linkGameDir(const QString& levelDir, const QString& gameDir) {
218-
const QString& l = m_levelDir.absolutePath() + levelDir;
219-
const QString& g = m_gameDir.absolutePath() + gameDir;
213+
const QString levelPath = QString("%1%2")
214+
.arg(m_levelDir.absolutePath(), levelDir);
215+
const QString gamePath = QString("%1%2")
216+
.arg(m_gameDir.absolutePath(), gameDir);
220217

221-
test(l);
218+
test(levelPath); // here we just output the directory tree for now..
222219

223-
if (QFile::link(l, g)) {
220+
if (QFile::link(levelPath, gamePath)) {
224221
qDebug() << "Symbolic link created successfully.";
225222
return 0;
226223
} else {
227-
QFileInfo i(g);
228-
if (i.isSymLink()) {
229-
QFile::remove(g);
230-
if (QFile::link(l, g)) {
224+
QFileInfo fileInfo(gamePath);
225+
if (fileInfo.isSymLink() == true) {
226+
QFile::remove(gamePath);
227+
if (QFile::link(levelPath, gamePath)) {
231228
qDebug() << "Symbolic link created successfully.";
232229
return 0;
233230
} else {
@@ -271,29 +268,25 @@ bool FileManager::makeRelativeLink(
271268
}
272269

273270
qint64 FileManager::removeFileOrDirectory(
274-
const QString &file,
271+
const QString &fileName,
275272
bool lookGameDir) {
276273
qint64 status = 0;
277-
const QString& sep = QDir::separator();
278-
const QString& gamePath = m_gameDir.absolutePath() + sep + file;
279-
const QString& levelPath = m_levelDir.absolutePath() + sep + file;
280-
const QString& path = lookGameDir ? gamePath : levelPath;
281-
274+
const QString path = FileManager::lookGameDir(fileName, lookGameDir);
282275
QDir dir(path);
283-
if (dir.exists()) {
276+
if (dir.exists() == true) {
284277
// Remove directory and its contents
285-
if (dir.removeRecursively()) {
278+
if (dir.removeRecursively() == true) {
286279
qDebug() << "Directory removed successfully:" << path;
287280
status = 0;
288281
} else {
289282
qWarning() << "Failed to remove directory:" << path;
290283
status = 1;
291284
}
292285
} else {
293-
QFile f(path);
286+
QFile file(path);
294287
// Check if the file exists before attempting to remove it
295-
if (f.exists()) {
296-
if (f.remove()) {
288+
if (file.exists() == true) {
289+
if (file.remove() == true) {
297290
qDebug() << "File removed successfully:" << path;
298291
status = 0;
299292
} else {
@@ -310,11 +303,7 @@ qint64 FileManager::removeFileOrDirectory(
310303

311304
int FileManager::createDirectory(const QString &file, bool gameDir) {
312305
qint64 status = 0;
313-
const QString& sep = QDir::separator();
314-
const QString& gamePath = m_gameDir.absolutePath() + sep + file;
315-
const QString& levelPath = m_levelDir.absolutePath() + sep + file;
316-
const QString& path = gameDir ? gamePath : levelPath;
317-
306+
const QString path = FileManager::lookGameDir(file, gameDir);
318307
// Create the directory if it doesn't exist
319308
if (!QDir(path).exists()) {
320309
if (QDir().mkpath(path)) {
@@ -369,9 +358,8 @@ int FileManager::copyFile(
369358
}
370359

371360
int FileManager::cleanWorkingDir(const QString &levelDir) {
372-
const QString sep = QDir::separator();
373361
const QString& directoryPath =
374-
m_levelDir.absolutePath() + sep + levelDir;
362+
m_levelDir.absolutePath() + m_sep + levelDir;
375363

376364
QDir directory(directoryPath);
377365
if (directory.exists()) {
@@ -404,13 +392,11 @@ bool FileManager::moveFilesToDirectory(
404392
const QString& fromLevelDirectory,
405393
const QString& toLevelDirectory) {
406394

407-
const QString& sep = QDir::separator();
408-
409395
const QString& directoryFromPath =
410-
m_levelDir.absolutePath() + sep + fromLevelDirectory;
396+
m_levelDir.absolutePath() + m_sep + fromLevelDirectory;
411397

412398
const QString& directoryToPath =
413-
m_levelDir.absolutePath() + sep + toLevelDirectory;
399+
m_levelDir.absolutePath() + m_sep + toLevelDirectory;
414400

415401
QDir dir(directoryFromPath);
416402

@@ -420,11 +406,11 @@ bool FileManager::moveFilesToDirectory(
420406

421407
// Move files and recursively move directories
422408
for (const QString& entry : entryFileList) {
423-
QString entryPath = directoryFromPath + sep + entry;
409+
QString entryPath = directoryFromPath + m_sep + entry;
424410

425411
if (!QFile::rename(
426412
entryPath,
427-
directoryToPath + sep + entry)) {
413+
directoryToPath + m_sep + entry)) {
428414
qWarning() << "Failed to move file:" << entryPath;
429415
return false;
430416
}

src/FileManager.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class FileManager : public QObject {
3131
static FileManager instance;
3232
return instance;
3333
}
34+
const QString lookGameDir(const QString& file, bool lookGameDir);
3435
const QString calculateMD5(const QString& file, bool lookGameDir);
3536
bool extractZip(const QString& zipFile, const QString& extractPath);
3637
bool checkDir(const QString& file, bool lookGameDir);
@@ -67,6 +68,7 @@ class FileManager : public QObject {
6768

6869
QDir m_levelDir;
6970
QDir m_gameDir;
71+
const QString m_sep = QDir::separator();
7072
Q_DISABLE_COPY(FileManager)
7173
};
7274
#endif // SRC_FILEMANAGER_HPP_

0 commit comments

Comments
 (0)