feat: use mod metadata for getting mod information

For now this doesn't mean much, but it will help when we need data
exclusive from the metadata, such as addon id and mod provider.

Also removes the metadata when the mod is deleted, and make the Mod.h
file a little more pleasing to look at :)
This commit is contained in:
flow 2022-04-15 00:24:57 -03:00 committed by flow
parent e93b9560b5
commit fcfb2cfc3d
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
5 changed files with 90 additions and 60 deletions

View File

@ -33,6 +33,30 @@ Mod::Mod(const QFileInfo &file)
m_changedDateTime = file.lastModified();
}
Mod::Mod(const QDir& mods_dir, const Packwiz::Mod& metadata)
: m_file(mods_dir.absoluteFilePath(metadata.filename))
// It is weird, but name is not reliable for comparing with the JAR files name
// FIXME: Maybe use hash when implemented?
, m_mmc_id(metadata.filename)
, m_name(metadata.name)
{
if(m_file.isDir()){
m_type = MOD_FOLDER;
}
else{
if (metadata.filename.endsWith(".zip") || metadata.filename.endsWith(".jar"))
m_type = MOD_ZIPFILE;
else if (metadata.filename.endsWith(".litemod"))
m_type = MOD_LITEMOD;
else
m_type = MOD_SINGLEFILE;
}
m_from_metadata = true;
m_enabled = true;
m_changedDateTime = m_file.lastModified();
}
void Mod::repath(const QFileInfo &file)
{
m_file = file;
@ -101,13 +125,18 @@ bool Mod::enable(bool value)
if (!foo.rename(path))
return false;
}
if(!fromMetadata())
repath(QFileInfo(path));
m_enabled = value;
return true;
}
bool Mod::destroy()
bool Mod::destroy(QDir& index_dir)
{
// Delete metadata
Packwiz::deleteModIndex(index_dir, m_name);
m_type = MOD_UNKNOWN;
return FS::deletePath(m_file.filePath());
}

View File

@ -14,14 +14,14 @@
*/
#pragma once
#include <QFileInfo>
#include <QDateTime>
#include <QFileInfo>
#include <QList>
#include <memory>
#include "ModDetails.h"
#include "modplatform/packwiz/Packwiz.h"
class Mod
{
@ -37,36 +37,18 @@ public:
Mod() = default;
Mod(const QFileInfo &file);
explicit Mod(const QDir& mods_dir, const Packwiz::Mod& metadata);
QFileInfo filename() const
{
return m_file;
}
QString mmc_id() const
{
return m_mmc_id;
}
ModType type() const
{
return m_type;
}
bool valid()
{
return m_type != MOD_UNKNOWN;
}
QFileInfo filename() const { return m_file; }
QDateTime dateTimeChanged() const { return m_changedDateTime; }
QString mmc_id() const { return m_mmc_id; }
ModType type() const { return m_type; }
bool fromMetadata() const { return m_from_metadata; }
bool enabled() const { return m_enabled; }
QDateTime dateTimeChanged() const
{
return m_changedDateTime;
}
bool enabled() const
{
return m_enabled;
}
const ModDetails &details() const;
bool valid() const { return m_type != MOD_UNKNOWN; }
const ModDetails& details() const;
QString name() const;
QString version() const;
QString homeurl() const;
@ -76,21 +58,15 @@ public:
bool enable(bool value);
// delete all the files of this mod
bool destroy();
bool destroy(QDir& index_dir);
// change the mod's filesystem path (used by mod lists for *MAGIC* purposes)
void repath(const QFileInfo &file);
bool shouldResolve() {
return !m_resolving && !m_resolved;
}
bool isResolving() {
return m_resolving;
}
int resolutionTicket()
{
return m_resolutionTicket;
}
bool shouldResolve() const { return !m_resolving && !m_resolved; }
bool isResolving() const { return m_resolving; }
int resolutionTicket() const { return m_resolutionTicket; }
void setResolving(bool resolving, int resolutionTicket) {
m_resolving = resolving;
m_resolutionTicket = resolutionTicket;
@ -104,12 +80,15 @@ public:
protected:
QFileInfo m_file;
QDateTime m_changedDateTime;
QString m_mmc_id;
QString m_name;
ModType m_type = MOD_UNKNOWN;
bool m_from_metadata = false;
std::shared_ptr<ModDetails> m_localDetails;
bool m_enabled = true;
bool m_resolving = false;
bool m_resolved = false;
int m_resolutionTicket = 0;
ModType m_type = MOD_UNKNOWN;
std::shared_ptr<ModDetails> m_localDetails;
};

View File

@ -1,18 +1,35 @@
#include "ModFolderLoadTask.h"
#include <QDebug>
ModFolderLoadTask::ModFolderLoadTask(QDir dir) :
m_dir(dir), m_result(new Result())
#include "modplatform/packwiz/Packwiz.h"
ModFolderLoadTask::ModFolderLoadTask(QDir& mods_dir, QDir& index_dir) :
m_mods_dir(mods_dir), m_index_dir(index_dir), m_result(new Result())
{
}
void ModFolderLoadTask::run()
{
m_dir.refresh();
for (auto entry : m_dir.entryInfoList())
{
Mod m(entry);
m_result->mods[m.mmc_id()] = m;
// Read metadata first
m_index_dir.refresh();
for(auto entry : m_index_dir.entryList()){
// QDir::Filter::NoDotAndDotDot seems to exclude all files for some reason...
if(entry == "." || entry == "..")
continue;
entry.chop(5); // Remove .toml at the end
Mod mod(m_mods_dir, Packwiz::getIndexForMod(m_index_dir, entry));
m_result->mods[mod.mmc_id()] = mod;
}
// Read JAR files that don't have metadata
m_mods_dir.refresh();
for (auto entry : m_mods_dir.entryInfoList())
{
Mod mod(entry);
if(!m_result->mods.contains(mod.mmc_id()))
m_result->mods[mod.mmc_id()] = mod;
}
emit succeeded();
}

View File

@ -19,11 +19,11 @@ public:
}
public:
ModFolderLoadTask(QDir dir);
ModFolderLoadTask(QDir& mods_dir, QDir& index_dir);
void run();
signals:
void succeeded();
private:
QDir m_dir;
QDir& m_mods_dir, m_index_dir;
ResultPtr m_result;
};

View File

@ -79,10 +79,14 @@ bool ModFolderModel::update()
return true;
}
auto task = new ModFolderLoadTask(m_dir);
auto index_dir = indexDir();
auto task = new ModFolderLoadTask(dir(), index_dir);
m_update = task->result();
QThreadPool *threadPool = QThreadPool::globalInstance();
connect(task, &ModFolderLoadTask::succeeded, this, &ModFolderModel::finishUpdate);
threadPool->start(task);
return true;
}
@ -334,7 +338,8 @@ bool ModFolderModel::deleteMods(const QModelIndexList& indexes)
for (auto i: indexes)
{
Mod &m = mods[i.row()];
m.destroy();
auto index_dir = indexDir();
m.destroy(index_dir);
}
return true;
}