feat: remove existing mod when updating/redownloading it

Signed-off-by: flow <flowlnlnln@gmail.com>
This commit is contained in:
flow 2022-06-10 16:43:01 -03:00
parent 1709b47bb7
commit dfab55112b
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
7 changed files with 44 additions and 3 deletions

View File

@ -27,6 +27,7 @@ ModDownloadTask::ModDownloadTask(ModPlatform::IndexedPack mod, ModPlatform::Inde
{ {
if (is_indexed) { if (is_indexed) {
m_update_task.reset(new LocalModUpdateTask(mods->indexDir(), m_mod, m_mod_version)); m_update_task.reset(new LocalModUpdateTask(mods->indexDir(), m_mod, m_mod_version));
connect(m_update_task.get(), &LocalModUpdateTask::hasOldMod, this, &ModDownloadTask::hasOldMod);
addTask(m_update_task); addTask(m_update_task);
} }
@ -40,12 +41,13 @@ ModDownloadTask::ModDownloadTask(ModPlatform::IndexedPack mod, ModPlatform::Inde
connect(m_filesNetJob.get(), &NetJob::failed, this, &ModDownloadTask::downloadFailed); connect(m_filesNetJob.get(), &NetJob::failed, this, &ModDownloadTask::downloadFailed);
addTask(m_filesNetJob); addTask(m_filesNetJob);
} }
void ModDownloadTask::downloadSucceeded() void ModDownloadTask::downloadSucceeded()
{ {
m_filesNetJob.reset(); m_filesNetJob.reset();
if (!std::get<0>(to_delete).isEmpty())
mods->uninstallMod(std::get<1>(to_delete), true);
} }
void ModDownloadTask::downloadFailed(QString reason) void ModDownloadTask::downloadFailed(QString reason)
@ -58,3 +60,10 @@ void ModDownloadTask::downloadProgressChanged(qint64 current, qint64 total)
{ {
emit progress(current, total); emit progress(current, total);
} }
// This indirection is done so that we don't delete a mod before being sure it was
// downloaded successfully!
void ModDownloadTask::hasOldMod(QString name, QString filename)
{
to_delete = {name, filename};
}

View File

@ -46,6 +46,11 @@ private:
void downloadFailed(QString reason); void downloadFailed(QString reason);
void downloadSucceeded(); void downloadSucceeded();
std::tuple<QString, QString> to_delete {"", ""};
private slots:
void hasOldMod(QString name, QString filename);
}; };

View File

@ -183,9 +183,12 @@ auto Mod::details() const -> const ModDetails&
auto Mod::name() const -> QString auto Mod::name() const -> QString
{ {
auto d_name = details().name; auto d_name = details().name;
if (!d_name.isEmpty()) { if (!d_name.isEmpty())
return d_name; return d_name;
}
if (status() != ModStatus::NoMetadata)
return metadata()->name;
return m_name; return m_name;
} }

View File

@ -356,6 +356,20 @@ bool ModFolderModel::installMod(const QString &filename)
return false; return false;
} }
bool ModFolderModel::uninstallMod(const QString& filename, bool preserve_metadata)
{
for(auto mod : allMods()){
if(mod.fileinfo().fileName() == filename){
auto index_dir = indexDir();
mod.destroy(index_dir, preserve_metadata);
return true;
}
}
return false;
}
bool ModFolderModel::setModStatus(const QModelIndexList& indexes, ModStatusAction enable) bool ModFolderModel::setModStatus(const QModelIndexList& indexes, ModStatusAction enable)
{ {
if(interaction_disabled) { if(interaction_disabled) {

View File

@ -118,6 +118,8 @@ public:
*/ */
bool installMod(const QString& filename); bool installMod(const QString& filename);
bool uninstallMod(const QString& filename, bool preserve_metadata = false);
/// Deletes all the selected mods /// Deletes all the selected mods
bool deleteMods(const QModelIndexList &indexes); bool deleteMods(const QModelIndexList &indexes);

View File

@ -44,6 +44,11 @@ void LocalModUpdateTask::executeTask()
{ {
setStatus(tr("Updating index for mod:\n%1").arg(m_mod.name)); setStatus(tr("Updating index for mod:\n%1").arg(m_mod.name));
auto old_metadata = Metadata::get(m_index_dir, m_mod.name);
if (old_metadata.isValid()) {
emit hasOldMod(old_metadata.name, old_metadata.filename);
}
auto pw_mod = Metadata::create(m_index_dir, m_mod, m_mod_version); auto pw_mod = Metadata::create(m_index_dir, m_mod, m_mod_version);
Metadata::update(m_index_dir, pw_mod); Metadata::update(m_index_dir, pw_mod);

View File

@ -37,6 +37,9 @@ class LocalModUpdateTask : public Task {
//! Entry point for tasks. //! Entry point for tasks.
void executeTask() override; void executeTask() override;
signals:
void hasOldMod(QString name, QString filename);
private: private:
QDir m_index_dir; QDir m_index_dir;
ModPlatform::IndexedPack& m_mod; ModPlatform::IndexedPack& m_mod;