aboutsummaryrefslogtreecommitdiff
path: root/azalea-block/block-macros/src/lib.rs
blob: e65856003bb106b663187b61379da2144b9981d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
mod utils;

use proc_macro::TokenStream;
use quote::quote;
use std::collections::HashMap;
use std::fmt::Write;
use syn::{
    self, braced,
    parse::{Parse, ParseStream, Result},
    parse_macro_input,
    punctuated::Punctuated,
    Expr, Ident, LitStr, Token,
};
use utils::{combinations_of, to_pascal_case};

struct PropertyDefinition {
    name: LitStr,
    struct_name: Ident,
    variants: Punctuated<Ident, Token![,]>,
}
struct PropertyDefinitions {
    properties: Vec<PropertyDefinition>,
}

struct PropertyAndDefault {
    struct_name: Ident,
    default: Ident,
}
struct PropertyWithNameAndDefault {
    name: String,
    struct_name: Ident,
    default: Ident,
}
struct BlockDefinition {
    name: Ident,
    behavior: Expr,
    properties_and_defaults: Vec<PropertyAndDefault>,
}
impl PropertyAndDefault {
    fn as_property_with_name_and_default(&self, name: String) -> PropertyWithNameAndDefault {
        PropertyWithNameAndDefault {
            name,
            struct_name: self.struct_name.clone(),
            default: self.default.clone(),
        }
    }
}
struct BlockDefinitions {
    blocks: Vec<BlockDefinition>,
}
struct MakeBlockStates {
    property_definitions: PropertyDefinitions,
    block_definitions: BlockDefinitions,
}

impl Parse for PropertyDefinition {
    fn parse(input: ParseStream) -> Result<Self> {
        // "face" => Face {
        //     Floor,
        //     Wall,
        //     Ceiling
        // },

        // if you're wondering, the reason it's in quotes is because `type` is
        // a keyword in rust so if we don't put it in quotes it results in a
        // syntax error
        let name = input.parse()?;
        input.parse::<Token![=>]>()?;
        let struct_name = input.parse()?;

        let content;
        braced!(content in input);
        let variants = content.parse_terminated(Ident::parse)?;

        input.parse::<Token![,]>()?;
        Ok(PropertyDefinition {
            name,
            struct_name,
            variants,
        })
    }
}

impl Parse for PropertyDefinitions {
    fn parse(input: ParseStream) -> Result<Self> {
        let mut property_definitions = Vec::new();
        while !input.is_empty() {
            property_definitions.push(input.parse()?);
        }

        Ok(PropertyDefinitions {
            properties: property_definitions,
        })
    }
}

impl Parse for BlockDefinition {
    fn parse(input: ParseStream) -> Result<Self> {
        // acacia_button => BlockBehavior::default(), {
        //     Facing=North,
        //     Powered=False,
        //     Face=Wall,
        // },
        let name = input.parse()?;
        input.parse::<Token![=>]>()?;
        let behavior = input.parse()?;

        input.parse::<Token![,]>()?;
        let content;
        braced!(content in input);

        let mut properties_and_defaults = Vec::new();

        while let Ok(property) = content.parse() {
            content.parse::<Token![=]>()?;
            let property_default = content.parse()?;
            properties_and_defaults.push(PropertyAndDefault {
                struct_name: property,
                default: property_default,
            });
            if content.parse::<Token![,]>().is_err() {
                break;
            }
        }
        input.parse::<Token![,]>()?;
        Ok(BlockDefinition {
            name,
            behavior,
            properties_and_defaults,
        })
    }
}

impl Parse for BlockDefinitions {
    fn parse(input: ParseStream) -> Result<Self> {
        let mut blocks = Vec::new();
        while !input.is_empty() {
            blocks.push(input.parse()?);
        }

        Ok(BlockDefinitions { blocks })
    }
}

impl Parse for MakeBlockStates {
    fn parse(input: ParseStream) -> Result<Self> {
        // Properties => { ... } Blocks => { ... }
        let properties_ident = input.parse::<Ident>()?;
        assert_eq!(properties_ident.to_string(), "Properties");
        input.parse::<Token![=>]>()?;
        let content;
        braced!(content in input);
        let properties = content.parse()?;

        input.parse::<Token![,]>()?;

        let blocks_ident = input.parse::<Ident>()?;
        assert_eq!(blocks_ident.to_string(), "Blocks");
        input.parse::<Token![=>]>()?;
        let content;
        braced!(content in input);
        let blocks = content.parse()?;

        Ok(MakeBlockStates {
            property_definitions: properties,
            block_definitions: blocks,
        })
    }
}

#[proc_macro]
pub fn make_block_states(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as MakeBlockStates);

    let mut property_enums = quote! {};
    let mut properties_map = HashMap::new();
    let mut property_struct_names_to_names = HashMap::new();

    let mut state_id: usize = 0;

    for property in &input.property_definitions.properties {
        let mut property_enum_variants = quote! {};
        let mut property_from_number_variants = quote! {};
        let mut property_enum_variant_names = Vec::new();

        let property_struct_name = &property.struct_name;

        property_struct_names_to_names.insert(
            property_struct_name.to_string(),
            property.name.clone().value(),
        );

        for i in 0..property.variants.len() {
            let variant = &property.variants[i];

            let i_lit = syn::Lit::Int(syn::LitInt::new(
                &i.to_string(),
                proc_macro2::Span::call_site(),
            ));

            property_enum_variants.extend(quote! {
                #variant = #i_lit,
            });

            // i_lit is used here instead of i because otherwise it says 0size
            // in the expansion and that looks uglier
            property_from_number_variants.extend(quote! {
                #i_lit => #property_struct_name::#variant,
            });

            property_enum_variant_names.push(variant.to_string());
        }

        property_enums.extend(quote! {
            #[derive(Debug, Clone, Copy)]
            pub enum #property_struct_name {
                #property_enum_variants
            }

            impl From<usize> for #property_struct_name {
                fn from(value: usize) -> Self {
                    match value {
                        #property_from_number_variants
                        _ => panic!("Invalid property value: {}", value),
                    }
                }
            }
        });
        properties_map.insert(
            property_struct_name.to_string(),
            property_enum_variant_names,
        );
    }

    let mut block_state_enum_variants = quote! {};
    let mut block_structs = quote! {};
    let mut from_state_to_block_match = quote! {};
    for block in &input.block_definitions.blocks {
        let block_property_names = &block
            .properties_and_defaults
            .iter()
            .map(|p| p.struct_name.to_string())
            .collect::<Vec<_>>();
        let mut block_properties_vec = Vec::new();
        for property_name in block_property_names {
            let property_variants = properties_map
                .get(property_name)
                .unwrap_or_else(|| panic!("Property '{}' not found", property_name))
                .clone();
            block_properties_vec.push(property_variants);
        }

        let mut properties_with_name: Vec<PropertyWithNameAndDefault> =
            Vec::with_capacity(block.properties_and_defaults.len());
        for property in &block.properties_and_defaults {
            let index: Option<usize> = if block
                .properties_and_defaults
                .iter()
                .filter(|p| p.struct_name == property.struct_name)
                .count()
                > 1
            {
                Some(
                    properties_with_name
                        .iter()
                        .filter(|p| p.struct_name == property.struct_name)
                        .count(),
                )
            } else {
                None
            };
            let mut property_name = property_struct_names_to_names
                .get(&property.struct_name.to_string())
                .unwrap_or_else(|| panic!("Property '{}' is bad", property.struct_name))
                .clone();
            if let Some(index) = index {
                // property_name.push_str(&format!("_{}", &index.to_string()));
                write!(property_name, "_{}", index).unwrap();
            }
            properties_with_name
                .push(property.as_property_with_name_and_default(property_name.clone()));
        }

        //     pub face: properties::Face,
        //     pub facing: properties::Facing,
        //     pub powered: properties::Powered,
        // or
        //     pub has_bottle_0: HasBottle,
        //     pub has_bottle_1: HasBottle,
        //     pub has_bottle_2: HasBottle,
        let mut block_struct_fields = quote! {};
        for PropertyWithNameAndDefault {
            struct_name, name, ..
        } in &properties_with_name
        {
            // let property_name_snake =
            //     Ident::new(&property.to_string(), proc_macro2::Span::call_site());
            let name_ident = Ident::new(name, proc_macro2::Span::call_site());
            block_struct_fields.extend(quote! {
                pub #name_ident: #struct_name,
            })
        }

        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", block_name_pascal_case),
            proc_macro2::Span::call_site(),
        );

        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.is_empty() {
            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!(
                    "{}_{}",
                    block_name_pascal_case,
                    combination
                        .iter()
                        .map(|v| v.to_string())
                        .collect::<Vec<String>>()
                        .join("")
                ),
                proc_macro2::Span::call_site(),
            );
            block_state_enum_variants.extend(quote! {
                #variant_name,
            });

            // 	face: properties::Face::Floor,
            // 	facing: properties::Facing::North,
            // 	powered: properties::Powered::True,
            let mut from_block_to_state_combination_match_inner = quote! {};
            for i in 0..properties_with_name.len() {
                let property = &properties_with_name[i];
                let property_name = &property.name;
                let property_name_ident = Ident::new(property_name, proc_macro2::Span::call_site());
                let property_struct_name_ident = &property.struct_name;
                let variant =
                    Ident::new(&combination[i].to_string(), proc_macro2::Span::call_site());

                from_block_to_state_combination_match_inner.extend(quote! {
                    #property_name_ident: #property_struct_name_ident::#variant,
                });
            }

            from_block_to_state_match_inner.extend(quote! {
                #block_struct_name {
                    #from_block_to_state_combination_match_inner
                } => BlockState::#variant_name,
            });
        }

        // 7035..=7058 => {
        //     let b = b - 7035;
        //     &AcaciaButtonBlock {
        //         powered: Powered::from((b / 1) % 2),
        //         facing: Facing::from((b / 2) % 4),
        //         face: Face::from((b / 8) % 3),
        //     }
        // }
        let mut from_state_to_block_inner = quote! {};
        let mut division = 1usize;
        for i in (0..properties_with_name.len()).rev() {
            let PropertyWithNameAndDefault {
                struct_name: property_struct_name_ident,
                name: property_name,
                ..
            } = &properties_with_name[i];

            let property_variants = &block_properties_vec[i];
            let property_variants_count = property_variants.len();
            let property_name_ident = Ident::new(property_name, proc_macro2::Span::call_site());
            from_state_to_block_inner.extend(quote! {
                #property_name_ident: #property_struct_name_ident::from((b / #division) % #property_variants_count),
            });

            division *= property_variants_count;
        }

        let last_state_id = state_id - 1;
        from_state_to_block_match.extend(quote! {
            #first_state_id..=#last_state_id => {
                let b = b - #first_state_id;
                Box::new(#block_struct_name {
                    #from_state_to_block_inner
                })
            },
        });

        let mut block_default_fields = quote! {};
        for PropertyWithNameAndDefault {
            struct_name: struct_name_ident,
            name,
            default: property_default,
        } in properties_with_name
        {
            let name_ident = Ident::new(&name, proc_macro2::Span::call_site());
            block_default_fields.extend(quote! {
                #name_ident: #struct_name_ident::#property_default,
            })
        }

        let block_behavior = &block.behavior;
        let block_id = block.name.to_string();

        let from_block_to_state_match = if !block.properties_and_defaults.is_empty() {
            quote! {
                match b {
                    #from_block_to_state_match_inner
                }
            }
        } else {
            quote! { BlockState::#block_name_pascal_case }
        };

        if cfg!(feature = "trait") {
            let block_struct = quote! {
                #[derive(Debug)]
                pub struct #block_struct_name {
                    #block_struct_fields
                }

                impl Block for #block_struct_name {
                    fn behavior(&self) -> BlockBehavior {
                        #block_behavior
                    }
                    fn id(&self) -> &'static str {
                        #block_id
                    }
                }

                impl From<#block_struct_name> for BlockState {
                    fn from(b: #block_struct_name) -> Self {
                        #from_block_to_state_match
                    }
                }

                impl Default for #block_struct_name {
                    fn default() -> Self {
                        Self {
                            #block_default_fields
                        }
                    }
                }
            };

            block_structs.extend(block_struct);
        }
    }

    let last_state_id = (state_id - 1) as u32;
    let mut generated = quote! {
        #property_enums

        #[repr(u32)]
        #[derive(Copy, Clone, PartialEq, Eq, Debug)]
        pub enum BlockState {
            #block_state_enum_variants
        }

        impl BlockState {
            /// Returns the highest possible state
            #[inline]
            pub fn max_state() -> u32 {
                #last_state_id
            }
        }
    };

    if cfg!(feature = "trait") {
        generated.extend(quote! {
            #block_structs

            impl From<BlockState> for Box<dyn Block> {
                fn from(b: BlockState) -> Self {
                    let b = b as usize;
                    match b {
                        #from_state_to_block_match
                        _ => panic!("Invalid block state: {}", b),
                    }
                }
            }
        });
    }

    generated.into()
}