refactor: tweak script file handling
This commit is contained in:
parent
c7358fd4c0
commit
2040eb0078
@ -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.
|
||||
|
@ -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<PathBuf>,
|
||||
|
||||
/// Code to execute after loading script
|
||||
/// Code to execute (after script)
|
||||
#[arg(short, long)]
|
||||
pub exec: Option<String>,
|
||||
}
|
||||
|
23
src/main.rs
23
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::<String>("Server")
|
||||
.expect("Server should be in lua globals");
|
||||
.context("lua globals missing Server variable")?;
|
||||
let username = globals
|
||||
.get::<String>("Username")
|
||||
.expect("Username should be in lua globals");
|
||||
.context("lua globals missing Username variable")?;
|
||||
|
||||
let mut commands = CommandDispatcher::new();
|
||||
register(&mut commands);
|
||||
|
Loading…
x
Reference in New Issue
Block a user