bevy_convars/loader/
assets.rs1use bevy_asset::{Asset, AssetLoader, Assets, AsyncReadExt as _, Handle};
2use bevy_ecs::world::World;
3use bevy_reflect::Reflect;
4use std::error::Error;
5use toml_edit::ImDocument;
6
7use crate::CVarError;
8
9use super::{ConfigLoader, DocumentContext};
10
11impl ConfigLoader {
12 pub fn apply_asset(
14 &self,
15 world: &mut World,
16 asset: Handle<CVarConfig>,
17 ) -> Result<(), CVarError> {
18 let document = world
19 .resource::<Assets<CVarConfig>>()
20 .get(&asset)
21 .unwrap()
22 .clone()
23 .0;
24
25 self.apply(world, document)?;
26
27 Ok(())
28 }
29}
30
31#[derive(Asset, Reflect, Clone)]
33pub struct CVarConfig(#[reflect(ignore)] pub(super) DocumentContext<String>);
34
35#[derive(Default)]
37pub struct ConfigAssetLoader {}
38
39impl AssetLoader for ConfigAssetLoader {
40 type Asset = CVarConfig;
41
42 type Settings = ();
43
44 type Error = Box<dyn Error + Send + Sync + 'static>;
45
46 async fn load(
47 &self,
48 reader: &mut dyn bevy_asset::io::Reader,
49 _: &Self::Settings,
50 load_context: &mut bevy_asset::LoadContext<'_>,
51 ) -> Result<Self::Asset, Self::Error> {
52 let mut buf = String::new();
53
54 reader.read_to_string(&mut buf).await?;
55
56 Ok(CVarConfig(DocumentContext::new(
57 ImDocument::parse(buf)?,
58 load_context.path().to_str().unwrap().to_owned(),
59 )))
60 }
61}