Add helpers for appending to Lua's package.path
This commit is contained in:
1 parent
29e2958dd3
commit
aa1bfb768e
3 files changed
+33
-22
No files matched your search
+2
-15
@@ -13,7 +13,7 @@ use crate::{
|
|||||||
pob::subscript::{SubscriptManager, SubscriptResult},
|
pob::subscript::{SubscriptManager, SubscriptResult},
|
||||||
renderer::textures::TextureManager,
|
renderer::textures::TextureManager,
|
||||||
stage::{StageEvent, StageFrameOutput, StageTransition},
|
stage::{StageEvent, StageFrameOutput, StageTransition},
|
||||||
util::change_working_directory,
|
util::{append_lua_package_dir, change_working_directory},
|
||||||
window::WindowState,
|
window::WindowState,
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
@@ -111,14 +111,13 @@ impl PathOfBuilding {
|
|||||||
fn create_lua(ctx: Context) -> LuaResult<Lua> {
|
fn create_lua(ctx: Context) -> LuaResult<Lua> {
|
||||||
// `unsafe_new` needed to allow loading of C modules
|
// `unsafe_new` needed to allow loading of C modules
|
||||||
let lua = unsafe { Lua::unsafe_new() };
|
let lua = unsafe { Lua::unsafe_new() };
|
||||||
|
append_lua_package_dir(&lua, &ctx.script_dir.join("lua"))?;
|
||||||
|
|
||||||
// expose import url to lua
|
// expose import url to lua
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
let args_table = lua.create_sequence_from(std::iter::once(args.import_url))?;
|
let args_table = lua.create_sequence_from(std::iter::once(args.import_url))?;
|
||||||
lua.globals().set("arg", args_table)?;
|
lua.globals().set("arg", args_table)?;
|
||||||
|
|
||||||
Self::register_package_paths(&lua, &ctx.script_dir)?;
|
|
||||||
|
|
||||||
// make context accessible to API functions
|
// make context accessible to API functions
|
||||||
lua.set_app_data(ctx);
|
lua.set_app_data(ctx);
|
||||||
|
|
||||||
@@ -128,18 +127,6 @@ impl PathOfBuilding {
|
|||||||
Ok(lua)
|
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.
|
/// Loads and runs `Launch.lua`, then calls PoB's `OnInit` handler.
|
||||||
fn launch(lua: &Lua, script_dir: &Path) -> LuaResult<()> {
|
fn launch(lua: &Lua, script_dir: &Path) -> LuaResult<()> {
|
||||||
change_working_directory(script_dir)?;
|
change_working_directory(script_dir)?;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::{pob::PathOfBuilding, pob::api};
|
use crate::{pob::api, util::append_lua_package_dir};
|
||||||
use anyhow::{Result, anyhow};
|
use anyhow::{Result, anyhow};
|
||||||
use mlua::{Integer, IntoLuaMulti, Lua, MultiValue, Number, Value};
|
use mlua::{Integer, IntoLuaMulti, Lua, MultiValue, Number, Value};
|
||||||
use std::{
|
use std::{
|
||||||
@@ -134,10 +134,7 @@ impl Subscript {
|
|||||||
|
|
||||||
// unsafe required to load C modules (curl)
|
// unsafe required to load C modules (curl)
|
||||||
let lua = unsafe { Lua::unsafe_new() };
|
let lua = unsafe { Lua::unsafe_new() };
|
||||||
|
append_lua_package_dir(&lua, &script_dir.join("lua"))?;
|
||||||
// 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)?;
|
|
||||||
|
|
||||||
// register a proxy for each allowed blocking call.
|
// register a proxy for each allowed blocking call.
|
||||||
// forwards the call to the main thread and blocks on `reply_rx` until the main thread
|
// forwards the call to the main thread and blocks on `reply_rx` until the main thread
|
||||||
|
|||||||
+29
-2
@@ -1,11 +1,11 @@
|
|||||||
|
use ahash::AHasher;
|
||||||
|
use mlua::{Lua, Result as LuaResult, Table};
|
||||||
use std::{
|
use std::{
|
||||||
env,
|
env,
|
||||||
hash::{Hash, Hasher},
|
hash::{Hash, Hasher},
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
};
|
};
|
||||||
|
|
||||||
use ahash::AHasher;
|
|
||||||
|
|
||||||
pub fn get_executable_dir() -> anyhow::Result<PathBuf> {
|
pub fn get_executable_dir() -> anyhow::Result<PathBuf> {
|
||||||
let exe_path = env::current_exe()?;
|
let exe_path = env::current_exe()?;
|
||||||
let exe_parent_dir = exe_path.parent().unwrap().canonicalize()?;
|
let exe_parent_dir = exe_path.parent().unwrap().canonicalize()?;
|
||||||
@@ -52,3 +52,30 @@ pub fn replace_in_matching_lines(
|
|||||||
}
|
}
|
||||||
output
|
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:
|
||||||
|
/// - `<path>/?.lua`
|
||||||
|
/// - `<path>/?/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(())
|
||||||
|
}
|
||||||
Reference in new issue
Block a user