Skip to content

Commit 8ae4c54

Browse files
committed
improve setupGame byt not using stupid std::array<QVector<QString>, 2>
1 parent 1c619c5 commit 8ae4c54

File tree

3 files changed

+33
-29
lines changed

3 files changed

+33
-29
lines changed

src/Data.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ ZipData Data::getDownload(const int id) {
151151
return ZipData();
152152
}
153153

154-
std::array<QVector<QString>, 2> Data::getFileList(const int id) {
155-
std::array<QVector<QString>, 2> list;
154+
QVector<FileList> Data::getFileList(const int id) {
155+
QVector<FileList> list;
156156
QSqlQuery query(db);
157157
if (!query.prepare("SELECT File.path, File.md5sum "
158158
"FROM File "
@@ -163,8 +163,9 @@ std::array<QVector<QString>, 2> Data::getFileList(const int id) {
163163
query.bindValue(":id", id);
164164
if (query.exec()) {
165165
while (query.next()) {
166-
list[0] << query.value("path").toString();
167-
list[1] << query.value("md5sum").toString();
166+
list.append({
167+
query.value("path").toString(),
168+
query.value("md5sum").toString()});
168169
}
169170
} else {
170171
qDebug() << "Error executing query:" << query.lastError().text();

src/Data.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
#include <QSqlError>
2626
#include <QSqlQuery>
2727

28+
struct FileList {
29+
QString path;
30+
QString md5sum;
31+
};
32+
2833
/**
2934
* @struct FolderNames
3035
* @brief Folder names game used on Windows
@@ -234,7 +239,7 @@ class Data : public QObject {
234239
QString getWalkthrough(int id);
235240
int getType(int id);
236241

237-
std::array<QVector<QString>, 2> getFileList(const int id);
242+
QVector<FileList> getFileList(const int id);
238243
ZipData getDownload(int id);
239244

240245
private:

src/Model.cpp

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -143,41 +143,39 @@ bool Model::setLink(int id) {
143143
}
144144

145145
void Model::setupGame(int id) {
146-
std::array<QVector<QString>, 2> list = data.getFileList(id);
147-
const size_t s = list[0].size();
148-
const size_t sm = list[1].size();
149-
if (s != sm) {
150-
qDebug()
151-
<< "Corrupt list, there seems to bee"
152-
<< " more or less checksums for the files\n";
153-
assert(false);
154-
}
146+
QVector<FileList> list = data.getFileList(id);
147+
const size_t s = list.size();
155148
assert(s != 0);
156-
const QString sd = "/Original.TR" + QString::number(id) +"/";
157-
const QString sg = getGameDirectory(id) + "/";
149+
150+
const QString levelPath = "/Original.TR" + QString::number(id) +"/";
151+
const QString gamePath = getGameDirectory(id) + "/";
152+
158153
for (size_t i = 0; i < s; i++) {
159-
const QString& fFile = list[0][i];
160-
const QString& fMd5sum = list[1][i];
161-
const QString& calculated = fileManager.calculateMD5(sg+fFile, true);
162-
if (fMd5sum == calculated) {
163-
fileManager.copyFile(sg+fFile, sd+fFile, true);
154+
const QString levelFile = QString("%1%2").arg(levelPath, list[i].path);
155+
const QString gameFile = QString("%1%2").arg(gamePath, list[i].path);
156+
const QString calculated = fileManager.calculateMD5(gameFile, true);
157+
158+
if (list[i].md5sum == calculated) {
159+
fileManager.copyFile(gameFile, levelFile, true);
164160
} else {
165-
qDebug() << "Original file was modified, had" << fMd5sum
161+
qDebug() << "Original file was modified, had" << list[i].md5sum
166162
<< " got " << calculated << " for file "
167-
<< fFile << Qt::endl;
168-
fileManager.cleanWorkingDir(sd + fFile);
163+
<< list[i].path << Qt::endl;
164+
fileManager.cleanWorkingDir(levelPath);
169165
break;
170166
}
171167
}
172-
if (fileManager.backupGameDir(sg)) {
173-
const QString src = sd.chopped(1);
174-
const QString des = sg.chopped(1);
168+
if (fileManager.backupGameDir(gamePath)) {
169+
// remove the ending '/' and instantly link to
170+
// the game directory link to new game directory
171+
const QString src = levelPath.chopped(1);
172+
const QString des = gamePath.chopped(1);
175173
if (!fileManager.linkGameDir(src, des)) {
176-
checkCommonFiles();
174+
checkCommonFiles(); // TODO(noisecode3): Remove this
177175
return;
178176
}
179177
}
180-
checkCommonFiles();
178+
checkCommonFiles(); // TODO(noisecode3): Remove this
181179
}
182180

183181
bool Model::getGame(int id) {

0 commit comments

Comments
 (0)