Skip to content

Commit

Permalink
improve setupGame byt not using stupid std::array<QVector<QString>, 2>
Browse files Browse the repository at this point in the history
  • Loading branch information
noisecode3 committed Dec 23, 2024
1 parent 1c619c5 commit 8ae4c54
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
9 changes: 5 additions & 4 deletions src/Data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ ZipData Data::getDownload(const int id) {
return ZipData();
}

std::array<QVector<QString>, 2> Data::getFileList(const int id) {
std::array<QVector<QString>, 2> list;
QVector<FileList> Data::getFileList(const int id) {
QVector<FileList> list;
QSqlQuery query(db);
if (!query.prepare("SELECT File.path, File.md5sum "
"FROM File "
Expand All @@ -163,8 +163,9 @@ std::array<QVector<QString>, 2> Data::getFileList(const int id) {
query.bindValue(":id", id);
if (query.exec()) {
while (query.next()) {
list[0] << query.value("path").toString();
list[1] << query.value("md5sum").toString();
list.append({
query.value("path").toString(),
query.value("md5sum").toString()});
}
} else {
qDebug() << "Error executing query:" << query.lastError().text();
Expand Down
7 changes: 6 additions & 1 deletion src/Data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
#include <QSqlError>
#include <QSqlQuery>

struct FileList {
QString path;
QString md5sum;
};

/**
* @struct FolderNames
* @brief Folder names game used on Windows
Expand Down Expand Up @@ -234,7 +239,7 @@ class Data : public QObject {
QString getWalkthrough(int id);
int getType(int id);

std::array<QVector<QString>, 2> getFileList(const int id);
QVector<FileList> getFileList(const int id);
ZipData getDownload(int id);

private:
Expand Down
46 changes: 22 additions & 24 deletions src/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,41 +143,39 @@ bool Model::setLink(int id) {
}

void Model::setupGame(int id) {
std::array<QVector<QString>, 2> list = data.getFileList(id);
const size_t s = list[0].size();
const size_t sm = list[1].size();
if (s != sm) {
qDebug()
<< "Corrupt list, there seems to bee"
<< " more or less checksums for the files\n";
assert(false);
}
QVector<FileList> list = data.getFileList(id);
const size_t s = list.size();
assert(s != 0);
const QString sd = "/Original.TR" + QString::number(id) +"/";
const QString sg = getGameDirectory(id) + "/";

const QString levelPath = "/Original.TR" + QString::number(id) +"/";
const QString gamePath = getGameDirectory(id) + "/";

for (size_t i = 0; i < s; i++) {
const QString& fFile = list[0][i];
const QString& fMd5sum = list[1][i];
const QString& calculated = fileManager.calculateMD5(sg+fFile, true);
if (fMd5sum == calculated) {
fileManager.copyFile(sg+fFile, sd+fFile, true);
const QString levelFile = QString("%1%2").arg(levelPath, list[i].path);
const QString gameFile = QString("%1%2").arg(gamePath, list[i].path);
const QString calculated = fileManager.calculateMD5(gameFile, true);

if (list[i].md5sum == calculated) {
fileManager.copyFile(gameFile, levelFile, true);
} else {
qDebug() << "Original file was modified, had" << fMd5sum
qDebug() << "Original file was modified, had" << list[i].md5sum
<< " got " << calculated << " for file "
<< fFile << Qt::endl;
fileManager.cleanWorkingDir(sd + fFile);
<< list[i].path << Qt::endl;
fileManager.cleanWorkingDir(levelPath);
break;
}
}
if (fileManager.backupGameDir(sg)) {
const QString src = sd.chopped(1);
const QString des = sg.chopped(1);
if (fileManager.backupGameDir(gamePath)) {
// remove the ending '/' and instantly link to
// the game directory link to new game directory
const QString src = levelPath.chopped(1);
const QString des = gamePath.chopped(1);
if (!fileManager.linkGameDir(src, des)) {
checkCommonFiles();
checkCommonFiles(); // TODO(noisecode3): Remove this
return;
}
}
checkCommonFiles();
checkCommonFiles(); // TODO(noisecode3): Remove this
}

bool Model::getGame(int id) {
Expand Down

0 comments on commit 8ae4c54

Please sign in to comment.