use core::any::TypeId;
use anyhow::{anyhow, Result as AnyhowResult};
use bevy_ecs::{
component::ComponentId,
entity::Entity,
event::EventCursor,
query::QueryBuilder,
reflect::{AppTypeRegistry, ReflectComponent},
removal_detection::RemovedComponentEntity,
system::{In, Local},
world::{EntityRef, EntityWorldMut, FilteredEntityRef, World},
};
use bevy_hierarchy::BuildChildren as _;
use bevy_reflect::{
serde::{ReflectSerializer, TypedReflectDeserializer},
PartialReflect, TypeRegistration, TypeRegistry,
};
use bevy_utils::HashMap;
use serde::{de::DeserializeSeed as _, Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::{error_codes, BrpError, BrpResult};
pub const BRP_GET_METHOD: &str = "bevy/get";
pub const BRP_QUERY_METHOD: &str = "bevy/query";
pub const BRP_SPAWN_METHOD: &str = "bevy/spawn";
pub const BRP_INSERT_METHOD: &str = "bevy/insert";
pub const BRP_REMOVE_METHOD: &str = "bevy/remove";
pub const BRP_DESTROY_METHOD: &str = "bevy/destroy";
pub const BRP_REPARENT_METHOD: &str = "bevy/reparent";
pub const BRP_LIST_METHOD: &str = "bevy/list";
pub const BRP_GET_AND_WATCH_METHOD: &str = "bevy/get+watch";
pub const BRP_LIST_AND_WATCH_METHOD: &str = "bevy/list+watch";
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BrpGetParams {
pub entity: Entity,
pub components: Vec<String>,
#[serde(default)]
pub strict: bool,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BrpQueryParams {
pub data: BrpQuery,
#[serde(default)]
pub filter: BrpQueryFilter,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BrpSpawnParams {
pub components: HashMap<String, Value>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BrpDestroyParams {
pub entity: Entity,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BrpRemoveParams {
pub entity: Entity,
pub components: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BrpInsertParams {
pub entity: Entity,
pub components: HashMap<String, Value>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BrpReparentParams {
pub entities: Vec<Entity>,
#[serde(default)]
pub parent: Option<Entity>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BrpListParams {
pub entity: Entity,
}
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct BrpQuery {
#[serde(default)]
pub components: Vec<String>,
#[serde(default)]
pub option: Vec<String>,
#[serde(default)]
pub has: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct BrpQueryFilter {
#[serde(default)]
pub without: Vec<String>,
#[serde(default)]
pub with: Vec<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BrpSpawnResponse {
pub entity: Entity,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum BrpGetResponse {
Lenient {
components: HashMap<String, Value>,
errors: HashMap<String, Value>,
},
Strict(HashMap<String, Value>),
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum BrpGetWatchingResponse {
Lenient {
components: HashMap<String, Value>,
removed: Vec<String>,
errors: HashMap<String, Value>,
},
Strict {
components: HashMap<String, Value>,
removed: Vec<String>,
},
}
pub type BrpListResponse = Vec<String>;
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct BrpListWatchingResponse {
added: Vec<String>,
removed: Vec<String>,
}
pub type BrpQueryResponse = Vec<BrpQueryRow>;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BrpQueryRow {
pub entity: Entity,
pub components: HashMap<String, Value>,
#[serde(skip_serializing_if = "HashMap::is_empty", default)]
pub has: HashMap<String, Value>,
}
fn parse<T: for<'de> Deserialize<'de>>(value: Value) -> Result<T, BrpError> {
serde_json::from_value(value).map_err(|err| BrpError {
code: error_codes::INVALID_PARAMS,
message: err.to_string(),
data: None,
})
}
fn parse_some<T: for<'de> Deserialize<'de>>(value: Option<Value>) -> Result<T, BrpError> {
match value {
Some(value) => parse(value),
None => Err(BrpError {
code: error_codes::INVALID_PARAMS,
message: String::from("Params not provided"),
data: None,
}),
}
}
pub fn process_remote_get_request(In(params): In<Option<Value>>, world: &World) -> BrpResult {
let BrpGetParams {
entity,
components,
strict,
} = parse_some(params)?;
let app_type_registry = world.resource::<AppTypeRegistry>();
let type_registry = app_type_registry.read();
let entity_ref = get_entity(world, entity)?;
let response =
reflect_components_to_response(components, strict, entity, entity_ref, &type_registry)?;
serde_json::to_value(response).map_err(BrpError::internal)
}
pub fn process_remote_get_watching_request(
In(params): In<Option<Value>>,
world: &World,
mut removal_cursors: Local<HashMap<ComponentId, EventCursor<RemovedComponentEntity>>>,
) -> BrpResult<Option<Value>> {
let BrpGetParams {
entity,
components,
strict,
} = parse_some(params)?;
let app_type_registry = world.resource::<AppTypeRegistry>();
let type_registry = app_type_registry.read();
let entity_ref = get_entity(world, entity)?;
let mut changed = Vec::new();
let mut removed = Vec::new();
let mut errors = HashMap::new();
'component_loop: for component_path in components {
let Ok(type_registration) =
get_component_type_registration(&type_registry, &component_path)
else {
let err =
BrpError::component_error(format!("Unknown component type: `{component_path}`"));
if strict {
return Err(err);
}
errors.insert(
component_path,
serde_json::to_value(err).map_err(BrpError::internal)?,
);
continue;
};
let Some(component_id) = world.components().get_id(type_registration.type_id()) else {
let err = BrpError::component_error(format!("Unknown component: `{component_path}`"));
if strict {
return Err(err);
}
errors.insert(
component_path,
serde_json::to_value(err).map_err(BrpError::internal)?,
);
continue;
};
if let Some(ticks) = entity_ref.get_change_ticks_by_id(component_id) {
if ticks.is_changed(world.last_change_tick(), world.read_change_tick()) {
changed.push(component_path);
continue;
}
};
let Some(events) = world.removed_components().get(component_id) else {
continue;
};
let cursor = removal_cursors
.entry(component_id)
.or_insert_with(|| events.get_cursor());
for event in cursor.read(events) {
if Entity::from(event.clone()) == entity {
removed.push(component_path);
continue 'component_loop;
}
}
}
if changed.is_empty() && removed.is_empty() {
return Ok(None);
}
let response =
reflect_components_to_response(changed, strict, entity, entity_ref, &type_registry)?;
let response = match response {
BrpGetResponse::Lenient {
components,
errors: mut errs,
} => BrpGetWatchingResponse::Lenient {
components,
removed,
errors: {
errs.extend(errors);
errs
},
},
BrpGetResponse::Strict(components) => BrpGetWatchingResponse::Strict {
components,
removed,
},
};
Ok(Some(
serde_json::to_value(response).map_err(BrpError::internal)?,
))
}
fn reflect_components_to_response(
components: Vec<String>,
strict: bool,
entity: Entity,
entity_ref: EntityRef,
type_registry: &TypeRegistry,
) -> BrpResult<BrpGetResponse> {
let mut response = if strict {
BrpGetResponse::Strict(Default::default())
} else {
BrpGetResponse::Lenient {
components: Default::default(),
errors: Default::default(),
}
};
for component_path in components {
match reflect_component(&component_path, entity, entity_ref, type_registry) {
Ok(serialized_object) => match response {
BrpGetResponse::Strict(ref mut components)
| BrpGetResponse::Lenient {
ref mut components, ..
} => {
components.extend(serialized_object.into_iter());
}
},
Err(err) => match response {
BrpGetResponse::Strict(_) => return Err(err),
BrpGetResponse::Lenient { ref mut errors, .. } => {
let err_value = serde_json::to_value(err).map_err(BrpError::internal)?;
errors.insert(component_path, err_value);
}
},
}
}
Ok(response)
}
fn reflect_component(
component_path: &str,
entity: Entity,
entity_ref: EntityRef,
type_registry: &TypeRegistry,
) -> BrpResult<Map<String, Value>> {
let reflect_component =
get_reflect_component(type_registry, component_path).map_err(BrpError::component_error)?;
let Some(reflected) = reflect_component.reflect(entity_ref) else {
return Err(BrpError::component_not_present(component_path, entity));
};
let reflect_serializer = ReflectSerializer::new(reflected.as_partial_reflect(), type_registry);
let Value::Object(serialized_object) =
serde_json::to_value(&reflect_serializer).map_err(|err| BrpError {
code: error_codes::COMPONENT_ERROR,
message: err.to_string(),
data: None,
})?
else {
return Err(BrpError {
code: error_codes::COMPONENT_ERROR,
message: format!("Component `{}` could not be serialized", component_path),
data: None,
});
};
Ok(serialized_object)
}
pub fn process_remote_query_request(In(params): In<Option<Value>>, world: &mut World) -> BrpResult {
let BrpQueryParams {
data: BrpQuery {
components,
option,
has,
},
filter: BrpQueryFilter { without, with },
} = parse_some(params)?;
let app_type_registry = world.resource::<AppTypeRegistry>().clone();
let type_registry = app_type_registry.read();
let components =
get_component_ids(&type_registry, world, components).map_err(BrpError::component_error)?;
let option =
get_component_ids(&type_registry, world, option).map_err(BrpError::component_error)?;
let has = get_component_ids(&type_registry, world, has).map_err(BrpError::component_error)?;
let without =
get_component_ids(&type_registry, world, without).map_err(BrpError::component_error)?;
let with = get_component_ids(&type_registry, world, with).map_err(BrpError::component_error)?;
let mut query = QueryBuilder::<FilteredEntityRef>::new(world);
for (_, component) in &components {
query.ref_id(*component);
}
for (_, option) in &option {
query.optional(|query| {
query.ref_id(*option);
});
}
for (_, has) in &has {
query.optional(|query| {
query.ref_id(*has);
});
}
for (_, without) in without {
query.without_id(without);
}
for (_, with) in with {
query.with_id(with);
}
let paths_and_reflect_components: Vec<(&str, &ReflectComponent)> = components
.into_iter()
.chain(option)
.map(|(type_id, _)| reflect_component_from_id(type_id, &type_registry))
.collect::<AnyhowResult<Vec<(&str, &ReflectComponent)>>>()
.map_err(BrpError::component_error)?;
let has_paths_and_reflect_components: Vec<(&str, &ReflectComponent)> = has
.into_iter()
.map(|(type_id, _)| reflect_component_from_id(type_id, &type_registry))
.collect::<AnyhowResult<Vec<(&str, &ReflectComponent)>>>()
.map_err(BrpError::component_error)?;
let mut response = BrpQueryResponse::default();
let mut query = query.build();
for row in query.iter(world) {
let components_map = build_components_map(
row.clone(),
paths_and_reflect_components.iter().copied(),
&type_registry,
)
.map_err(BrpError::component_error)?;
let has_map = build_has_map(
row.clone(),
has_paths_and_reflect_components.iter().copied(),
);
response.push(BrpQueryRow {
entity: row.id(),
components: components_map,
has: has_map,
});
}
serde_json::to_value(response).map_err(BrpError::internal)
}
pub fn process_remote_spawn_request(In(params): In<Option<Value>>, world: &mut World) -> BrpResult {
let BrpSpawnParams { components } = parse_some(params)?;
let app_type_registry = world.resource::<AppTypeRegistry>().clone();
let type_registry = app_type_registry.read();
let reflect_components =
deserialize_components(&type_registry, components).map_err(BrpError::component_error)?;
let entity = world.spawn_empty();
let entity_id = entity.id();
insert_reflected_components(&type_registry, entity, reflect_components)
.map_err(BrpError::component_error)?;
let response = BrpSpawnResponse { entity: entity_id };
serde_json::to_value(response).map_err(BrpError::internal)
}
pub fn process_remote_insert_request(
In(params): In<Option<Value>>,
world: &mut World,
) -> BrpResult {
let BrpInsertParams { entity, components } = parse_some(params)?;
let app_type_registry = world.resource::<AppTypeRegistry>().clone();
let type_registry = app_type_registry.read();
let reflect_components =
deserialize_components(&type_registry, components).map_err(BrpError::component_error)?;
insert_reflected_components(
&type_registry,
get_entity_mut(world, entity)?,
reflect_components,
)
.map_err(BrpError::component_error)?;
Ok(Value::Null)
}
pub fn process_remote_remove_request(
In(params): In<Option<Value>>,
world: &mut World,
) -> BrpResult {
let BrpRemoveParams { entity, components } = parse_some(params)?;
let app_type_registry = world.resource::<AppTypeRegistry>().clone();
let type_registry = app_type_registry.read();
let component_ids =
get_component_ids(&type_registry, world, components).map_err(BrpError::component_error)?;
let mut entity_world_mut = get_entity_mut(world, entity)?;
for (_, component_id) in component_ids {
entity_world_mut.remove_by_id(component_id);
}
Ok(Value::Null)
}
pub fn process_remote_destroy_request(
In(params): In<Option<Value>>,
world: &mut World,
) -> BrpResult {
let BrpDestroyParams { entity } = parse_some(params)?;
get_entity_mut(world, entity)?.despawn();
Ok(Value::Null)
}
pub fn process_remote_reparent_request(
In(params): In<Option<Value>>,
world: &mut World,
) -> BrpResult {
let BrpReparentParams {
entities,
parent: maybe_parent,
} = parse_some(params)?;
if let Some(parent) = maybe_parent {
let mut parent_commands =
get_entity_mut(world, parent).map_err(|_| BrpError::entity_not_found(parent))?;
for entity in entities {
if entity == parent {
return Err(BrpError::self_reparent(entity));
}
parent_commands.add_child(entity);
}
}
else {
for entity in entities {
get_entity_mut(world, entity)?.remove_parent();
}
}
Ok(Value::Null)
}
pub fn process_remote_list_request(In(params): In<Option<Value>>, world: &World) -> BrpResult {
let app_type_registry = world.resource::<AppTypeRegistry>();
let type_registry = app_type_registry.read();
let mut response = BrpListResponse::default();
if let Some(BrpListParams { entity }) = params.map(parse).transpose()? {
let entity = get_entity(world, entity)?;
for component_id in entity.archetype().components() {
let Some(component_info) = world.components().get_info(component_id) else {
continue;
};
response.push(component_info.name().to_owned());
}
}
else {
for registered_type in type_registry.iter() {
if registered_type.data::<ReflectComponent>().is_some() {
response.push(registered_type.type_info().type_path().to_owned());
}
}
}
response.sort();
serde_json::to_value(response).map_err(BrpError::internal)
}
pub fn process_remote_list_watching_request(
In(params): In<Option<Value>>,
world: &World,
mut removal_cursors: Local<HashMap<ComponentId, EventCursor<RemovedComponentEntity>>>,
) -> BrpResult<Option<Value>> {
let BrpListParams { entity } = parse_some(params)?;
let entity_ref = get_entity(world, entity)?;
let mut response = BrpListWatchingResponse::default();
for component_id in entity_ref.archetype().components() {
let ticks = entity_ref
.get_change_ticks_by_id(component_id)
.ok_or(BrpError::internal("Failed to get ticks"))?;
if ticks.is_added(world.last_change_tick(), world.read_change_tick()) {
let Some(component_info) = world.components().get_info(component_id) else {
continue;
};
response.added.push(component_info.name().to_owned());
}
}
for (component_id, events) in world.removed_components().iter() {
let cursor = removal_cursors
.entry(*component_id)
.or_insert_with(|| events.get_cursor());
for event in cursor.read(events) {
if Entity::from(event.clone()) == entity {
let Some(component_info) = world.components().get_info(*component_id) else {
continue;
};
response.removed.push(component_info.name().to_owned());
}
}
}
if response.added.is_empty() && response.removed.is_empty() {
Ok(None)
} else {
Ok(Some(
serde_json::to_value(response).map_err(BrpError::internal)?,
))
}
}
fn get_entity(world: &World, entity: Entity) -> Result<EntityRef<'_>, BrpError> {
world
.get_entity(entity)
.map_err(|_| BrpError::entity_not_found(entity))
}
fn get_entity_mut(world: &mut World, entity: Entity) -> Result<EntityWorldMut<'_>, BrpError> {
world
.get_entity_mut(entity)
.map_err(|_| BrpError::entity_not_found(entity))
}
fn get_component_ids(
type_registry: &TypeRegistry,
world: &World,
component_paths: Vec<String>,
) -> AnyhowResult<Vec<(TypeId, ComponentId)>> {
let mut component_ids = vec![];
for component_path in component_paths {
let type_id = get_component_type_registration(type_registry, &component_path)?.type_id();
let Some(component_id) = world.components().get_id(type_id) else {
return Err(anyhow!(
"Component `{}` isn't used in the world",
component_path
));
};
component_ids.push((type_id, component_id));
}
Ok(component_ids)
}
fn build_components_map<'a>(
entity_ref: FilteredEntityRef,
paths_and_reflect_components: impl Iterator<Item = (&'a str, &'a ReflectComponent)>,
type_registry: &TypeRegistry,
) -> AnyhowResult<HashMap<String, Value>> {
let mut serialized_components_map = HashMap::new();
for (type_path, reflect_component) in paths_and_reflect_components {
let Some(reflected) = reflect_component.reflect(entity_ref.clone()) else {
continue;
};
let reflect_serializer =
ReflectSerializer::new(reflected.as_partial_reflect(), type_registry);
let Value::Object(serialized_object) = serde_json::to_value(&reflect_serializer)? else {
return Err(anyhow!("Component `{}` could not be serialized", type_path));
};
serialized_components_map.extend(serialized_object.into_iter());
}
Ok(serialized_components_map)
}
fn build_has_map<'a>(
entity_ref: FilteredEntityRef,
paths_and_reflect_components: impl Iterator<Item = (&'a str, &'a ReflectComponent)>,
) -> HashMap<String, Value> {
let mut has_map = HashMap::new();
for (type_path, reflect_component) in paths_and_reflect_components {
let has = reflect_component.contains(entity_ref.clone());
has_map.insert(type_path.to_owned(), Value::Bool(has));
}
has_map
}
fn reflect_component_from_id(
component_type_id: TypeId,
type_registry: &TypeRegistry,
) -> AnyhowResult<(&str, &ReflectComponent)> {
let Some(type_registration) = type_registry.get(component_type_id) else {
return Err(anyhow!(
"Component `{:?}` isn't registered",
component_type_id
));
};
let type_path = type_registration.type_info().type_path();
let Some(reflect_component) = type_registration.data::<ReflectComponent>() else {
return Err(anyhow!("Component `{}` isn't reflectable", type_path));
};
Ok((type_path, reflect_component))
}
fn deserialize_components(
type_registry: &TypeRegistry,
components: HashMap<String, Value>,
) -> AnyhowResult<Vec<Box<dyn PartialReflect>>> {
let mut reflect_components = vec![];
for (component_path, component) in components {
let Some(component_type) = type_registry.get_with_type_path(&component_path) else {
return Err(anyhow!("Unknown component type: `{}`", component_path));
};
let reflected: Box<dyn PartialReflect> =
TypedReflectDeserializer::new(component_type, type_registry)
.deserialize(&component)
.unwrap();
reflect_components.push(reflected);
}
Ok(reflect_components)
}
fn insert_reflected_components(
type_registry: &TypeRegistry,
mut entity_world_mut: EntityWorldMut,
reflect_components: Vec<Box<dyn PartialReflect>>,
) -> AnyhowResult<()> {
for reflected in reflect_components {
let reflect_component =
get_reflect_component(type_registry, reflected.reflect_type_path())?;
reflect_component.insert(&mut entity_world_mut, &*reflected, type_registry);
}
Ok(())
}
fn get_reflect_component<'r>(
type_registry: &'r TypeRegistry,
component_path: &str,
) -> AnyhowResult<&'r ReflectComponent> {
let component_registration = get_component_type_registration(type_registry, component_path)?;
component_registration
.data::<ReflectComponent>()
.ok_or_else(|| anyhow!("Component `{}` isn't reflectable", component_path))
}
fn get_component_type_registration<'r>(
type_registry: &'r TypeRegistry,
component_path: &str,
) -> AnyhowResult<&'r TypeRegistration> {
type_registry
.get_with_type_path(component_path)
.ok_or_else(|| anyhow!("Unknown component type: `{}`", component_path))
}