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", "built",
"clap", "clap",
"console-subscriber", "console-subscriber",
"ctrlc",
"futures", "futures",
"futures-locks", "futures-locks",
"http-body-util", "http-body-util",

View File

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

View File

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