mirror of
https://github.com/BeamMP/BeamMP-Server.git
synced 2026-04-13 19:26:12 +00:00
start integrating lua with the rest
This commit is contained in:
@@ -93,9 +93,9 @@ void FileWatcher::check_file(const std::filesystem::path& file) {
|
||||
} else {
|
||||
if (m_file_mod_times.at(file) != time) {
|
||||
beammp_tracef("File changed: {}", file);
|
||||
sig_file_changed(file);;
|
||||
m_file_mod_times.at(file) = time;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -113,19 +113,19 @@ Error LuaPlugin::initialize_libraries() {
|
||||
glob["MP"]["GetStateMemoryUsage"] = [this]() { return size_t(m_state.memory_used()); };
|
||||
glob["MP"]["GetPluginMemoryUsage"] = [this] { return memory_usage(); };
|
||||
glob["MP"]["LogError"] = [this](const sol::variadic_args& args) {
|
||||
auto result = print_impl(args);
|
||||
auto result = print_impl({ args.begin(), args.end() });
|
||||
beammp_lua_errorf("[out] {}", result);
|
||||
};
|
||||
glob["MP"]["LogWarn"] = [this](const sol::variadic_args& args) {
|
||||
auto result = print_impl(args);
|
||||
auto result = print_impl({ args.begin(), args.end() });
|
||||
beammp_lua_warnf("[out] {}", result);
|
||||
};
|
||||
glob["MP"]["LogInfo"] = [this](const sol::variadic_args& args) {
|
||||
auto result = print_impl(args);
|
||||
auto result = print_impl({ args.begin(), args.end() });
|
||||
beammp_lua_infof("[out] {}", result);
|
||||
};
|
||||
glob["MP"]["LogDebug"] = [this](const sol::variadic_args& args) {
|
||||
auto result = print_impl(args);
|
||||
auto result = print_impl({ args.begin(), args.end() });
|
||||
beammp_lua_debugf("[out] {}", result);
|
||||
};
|
||||
glob["MP"]["GetPluginPath"] = [this] {
|
||||
@@ -256,9 +256,7 @@ Error LuaPlugin::load_files() {
|
||||
m_extensions_watcher.watch_files_in(extensions_folder);
|
||||
// set up callback for when an extension changes.
|
||||
// we simply reload the extension as if nothing happened :)
|
||||
// TODO
|
||||
/*
|
||||
m_extensions_watch_conn = m_extensions_watcher.sig_file_changed.connect_scoped(
|
||||
m_extensions_watch_conn = m_extensions_watcher.sig_file_changed.connect(
|
||||
[this, extensions_folder](const std::filesystem::path& path) {
|
||||
if (path.extension() != ".lua") {
|
||||
return; // ignore
|
||||
@@ -272,23 +270,24 @@ Error LuaPlugin::load_files() {
|
||||
load_extension(path, rel);
|
||||
}
|
||||
});
|
||||
*/
|
||||
} else {
|
||||
beammp_lua_debugf("Plugin '{}' has no extensions.", name());
|
||||
}
|
||||
auto main_lua = m_path / "main.lua";
|
||||
if (std::filesystem::exists(main_lua)) {
|
||||
// TODO: Check that it's a regular file or symlink
|
||||
beammp_lua_debugf("Found main.lua: {}", main_lua.string());
|
||||
boost::asio::post(m_io, [this, main_lua] {
|
||||
try {
|
||||
m_state.safe_script_file(main_lua.string());
|
||||
} catch (const std::exception& e) {
|
||||
beammp_lua_errorf("Error running '{}': {}", main_lua.string(), e.what());
|
||||
}
|
||||
});
|
||||
} else {
|
||||
beammp_lua_warnf("No 'main.lua' found, a plugin should have a 'main.lua'.");
|
||||
if (m_path != BEAMMP_MEMORY_STATE) {
|
||||
auto main_lua = m_path / "main.lua";
|
||||
if (std::filesystem::exists(main_lua)) {
|
||||
// TODO: Check that it's a regular file or symlink
|
||||
beammp_lua_debugf("Found main.lua: {}", main_lua.string());
|
||||
boost::asio::post(m_io, [this, main_lua] {
|
||||
try {
|
||||
m_state.safe_script_file(main_lua.string());
|
||||
} catch (const std::exception& e) {
|
||||
beammp_lua_errorf("Error running '{}': {}", main_lua.string(), e.what());
|
||||
}
|
||||
});
|
||||
} else {
|
||||
beammp_lua_warnf("No 'main.lua' found, a plugin should have a 'main.lua'.");
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
@@ -559,7 +558,7 @@ void LuaPlugin::cancel_timer(const std::shared_ptr<Timer>& timer) {
|
||||
}
|
||||
|
||||
void LuaPlugin::l_print(const sol::variadic_args& args) {
|
||||
auto result = print_impl(args);
|
||||
auto result = print_impl({ args.begin(), args.end() });
|
||||
beammp_lua_infof("{}", result);
|
||||
}
|
||||
|
||||
@@ -635,8 +634,7 @@ std::shared_ptr<Timer> LuaPlugin::l_mp_schedule_call_repeat(size_t ms, const sol
|
||||
return timer;
|
||||
}
|
||||
|
||||
std::string LuaPlugin::print_impl(const sol::variadic_args& args) {
|
||||
auto obj_args = std::vector<sol::object>(args.begin(), args.end());
|
||||
std::string LuaPlugin::print_impl(const std::vector<sol::object>& obj_args) {
|
||||
std::string result {};
|
||||
result.reserve(500);
|
||||
|
||||
@@ -678,3 +676,28 @@ std::string LuaPlugin::print_impl(const sol::variadic_args& args) {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
void LuaPlugin::run_raw_lua(const std::string& raw) {
|
||||
if (raw.empty()) {
|
||||
return;
|
||||
}
|
||||
std::string id = "";
|
||||
if (raw.size() < 5) {
|
||||
id = raw;
|
||||
} else {
|
||||
id = raw.substr(0, 5) + "...";
|
||||
}
|
||||
boost::asio::post(m_io, [this, raw, id] {
|
||||
try {
|
||||
beammp_debugf("Running '{}'", raw);
|
||||
auto res = m_state.safe_script(raw, id);
|
||||
if (res.valid()) {
|
||||
std::vector<sol::object> args;
|
||||
args.push_back(res.get<sol::object>());
|
||||
auto str = print_impl(args);
|
||||
Application::Console().WriteRaw(fmt::format("=> {}", str));
|
||||
}
|
||||
} catch (const sol::error& err) {
|
||||
beammp_lua_errorf("Error: {}", err.what());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -169,8 +169,7 @@ void TConsole::ChangeToLuaConsole(const std::string& LuaStateId) {
|
||||
mCommandline->set_prompt("lua> ");
|
||||
}
|
||||
mCachedRegularHistory = mCommandline->history();
|
||||
mCommandline->set_history(mCachedLuaHistory);
|
||||
*/
|
||||
mCommandline->set_history(mCachedLuaHistory);*/
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,6 +222,7 @@ void TConsole::Command_Lua(const std::string&, const std::vector<std::string>& a
|
||||
if (args.size() == 1) {
|
||||
auto NewStateId = args.at(0);
|
||||
beammp_assert(!NewStateId.empty());
|
||||
// TODO: This must be implemented
|
||||
/*
|
||||
if (mLuaEngine->HasState(NewStateId)) {
|
||||
ChangeToLuaConsole(NewStateId);
|
||||
@@ -232,6 +232,7 @@ void TConsole::Command_Lua(const std::string&, const std::vector<std::string>& a
|
||||
*/
|
||||
} else if (args.size() == 0) {
|
||||
ChangeToLuaConsole(mDefaultStateId);
|
||||
// CONTINUE HERE
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -188,7 +188,6 @@ TEST_CASE("ValueToStringVisitor") {
|
||||
|
||||
static Result<Value> sol_obj_to_value_impl(const sol::object& obj, const std::function<Result<Value>(const sol::object&)>& invalid_provider, size_t max_depth, size_t depth) {
|
||||
if (depth == max_depth) {
|
||||
beammp_errorf("Maximum depth reached for sol_obj_to_value_impl, assuming recursion and returning null for this branch");
|
||||
return { Null {} };
|
||||
}
|
||||
++depth;
|
||||
|
||||
@@ -169,6 +169,10 @@ int BeamMPServerMain(MainArguments Arguments) {
|
||||
// TPluginMonitor PluginMonitor(fs::path(Application::Settings.Resource) / "Server", LuaEngine);
|
||||
PluginManager PluginManager;
|
||||
(void)PluginManager.add_plugin(Plugin::make_pointer<LuaPlugin>("Resources/Server/Test"));
|
||||
auto console = Plugin::make_pointer<LuaPlugin>(BEAMMP_MEMORY_STATE);
|
||||
(void)PluginManager.add_plugin(console);
|
||||
|
||||
dynamic_cast<LuaPlugin&>(*console).run_raw_lua(R"(local n = 12; return n)");
|
||||
|
||||
PluginManager.trigger_event("onInit", std::make_shared<Value>(HashMap<std::string, Value> {
|
||||
{ "big", "balls" },
|
||||
|
||||
Reference in New Issue
Block a user