feat!: allow executing code after loading script

This commit is contained in:
Ryan 2025-03-10 19:03:12 -04:00
parent 2932c12468
commit f121addc40
3 changed files with 11 additions and 3 deletions

View File

@ -6,7 +6,11 @@ use std::path::PathBuf;
#[derive(Parser)]
#[command(version = build_info::version_formatted())]
pub struct Arguments {
/// Path to main Lua file
/// Path to Lua entrypoint
#[arg(short, long)]
pub script: Option<PathBuf>,
/// Code to execute after loading script
#[arg(short, long)]
pub exec: Option<String>,
}

View File

@ -52,7 +52,7 @@ pub fn reload(lua: &Lua, sender: Option<String>) -> Result<(), Error> {
lua.load(
&std::fs::read_to_string(
lua.globals()
.get::<String>("script_path")
.get::<String>("SCRIPT_PATH")
.map_err(Error::MissingPath)?,
)
.map_err(Error::ReadFile)?,

View File

@ -54,13 +54,17 @@ async fn main() -> anyhow::Result<()> {
let lua = unsafe { Lua::unsafe_new() };
let globals = lua.globals();
globals.set("script_path", &*script_path)?;
lua::register_globals(&lua, &globals, event_listeners.clone())?;
globals.set("SCRIPT_PATH", &*script_path)?;
lua.load(
read_to_string(script_path)
.expect(&(DEFAULT_SCRIPT_PATH.to_owned() + " should be in current directory")),
)
.exec()?;
if let Some(code) = args.exec {
lua.load(code).exec()?;
}
let server = globals
.get::<String>("Server")
.expect("Server should be in lua globals");