> Note: This is rebased off #4561 and can be viewed as a competitor to that PR. See `Comparison with #4561` section for details. # Objective The current serialization format used by `bevy_reflect` is both verbose and error-prone. Taking the following structs[^1] for example: ```rust // -- src/inventory.rs #[derive(Reflect)] struct Inventory { id: String, max_storage: usize, items: Vec<Item> } #[derive(Reflect)] struct Item { name: String } ``` Given an inventory of a single item, this would serialize to something like: ```rust // -- assets/inventory.ron { "type": "my_game::inventory::Inventory", "struct": { "id": { "type": "alloc::string::String", "value": "inv001", }, "max_storage": { "type": "usize", "value": 10 }, "items": { "type": "alloc::vec::Vec<alloc::string::String>", "list": [ { "type": "my_game::inventory::Item", "struct": { "name": { "type": "alloc::string::String", "value": "Pickaxe" }, }, }, ], }, }, } ``` Aside from being really long and difficult to read, it also has a few "gotchas" that users need to be aware of if they want to edit the file manually. A major one is the requirement that you use the proper keys for a given type. For structs, you need `"struct"`. For lists, `"list"`. For tuple structs, `"tuple_struct"`. And so on. It also ***requires*** that the `"type"` entry come before the actual data. Despite being a map— which in programming is almost always orderless by default— the entries need to be in a particular order. Failure to follow the ordering convention results in a failure to deserialize the data. This makes it very prone to errors and annoyances. ## Solution Using #4042, we can remove a lot of the boilerplate and metadata needed by this older system. Since we now have static access to type information, we can simplify our serialized data to look like: ```rust // -- assets/inventory.ron { "my_game::inventory::Inventory": ( id: "inv001", max_storage: 10, items: [ ( name: "Pickaxe" ), ], ), } ``` This is much more digestible and a lot less error-prone (no more key requirements and no more extra type names). Additionally, it is a lot more familiar to users as it follows conventional serde mechanics. For example, the struct is represented with `(...)` when serialized to RON. #### Custom Serialization Additionally, this PR adds the opt-in ability to specify a custom serde implementation to be used rather than the one created via reflection. For example[^1]: ```rust // -- src/inventory.rs #[derive(Reflect, Serialize)] #[reflect(Serialize)] struct Item { #[serde(alias = "id")] name: String } ``` ```rust // -- assets/inventory.ron { "my_game::inventory::Inventory": ( id: "inv001", max_storage: 10, items: [ ( id: "Pickaxe" ), ], ), }, ``` By allowing users to define their own serialization methods, we do two things: 1. We give more control over how data is serialized/deserialized to the end user 2. We avoid having to re-define serde's attributes and forcing users to apply both (e.g. we don't need a `#[reflect(alias)]` attribute). ### Improved Formats One of the improvements this PR provides is the ability to represent data in ways that are more conventional and/or familiar to users. Many users are familiar with RON so here are some of the ways we can now represent data in RON: ###### Structs ```js { "my_crate::Foo": ( bar: 123 ) } // OR { "my_crate::Foo": Foo( bar: 123 ) } ``` <details> <summary>Old Format</summary> ```js { "type": "my_crate::Foo", "struct": { "bar": { "type": "usize", "value": 123 } } } ``` </details> ###### Tuples ```js { "(f32, f32)": (1.0, 2.0) } ``` <details> <summary>Old Format</summary> ```js { "type": "(f32, f32)", "tuple": [ { "type": "f32", "value": 1.0 }, { "type": "f32", "value": 2.0 } ] } ``` </details> ###### Tuple Structs ```js { "my_crate::Bar": ("Hello World!") } // OR { "my_crate::Bar": Bar("Hello World!") } ``` <details> <summary>Old Format</summary> ```js { "type": "my_crate::Bar", "tuple_struct": [ { "type": "alloc::string::String", "value": "Hello World!" } ] } ``` </details> ###### Arrays It may be a bit surprising to some, but arrays now also use the tuple format. This is because they essentially _are_ tuples (a sequence of values with a fixed size), but only allow for homogenous types. Additionally, this is how RON handles them and is probably a result of the 32-capacity limit imposed on them (both by [serde](https://docs.rs/serde/latest/serde/trait.Serialize.html#impl-Serialize-for-%5BT%3B%2032%5D) and by [bevy_reflect](https://docs.rs/bevy/latest/bevy/reflect/trait.GetTypeRegistration.html#impl-GetTypeRegistration-for-%5BT%3B%2032%5D)). ```js { "[i32; 3]": (1, 2, 3) } ``` <details> <summary>Old Format</summary> ```js { "type": "[i32; 3]", "array": [ { "type": "i32", "value": 1 }, { "type": "i32", "value": 2 }, { "type": "i32", "value": 3 } ] } ``` </details> ###### Enums To make things simple, I'll just put a struct variant here, but the style applies to all variant types: ```js { "my_crate::ItemType": Consumable( name: "Healing potion" ) } ``` <details> <summary>Old Format</summary> ```js { "type": "my_crate::ItemType", "enum": { "variant": "Consumable", "struct": { "name": { "type": "alloc::string::String", "value": "Healing potion" } } } } ``` </details> ### Comparison with #4561 This PR is a rebased version of #4561. The reason for the split between the two is because this PR creates a _very_ different scene format. You may notice that the PR descriptions for either PR are pretty similar. This was done to better convey the changes depending on which (if any) gets merged first. If #4561 makes it in first, I will update this PR description accordingly. --- ## Changelog * Re-worked serialization/deserialization for reflected types * Added `TypedReflectDeserializer` for deserializing data with known `TypeInfo` * Renamed `ReflectDeserializer` to `UntypedReflectDeserializer` * ~~Replaced usages of `deserialize_any` with `deserialize_map` for non-self-describing formats~~ Reverted this change since there are still some issues that need to be sorted out (in a separate PR). By reverting this, crates like `bincode` can throw an error when attempting to deserialize non-self-describing formats (`bincode` results in `DeserializeAnyNotSupported`) * Structs, tuples, tuple structs, arrays, and enums are now all de/serialized using conventional serde methods ## Migration Guide * This PR reduces the verbosity of the scene format. Scenes will need to be updated accordingly: ```js // Old format { "type": "my_game::item::Item", "struct": { "id": { "type": "alloc::string::String", "value": "bevycraft:stone", }, "tags": { "type": "alloc::vec::Vec<alloc::string::String>", "list": [ { "type": "alloc::string::String", "value": "material" }, ], }, } // New format { "my_game::item::Item": ( id: "bevycraft:stone", tags: ["material"] ) } ``` [^1]: Some derives omitted for brevity.
248 lines
5.4 KiB
Rust
248 lines
5.4 KiB
Rust
use crate as bevy_reflect;
|
|
use crate::prelude::ReflectDefault;
|
|
use crate::reflect::Reflect;
|
|
use crate::{ReflectDeserialize, ReflectSerialize};
|
|
use bevy_reflect_derive::{impl_from_reflect_value, impl_reflect_struct, impl_reflect_value};
|
|
use glam::*;
|
|
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct IVec2 {
|
|
x: i32,
|
|
y: i32,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct IVec3 {
|
|
x: i32,
|
|
y: i32,
|
|
z: i32,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct IVec4 {
|
|
x: i32,
|
|
y: i32,
|
|
z: i32,
|
|
w: i32,
|
|
}
|
|
);
|
|
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct UVec2 {
|
|
x: u32,
|
|
y: u32,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct UVec3 {
|
|
x: u32,
|
|
y: u32,
|
|
z: u32,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct UVec4 {
|
|
x: u32,
|
|
y: u32,
|
|
z: u32,
|
|
w: u32,
|
|
}
|
|
);
|
|
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct Vec2 {
|
|
x: f32,
|
|
y: f32,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct Vec3 {
|
|
x: f32,
|
|
y: f32,
|
|
z: f32,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct Vec3A {
|
|
x: f32,
|
|
y: f32,
|
|
z: f32,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct Vec4 {
|
|
x: f32,
|
|
y: f32,
|
|
z: f32,
|
|
w: f32,
|
|
}
|
|
);
|
|
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct BVec2 {
|
|
x: bool,
|
|
y: bool,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct BVec3 {
|
|
x: bool,
|
|
y: bool,
|
|
z: bool,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct BVec4 {
|
|
x: bool,
|
|
y: bool,
|
|
z: bool,
|
|
w: bool,
|
|
}
|
|
);
|
|
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct DVec2 {
|
|
x: f64,
|
|
y: f64,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct DVec3 {
|
|
x: f64,
|
|
y: f64,
|
|
z: f64,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Default)]
|
|
struct DVec4 {
|
|
x: f64,
|
|
y: f64,
|
|
z: f64,
|
|
w: f64,
|
|
}
|
|
);
|
|
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
|
|
struct Mat2 {
|
|
x_axis: Vec2,
|
|
y_axis: Vec2,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
|
|
struct Mat3 {
|
|
x_axis: Vec3,
|
|
y_axis: Vec3,
|
|
z_axis: Vec3,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
|
|
struct Mat3A {
|
|
x_axis: Vec3A,
|
|
y_axis: Vec3A,
|
|
z_axis: Vec3A,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
|
|
struct Mat4 {
|
|
x_axis: Vec4,
|
|
y_axis: Vec4,
|
|
z_axis: Vec4,
|
|
w_axis: Vec4,
|
|
}
|
|
);
|
|
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
|
|
struct DMat2 {
|
|
x_axis: DVec2,
|
|
y_axis: DVec2,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
|
|
struct DMat3 {
|
|
x_axis: DVec3,
|
|
y_axis: DVec3,
|
|
z_axis: DVec3,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
|
|
struct DMat4 {
|
|
x_axis: DVec4,
|
|
y_axis: DVec4,
|
|
z_axis: DVec4,
|
|
w_axis: DVec4,
|
|
}
|
|
);
|
|
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
|
|
struct Affine2 {
|
|
matrix2: Mat2,
|
|
translation: Vec2,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
|
|
struct Affine3A {
|
|
matrix3: Mat3A,
|
|
translation: Vec3A,
|
|
}
|
|
);
|
|
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
|
|
struct DAffine2 {
|
|
matrix2: DMat2,
|
|
translation: DVec2,
|
|
}
|
|
);
|
|
impl_reflect_struct!(
|
|
#[reflect(Debug, PartialEq, Serialize, Deserialize, Default)]
|
|
struct DAffine3 {
|
|
matrix3: DMat3,
|
|
translation: DVec3,
|
|
}
|
|
);
|
|
|
|
// Quat fields are read-only (as of now), and reflection is currently missing
|
|
// mechanisms for read-only fields. I doubt those mechanisms would be added,
|
|
// so for now quaternions will remain as values. They are represented identically
|
|
// to Vec4 and DVec4, so you may use those instead and convert between.
|
|
impl_reflect_value!(Quat(Debug, PartialEq, Serialize, Deserialize, Default));
|
|
impl_reflect_value!(DQuat(Debug, PartialEq, Serialize, Deserialize, Default));
|
|
|
|
impl_from_reflect_value!(Quat);
|
|
impl_from_reflect_value!(DQuat);
|
|
|
|
impl_reflect_value!(EulerRot(Debug, Default));
|
|
|
|
// glam type aliases these to the non simd versions when there is no support (this breaks wasm builds for example)
|
|
// ideally it shouldn't do that and there's an issue on glam for this
|
|
// https://github.com/bitshifter/glam-rs/issues/306
|
|
#[cfg(any(target_feature = "sse2", target_feature = "simd128"))]
|
|
impl_reflect_value!(BVec3A(Debug, PartialEq, Default));
|
|
#[cfg(any(target_feature = "sse2", target_feature = "simd128"))]
|
|
impl_reflect_value!(BVec4A(Debug, PartialEq, Default));
|