From 63027a7f8451c5082d829f53966bd02afda7b5a1 Mon Sep 17 00:00:00 2001 From: altalk23 <45172705+altalk23@users.noreply.github.com> Date: Mon, 4 Sep 2023 23:45:04 +0300 Subject: [PATCH] Fix mod logo and special files of index entries --- loader/include/Geode/loader/Index.hpp | 10 ++++++++ loader/include/Geode/loader/ModMetadata.hpp | 1 + loader/src/loader/Index.cpp | 26 +++++++++++++-------- loader/src/ui/internal/GeodeUI.cpp | 2 +- 4 files changed, 28 insertions(+), 11 deletions(-) diff --git a/loader/include/Geode/loader/Index.hpp b/loader/include/Geode/loader/Index.hpp index d3db061a..5d48430f 100644 --- a/loader/include/Geode/loader/Index.hpp +++ b/loader/include/Geode/loader/Index.hpp @@ -107,6 +107,14 @@ namespace geode { std::unique_ptr m_impl; public: + /** + * Returns the path that contains all the versions + */ + ghc::filesystem::path getRootPath() const; + + /** + * Returns the path to the specific version + */ ghc::filesystem::path getPath() const; [[deprecated("use getMetadata instead")]] ModInfo getModInfo() const; ModMetadata getMetadata() const; @@ -128,6 +136,8 @@ namespace geode { IndexItem(); ~IndexItem(); + + friend class ModMetadata; }; using IndexItemHandle = std::shared_ptr; diff --git a/loader/include/Geode/loader/ModMetadata.hpp b/loader/include/Geode/loader/ModMetadata.hpp index 0647d98a..f13f2788 100644 --- a/loader/include/Geode/loader/ModMetadata.hpp +++ b/loader/include/Geode/loader/ModMetadata.hpp @@ -240,6 +240,7 @@ namespace geode { friend class Loader; friend class ModMetadataImpl; + friend class IndexItem; }; } diff --git a/loader/src/loader/Index.cpp b/loader/src/loader/Index.cpp index 67bdc516..a8e7ea9b 100644 --- a/loader/src/loader/Index.cpp +++ b/loader/src/loader/Index.cpp @@ -47,6 +47,7 @@ IndexUpdateFilter::IndexUpdateFilter() {} class IndexItem::Impl final { private: + ghc::filesystem::path m_rootPath; ghc::filesystem::path m_path; ModMetadata m_metadata; std::string m_downloadURL; @@ -62,6 +63,7 @@ public: * Create IndexItem from a directory */ static Result> create( + ghc::filesystem::path const& rootDir, ghc::filesystem::path const& dir ); @@ -71,6 +73,10 @@ public: IndexItem::IndexItem() : m_impl(std::make_unique()) {} IndexItem::~IndexItem() = default; +ghc::filesystem::path IndexItem::getRootPath() const { + return m_impl->m_rootPath; +} + ghc::filesystem::path IndexItem::getPath() const { return m_impl->m_path; } @@ -133,7 +139,7 @@ void IndexItem::setTags(std::unordered_set const& value) { } #endif -Result IndexItem::Impl::create(ghc::filesystem::path const& dir) { +Result IndexItem::Impl::create(ghc::filesystem::path const& rootDir, ghc::filesystem::path const& dir) { GEODE_UNWRAP_INTO( auto entry, file::readJson(dir / "entry.json") .expect("Unable to read entry.json") @@ -142,6 +148,10 @@ Result IndexItem::Impl::create(ghc::filesystem::path const& dir auto metadata, ModMetadata::createFromFile(dir / "mod.json") .expect("Unable to read mod.json: {error}") ); + auto metadataRes = metadata.addSpecialFiles(rootDir); + if (!metadataRes) { + log::warn("Unable to add special files from {}: {}", rootDir, metadataRes.unwrapErr()); + } JsonChecker checker(entry); auto root = checker.root("[entry.json]").obj(); @@ -157,6 +167,7 @@ Result IndexItem::Impl::create(ghc::filesystem::path const& dir } auto item = std::make_shared(); + item->m_impl->m_rootPath = rootDir; item->m_impl->m_path = dir; item->m_impl->m_metadata = metadata; item->m_impl->m_platforms = platforms; @@ -384,10 +395,10 @@ void Index::Impl::updateFromLocalTree() { for (auto& [modID, entry] : root.has("entries").items()) { for (auto& version : entry.obj().has("versions").iterate()) { - log::debug("Adding index item for {} {}", modID, version.get()); - auto dir = entriesRoot / modID / version.get(); + auto rootDir = entriesRoot / modID; + auto dir = rootDir / version.get(); - auto addRes = IndexItem::Impl::create(dir); + auto addRes = IndexItem::Impl::create(rootDir, dir); if (!addRes) { log::warn("Unable to add index item from {}: {}", dir, addRes.unwrapErr()); continue; @@ -487,19 +498,14 @@ IndexItemHandle Index::getItem( if (m_impl->m_items.count(id)) { auto versions = m_impl->m_items.at(id); if (version) { - // prefer most major version for (auto& [_, item] : ranges::reverse(m_impl->m_items.at(id))) { if (version.value() == item->getMetadata().getVersion()) { return item; } } - } else { - if (!versions.empty()) { - return m_impl->m_items.at(id).rbegin()->second; - } } } - return nullptr; + return this->getMajorItem(id); } IndexItemHandle Index::getItem( diff --git a/loader/src/ui/internal/GeodeUI.cpp b/loader/src/ui/internal/GeodeUI.cpp index b7e1d3e9..a575a737 100644 --- a/loader/src/ui/internal/GeodeUI.cpp +++ b/loader/src/ui/internal/GeodeUI.cpp @@ -83,7 +83,7 @@ CCNode* geode::createModLogo(Mod* mod, CCSize const& size) { } CCNode* geode::createIndexItemLogo(IndexItemHandle item, CCSize const& size) { - auto logoPath = ghc::filesystem::absolute(item->getPath() / "logo.png"); + auto logoPath = ghc::filesystem::absolute(item->getRootPath() / "logo.png"); CCNode* spr = CCSprite::create(logoPath.string().c_str()); if (!spr) { spr = CCSprite::createWithSpriteFrameName("no-logo.png"_spr);