use crate::{ui::UiState, GraphHandles};
use bevy::{
asset::io::{file::FileAssetReader, AssetReader, AssetSourceId},
prelude::*,
};
use bevy_animation_graph::core::animation_graph::{serial::AnimationGraphSerial, AnimationGraph};
use std::path::PathBuf;
#[derive(Event)]
pub struct SaveGraph {
pub graph: AssetId<AnimationGraph>,
pub virtual_path: PathBuf,
}
pub fn save_graph_system(
mut evr_save_graph: EventReader<SaveGraph>,
asset_server: Res<AssetServer>,
graph_assets: Res<Assets<AnimationGraph>>,
mut ui_state: ResMut<UiState>,
mut graph_handles: ResMut<GraphHandles>,
) {
for ev in evr_save_graph.read() {
let graph = graph_assets.get(ev.graph).unwrap();
let graph_serial = AnimationGraphSerial::from(graph);
let source = asset_server.get_source(AssetSourceId::Default).unwrap();
let reader = source.reader();
let reader = unsafe { &*((reader as *const dyn AssetReader) as *const FileAssetReader) };
let mut final_path = reader.root_path().clone();
final_path.push(&ev.virtual_path);
info!("Saving graph with id {:?} to {:?}", ev.graph, final_path);
ron::ser::to_writer_pretty(
std::fs::File::create(final_path).unwrap(),
&graph_serial,
ron::ser::PrettyConfig::default(),
)
.unwrap();
if asset_server.get_path(ev.graph).is_none() {
ui_state.selection.graph_editor = None;
graph_handles.unsaved.retain(|h| h.id() != ev.graph);
}
}
}