refactor: move general code from mod model to its own model
This aims to continue decoupling other types of resources (e.g. resource packs, shader packs, etc) from mods, so that we don't have to continuously watch our backs for changes to one of them affecting the others. To do so, this creates a more general list model for resources, based on the mods one, that allows you to extend it with functionality for other resources. I had to do some template and preprocessor stuff to get around the QObject limitation of not allowing templated classes, so that's sadge :c On the other hand, I tried cleaning up most general-purpose code in the mod model, and added some documentation, because it looks nice :D Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
44
launcher/minecraft/mod/tasks/BasicFolderLoadTask.h
Normal file
44
launcher/minecraft/mod/tasks/BasicFolderLoadTask.h
Normal file
@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <QDir>
|
||||
#include <QMap>
|
||||
#include <QObject>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "minecraft/mod/Resource.h"
|
||||
|
||||
#include "tasks/Task.h"
|
||||
|
||||
/** Very simple task that just loads a folder's contents directly.
|
||||
*/
|
||||
class BasicFolderLoadTask : public Task
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
struct Result {
|
||||
QMap<QString, Resource::Ptr> resources;
|
||||
};
|
||||
using ResultPtr = std::shared_ptr<Result>;
|
||||
|
||||
[[nodiscard]] ResultPtr result() const {
|
||||
return m_result;
|
||||
}
|
||||
|
||||
public:
|
||||
BasicFolderLoadTask(QDir dir) : Task(nullptr, false), m_dir(dir), m_result(new Result) {}
|
||||
void executeTask() override
|
||||
{
|
||||
m_dir.refresh();
|
||||
for (auto entry : m_dir.entryInfoList()) {
|
||||
auto resource = new Resource(entry);
|
||||
m_result->resources.insert(resource->internal_id(), resource);
|
||||
}
|
||||
|
||||
emitSucceeded();
|
||||
}
|
||||
|
||||
private:
|
||||
QDir m_dir;
|
||||
ResultPtr m_result;
|
||||
};
|
@ -338,13 +338,13 @@ std::shared_ptr<ModDetails> ReadLiteModInfo(QByteArray contents)
|
||||
|
||||
}
|
||||
|
||||
LocalModParseTask::LocalModParseTask(int token, Mod::ModType type, const QFileInfo& modFile):
|
||||
LocalModParseTask::LocalModParseTask(int token, ResourceType type, const QFileInfo& modFile):
|
||||
Task(nullptr, false),
|
||||
m_token(token),
|
||||
m_type(type),
|
||||
m_modFile(modFile),
|
||||
m_result(new Result())
|
||||
{
|
||||
}
|
||||
{}
|
||||
|
||||
void LocalModParseTask::processAsZip()
|
||||
{
|
||||
@ -497,21 +497,21 @@ void LocalModParseTask::processAsLitemod()
|
||||
zip.close();
|
||||
}
|
||||
|
||||
void LocalModParseTask::run()
|
||||
void LocalModParseTask::executeTask()
|
||||
{
|
||||
switch(m_type)
|
||||
{
|
||||
case Mod::MOD_ZIPFILE:
|
||||
case ResourceType::ZIPFILE:
|
||||
processAsZip();
|
||||
break;
|
||||
case Mod::MOD_FOLDER:
|
||||
case ResourceType::FOLDER:
|
||||
processAsFolder();
|
||||
break;
|
||||
case Mod::MOD_LITEMOD:
|
||||
case ResourceType::LITEMOD:
|
||||
processAsLitemod();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
emit finished(m_token);
|
||||
emitSucceeded();
|
||||
}
|
||||
|
@ -2,17 +2,17 @@
|
||||
|
||||
#include <QDebug>
|
||||
#include <QObject>
|
||||
#include <QRunnable>
|
||||
|
||||
#include "minecraft/mod/Mod.h"
|
||||
#include "minecraft/mod/ModDetails.h"
|
||||
|
||||
class LocalModParseTask : public QObject, public QRunnable
|
||||
#include "tasks/Task.h"
|
||||
|
||||
class LocalModParseTask : public Task
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
struct Result {
|
||||
QString id;
|
||||
std::shared_ptr<ModDetails> details;
|
||||
};
|
||||
using ResultPtr = std::shared_ptr<Result>;
|
||||
@ -20,11 +20,10 @@ public:
|
||||
return m_result;
|
||||
}
|
||||
|
||||
LocalModParseTask(int token, Mod::ModType type, const QFileInfo & modFile);
|
||||
void run();
|
||||
LocalModParseTask(int token, ResourceType type, const QFileInfo & modFile);
|
||||
void executeTask() override;
|
||||
|
||||
signals:
|
||||
void finished(int token);
|
||||
[[nodiscard]] int token() const { return m_token; }
|
||||
|
||||
private:
|
||||
void processAsZip();
|
||||
@ -33,7 +32,7 @@ private:
|
||||
|
||||
private:
|
||||
int m_token;
|
||||
Mod::ModType m_type;
|
||||
ResourceType m_type;
|
||||
QFileInfo m_modFile;
|
||||
ResultPtr m_result;
|
||||
};
|
||||
|
@ -38,11 +38,11 @@
|
||||
|
||||
#include "minecraft/mod/MetadataHandler.h"
|
||||
|
||||
ModFolderLoadTask::ModFolderLoadTask(QDir& mods_dir, QDir& index_dir, bool is_indexed, bool clean_orphan)
|
||||
: m_mods_dir(mods_dir), m_index_dir(index_dir), m_is_indexed(is_indexed), m_clean_orphan(clean_orphan), m_result(new Result())
|
||||
ModFolderLoadTask::ModFolderLoadTask(QDir mods_dir, QDir index_dir, bool is_indexed, bool clean_orphan)
|
||||
: Task(nullptr, false), m_mods_dir(mods_dir), m_index_dir(index_dir), m_is_indexed(is_indexed), m_clean_orphan(clean_orphan), m_result(new Result())
|
||||
{}
|
||||
|
||||
void ModFolderLoadTask::run()
|
||||
void ModFolderLoadTask::executeTask()
|
||||
{
|
||||
if (m_is_indexed) {
|
||||
// Read metadata first
|
||||
@ -96,7 +96,7 @@ void ModFolderLoadTask::run()
|
||||
}
|
||||
}
|
||||
|
||||
emit succeeded();
|
||||
emitSucceeded();
|
||||
}
|
||||
|
||||
void ModFolderLoadTask::getFromMetadata()
|
||||
|
@ -42,8 +42,9 @@
|
||||
#include <QRunnable>
|
||||
#include <memory>
|
||||
#include "minecraft/mod/Mod.h"
|
||||
#include "tasks/Task.h"
|
||||
|
||||
class ModFolderLoadTask : public QObject, public QRunnable
|
||||
class ModFolderLoadTask : public Task
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
@ -56,16 +57,15 @@ public:
|
||||
}
|
||||
|
||||
public:
|
||||
ModFolderLoadTask(QDir& mods_dir, QDir& index_dir, bool is_indexed, bool clean_orphan = false);
|
||||
void run();
|
||||
signals:
|
||||
void succeeded();
|
||||
ModFolderLoadTask(QDir mods_dir, QDir index_dir, bool is_indexed, bool clean_orphan = false);
|
||||
|
||||
void executeTask() override;
|
||||
|
||||
private:
|
||||
void getFromMetadata();
|
||||
|
||||
private:
|
||||
QDir& m_mods_dir, m_index_dir;
|
||||
QDir m_mods_dir, m_index_dir;
|
||||
bool m_is_indexed;
|
||||
bool m_clean_orphan;
|
||||
ResultPtr m_result;
|
||||
|
Reference in New Issue
Block a user