From f8ec1ca95e2abd4c41dff43344769d73daeea41f Mon Sep 17 00:00:00 2001 From: PRESFIL Date: Wed, 11 Jun 2025 18:34:26 +0000 Subject: [PATCH] fix(lua): Make rtorrent.lua accessible at start --- Makefile.am | 4 +++- rtorrent.lua => lua/rtorrent.lua | 0 src/Makefile.am | 2 +- src/rpc/lua.cc | 14 ++++++++++++++ src/rpc/lua.h | 2 ++ 5 files changed, 20 insertions(+), 2 deletions(-) rename rtorrent.lua => lua/rtorrent.lua (100%) diff --git a/Makefile.am b/Makefile.am index 3ef7262c..f41bec55 100644 --- a/Makefile.am +++ b/Makefile.am @@ -3,8 +3,10 @@ SUBDIRS = \ src \ test +nobase_dist_pkgdata_DATA = \ + lua/rtorrent.lua + EXTRA_DIST= \ - rtorrent.lua \ rak/address_info.h \ rak/algorithm.h \ rak/error_number.h \ diff --git a/rtorrent.lua b/lua/rtorrent.lua similarity index 100% rename from rtorrent.lua rename to lua/rtorrent.lua diff --git a/src/Makefile.am b/src/Makefile.am index 9dee76b5..6b2f1c9a 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -196,4 +196,4 @@ libsub_root_a_SOURCES = \ thread_worker.h -AM_CPPFLAGS = -I$(srcdir) -I$(top_srcdir) +AM_CPPFLAGS = -I$(srcdir) -I$(top_srcdir) -DPACKAGE_DATADIR=\"$(pkgdatadir)\" diff --git a/src/rpc/lua.cc b/src/rpc/lua.cc index bebf5e01..442b70db 100644 --- a/src/rpc/lua.cc +++ b/src/rpc/lua.cc @@ -25,6 +25,7 @@ namespace rpc { const int LuaEngine::flag_string; const std::string LuaEngine::module_name = "rtorrent"; +const std::string LuaEngine::local_path = PACKAGE_DATADIR "/lua/?.lua;" PACKAGE_DATADIR "/lua/?/init.lua"; #ifdef HAVE_LUA @@ -32,6 +33,7 @@ LuaEngine::LuaEngine() { m_luaState = luaL_newstate(); luaL_openlibs(m_luaState); set_package_preload(); + override_package_path(); } LuaEngine::~LuaEngine() { lua_close(m_luaState); } @@ -46,6 +48,18 @@ LuaEngine::set_package_preload() { lua_pop(l_state, 2); } +void +LuaEngine::override_package_path() { + auto l_state = m_luaState; + lua_getglobal(l_state, "package"); + lua_getfield(l_state, -1, "path"); + std::string current_path(lua_tostring(l_state, -1)); + std::string new_path = local_path + ';' + current_path; + lua_pushstring(l_state, new_path.c_str()); + lua_setfield(l_state, -3, "path"); + lua_pop(l_state, 2); +} + void check_lua_status(lua_State* l_state, int status) { if (status != LUA_OK) { diff --git a/src/rpc/lua.h b/src/rpc/lua.h index 00533d5a..584dc38d 100644 --- a/src/rpc/lua.h +++ b/src/rpc/lua.h @@ -14,6 +14,7 @@ class LuaEngine { public: static const int flag_string = 0x1; static const std::string module_name; + static const std::string local_path; LuaEngine(); ~LuaEngine(); @@ -23,6 +24,7 @@ public: static int lua_init_module(lua_State* l_state); static int lua_rtorrent_call(lua_State* l_state); void set_package_preload(); + void override_package_path(); lua_State* state() { return m_luaState; } private: