From bf80bd1143a19476502a3fa510f7fb72cfa8fbd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 13 Jun 2022 23:41:47 +0200 Subject: [PATCH] NOISSUE implement handling of client-overrides for Modrinth --- launcher/InstanceImportTask.cpp | 61 +++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 3 deletions(-) diff --git a/launcher/InstanceImportTask.cpp b/launcher/InstanceImportTask.cpp index b65b9ff1..1cdcb4a6 100644 --- a/launcher/InstanceImportTask.cpp +++ b/launcher/InstanceImportTask.cpp @@ -250,6 +250,47 @@ void InstanceImportTask::processMultiMC() emitSucceeded(); } +namespace { +bool mergeOverrides(const QString &fromDir, const QString &toDir) { + QDir dir(fromDir); + if(!dir.exists()) { + return true; + } + if(!FS::ensureFolderPathExists(toDir)) { + return false; + } + const int absSourcePathLength = dir.absoluteFilePath(fromDir).length(); + + QDirIterator it(fromDir, QDirIterator::Subdirectories); + while (it.hasNext()){ + it.next(); + const auto fileInfo = it.fileInfo(); + auto fileName = fileInfo.fileName(); + if(fileName == "." || fileName == "..") { + continue; + } + const QString subPathStructure = fileInfo.absoluteFilePath().mid(absSourcePathLength); + const QString constructedAbsolutePath = toDir + subPathStructure; + + if(fileInfo.isDir()){ + //Create directory in target folder + dir.mkpath(constructedAbsolutePath); + } else if(fileInfo.isFile()) { + QFileInfo targetFileInfo(constructedAbsolutePath); + if(targetFileInfo.exists()) { + continue; + } + // move + QFile::rename(fileInfo.absoluteFilePath(), constructedAbsolutePath); + } + } + + dir.removeRecursively(); + return true; +} + +} + void InstanceImportTask::processModrinth() { std::vector files; QString minecraftVersion, fabricVersion, quiltVersion, forgeVersion; @@ -361,15 +402,29 @@ void InstanceImportTask::processModrinth() { emitFailed(tr("Could not understand pack index:\n") + e.cause()); return; } - QString overridePath = FS::PathCombine(m_stagingPath, "overrides"); - if (QFile::exists(overridePath)) { + QString clientOverridePath = FS::PathCombine(m_stagingPath, "client-overrides"); + if (QFile::exists(clientOverridePath)) { QString mcPath = FS::PathCombine(m_stagingPath, ".minecraft"); - if (!QFile::rename(overridePath, mcPath)) { + if (!QFile::rename(clientOverridePath, mcPath)) { emitFailed(tr("Could not rename the overrides folder:\n") + "overrides"); return; } } + // TODO: only extract things we actually want instead of everything only to just delete it afterwards ... + if(!mergeOverrides(FS::PathCombine(m_stagingPath, "client-overrides"), FS::PathCombine(m_stagingPath, ".minecraft"))) { + emitFailed(tr("Could not merge the overrides folder:\n") + "client-overrides"); + return; + } + + if(!mergeOverrides(FS::PathCombine(m_stagingPath, "overrides"), FS::PathCombine(m_stagingPath, ".minecraft"))) { + emitFailed(tr("Could not merge the overrides folder:\n") + "overrides"); + return; + } + + FS::deletePath(FS::PathCombine(m_stagingPath, "server-overrides")); + + QString configPath = FS::PathCombine(m_stagingPath, "instance.cfg"); auto instanceSettings = std::make_shared(configPath); instanceSettings->registerSetting("InstanceType", "Legacy");