mirror of
https://codeberg.org/schrottkatze/mgd2-tram-championships.git
synced 2025-07-01 17:27:38 +00:00
114 lines
3 KiB
Rust
114 lines
3 KiB
Rust
//! Seperate out debugging uis/plugins in it's own module for cleanliness.
|
|
use std::{fs, path::PathBuf};
|
|
|
|
use bevy::prelude::*;
|
|
use bevy_inspector_egui::{bevy_egui::EguiPlugin, quick::WorldInspectorPlugin};
|
|
use clap::Subcommand;
|
|
use console::exec_script;
|
|
|
|
use crate::AppState;
|
|
|
|
mod console;
|
|
mod debug_event;
|
|
|
|
pub use console::ConsoleLog;
|
|
|
|
pub use debug_event::DebugEvent;
|
|
|
|
/// Debug system set.
|
|
#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)]
|
|
pub struct DebugSet;
|
|
|
|
pub fn plugin(app: &mut App) {
|
|
app.add_plugins(EguiPlugin {
|
|
enable_multipass_for_primary_context: true,
|
|
})
|
|
.add_event::<DebugEvent>()
|
|
.init_state::<DebugMode>()
|
|
.add_systems(
|
|
Update,
|
|
(
|
|
handle_debug_logs,
|
|
handle_enable_debug_mode.run_if(not(in_state(DebugMode::Enabled))),
|
|
start_game,
|
|
run_file.pipe(exec_script),
|
|
)
|
|
.in_set(DebugSet),
|
|
)
|
|
.add_systems(Startup, startup_file.pipe(exec_script))
|
|
.add_plugins((console::plugin /* WorldInspectorPlugin::default() */,));
|
|
}
|
|
|
|
fn start_game(
|
|
mut dbg_reader: EventReader<DebugEvent>,
|
|
mut log: ResMut<ConsoleLog>,
|
|
cur_state: Res<State<AppState>>,
|
|
mut next_state: ResMut<NextState<AppState>>,
|
|
) {
|
|
for ev in dbg_reader.read() {
|
|
if *ev == DebugEvent::StartGame {
|
|
if *cur_state == AppState::Ingame {
|
|
log.err("Can't start game since it's already started.");
|
|
} else {
|
|
log.write("Starting game...");
|
|
next_state.set(AppState::Ingame);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash, Reflect)]
|
|
pub enum DebugMode {
|
|
#[default]
|
|
Disabled,
|
|
Enabled,
|
|
}
|
|
|
|
fn handle_enable_debug_mode(
|
|
mut dbg_reader: EventReader<DebugEvent>,
|
|
mut debug_state: ResMut<NextState<DebugMode>>,
|
|
) {
|
|
for ev in dbg_reader.read() {
|
|
if matches!(ev, DebugEvent::EnableDebugMode) {
|
|
info!("Enabling debug mode. There be demons (hopefully the cute ones :3)!");
|
|
debug_state.set(DebugMode::Enabled)
|
|
}
|
|
}
|
|
}
|
|
|
|
fn handle_debug_logs(mut dbg_reader: EventReader<DebugEvent>) {
|
|
for ev in dbg_reader.read() {
|
|
if let DebugEvent::Log { error, text } = ev {
|
|
if *error {
|
|
error!("{text}")
|
|
} else {
|
|
info!("{text}")
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
fn run_file(mut dbg_reader: EventReader<DebugEvent>) -> Option<(String, String)> {
|
|
for ev in dbg_reader.read() {
|
|
if let DebugEvent::RunFile { file } = ev {
|
|
let f = std::fs::read_to_string(file);
|
|
|
|
if let Ok(f) = f {
|
|
return Some((f, String::from(file.file_name().unwrap().to_string_lossy())));
|
|
}
|
|
}
|
|
}
|
|
|
|
None
|
|
}
|
|
|
|
fn startup_file() -> Option<(String, String)> {
|
|
let path: PathBuf = "./startup.tx".into();
|
|
|
|
fs::exists(&path).unwrap_or(false).then(|| {
|
|
Some((
|
|
fs::read_to_string(&path).unwrap(),
|
|
path.file_name().unwrap().to_string_lossy().to_string(),
|
|
))
|
|
})?
|
|
}
|