Skip to content

Commit

Permalink
upgraded to bevy 0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
raffaeleragni committed Feb 28, 2024
1 parent 4a34bb0 commit 9df343f
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 43 deletions.
10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_sync"
version = "0.12.11"
version = "0.13.0"
edition = "2021"
authors = ["Raffaele Ragni <raffaele.ragni@gmail.com>"]
license = "MIT OR Apache-2.0"
Expand All @@ -16,9 +16,9 @@ include = ["/src", "/LICENSE-MIT", "/LICENSE-APACHE"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
bevy = { version = "0.12" }
wgpu-types = { version = "0.17", features = ["serde", "replay", "trace"] }
bevy_renet = "0.0.10"
bevy = { version = "0.13" }
wgpu-types = { version = "0.19", features = ["serde", "replay", "trace"] }
bevy_renet = "0.0.11"
bincode = "1.3.3"
serde = { version = "1.0.160", features = ["derive"] }

Expand All @@ -31,5 +31,5 @@ lz4-compress = "0.1.1"

[dev-dependencies]
serial_test = "2.0.0"
bevy_editor_pls = "0.6"
bevy_editor_pls = "0.8"
uuid = "1.5.0"
4 changes: 2 additions & 2 deletions examples/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ fn load_world(
) {
commands.spawn((
PbrBundle {
mesh: meshes.addu(shape::Plane::from_size(5.0).into()),
mesh: meshes.addu(Plane3d::default().mesh().size(5.0, 5.0).into()),
material: materials.addu(Color::rgb(0.3, 0.5, 0.3).into()),
..default()
},
Expand All @@ -72,7 +72,7 @@ fn load_world(
));
commands.spawn((
PbrBundle {
mesh: meshes.addu(Mesh::from(shape::Cube { size: 1.0 })),
mesh: meshes.addu(Mesh::from(Cuboid::new(1.0, 1.0, 1.0))),
material: materials.addu(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
Expand Down
12 changes: 6 additions & 6 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ impl Plugin for ClientSyncPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<SyncTrackerRes>();

app.add_state::<ClientState>();
app.init_state::<ClientState>();

app.add_systems(
Update,
client_connected.run_if(state_exists_and_equals(ClientState::Connecting)),
client_connected.run_if(in_state(ClientState::Connecting)),
);
app.add_systems(
Update,
client_connecting.run_if(state_exists_and_equals(ClientState::Disconnected)),
client_connecting.run_if(in_state(ClientState::Disconnected)),
);
app.add_systems(
Update,
client_disconnected
.run_if(state_exists_and_equals(ClientState::Disconnected))
.run_if(in_state(ClientState::Disconnected))
.run_if(resource_removed::<NetcodeClientTransport>()),
);

Expand All @@ -53,8 +53,8 @@ impl Plugin for ClientSyncPlugin {
receiver::poll_for_messages,
)
.chain()
.run_if(resource_exists::<RenetClient>())
.run_if(state_exists_and_equals(ClientState::Connected)),
.run_if(resource_exists::<RenetClient>)
.run_if(in_state(ClientState::Connected)),
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/client/track.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ pub(crate) fn entity_parented_on_client(
query_parent: Query<(Entity, &SyncUp), With<Children>>,
) {
for (p, sup) in query.iter() {
let Ok(parent) = query_parent.get_component::<SyncUp>(p.get()) else {
let Ok(parent) = query_parent.get(p.get()) else {
continue;
};
client.send_message(
DefaultChannel::ReliableOrdered,
bincode::serialize(&Message::EntityParented {
server_entity_id: sup.server_entity_id,
server_parent_id: parent.server_entity_id,
server_parent_id: parent.1.server_entity_id,
})
.unwrap(),
);
Expand Down
2 changes: 1 addition & 1 deletion src/lib_priv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl SyncTrackerRes {
.pushed_component_from_network
.insert(ComponentChangeId { id: e_id, name });
let entity = &mut world.entity_mut(e_id);
reflect_component.apply_or_insert(entity, component_data.as_reflect());
reflect_component.apply_or_insert(entity, component_data.as_reflect(), &registry);
true
} else {
debug!(
Expand Down
3 changes: 2 additions & 1 deletion src/networking/assets/image_serde.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::{
prelude::*,
render::render_resource::{Extent3d, TextureDimension, TextureFormat},
render::{render_resource::{Extent3d, TextureDimension, TextureFormat}, render_asset::RenderAssetUsages},
};
use lz4_compress::{compress, decompress};
use serde::{Deserialize, Serialize};
Expand All @@ -23,6 +23,7 @@ pub(crate) fn bin_to_image(bin: &[u8]) -> Option<Image> {
dimension,
img.data,
img.format,
RenderAssetUsages::RENDER_WORLD | RenderAssetUsages::MAIN_WORLD,
))
}

Expand Down
36 changes: 26 additions & 10 deletions src/networking/assets/mesh_serde.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bevy::render::mesh::Indices;
use bevy::render::mesh::VertexAttributeValues::{Float32x2, Float32x3, Float32x4, Uint16x4};
use bevy::render::render_asset::RenderAssetUsages;
use bevy::{prelude::*, render::render_resource::PrimitiveTopology};
use lz4_compress::{compress, decompress};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -117,7 +118,10 @@ pub(crate) fn mesh_to_bin(mesh: &Mesh) -> Vec<u8> {
pub(crate) fn bin_to_mesh(binary: &[u8]) -> Mesh {
let binary = decompress(binary).unwrap();
let Ok(data) = bincode::deserialize::<MeshData>(&binary) else {
return Mesh::new(PrimitiveTopology::TriangleList);
return Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD,
);
};

let mesh_type_enum = match data.mesh_type {
Expand All @@ -129,7 +133,10 @@ pub(crate) fn bin_to_mesh(binary: &[u8]) -> Mesh {
_ => PrimitiveTopology::TriangleList,
};

let mut mesh = Mesh::new(mesh_type_enum);
let mut mesh = Mesh::new(
mesh_type_enum,
RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD,
);

if let Some(positions) = data.positions {
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, positions);
Expand Down Expand Up @@ -164,10 +171,10 @@ pub(crate) fn bin_to_mesh(binary: &[u8]) -> Mesh {
}

if let Some(indices) = data.indices32 {
mesh.set_indices(Some(Indices::U32(indices)));
mesh.insert_indices(Indices::U32(indices));
}
if let Some(indices) = data.indices16 {
mesh.set_indices(Some(Indices::U16(indices)));
mesh.insert_indices(Indices::U16(indices));
}

//if let Some(morph_targets) = data.morph_targets {
Expand Down Expand Up @@ -362,7 +369,10 @@ mod test {
}

fn sample_mesh() -> Mesh {
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
let mut mesh = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::RENDER_WORLD | RenderAssetUsages::MAIN_WORLD,
);
mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![[0., 0., 0.], [1., 2., 1.], [2., 0., 0.]],
Expand All @@ -377,13 +387,16 @@ mod test {
Mesh::ATTRIBUTE_JOINT_INDEX,
Uint16x4(vec![[1u16, 2, 3, 4]; 4]),
);
mesh.set_indices(Some(Indices::U32(vec![0, 2, 1])));
mesh.insert_indices(Indices::U32(vec![0, 2, 1]));
mesh.set_morph_target_names(vec!["name1".into(), "name2".into()]);
mesh
}

fn sample_mesh_idx16() -> Mesh {
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
let mut mesh = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::RENDER_WORLD | RenderAssetUsages::MAIN_WORLD,
);
mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![[0., 0., 0.], [1., 2., 1.], [2., 0., 0.]],
Expand All @@ -398,21 +411,24 @@ mod test {
Mesh::ATTRIBUTE_JOINT_INDEX,
Uint16x4(vec![[1u16, 2, 3, 4]; 4]),
);
mesh.set_indices(Some(Indices::U16(vec![0, 2, 1])));
mesh.insert_indices(Indices::U16(vec![0, 2, 1]));
mesh.set_morph_target_names(vec!["name1".into(), "name2".into()]);
mesh
}

fn sample_mesh_no_tangents() -> Mesh {
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
let mut mesh = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::RENDER_WORLD | RenderAssetUsages::MAIN_WORLD,
);
mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![[0., 0., 0.], [1., 2., 1.], [2., 0., 0.]],
);
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, vec![[0., 1., 0.]; 3]);
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, vec![[0., 0.]; 3]);
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_1, vec![[1., 1.]; 3]);
mesh.set_indices(Some(Indices::U32(vec![0, 2, 1])));
mesh.insert_indices(Indices::U32(vec![0, 2, 1]));
mesh
}
}
4 changes: 2 additions & 2 deletions src/networking/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ pub(crate) fn init(app: &mut App, addr: IpAddr, port: u16, max_transfer: usize)
app.insert_resource(http_transfer);
app.add_systems(
Update,
process_mesh_assets.run_if(resource_exists::<SyncAssetTransfer>()),
process_mesh_assets.run_if(resource_exists::<SyncAssetTransfer>),
);
app.add_systems(
Update,
process_image_assets.run_if(resource_exists::<SyncAssetTransfer>()),
process_image_assets.run_if(resource_exists::<SyncAssetTransfer>),
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/server/initial_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ fn check_entity_components(world: &World, result: &mut Vec<Message>) -> Result<(
.filter(|arch| arch.contains(sync_down_id))
{
for arch_entity in arch.entities() {
let entity = world.entity(arch_entity.entity());
let entity = world.entity(arch_entity.id());
let e_id = entity.id();
if !entity_ids_sent.contains(&e_id) {
result.push(Message::EntitySpawn { id: e_id });
Expand Down Expand Up @@ -87,7 +87,7 @@ fn check_entity_components(world: &World, result: &mut Vec<Message>) -> Result<(
.data::<ReflectComponent>()
.ok_or("missing #[reflect(Component)]")?;
for arch_entity in arch.entities() {
let entity = world.entity(arch_entity.entity());
let entity = world.entity(arch_entity.id());
let e_id = entity.id();
let component = reflect_component.reflect(entity).ok_or("not registered")?;
let Ok(compo_bin) = reflect_to_bin(component.as_reflect(), &registry) else {
Expand Down Expand Up @@ -122,7 +122,7 @@ fn check_parents(world: &World, result: &mut Vec<Message>) -> Result<(), Box<dyn
.filter(|&c_id| c_id == parent_component_id)
{
for arch_entity in arch.entities() {
let entity = world.entity(arch_entity.entity());
let entity = world.entity(arch_entity.id());
let e_id = entity.id();
let Some(parent) = entity.get::<Parent>() else {
continue;
Expand Down
16 changes: 8 additions & 8 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ impl Plugin for ServerSyncPlugin {
fn build(&self, app: &mut App) {
app.init_resource::<SyncTrackerRes>();

app.add_state::<ServerState>();
app.init_state::<ServerState>();
app.add_systems(
Update,
server_connected
.run_if(state_exists_and_equals(ServerState::Disconnected))
.run_if(resource_added::<NetcodeServerTransport>()),
.run_if(in_state(ServerState::Disconnected))
.run_if(resource_added::<NetcodeServerTransport>),
);
app.add_systems(
Update,
server_disconnected
.run_if(state_exists_and_equals(ServerState::Connected))
.run_if(in_state(ServerState::Connected))
.run_if(resource_removed::<NetcodeServerTransport>()),
);

Expand All @@ -57,15 +57,15 @@ impl Plugin for ServerSyncPlugin {
react_on_changed_meshes.run_if(sync_mesh_enabled),
)
.chain()
.run_if(resource_exists::<RenetServer>())
.run_if(state_exists_and_equals(ServerState::Connected)),
.run_if(resource_exists::<RenetServer>)
.run_if(in_state(ServerState::Connected)),
);
app.add_systems(
Update,
(client_connected, receiver::poll_for_messages)
.chain()
.run_if(resource_exists::<RenetServer>())
.run_if(state_exists_and_equals(ServerState::Connected)),
.run_if(resource_exists::<RenetServer>)
.run_if(in_state(ServerState::Connected)),
);
}
}
Expand Down
9 changes: 6 additions & 3 deletions tests/setup/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy::{
pbr::PbrPlugin,
prelude::*,
reflect::{DynamicTypePath, FromReflect, GetTypeRegistration, Reflect},
render::{mesh::Indices, render_resource::PrimitiveTopology},
render::{mesh::Indices, render_asset::RenderAssetUsages, render_resource::PrimitiveTopology},
transform::TransformBundle,
utils::Uuid,
MinimalPlugins,
Expand Down Expand Up @@ -263,15 +263,18 @@ pub(crate) fn spawn_new_mesh_nouuid(app: &mut App) -> Handle<Mesh> {

#[allow(dead_code)]
pub(crate) fn sample_mesh() -> Mesh {
let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
let mut mesh = Mesh::new(
PrimitiveTopology::TriangleList,
RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD,
);
mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![[0., 0., 0.], [1., 2., 1.], [2., 0., 0.]],
);
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, vec![[0., 1., 0.]; 3]);
mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, vec![[0., 0.]; 3]);
mesh.insert_attribute(Mesh::ATTRIBUTE_TANGENT, vec![[0., 1., 0., 0.]; 3]);
mesh.set_indices(Some(Indices::U32(vec![0, 2, 1])));
mesh.insert_indices(Indices::U32(vec![0, 2, 1]));

mesh
}
Expand Down

0 comments on commit 9df343f

Please sign in to comment.