[Fixes #6030] Bevy scene optional serde (#6076)

# Objective

Fixes #6030, making ``serde`` optional.

## Solution

This was solved by making a ``serialize`` feature that can activate ``serde``, which is now optional. 

When ``serialize`` is deactivated, the ``Plugin`` implementation for ``ScenePlugin`` does nothing.


Co-authored-by: Linus Käll <linus.kall.business@gmail.com>
This commit is contained in:
Zhell 2022-11-14 23:08:22 +00:00
parent f2f8f9097f
commit af2a199254
5 changed files with 25 additions and 4 deletions

View file

@ -45,7 +45,7 @@ wav = ["bevy_audio/wav"]
# Enable watching file system for asset hot reload
filesystem_watcher = ["bevy_asset/filesystem_watcher"]
serialize = ["bevy_core/serialize", "bevy_input/serialize", "bevy_time/serialize", "bevy_window/serialize", "bevy_transform/serialize", "bevy_math/serialize"]
serialize = ["bevy_core/serialize", "bevy_input/serialize", "bevy_time/serialize", "bevy_window/serialize", "bevy_transform/serialize", "bevy_math/serialize", "bevy_scene/serialize"]
# Display server protocol support (X11 is enabled by default)
wayland = ["bevy_winit/wayland"]

View file

@ -8,6 +8,10 @@ repository = "https://github.com/bevyengine/bevy"
license = "MIT OR Apache-2.0"
keywords = ["bevy"]
[features]
default = ["serialize"]
serialize = ["dep:serde", "uuid/serde"]
[dependencies]
# bevy
bevy_app = { path = "../bevy_app", version = "0.9.0" }
@ -21,9 +25,9 @@ bevy_utils = { path = "../bevy_utils", version = "0.9.0" }
bevy_render = { path = "../bevy_render", version = "0.9.0" }
# other
serde = { version = "1.0", features = ["derive"] }
serde = { version = "1.0", features = ["derive"], optional = true }
ron = "0.8.0"
uuid = { version = "1.1", features = ["v4", "serde"] }
uuid = { version = "1.1", features = ["v4"] }
anyhow = "1.0.4"
thiserror = "1.0"

View file

@ -1,4 +1,4 @@
use crate::{serde::SceneSerializer, DynamicSceneBuilder, Scene, SceneSpawnError};
use crate::{DynamicSceneBuilder, Scene, SceneSpawnError};
use anyhow::Result;
use bevy_app::AppTypeRegistry;
use bevy_ecs::{
@ -7,6 +7,10 @@ use bevy_ecs::{
world::World,
};
use bevy_reflect::{Reflect, TypeRegistryArc, TypeUuid};
#[cfg(feature = "serialize")]
use crate::serde::SceneSerializer;
#[cfg(feature = "serialize")]
use serde::Serialize;
/// A collection of serializable dynamic entities, each with its own run-time defined set of components.
@ -116,12 +120,14 @@ impl DynamicScene {
// TODO: move to AssetSaver when it is implemented
/// Serialize this dynamic scene into rust object notation (ron).
#[cfg(feature = "serialize")]
pub fn serialize_ron(&self, registry: &TypeRegistryArc) -> Result<String, ron::Error> {
serialize_ron(SceneSerializer::new(self, registry))
}
}
/// Serialize a given Rust data structure into rust object notation (ron).
#[cfg(feature = "serialize")]
pub fn serialize_ron<S>(serialize: S) -> Result<String, ron::Error>
where
S: Serialize,

View file

@ -4,6 +4,8 @@ mod dynamic_scene_builder;
mod scene;
mod scene_loader;
mod scene_spawner;
#[cfg(feature = "serialize")]
pub mod serde;
pub use bundle::*;
@ -27,6 +29,7 @@ use bevy_ecs::prelude::*;
#[derive(Default)]
pub struct ScenePlugin;
#[cfg(feature = "serialize")]
impl Plugin for ScenePlugin {
fn build(&self, app: &mut App) {
app.add_asset::<DynamicScene>()
@ -38,3 +41,8 @@ impl Plugin for ScenePlugin {
.add_system_to_stage(CoreStage::PreUpdate, scene_spawner);
}
}
#[cfg(not(feature = "serialize"))]
impl Plugin for ScenePlugin {
fn build(&self, _: &mut App) {}
}

View file

@ -5,6 +5,8 @@ use bevy_asset::{AssetLoader, LoadContext, LoadedAsset};
use bevy_ecs::world::{FromWorld, World};
use bevy_reflect::TypeRegistryArc;
use bevy_utils::BoxedFuture;
#[cfg(feature = "serialize")]
use serde::de::DeserializeSeed;
#[derive(Debug)]
@ -21,6 +23,7 @@ impl FromWorld for SceneLoader {
}
}
#[cfg(feature = "serialize")]
impl AssetLoader for SceneLoader {
fn load<'a>(
&'a self,