start integrating lua with the rest

This commit is contained in:
Lion Kortlepel
2024-02-06 17:50:22 +01:00
parent 43429eadb3
commit 931d94b7d8
11 changed files with 78 additions and 35 deletions

View File

@@ -6,6 +6,7 @@
#include <boost/asio/deadline_timer.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/date_time/posix_time/posix_time_duration.hpp>
#include <boost/signals2.hpp>
#include <boost/system/detail/error_code.hpp>
#include <boost/thread/scoped_thread.hpp>
#include <boost/thread/synchronized_value.hpp>
@@ -45,6 +46,8 @@ public:
/// FileWatcher::sig_file_changed is triggered.
void watch_files_in(const std::filesystem::path& dir);
boost::signals2::signal<void(const std::filesystem::path&)> sig_file_changed {};
private:
/// Entry point for the timer thread.
void thread_main();
@@ -87,4 +90,3 @@ private:
/// Thread on which all watching and timing work runs.
boost::scoped_thread<> m_thread;
};

View File

@@ -8,6 +8,7 @@
#include <boost/asio/io_service.hpp>
#include <boost/asio/post.hpp>
#include <boost/date_time/posix_time/posix_time_duration.hpp>
#include <boost/signals2.hpp>
#include <boost/thread/scoped_thread.hpp>
#include <boost/thread/synchronized_value.hpp>
#include <sol/forward.hpp>
@@ -24,6 +25,8 @@ struct Timer {
boost::posix_time::milliseconds interval;
};
static constexpr const char* BEAMMP_MEMORY_STATE = ":console:";
class LuaPlugin : public Plugin {
public:
/// Declare a new plugin with the path.
@@ -46,6 +49,10 @@ public:
});
}
/// Dangerous: Only use this on a special memory state.
/// Results are *printed to stdout*.
void run_raw_lua(const std::string& raw);
private:
/// Initializes the error handlers for panic and exceptions.
Error initialize_error_handlers();
@@ -117,6 +124,8 @@ private:
FileWatcher m_extensions_watcher { 2 };
boost::signals2::connection m_extensions_watch_conn;
std::vector<std::shared_ptr<Timer>> m_timers {};
std::shared_ptr<Timer> make_timer(size_t ms);
@@ -131,5 +140,5 @@ private:
void l_mp_schedule_call_helper(const boost::system::error_code& err, std::shared_ptr<Timer> timer, const sol::function& fn, std::shared_ptr<ValueTuple> args);
void l_mp_schedule_call_once(size_t ms, const sol::function& fn, sol::variadic_args args);
std::string print_impl(const sol::variadic_args&);
std::string print_impl(const std::vector<sol::object>&);
};

View File

@@ -16,11 +16,11 @@
class Plugin {
public:
/// Self-managing pointer type of this plugin.
using Pointer = std::unique_ptr<Plugin>;
using Pointer = std::shared_ptr<Plugin>;
/// Allocates a Plugin of the specific derived plugin type.
template <typename T, typename... Args>
static Pointer make_pointer(Args&&... args) {
return std::unique_ptr<Plugin>(new T(std::forward<Args>(args)...));
return std::shared_ptr<Plugin>(new T(std::forward<Args>(args)...));
}
/// Default constructor to enable derived classes to default-construct.

View File

@@ -48,7 +48,7 @@ public:
}
/// Adds the plugin, calls Plugin::initialize(), and so on
[[nodiscard]] Error add_plugin(Plugin::Pointer&& plugin) {
[[nodiscard]] Error add_plugin(Plugin::Pointer plugin) {
auto plugins = m_plugins.synchronize();
if (plugins->contains(plugin->name())) {
return Error("Plugin with the name '{}' already exists, refusing to replace it.", plugin->name());
@@ -63,6 +63,10 @@ public:
}
}
HashMap<std::string, Plugin::Pointer> get_plugins() {
return *m_plugins;
}
private:
/// All plugins as pointers to allow inheritance.
SynchronizedHashMap<std::string, Plugin::Pointer> m_plugins;

View File

@@ -19,6 +19,7 @@
#pragma once
#include "Cryptography.h"
#include "LuaPlugin.h"
#include "TScopedTimer.h"
#include "commandline.h"
#include <atomic>
@@ -82,7 +83,7 @@ private:
bool mIsLuaConsole { false };
bool mFirstTime { true };
std::string mStateId;
const std::string mDefaultStateId = "BEAMMP_SERVER_CONSOLE";
const std::string mDefaultStateId = BEAMMP_MEMORY_STATE;
std::ofstream mLogFileStream;
std::mutex mLogFileStreamMtx;
TScopedTimer mUptimeTimer{};

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, size_t max_depth = 500);
Result<Value> sol_obj_to_value(const sol::object&, const std::function<Result<Value>(const sol::object&)>& invalid_provider = nullptr, size_t max_depth = 50);