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
|
## Features
|
||||||
|
|
||||||
- Running Lua from
|
- Running Lua from
|
||||||
- `errornowatcher.lua`
|
|
||||||
- in-game chat messages
|
- in-game chat messages
|
||||||
- Matrix chat messages
|
- Matrix chat messages
|
||||||
- POST requests to HTTP server
|
- POST requests to HTTP server
|
||||||
@ -25,4 +24,4 @@ $ cargo build --release
|
|||||||
$ # ./target/release/errornowatcher
|
$ # ./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)]
|
#[derive(Parser)]
|
||||||
#[command(version = build_info::version_formatted())]
|
#[command(version = build_info::version_formatted())]
|
||||||
pub struct Arguments {
|
pub struct Arguments {
|
||||||
/// Path to Lua entry point
|
/// Path to main Lua file
|
||||||
#[arg(short, long, default_value = "errornowatcher.lua")]
|
#[arg(short, long)]
|
||||||
pub script: PathBuf,
|
pub script: Option<PathBuf>,
|
||||||
|
|
||||||
/// Code to execute after loading script
|
/// Code to execute (after script)
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
pub exec: Option<String>,
|
pub exec: Option<String>,
|
||||||
}
|
}
|
||||||
|
23
src/main.rs
23
src/main.rs
@ -27,6 +27,7 @@ use clap::Parser;
|
|||||||
use commands::{CommandSource, register};
|
use commands::{CommandSource, register};
|
||||||
use futures::lock::Mutex;
|
use futures::lock::Mutex;
|
||||||
use futures_locks::RwLock;
|
use futures_locks::RwLock;
|
||||||
|
use log::debug;
|
||||||
use mlua::{Function, Lua, Table};
|
use mlua::{Function, Lua, Table};
|
||||||
use replay::{plugin::RecordPlugin, recorder::Recorder};
|
use replay::{plugin::RecordPlugin, recorder::Recorder};
|
||||||
use std::{
|
use std::{
|
||||||
@ -58,24 +59,28 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
let event_listeners = Arc::new(RwLock::new(HashMap::new()));
|
let event_listeners = Arc::new(RwLock::new(HashMap::new()));
|
||||||
let lua = unsafe { Lua::unsafe_new() };
|
let lua = unsafe { Lua::unsafe_new() };
|
||||||
let globals = lua.globals();
|
let globals = lua.globals();
|
||||||
|
|
||||||
lua::register_globals(&lua, &globals, event_listeners.clone())?;
|
lua::register_globals(&lua, &globals, event_listeners.clone())?;
|
||||||
globals.set("SCRIPT_PATH", &*args.script)?;
|
|
||||||
lua.load(
|
if let Some(path) = args.script {
|
||||||
read_to_string(&args.script)
|
globals.set("SCRIPT_PATH", &*path)?;
|
||||||
.with_context(|| format!("failed to read {}", args.script.display()))?,
|
lua.load(read_to_string(path)?).exec()?;
|
||||||
)
|
} else if let Some(code) = ["main.lua", "errornowatcher.lua"].iter().find_map(|path| {
|
||||||
.exec()?;
|
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 {
|
if let Some(code) = args.exec {
|
||||||
lua.load(code).exec()?;
|
lua.load(code).exec()?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let server = globals
|
let server = globals
|
||||||
.get::<String>("Server")
|
.get::<String>("Server")
|
||||||
.expect("Server should be in lua globals");
|
.context("lua globals missing Server variable")?;
|
||||||
let username = globals
|
let username = globals
|
||||||
.get::<String>("Username")
|
.get::<String>("Username")
|
||||||
.expect("Username should be in lua globals");
|
.context("lua globals missing Username variable")?;
|
||||||
|
|
||||||
let mut commands = CommandDispatcher::new();
|
let mut commands = CommandDispatcher::new();
|
||||||
register(&mut commands);
|
register(&mut commands);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user