mark files as duplicate

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2024-06-14 22:45:15 +03:00
parent 2d51008304
commit d2510b851b
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
5 changed files with 49 additions and 16 deletions

View File

@ -1701,4 +1701,30 @@ QString getPathNameInLocal8bit(const QString& file)
} }
#endif #endif
QString getUniqueResourceName(const QString& filePath)
{
auto newFileName = filePath;
if (!newFileName.endsWith(".disabled")) {
return newFileName; // prioritize enabled mods
}
newFileName.chop(9);
if (!QFile::exists(newFileName)) {
return filePath;
}
QFileInfo fileInfo(filePath);
auto baseName = fileInfo.completeBaseName();
auto path = fileInfo.absolutePath();
int counter = 1;
do {
if (counter == 1) {
newFileName = FS::PathCombine(path, baseName + ".duplicate");
} else {
newFileName = FS::PathCombine(path, baseName + ".duplicate" + QString::number(counter));
}
counter++;
} while (QFile::exists(newFileName));
return newFileName;
}
} // namespace FS } // namespace FS

View File

@ -560,4 +560,6 @@ uintmax_t hardLinkCount(const QString& path);
QString getPathNameInLocal8bit(const QString& file); QString getPathNameInLocal8bit(const QString& file);
#endif #endif
QString getUniqueResourceName(const QString& filePath);
} // namespace FS } // namespace FS

View File

@ -161,10 +161,13 @@ bool Resource::enable(EnableAction action)
path.chop(9); path.chop(9);
} else { } else {
path += ".disabled"; path += ".disabled";
auto newFilePath = FS::getUniqueResourceName(path);
if (newFilePath != path) {
FS::move(path, newFilePath);
}
} }
if (QFileInfo::exists(path)) { // the path exists so just remove the file at path if (QFileInfo::exists(path)) { // the path exists so just remove the file at path
if (!QFile::remove(path)) return false;
return false;
} }
if (!file.rename(path)) if (!file.rename(path))
return false; return false;

View File

@ -7,6 +7,7 @@
#include <memory> #include <memory>
#include "FileSystem.h"
#include "minecraft/mod/Resource.h" #include "minecraft/mod/Resource.h"
#include "tasks/Task.h" #include "tasks/Task.h"
@ -49,16 +50,16 @@ class BasicFolderLoadTask : public Task {
connect(this, &Task::finished, this->thread(), &QThread::quit); connect(this, &Task::finished, this->thread(), &QThread::quit);
m_dir.refresh(); m_dir.refresh();
QStringList names;
for (auto entry : m_dir.entryInfoList()) { for (auto entry : m_dir.entryInfoList()) {
auto resource = m_create_func(entry); auto filePath = entry.absoluteFilePath();
if (names.contains(resource->name())) { auto newFilePath = FS::getUniqueResourceName(filePath);
resource->destroy(); if (newFilePath != filePath) {
} else { FS::move(filePath, newFilePath);
names << resource->name(); entry = QFileInfo(newFilePath);
resource->moveToThread(m_thread_to_spawn_into);
m_result->resources.insert(resource->internal_id(), resource);
} }
auto resource = m_create_func(entry);
resource->moveToThread(m_thread_to_spawn_into);
m_result->resources.insert(resource->internal_id(), resource);
} }
if (m_aborted) if (m_aborted)

View File

@ -36,6 +36,7 @@
#include "ModFolderLoadTask.h" #include "ModFolderLoadTask.h"
#include "FileSystem.h"
#include "minecraft/mod/MetadataHandler.h" #include "minecraft/mod/MetadataHandler.h"
#include <QThread> #include <QThread>
@ -62,15 +63,15 @@ void ModFolderLoadTask::executeTask()
// Read JAR files that don't have metadata // Read JAR files that don't have metadata
m_mods_dir.refresh(); m_mods_dir.refresh();
QStringList names;
for (auto entry : m_mods_dir.entryInfoList()) { for (auto entry : m_mods_dir.entryInfoList()) {
auto filePath = entry.absoluteFilePath();
auto newFilePath = FS::getUniqueResourceName(filePath);
if (newFilePath != filePath) {
FS::move(filePath, newFilePath);
entry = QFileInfo(newFilePath);
}
Mod* mod(new Mod(entry)); Mod* mod(new Mod(entry));
if (names.contains(mod->name())) {
mod->destroy(m_index_dir, true);
continue;
}
names << mod->name();
if (mod->enabled()) { if (mod->enabled()) {
if (m_result->mods.contains(mod->internal_id())) { if (m_result->mods.contains(mod->internal_id())) {
m_result->mods[mod->internal_id()]->setStatus(ModStatus::Installed); m_result->mods[mod->internal_id()]->setStatus(ModStatus::Installed);