bevy_remote/schemas/
mod.rs1use bevy_ecs::{
3 reflect::{ReflectComponent, ReflectEvent, ReflectResource},
4 resource::Resource,
5};
6use bevy_platform::collections::HashMap;
7use bevy_reflect::{
8 prelude::ReflectDefault, Reflect, ReflectDeserialize, ReflectSerialize, TypeData,
9 TypeRegistration,
10};
11use core::any::TypeId;
12
13pub mod json_schema;
14pub mod open_rpc;
15
16#[derive(Debug, Resource, Reflect)]
19#[reflect(Resource)]
20pub struct SchemaTypesMetadata {
21 pub type_data_map: HashMap<TypeId, String>,
23}
24
25impl Default for SchemaTypesMetadata {
26 fn default() -> Self {
27 let mut data_types = Self {
28 type_data_map: Default::default(),
29 };
30 data_types.map_type_data::<ReflectComponent>("Component");
31 data_types.map_type_data::<ReflectResource>("Resource");
32 data_types.map_type_data::<ReflectEvent>("Event");
33 data_types.map_type_data::<ReflectDefault>("Default");
34 #[cfg(feature = "bevy_asset")]
35 data_types.map_type_data::<bevy_asset::ReflectAsset>("Asset");
36 #[cfg(feature = "bevy_asset")]
37 data_types.map_type_data::<bevy_asset::ReflectHandle>("AssetHandle");
38 data_types.map_type_data::<ReflectSerialize>("Serialize");
39 data_types.map_type_data::<ReflectDeserialize>("Deserialize");
40 data_types
41 }
42}
43
44impl SchemaTypesMetadata {
45 pub fn map_type_data<T: TypeData>(&mut self, name: impl Into<String>) {
47 self.type_data_map.insert(TypeId::of::<T>(), name.into());
48 }
49
50 pub fn get_registered_reflect_types(&self, reg: &TypeRegistration) -> Vec<String> {
52 self.type_data_map
53 .iter()
54 .filter_map(|(id, name)| reg.data_by_id(*id).and(Some(name.clone())))
55 .collect()
56 }
57
58 pub fn has_type_data<T: TypeData>(&self, types_string_slice: &[String]) -> bool {
60 self.has_type_data_by_id(TypeId::of::<T>(), types_string_slice)
61 }
62
63 pub fn has_type_data_by_id(&self, id: TypeId, types_string_slice: &[String]) -> bool {
65 self.type_data_map
66 .get(&id)
67 .is_some_and(|data_s| types_string_slice.iter().any(|e| e.eq(data_s)))
68 }
69}