Skip to content

Commit 1ce0b43

Browse files
authored
Merge pull request #11 from ModdingLinked/non-nexus-meta-file-handling
Non nexus meta file handling
2 parents 34a0b1b + b66e432 commit 1ce0b43

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

src/installationmanager.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
3535
#include "settings.h"
3636
#include <scopeguard.h>
3737
#include <utility.h>
38+
#include "shared/util.h"
3839

3940
#include <QApplication>
4041
#include <QDateTime>
@@ -632,6 +633,7 @@ InstallationResult InstallationManager::install(const QString& fileName,
632633
});
633634

634635
QFileInfo fileInfo(fileName);
636+
QString url;
635637
if (!getSupportedExtensions().contains(fileInfo.suffix(), Qt::CaseInsensitive)) {
636638
reportError(tr("File format \"%1\" not supported").arg(fileInfo.suffix()));
637639
return InstallationResult(IPluginInstaller::RESULT_FAILED);
@@ -655,6 +657,7 @@ InstallationResult InstallationManager::install(const QString& fileName,
655657
QSettings metaFile(metaName, QSettings::IniFormat);
656658
gameName = metaFile.value("gameName", "").toString();
657659
modID = metaFile.value("modID", 0).toInt();
660+
url = metaFile.value("url", "").toString();
658661
QTextDocument doc;
659662
doc.setHtml(metaFile.value("name", "").toString());
660663
modName.update(doc.toPlainText(), GUESS_FALLBACK);
@@ -700,8 +703,14 @@ InstallationResult InstallationManager::install(const QString& fileName,
700703
// information
701704
QString guessedModName;
702705
int guessedModID = modID;
703-
NexusInterface::interpretNexusFileName(QFileInfo(fileName).fileName(),
704-
guessedModName, guessedModID, false);
706+
707+
if (isNxmLink(url))
708+
NexusInterface::interpretNexusFileName(QFileInfo(fileName).fileName(),
709+
guessedModName, guessedModID, false);
710+
else
711+
NexusInterface::interpretNonNexusFileName(QFileInfo(fileName).fileName(),
712+
guessedModName, guessedModID, false);
713+
705714
if ((modID == 0) && (guessedModID != -1)) {
706715
modID = guessedModID;
707716
} else if (modID != guessedModID) {

src/nexusinterface.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,74 @@ void NexusInterface::setUserAccount(const APIUserAccount& user)
320320
emit requestsChanged(getAPIStats(), m_User);
321321
}
322322

323+
void NexusInterface::interpretNonNexusFileName(const QString& fileName, QString& modName,
324+
int& modID, bool query)
325+
{
326+
qDebug("executing NexusInterface::interpretNonNexusFileName");
327+
// guess the mod name from the file name
328+
static const QRegularExpression complex(
329+
R"(^([a-zA-Z0-9_'"\-.() ]*?)([-_ ][VvRr]+[0-9]+(?:(?:[\.][0-9]+){0,2}|(?:[_][0-9]+){0,2}|(?:[-.][0-9]+){0,2})?[ab]?)??-([1-9][0-9]+)?-.*?\.(zip|rar|7z))");
330+
// complex regex explanation:
331+
// group 1: modname.
332+
// group 2: optional version,
333+
// assumed to start with v (empty most of the time).
334+
// group 3: NexusId,
335+
// assumed wrapped in "-", will miss single digit IDs for better accuracy.
336+
// If no id is present the whole regex does not match.
337+
static const QRegularExpression simple(R"(^[^a-zA-Z]*([a-zA-Z_ ]+))");
338+
auto complexMatch = complex.match(fileName);
339+
auto simpleMatch = simple.match(fileName);
340+
341+
// assume not found
342+
modID = -1;
343+
344+
if (complexMatch.hasMatch()) {
345+
modName = complexMatch.captured(1);
346+
if (!query && !complexMatch.captured(3).isNull()) {
347+
modID = 0; //Todo when other website made an API
348+
}
349+
} else if (simpleMatch.hasMatch()) {
350+
modName = simpleMatch.captured(0);
351+
} else {
352+
modName.clear();
353+
}
354+
355+
if (query) {
356+
SelectionDialog selection(tr("Please pick the mod ID for \"%1\"").arg(fileName));
357+
int index = 0;
358+
auto splits = fileName.split(QRegularExpression("[^0-9]"), Qt::KeepEmptyParts);
359+
for (auto substr : splits) {
360+
bool ok = false;
361+
int value = substr.toInt(&ok);
362+
if (ok) {
363+
QString highlight(fileName);
364+
highlight.insert(index, " *");
365+
highlight.insert(index + substr.length() + 2, "* ");
366+
367+
QStringList choice;
368+
choice << substr;
369+
choice << (index > 0 ? fileName.left(index - 1) : substr);
370+
selection.addChoice(substr, highlight, choice);
371+
}
372+
index += substr.length() + 1;
373+
}
374+
375+
if (selection.numChoices() > 0) {
376+
if (selection.exec() == QDialog::Accepted) {
377+
auto choice = selection.getChoiceData().toStringList();
378+
modID = choice.at(0).toInt();
379+
modName = choice.at(1);
380+
modName = modName.replace('_', ' ').trimmed();
381+
log::debug("user selected mod ID {} and mod name \"{}\"", modID, modName);
382+
} else {
383+
log::debug("user canceled mod ID selection");
384+
}
385+
} else {
386+
log::debug("no possible mod IDs found in file name");
387+
}
388+
}
389+
}
390+
323391
void NexusInterface::interpretNexusFileName(const QString& fileName, QString& modName,
324392
int& modID, bool query)
325393
{

src/nexusinterface.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,16 @@ class NexusInterface : public QObject
518518
APIStats getAPIStats() const;
519519

520520
public:
521+
/**
522+
* @brief guess the mod id from a filename as delivered by nonNexus
523+
* @param fileName name of the file
524+
* @return the guessed mod id
525+
* @note this currently doesn't fit well with the remaining interface but this is the
526+
* best place for the function
527+
*/
528+
static void interpretNonNexusFileName(const QString& fileName, QString& modName,
529+
int& modID, bool query);
530+
521531
/**
522532
* @brief guess the mod id from a filename as delivered by Nexus
523533
* @param fileName name of the file

src/shared/util.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,5 +425,6 @@ void ResetExitFlag()
425425

426426
bool isNxmLink(const QString& link)
427427
{
428-
return link.startsWith("nxm://", Qt::CaseInsensitive);
428+
return link.startsWith("nxm://", Qt::CaseInsensitive) ||
429+
link.startsWith("https://cf-files.nexusmods.com", Qt::CaseInsensitive);
429430
}

0 commit comments

Comments
 (0)