Skip to content

Commit

Permalink
add optional event based api to fetch loaded packs
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Dec 27, 2024
1 parent dc4753e commit 578d855
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 2 deletions.
46 changes: 46 additions & 0 deletions include/TextureLoader.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <Geode/loader/Dispatch.hpp>

#include <filesystem>
#include <vector>
#include <string>

namespace geode::texture_loader {

inline bool isLoaded() {
return geode::Loader::get()->isModLoaded("geode.texture-loader");
}

struct Pack {
std::string id;
std::string name;
VersionInfo version;
std::vector<std::string> authors;

/// Path from where the pack originates from. May be a file (apk, zip) or a folder
std::filesystem::path path;
/// Path where the resources are located. This is what is added to the search path
std::filesystem::path resourcesPath;
};

namespace impl {
using EventGetAvailablePacks = geode::DispatchEvent<std::vector<Pack>*>;
using EventGetAppliedPacks = geode::DispatchEvent<std::vector<Pack>*>;
}

inline std::vector<Pack> getAvailablePacks() {
std::vector<Pack> result;
log::debug("sending event!");
impl::EventGetAvailablePacks("geode.texture-loader/v1/get-available-packs", &result).post();
log::debug("ok sent event!");
return result;
}

inline std::vector<Pack> getAppliedPacks() {
std::vector<Pack> result;
impl::EventGetAppliedPacks("geode.texture-loader/v1/get-applied-packs", &result).post();
return result;
}

}
7 changes: 5 additions & 2 deletions mod.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"geode": "4.0.1",
"version": "v1.6.7",
"geode": "4.1.1",
"version": "v1.7.0",
"gd": {
"win": "2.2074",
"android": "2.2074",
Expand All @@ -18,5 +18,8 @@
},
"links": {
"source": "https://github.com/geode-sdk/textureldr"
},
"api": {
"include": ["include/*.hpp"]
}
}
46 changes: 46 additions & 0 deletions src/API.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "../include/TextureLoader.hpp"

#include "PackManager.hpp"

using namespace geode;

namespace ext = geode::texture_loader;

template <class>
struct ToFilterImpl;

template <class... Args>
struct ToFilterImpl<geode::DispatchEvent<Args...>> {
using type = geode::DispatchFilter<Args...>;
};

template <class T>
using ToFilter = typename ToFilterImpl<T>::type;

ext::Pack convertPack(std::shared_ptr<Pack> const& pack) {
ext::Pack res;
res.id = pack->getID();
res.name = pack->getDisplayName();
res.path = pack->getOriginPath();
res.resourcesPath = pack->getResourcesPath();
// if the pack has a mod.json
if (auto opt = pack->getInfo()) {
auto info = opt.value();
res.version = info.m_version;
res.authors = info.m_authors;
}
return res;
}

$execute {
new EventListener(+[](std::vector<ext::Pack>* res) {
log::debug("received event!");
*res = utils::ranges::map<std::vector<ext::Pack>>(PackManager::get()->getAvailablePacks(), convertPack);
return ListenerResult::Stop;
}, ToFilter<ext::impl::EventGetAvailablePacks>("geode.texture-loader/v1/get-available-packs"));

new EventListener(+[](std::vector<ext::Pack>* res) {
*res = utils::ranges::map<std::vector<ext::Pack>>(PackManager::get()->getAppliedPacks(), convertPack);
return ListenerResult::Stop;
}, ToFilter<ext::impl::EventGetAppliedPacks>("geode.texture-loader/v1/get-applied-packs"));
}
4 changes: 4 additions & 0 deletions src/Pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ std::string Pack::getDisplayName() const {
m_path.filename().string();
}

std::optional<PackInfo> Pack::getInfo() const {
return m_info;
}

Result<> Pack::apply() {
CCFileUtils::get()->addTexturePack(CCTexturePack {
.m_id = this->getID(),
Expand Down
1 change: 1 addition & 0 deletions src/Pack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Pack {
[[nodiscard]] std::filesystem::path getResourcesPath() const;
[[nodiscard]] std::string getDisplayName() const;
[[nodiscard]] std::string getID() const;
[[nodiscard]] std::optional<PackInfo> getInfo() const;

[[nodiscard]] Result<> apply();
[[nodiscard]] Result<> unapply() const;
Expand Down

0 comments on commit 578d855

Please sign in to comment.