From 42ae72256c772732f6c99ea35fcd2cce89a83004 Mon Sep 17 00:00:00 2001 From: Piotr Siuszko Date: Sat, 7 Jun 2025 12:38:21 +0200 Subject: [PATCH] Cleanup, init resource --- crates/bevy_remote/src/builtin_methods.rs | 16 +++------ crates/bevy_remote/src/lib.rs | 1 + crates/bevy_remote/src/schemas/json_schema.rs | 34 +++++++++++-------- crates/bevy_remote/src/schemas/mod.rs | 19 +++++++---- 4 files changed, 36 insertions(+), 34 deletions(-) diff --git a/crates/bevy_remote/src/builtin_methods.rs b/crates/bevy_remote/src/builtin_methods.rs index a21cb3d0a4..5f6be9904e 100644 --- a/crates/bevy_remote/src/builtin_methods.rs +++ b/crates/bevy_remote/src/builtin_methods.rs @@ -24,7 +24,10 @@ use serde_json::{Map, Value}; use crate::{ error_codes, - schemas::{json_schema::{export_type, JsonSchemaBevyType}, open_rpc::OpenRpcDocument}, + schemas::{ + json_schema::{export_type, JsonSchemaBevyType}, + open_rpc::OpenRpcDocument, + }, BrpError, BrpResult, }; @@ -1229,17 +1232,6 @@ pub fn export_registry_types(In(params): In>, world: &World) -> Br let schemas = types .iter() .filter(|type_reg| { - // EXTRA FILTER, not decided yet if gonna make it in the end - match type_reg.type_info() { - bevy_reflect::TypeInfo::Tuple(_) => return false, - bevy_reflect::TypeInfo::TupleStruct(_) => return false, - bevy_reflect::TypeInfo::List(_) => return false, - bevy_reflect::TypeInfo::Array(_) => return false, - bevy_reflect::TypeInfo::Map(_) => return false, - bevy_reflect::TypeInfo::Set(_) => return false, - _ => {} - } - let path_table = type_reg.type_info().type_path_table(); if let Some(crate_name) = &path_table.crate_name() { if !filter.with_crates.is_empty() diff --git a/crates/bevy_remote/src/lib.rs b/crates/bevy_remote/src/lib.rs index 97b2e453e7..cce512d34b 100644 --- a/crates/bevy_remote/src/lib.rs +++ b/crates/bevy_remote/src/lib.rs @@ -539,6 +539,7 @@ impl Plugin for RemotePlugin { .insert_after(Last, RemoteLast); app.insert_resource(remote_methods) + .init_resource::() .init_resource::() .add_systems(PreStartup, setup_mailbox_channel) .configure_sets( diff --git a/crates/bevy_remote/src/schemas/json_schema.rs b/crates/bevy_remote/src/schemas/json_schema.rs index 4114357653..922bffb184 100644 --- a/crates/bevy_remote/src/schemas/json_schema.rs +++ b/crates/bevy_remote/src/schemas/json_schema.rs @@ -2,7 +2,8 @@ //! It tries to follow this standard: use bevy_platform::collections::HashMap; use bevy_reflect::{ - GetTypeRegistration, NamedField, OpaqueInfo, TypeInfo, TypeRegistration, TypeRegistry, VariantInfo + GetTypeRegistration, NamedField, OpaqueInfo, TypeInfo, TypeRegistration, TypeRegistry, + VariantInfo, }; use core::any::TypeId; use serde::{Deserialize, Serialize}; @@ -10,19 +11,16 @@ use serde_json::{json, Map, Value}; use crate::schemas::SchemaTypesMetadata; - /// Helper trait for converting TypeRegistration to JsonSchemaBevyType pub trait TypeRegistrySchemaReader { - /// Export type JSON Schema with definitions. - /// It can be useful for generating schemas for assets validation. + /// Export type JSON Schema. fn export_type_json_schema( &self, extra_info: &SchemaTypesMetadata, ) -> Option { self.export_type_json_schema_for_id(extra_info, T::get_type_registration().type_id()) } - /// Export type JSON Schema with definitions. - /// It can be useful for generating schemas for assets validation. + /// Export type JSON Schema. fn export_type_json_schema_for_id( &self, extra_info: &SchemaTypesMetadata, @@ -37,13 +35,19 @@ impl TypeRegistrySchemaReader for TypeRegistry { type_id: TypeId, ) -> Option { let type_reg = self.get(type_id)?; - Some((type_reg,extra_info).into()) + Some((type_reg, extra_info).into()) } } /// Exports schema info for a given type -pub fn export_type(reg: &TypeRegistration, metadata: &SchemaTypesMetadata) -> (String, JsonSchemaBevyType) { - (reg.type_info().type_path().to_owned(), (reg,metadata).into()) +pub fn export_type( + reg: &TypeRegistration, + metadata: &SchemaTypesMetadata, +) -> (String, JsonSchemaBevyType) { + ( + reg.type_info().type_path().to_owned(), + (reg, metadata).into(), + ) } impl From<(&TypeRegistration, &SchemaTypesMetadata)> for JsonSchemaBevyType { @@ -365,11 +369,11 @@ impl SchemaJsonReference for &NamedField { #[cfg(test)] mod tests { use super::*; + use bevy_ecs::prelude::ReflectComponent; use bevy_ecs::prelude::ReflectResource; use bevy_ecs::{component::Component, reflect::AppTypeRegistry, resource::Resource}; use bevy_reflect::prelude::ReflectDefault; - use bevy_ecs::prelude::ReflectComponent; - use bevy_reflect::{Reflect,ReflectSerialize, ReflectDeserialize}; + use bevy_reflect::{Reflect, ReflectDeserialize, ReflectSerialize}; #[test] fn reflect_export_struct() { @@ -435,7 +439,7 @@ mod tests { .get(TypeId::of::()) .expect("SHOULD BE REGISTERED") .clone(); - let (_, schema) = export_type(&foo_registration,&SchemaTypesMetadata::default()); + let (_, schema) = export_type(&foo_registration, &SchemaTypesMetadata::default()); assert!( schema.reflect_types.contains(&"Component".to_owned()), "Should be a component" @@ -470,7 +474,7 @@ mod tests { .get(TypeId::of::()) .expect("SHOULD BE REGISTERED") .clone(); - let (_, schema) = export_type(&foo_registration,&SchemaTypesMetadata::default()); + let (_, schema) = export_type(&foo_registration, &SchemaTypesMetadata::default()); assert!( !schema.reflect_types.contains(&"Component".to_owned()), "Should not be a component" @@ -558,12 +562,12 @@ mod tests { assert_normalized_values(schema_as_value, value); } - fn assert_normalized_values(one: Value, two: Value){ + fn assert_normalized_values(one: Value, two: Value) { let mut one = one.clone(); normalize_json(&mut one); let mut two = two.clone(); normalize_json(&mut two); - assert_eq!(one,two); + assert_eq!(one, two); /// Recursively sorts arrays in a serde_json::Value fn normalize_json(value: &mut Value) { diff --git a/crates/bevy_remote/src/schemas/mod.rs b/crates/bevy_remote/src/schemas/mod.rs index 64dae17a3a..ba1ea449b3 100644 --- a/crates/bevy_remote/src/schemas/mod.rs +++ b/crates/bevy_remote/src/schemas/mod.rs @@ -1,17 +1,22 @@ //! Module with schemas used for various BRP endpoints -use std::any::TypeId; use bevy_asset::{ReflectAsset, ReflectHandle}; -use bevy_ecs::{reflect::{ReflectComponent, ReflectResource}, resource::Resource}; +use bevy_ecs::{ + reflect::{ReflectComponent, ReflectResource}, + resource::Resource, +}; use bevy_platform::collections::HashMap; -use bevy_reflect::{prelude::ReflectDefault, Reflect, ReflectDeserialize, ReflectSerialize, TypeData, TypeRegistration}; +use bevy_reflect::{ + prelude::ReflectDefault, Reflect, ReflectDeserialize, ReflectSerialize, TypeData, + TypeRegistration, +}; +use std::any::TypeId; pub mod json_schema; pub mod open_rpc; - -/// Holds mapping of reflect data types to strings, -/// later on used in Bevy Json Schema. +/// Holds mapping of reflect data types to strings, +/// later on used in Bevy Json Schema. #[derive(Debug, Resource, Reflect)] #[reflect(Resource)] pub struct SchemaTypesMetadata { @@ -48,4 +53,4 @@ impl SchemaTypesMetadata { .flat_map(|(id, name)| reg.data_by_id(*id).and(Some(name.clone()))) .collect() } -} \ No newline at end of file +}