use clap::Parser;
use utils::args::Args;
#[cfg(target_os = "android")]
const TAG: &str = "CatgirlEngine";
pub(super) fn process_args() {
#[cfg(feature = "client")]
client::game::store_resources_path(get_args().resources);
#[cfg(all(feature = "client", target_os = "linux"))]
if get_args().uninstall_desktop_files {
trace!("Uninstalling desktop files...");
let _ = client::uninstall_desktop_files();
}
#[cfg(all(feature = "client", target_os = "linux"))]
if get_args().install_desktop_files {
trace!("Installing desktop files...");
let _ = client::install_desktop_files();
}
#[cfg(not(target_family = "wasm"))]
if get_args().print_environment_variables {
trace!("Printing environment variables...");
utils::environment::print_environment_vars();
}
#[cfg(feature = "client")]
trace!("Resources Path: {:?}", client::game::get_resources_path());
}
#[must_use]
pub(super) fn get_args() -> Args {
if utils::args::get_args().is_some() {
utils::args::get_args().unwrap()
} else {
Args::parse()
}
}
#[cfg(feature = "logging-subscriber")]
pub(super) fn setup_logger() {
if cfg!(target_os = "android") {
#[cfg(target_os = "android")]
android_logger::init_once(
android_logger::Config::default()
.with_max_level(tracing::log::LevelFilter::Trace)
.with_tag(TAG)
.with_filter(
android_logger::FilterBuilder::new()
.parse("main=trace,catgirl_engine=trace")
.build(),
),
);
} else if cfg!(target_family = "wasm") {
#[cfg(target_family = "wasm")]
if let Err(_error) = fern::Dispatch::new()
.level(tracing::log::LevelFilter::Off)
.level_for("main", tracing::log::LevelFilter::Trace)
.level_for("catgirl_engine", tracing::log::LevelFilter::Trace)
.level_for("catgirl_engine_client", tracing::log::LevelFilter::Trace)
.level_for("catgirl_engine_server", tracing::log::LevelFilter::Trace)
.level_for("catgirl_engine_utils", tracing::log::LevelFilter::Trace)
.chain(fern::Output::call(console_log::log))
.apply()
{
warn!("Failed to initialize console logger...")
}
} else {
let mut builder = pretty_env_logger::formatted_builder();
if let Ok(s) = std::env::var("RUST_LOG") {
builder.parse_filters(&s);
} else {
builder
.default_format()
.filter(Some("main"), tracing::log::LevelFilter::Info)
.filter(Some("catgirl_engine"), tracing::log::LevelFilter::Info)
.filter(
Some("catgirl_engine_client"),
tracing::log::LevelFilter::Info,
)
.filter(
Some("catgirl_engine_server"),
tracing::log::LevelFilter::Info,
)
.filter(
Some("catgirl_engine_utils"),
tracing::log::LevelFilter::Info,
);
}
builder.try_init().unwrap();
}
}
fn set_panic_hook() {
std::panic::set_hook(Box::new(|info| {
let location_string = if let Some(location) = info.location() {
format!(
" in file {} at line:column {}:{}",
location.file(),
location.line(),
location.column()
)
} else {
String::new()
};
if let Some(string) = info.payload().downcast_ref::<String>() {
error!("Caught panic{location_string}: {string}");
} else {
error!("Caught panic{location_string}");
}
utils::exit::set_exit();
}));
}
pub(super) fn start() -> Result<(), String> {
info!("Starting Game...");
debug!("Setting panic hook...");
set_panic_hook();
#[cfg(any(target_family = "unix", target_family = "windows"))]
{
debug!("Setting SIGINT hook...");
ctrlc::set_handler(move || {
debug!("SIGINT (Ctrl+C) Was Called! Stopping...");
utils::exit::set_exit();
#[cfg(feature = "client")]
if !get_args().server {
#[cfg(not(target_family = "wasm"))]
let _ = client::game::advance_event_loop();
}
})
.expect("Could not create Interrupt Handler (e.g. Ctrl+C)...");
}
debug!("Starting main loop...");
#[cfg(feature = "server")]
if cfg!(not(feature = "client")) || get_args().server {
return server::game::game_loop();
}
#[cfg(feature = "client")]
return client::game::game_loop();
#[cfg(not(feature = "client"))]
Err("Neither the client nor server features were configured at build time...".to_string())
}