Skip to content

Commit 528257e

Browse files
committed
Upgrade reflection API's
1 parent 93be041 commit 528257e

File tree

8 files changed

+180
-117
lines changed

8 files changed

+180
-117
lines changed

crates/bevy_script_api/src/common/bevy/mod.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ use bevy::{
77
world::{Command, EntityRef, World},
88
},
99
prelude::{
10-
AppTypeRegistry, BuildWorldChildren, Children, DespawnChildrenRecursive, DespawnRecursive,
10+
AppTypeRegistry, BuildChildren, Children, DespawnChildrenRecursive, DespawnRecursive,
1111
Entity, Parent, ReflectComponent, ReflectDefault, ReflectResource,
1212
},
1313
reflect::{
14-
DynamicArray, DynamicEnum, DynamicList, DynamicMap, DynamicStruct, DynamicTuple,
15-
DynamicTupleStruct, TypeRegistration,
14+
DynamicArray, DynamicEnum, DynamicList, DynamicMap, DynamicSet, DynamicStruct,
15+
DynamicTuple, DynamicTupleStruct, TypeRegistration,
1616
},
1717
};
1818
use bevy_mod_scripting_core::{prelude::ScriptError, world::WorldPointer};
@@ -169,35 +169,35 @@ impl ScriptWorld {
169169

170170
pub fn push_child(&self, parent: Entity, child: Entity) {
171171
let mut w = self.write();
172-
if let Some(mut entity) = w.get_entity_mut(parent) {
173-
entity.push_children(&[child]);
172+
if let Ok(mut entity) = w.get_entity_mut(parent) {
173+
entity.add_children(&[child]);
174174
}
175175
}
176176

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

180-
if let Some(mut entity) = w.get_entity_mut(parent) {
180+
if let Ok(mut entity) = w.get_entity_mut(parent) {
181181
entity.remove_children(children);
182182
}
183183
}
184184

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

188-
if let Some(mut entity) = w.get_entity_mut(parent) {
188+
if let Ok(mut entity) = w.get_entity_mut(parent) {
189189
entity.insert_children(index, children);
190190
}
191191
}
192192

193193
pub fn despawn_children_recursive(&self, entity: Entity) {
194194
let mut w = self.write();
195-
DespawnChildrenRecursive { entity }.apply(&mut w);
195+
DespawnChildrenRecursive { entity, warn: true }.apply(&mut w);
196196
}
197197

198198
pub fn despawn_recursive(&self, entity: Entity) {
199199
let mut w = self.write();
200-
DespawnRecursive { entity }.apply(&mut w);
200+
DespawnRecursive { entity, warn: true }.apply(&mut w);
201201
}
202202

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

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

230230
let component_data = comp_type.data::<ReflectComponent>().ok_or_else(|| {
231231
ScriptError::Other(format!("Not a component {}", comp_type.short_name()))
@@ -243,13 +243,14 @@ impl ScriptWorld {
243243
bevy::reflect::TypeInfo::List(_) => component_data.insert(&mut entity_ref, &DynamicList::default(), &registry_lock),
244244
bevy::reflect::TypeInfo::Array(_) => component_data.insert(&mut entity_ref, &DynamicArray::new(Box::new([])), &registry_lock),
245245
bevy::reflect::TypeInfo::Map(_) => component_data.insert(&mut entity_ref, &DynamicMap::default(), &registry_lock),
246-
bevy::reflect::TypeInfo::Value(_) => component_data.insert(&mut entity_ref,
246+
bevy::reflect::TypeInfo::Set(set_info) => component_data.insert(&mut entity_ref, &DynamicSet::default(), &registry_lock),
247+
bevy::reflect::TypeInfo::Opaque(_) => component_data.insert(&mut entity_ref,
247248
comp_type.data::<ReflectDefault>().ok_or_else(||
248249
ScriptError::Other(format!("Component {} is a value or dynamic type with no `ReflectDefault` type_data, cannot instantiate sensible value",comp_type.short_name())))?
249250
.default()
250-
.as_ref(),
251+
.as_partial_reflect(),
251252
&registry_lock),
252-
bevy::reflect::TypeInfo::Enum(_) => component_data.insert(&mut entity_ref, &DynamicEnum::default(), &registry_lock)
253+
bevy::reflect::TypeInfo::Enum(_) => component_data.insert(&mut entity_ref, &DynamicEnum::default(), &registry_lock),
253254
};
254255
// if we do not drop the lock here, line below will complain registry is still borrowed at drop
255256
drop(registry_lock);
@@ -273,7 +274,7 @@ impl ScriptWorld {
273274

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

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

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

301302
Ok(component_data.reflect(entity_ref).is_some())
302303
}
@@ -310,7 +311,7 @@ impl ScriptWorld {
310311

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

315316
let component_data = comp_type.data::<ReflectComponent>().ok_or_else(|| {
316317
ScriptError::Other(format!("Not a component {}", comp_type.short_name()))

crates/bevy_script_api/src/common/std.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::marker::PhantomData;
22

3-
use bevy::reflect::{FromReflect, GetTypeRegistration, TypePath};
3+
use bevy::reflect::{FromReflect, GetTypeRegistration, TypePath, Typed};
44

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

@@ -26,7 +26,7 @@ impl<T> std::fmt::Debug for ScriptVec<T> {
2626
}
2727
}
2828

29-
impl<T: std::fmt::Display + FromReflect + GetTypeRegistration + TypePath> std::fmt::Display
29+
impl<T: std::fmt::Display + FromReflect + GetTypeRegistration + TypePath + Typed> std::fmt::Display
3030
for ScriptVec<T>
3131
{
3232
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -41,7 +41,7 @@ impl<T: std::fmt::Display + FromReflect + GetTypeRegistration + TypePath> std::f
4141
}
4242
}
4343

44-
impl<T: FromReflect + TypePath + GetTypeRegistration> ScriptVec<T> {
44+
impl<T: FromReflect + TypePath + GetTypeRegistration + Typed> ScriptVec<T> {
4545
pub fn new_ref(ref_: ReflectReference) -> Self {
4646
Self {
4747
ref_,
@@ -118,7 +118,7 @@ impl<T: FromReflect> Iterator for ScriptVecIterator<T> {
118118
}
119119
}
120120

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

124124
type IntoIter = ScriptVecIterator<T>;

crates/bevy_script_api/src/lua/bevy/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::lua::{
99
};
1010
use crate::providers::bevy_ecs::LuaEntity;
1111
use crate::{impl_from_lua_with_clone, impl_tealr_type};
12-
use bevy::hierarchy::BuildWorldChildren;
12+
use bevy::hierarchy::BuildChildren;
1313
use bevy::prelude::{AppTypeRegistry, ReflectResource};
1414
use bevy_mod_scripting_core::prelude::*;
1515
use bevy_mod_scripting_lua::{prelude::IntoLua, tealr};
@@ -288,8 +288,8 @@ impl TealData for LuaWorld {
288288
.map(|e| e.inner())
289289
.collect::<Result<Vec<_>, _>>()?;
290290

291-
if let Some(mut entity) = w.get_entity_mut(parent.inner()?) {
292-
entity.push_children(&children);
291+
if let Ok(mut entity) = w.get_entity_mut(parent.inner()?) {
292+
entity.add_children(&children);
293293
}
294294

295295
Ok(())

crates/bevy_script_api/src/lua/std.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ impl<
106106
+ GetTypeRegistration
107107
+ TypePath
108108
+ for<'a> FromLuaProxy<'a>
109-
+ Clone,
109+
+ Clone
110+
+ bevy::reflect::Typed,
110111
> LuaProxyable for Option<T>
111112
{
112113
fn ref_to_lua(self_: ReflectReference, lua: &Lua) -> mlua::Result<Value> {
@@ -246,6 +247,7 @@ impl<
246247
+ GetTypeRegistration
247248
+ TypePath
248249
+ LuaProxyable
250+
+ bevy::reflect::Typed
249251
+ for<'a> FromLuaProxy<'a>
250252
+ for<'a> IntoLuaProxy<'a>
251253
+ std::fmt::Debug,
@@ -274,6 +276,7 @@ impl<
274276
+ GetTypeRegistration
275277
+ TypePath
276278
+ LuaProxyable
279+
+ bevy::reflect::Typed
277280
+ for<'a> FromLuaProxy<'a>
278281
+ for<'a> IntoLuaProxy<'a>
279282
+ std::fmt::Debug,
@@ -294,6 +297,7 @@ impl<
294297
+ GetTypeRegistration
295298
+ TypePath
296299
+ LuaProxyable
300+
+ bevy::reflect::Typed
297301
+ for<'a> FromLuaProxy<'a>
298302
+ for<'a> IntoLuaProxy<'a>,
299303
> TealData for LuaVec<T>
@@ -384,6 +388,7 @@ impl<
384388
+ GetTypeRegistration
385389
+ TypePath
386390
+ LuaProxyable
391+
+ bevy::reflect::Typed
387392
+ for<'a> FromLuaProxy<'a>
388393
+ for<'a> IntoLuaProxy<'a>
389394
+ std::fmt::Debug,
@@ -446,6 +451,7 @@ impl<
446451
+ GetTypeRegistration
447452
+ TypePath
448453
+ LuaProxyable
454+
+ bevy::reflect::Typed
449455
+ std::fmt::Debug,
450456
> FromLuaProxy<'lua> for Vec<T>
451457
{

crates/bevy_script_api/src/lua/util.rs

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,12 @@ macro_rules! impl_tealr_generic{
287287

288288
}
289289

290+
impl ::bevy::reflect::Typed for $name {
291+
fn type_info() -> &'static ::bevy::reflect::TypeInfo {
292+
panic!("This should never be called, I am a dummy implementation")
293+
}
294+
}
295+
290296
impl ::bevy::reflect::TypePath for $name {
291297
fn short_type_path() -> &'static str{
292298
panic!("This should never be called, I am a dummy implementation")
@@ -297,69 +303,94 @@ macro_rules! impl_tealr_generic{
297303
}
298304
}
299305

300-
impl ::bevy::reflect::Reflect for $name {
301306

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

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

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

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

318-
fn as_reflect(&self) -> &dyn ::bevy::reflect::Reflect {
324+
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)>> {
319325
panic!("This should never be called, I am a dummy implementation");
320326
}
321327

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

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

330-
fn set(&mut self, _: Box<dyn ::bevy::reflect::Reflect>) -> Result<(), Box<dyn ::bevy::reflect::Reflect>> {
336+
fn try_apply(&mut self, value: &dyn ::bevy::prelude::PartialReflect) -> std::result::Result<(), ::bevy::reflect::ApplyError> {
331337
panic!("This should never be called, I am a dummy implementation");
332338
}
333339

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

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

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

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

350-
fn reflect_owned(self: Box<Self>) -> ::bevy::reflect::ReflectOwned {
357+
358+
impl ::bevy::reflect::Reflect for $name {
359+
360+
fn into_any(self: Box<Self>) -> Box<dyn std::any::Any> {
351361
panic!("This should never be called, I am a dummy implementation");
352362
}
353363

354-
fn get_represented_type_info(&self) -> std::option::Option<&'static bevy::reflect::TypeInfo> {
364+
fn as_any(&self) -> &dyn std::any::Any {
365+
panic!("This should never be called, I am a dummy implementation");
366+
}
367+
368+
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
369+
panic!("This should never be called, I am a dummy implementation");
370+
}
371+
372+
fn as_reflect(&self) -> &dyn ::bevy::reflect::Reflect {
373+
panic!("This should never be called, I am a dummy implementation");
374+
}
375+
376+
fn as_reflect_mut(&mut self) -> &mut dyn ::bevy::reflect::Reflect {
377+
panic!("This should never be called, I am a dummy implementation");
378+
}
379+
380+
fn set(&mut self, _: Box<dyn ::bevy::reflect::Reflect>) -> Result<(), Box<dyn ::bevy::reflect::Reflect>> {
381+
panic!("This should never be called, I am a dummy implementation");
382+
}
383+
384+
fn into_reflect(self: Box<Self>) -> Box<dyn ::bevy::reflect::Reflect> {
355385
panic!("This should never be called, I am a dummy implementation");
356386
}
357387
}
358388

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

365396
impl ::bevy::reflect::GetTypeRegistration for $name {

crates/bevy_script_api/src/rhai/std.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ impl<
121121
+ TypePath
122122
+ Clone
123123
+ FromRhaiProxy
124+
+ bevy::reflect::Typed
124125
+ GetTypeRegistration,
125126
> RhaiProxyable for Option<T>
126127
{
@@ -249,11 +250,24 @@ impl<T: ToRhaiProxy> ToRhaiProxy for Option<T> {
249250

250251
/// Composite trait composing the various traits required for a type `T` to be used as part of a RhaiVec<T>
251252
pub trait RhaiVecElem:
252-
FromReflect + GetTypeRegistration + TypePath + RhaiProxyable + FromRhaiProxy + Clone
253+
FromReflect
254+
+ GetTypeRegistration
255+
+ TypePath
256+
+ RhaiProxyable
257+
+ FromRhaiProxy
258+
+ Clone
259+
+ bevy::reflect::Typed
253260
{
254261
}
255-
impl<T: FromReflect + GetTypeRegistration + TypePath + RhaiProxyable + FromRhaiProxy + Clone>
256-
RhaiVecElem for T
262+
impl<
263+
T: FromReflect
264+
+ GetTypeRegistration
265+
+ TypePath
266+
+ RhaiProxyable
267+
+ FromRhaiProxy
268+
+ Clone
269+
+ bevy::reflect::Typed,
270+
> RhaiVecElem for T
257271
{
258272
}
259273

0 commit comments

Comments
 (0)