diff --git a/src-server/ll/api/service/PlayerInfo.cpp b/src-server/ll/api/service/PlayerInfo.cpp index 6a518f2413..5a75256aef 100644 --- a/src-server/ll/api/service/PlayerInfo.cpp +++ b/src-server/ll/api/service/PlayerInfo.cpp @@ -38,12 +38,11 @@ class PlayerInfo::Impl { } Impl() { std::lock_guard lock(mutex); - storage.iter([this](std::string_view key, std::string_view value) { + for (auto&& [key, value] : storage.iter()) { auto uuid = mce::UUID(key); auto nbt = CompoundTag::fromBinaryNbt(value).value(); addPlayer(uuid, nbt["xuid"], nbt["name"]); - return true; - }); + } listener = event::EventBus::getInstance().emplaceListener([this](event::PlayerJoinEvent& ev) { if (ev.self().isSimulated()) { diff --git a/src/ll/api/data/KeyValueDB.cpp b/src/ll/api/data/KeyValueDB.cpp index dc9b3535ea..40c2819605 100644 --- a/src/ll/api/data/KeyValueDB.cpp +++ b/src/ll/api/data/KeyValueDB.cpp @@ -16,8 +16,7 @@ #include "ll/api/utils/StringUtils.h" -using namespace ll::data; - +namespace ll::data { class KeyValueDB::KeyValueDBImpl { public: std::unique_ptr db; @@ -108,3 +107,15 @@ void KeyValueDB::iter(std::function co } } } +coro::Generator> KeyValueDB::iter() const { + std::unique_ptr it(impl->db->NewIterator(impl->readOptions)); + for (it->SeekToFirst(); it->Valid(); it->Next()) { + auto k = it->key(); + auto v = it->value(); + co_yield { + {k.data(), k.size()}, + {v.data(), v.size()} + }; + } +} +} // namespace ll::data diff --git a/src/ll/api/data/KeyValueDB.h b/src/ll/api/data/KeyValueDB.h index 9bc93ff710..a3e6dbc94d 100644 --- a/src/ll/api/data/KeyValueDB.h +++ b/src/ll/api/data/KeyValueDB.h @@ -9,6 +9,7 @@ #include #include "ll/api/base/Macro.h" +#include "ll/api/coro/Generator.h" namespace ll::data { class KeyValueDB { @@ -47,7 +48,9 @@ class KeyValueDB { LLAPI bool del(std::string_view key); - LLAPI void iter(std::function const& fn) const; + [[deprecated]] LLAPI void iter(std::function const& fn) const; + + LLNDAPI coro::Generator> iter() const; }; } // namespace ll::data diff --git a/src/ll/api/io/Logger.cpp b/src/ll/api/io/Logger.cpp index 222388b7da..a484ebbd85 100644 --- a/src/ll/api/io/Logger.cpp +++ b/src/ll/api/io/Logger.cpp @@ -104,11 +104,9 @@ size_t Logger::addSink(std::shared_ptr sink) const { std::shared_ptr Logger::getSink(size_t index) const { return impl->sinks->at(index); } -void Logger::forEachSink(std::function const& fn) const { +coro::Generator Logger::sinks() const { for (auto& sink : *impl->sinks) { - if (!fn(*sink)) { - break; - } + co_yield *sink; } } diff --git a/src/ll/api/io/Logger.h b/src/ll/api/io/Logger.h index 31ec3b5c4d..586c9deba7 100644 --- a/src/ll/api/io/Logger.h +++ b/src/ll/api/io/Logger.h @@ -7,6 +7,7 @@ #include "ll/api/base/Concepts.h" // IWYU pragma: keep #include "ll/api/base/Macro.h" +#include "ll/api/coro/Generator.h" #include "ll/api/io/LogLevel.h" #include "ll/api/io/Sink.h" @@ -121,6 +122,6 @@ class Logger { LLAPI std::shared_ptr getSink(size_t index) const; - LLAPI void forEachSink(std::function const& fn) const; + LLNDAPI coro::Generator sinks() const; }; } // namespace ll::io diff --git a/src/ll/api/mod/Mod.cpp b/src/ll/api/mod/Mod.cpp index 22f17821cc..6909d65c7b 100644 --- a/src/ll/api/mod/Mod.cpp +++ b/src/ll/api/mod/Mod.cpp @@ -47,6 +47,8 @@ Manifest const& Mod::getManifest() const { return mImpl->manifest; } std::string const& Mod::getName() const { return getManifest().name; } +std::string const& Mod::getType() const { return getManifest().type; } + std::filesystem::path const& Mod::getModDir() const { return mImpl->modDir; } std::filesystem::path const& Mod::getDataDir() const { return mImpl->dataDir; } diff --git a/src/ll/api/mod/Mod.h b/src/ll/api/mod/Mod.h index 71e39ed5f3..5e739bb75e 100644 --- a/src/ll/api/mod/Mod.h +++ b/src/ll/api/mod/Mod.h @@ -33,6 +33,8 @@ class Mod { LLNDAPI std::string const& getName() const; + LLNDAPI std::string const& getType() const; + LLNDAPI std::filesystem::path const& getModDir() const; LLNDAPI std::filesystem::path const& getDataDir() const; diff --git a/src/ll/api/mod/ModManager.cpp b/src/ll/api/mod/ModManager.cpp index 04268fab79..76afb879b3 100644 --- a/src/ll/api/mod/ModManager.cpp +++ b/src/ll/api/mod/ModManager.cpp @@ -13,7 +13,7 @@ ModManager::ModManager(std::string_view type) : impl(std::make_unique(std: ModManager::~ModManager() = default; -std::lock_guard ModManager::lock() { return std::lock_guard(impl->mutex); } +std::lock_guard ModManager::lock() const { return std::lock_guard(impl->mutex); } void ModManager::addMod(std::string_view name, std::shared_ptr const& mod) { auto l(lock()); @@ -39,12 +39,12 @@ Expected<> ModManager::disable(std::string_view name) { [[nodiscard]] std::string const& ModManager::getType() const { return impl->type; } -[[nodiscard]] bool ModManager::hasMod(std::string_view name) { +[[nodiscard]] bool ModManager::hasMod(std::string_view name) const { auto l(lock()); return impl->mods.contains(name); } -[[nodiscard]] std::shared_ptr ModManager::getMod(std::string_view name) { +[[nodiscard]] std::shared_ptr ModManager::getMod(std::string_view name) const { auto l(lock()); if (auto i = impl->mods.find(name); i != impl->mods.end()) { return i->second; @@ -52,7 +52,7 @@ Expected<> ModManager::disable(std::string_view name) { return {}; } -[[nodiscard]] size_t ModManager::getModCount() { +[[nodiscard]] size_t ModManager::getModCount() const { auto l(lock()); return impl->mods.size(); } @@ -66,4 +66,10 @@ void ModManager::forEachMod(std::function con } } +coro::Generator ModManager::mods() const { + auto l(lock()); + for (auto& p : impl->mods) { + co_yield *p.second; + } +} } // namespace ll::mod diff --git a/src/ll/api/mod/ModManager.h b/src/ll/api/mod/ModManager.h index 31d58e7477..649e7ebba7 100644 --- a/src/ll/api/mod/ModManager.h +++ b/src/ll/api/mod/ModManager.h @@ -9,6 +9,7 @@ #include "ll/api/Expected.h" #include "ll/api/base/Macro.h" +#include "ll/api/coro/Generator.h" #include "ll/api/mod/Manifest.h" #include "ll/api/mod/Mod.h" @@ -23,16 +24,18 @@ class ModManager { public: LLNDAPI std::string const& getType() const; - LLNDAPI bool hasMod(std::string_view name); + LLNDAPI bool hasMod(std::string_view name) const; - LLNDAPI std::shared_ptr getMod(std::string_view name); + LLNDAPI std::shared_ptr getMod(std::string_view name) const; - LLNDAPI size_t getModCount(); + LLNDAPI size_t getModCount() const; - LLAPI void forEachMod(std::function const& fn); + [[deprecated]] LLAPI void forEachMod(std::function const& fn); + + LLNDAPI coro::Generator mods() const; protected: - LLNDAPI std::lock_guard lock(); + LLNDAPI std::lock_guard lock() const; LLNDAPI explicit ModManager(std::string_view type); diff --git a/src/ll/api/mod/ModManagerRegistry.cpp b/src/ll/api/mod/ModManagerRegistry.cpp index 592f94a9e0..0e02f56072 100644 --- a/src/ll/api/mod/ModManagerRegistry.cpp +++ b/src/ll/api/mod/ModManagerRegistry.cpp @@ -127,10 +127,25 @@ bool ModManagerRegistry::eraseManager(std::string_view type) { return false; } -void ModManagerRegistry::forEachManager(std::function const& fn) const { +coro::Generator ModManagerRegistry::managers() const { + std::lock_guard lock(impl->modMtx); + for (auto& p : impl->managers) { + co_yield *p.second; + } +} + +coro::Generator ModManagerRegistry::mods() const { std::lock_guard lock(impl->modMtx); - for (auto& [type, manager] : impl->managers) { - if (!fn(type, *manager)) { + for (auto& manager : managers()) { + for (auto& mod : manager.mods()) { + co_yield mod; + } + } +} + +void ModManagerRegistry::forEachManager(std::function const& fn) const { + for (auto& manager : managers()) { + if (!fn(manager.getType(), manager)) { return; } } @@ -139,17 +154,11 @@ void ModManagerRegistry::forEachManager(std::function const& fn ) const { - std::lock_guard lock(impl->modMtx); - bool interrupted = false; - forEachManager([&](std::string_view type, ModManager& manager) { - manager.forEachMod([&](std::string_view name, Mod& mod) { - if (!fn(type, name, mod)) { - interrupted = true; - } - return !interrupted; - }); - return !interrupted; - }); + for (auto& mod : mods()) { + if (!fn(mod.getType(), mod.getName(), mod)) { + return; + } + } } bool ModManagerRegistry::hasMod(std::string_view name) const { diff --git a/src/ll/api/mod/ModManagerRegistry.h b/src/ll/api/mod/ModManagerRegistry.h index 60bd4490b7..4c2cf5526a 100644 --- a/src/ll/api/mod/ModManagerRegistry.h +++ b/src/ll/api/mod/ModManagerRegistry.h @@ -2,6 +2,7 @@ #include "ll/api/Expected.h" #include "ll/api/base/Macro.h" +#include "ll/api/coro/Generator.h" #include "ll/api/data/DependencyGraph.h" #include "ll/api/mod/ModManager.h" @@ -47,10 +48,14 @@ class ModManagerRegistry { LLNDAPI std::shared_ptr getManagerForMod(std::string_view name) const; - LLAPI void forEachManager(std::function const& fn) const; + [[deprecated]] LLAPI void forEachManager(std::function const& fn) const; - LLAPI void forEachModWithType(std::function const& fn - ) const; + [[deprecated]] LLAPI void + forEachModWithType(std::function const& fn) const; + + LLNDAPI coro::Generator managers() const; + + LLNDAPI coro::Generator mods() const; LLNDAPI bool hasMod(std::string_view name) const; diff --git a/src/ll/core/command/ModManage.cpp b/src/ll/core/command/ModManage.cpp index 298a6a4755..aeeb27c3e5 100644 --- a/src/ll/core/command/ModManage.cpp +++ b/src/ll/core/command/ModManage.cpp @@ -164,13 +164,10 @@ void registerModManageCommand() { cmd.overload().text("list").execute([](CommandOrigin const&, CommandOutput& output) { size_t amount = 0; std::string mods; - ll::mod::ModManagerRegistry::getInstance().forEachModWithType( - [&amount, &mods](std::string_view, std::string_view name, mod::Mod&) { - ++amount; - mods = mods.append(name).append(", "); - return true; - } - ); + for (auto& mod : ll::mod::ModManagerRegistry::getInstance().mods()) { + ++amount; + mods = mods.append(mod.getName()).append(", "); + } if (!mods.empty()) { mods.resize(mods.size() - 2); }