This commit is contained in:
Lion Kortlepel
2024-02-06 00:11:31 +01:00
parent 6af471f025
commit 43429eadb3
6 changed files with 143 additions and 56 deletions

View File

@@ -82,7 +82,7 @@ public:
/// Path to the folder containing this lua plugin.
virtual std::filesystem::path path() const override;
/// Dispatches the event to the thread which runs all lua.
virtual std::shared_future<std::optional<Value>> handle_event(const std::string& event_name, const std::shared_ptr<Value>& args) override;
virtual std::shared_future<std::vector<Value>> handle_event(const std::string& event_name, const std::shared_ptr<Value>& args) override;
/// Returns the memory usage of this thread, updated at the slowest every 5 seconds.
virtual size_t memory_usage() const override;
@@ -95,9 +95,9 @@ private:
boost::asio::io_context m_io;
/// Event handlers which are legacy-style (by name)
HashMap<std::string, std::string> m_event_handlers_named {};
HashMap<std::string, std::vector<std::string>> m_event_handlers_named {};
/// Event handlers which are functions (v4 style)
HashMap<std::string, sol::protected_function> m_event_handlers {};
HashMap<std::string, std::vector<sol::protected_function>> m_event_handlers {};
/// Main (and usually only) lua state of this plugin.
/// ONLY access this from the m_thread thread.

View File

@@ -58,7 +58,7 @@ public:
/// Instructs the plugin to handle the given event, with the given arguments.
/// Returns a future with a result if this event will be handled by the plugin, otherwise must return
/// std::nullopt.
virtual std::shared_future<std::optional<Value>> handle_event(const std::string& event_name, const std::shared_ptr<Value>& args) = 0;
virtual std::shared_future<std::vector<Value>> handle_event(const std::string& event_name, const std::shared_ptr<Value>& args) = 0;
/// Returns how much memory this state thinks it uses.
///

View File

@@ -24,9 +24,9 @@ public:
/// so try not to wait on these futures without a timeout.
///
/// This function should not block.
std::vector<std::shared_future<std::optional<Value>>> trigger_event(const std::string& event_name, const std::shared_ptr<Value>& args) {
std::vector<std::shared_future<std::vector<Value>>> trigger_event(const std::string& event_name, const std::shared_ptr<Value>& args) {
// results will go in here
std::vector<std::shared_future<std::optional<Value>>> results;
std::vector<std::shared_future<std::vector<Value>>> results;
// synchronize practically grabs a lock to the mutex, this is (as the name suggests)
// a synchronization point. technically, it could dead-lock if something that is called
// in this locked context tries to lock the m_plugins mutex.
@@ -40,9 +40,9 @@ public:
(void)name; // ignore the name
// propagates the event to the plugin, this returns a future
// we assume that at this point no plugin-specific code has executed
auto maybe_result = plugin->handle_event(event_name, args);
auto result = plugin->handle_event(event_name, args);
// if the plugin had no handler, this result has no value, and we can ignore it
results.push_back(maybe_result);
results.push_back(std::move(result));
}
return results;
}
@@ -67,4 +67,3 @@ private:
/// All plugins as pointers to allow inheritance.
SynchronizedHashMap<std::string, Plugin::Pointer> m_plugins;
};

View File

@@ -265,5 +265,5 @@ private:
/// "invalid provider" means "provider of values for invalid sol values". If nullptr, then
/// any invalid value (such as a function) will be resolved to an error instead and the function will
/// fail.
Result<Value> sol_obj_to_value(const sol::object&, const std::function<Result<Value>(const sol::object&)>& invalid_provider = nullptr);
Result<Value> sol_obj_to_value(const sol::object&, const std::function<Result<Value>(const sol::object&)>& invalid_provider = nullptr, size_t max_depth = 500);