Skip to content

Commit

Permalink
refactor: refactoring PluginRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Dec 30, 2023
1 parent af5b0e2 commit d7f8aab
Show file tree
Hide file tree
Showing 39 changed files with 908 additions and 417 deletions.
3 changes: 2 additions & 1 deletion src/ll/api/ServerInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
#include "mc/world/Minecraft.h"

namespace ll {
std::atomic<ServerStatus>& getServerStatus() {
std::atomic<ServerStatus>& getServerStatusNonConst() {
static std::atomic<ServerStatus> status;
return status;
}
ServerStatus getServerStatus() { return getServerStatusNonConst().load(); }

Version getBdsVersion() {
static auto ver = [] {
Expand Down
16 changes: 10 additions & 6 deletions src/ll/api/ServerInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
#include "ll/api/base/Version.h"

namespace ll {

enum class ServerStatus {
Default = 0,
Running = 1,
Starting,
Running,
Stopping,
};

LLNDAPI std::atomic<ServerStatus>& getServerStatus();
LLNDAPI Version getBdsVersion();
LLNDAPI Version getLoaderVersion();
LLNDAPI int getServerProtocolVersion();
LLNDAPI bool setServerMotd(std::string const& motd);
std::atomic<ServerStatus>& getServerStatusNonConst();
LLNDAPI ServerStatus getServerStatus();
LLNDAPI Version getBdsVersion();
LLNDAPI Version getLoaderVersion();
LLNDAPI int getServerProtocolVersion();
LLNDAPI bool setServerMotd(std::string const& motd);
} // namespace ll
6 changes: 4 additions & 2 deletions src/ll/api/base/KeyValueDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ class KeyValueDB::KeyValueDBImpl {
};

KeyValueDB::KeyValueDB(std::filesystem::path const& path, bool createIfMiss, int bloomFilterBit) {
std::error_code ec;
std::filesystem::create_directories(path, ec);
if (createIfMiss) {
std::error_code ec;
std::filesystem::create_directories(path, ec);
}
impl = std::make_unique<KeyValueDBImpl>(string_utils::wstr2str(path.native()), createIfMiss, bloomFilterBit);
}

Expand Down
8 changes: 6 additions & 2 deletions src/ll/api/event/DynamicListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ class DynamicListener : public ListenerBase {
public:
using callback_fn = std::function<void(CompoundTag&)>;

explicit DynamicListener(callback_fn fn, EventPriority priority = EventPriority::Normal)
: ListenerBase(priority),
explicit DynamicListener(
callback_fn fn,
EventPriority priority = EventPriority::Normal,
std::weak_ptr<plugin::Plugin> const& plugin = plugin::NativePlugin::current()
)
: ListenerBase(priority, plugin),
callback(std::move(fn)) {}

explicit DynamicListener(
Expand Down
34 changes: 32 additions & 2 deletions src/ll/api/event/EventBus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,32 @@ class CallbackStream {
auto lock = ll::Logger::lock();
try {
logger.error(
"Error in [{}:{}]:",
"Error in [{}:{}] of <{}>:",
ll::reflection::removeTypePrefix(ll::reflection::getDynamicRawName(*l)),
l->getId()
l->getId(),
l->pluginPtr.expired() ? "unknown plugin" : l->pluginPtr.lock()->getManifest().name
);
} catch (...) {}
error_info::printCurrentException();
}
}
}
void publish(std::string_view pluginName, Event& event) {
for (auto& l : listeners) {
auto ptr = l->pluginPtr.lock();
if (!ptr || ptr->getManifest().name != pluginName) {
continue;
}
try {
l->call(event);
} catch (...) {
auto lock = ll::Logger::lock();
try {
logger.error(
"Error in [{}:{}] of <{}>:",
ll::reflection::removeTypePrefix(ll::reflection::getDynamicRawName(*l)),
l->getId(),
pluginName
);
} catch (...) {}
error_info::printCurrentException();
Expand Down Expand Up @@ -111,6 +134,13 @@ void EventBus::publish(Event& event, EventId eventId) {
i->second.publish(event);
}
}
void EventBus::publish(std::string_view pluginName, Event& event, EventId eventId) {
std::shared_lock lock(impl->mutex);

if (auto i = impl->streams.find(eventId); i != impl->streams.end()) {
i->second.publish(pluginName, event);
}
}
size_t EventBus::getListenerCount(EventId eventId) {
std::shared_lock lock(impl->mutex);
if (auto i = impl->streams.find(eventId); i != impl->streams.end()) {
Expand Down
8 changes: 8 additions & 0 deletions src/ll/api/event/EventBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class EventBus {

LLAPI void publish(Event&, EventId);

LLAPI void publish(std::string_view pluginName, Event&, EventId);

LLAPI void setEventEmitter(std::function<std::unique_ptr<EmitterBase>(ListenerBase&)> fn, EventId eventId);

template <std::derived_from<Event> T>
Expand All @@ -55,6 +57,12 @@ class EventBus {
publish(event, getEventId<T>);
}

template <class T>
requires(std::derived_from<std::remove_cvref_t<T>, Event>)
void publish(std::string_view pluginName, T&& event) {
publish(pluginName, event, getEventId<T>);
}

LLNDAPI size_t getListenerCount(EventId);

template <std::derived_from<Event> T>
Expand Down
2 changes: 1 addition & 1 deletion src/ll/api/event/EventId.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ constexpr EventId getEventId = []() -> EventId {
namespace std {
template <>
struct hash<ll::event::EventId> {
size_t operator()(ll::event::EventId id) const noexcept { return id.hash; }
size_t operator()(ll::event::EventId const& id) const noexcept { return id.hash; }
};
} // namespace std
8 changes: 6 additions & 2 deletions src/ll/api/event/Listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ class Listener : public ListenerBase {
using event_type = T;
using callback_fn = std::function<void(event_type&)>;

explicit Listener(callback_fn fn, EventPriority priority = EventPriority::Normal)
: ListenerBase(priority),
explicit Listener(
callback_fn fn,
EventPriority priority = EventPriority::Normal,
std::weak_ptr<plugin::Plugin> const& plugin = plugin::NativePlugin::current()
)
: ListenerBase(priority, plugin),
callback(std::move(fn)) {}

~Listener() override = default;
Expand Down
14 changes: 11 additions & 3 deletions src/ll/api/event/ListenerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "ll/api/base/StdInt.h"
#include "ll/api/event/EventId.h"
#include "ll/api/plugin/NativePlugin.h"

namespace ll::event {
class EventBus;
Expand All @@ -24,11 +25,18 @@ class ListenerBase {
LLAPI static std::atomic_ullong listenerId;

friend CallbackStream;
ListenerId id;
EventPriority priority;
ListenerId id;
EventPriority priority;
std::weak_ptr<plugin::Plugin> pluginPtr;

protected:
explicit ListenerBase(EventPriority priority) : id(listenerId++), priority(priority) {}
explicit ListenerBase(
EventPriority priority,
std::weak_ptr<plugin::Plugin> const& plugin = plugin::NativePlugin::current()
)
: id(listenerId++),
priority(priority),
pluginPtr(plugin) {}

public:
ListenerBase(ListenerBase&&) = delete;
Expand Down
7 changes: 6 additions & 1 deletion src/ll/api/event/MultiListener.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ class MultiListener : public ListenerBase {
using callback_fn = std::function<void(Event&)>;

template <class Callable>
explicit MultiListener(Callable&& fn, EventPriority priority = EventPriority::Normal) : ListenerBase(priority) {
explicit MultiListener(
Callable&& fn,
EventPriority priority = EventPriority::Normal,
std::weak_ptr<plugin::Plugin> const& plugin = plugin::NativePlugin::current()
)
: ListenerBase(priority, plugin) {
event_list::forEach([fn = std::forward<Callable>(fn), this]<class E>() {
callback.emplace(typeid(E), [fn](Event& ev) { static_cast<void>(fn(static_cast<E&>(ev))); });
});
Expand Down
9 changes: 7 additions & 2 deletions src/ll/api/event/filesystem/FileActionEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,13 @@ class Listener<fs::FileActionEvent> : public ListenerBase {
using event_type = fs::FileActionEvent;
using callback_fn = std::function<void(event_type&)>;

explicit Listener(std::string const& path, callback_fn fn, EventPriority priority = EventPriority::Normal)
: ListenerBase(priority),
explicit Listener(
std::string const& path,
callback_fn fn,
EventPriority priority = EventPriority::Normal,
std::weak_ptr<plugin::Plugin> const& plugin = plugin::NativePlugin::current()
)
: ListenerBase(priority, plugin),
path(path),
callback(std::move(fn)) {
nativeId.assign(event::getEventId<event_type>.name);
Expand Down
2 changes: 2 additions & 0 deletions src/ll/api/event/world/RequestShutdownEvent.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ll/api/event/world/RequestShutdownEvent.h"
#include "ll/api/event/Emitter.h"
#include "ll/api/memory/Hook.h"
#include "ll/api/ServerInfo.h"

#include "mc/nbt/CompoundTag.h"

Expand All @@ -23,6 +24,7 @@ LL_TYPED_INSTANCE_HOOK(
void,
std::string const& message
) {
getServerStatusNonConst() = ServerStatus::Stopping;
EventBus::getInstance().publish(RequestShutdownEvent(*this, message));
origin(message);
}
Expand Down
2 changes: 2 additions & 0 deletions src/ll/api/event/world/ServerStartedEvent.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "ll/api/event/world/ServerStartedEvent.h"
#include "ll/api/ServerInfo.h"
#include "ll/api/event/Emitter.h"
#include "ll/api/memory/Hook.h"

Expand All @@ -24,6 +25,7 @@ LL_TYPED_INSTANCE_HOOK(
::ServerInstance& ins
) {
origin(ins);
getServerStatusNonConst() = ServerStatus::Running;
EventBus::getInstance().publish(ServerStartedEvent(ins));
}

Expand Down
3 changes: 3 additions & 0 deletions src/ll/api/io/FileUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace fs = std::filesystem;
std::filesystem::path u8path(std::string_view src) { return std::filesystem::path{sv2u8sv(src)}; }

std::optional<std::string> readFile(const fs::path& filePath, bool isBinary) {
if (!fs::exists(filePath)) {
return std::nullopt;
}
std::ifstream fRead;
std::ios_base::openmode mode = std::ios_base::in;
if (isBinary) mode |= std::ios_base::binary;
Expand Down
25 changes: 20 additions & 5 deletions src/ll/api/plugin/Manifest.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@
#include <unordered_map>
#include <unordered_set>

#include "ll/api/base/Hash.h"
#include "ll/api/base/Version.h"

namespace ll::plugin {
struct Dependency {
std::string name;
std::optional<std::string> version; // TODO: add version range

[[nodiscard]] constexpr bool operator==(Dependency const& other) const {
return name == other.name && version == other.version;
}
};
} // namespace ll::plugin
namespace std {
template <>
struct hash<ll::plugin::Dependency> {
size_t operator()(ll::plugin::Dependency const& d) const noexcept {
size_t hash = std::hash<std::string>{}(d.name);
ll::hash::hashCombine(std::hash<std::optional<std::string>>{}(d.version), hash);
return hash;
}
};
} // namespace std
namespace ll::plugin {
struct Manifest {
struct Dependency {
std::string name;
std::optional<std::string> version; // TODO: add version range
};
std::string entry;
std::string name;
std::string type;
Expand All @@ -27,5 +43,4 @@ struct Manifest {
std::optional<std::unordered_set<Dependency>> loadBefore;
std::optional<std::unordered_map<std::string, std::string>> extraInfo;
};

} // namespace ll::plugin
29 changes: 29 additions & 0 deletions src/ll/api/plugin/NativePlugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "ll/api/plugin/NativePlugin.h"
#include "ll/api/plugin/PluginManagerRegistry.h"
#include "ll/core/plugin/NativePluginManager.h"

namespace ll::plugin {
struct NativePlugin::Impl {
Handle handle;
};
NativePlugin::NativePlugin(Manifest manifest, Handle handle)
: Plugin(manifest),
mImpl(std::make_unique<Impl>(handle)) {}

NativePlugin::~NativePlugin() = default;

void NativePlugin::setHandle(Handle handle) { mImpl->handle = handle; }

NativePlugin::Handle NativePlugin::getHandle() const { return mImpl->handle; }

std::weak_ptr<NativePlugin> NativePlugin::getByHandle(Handle handle) {
auto manger =
std::static_pointer_cast<NativePluginManager>(PluginManagerRegistry::getInstance().getManager("native").lock());
if (!manger) {
return {};
}
return manger->getPluginByHandle(handle);
}
std::weak_ptr<NativePlugin> NativePlugin::current(Handle handle) { return getByHandle(handle); }

} // namespace ll::plugin
29 changes: 29 additions & 0 deletions src/ll/api/plugin/NativePlugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#pragma once

#include "ll/api/plugin/Plugin.h"
#include "ll/api/utils/WinUtils.h"

namespace ll::plugin {
class NativePluginManager;
class NativePlugin : public Plugin {
friend NativePluginManager;
struct Impl;
std::unique_ptr<Impl> mImpl;

public:
using Handle = void*;

protected:
void setHandle(Handle);

public:
NativePlugin(Manifest, Handle);
~NativePlugin();

LLNDAPI Handle getHandle() const;

LLNDAPI static std::weak_ptr<NativePlugin> getByHandle(Handle);

LLNDAPI static std::weak_ptr<NativePlugin> current(Handle = win_utils::getCurrentModuleHandle());
};
} // namespace ll::plugin
Loading

0 comments on commit d7f8aab

Please sign in to comment.