1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use bevy::{
    asset::{AssetLoader, LoadContext, LoadedAsset},
    reflect::{TypePath, TypeUuid},
    utils::BoxedFuture,
};
use serde::Deserialize;

/// A script that can be loaded by the [crate::ScriptingPlugin].
#[derive(Debug, Deserialize, TypeUuid, TypePath)]
#[uuid = "3ed4b68b-4f5d-4d82-96f6-5194e358921a"]
pub struct RhaiScript(pub String);

/// A loader for [RhaiScript] assets.
#[derive(Default)]
pub struct RhaiScriptLoader;

impl AssetLoader for RhaiScriptLoader {
    fn load<'a>(
        &'a self,
        bytes: &'a [u8],
        load_context: &'a mut LoadContext,
    ) -> BoxedFuture<'a, Result<(), bevy::asset::Error>> {
        Box::pin(async move {
            let rhai_script = RhaiScript(String::from_utf8(bytes.to_vec())?);
            load_context.set_default_asset(LoadedAsset::new(rhai_script));
            Ok(())
        })
    }

    fn extensions(&self) -> &[&str] {
        &["rhai"]
    }
}