Skip to content

Commit

Permalink
Consolidate parsing, read failure error check
Browse files Browse the repository at this point in the history
  • Loading branch information
Silarn committed Jun 17, 2024
1 parent fe79300 commit 1874156
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 52 deletions.
113 changes: 61 additions & 52 deletions src/starfieldunmanagedmods.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "starfieldunmanagedmods.h"

#include "log.h"
#include "scopeguard.h"

#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>

StarfieldUnmangedMods::StarfieldUnmangedMods(const GameStarfield* game,
Expand Down Expand Up @@ -53,40 +55,54 @@ QFileInfo StarfieldUnmangedMods::referenceFile(const QString& modName) const
}
}

QStringList StarfieldUnmangedMods::secondaryFiles(const QString& modName) const
QJsonObject StarfieldUnmangedMods::getContentCatalog() const
{
QFile content(m_AppDataFolder + "/" + game()->gameShortName() +
"/ContentCatalog.txt");
QStringList files;
if (content.exists()) {
if (content.open(QIODevice::OpenModeFlag::ReadOnly)) {
auto contentData = content.readAll();
QJsonDocument contentDoc = QJsonDocument::fromJson(contentData);
QJsonObject contentObj = contentDoc.object();
for (auto mod : contentObj.keys()) {
if (mod == "ContentCatalog")
continue;
auto modData = contentObj.value(mod).toObject();
auto modFiles = modData.value("Files").toArray();
bool found = false;
for (auto file : modFiles) {
if (file.toString().startsWith(modName, Qt::CaseInsensitive)) {
found = true;
}
if (found)
break;
}
if (found) {
for (auto file : modFiles) {
if (!file.toString().endsWith(".esm") &&
!file.toString().endsWith(".esl") && !file.toString().endsWith(".esp"))
files.append(file.toString());
}
break;
}
ON_BLOCK_EXIT([&content]() {
content.close();
});
auto contentData = content.readAll();
QJsonParseError jsonError;
QJsonDocument contentDoc = QJsonDocument::fromJson(contentData, &jsonError);
if (jsonError.error) {
MOBase::log::warn(QObject::tr("ContentCatalog.txt appears to be corrupt: %1")
.arg(jsonError.errorString()));
} else {
return contentDoc.object();
}
}
}
return QJsonObject();
}

QStringList StarfieldUnmangedMods::secondaryFiles(const QString& modName) const
{
QStringList files;
QJsonObject contentObj = getContentCatalog();
for (auto mod : contentObj.keys()) {
if (mod == "ContentCatalog")
continue;
auto modData = contentObj.value(mod).toObject();
auto modFiles = modData.value("Files").toArray();
bool found = false;
for (auto file : modFiles) {
if (file.toString().startsWith(modName, Qt::CaseInsensitive)) {
found = true;
}
if (found)
break;
}
if (found) {
for (auto file : modFiles) {
if (!file.toString().endsWith(".esm") && !file.toString().endsWith(".esl") &&
!file.toString().endsWith(".esp"))
files.append(file.toString());
}
break;
}
content.close();
}
// file extension in FO4 is .ba2 instead of bsa
QMap<QString, QDir> directories = {{"data", game()->dataDirectory()}};
Expand All @@ -103,32 +119,25 @@ QString StarfieldUnmangedMods::displayName(const QString& modName) const
{
QFile content(m_AppDataFolder + "/" + game()->gameShortName() +
"/ContentCatalog.txt");
QString name = modName;
if (content.exists()) {
if (content.open(QIODevice::OpenModeFlag::ReadOnly)) {
auto contentData = content.readAll();
QJsonDocument contentDoc = QJsonDocument::fromJson(contentData);
QJsonObject contentObj = contentDoc.object();
for (auto mod : contentObj.keys()) {
if (mod == "ContentCatalog")
continue;
auto modData = contentObj.value(mod).toObject();
auto modFiles = modData.value("Files").toArray();
bool found = false;
for (auto file : modFiles) {
if (file.toString().startsWith(modName, Qt::CaseInsensitive)) {
found = true;
}
if (found)
break;
}
if (found) {
name = modData.value("Title").toString();
break;
}
QString name = modName;
QJsonObject contentObj = getContentCatalog();
for (auto mod : contentObj.keys()) {
if (mod == "ContentCatalog")
continue;
auto modData = contentObj.value(mod).toObject();
auto modFiles = modData.value("Files").toArray();
bool found = false;
for (auto file : modFiles) {
if (file.toString().startsWith(modName, Qt::CaseInsensitive)) {
found = true;
}
if (found)
break;
}
if (found) {
name = modData.value("Title").toString();
break;
}
content.close();
}
// unlike in earlier games, in fallout 4 the file name doesn't correspond to
// the public name
Expand Down
5 changes: 5 additions & 0 deletions src/starfieldunmanagedmods.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "gamestarfield.h"
#include <gamegamebryo.h>

#include <QJsonObject>

class StarfieldUnmangedMods : public GamebryoUnmangedMods
{
public:
Expand All @@ -16,6 +18,9 @@ class StarfieldUnmangedMods : public GamebryoUnmangedMods
virtual QStringList secondaryFiles(const QString& modName) const override;
virtual QString displayName(const QString& modName) const override;

private:
QJsonObject getContentCatalog() const;

private:
QString m_AppDataFolder;
};
Expand Down

0 comments on commit 1874156

Please sign in to comment.