From aa1bfb768ea4ba08bc706f2d33d40f32c0697863 Mon Sep 17 00:00:00 2001 From: meehl <12648828+meehl@users.noreply.github.com> Date: Wed, 2 Sep 2026 11:16:21 +0200 Subject: [PATCH] Add helpers for appending to Lua's `package.path` --- src/pob.rs | 17 ++--------------- src/pob/subscript.rs | 7 ++----- src/util.rs | 31 +++++++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/pob.rs b/src/pob.rs index 9ef7f66..efc36c1 100644 --- a/src/pob.rs +++ b/src/pob.rs @@ -13,7 +13,7 @@ use crate::{ pob::subscript::{SubscriptManager, SubscriptResult}, renderer::textures::TextureManager, stage::{StageEvent, StageFrameOutput, StageTransition}, - util::change_working_directory, + util::{append_lua_package_dir, change_working_directory}, window::WindowState, }; use std::{ @@ -111,14 +111,13 @@ impl PathOfBuilding { fn create_lua(ctx: Context) -> LuaResult { // `unsafe_new` needed to allow loading of C modules let lua = unsafe { Lua::unsafe_new() }; + append_lua_package_dir(&lua, &ctx.script_dir.join("lua"))?; // expose import url to lua let args = Args::parse(); let args_table = lua.create_sequence_from(std::iter::once(args.import_url))?; lua.globals().set("arg", args_table)?; - Self::register_package_paths(&lua, &ctx.script_dir)?; - // make context accessible to API functions lua.set_app_data(ctx); @@ -128,18 +127,6 @@ impl PathOfBuilding { Ok(lua) } - /// Adds `${script_dir}/lua` to package path - pub fn register_package_paths(lua: &Lua, script_dir: &Path) -> LuaResult<()> { - let package: Table = lua.globals().get("package")?; - let mut package_path: String = package.get("path")?; - package_path.push(';'); - package_path.push_str(script_dir.join("lua/?.lua").to_str().unwrap()); - package_path.push(';'); - package_path.push_str(script_dir.join("lua/?/init.lua").to_str().unwrap()); - package.set("path", package_path)?; - Ok(()) - } - /// Loads and runs `Launch.lua`, then calls PoB's `OnInit` handler. fn launch(lua: &Lua, script_dir: &Path) -> LuaResult<()> { change_working_directory(script_dir)?; diff --git a/src/pob/subscript.rs b/src/pob/subscript.rs index 6a06ae3..9f4f5ff 100644 --- a/src/pob/subscript.rs +++ b/src/pob/subscript.rs @@ -1,4 +1,4 @@ -use crate::{pob::PathOfBuilding, pob::api}; +use crate::{pob::api, util::append_lua_package_dir}; use anyhow::{Result, anyhow}; use mlua::{Integer, IntoLuaMulti, Lua, MultiValue, Number, Value}; use std::{ @@ -134,10 +134,7 @@ impl Subscript { // unsafe required to load C modules (curl) let lua = unsafe { Lua::unsafe_new() }; - - // add ./lua to package.path and package.cpath - // TODO: this is awkward, move package path registration out of PoB? - PathOfBuilding::register_package_paths(&lua, &script_dir)?; + append_lua_package_dir(&lua, &script_dir.join("lua"))?; // register a proxy for each allowed blocking call. // forwards the call to the main thread and blocks on `reply_rx` until the main thread diff --git a/src/util.rs b/src/util.rs index 5025bab..63f30f6 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,11 +1,11 @@ +use ahash::AHasher; +use mlua::{Lua, Result as LuaResult, Table}; use std::{ env, hash::{Hash, Hasher}, path::{Path, PathBuf}, }; -use ahash::AHasher; - pub fn get_executable_dir() -> anyhow::Result { let exe_path = env::current_exe()?; let exe_parent_dir = exe_path.parent().unwrap().canonicalize()?; @@ -52,3 +52,30 @@ pub fn replace_in_matching_lines( } output } + +/// Appends a search pattern to Lua's `package.path`. +pub fn append_lua_package_path(lua: &Lua, pattern: &str) -> LuaResult<()> { + let package: Table = lua.globals().get("package")?; + let mut package_path: String = package.get("path")?; + + if !package_path.is_empty() { + package_path.push(';'); + } + + package_path.push_str(pattern); + package.set("path", package_path)?; + + Ok(()) +} + +/// Adds a directory to Lua's `package.path`. +/// +/// This adds the following patterns: +/// - `/?.lua` +/// - `/?/init.lua` +pub fn append_lua_package_dir(lua: &Lua, path: &Path) -> LuaResult<()> { + let path = path.to_string_lossy(); + append_lua_package_path(lua, &format!("{path}/?.lua"))?; + append_lua_package_path(lua, &format!("{path}/?/init.lua"))?; + Ok(()) +}