Skip to content

Commit

Permalink
Merge branch 'bevyengine:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
ickshonpe committed Feb 6, 2022
2 parents f024096 + b13f238 commit fa15a91
Show file tree
Hide file tree
Showing 65 changed files with 1,202 additions and 272 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ jobs:
run: sudo apt-get update; sudo apt-get install --no-install-recommends libasound2-dev libudev-dev libwayland-dev libxkbcommon-dev
- name: CI job
# See tools/ci/src/main.rs for the commands this runs
run: cargo run -p ci
run: cargo run -p ci -- nonlocal

check-benches:
runs-on: ubuntu-latest
Expand Down
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ serde = { version = "1", features = ["derive"] }
bytemuck = "1.7"
# Needed to poll Task examples
futures-lite = "1.11.3"
crossbeam-channel = "0.5.0"

[[example]]
name = "hello_world"
Expand Down Expand Up @@ -287,6 +288,10 @@ path = "examples/asset/hot_asset_reloading.rs"
name = "async_compute"
path = "examples/async_tasks/async_compute.rs"

[[example]]
name = "external_source_external_thread"
path = "examples/async_tasks/external_source_external_thread.rs"

# Audio
[[example]]
name = "audio"
Expand Down
6 changes: 5 additions & 1 deletion benches/benches/bevy_ecs/world_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const RANGE: std::ops::Range<u32> = 5..6;
fn setup<T: Component + Default>(entity_count: u32) -> World {
let mut world = World::default();
world.spawn_batch((0..entity_count).map(|_| (T::default(),)));
world
black_box(world)
}

fn world_entity(criterion: &mut Criterion) {
Expand Down Expand Up @@ -126,6 +126,7 @@ fn world_query_iter(criterion: &mut Criterion) {
for comp in query.iter(&world) {
black_box(comp);
count += 1;
black_box(count);
}
assert_eq!(black_box(count), entity_count);
});
Expand All @@ -139,6 +140,7 @@ fn world_query_iter(criterion: &mut Criterion) {
for comp in query.iter(&world) {
black_box(comp);
count += 1;
black_box(count);
}
assert_eq!(black_box(count), entity_count);
});
Expand All @@ -163,6 +165,7 @@ fn world_query_for_each(criterion: &mut Criterion) {
query.for_each(&world, |comp| {
black_box(comp);
count += 1;
black_box(count);
});
assert_eq!(black_box(count), entity_count);
});
Expand All @@ -176,6 +179,7 @@ fn world_query_for_each(criterion: &mut Criterion) {
query.for_each(&world, |comp| {
black_box(comp);
count += 1;
black_box(count);
});
assert_eq!(black_box(count), entity_count);
});
Expand Down
10 changes: 9 additions & 1 deletion crates/bevy_app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ impl App {
stage_label: impl StageLabel,
system: impl IntoSystemDescriptor<Params>,
) -> &mut Self {
use std::any::TypeId;
if stage_label.type_id() == TypeId::of::<StartupStage>() {
panic!("add systems to a startup stage using App::add_startup_system_to_stage");
}
self.schedule.add_system_to_stage(stage_label, system);
self
}
Expand Down Expand Up @@ -400,6 +404,10 @@ impl App {
stage_label: impl StageLabel,
system_set: SystemSet,
) -> &mut Self {
use std::any::TypeId;
if stage_label.type_id() == TypeId::of::<StartupStage>() {
panic!("add system sets to a startup stage using App::add_startup_system_set_to_stage");
}
self.schedule
.add_system_set_to_stage(stage_label, system_set);
self
Expand Down Expand Up @@ -915,5 +923,5 @@ fn run_once(mut app: App) {
}

/// An event that indicates the app should exit. This will fully exit the app process.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct AppExit;
3 changes: 2 additions & 1 deletion crates/bevy_asset/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ rand = "0.8.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen = { version = "0.2" }
web-sys = { version = "0.3", features = ["Request", "Window", "Response"]}
web-sys = { version = "0.3", features = ["Request", "Window", "Response"] }
wasm-bindgen-futures = "0.4"
js-sys = "0.3"

Expand All @@ -44,3 +44,4 @@ ndk-glue = { version = "0.5" }
[dev-dependencies]
futures-lite = "1.4.0"
tempfile = "3.2.0"
bevy_core = { path = "../bevy_core", version = "0.6.0" }
4 changes: 3 additions & 1 deletion crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ impl AssetServer {
loaders.push(Arc::new(loader));
}

/// Enable watching of the filesystem for changes, if support is available, starting from after
/// the point of calling this function.
pub fn watch_for_changes(&self) -> Result<(), AssetServerError> {
self.server.asset_io.watch_for_changes()?;
Ok(())
Expand Down Expand Up @@ -622,7 +624,7 @@ mod test {
handle_to_path: Default::default(),
asset_lifecycles: Default::default(),
task_pool: Default::default(),
asset_io: Box::new(FileAssetIo::new(asset_path)),
asset_io: Box::new(FileAssetIo::new(asset_path, false)),
}),
}
}
Expand Down
29 changes: 29 additions & 0 deletions crates/bevy_asset/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,10 +270,16 @@ pub trait AddAsset {
}

impl AddAsset for App {
/// Add an [`Asset`] to the [`App`].
///
/// Adding the same [`Asset`] again after it has been added does nothing.
fn add_asset<T>(&mut self) -> &mut Self
where
T: Asset,
{
if self.world.contains_resource::<Assets<T>>() {
return self;
}
let assets = {
let asset_server = self.world.get_resource::<AssetServer>().unwrap();
asset_server.register_asset_type::<T>()
Expand Down Expand Up @@ -305,3 +311,26 @@ impl AddAsset for App {
self
}
}

#[cfg(test)]
mod tests {
use bevy_app::App;

use crate::{AddAsset, Assets};

#[test]
fn asset_overwriting() {
#[derive(bevy_reflect::TypeUuid)]
#[uuid = "44115972-f31b-46e5-be5c-2b9aece6a52f"]
struct MyAsset;
let mut app = App::new();
app.add_plugin(bevy_core::CorePlugin)
.add_plugin(crate::AssetPlugin);
app.add_asset::<MyAsset>();
let mut assets_before = app.world.get_resource_mut::<Assets<MyAsset>>().unwrap();
let handle = assets_before.add(MyAsset);
app.add_asset::<MyAsset>(); // Ensure this doesn't overwrite the Asset
let assets_after = app.world.get_resource_mut::<Assets<MyAsset>>().unwrap();
assert!(assets_after.get(handle).is_some())
}
}
18 changes: 16 additions & 2 deletions crates/bevy_asset/src/io/file_asset_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,26 @@ pub struct FileAssetIo {
}

impl FileAssetIo {
pub fn new<P: AsRef<Path>>(path: P) -> Self {
FileAssetIo {
pub fn new<P: AsRef<Path>>(path: P, watch_for_changes: bool) -> Self {
let file_asset_io = FileAssetIo {
#[cfg(feature = "filesystem_watcher")]
filesystem_watcher: Default::default(),
root_path: Self::get_root_path().join(path.as_ref()),
};
if watch_for_changes {
#[cfg(any(
not(feature = "filesystem_watcher"),
target_arch = "wasm32",
target_os = "android"
))]
panic!(
"Watch for changes requires the filesystem_watcher feature and cannot be used on \
wasm32 / android targets"
);
#[cfg(feature = "filesystem_watcher")]
file_asset_io.watch_for_changes().unwrap();
}
file_asset_io
}

pub fn get_root_path() -> PathBuf {
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ pub struct AssetPlugin;

pub struct AssetServerSettings {
pub asset_folder: String,
/// Whether to watch for changes in asset files. Requires the `filesystem_watcher` feature,
/// and cannot be supported on the wasm32 arch nor android os.
pub watch_for_changes: bool,
}

impl Default for AssetServerSettings {
fn default() -> Self {
Self {
asset_folder: "assets".to_string(),
watch_for_changes: false,
}
}
}
Expand All @@ -64,7 +68,7 @@ pub fn create_platform_default_asset_io(app: &mut App) -> Box<dyn AssetIo> {
.get_resource_or_insert_with(AssetServerSettings::default);

#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
let source = FileAssetIo::new(&settings.asset_folder);
let source = FileAssetIo::new(&settings.asset_folder, settings.watch_for_changes);
#[cfg(target_arch = "wasm32")]
let source = WasmAssetIo::new(&settings.asset_folder);
#[cfg(target_os = "android")]
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_audio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ bevy_utils = { path = "../bevy_utils", version = "0.6.0" }

# other
anyhow = "1.0.4"
rodio = { version = "0.14", default-features = false }
rodio = { version = "0.15", default-features = false }
parking_lot = "0.11.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
rodio = { version = "0.14", default-features = false, features = ["wasm-bindgen"] }
rodio = { version = "0.15", default-features = false, features = ["wasm-bindgen"] }

[dev-dependencies]
# bevy
Expand Down
36 changes: 36 additions & 0 deletions crates/bevy_core/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,48 @@ impl Name {
}
}

impl std::fmt::Display for Name {
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
std::fmt::Display::fmt(&self.name, f)
}
}

/* Conversions from strings */

impl From<&str> for Name {
#[inline(always)]
fn from(name: &str) -> Self {
Name::new(name.to_owned())
}
}
impl From<String> for Name {
#[inline(always)]
fn from(name: String) -> Self {
Name::new(name)
}
}

/* Conversions to strings */

impl AsRef<str> for Name {
#[inline(always)]
fn as_ref(&self) -> &str {
&self.name
}
}
impl From<&Name> for String {
#[inline(always)]
fn from(val: &Name) -> String {
val.as_str().to_owned()
}
}
impl From<Name> for String {
#[inline(always)]
fn from(val: Name) -> String {
val.name.into_owned()
}
}

impl Hash for Name {
fn hash<H: Hasher>(&self, state: &mut H) {
Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_core/src/time/stopwatch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_reflect::Reflect;
use bevy_utils::Duration;

Expand All @@ -23,8 +22,7 @@ use bevy_utils::Duration;
/// assert!(stopwatch.paused());
/// assert_eq!(stopwatch.elapsed_secs(), 0.0);
/// ```
#[derive(Component, Clone, Debug, Default, Reflect)]
#[reflect(Component)]
#[derive(Clone, Debug, Default, Reflect)]
pub struct Stopwatch {
elapsed: Duration,
paused: bool,
Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_core/src/time/timer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::Stopwatch;
use bevy_ecs::{component::Component, reflect::ReflectComponent};
use bevy_reflect::Reflect;
use bevy_utils::Duration;

Expand All @@ -10,8 +9,7 @@ use bevy_utils::Duration;
/// exceeded, and can still be reset at any given point.
///
/// Paused timers will not have elapsed time increased.
#[derive(Component, Clone, Debug, Default, Reflect)]
#[reflect(Component)]
#[derive(Clone, Debug, Default, Reflect)]
pub struct Timer {
stopwatch: Stopwatch,
duration: Duration,
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ fn system_changed(query: Query<&Position, Changed<Velocity>>) {
}
}

// Gets the i32 component of all Entities that had a f32 component added since the last run of the System
// Gets the Position component of all Entities that had a Velocity component added since the last run of the System
fn system_added(query: Query<&Position, Added<Velocity>>) {
for position in query.iter() {
}
Expand Down
14 changes: 10 additions & 4 deletions crates/bevy_ecs/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use syn::{
parse_macro_input,
punctuated::Punctuated,
token::Comma,
DeriveInput, Field, GenericParam, Ident, Index, LitInt, Result, Token,
DeriveInput, Field, GenericParam, Ident, Index, LitInt, Result, Token, TypeParam,
};

struct AllTuples {
Expand Down Expand Up @@ -357,12 +357,18 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
.collect();

let mut punctuated_generics = Punctuated::<_, Token![,]>::new();
punctuated_generics.extend(lifetimeless_generics.iter());
punctuated_generics.extend(lifetimeless_generics.iter().map(|g| match g {
GenericParam::Type(g) => GenericParam::Type(TypeParam {
default: None,
..g.clone()
}),
_ => unreachable!(),
}));

let mut punctuated_generic_idents = Punctuated::<_, Token![,]>::new();
punctuated_generic_idents.extend(lifetimeless_generics.iter().map(|g| match g {
GenericParam::Type(g) => &g.ident,
_ => panic!(),
_ => unreachable!(),
}));

let struct_name = &ast.ident;
Expand Down Expand Up @@ -402,7 +408,7 @@ pub fn derive_system_param(input: TokenStream) -> TokenStream {
}
}

impl #impl_generics #path::system::SystemParamFetch<'w, 's> for #fetch_struct_name <(#(<#field_types as #path::system::SystemParam>::Fetch,)*), #punctuated_generic_idents> {
impl #impl_generics #path::system::SystemParamFetch<'w, 's> for #fetch_struct_name <(#(<#field_types as #path::system::SystemParam>::Fetch,)*), #punctuated_generic_idents> #where_clause {
type Item = #struct_name #ty_generics;
unsafe fn get_param(
state: &'s mut Self,
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,5 @@ pub struct ReflectMut<'a> {

#[cfg(feature = "bevy_reflect")]
change_detection_impl!(ReflectMut<'a>, dyn Reflect,);
#[cfg(feature = "bevy_reflect")]
impl_into_inner!(ReflectMut<'a>, dyn Reflect,);
9 changes: 8 additions & 1 deletion crates/bevy_ecs/src/entity/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl<'de> Visitor<'de> for EntityVisitor {
type Value = Entity;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("expected Entity")
formatter.write_str("Entity")
}

fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E>
Expand All @@ -34,4 +34,11 @@ impl<'de> Visitor<'de> for EntityVisitor {
{
Ok(Entity::from_raw(v))
}

fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Entity::from_raw(v as u32))
}
}
Loading

0 comments on commit fa15a91

Please sign in to comment.