use concinnity_engine::app::run::LaunchRequest;
use concinnity_engine::app::runtime::Runtime;
use crate::command::resolve_world_path;
use crate::debug::hot_reload::WorldPathHandle;
use crate::frame_hook::FrameHook;
pub fn run_debug(launch: LaunchRequest, json_path: Option<&str>, port: u16) -> std::io::Result<()> {
concinnity_engine::app::run::init_logging();
let world_path = resolve_world_path(json_path)?;
let hook: Box<dyn FrameHook> = match crate::debug::DebugServer::start(port) {
Ok(srv) => Box::new(srv.with_world_path(WorldPathHandle::new(world_path.as_str()))),
Err(e) => {
eprintln!("error: could not start debug server: {e}");
return Err(e);
}
};
run_interpreted(launch, &world_path, Some(hook))
}
pub(crate) fn run_interpreted(
launch: LaunchRequest,
json_path: &str,
hook: Option<Box<dyn FrameHook>>,
) -> std::io::Result<()> {
let mut runtime = crate::project::runtime().with_launch(launch);
*runtime.world_mut() = crate::authoring::build_world_from_path(json_path).map_err(|e| {
tracing::error!("Could not build world from {json_path}: {e}");
e
})?;
start_app(runtime, hook)
}
pub(crate) fn start_app(
mut runtime: Runtime,
mut hook: Option<Box<dyn FrameHook>>,
) -> std::io::Result<()> {
use concinnity_engine::app::runloop;
let shutdown = runtime.shutdown_token();
runloop::install_ctrlc_handler(&runtime);
if let Some(hook) = hook.as_mut() {
hook.attach_shutdown(shutdown.clone());
}
#[cfg(target_os = "macos")]
let renders = runtime.render_mode().renders();
#[cfg(target_os = "macos")]
if renders {
runloop::activate_app_macos();
}
if let Err(e) = runtime.start() {
tracing::error!("failed to start app: {e}");
return Err(std::io::Error::other(format!("failed to start app: {e}")));
}
let on_tick = |runtime: &mut Runtime| {
if let Some(hook) = hook.as_deref_mut() {
hook.tick(runtime.world_mut());
hook.apply_world_swap(runtime);
}
};
#[cfg(target_os = "macos")]
runloop::run_loop(&mut runtime, renders, on_tick);
#[cfg(not(target_os = "macos"))]
runloop::run_loop(&mut runtime, false, on_tick);
Ok(())
}