Skip to content

Commit f863bd7

Browse files
committed
fix misra
and bug if link gets broken original games disappear
1 parent 8e090d1 commit f863bd7

File tree

10 files changed

+106
-63
lines changed

10 files changed

+106
-63
lines changed

.neovim.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ end
8181
require('lint').linters_by_ft = {
8282
sh = {'shellcheck'}, -- Ensure you have shellcheck installed
8383
python = {'pylint', 'bandit', 'ruff', 'pydocstyle', 'mypy', 'flake8'}, -- Ensure these are installed
84-
cmake = { 'cmakelint' },
85-
cpp = {'cppcheck', 'cpplint', 'flawfinder'},
84+
cmake = {'cmakelint'},
85+
cpp = {'cppcheck', 'cpplint', 'flawfinder', 'clangtidy'},
8686
}
8787

8888
-- .local/share/nvim/plugged/nvim-lint/lua/lint/linters/cppcheck.lua

src/FileManager.cpp

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,34 +58,31 @@ inline const QString FileManager::lookGameDir(
5858
return path;
5959
}
6060

61-
const QString FileManager::calculateMD5(const QString& file, bool lookGameDir) {
62-
const QString path = FileManager::lookGameDir(file, lookGameDir);
61+
const QString FileManager::calculateMD5(
62+
const QString& fileName, bool lookGameDir) {
63+
const QString path = FileManager::lookGameDir(fileName, lookGameDir);
6364
QFileInfo fileInfo(path);
65+
QString result;
6466

6567
if (fileInfo.exists() && !fileInfo.isFile()) {
6668
qDebug() << "Error: The path is not a regular file." << path;
67-
return"";
68-
}
69-
70-
QFile f(path);
71-
if (!f.open(QIODevice::ReadOnly)) { // flawfinder: ignore
72-
qDebug() << "Error opening file for reading: " << f.errorString();
73-
return "";
74-
}
75-
76-
QCryptographicHash md5(QCryptographicHash::Md5);
77-
78-
std::array<char, 1024> buffer;
79-
const int size = buffer.size();
80-
int bytesRead;
81-
82-
// flawfinder: ignore
83-
while ((bytesRead = f.read(buffer.data(), size)) > 0) {
84-
md5.addData(buffer.data(), bytesRead);
69+
} else {
70+
QFile file(path);
71+
bool status = file.open(QIODevice::ReadOnly); // flawfinder: ignore
72+
if (!status) {
73+
qDebug()
74+
<< "Error opening file for reading: " << file.errorString();
75+
} else {
76+
QCryptographicHash md5(QCryptographicHash::Md5);
77+
if (!md5.addData(&file)) {
78+
qWarning() << "Failed to process file for MD5 hash.";
79+
} else {
80+
result = QString(md5.result().toHex());
81+
}
82+
file.close();
83+
}
8584
}
86-
87-
f.close();
88-
return QString(md5.result().toHex());
85+
return result;
8986
}
9087

9188
bool FileManager::extractZip(
@@ -195,22 +192,24 @@ bool FileManager::checkFile(const QString& file, bool lookGameDir) {
195192
}
196193

197194
int FileManager::checkFileInfo(const QString& file, bool lookGameDir) {
195+
int status = 0;
198196
const QString path = FileManager::lookGameDir(file, lookGameDir);
199197
QFileInfo fileInfo(path);
200198
if (fileInfo.isDir() == true) {
201199
qDebug() << "The path is a directory.";
202200
if (fileInfo.isSymLink() == true) {
203201
qDebug() << "return value 1:The path is a symbolic link.";
204-
return 1;
202+
status = 1;
205203
} else {
206204
qDebug() << "return value 2:The path is not a symbolic link.";
207-
return 2;
205+
status = 2;
208206
}
209207
} else {
210208
qDebug() << "value 3:The path is not a directory.";
211209
qDebug() << "filePath " << path;
212-
return 3;
210+
status = 3;
213211
}
212+
return status;
214213
}
215214

216215
bool FileManager::linkGameDir(const QString& levelDir, const QString& gameDir) {

src/FileManager.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <QObject>
1919
#include <QFile>
2020
#include <QDir>
21+
#include <QByteArray>
2122
#include <QCryptographicHash>
2223
#include <QDebug>
2324

@@ -35,7 +36,7 @@ class FileManager : public QObject {
3536
bool extractZip(const QString& zipFile, const QString& extractPath);
3637
bool checkDir(const QString& file, bool lookGameDir);
3738
bool checkFile(const QString& file, bool lookGameDir);
38-
int checkFileInfo(const QString& file, bool lookGameDir = true);
39+
int checkFileInfo(const QString& file, bool lookGameDir);
3940
qint64 removeFileOrDirectory(const QString &file, bool lookGameDir);
4041
bool moveFilesToDirectory(
4142
const QString& fromLevelDirectory,
@@ -70,4 +71,5 @@ class FileManager : public QObject {
7071
const QString m_sep = QDir::separator();
7172
Q_DISABLE_COPY(FileManager)
7273
};
74+
7375
#endif // SRC_FILEMANAGER_HPP_

src/Model.cpp

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,30 @@ Model::Model() {
3636

3737
Model::~Model() {}
3838

39-
bool Model::setup(const QString& level, const QString& game) {
40-
bool status = false;
39+
void Model::setup(const QString& level, const QString& game) {
4140
if (setDirectory(level, game) == true) {
42-
status = true;
43-
emit generateListSignal(checkCommonFiles());
41+
QByteArray commonFiles(8, '\0');
42+
checkCommonFiles(&commonFiles);
43+
m_availableGames.clear();
44+
for (int i = 1; i <= 5; i++) {
45+
qint8 dirStatus = commonFiles[i];
46+
if (dirStatus == 1) {
47+
m_availableGames.append(i);
48+
} else if (dirStatus == 2) {
49+
m_availableGames.append(-i);
50+
} else {
51+
if (fileManager.checkDir(
52+
QString("/Original.TR%1").arg(i), false) == true) {
53+
m_availableGames.append(i);
54+
}
55+
}
56+
}
57+
emit generateListSignal(m_availableGames);
4458
QCoreApplication::processEvents();
59+
} else {
60+
// send signal to gui with error about setup fail
61+
qDebug() << "setDirectory setup failed";
4562
}
46-
return status;
4763
}
4864

4965
bool Model::setDirectory(const QString& level, const QString& game) {
@@ -56,17 +72,21 @@ bool Model::setDirectory(const QString& level, const QString& game) {
5672
return status;
5773
}
5874

59-
const QList<int>& Model::checkCommonFiles() {
60-
m_availableGames.clear();
75+
void Model::checkCommonFiles(QByteArray* games) {
6176
for (int i = 1; i <= 5; i++) {
6277
int dirStatus = checkGameDirectory(i);
6378
if (dirStatus == 1) { // symbolic link
64-
m_availableGames.append(i);
79+
m_availableGames.append(1);
6580
} else if (dirStatus == 2) { // directory
66-
m_availableGames.append(-i);
81+
m_availableGames.append(2);
82+
} else if (dirStatus == 3) {
83+
m_availableGames.append(3);
84+
qDebug() << "File could be a broken link or a regular file";
85+
} else {
86+
m_availableGames.append(0);
87+
qDebug() << "File don't exist";
6788
}
6889
}
69-
return m_availableGames;
7090
}
7191

7292
QString Model::getGameDirectory(int id) {
@@ -227,25 +247,27 @@ bool Model::getLevelDontHaveFile(
227247
return status;
228248
}
229249

230-
bool Model::getLevel(int id) {
250+
void Model::getLevel(int id) {
231251
assert(id > 0);
232-
bool status = false;
233252
if (id > 0) {
253+
bool status = false;
234254
ZipData zipData = data.getDownload(id);
235255
downloader.setUrl(zipData.url);
236256
downloader.setSaveFile(zipData.name);
257+
// this if just slips up execution but has nothing to do with the error
237258
if (fileManager.checkFile(zipData.name, false)) {
238-
qDebug() << "File exists:" << zipData.name;
259+
qWarning() << "File exists:" << zipData.name;
239260
status = getLevelHaveFile(id, zipData.md5sum, zipData.name);
240261
} else {
241-
qWarning() << "File does not exist:" << zipData.name;
262+
qDebug() << "File does not exist:" << zipData.name;
242263
status = getLevelDontHaveFile(id, zipData.md5sum, zipData.name);
243264
}
244265
if (status == true) {
245-
unpackLevel(id, zipData.name);
266+
if (!unpackLevel(id, zipData.name)) {
267+
qDebug() << "unpackLevel failed";
268+
}
246269
}
247270
}
248-
return status;
249271
}
250272

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

src/Model.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <QObject>
1818
#include <QMap>
19+
#include <QBitArray>
1920
#include <QDebug>
2021
#include <QtCore>
2122
#include <cassert>
@@ -55,19 +56,19 @@ class Model : public QObject {
5556
static Model instance;
5657
return instance;
5758
}
58-
const QList<int>& checkCommonFiles();
59+
void checkCommonFiles(QByteArray* games);
5960
int checkGameDirectory(int id);
6061
int checkLevelDirectory(int id);
6162
void getList(QVector<ListItemData>* list);
6263
int getItemState(int id);
6364
bool setLink(int id);
6465
QString getGameDirectory(int id);
6566
void setupGame(int id);
66-
bool getLevel(int id);
67+
void getLevel(int id);
6768
const InfoData getInfo(int id);
6869
const QString getWalkthrough(int id);
6970
bool setDirectory(const QString& level, const QString& game);
70-
bool setup(const QString& level, const QString& game);
71+
void setup(const QString& level, const QString& game);
7172

7273
signals:
7374
void generateListSignal(QList<int> availableGames);

src/TombRaiderLinuxLauncher.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "TombRaiderLinuxLauncher.hpp"
1616
#include "ui_TombRaiderLinuxLauncher.h"
1717
#include "staticData.hpp"
18-
#include "debug.hpp"
18+
// #include "debug.hpp"
1919

2020
TombRaiderLinuxLauncher::TombRaiderLinuxLauncher(QWidget *parent)
2121
: QMainWindow(parent) {
@@ -58,7 +58,7 @@ TombRaiderLinuxLauncher::TombRaiderLinuxLauncher(QWidget *parent)
5858
filter_p->setVisible(false);
5959

6060
connect(ui->filterButton, &QPushButton::clicked,
61-
[filter_p, filterButton_p]() {
61+
[filter_p, filterButton_p]() -> void {
6262
bool isVisible = !filter_p->isVisible();
6363
filter_p->setVisible(isVisible);
6464

@@ -114,13 +114,7 @@ TombRaiderLinuxLauncher::TombRaiderLinuxLauncher(QWidget *parent)
114114

115115
void TombRaiderLinuxLauncher::generateList(const QList<int>& availableGames) {
116116
const QString pictures = ":/pictures/";
117-
118-
QMap<int, QPair<QString, QString>> fileMap = {
119-
{1, {"Tomb_Raider_I.jpg", "I"}},
120-
{2, {"Tomb_Raider_II.jpg", "II"}},
121-
{3, {"Tomb_Raider_III.jpg", "III"}},
122-
{4, {"Tomb_Raider_IIII.jpg", "VI"}},
123-
{5, {"Tomb_Raider_IIIII.jpg", "V"}} };
117+
OriginalGameData pictueData;
124118

125119
foreach(const int &id, availableGames) {
126120
// debugStop(QString("%1").arg(id));
@@ -133,9 +127,15 @@ void TombRaiderLinuxLauncher::generateList(const QList<int>& availableGames) {
133127
linkedGameDir = true;
134128
IdPositive = id;
135129
}
136-
QString iconPath = pictures + fileMap[IdPositive].first;
130+
// debugStop();
131+
// Picture and title
132+
QString iconPath = pictures + pictueData.getPicture(IdPositive);
137133
QString itemName =
138-
QString("Tomb Raider %1 Original").arg(fileMap[IdPositive].second);
134+
QString("Tomb Raider %1 Original")
135+
.arg(pictueData.romanNumerals[IdPositive]);
136+
137+
qDebug() << "iconPath :" << iconPath;
138+
qDebug() << "itemName :" << itemName;
139139
QListWidgetItem *wi = new QListWidgetItem(QIcon(iconPath), itemName);
140140
wi->setData(Qt::UserRole, QVariant(IdPositive*(-1)));
141141
wi->setData(Qt::UserRole + 1, QVariant(linkedGameDir));
@@ -344,8 +344,8 @@ void TombRaiderLinuxLauncher::originalSelected(QListWidgetItem *selectedItem) {
344344
if (selectedItem != nullptr) {
345345
int id = selectedItem->data(Qt::UserRole).toInt();
346346
bool linkedGameDir = selectedItem->data(Qt::UserRole + 1).toBool();
347-
// the game dirr was a symbolic link and it has a level dir
348-
if (linkedGameDir == true && controller.getItemState(id) == 1) {
347+
// the game directory was a symbolic link and it has a level directory
348+
if ((linkedGameDir == true) && (controller.getItemState(id) == 1)) {
349349
ui->pushButtonLink->setEnabled(true);
350350
ui->pushButtonDownload->setEnabled(false);
351351
} else {

src/TombRaiderLinuxLauncher.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,23 @@ class TombRaiderLinuxLauncher : public QMainWindow {
145145
QSettings m_settings;
146146
Ui::TombRaiderLinuxLauncher *ui;
147147
};
148+
149+
struct OriginalGameData {
150+
QMap<int, QString> romanNumerals = {
151+
{0, "null"},
152+
{1, "I"},
153+
{2, "II"},
154+
{3, "III"},
155+
{4, "IV"},
156+
{5, "V"},
157+
{6, "VI"},
158+
{7, "IUB"},
159+
{8, "IIGM"},
160+
{9, "IIILM"},
161+
};
162+
const QString getPicture(int id) {
163+
return QString ("Tomb_Raider_%1.jpg").arg(romanNumerals[id]);
164+
}
165+
};
166+
148167
#endif // SRC_TOMBRAIDERLINUXLAUNCHER_HPP_

src/resources.qrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
<file>pictures/Tomb_Raider_I.jpg</file>
99
<file>pictures/Tomb_Raider_II.jpg</file>
1010
<file>pictures/Tomb_Raider_III.jpg</file>
11-
<file>pictures/Tomb_Raider_IIII.jpg</file>
12-
<file>pictures/Tomb_Raider_IIIII.jpg</file>
11+
<file>pictures/Tomb_Raider_IV.jpg</file>
12+
<file>pictures/Tomb_Raider_V.jpg</file>
1313
<file>pictures/Tomb_Raider_III_LA.jpg</file>
1414
<file>pictures/Tomb_Raider_III_unkown.jpg</file>
1515
</qresource>

0 commit comments

Comments
 (0)