From 2040eb00787d1a1a2c59aeec0ec5b7143d7334a0 Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Sat, 15 Mar 2025 23:35:51 -0400 Subject: [PATCH] refactor: tweak script file handling --- README.md | 3 +-- errornowatcher.lua => main.lua | 0 src/arguments.rs | 8 ++++---- src/main.rs | 23 ++++++++++++++--------- 4 files changed, 19 insertions(+), 15 deletions(-) rename errornowatcher.lua => main.lua (100%) diff --git a/README.md b/README.md index b524c56..b3163a2 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,6 @@ A Minecraft bot with Lua scripting support, written in Rust with [azalea](https: ## Features - Running Lua from - - `errornowatcher.lua` - in-game chat messages - Matrix chat messages - POST requests to HTTP server @@ -25,4 +24,4 @@ $ cargo build --release $ # ./target/release/errornowatcher ``` -Make sure the `Server` and `Username` globals are defined in `errornowatcher.lua` before starting the bot. +Make sure the `Server` and `Username` globals are defined in `main.lua` before starting the bot. diff --git a/errornowatcher.lua b/main.lua similarity index 100% rename from errornowatcher.lua rename to main.lua diff --git a/src/arguments.rs b/src/arguments.rs index 2ac29e4..ae24ad5 100644 --- a/src/arguments.rs +++ b/src/arguments.rs @@ -6,11 +6,11 @@ use std::path::PathBuf; #[derive(Parser)] #[command(version = build_info::version_formatted())] pub struct Arguments { - /// Path to Lua entry point - #[arg(short, long, default_value = "errornowatcher.lua")] - pub script: PathBuf, + /// Path to main Lua file + #[arg(short, long)] + pub script: Option, - /// Code to execute after loading script + /// Code to execute (after script) #[arg(short, long)] pub exec: Option, } diff --git a/src/main.rs b/src/main.rs index 3cc4dc1..4538797 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,7 @@ use clap::Parser; use commands::{CommandSource, register}; use futures::lock::Mutex; use futures_locks::RwLock; +use log::debug; use mlua::{Function, Lua, Table}; use replay::{plugin::RecordPlugin, recorder::Recorder}; use std::{ @@ -58,24 +59,28 @@ async fn main() -> anyhow::Result<()> { let event_listeners = Arc::new(RwLock::new(HashMap::new())); let lua = unsafe { Lua::unsafe_new() }; let globals = lua.globals(); - lua::register_globals(&lua, &globals, event_listeners.clone())?; - globals.set("SCRIPT_PATH", &*args.script)?; - lua.load( - read_to_string(&args.script) - .with_context(|| format!("failed to read {}", args.script.display()))?, - ) - .exec()?; + + if let Some(path) = args.script { + globals.set("SCRIPT_PATH", &*path)?; + lua.load(read_to_string(path)?).exec()?; + } else if let Some(code) = ["main.lua", "errornowatcher.lua"].iter().find_map(|path| { + debug!("trying to load code from {path}"); + globals.set("SCRIPT_PATH", *path).ok()?; + read_to_string(path).ok() + }) { + lua.load(code).exec()?; + } if let Some(code) = args.exec { lua.load(code).exec()?; } let server = globals .get::("Server") - .expect("Server should be in lua globals"); + .context("lua globals missing Server variable")?; let username = globals .get::("Username") - .expect("Username should be in lua globals"); + .context("lua globals missing Username variable")?; let mut commands = CommandDispatcher::new(); register(&mut commands);