bevy/crates
Jakob Hellermann f867319336 add ReflectAsset and ReflectHandle (#5923)
# Objective
![image](https://user-images.githubusercontent.com/22177966/189350194-639a0211-e984-4f73-ae62-0ede44891eb9.png)

^ enable this

Concretely, I need to
- list all handle ids for an asset type
- fetch the asset as `dyn Reflect`, given a `HandleUntyped`
- when encountering a `Handle<T>`, find out what asset type that handle refers to (`T`'s type id) and turn the handle into a `HandleUntyped`

## Solution

- add `ReflectAsset` type containing function pointers for working with assets
```rust
pub struct ReflectAsset {
    type_uuid: Uuid,
    assets_resource_type_id: TypeId, // TypeId of the `Assets<T>` resource

    get: fn(&World, HandleUntyped) -> Option<&dyn Reflect>,
    get_mut: fn(&mut World, HandleUntyped) -> Option<&mut dyn Reflect>,
    get_unchecked_mut: unsafe fn(&World, HandleUntyped) -> Option<&mut dyn Reflect>,
    add: fn(&mut World, &dyn Reflect) -> HandleUntyped,
    set: fn(&mut World, HandleUntyped, &dyn Reflect) -> HandleUntyped,
    len: fn(&World) -> usize,
    ids: for<'w> fn(&'w World) -> Box<dyn Iterator<Item = HandleId> + 'w>,
    remove: fn(&mut World, HandleUntyped) -> Option<Box<dyn Reflect>>,
}
```
- add `ReflectHandle` type relating the handle back to the asset type and providing a way to create a `HandleUntyped`
```rust
pub struct ReflectHandle {
    type_uuid: Uuid,
    asset_type_id: TypeId,
    downcast_handle_untyped: fn(&dyn Any) -> Option<HandleUntyped>,
}
```
- add the corresponding `FromType` impls
- add a function `app.register_asset_reflect` which is supposed to be called after `.add_asset` and registers `ReflectAsset` and `ReflectHandle` in the type registry
---

## Changelog

- add `ReflectAsset` and `ReflectHandle` types, which allow code to use reflection to manipulate arbitrary assets without knowing their types at compile time
2022-10-28 20:42:33 +00:00
..
bevy_animation add ReflectAsset and ReflectHandle (#5923) 2022-10-28 20:42:33 +00:00
bevy_app add ReflectAsset and ReflectHandle (#5923) 2022-10-28 20:42:33 +00:00
bevy_asset add ReflectAsset and ReflectHandle (#5923) 2022-10-28 20:42:33 +00:00
bevy_audio Expose rodio's Source and Sample traits in bevy_audio (#6374) 2022-10-27 12:34:38 +00:00
bevy_core add ReflectAsset and ReflectHandle (#5923) 2022-10-28 20:42:33 +00:00
bevy_core_pipeline Support arbitrary RenderTarget texture formats (#6380) 2022-10-26 23:12:12 +00:00
bevy_derive Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_diagnostic Add Exponential Moving Average into diagnostics (#4992) 2022-10-24 13:46:37 +00:00
bevy_dylib Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_dynamic_plugin Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_ecs Clean up Fetch code (#4800) 2022-10-28 09:25:50 +00:00
bevy_ecs_compile_fail_tests fix: specify required trybuild patch version (#6333) 2022-10-25 10:21:31 +00:00
bevy_encase_derive Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_gilrs feat: add GamepadInfo, expose gamepad names (#6342) 2022-10-24 14:33:50 +00:00
bevy_gltf Prepare for upcoming rustlang by fixing upcoming clippy warnings (#6376) 2022-10-26 19:15:15 +00:00
bevy_hierarchy Add set_parent and remove_parent to EntityCommands (#6189) 2022-10-24 14:33:49 +00:00
bevy_input Derive Reflect + FromReflect for input types (#6232) 2022-10-26 19:52:20 +00:00
bevy_internal Use plugin setup for resource only used at setup time (#6360) 2022-10-25 22:19:34 +00:00
bevy_log Use plugin setup for resource only used at setup time (#6360) 2022-10-25 22:19:34 +00:00
bevy_macro_utils Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_math Add Camera::viewport_to_world (#6126) 2022-10-05 22:16:26 +00:00
bevy_mikktspace Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_pbr add ReflectAsset and ReflectHandle (#5923) 2022-10-28 20:42:33 +00:00
bevy_ptr Bump Version after Release (#5576) 2022-08-05 02:03:05 +00:00
bevy_reflect bevy_reflect: Fix DynamicScene not respecting component registrations during serialization (#6288) 2022-10-24 14:53:12 +00:00
bevy_render add ReflectAsset and ReflectHandle (#5923) 2022-10-28 20:42:33 +00:00
bevy_scene Clean up Fetch code (#4800) 2022-10-28 09:25:50 +00:00
bevy_sprite add ReflectAsset and ReflectHandle (#5923) 2022-10-28 20:42:33 +00:00
bevy_tasks Prepare for upcoming rustlang by fixing upcoming clippy warnings (#6376) 2022-10-26 19:15:15 +00:00
bevy_text Resolve most remaining execution-order ambiguities (#6341) 2022-10-27 12:56:03 +00:00
bevy_time elaborate on Timer docs (#6385) 2022-10-27 15:48:29 +00:00
bevy_transform Fix documentation for looking_at/look_at (#4696) 2022-10-12 20:18:20 +00:00
bevy_ui Resolve most remaining execution-order ambiguities (#6341) 2022-10-27 12:56:03 +00:00
bevy_utils Remove Sync bound from Local (#5483) 2022-09-12 04:15:55 +00:00
bevy_window Plugins own their settings. Rework PluginGroup trait. (#6336) 2022-10-24 21:20:33 +00:00
bevy_winit Update wgpu to 0.14.0, naga to 0.10.0, winit to 0.27.4, raw-window-handle to 0.5.0, ndk to 0.7 (#6218) 2022-10-19 17:40:23 +00:00