refactor: clean up and restructure
This commit is contained in:
66
src/main.rs
66
src/main.rs
@@ -12,62 +12,29 @@ use commands::{CommandSource, register};
|
||||
use events::handle_event;
|
||||
use mlua::Lua;
|
||||
use parking_lot::Mutex;
|
||||
use std::{net::SocketAddr, path::PathBuf, process::ExitCode, sync::Arc};
|
||||
use std::{net::SocketAddr, path::PathBuf, sync::Arc};
|
||||
|
||||
#[derive(Default, Clone, Component)]
|
||||
pub struct State {
|
||||
commands: Arc<CommandDispatcher<Mutex<CommandSource>>>,
|
||||
lua: Arc<Mutex<Lua>>,
|
||||
address: Option<SocketAddr>,
|
||||
http_address: Option<SocketAddr>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> ExitCode {
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let args = arguments::Arguments::parse();
|
||||
let lua = Lua::new();
|
||||
let script_path = args.script.unwrap_or(PathBuf::from("errornowatcher.lua"));
|
||||
|
||||
let config_path = args.script.unwrap_or(PathBuf::from("errornowatcher.lua"));
|
||||
if let Err(error) = match &std::fs::read_to_string(&config_path) {
|
||||
Ok(string) => lua.load(string).exec(),
|
||||
Err(error) => {
|
||||
eprintln!("failed to read {config_path:?}: {error:?}");
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
} {
|
||||
eprintln!("failed to execute configuration as lua code: {error:?}");
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
let lua = Lua::new();
|
||||
lua.load(std::fs::read_to_string(&script_path)?).exec()?;
|
||||
|
||||
let globals = lua.globals();
|
||||
let Ok(server) = globals.get::<String>("SERVER") else {
|
||||
eprintln!("no server defined in lua globals!");
|
||||
return ExitCode::FAILURE;
|
||||
};
|
||||
let Ok(username) = globals.get::<String>("USERNAME") else {
|
||||
eprintln!("no username defined in lua globals!");
|
||||
return ExitCode::FAILURE;
|
||||
};
|
||||
let server = globals.get::<String>("SERVER")?;
|
||||
let username = globals.get::<String>("USERNAME")?;
|
||||
|
||||
if let Err(error) = globals.set("config_path", config_path) {
|
||||
eprintln!("failed to set config_path in lua globals: {error:?}");
|
||||
return ExitCode::FAILURE;
|
||||
};
|
||||
if let Err(error) = scripting::logging::init(&lua, &globals) {
|
||||
eprintln!("failed to set up logging wrappers: {error:?}");
|
||||
return ExitCode::FAILURE;
|
||||
};
|
||||
|
||||
let account = if username.contains('@') {
|
||||
match Account::microsoft(&username).await {
|
||||
Ok(a) => a,
|
||||
Err(error) => {
|
||||
eprintln!("failed to login using microsoft account: {error:?}");
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Account::offline(&username)
|
||||
};
|
||||
globals.set("script_path", script_path)?;
|
||||
scripting::logging::register(&lua, &globals)?;
|
||||
|
||||
let mut commands = CommandDispatcher::new();
|
||||
register(&mut commands);
|
||||
@@ -77,11 +44,18 @@ async fn main() -> ExitCode {
|
||||
.set_state(State {
|
||||
commands: Arc::new(commands),
|
||||
lua: Arc::new(Mutex::new(lua)),
|
||||
address: args.address,
|
||||
http_address: args.http_address,
|
||||
})
|
||||
.start(account, server.as_ref())
|
||||
.start(
|
||||
if username.contains('@') {
|
||||
Account::microsoft(&username).await?
|
||||
} else {
|
||||
Account::offline(&username)
|
||||
},
|
||||
server.as_ref(),
|
||||
)
|
||||
.await;
|
||||
eprintln!("{error:?}");
|
||||
|
||||
ExitCode::SUCCESS
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user