diff options
| author | mat <github@matdoes.dev> | 2022-05-28 21:38:30 -0500 |
|---|---|---|
| committer | mat <github@matdoes.dev> | 2022-05-28 21:38:30 -0500 |
| commit | e832c84eb8196744c3c48a4412c808d38c917a80 (patch) | |
| tree | 4f19376588978b53db278b66d21b2d6786467847 | |
| parent | 9c1c2862361a4863cfd0af36c80705fb6213c3a4 (diff) | |
| download | azalea-drasl-e832c84eb8196744c3c48a4412c808d38c917a80.tar.xz | |
fixed macro for blocks with no properties
| -rw-r--r-- | azalea-block/block-macros/src/lib.rs | 40 | ||||
| -rw-r--r-- | azalea-block/block-macros/src/utils.rs | 3 | ||||
| -rw-r--r-- | azalea-block/src/blocks.rs | 6 | ||||
| -rw-r--r-- | codegen/lib/code/blocks.py | 22 |
4 files changed, 53 insertions, 18 deletions
diff --git a/azalea-block/block-macros/src/lib.rs b/azalea-block/block-macros/src/lib.rs index c0b5422d..b5a7909f 100644 --- a/azalea-block/block-macros/src/lib.rs +++ b/azalea-block/block-macros/src/lib.rs @@ -230,21 +230,32 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { pub #property_name_snake: #property, }) } + let block_name_pascal_case = Ident::new( + &to_pascal_case(&block.name.to_string()), + proc_macro2::Span::call_site(), + ); let block_struct_name = Ident::new( - &format!("{}Block", to_pascal_case(&block.name.to_string())), + &format!("{}Block", block_name_pascal_case), proc_macro2::Span::call_site(), ); - let mut from_block_to_state_match = quote! {}; + let mut from_block_to_state_match_inner = quote! {}; let first_state_id = state_id; + // if there's no properties, then the block is just a single state + if block_properties_vec.len() == 0 { + block_state_enum_variants.extend(quote! { + #block_name_pascal_case, + }); + state_id += 1; + } for combination in combinations_of(&block_properties_vec) { state_id += 1; let variant_name = Ident::new( &format!( "{}_{}", - to_pascal_case(&block.name.to_string()), + block_name_pascal_case, combination .iter() .map(|v| v.to_string()) @@ -260,7 +271,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { // face: properties::Face::Floor, // facing: properties::Facing::North, // powered: properties::Powered::True, - let mut from_block_to_state_match_inner = quote! {}; + let mut from_block_to_state_combination_match_inner = quote! {}; for i in 0..block_property_names.len() { let property_name = &block_property_names[i]; let property_name_ident = Ident::new(property_name, proc_macro2::Span::call_site()); @@ -269,14 +280,14 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { let variant = Ident::new(&combination[i].to_string(), proc_macro2::Span::call_site()); - from_block_to_state_match_inner.extend(quote! { + from_block_to_state_combination_match_inner.extend(quote! { #property_name_ident: #property_name_snake::#variant, }); } - from_block_to_state_match.extend(quote! { + from_block_to_state_match_inner.extend(quote! { #block_struct_name { - #from_block_to_state_match_inner + #from_block_to_state_combination_match_inner } => BlockState::#variant_name, }); } @@ -329,6 +340,17 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { let block_behavior = &block.behavior; let block_id = block.name.to_string(); + + let from_block_to_state_match = if block.properties_and_defaults.len() > 0 { + quote! { + match b { + #from_block_to_state_match_inner + } + } + } else { + quote! { BlockState::#block_name_pascal_case } + }; + let block_struct = quote! { #[derive(Debug)] pub struct #block_struct_name { @@ -346,9 +368,7 @@ pub fn make_block_states(input: TokenStream) -> TokenStream { impl From<#block_struct_name> for BlockState { fn from(b: #block_struct_name) -> Self { - match b { - #from_block_to_state_match - } + #from_block_to_state_match } } diff --git a/azalea-block/block-macros/src/utils.rs b/azalea-block/block-macros/src/utils.rs index 8700e17f..019fd60f 100644 --- a/azalea-block/block-macros/src/utils.rs +++ b/azalea-block/block-macros/src/utils.rs @@ -1,5 +1,8 @@ pub fn combinations_of<T: Clone>(items: &[Vec<T>]) -> Vec<Vec<T>> { let mut combinations = Vec::new(); + if items.len() == 0 { + return combinations; + }; if items.len() == 1 { for item in &items[0] { combinations.push(vec![item.clone()]); diff --git a/azalea-block/src/blocks.rs b/azalea-block/src/blocks.rs index 8a4a150b..a5f6283d 100644 --- a/azalea-block/src/blocks.rs +++ b/azalea-block/src/blocks.rs @@ -43,11 +43,6 @@ make_block_states! { Powered=True }, acacia_door => BlockBehavior::default(), { - Facing=North, - Half=Upper, - Hinge=Left, - Open=True, - Powered=True }, } } @@ -168,3 +163,4 @@ make_block_states! { // assert_eq!(block.id(), "acacia_button"); // } // } +// } diff --git a/codegen/lib/code/blocks.py b/codegen/lib/code/blocks.py index a8f9afd1..ca178ff3 100644 --- a/codegen/lib/code/blocks.py +++ b/codegen/lib/code/blocks.py @@ -22,7 +22,7 @@ def generate_blocks(blocks: dict): new_make_block_states_macro_code.append(' Properties => {') for property_name, property_variants in properties.items(): new_make_block_states_macro_code.append( - f' {to_camel_case(property_name)} => {{') + f' {to_camel_case(property_name)} {{') for variant in property_variants: new_make_block_states_macro_code.append( @@ -51,6 +51,22 @@ def generate_blocks(blocks: dict): new_make_block_states_macro_code.append( f' {to_camel_case(property)}={to_camel_case(property_default)},') new_make_block_states_macro_code.append(' },') - new_make_block_states_macro_code.append(' },') + new_make_block_states_macro_code.append(' }') + new_make_block_states_macro_code.append('}') + + new_code = [] + in_macro = False + for line in existing_code: + if line == 'make_block_states! {': + in_macro = True + elif line == '}': + if in_macro: + in_macro = False + new_code.extend(new_make_block_states_macro_code) + continue + if in_macro: + continue + new_code.append(line) - print('\n'.join(new_make_block_states_macro_code)) + with open(BLOCKS_RS_DIR, 'w') as f: + f.write('\n'.join(new_code)) |
