feat: finish replay recording automatically on exit
Some checks failed
Lint / Cargo.toml (push) Has been cancelled
Lint / Rust (push) Has been cancelled
Build / errornowatcher (push) Has been cancelled

This commit is contained in:
Ryan 2025-03-11 20:03:51 -04:00
parent d8ac556884
commit 90512d631d
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
3 changed files with 36 additions and 20 deletions

1
Cargo.lock generated
View File

@ -1405,6 +1405,7 @@ dependencies = [
"built",
"clap",
"console-subscriber",
"ctrlc",
"futures",
"futures-locks",
"http-body-util",

View File

@ -26,6 +26,7 @@ bevy_ecs = "0"
bevy_log = "0"
clap = { version = "4", features = ["derive", "string"] }
console-subscriber = { version = "0", optional = true }
ctrlc = { version = "3", features = ["termination"] }
futures = "0"
futures-locks = "0"
http-body-util = "0"

View File

@ -16,7 +16,7 @@ use hyper_util::rt::TokioIo;
use log::{debug, error, info, trace};
use mlua::{Error, Function, IntoLuaMulti, Table};
use ncr::utils::trim_header;
use std::net::SocketAddr;
use std::{net::SocketAddr, process::exit};
use tokio::net::TcpListener;
#[allow(clippy::too_many_lines)]
@ -177,26 +177,17 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
Event::Init => {
debug!("received initialize event");
let globals = state.lua.globals();
let ecs = client.ecs.clone();
globals.set(
"finish_replay_recording",
state.lua.create_function_mut(move |_, (): ()| {
ctrlc::set_handler(move || {
debug!("finishing replay recording");
ecs.lock()
.remove_resource::<Recorder>()
.context("recording not active")
.map_err(Error::external)?
.finish()
.map_err(Error::external)
})?,
)?;
globals.set(
"client",
client::Client {
inner: Some(client),
},
)?;
call_listeners(&state, "init", ()).await;
.map(Recorder::finish);
exit(0);
})?;
let globals = state.lua.globals();
lua_init(client, &state, &globals).await?;
let Some(address): Option<SocketAddr> = globals
.get::<String>("HttpAddress")
@ -237,6 +228,29 @@ pub async fn handle_event(client: Client, event: Event, state: State) -> Result<
Ok(())
}
async fn lua_init(client: Client, state: &State, globals: &Table) -> Result<()> {
let ecs = client.ecs.clone();
globals.set(
"finish_replay_recording",
state.lua.create_function_mut(move |_, (): ()| {
ecs.lock()
.remove_resource::<Recorder>()
.context("recording not active")
.map_err(Error::external)?
.finish()
.map_err(Error::external)
})?,
)?;
globals.set(
"client",
client::Client {
inner: Some(client),
},
)?;
call_listeners(state, "init", ()).await;
Ok(())
}
async fn call_listeners<T: Clone + IntoLuaMulti + Send + 'static>(
state: &State,
event_type: &'static str,