Skip to content

Commit f4f5a5f

Browse files
committed
let is change the md5sum
its only used to check if it still have the same file
1 parent 8d7bd6c commit f4f5a5f

File tree

6 files changed

+167
-96
lines changed

6 files changed

+167
-96
lines changed

src/Data.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,32 @@ ZipData Data::getDownload(const int id) {
151151
return ZipData();
152152
}
153153

154+
void Data::setDownloadMd5(const int id, const QString& newMd5sum) {
155+
QSqlQuery query(db);
156+
bool status = false;
157+
status = query.prepare(
158+
"UPDATE Zip "
159+
"SET md5sum = :newMd5sum "
160+
"WHERE Zip.ZipID IN ("
161+
" SELECT ZipList.zipID"
162+
" FROM Level"
163+
" JOIN ZipList ON Level.LevelID = ZipList.levelID"
164+
" WHERE Level.LevelID = :id)");
165+
166+
if (status) {
167+
query.bindValue(":newMd5sum", newMd5sum);
168+
query.bindValue(":id", id);
169+
170+
if (!query.exec()) {
171+
qDebug() << "Error executing query:" << query.lastError().text();
172+
} else {
173+
qDebug() << "md5sum updated successfully.";
174+
}
175+
} else {
176+
qDebug() << "Error preparing query:" << query.lastError().text();
177+
}
178+
}
179+
154180
QVector<FileList> Data::getFileList(const int id) {
155181
QVector<FileList> list;
156182
QSqlQuery query(db);
@@ -172,4 +198,3 @@ QVector<FileList> Data::getFileList(const int id) {
172198
}
173199
return list;
174200
}
175-

src/Data.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class Data : public QObject {
219219
} else {
220220
db = QSqlDatabase::addDatabase("QSQLITE");
221221
db.setDatabaseName(path + "/tombll.db");
222-
db.setConnectOptions("QSQLITE_OPEN_READONLY");
222+
// db.setConnectOptions("QSQLITE_OPEN_READONLY");
223223
if (db.open() == true) { // flawfinder: ignore
224224
status = true;
225225
} else {
@@ -240,7 +240,8 @@ class Data : public QObject {
240240
int getType(int id);
241241

242242
QVector<FileList> getFileList(const int id);
243-
ZipData getDownload(int id);
243+
ZipData getDownload(const int id);
244+
void setDownloadMd5(const int id, const QString& newMd5sum);
244245

245246
private:
246247
explicit Data(QObject *parent = nullptr) : QObject(parent) {

src/FileManager.cpp

Lines changed: 61 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -331,83 +331,87 @@ int FileManager::createDirectory(const QString &file, bool gameDir) {
331331
return status;
332332
}
333333

334-
int FileManager::copyFile(
335-
const QString &gameFile,
336-
const QString &levelFile,
337-
bool fromGameDir) {
334+
int FileManager::copyFile(const QString &gameFile,
335+
const QString &levelFile, bool fromGameDir) {
336+
int status = 0;
338337

339-
const QString& gamePath = m_gameDir.absolutePath() + gameFile;
340-
const QString& levelPath = m_levelDir.absolutePath() + levelFile;
338+
const QString gamePath = QString("%1%2")
339+
.arg(m_gameDir.absolutePath(), gameFile);
341340

342-
const QString& g = fromGameDir ? gamePath : levelPath;
343-
const QString& l = fromGameDir ? levelPath : gamePath;
341+
const QString levelPath = QString("%1%2")
342+
.arg(m_levelDir.absolutePath(), levelFile);
343+
344+
const QString sourceFile = fromGameDir ? gamePath : levelPath;
345+
const QString destinationFile = fromGameDir ? levelPath : gamePath;
344346

345347
// Ensure the destination directory exists
346-
const QFileInfo destinationFileInfo(l);
348+
const QFileInfo destinationFileInfo(destinationFile);
347349
QDir destinationDir(destinationFileInfo.absolutePath());
348-
if (!destinationDir.exists()) {
350+
if (!destinationDir.exists() == true) {
349351
if (!QDir().mkpath(destinationDir.absolutePath())) {
350352
qDebug() << "Error creating destination directory.";
351-
return 1;
353+
status = 1;
352354
}
353-
}
354-
355-
// Copy the file
356-
if (QFile::copy(g, l)) {
357-
qDebug() << "File copy to " + l +" successfully.";
358-
return 0;
355+
} else if (QFile::copy(sourceFile, destinationFile) == true) {
356+
qDebug() << "File copy to " << destinationFile << " successfully.";
357+
status = 0;
359358
} else {
360-
if (QFile::exists(l)) {
361-
qDebug() << "File exist";
362-
return 2;
359+
if (QFile::exists(destinationFile) == true) {
360+
qDebug() << "Target file already exist.";
361+
status = 2;
363362
} else {
364-
qDebug() << "Failed to copy file and dose not exist "
365-
<< l << Qt::endl;
366-
return 3;
363+
qDebug() << "Failed to copy file: " << destinationFile;
364+
status = 3;
367365
}
368366
}
367+
return status;
369368
}
370369

371370
int FileManager::cleanWorkingDir(const QString &levelDir) {
372-
const QString& directoryPath =
373-
m_levelDir.absolutePath() + m_sep + levelDir;
371+
int status = 0;
372+
const QString directoryPath = QString("%1%2%3")
373+
.arg(m_levelDir.absolutePath(), m_sep, levelDir);
374374

375375
QDir directory(directoryPath);
376-
if (directory.exists()) {
377-
if (directory.removeRecursively()) {
376+
if (directory.exists() == true) {
377+
if (directory.removeRecursively() == true) {
378378
qDebug() << "Working Directory removed successfully.";
379-
return 0;
379+
status = 0;
380380
} else {
381381
qDebug() << "Error removing working directory.";
382-
return 1;
382+
status = 1;
383383
}
384+
} else {
385+
qDebug() << "Error working directory seems to not exist";
384386
}
385-
qDebug() << "Error working directory seems to not exist";
386-
return 3;
387+
return status;
387388
}
388389

389390
bool FileManager::backupGameDir(const QString &gameDir) {
390-
const QString& source = m_gameDir.absolutePath() + gameDir.chopped(1);
391-
const QString& des = source + ".old";
391+
bool status = false;
392+
const QString source = QString("%1%2")
393+
.arg(m_gameDir.absolutePath(), gameDir.chopped(1));
394+
const QString destination = QString("%1%2").arg(source, ".old");
392395
QDir directory;
393-
if (directory.rename(source, des)) {
394-
qDebug() << "Directory renamed successfully. New path:" << des;
395-
return true;
396+
if (directory.rename(source, destination) == true) {
397+
qDebug() << "Directory renamed successfully. New path:" << destination;
398+
status = true;
396399
} else {
397400
qWarning() << "Failed to rename directory:" << source;
398-
return false;
401+
status = false;
399402
}
403+
return status;
400404
}
401405

402406
bool FileManager::moveFilesToDirectory(
403407
const QString& fromLevelDirectory,
404408
const QString& toLevelDirectory) {
405409

406-
const QString& directoryFromPath =
407-
m_levelDir.absolutePath() + m_sep + fromLevelDirectory;
410+
const QString directoryFromPath = QString("%1%2%3")
411+
.arg(m_levelDir.absolutePath(), m_sep, fromLevelDirectory);
408412

409-
const QString& directoryToPath =
410-
m_levelDir.absolutePath() + m_sep + toLevelDirectory;
413+
const QString directoryToPath = QString("%1%2%3")
414+
.arg(m_levelDir.absolutePath(), m_sep, toLevelDirectory);
411415

412416
QDir dir(directoryFromPath);
413417

@@ -420,23 +424,29 @@ bool FileManager::moveFilesToDirectory(
420424
entryFileList.cbegin(),
421425
entryFileList.cend(),
422426
[&](const QString& entry) {
423-
QString entryPath = directoryFromPath + m_sep + entry;
424-
if (!QFile::rename(entryPath, directoryToPath + m_sep + entry)) {
425-
qWarning() << "Failed to move file:" << entryPath;
426-
return false;
427+
bool status = true;
428+
429+
QString entryPath = QString("%1%2%3")
430+
.arg(directoryFromPath, m_sep, entry);
431+
QString destinationPath = QString("%1%2%3")
432+
.arg(directoryToPath, m_sep, entry);
433+
434+
if (!QFile::rename(entryPath, destinationPath)) {
435+
qWarning() << "Failed to move file from:"
436+
<< entryPath << "to:" << destinationPath;
437+
status = false;
427438
}
428-
return true;
439+
440+
return status;
429441
});
430442
return allMoved;
431443
}
432444

433445
bool FileManager::moveFilesToParentDirectory(
434-
const QString& levelDirectory,
435-
int levelsUp) {
436-
446+
const QString& levelDirectory, int levelsUp) {
437447
const QString& sep = QDir::separator();
438-
QDir levelDirectoryFullPath(m_levelDir.absolutePath() + sep +
439-
levelDirectory);
448+
QDir levelDirectoryFullPath(QString("%1%2%3")
449+
.arg(m_levelDir.absolutePath(), sep, levelDirectory));
440450

441451
if (!levelDirectoryFullPath.exists()) {
442452
qWarning() << "Directory does not exist:"

src/Model.cpp

Lines changed: 65 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@
1919
Model::Model(QObject *parent) : QObject(parent) {
2020
instructionManager.addInstruction(4, [this](int id) {
2121
qDebug() << "Perform Operation A";
22-
const QString s = "/"+QString::number(id) + ".TRLE";
22+
const QString s = QString("/%1.TRLE").arg(id);
2323
fileManager.makeRelativeLink(s, "/The Rescue.exe", "/tomb4.exe");
2424
});
2525
instructionManager.addInstruction(5, [this](int id) {
2626
qDebug() << "Perform Operation B";
27-
const QString s = "/"+QString::number(id) + ".TRLE";
27+
const QString s = QString("/%1.TRLE").arg(id);
2828
fileManager.makeRelativeLink(s, "/War of the Worlds.exe", "/tomb4.exe");
2929
});
3030
instructionManager.addInstruction(11, [this](int id) {
3131
qDebug() << "Perform Operation C";
32-
const QString s = QString::number(id) + ".TRLE/TRBiohazard";
32+
const QString s = QString("%1.TRLE/TRBiohazard").arg(id);
3333
fileManager.moveFilesToParentDirectory(s, 1);
3434
});
3535
}
@@ -109,7 +109,7 @@ int Model::getItemState(int id) {
109109
if (id < 0) {
110110
status = 1;
111111
} else if (id > 0) {
112-
QString dir(QString::number(id) + ".TRLE");
112+
QString dir = QString("%1.TRLE").arg(id);
113113
if (fileManager.checkDir(dir, false)) {
114114
status = 2;
115115
} else {
@@ -125,11 +125,11 @@ bool Model::setLink(int id) {
125125
bool status = false;
126126
if (id < 0) { // we use original game id as negative number
127127
id = -id;
128-
const QString s = "/Original.TR" + QString::number(id);
128+
const QString s = QString("/Original.TR%1").arg(id);
129129
if (fileManager.checkDir(s, false ))
130130
status = fileManager.linkGameDir(s, getGameDirectory(id));
131131
} else if (id > 0) {
132-
const QString s = "/"+QString::number(id) + ".TRLE";
132+
const QString s = QString("/%1.TRLE").arg(id);
133133
const int t = data.getType(id);
134134

135135
if (fileManager.checkDir(s, false ))
@@ -143,8 +143,8 @@ void Model::setupGame(int id) {
143143
const size_t s = list.size();
144144
assert(s != 0);
145145

146-
const QString levelPath = "/Original.TR" + QString::number(id) +"/";
147-
const QString gamePath = getGameDirectory(id) + "/";
146+
const QString levelPath = QString("/Original.TR%1/").arg(id);
147+
const QString gamePath = QString("%1/").arg(getGameDirectory(id));
148148

149149
for (size_t i = 0; i < s; i++) {
150150
const QString levelFile = QString("%1%2").arg(levelPath, list[i].path);
@@ -176,51 +176,76 @@ void Model::setupGame(int id) {
176176
}
177177
}
178178

179+
bool Model::unpackLevel(const int id, const QString& name) {
180+
bool status = false;
181+
const QString directory = QString("%1.TRLE").arg(id);
182+
if (fileManager.extractZip(name, directory) == true) {
183+
instructionManager.executeInstruction(id);
184+
status = true;
185+
}
186+
return status;
187+
}
188+
189+
bool Model::getLevelHaveFile(
190+
const int id, const QString& md5sum, const QString& name) {
191+
bool status = false;
192+
if (md5sum != "") {
193+
const QString existingFilesum = fileManager.calculateMD5(name, false);
194+
if (existingFilesum != md5sum) {
195+
downloader.run();
196+
if (downloader.getStatus() == 0) {
197+
const QString downloadedSum =
198+
fileManager.calculateMD5(name, false);
199+
if (downloadedSum != md5sum) {
200+
data.setDownloadMd5(id, downloadedSum);
201+
}
202+
status = true;
203+
}
204+
} else {
205+
// send 50% signal for skipped downloading ticks
206+
for (int i=0; i < 50; i++) {
207+
emit this->modelTickSignal();
208+
QCoreApplication::processEvents();
209+
}
210+
status = true;
211+
}
212+
}
213+
return status;
214+
}
215+
216+
bool Model::getLevelDontHaveFile(
217+
const int id, const QString& md5sum, const QString& name) {
218+
bool status = false;
219+
downloader.run();
220+
if (!downloader.getStatus()) {
221+
const QString downloadedSum = fileManager.calculateMD5(name, false);
222+
if (downloadedSum != md5sum) {
223+
data.setDownloadMd5(id, downloadedSum);
224+
}
225+
status = true;
226+
}
227+
return status;
228+
}
229+
179230
bool Model::getLevel(int id) {
180231
assert(id > 0);
232+
bool status = false;
181233
if (id) {
182-
int status = 0;
183234
ZipData zipData = data.getDownload(id);
184235
downloader.setUrl(zipData.url);
185236
downloader.setSaveFile(zipData.name);
186-
187237
if (fileManager.checkFile(zipData.name, false)) {
188238
qDebug() << "File exists:" << zipData.name;
189-
const QString sum = fileManager.calculateMD5(zipData.name, false);
190-
if (sum != zipData.md5sum) {
191-
downloader.run();
192-
status = downloader.getStatus();
193-
} else {
194-
// send 50% signal here
195-
for (int i=0; i < 50; i++) {
196-
emit this->modelTickSignal();
197-
QCoreApplication::processEvents();
198-
}
199-
}
239+
status = getLevelHaveFile(id, zipData.md5sum, zipData.name);
200240
} else {
201241
qWarning() << "File does not exist:" << zipData.name;
202-
downloader.run();
203-
status = downloader.getStatus();
242+
status = getLevelDontHaveFile(id, zipData.md5sum, zipData.name);
204243
}
205-
if (status == 0) {
206-
/* when the problem about updateing sum is solved it should verify
207-
* the download md5sum can just change on trle so it should just
208-
* update it
209-
const QString sum = fileManager.calculateMD5(zipData.name, false);
210-
if (sum == zipData.md5sum) {
211-
const QString directory = QString::number(id)+".TRLE";
212-
fileManager.extractZip(zipData.name, directory);
213-
instructionManager.executeInstruction(id);
214-
return true;
215-
}
216-
*/
217-
const QString directory = QString::number(id)+".TRLE";
218-
fileManager.extractZip(zipData.name, directory);
219-
instructionManager.executeInstruction(id);
220-
return true;
244+
if (status == true) {
245+
unpackLevel(id, zipData.name);
221246
}
222247
}
223-
return false;
248+
return status;
224249
}
225250

226251
const InfoData Model::getInfo(int id) {

0 commit comments

Comments
 (0)