-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from pablo-lua/prefab_data_derive
PrefabData derive
- Loading branch information
Showing
6 changed files
with
83 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
Cargo.lock | ||
/target | ||
**/target |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "bevy_prfb_macro" | ||
version = "0.12.1" | ||
edition = "2021" | ||
|
||
|
||
[profile.dev] | ||
opt-level = 1 | ||
|
||
[profile.release] | ||
codegen-units = 1 | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
quote = "1.0.33" | ||
syn = "2.0.43" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use proc_macro::TokenStream; | ||
use quote::quote; | ||
use syn::{parse_macro_input, Index, DeriveInput, spanned::Spanned}; | ||
|
||
|
||
|
||
#[proc_macro_derive(PrefabData)] | ||
pub fn derive_system_param(input: TokenStream) -> TokenStream { | ||
let ast = parse_macro_input!(input as DeriveInput); | ||
let syn::Data::Struct(syn::DataStruct { | ||
fields: field_definitions, | ||
.. | ||
}) = ast.data | ||
else { | ||
return syn::Error::new( | ||
ast.span(), | ||
"Invalid `PrefabData` type: expected a `struct`", | ||
) | ||
.into_compile_error() | ||
.into(); | ||
}; | ||
|
||
let mut fields = Vec::new(); | ||
for (i, field) in field_definitions.iter().enumerate() { | ||
let i = Index::from(i); | ||
fields.push( | ||
field | ||
.ident | ||
.as_ref() | ||
.map(|f| quote! { #f }) | ||
.unwrap_or_else(|| quote! { #i }), | ||
); | ||
} | ||
let struct_name = &ast.ident; | ||
let generics = ast.generics; | ||
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl(); | ||
|
||
TokenStream::from(quote! { | ||
const _: () = { | ||
impl #impl_generics bevy_prfb::prefab::PrefabData for #struct_name #ty_generics #where_clause { | ||
fn insert_into_entity(self, entity: &mut bevy::ecs::world::EntityWorldMut) { | ||
#( | ||
self.#fields.insert_into_entity(entity); | ||
)* | ||
} | ||
fn load_sub_assets(&mut self, world: &mut bevy::ecs::world::World) -> bool { | ||
let mut loaded = false; | ||
#( | ||
loaded |= self.#fields.load_sub_assets(world); | ||
)* | ||
loaded | ||
} | ||
} | ||
}; | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters