1use bevy::{
2 asset::{AssetLoader, LoadContext, io::Reader},
3 prelude::*,
4};
5use miette::{GraphicalReportHandler, GraphicalTheme, Report};
6
7#[derive(Asset, TypePath, Debug, Clone)]
9pub struct MimasScript {
10 pub path: String,
11 pub source: String,
12}
13
14#[derive(Default, TypePath)]
16pub(crate) struct MimasScriptLoader;
17
18impl AssetLoader for MimasScriptLoader {
19 type Asset = MimasScript;
20 type Settings = ();
21 type Error = BevyError;
22
23 async fn load(
24 &self,
25 reader: &mut dyn Reader,
26 _settings: &(),
27 load_context: &mut LoadContext<'_>,
28 ) -> Result<MimasScript, BevyError> {
29 let mut bytes = Vec::new();
30 reader.read_to_end(&mut bytes).await?;
31 Ok(MimasScript {
32 path: load_context.path().to_string(),
33 source: String::from_utf8(bytes)?,
34 })
35 }
36
37 fn extensions(&self) -> &[&str] {
38 &["mim"]
39 }
40}
41
42#[derive(Component)]
44pub struct Script(pub Handle<MimasScript>);
45
46#[derive(Message, Debug, Clone)]
48pub struct ScriptError {
49 pub script: AssetId<MimasScript>,
50 pub entity: Option<Entity>,
51 pub message: String,
52}
53
54pub(crate) fn report(
56 world: &mut World,
57 script: AssetId<MimasScript>,
58 entity: Option<Entity>,
59 err: &Report,
60 message: String,
61) {
62 error!("{err}");
63 eprintln!("{err:?}");
65 world.write_message(ScriptError {
66 script,
67 entity,
68 message,
69 });
70}
71
72pub(crate) fn render(err: &Report) -> String {
74 let mut text = String::new();
75 GraphicalReportHandler::new_themed(GraphicalTheme::unicode_nocolor())
76 .with_wrap_lines(false)
77 .render_report(&mut text, err.as_ref())
78 .expect("writing to a String can't fail");
79 text
80}