From 48b6e535a2f7bf67c1f888850a9544c766438774 Mon Sep 17 00:00:00 2001 From: SpookyDervish Date: Mon, 27 Oct 2025 13:09:10 +1100 Subject: [PATCH] we're sandboxing and loading the log function from textual into LUA!!!!!!! :D --- plugin_loader.py | 24 +++++++++++++++++++++++- plugins/test-plugin/lua/main.lua | 9 +++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 plugins/test-plugin/lua/main.lua diff --git a/plugin_loader.py b/plugin_loader.py index d80e792..783f389 100644 --- a/plugin_loader.py +++ b/plugin_loader.py @@ -1,6 +1,7 @@ from textual_window import Window from textual.widgets import RichLog from textual import work +from lupa import LuaRuntime, lua51 import os, json, asyncio @@ -16,6 +17,8 @@ class PluginLoader(Window): start_open=True ) + self.lua_runtime = lua51.LuaRuntime() + @work async def find_plugins(self): log = self.query_one(RichLog) @@ -67,9 +70,28 @@ class PluginLoader(Window): log.write(f"[d]Ignoring {plugin_folder} because of error: {e}.[/]") continue - log.write(f"[b green]FOUND[/] {plugin_json['name']} ({plugin_json['version']})") + for lua_file in os.listdir(os.path.join(plugin_folder, "lua")): + lua_file_path = os.path.join(plugin_folder, f"lua/{lua_file}") + + with open(lua_file_path, "r") as f: + code = f.read() + sandbox = self.lua_runtime.eval("{}") + setfenv = self.lua_runtime.eval("setfenv") + + sandbox.print = self.log #self.lua_runtime.globals().print + sandbox.math = self.lua_runtime.globals().math + sandbox.string = self.lua_runtime.globals().string + + setfenv(0, sandbox) + executed_code = self.lua_runtime.execute(code) + + if executed_code.init: + executed_code.init() + if executed_code.main: + self.run_worker(thread=True, work=executed_code.main) + plugin_paths.append(plugin_folder) log.write("\n[d]Window will automatically close in 5 seconds.[/]") diff --git a/plugins/test-plugin/lua/main.lua b/plugins/test-plugin/lua/main.lua new file mode 100644 index 0000000..0ea2c7b --- /dev/null +++ b/plugins/test-plugin/lua/main.lua @@ -0,0 +1,9 @@ +local plugin = {} + +function plugin.init() +end + +function plugin.main() +end + +return plugin \ No newline at end of file