feat: expose build information

This commit is contained in:
2025-03-06 19:27:02 -05:00
parent b20beb6e88
commit 4d3947c4ef
12 changed files with 105 additions and 15 deletions

View File

@@ -1,8 +1,10 @@
use crate::build_info;
use clap::Parser;
use std::{net::SocketAddr, path::PathBuf};
/// A Minecraft utility bot
#[derive(Parser)]
#[command(version = build_info::version_formatted())]
pub struct Arguments {
/// Path to main Lua file
#[arg(short, long)]

11
src/build_info.rs Normal file
View File

@@ -0,0 +1,11 @@
pub mod built {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
}
pub fn version_formatted() -> String {
format!(
"v{} ({})",
env!("CARGO_PKG_VERSION"),
built::GIT_COMMIT_HASH_SHORT.unwrap_or("unknown commit")
)
}

View File

@@ -4,7 +4,7 @@ use azalea::blocks::{
};
use mlua::{Function, Lua, Result, Table};
pub fn register_functions(lua: &Lua, globals: &Table) -> Result<()> {
pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> {
globals.set(
"get_block_from_state",
lua.create_function(get_block_from_state)?,

View File

@@ -3,7 +3,7 @@ use futures::executor::block_on;
use mlua::{Function, Lua, Result, Table};
use std::time::{SystemTime, UNIX_EPOCH};
pub fn register_functions(lua: &Lua, globals: &Table, event_listeners: ListenerMap) -> Result<()> {
pub fn register_globals(lua: &Lua, globals: &Table, event_listeners: ListenerMap) -> Result<()> {
let m = event_listeners.clone();
globals.set(
"add_listener",

View File

@@ -1,7 +1,7 @@
use log::{debug, error, info, trace, warn};
use mlua::{Lua, Result, Table};
pub fn register_functions(lua: &Lua, globals: &Table) -> Result<()> {
pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> {
globals.set(
"error",
lua.create_function(|_, message: String| {

View File

@@ -9,7 +9,7 @@ pub mod player;
pub mod system;
pub mod vec3;
use crate::ListenerMap;
use crate::{ListenerMap, build_info::built};
use mlua::{Lua, Table};
use std::{io, time::Duration};
@@ -24,11 +24,15 @@ pub enum Error {
ReadFile(io::Error),
}
pub fn register_functions(
pub fn register_globals(
lua: &Lua,
globals: &Table,
event_listeners: ListenerMap,
) -> mlua::Result<()> {
globals.set("CARGO_PKG_VERSION", env!("CARGO_PKG_VERSION"))?;
globals.set("GIT_COMMIT_HASH", built::GIT_COMMIT_HASH)?;
globals.set("GIT_COMMIT_HASH_SHORT", built::GIT_COMMIT_HASH_SHORT)?;
globals.set(
"sleep",
lua.create_async_function(async |_, duration: u64| {
@@ -37,11 +41,11 @@ pub fn register_functions(
})?,
)?;
block::register_functions(lua, globals)?;
events::register_functions(lua, globals, event_listeners)?;
logging::register_functions(lua, globals)?;
nochatreports::register_functions(lua, globals)?;
system::register_functions(lua, globals)
block::register_globals(lua, globals)?;
events::register_globals(lua, globals, event_listeners)?;
logging::register_globals(lua, globals)?;
nochatreports::register_globals(lua, globals)?;
system::register_globals(lua, globals)
}
pub fn reload(lua: &Lua, sender: Option<String>) -> Result<(), Error> {

View File

@@ -10,7 +10,7 @@ use ncr::{
utils::{prepend_header, trim_header},
};
pub fn register_functions(lua: &Lua, globals: &Table) -> Result<()> {
pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> {
globals.set(
"ncr_aes_key_from_passphrase",
lua.create_function(|_, passphrase: Vec<u8>| {

View File

@@ -6,7 +6,7 @@ use std::{
thread,
};
pub fn register_functions(lua: &Lua, globals: &Table) -> Result<()> {
pub fn register_globals(lua: &Lua, globals: &Table) -> Result<()> {
globals.set(
"system",
lua.create_function(|_, (command, args): (String, Option<Vec<OsString>>)| {

View File

@@ -1,12 +1,14 @@
#![feature(let_chains)]
mod arguments;
mod build_info;
mod commands;
mod events;
mod http;
mod lua;
mod particle;
use arguments::Arguments;
use azalea::{
DefaultBotPlugins, DefaultPlugins, brigadier::prelude::CommandDispatcher, prelude::*,
};
@@ -46,14 +48,14 @@ async fn main() -> anyhow::Result<()> {
#[cfg(feature = "console-subscriber")]
console_subscriber::init();
let args = arguments::Arguments::parse();
let args = Arguments::parse();
let script_path = args.script.unwrap_or(PathBuf::from(DEFAULT_SCRIPT_PATH));
let event_listeners = Arc::new(RwLock::new(HashMap::new()));
let lua = unsafe { Lua::unsafe_new() };
let globals = lua.globals();
globals.set("script_path", &*script_path)?;
lua::register_functions(&lua, &globals, event_listeners.clone())?;
lua::register_globals(&lua, &globals, event_listeners.clone())?;
lua.load(
read_to_string(script_path)
.expect(&(DEFAULT_SCRIPT_PATH.to_owned() + " should be in current directory")),