From b0c66ae69b08ceb2469f4c6842fdc3d70f97cdc4 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Fri, 13 Dec 2024 17:24:48 +0000 Subject: [PATCH] Completely erase old cache on mod repo checkout This allows to remove any outdated data that might be present: - mods removed from repository - mods that came from no longer used repository - fields that were removed from mod.json's --- launcher/modManager/cmodlistview_moc.cpp | 17 +++++++++++------ launcher/modManager/cmodlistview_moc.h | 1 + launcher/modManager/modstatecontroller.cpp | 4 ++-- launcher/modManager/modstatecontroller.h | 2 +- launcher/modManager/modstatemodel.cpp | 6 ++---- launcher/modManager/modstatemodel.h | 2 +- 6 files changed, 18 insertions(+), 14 deletions(-) diff --git a/launcher/modManager/cmodlistview_moc.cpp b/launcher/modManager/cmodlistview_moc.cpp index b5c222088..79d774b22 100644 --- a/launcher/modManager/cmodlistview_moc.cpp +++ b/launcher/modManager/cmodlistview_moc.cpp @@ -43,7 +43,7 @@ void CModListView::setupModModel() modStateModel = std::make_shared(); if (!cachedRepositoryData.isNull()) - modStateModel->appendRepositories(cachedRepositoryData); + modStateModel->setRepositoryData(cachedRepositoryData); modModel = new ModStateItemModel(modStateModel, this); manager = std::make_unique(modStateModel); @@ -151,6 +151,8 @@ void CModListView::reload() void CModListView::loadRepositories() { + accumulatedRepositoryData.clear(); + QStringList repositories; if (settings["launcher"]["defaultRepositoryEnabled"].Bool()) @@ -731,7 +733,7 @@ void CModListView::installFiles(QStringList files) QStringList maps; QStringList images; QStringList exe; - JsonNode repository; + bool repositoryFilesEnqueued = false; // TODO: some better way to separate zip's with mods and downloaded repository files for(QString filename : files) @@ -754,9 +756,12 @@ void CModListView::installFiles(QStringList files) auto modNameLower = boost::algorithm::to_lower_copy(modName); auto modJsonUrl = modJson["mod"]; if(!modJsonUrl.isNull()) + { downloadFile(QString::fromStdString(modName + ".json"), QString::fromStdString(modJsonUrl.String()), tr("mods repository index")); + repositoryFilesEnqueued = true; + } - repository[modNameLower] = modJson; + accumulatedRepositoryData[modNameLower] = modJson; } } else @@ -764,16 +769,16 @@ void CModListView::installFiles(QStringList files) // This is json of a single mod. Extract name of mod and add it to repo auto modName = QFileInfo(filename).baseName().toStdString(); auto modNameLower = boost::algorithm::to_lower_copy(modName); - repository[modNameLower] = repoData; + accumulatedRepositoryData[modNameLower] = repoData; } } else if(filename.endsWith(".png", Qt::CaseInsensitive)) images.push_back(filename); } - if (!repository.isNull()) + if (!accumulatedRepositoryData.isNull() && !repositoryFilesEnqueued) { - manager->appendRepositories(repository); + manager->setRepositoryData(accumulatedRepositoryData); modModel->reloadRepositories(); static const QString repositoryCachePath = CLauncherDirs::downloadsPath() + "/repositoryCache.json"; diff --git a/launcher/modManager/cmodlistview_moc.h b/launcher/modManager/cmodlistview_moc.h index 8c2edd40a..6dbd198ce 100644 --- a/launcher/modManager/cmodlistview_moc.h +++ b/launcher/modManager/cmodlistview_moc.h @@ -36,6 +36,7 @@ class CModListView : public QWidget ModStateItemModel * modModel; CModFilterModel * filterModel; CDownloadManager * dlManager; + JsonNode accumulatedRepositoryData; QStringList enqueuedModDownloads; diff --git a/launcher/modManager/modstatecontroller.cpp b/launcher/modManager/modstatecontroller.cpp index bbfb2e75b..718f9841a 100644 --- a/launcher/modManager/modstatecontroller.cpp +++ b/launcher/modManager/modstatecontroller.cpp @@ -72,9 +72,9 @@ ModStateController::ModStateController(std::shared_ptr modList) ModStateController::~ModStateController() = default; -void ModStateController::appendRepositories(const JsonNode & repomap) +void ModStateController::setRepositoryData(const JsonNode & repomap) { - modList->appendRepositories(repomap); + modList->setRepositoryData(repomap); } bool ModStateController::addError(QString modname, QString message) diff --git a/launcher/modManager/modstatecontroller.h b/launcher/modManager/modstatecontroller.h index 57cfc53e6..9ee087feb 100644 --- a/launcher/modManager/modstatecontroller.h +++ b/launcher/modManager/modstatecontroller.h @@ -37,7 +37,7 @@ public: ModStateController(std::shared_ptr modList); ~ModStateController(); - void appendRepositories(const JsonNode & repositoriesList); + void setRepositoryData(const JsonNode & repositoriesList); QStringList getErrors(); diff --git a/launcher/modManager/modstatemodel.cpp b/launcher/modManager/modstatemodel.cpp index 9bcfa2863..aaaa037ce 100644 --- a/launcher/modManager/modstatemodel.cpp +++ b/launcher/modManager/modstatemodel.cpp @@ -11,7 +11,6 @@ #include "modstatemodel.h" #include "../../lib/filesystem/Filesystem.h" -#include "../../lib/json/JsonUtils.h" #include "../../lib/modding/ModManager.h" ModStateModel::ModStateModel() @@ -22,10 +21,9 @@ ModStateModel::ModStateModel() ModStateModel::~ModStateModel() = default; -void ModStateModel::appendRepositories(const JsonNode & repositoriesList) +void ModStateModel::setRepositoryData(const JsonNode & repositoriesList) { - JsonUtils::mergeCopy(*repositoryData, repositoriesList); - + *repositoryData = repositoriesList; modManager = std::make_unique(*repositoryData); } diff --git a/launcher/modManager/modstatemodel.h b/launcher/modManager/modstatemodel.h index 168da5dfe..e15433dc7 100644 --- a/launcher/modManager/modstatemodel.h +++ b/launcher/modManager/modstatemodel.h @@ -27,7 +27,7 @@ public: ModStateModel(); ~ModStateModel(); - void appendRepositories(const JsonNode & repositoriesList); + void setRepositoryData(const JsonNode & repositoriesList); void reloadLocalState(); const JsonNode & getRepositoryData() const;