refactor: remove unused mod info and organize some stuff
This commit is contained in:
		@@ -13,12 +13,13 @@
 | 
			
		||||
 * limitations under the License.
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
#include "Mod.h"
 | 
			
		||||
 | 
			
		||||
#include <QDir>
 | 
			
		||||
#include <QString>
 | 
			
		||||
 | 
			
		||||
#include "Mod.h"
 | 
			
		||||
#include <QDebug>
 | 
			
		||||
#include <FileSystem.h>
 | 
			
		||||
#include <QDebug>
 | 
			
		||||
 | 
			
		||||
namespace {
 | 
			
		||||
 | 
			
		||||
@@ -26,8 +27,7 @@ ModDetails invalidDetails;
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
Mod::Mod(const QFileInfo &file)
 | 
			
		||||
Mod::Mod(const QFileInfo& file)
 | 
			
		||||
{
 | 
			
		||||
    repath(file);
 | 
			
		||||
    m_changedDateTime = file.lastModified();
 | 
			
		||||
@@ -37,13 +37,12 @@ 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_internal_id(metadata.filename)
 | 
			
		||||
    , m_name(metadata.name)
 | 
			
		||||
{
 | 
			
		||||
    if(m_file.isDir()){
 | 
			
		||||
    if (m_file.isDir()) {
 | 
			
		||||
        m_type = MOD_FOLDER;
 | 
			
		||||
    }
 | 
			
		||||
    else{
 | 
			
		||||
    } else {
 | 
			
		||||
        if (metadata.filename.endsWith(".zip") || metadata.filename.endsWith(".jar"))
 | 
			
		||||
            m_type = MOD_ZIPFILE;
 | 
			
		||||
        else if (metadata.filename.endsWith(".litemod"))
 | 
			
		||||
@@ -57,43 +56,32 @@ Mod::Mod(const QDir& mods_dir, const Packwiz::Mod& metadata)
 | 
			
		||||
    m_changedDateTime = m_file.lastModified();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Mod::repath(const QFileInfo &file)
 | 
			
		||||
void Mod::repath(const QFileInfo& file)
 | 
			
		||||
{
 | 
			
		||||
    m_file = file;
 | 
			
		||||
    QString name_base = file.fileName();
 | 
			
		||||
 | 
			
		||||
    m_type = Mod::MOD_UNKNOWN;
 | 
			
		||||
 | 
			
		||||
    m_mmc_id = name_base;
 | 
			
		||||
    m_internal_id = name_base;
 | 
			
		||||
 | 
			
		||||
    if (m_file.isDir())
 | 
			
		||||
    {
 | 
			
		||||
    if (m_file.isDir()) {
 | 
			
		||||
        m_type = MOD_FOLDER;
 | 
			
		||||
        m_name = name_base;
 | 
			
		||||
    }
 | 
			
		||||
    else if (m_file.isFile())
 | 
			
		||||
    {
 | 
			
		||||
        if (name_base.endsWith(".disabled"))
 | 
			
		||||
        {
 | 
			
		||||
    } else if (m_file.isFile()) {
 | 
			
		||||
        if (name_base.endsWith(".disabled")) {
 | 
			
		||||
            m_enabled = false;
 | 
			
		||||
            name_base.chop(9);
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
        } else {
 | 
			
		||||
            m_enabled = true;
 | 
			
		||||
        }
 | 
			
		||||
        if (name_base.endsWith(".zip") || name_base.endsWith(".jar"))
 | 
			
		||||
        {
 | 
			
		||||
        if (name_base.endsWith(".zip") || name_base.endsWith(".jar")) {
 | 
			
		||||
            m_type = MOD_ZIPFILE;
 | 
			
		||||
            name_base.chop(4);
 | 
			
		||||
        }
 | 
			
		||||
        else if (name_base.endsWith(".litemod"))
 | 
			
		||||
        {
 | 
			
		||||
        } else if (name_base.endsWith(".litemod")) {
 | 
			
		||||
            m_type = MOD_LITEMOD;
 | 
			
		||||
            name_base.chop(8);
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
        } else {
 | 
			
		||||
            m_type = MOD_SINGLEFILE;
 | 
			
		||||
        }
 | 
			
		||||
        m_name = name_base;
 | 
			
		||||
@@ -109,23 +97,22 @@ bool Mod::enable(bool value)
 | 
			
		||||
        return false;
 | 
			
		||||
 | 
			
		||||
    QString path = m_file.absoluteFilePath();
 | 
			
		||||
    if (value)
 | 
			
		||||
    {
 | 
			
		||||
        QFile foo(path);
 | 
			
		||||
    QFile file(path);
 | 
			
		||||
    if (value) {
 | 
			
		||||
        if (!path.endsWith(".disabled"))
 | 
			
		||||
            return false;
 | 
			
		||||
        path.chop(9);
 | 
			
		||||
        if (!foo.rename(path))
 | 
			
		||||
 | 
			
		||||
        if (!file.rename(path))
 | 
			
		||||
            return false;
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        QFile foo(path);
 | 
			
		||||
    } else {
 | 
			
		||||
        path += ".disabled";
 | 
			
		||||
        if (!foo.rename(path))
 | 
			
		||||
        
 | 
			
		||||
        if (!file.rename(path))
 | 
			
		||||
            return false;
 | 
			
		||||
    }
 | 
			
		||||
    if(!fromMetadata())
 | 
			
		||||
 | 
			
		||||
    if (!fromMetadata())
 | 
			
		||||
        repath(QFileInfo(path));
 | 
			
		||||
 | 
			
		||||
    m_enabled = value;
 | 
			
		||||
@@ -141,29 +128,25 @@ bool Mod::destroy(QDir& index_dir)
 | 
			
		||||
    return FS::deletePath(m_file.filePath());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
const ModDetails & Mod::details() const
 | 
			
		||||
const ModDetails& Mod::details() const
 | 
			
		||||
{
 | 
			
		||||
    if(!m_localDetails)
 | 
			
		||||
        return invalidDetails;
 | 
			
		||||
    return *m_localDetails;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
QString Mod::version() const
 | 
			
		||||
{
 | 
			
		||||
    return details().version;
 | 
			
		||||
    return m_localDetails ? *m_localDetails : invalidDetails;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString Mod::name() const
 | 
			
		||||
{
 | 
			
		||||
    auto & d = details();
 | 
			
		||||
    if(!d.name.isEmpty()) {
 | 
			
		||||
        return d.name;
 | 
			
		||||
    auto d_name = details().name;
 | 
			
		||||
    if (!d_name.isEmpty()) {
 | 
			
		||||
        return d_name;
 | 
			
		||||
    }
 | 
			
		||||
    return m_name;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString Mod::version() const
 | 
			
		||||
{
 | 
			
		||||
    return details().version;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
QString Mod::homeurl() const
 | 
			
		||||
{
 | 
			
		||||
    return details().homeurl;
 | 
			
		||||
 
 | 
			
		||||
@@ -41,7 +41,7 @@ public:
 | 
			
		||||
 | 
			
		||||
    QFileInfo filename()        const { return m_file; }
 | 
			
		||||
    QDateTime dateTimeChanged() const { return m_changedDateTime; }
 | 
			
		||||
    QString   mmc_id()          const { return m_mmc_id; }
 | 
			
		||||
    QString   internal_id()     const { return m_internal_id; }
 | 
			
		||||
    ModType   type()            const { return m_type; }
 | 
			
		||||
    bool      fromMetadata()    const { return m_from_metadata; }
 | 
			
		||||
    bool      enabled()         const { return m_enabled; }
 | 
			
		||||
@@ -81,7 +81,7 @@ protected:
 | 
			
		||||
    QFileInfo m_file;
 | 
			
		||||
    QDateTime m_changedDateTime;
 | 
			
		||||
 | 
			
		||||
    QString m_mmc_id;
 | 
			
		||||
    QString m_internal_id;
 | 
			
		||||
    QString m_name;
 | 
			
		||||
    ModType m_type = MOD_UNKNOWN;
 | 
			
		||||
    bool m_from_metadata = false;
 | 
			
		||||
 
 | 
			
		||||
@@ -5,13 +5,24 @@
 | 
			
		||||
 | 
			
		||||
struct ModDetails
 | 
			
		||||
{
 | 
			
		||||
    /* Mod ID as defined in the ModLoader-specific metadata */
 | 
			
		||||
    QString mod_id;
 | 
			
		||||
    
 | 
			
		||||
    /* Human-readable name */
 | 
			
		||||
    QString name;
 | 
			
		||||
    
 | 
			
		||||
    /* Human-readable mod version */
 | 
			
		||||
    QString version;
 | 
			
		||||
    
 | 
			
		||||
    /* Human-readable minecraft version */
 | 
			
		||||
    QString mcversion;
 | 
			
		||||
    
 | 
			
		||||
    /* URL for mod's home page */
 | 
			
		||||
    QString homeurl;
 | 
			
		||||
    QString updateurl;
 | 
			
		||||
    
 | 
			
		||||
    /* Human-readable description */
 | 
			
		||||
    QString description;
 | 
			
		||||
 | 
			
		||||
    /* List of the author's names */
 | 
			
		||||
    QStringList authors;
 | 
			
		||||
    QString credits;
 | 
			
		||||
};
 | 
			
		||||
 
 | 
			
		||||
@@ -159,7 +159,7 @@ void ModFolderModel::finishUpdate()
 | 
			
		||||
        modsIndex.clear();
 | 
			
		||||
        int idx = 0;
 | 
			
		||||
        for(auto & mod: mods) {
 | 
			
		||||
            modsIndex[mod.mmc_id()] = idx;
 | 
			
		||||
            modsIndex[mod.internal_id()] = idx;
 | 
			
		||||
            idx++;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -182,7 +182,7 @@ void ModFolderModel::resolveMod(Mod& m)
 | 
			
		||||
 | 
			
		||||
    auto task = new LocalModParseTask(nextResolutionTicket, m.type(), m.filename());
 | 
			
		||||
    auto result = task->result();
 | 
			
		||||
    result->id = m.mmc_id();
 | 
			
		||||
    result->id = m.internal_id();
 | 
			
		||||
    activeTickets.insert(nextResolutionTicket, result);
 | 
			
		||||
    m.setResolving(true, nextResolutionTicket);
 | 
			
		||||
    nextResolutionTicket++;
 | 
			
		||||
@@ -388,7 +388,7 @@ QVariant ModFolderModel::data(const QModelIndex &index, int role) const
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    case Qt::ToolTipRole:
 | 
			
		||||
        return mods[row].mmc_id();
 | 
			
		||||
        return mods[row].internal_id();
 | 
			
		||||
 | 
			
		||||
    case Qt::CheckStateRole:
 | 
			
		||||
        switch (column)
 | 
			
		||||
@@ -443,11 +443,11 @@ bool ModFolderModel::setModStatus(int row, ModFolderModel::ModStatusAction actio
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // preserve the row, but change its ID
 | 
			
		||||
    auto oldId = mod.mmc_id();
 | 
			
		||||
    auto oldId = mod.internal_id();
 | 
			
		||||
    if(!mod.enable(!mod.enabled())) {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    auto newId = mod.mmc_id();
 | 
			
		||||
    auto newId = mod.internal_id();
 | 
			
		||||
    if(modsIndex.contains(newId)) {
 | 
			
		||||
        // NOTE: this could handle a corner case, where we are overwriting a file, because the same 'mod' exists both enabled and disabled
 | 
			
		||||
        // But is it necessary?
 | 
			
		||||
 
 | 
			
		||||
@@ -35,7 +35,6 @@ std::shared_ptr<ModDetails> ReadMCModInfo(QByteArray contents)
 | 
			
		||||
            details->name = name;
 | 
			
		||||
        }
 | 
			
		||||
        details->version = firstObj.value("version").toString();
 | 
			
		||||
        details->updateurl = firstObj.value("updateUrl").toString();
 | 
			
		||||
        auto homeurl = firstObj.value("url").toString().trimmed();
 | 
			
		||||
        if(!homeurl.isEmpty())
 | 
			
		||||
        {
 | 
			
		||||
@@ -57,7 +56,6 @@ std::shared_ptr<ModDetails> ReadMCModInfo(QByteArray contents)
 | 
			
		||||
        {
 | 
			
		||||
            details->authors.append(author.toString());
 | 
			
		||||
        }
 | 
			
		||||
        details->credits = firstObj.value("credits").toString();
 | 
			
		||||
        return details;
 | 
			
		||||
    };
 | 
			
		||||
    QJsonParseError jsonError;
 | 
			
		||||
@@ -168,27 +166,9 @@ std::shared_ptr<ModDetails> ReadMCModTOML(QByteArray contents)
 | 
			
		||||
    }
 | 
			
		||||
    if(!authors.isEmpty())
 | 
			
		||||
    {
 | 
			
		||||
        // author information is stored as a string now, not a list
 | 
			
		||||
        details->authors.append(authors);
 | 
			
		||||
    }
 | 
			
		||||
    // is credits even used anywhere? including this for completion/parity with old data version
 | 
			
		||||
    toml_datum_t creditsDatum = toml_string_in(tomlData, "credits");
 | 
			
		||||
    QString credits = "";
 | 
			
		||||
    if(creditsDatum.ok)
 | 
			
		||||
    {
 | 
			
		||||
        authors = creditsDatum.u.s;
 | 
			
		||||
        free(creditsDatum.u.s);
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        creditsDatum = toml_string_in(tomlModsTable0, "credits");
 | 
			
		||||
        if(creditsDatum.ok)
 | 
			
		||||
        {
 | 
			
		||||
            credits = creditsDatum.u.s;
 | 
			
		||||
            free(creditsDatum.u.s);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    details->credits = credits;
 | 
			
		||||
 | 
			
		||||
    toml_datum_t homeurlDatum = toml_string_in(tomlData, "displayURL");
 | 
			
		||||
    QString homeurl = "";
 | 
			
		||||
    if(homeurlDatum.ok)
 | 
			
		||||
 
 | 
			
		||||
@@ -3,32 +3,30 @@
 | 
			
		||||
 | 
			
		||||
#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())
 | 
			
		||||
{
 | 
			
		||||
}
 | 
			
		||||
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()
 | 
			
		||||
{
 | 
			
		||||
    // Read metadata first
 | 
			
		||||
    m_index_dir.refresh();
 | 
			
		||||
    for(auto entry : m_index_dir.entryList()){
 | 
			
		||||
    for (auto entry : m_index_dir.entryList()) {
 | 
			
		||||
        // QDir::Filter::NoDotAndDotDot seems to exclude all files for some reason...
 | 
			
		||||
        if(entry == "." || entry == "..")
 | 
			
		||||
        if (entry == "." || entry == "..")
 | 
			
		||||
            continue;
 | 
			
		||||
 | 
			
		||||
        entry.chop(5); // Remove .toml at the end
 | 
			
		||||
        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;
 | 
			
		||||
        m_result->mods[mod.internal_id()] = mod;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Read JAR files that don't have metadata
 | 
			
		||||
    m_mods_dir.refresh();
 | 
			
		||||
    for (auto entry : m_mods_dir.entryInfoList())
 | 
			
		||||
    {
 | 
			
		||||
    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;
 | 
			
		||||
        if (!m_result->mods.contains(mod.internal_id()))
 | 
			
		||||
            m_result->mods[mod.internal_id()] = mod;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    emit succeeded();
 | 
			
		||||
 
 | 
			
		||||
@@ -32,7 +32,7 @@ void MCModInfoFrame::updateWithMod(Mod &m)
 | 
			
		||||
    QString text = "";
 | 
			
		||||
    QString name = "";
 | 
			
		||||
    if (m.name().isEmpty())
 | 
			
		||||
        name = m.mmc_id();
 | 
			
		||||
        name = m.internal_id();
 | 
			
		||||
    else
 | 
			
		||||
        name = m.name();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user