Skip to content

Commit

Permalink
Upgrade reflection API's
Browse files Browse the repository at this point in the history
  • Loading branch information
makspll committed Nov 5, 2024
1 parent 5d63890 commit 263f412
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 117 deletions.
33 changes: 17 additions & 16 deletions crates/bevy_script_api/src/common/bevy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use bevy::{
world::{Command, EntityRef, World},
},
prelude::{
AppTypeRegistry, BuildWorldChildren, Children, DespawnChildrenRecursive, DespawnRecursive,
AppTypeRegistry, BuildChildren, Children, DespawnChildrenRecursive, DespawnRecursive,
Entity, Parent, ReflectComponent, ReflectDefault, ReflectResource,
},
reflect::{
DynamicArray, DynamicEnum, DynamicList, DynamicMap, DynamicStruct, DynamicTuple,
DynamicTupleStruct, TypeRegistration,
DynamicArray, DynamicEnum, DynamicList, DynamicMap, DynamicSet, DynamicStruct,
DynamicTuple, DynamicTupleStruct, TypeRegistration,
},
};
use bevy_mod_scripting_core::{prelude::ScriptError, world::WorldPointer};
Expand Down Expand Up @@ -169,35 +169,35 @@ impl ScriptWorld {

pub fn push_child(&self, parent: Entity, child: Entity) {
let mut w = self.write();
if let Some(mut entity) = w.get_entity_mut(parent) {
entity.push_children(&[child]);
if let Ok(mut entity) = w.get_entity_mut(parent) {
entity.add_children(&[child]);
}
}

pub fn remove_children(&self, parent: Entity, children: &[Entity]) {
let mut w = self.write();

if let Some(mut entity) = w.get_entity_mut(parent) {
if let Ok(mut entity) = w.get_entity_mut(parent) {
entity.remove_children(children);
}
}

pub fn insert_children(&self, parent: Entity, index: usize, children: &[Entity]) {
let mut w = self.write();

if let Some(mut entity) = w.get_entity_mut(parent) {
if let Ok(mut entity) = w.get_entity_mut(parent) {
entity.insert_children(index, children);
}
}

pub fn despawn_children_recursive(&self, entity: Entity) {
let mut w = self.write();
DespawnChildrenRecursive { entity }.apply(&mut w);
DespawnChildrenRecursive { entity, warn: true }.apply(&mut w);
}

pub fn despawn_recursive(&self, entity: Entity) {
let mut w = self.write();
DespawnRecursive { entity }.apply(&mut w);
DespawnRecursive { entity, warn: true }.apply(&mut w);
}

pub fn get_type_by_name(&self, type_name: &str) -> Option<ScriptTypeRegistration> {
Expand Down Expand Up @@ -225,7 +225,7 @@ impl ScriptWorld {

let mut entity_ref = w
.get_entity_mut(entity)
.ok_or_else(|| ScriptError::Other(format!("Entity is not valid {:#?}", entity)))?;
.map_err(|e| ScriptError::Other(format!("Entity is not valid {:#?}. {e}", entity)))?;

let component_data = comp_type.data::<ReflectComponent>().ok_or_else(|| {
ScriptError::Other(format!("Not a component {}", comp_type.short_name()))
Expand All @@ -243,13 +243,14 @@ impl ScriptWorld {
bevy::reflect::TypeInfo::List(_) => component_data.insert(&mut entity_ref, &DynamicList::default(), &registry_lock),
bevy::reflect::TypeInfo::Array(_) => component_data.insert(&mut entity_ref, &DynamicArray::new(Box::new([])), &registry_lock),
bevy::reflect::TypeInfo::Map(_) => component_data.insert(&mut entity_ref, &DynamicMap::default(), &registry_lock),
bevy::reflect::TypeInfo::Value(_) => component_data.insert(&mut entity_ref,
bevy::reflect::TypeInfo::Set(set_info) => component_data.insert(&mut entity_ref, &DynamicSet::default(), &registry_lock),

Check warning on line 246 in crates/bevy_script_api/src/common/bevy/mod.rs

View workflow job for this annotation

GitHub Actions / Tests

unused variable: `set_info`
bevy::reflect::TypeInfo::Opaque(_) => component_data.insert(&mut entity_ref,
comp_type.data::<ReflectDefault>().ok_or_else(||
ScriptError::Other(format!("Component {} is a value or dynamic type with no `ReflectDefault` type_data, cannot instantiate sensible value",comp_type.short_name())))?
.default()
.as_ref(),
.as_partial_reflect(),
&registry_lock),
bevy::reflect::TypeInfo::Enum(_) => component_data.insert(&mut entity_ref, &DynamicEnum::default(), &registry_lock)
bevy::reflect::TypeInfo::Enum(_) => component_data.insert(&mut entity_ref, &DynamicEnum::default(), &registry_lock),
};
// if we do not drop the lock here, line below will complain registry is still borrowed at drop
drop(registry_lock);
Expand All @@ -273,7 +274,7 @@ impl ScriptWorld {

let entity_ref = w
.get_entity(entity)
.ok_or_else(|| ScriptError::Other(format!("Entity is not valid {:#?}", entity)))?;
.map_err(|e| ScriptError::Other(format!("Entity is not valid {:#?}. {e}", entity)))?;

let component_data = comp_type.data::<ReflectComponent>().ok_or_else(|| {
ScriptError::Other(format!("Not a component {}", comp_type.short_name()))
Expand All @@ -296,7 +297,7 @@ impl ScriptWorld {

let entity_ref = w
.get_entity(entity)
.ok_or_else(|| ScriptError::Other(format!("Entity is not valid {:#?}", entity)))?;
.map_err(|e| ScriptError::Other(format!("Entity is not valid {:#?}. {e}", entity)))?;

Ok(component_data.reflect(entity_ref).is_some())
}
Expand All @@ -310,7 +311,7 @@ impl ScriptWorld {

let mut entity_ref = w
.get_entity_mut(entity)
.ok_or_else(|| ScriptError::Other(format!("Entity is not valid {:#?}", entity)))?;
.map_err(|e| ScriptError::Other(format!("Entity is not valid {:#?}. {e}", entity)))?;

let component_data = comp_type.data::<ReflectComponent>().ok_or_else(|| {
ScriptError::Other(format!("Not a component {}", comp_type.short_name()))
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_script_api/src/common/std.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::marker::PhantomData;

use bevy::reflect::{FromReflect, GetTypeRegistration, TypePath};
use bevy::reflect::{FromReflect, GetTypeRegistration, TypePath, Typed};

use crate::{error::ReflectionError, ReflectReference, ValueIndex};

Expand All @@ -26,7 +26,7 @@ impl<T> std::fmt::Debug for ScriptVec<T> {
}
}

impl<T: std::fmt::Display + FromReflect + GetTypeRegistration + TypePath> std::fmt::Display
impl<T: std::fmt::Display + FromReflect + GetTypeRegistration + TypePath + Typed> std::fmt::Display
for ScriptVec<T>
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand All @@ -41,7 +41,7 @@ impl<T: std::fmt::Display + FromReflect + GetTypeRegistration + TypePath> std::f
}
}

impl<T: FromReflect + TypePath + GetTypeRegistration> ScriptVec<T> {
impl<T: FromReflect + TypePath + GetTypeRegistration + Typed> ScriptVec<T> {
pub fn new_ref(ref_: ReflectReference) -> Self {
Self {
ref_,
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<T: FromReflect> Iterator for ScriptVecIterator<T> {
}
}

impl<T: FromReflect + TypePath + GetTypeRegistration> IntoIterator for ScriptVec<T> {
impl<T: FromReflect + TypePath + GetTypeRegistration + Typed> IntoIterator for ScriptVec<T> {
type Item = ReflectReference;

type IntoIter = ScriptVecIterator<T>;
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_script_api/src/lua/bevy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::lua::{
};
use crate::providers::bevy_ecs::LuaEntity;
use crate::{impl_from_lua_with_clone, impl_tealr_type};
use bevy::hierarchy::BuildWorldChildren;
use bevy::hierarchy::BuildChildren;
use bevy::prelude::{AppTypeRegistry, ReflectResource};
use bevy_mod_scripting_core::prelude::*;
use bevy_mod_scripting_lua::{prelude::IntoLua, tealr};
Expand Down Expand Up @@ -288,8 +288,8 @@ impl TealData for LuaWorld {
.map(|e| e.inner())
.collect::<Result<Vec<_>, _>>()?;

if let Some(mut entity) = w.get_entity_mut(parent.inner()?) {
entity.push_children(&children);
if let Ok(mut entity) = w.get_entity_mut(parent.inner()?) {
entity.add_children(&children);
}

Ok(())
Expand Down
8 changes: 7 additions & 1 deletion crates/bevy_script_api/src/lua/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ impl<
+ GetTypeRegistration
+ TypePath
+ for<'a> FromLuaProxy<'a>
+ Clone,
+ Clone
+ bevy::reflect::Typed,
> LuaProxyable for Option<T>
{
fn ref_to_lua(self_: ReflectReference, lua: &Lua) -> mlua::Result<Value> {
Expand Down Expand Up @@ -246,6 +247,7 @@ impl<
+ GetTypeRegistration
+ TypePath
+ LuaProxyable
+ bevy::reflect::Typed
+ for<'a> FromLuaProxy<'a>
+ for<'a> IntoLuaProxy<'a>
+ std::fmt::Debug,
Expand Down Expand Up @@ -274,6 +276,7 @@ impl<
+ GetTypeRegistration
+ TypePath
+ LuaProxyable
+ bevy::reflect::Typed
+ for<'a> FromLuaProxy<'a>
+ for<'a> IntoLuaProxy<'a>
+ std::fmt::Debug,
Expand All @@ -294,6 +297,7 @@ impl<
+ GetTypeRegistration
+ TypePath
+ LuaProxyable
+ bevy::reflect::Typed
+ for<'a> FromLuaProxy<'a>
+ for<'a> IntoLuaProxy<'a>,
> TealData for LuaVec<T>
Expand Down Expand Up @@ -384,6 +388,7 @@ impl<
+ GetTypeRegistration
+ TypePath
+ LuaProxyable
+ bevy::reflect::Typed
+ for<'a> FromLuaProxy<'a>
+ for<'a> IntoLuaProxy<'a>
+ std::fmt::Debug,
Expand Down Expand Up @@ -446,6 +451,7 @@ impl<
+ GetTypeRegistration
+ TypePath
+ LuaProxyable
+ bevy::reflect::Typed
+ std::fmt::Debug,
> FromLuaProxy<'lua> for Vec<T>
{
Expand Down
63 changes: 47 additions & 16 deletions crates/bevy_script_api/src/lua/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ macro_rules! impl_tealr_generic{

}

impl ::bevy::reflect::Typed for $name {
fn type_info() -> &'static ::bevy::reflect::TypeInfo {
panic!("This should never be called, I am a dummy implementation")
}
}

impl ::bevy::reflect::TypePath for $name {
fn short_type_path() -> &'static str{
panic!("This should never be called, I am a dummy implementation")
Expand All @@ -297,69 +303,94 @@ macro_rules! impl_tealr_generic{
}
}

impl ::bevy::reflect::Reflect for $name {

fn try_apply(&mut self, _: &(dyn bevy::prelude::Reflect + 'static)) -> std::result::Result<(), bevy::reflect::ApplyError> {
impl ::bevy::reflect::PartialReflect for $name {
fn get_represented_type_info(&self) -> std::option::Option<&'static bevy::reflect::TypeInfo> {
panic!("This should never be called, I am a dummy implementation");
}

fn into_any(self: Box<Self>) -> Box<dyn std::any::Any> {
fn into_partial_reflect(self: Box<Self>) -> Box<dyn ::bevy::reflect::PartialReflect> {
panic!("This should never be called, I am a dummy implementation");
}

fn as_any(&self) -> &dyn std::any::Any {
fn as_partial_reflect(&self) -> &dyn ::bevy::reflect::PartialReflect {
panic!("This should never be called, I am a dummy implementation");
}

fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
fn as_partial_reflect_mut(&mut self) -> &mut dyn ::bevy::reflect::PartialReflect {
panic!("This should never be called, I am a dummy implementation");
}

fn as_reflect(&self) -> &dyn ::bevy::reflect::Reflect {
fn try_into_reflect(self: Box<Self>) -> std::result::Result<std::boxed::Box<(dyn bevy::prelude::Reflect + 'static)>, std::boxed::Box<(dyn bevy::prelude::PartialReflect + 'static)>> {
panic!("This should never be called, I am a dummy implementation");
}

fn as_reflect_mut(&mut self) -> &mut dyn ::bevy::reflect::Reflect {
fn try_as_reflect(&self) -> std::option::Option<&(dyn bevy::prelude::Reflect + 'static)> {
panic!("This should never be called, I am a dummy implementation");
}

fn apply(&mut self, _: &dyn ::bevy::reflect::Reflect) {
fn try_as_reflect_mut(&mut self) -> std::option::Option<&mut (dyn bevy::prelude::Reflect + 'static)> {
panic!("This should never be called, I am a dummy implementation");
}

fn set(&mut self, _: Box<dyn ::bevy::reflect::Reflect>) -> Result<(), Box<dyn ::bevy::reflect::Reflect>> {
fn try_apply(&mut self, value: &dyn ::bevy::prelude::PartialReflect) -> std::result::Result<(), ::bevy::reflect::ApplyError> {

Check warning on line 336 in crates/bevy_script_api/src/lua/util.rs

View workflow job for this annotation

GitHub Actions / Tests

unused variable: `value`
panic!("This should never be called, I am a dummy implementation");
}

fn reflect_ref(&self) -> bevy::reflect::ReflectRef {
fn reflect_ref(&self) -> ::bevy::reflect::ReflectRef {
panic!("This should never be called, I am a dummy implementation");
}

fn reflect_mut(&mut self) -> bevy::reflect::ReflectMut {
fn reflect_mut(&mut self) -> ::bevy::reflect::ReflectMut {
panic!("This should never be called, I am a dummy implementation");
}

fn clone_value(&self) -> Box<dyn ::bevy::reflect::Reflect> {
fn reflect_owned(self: Box<Self>) -> ::bevy::reflect::ReflectOwned {
panic!("This should never be called, I am a dummy implementation");
}

fn into_reflect(self: Box<Self>) -> Box<dyn ::bevy::reflect::Reflect> {
fn clone_value(&self) -> Box<dyn ::bevy::prelude::PartialReflect + 'static> {
panic!("This should never be called, I am a dummy implementation");
}
}

fn reflect_owned(self: Box<Self>) -> ::bevy::reflect::ReflectOwned {

impl ::bevy::reflect::Reflect for $name {

fn into_any(self: Box<Self>) -> Box<dyn std::any::Any> {
panic!("This should never be called, I am a dummy implementation");
}

fn get_represented_type_info(&self) -> std::option::Option<&'static bevy::reflect::TypeInfo> {
fn as_any(&self) -> &dyn std::any::Any {
panic!("This should never be called, I am a dummy implementation");
}

fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
panic!("This should never be called, I am a dummy implementation");
}

fn as_reflect(&self) -> &dyn ::bevy::reflect::Reflect {
panic!("This should never be called, I am a dummy implementation");
}

fn as_reflect_mut(&mut self) -> &mut dyn ::bevy::reflect::Reflect {
panic!("This should never be called, I am a dummy implementation");
}

fn set(&mut self, _: Box<dyn ::bevy::reflect::Reflect>) -> Result<(), Box<dyn ::bevy::reflect::Reflect>> {
panic!("This should never be called, I am a dummy implementation");
}

fn into_reflect(self: Box<Self>) -> Box<dyn ::bevy::reflect::Reflect> {
panic!("This should never be called, I am a dummy implementation");
}
}

impl ::bevy::reflect::FromReflect for $name {
fn from_reflect(_: &(dyn bevy::prelude::Reflect + 'static)) -> std::option::Option<Self> {
fn from_reflect(_: &(dyn bevy::prelude::PartialReflect + 'static)) -> std::option::Option<Self> {
panic!("This should never be called, I am a dummy implementation");
}

}

impl ::bevy::reflect::GetTypeRegistration for $name {
Expand Down
20 changes: 17 additions & 3 deletions crates/bevy_script_api/src/rhai/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ impl<
+ TypePath
+ Clone
+ FromRhaiProxy
+ bevy::reflect::Typed
+ GetTypeRegistration,
> RhaiProxyable for Option<T>
{
Expand Down Expand Up @@ -249,11 +250,24 @@ impl<T: ToRhaiProxy> ToRhaiProxy for Option<T> {

/// Composite trait composing the various traits required for a type `T` to be used as part of a RhaiVec<T>
pub trait RhaiVecElem:
FromReflect + GetTypeRegistration + TypePath + RhaiProxyable + FromRhaiProxy + Clone
FromReflect
+ GetTypeRegistration
+ TypePath
+ RhaiProxyable
+ FromRhaiProxy
+ Clone
+ bevy::reflect::Typed
{
}
impl<T: FromReflect + GetTypeRegistration + TypePath + RhaiProxyable + FromRhaiProxy + Clone>
RhaiVecElem for T
impl<
T: FromReflect
+ GetTypeRegistration
+ TypePath
+ RhaiProxyable
+ FromRhaiProxy
+ Clone
+ bevy::reflect::Typed,
> RhaiVecElem for T
{
}

Expand Down
Loading

0 comments on commit 263f412

Please sign in to comment.