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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! This crate adds support for deriving [`AssetCollection`]
//!
//! You do not have to use it directly. Just import [`AssetCollection`] from `bevy_asset_loader`
//! and use `#[derive(AssetCollection)]` to derive the trait.

#![forbid(unsafe_code)]
#![warn(unused_imports)]

extern crate proc_macro;

mod assets;

use proc_macro::TokenStream;
use std::option::Option::Some;
use std::result::Result::{Err, Ok};

use crate::assets::*;
use proc_macro2::Ident;
use quote::{quote, quote_spanned, ToTokens, TokenStreamExt};
use syn::punctuated::Punctuated;
#[cfg(any(feature = "2d", feature = "3d"))]
use syn::ExprPath;
use syn::{Data, Expr, ExprLit, Field, Fields, Index, Lit, LitStr, Meta, Token};

/// Derive macro for [`AssetCollection`]
///
/// The helper attribute ``asset`` can be used to define the path to the asset file
/// and other asset options.
#[proc_macro_derive(AssetCollection, attributes(asset))]
pub fn asset_collection_derive(input: TokenStream) -> TokenStream {
    let ast = syn::parse(input).unwrap();
    impl_asset_collection(ast)
        .unwrap_or_else(to_compile_errors)
        .into()
}

pub(crate) const ASSET_ATTRIBUTE: &str = "asset";
pub(crate) const PATH_ATTRIBUTE: &str = "path";
pub(crate) const KEY_ATTRIBUTE: &str = "key";
pub(crate) const OPTIONAL_ATTRIBUTE: &str = "optional";

pub(crate) struct TextureAtlasAttribute;
impl TextureAtlasAttribute {
    pub const ATTRIBUTE_NAME_DEPRECATED: &'static str = "texture_atlas";
    pub const ATTRIBUTE_NAME: &'static str = "texture_atlas_layout";
    pub const TILE_SIZE_X: &'static str = "tile_size_x";
    pub const TILE_SIZE_Y: &'static str = "tile_size_y";
    pub const COLUMNS: &'static str = "columns";
    pub const ROWS: &'static str = "rows";
    #[allow(dead_code)]
    pub const PADDING_X: &'static str = "padding_x";
    #[allow(dead_code)]
    pub const PADDING_Y: &'static str = "padding_y";
    #[allow(dead_code)]
    pub const OFFSET_X: &'static str = "offset_x";
    #[allow(dead_code)]
    pub const OFFSET_Y: &'static str = "offset_y";
}

pub(crate) struct ImageAttribute;
impl ImageAttribute {
    pub const ATTRIBUTE_NAME: &'static str = "image";
    #[allow(dead_code)]
    pub const SAMPLER: &'static str = "sampler";
}

pub(crate) const COLLECTION_ATTRIBUTE: &str = "collection";
pub(crate) const PATHS_ATTRIBUTE: &str = "paths";
pub(crate) const TYPED_ATTRIBUTE: &str = "typed";
pub(crate) const MAPPED_ATTRIBUTE: &str = "mapped";
pub(crate) const STANDARD_MATERIAL_ATTRIBUTE: &str = "standard_material";

fn impl_asset_collection(
    ast: syn::DeriveInput,
) -> Result<proc_macro2::TokenStream, Vec<syn::Error>> {
    let name = &ast.ident;

    let mut from_world_fields: Vec<Ident> = vec![];
    let mut assets: Vec<AssetField> = vec![];
    if let Data::Struct(ref data_struct) = ast.data {
        if let Fields::Named(ref named_fields) = data_struct.fields {
            let mut compile_errors = vec![];
            for field in named_fields.named.iter() {
                match parse_field(field) {
                    Ok(asset) => assets.push(asset),
                    Err(errors) => {
                        for error in errors {
                            match error {
                                ParseFieldError::NoAttributes => {
                                    from_world_fields.push(field.clone().ident.unwrap());
                                }
                                ParseFieldError::KeyAttributeStandsAlone => {
                                    compile_errors.push(syn::Error::new_spanned(
                                        field.into_token_stream(),
                                        "The 'key' attribute cannot be combined with any other asset defining attributes",
                                    ));
                                }
                                ParseFieldError::OnlyDynamicCanBeOptional => {
                                    compile_errors.push(syn::Error::new_spanned(
                                        field.into_token_stream(),
                                        "Only a dynamic asset (with 'key' attribute) can be optional",
                                    ));
                                }
                                ParseFieldError::MissingAttributes(missing_attributes) => {
                                    compile_errors.push(syn::Error::new_spanned(
                                        field.into_token_stream(),
                                        format!(
                                            "Field is missing asset attributes: {}",
                                            missing_attributes.join(", ")
                                        ),
                                    ));
                                }
                                ParseFieldError::WrongAttributeType(token_stream, expected) => {
                                    compile_errors.push(syn::Error::new_spanned(
                                        token_stream,
                                        format!("Wrong attribute type. Expected '{expected}'"),
                                    ));
                                }
                                ParseFieldError::UnknownAttributeType(token_stream) => {
                                    compile_errors.push(syn::Error::new_spanned(
                                        token_stream,
                                        "Unknown attribute type",
                                    ));
                                }
                                ParseFieldError::UnknownAttribute(token_stream) => {
                                    compile_errors.push(syn::Error::new_spanned(
                                        token_stream,
                                        "Unknown attribute",
                                    ));
                                }
                                ParseFieldError::Missing2dFeature(token_stream) => {
                                    compile_errors.push(syn::Error::new_spanned(
                                        token_stream,
                                        "This attribute requires the '2d' feature",
                                    ));
                                }
                                ParseFieldError::Missing3dFeature(token_stream) => {
                                    compile_errors.push(syn::Error::new_spanned(
                                        token_stream,
                                        "This attribute requires the '3d' feature",
                                    ));
                                }
                                ParseFieldError::Missing2dOr3dFeature(token_stream) => {
                                    compile_errors.push(syn::Error::new_spanned(
                                        token_stream,
                                        "This attribute requires the '3d' or '2d' feature",
                                    ));
                                }
                                ParseFieldError::PathAndPathsAreExclusive => {
                                    compile_errors.push(syn::Error::new_spanned(
                                        field.into_token_stream(),
                                        "Either specify 'path' OR 'paths'",
                                    ));
                                }
                            }
                        }
                    }
                }
            }
            if !compile_errors.is_empty() {
                return Err(compile_errors);
            }
        } else {
            return Err(vec![syn::Error::new_spanned(
                data_struct.fields.clone().into_token_stream(),
                "only named fields are supported to derive AssetCollection",
            )]);
        }
    } else {
        return Err(vec![syn::Error::new_spanned(
            &ast.into_token_stream(),
            "AssetCollection can only be derived for a struct",
        )]);
    }

    let asset_loading = assets.iter().fold(quote!(), |token_stream, asset| {
        asset.attach_token_stream_for_loading(token_stream)
    });
    let load_function = quote! {
            fn load(world: &mut ::bevy::ecs::world::World) -> Vec<::bevy::prelude::UntypedHandle> {
                let cell = world.cell();
                let asset_server = cell.get_resource::<::bevy::prelude::AssetServer>().expect("Cannot get AssetServer");
                let asset_keys = cell.get_resource::<bevy_asset_loader::prelude::DynamicAssets>().expect("Cannot get bevy_asset_loader::prelude::DynamicAssets");
                let mut handles = vec![];
                #asset_loading
                handles
            }
    };

    let prepare_from_world = from_world_fields.iter().fold(
        quote!(),
        |es, ident| quote_spanned! {ident.span() => #es ::bevy::ecs::world::FromWorld::from_world(world),},
    );

    let mut asset_creation = assets.iter().fold(quote!(), |token_stream, asset| {
        asset.attach_token_stream_for_creation(token_stream, name.to_string())
    });
    let mut index = 0;
    asset_creation.append_all(from_world_fields.iter().fold(quote!(), |es, ident| {
        let index_ident = Index::from(index);
        let tokens = quote! (#es #ident : from_world_fields.#index_ident,);
        index += 1;
        tokens
    }));
    let create_function = quote! {
        fn create(world: &mut ::bevy::ecs::world::World) -> Self {
            let from_world_fields = (#prepare_from_world);
            world.resource_scope(
                |world, asset_keys: ::bevy::prelude::Mut<::bevy_asset_loader::dynamic_asset::DynamicAssets>| {
                    #name {
                        #asset_creation
                    }
                },
            )
        }
    };

    let impl_asset_collection = quote! {
        #[automatically_derived]
        #[allow(unused_variables)]
        impl AssetCollection for #name {
            #create_function

            #load_function
        }
    };
    Ok(impl_asset_collection)
}

#[derive(Debug)]
enum ParseFieldError {
    NoAttributes,
    KeyAttributeStandsAlone,
    OnlyDynamicCanBeOptional,
    PathAndPathsAreExclusive,
    WrongAttributeType(proc_macro2::TokenStream, &'static str),
    UnknownAttributeType(proc_macro2::TokenStream),
    UnknownAttribute(proc_macro2::TokenStream),
    MissingAttributes(Vec<String>),
    #[allow(dead_code)]
    Missing2dFeature(proc_macro2::TokenStream),
    #[allow(dead_code)]
    Missing3dFeature(proc_macro2::TokenStream),
    #[allow(dead_code)]
    Missing2dOr3dFeature(proc_macro2::TokenStream),
}

fn parse_field(field: &Field) -> Result<AssetField, Vec<ParseFieldError>> {
    let mut builder = AssetBuilder::default();
    let mut errors = vec![];
    for attr in field
        .attrs
        .iter()
        .filter(|attribute| attribute.path().is_ident(ASSET_ATTRIBUTE))
    {
        let asset_meta_list = attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated);

        builder.field_ident = Some(field.clone().ident.unwrap());

        for attribute in asset_meta_list.unwrap() {
            match attribute {
                Meta::List(meta_list)
                    if meta_list
                        .path
                        .is_ident(TextureAtlasAttribute::ATTRIBUTE_NAME)
                        || meta_list
                            .path
                            .is_ident(TextureAtlasAttribute::ATTRIBUTE_NAME_DEPRECATED) =>
                {
                    #[cfg(not(feature = "2d"))]
                    errors.push(ParseFieldError::Missing2dFeature(
                        meta_list.into_token_stream(),
                    ));
                    #[cfg(feature = "2d")]
                    {
                        let texture_atlas_meta_list = meta_list
                            .parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated);
                        for attribute in texture_atlas_meta_list.unwrap() {
                            match attribute {
                                Meta::NameValue(named_value) => {
                                    let path = named_value.path.get_ident().unwrap().clone();
                                    if path == TextureAtlasAttribute::TILE_SIZE_X {
                                        if let Expr::Lit(ExprLit {
                                            lit: Lit::Float(width),
                                            ..
                                        }) = &named_value.value
                                        {
                                            builder.tile_size_x =
                                                Some(width.base10_parse::<f32>().unwrap());
                                        } else {
                                            errors.push(ParseFieldError::WrongAttributeType(
                                                named_value.into_token_stream(),
                                                "float",
                                            ));
                                        }
                                    } else if path == TextureAtlasAttribute::TILE_SIZE_Y {
                                        if let Expr::Lit(ExprLit {
                                            lit: Lit::Float(height),
                                            ..
                                        }) = &named_value.value
                                        {
                                            builder.tile_size_y =
                                                Some(height.base10_parse::<f32>().unwrap());
                                        } else {
                                            errors.push(ParseFieldError::WrongAttributeType(
                                                named_value.into_token_stream(),
                                                "float",
                                            ));
                                        }
                                    } else if path == TextureAtlasAttribute::COLUMNS {
                                        if let Expr::Lit(ExprLit {
                                            lit: Lit::Int(columns),
                                            ..
                                        }) = &named_value.value
                                        {
                                            builder.columns =
                                                Some(columns.base10_parse::<usize>().unwrap());
                                        } else {
                                            errors.push(ParseFieldError::WrongAttributeType(
                                                named_value.into_token_stream(),
                                                "integer",
                                            ));
                                        }
                                    } else if path == TextureAtlasAttribute::ROWS {
                                        if let Expr::Lit(ExprLit {
                                            lit: Lit::Int(rows),
                                            ..
                                        }) = &named_value.value
                                        {
                                            builder.rows =
                                                Some(rows.base10_parse::<usize>().unwrap());
                                        } else {
                                            errors.push(ParseFieldError::WrongAttributeType(
                                                named_value.into_token_stream(),
                                                "integer",
                                            ));
                                        }
                                    } else if path == TextureAtlasAttribute::PADDING_X {
                                        if let Expr::Lit(ExprLit {
                                            lit: Lit::Float(padding_x),
                                            ..
                                        }) = &named_value.value
                                        {
                                            builder.padding_x =
                                                Some(padding_x.base10_parse::<f32>().unwrap());
                                        } else {
                                            errors.push(ParseFieldError::WrongAttributeType(
                                                named_value.into_token_stream(),
                                                "float",
                                            ));
                                        }
                                    } else if path == TextureAtlasAttribute::PADDING_Y {
                                        if let Expr::Lit(ExprLit {
                                            lit: Lit::Float(padding_y),
                                            ..
                                        }) = &named_value.value
                                        {
                                            builder.padding_y =
                                                Some(padding_y.base10_parse::<f32>().unwrap());
                                        } else {
                                            errors.push(ParseFieldError::WrongAttributeType(
                                                named_value.into_token_stream(),
                                                "float",
                                            ));
                                        }
                                    } else if path == TextureAtlasAttribute::OFFSET_X {
                                        if let Expr::Lit(ExprLit {
                                            lit: Lit::Float(offset_x),
                                            ..
                                        }) = &named_value.value
                                        {
                                            builder.offset_x =
                                                Some(offset_x.base10_parse::<f32>().unwrap());
                                        } else {
                                            errors.push(ParseFieldError::WrongAttributeType(
                                                named_value.into_token_stream(),
                                                "float",
                                            ));
                                        }
                                    } else if path == TextureAtlasAttribute::OFFSET_Y {
                                        if let Expr::Lit(ExprLit {
                                            lit: Lit::Float(offset_y),
                                            ..
                                        }) = &named_value.value
                                        {
                                            builder.offset_y =
                                                Some(offset_y.base10_parse::<f32>().unwrap());
                                        } else {
                                            errors.push(ParseFieldError::WrongAttributeType(
                                                named_value.into_token_stream(),
                                                "float",
                                            ));
                                        }
                                    } else {
                                        errors.push(ParseFieldError::UnknownAttribute(
                                            named_value.into_token_stream(),
                                        ));
                                    }
                                }
                                _ => {
                                    errors.push(ParseFieldError::UnknownAttributeType(
                                        attribute.into_token_stream(),
                                    ));
                                }
                            }
                        }
                    }
                }
                Meta::List(meta_list) if meta_list.path.is_ident(COLLECTION_ATTRIBUTE) => {
                    let collection_meta_list =
                        meta_list.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated);

                    builder.is_collection = true;
                    for attribute in collection_meta_list.unwrap() {
                        match attribute {
                            Meta::Path(meta_path) => {
                                let path = meta_path.get_ident().unwrap().clone();
                                if path == TYPED_ATTRIBUTE {
                                    builder.is_typed = true;
                                } else if path == MAPPED_ATTRIBUTE {
                                    builder.is_mapped = true;
                                } else {
                                    errors.push(ParseFieldError::UnknownAttribute(
                                        meta_path.into_token_stream(),
                                    ));
                                }
                            }
                            _ => {
                                errors.push(ParseFieldError::UnknownAttributeType(
                                    attribute.into_token_stream(),
                                ));
                            }
                        }
                    }
                }
                Meta::List(meta_list) if meta_list.path.is_ident(PATHS_ATTRIBUTE) => {
                    let paths_meta_list = meta_list
                        .parse_args_with(Punctuated::<LitStr, Token![,]>::parse_terminated);

                    let mut paths = vec![];
                    for path in paths_meta_list.unwrap() {
                        paths.push(path.value());
                    }
                    builder.asset_paths = Some(paths);
                }
                Meta::List(meta_list)
                    if meta_list.path.is_ident(ImageAttribute::ATTRIBUTE_NAME) =>
                {
                    #[cfg(all(not(feature = "2d"), not(feature = "3d")))]
                    errors.push(ParseFieldError::Missing2dOr3dFeature(
                        meta_list.into_token_stream(),
                    ));
                    #[cfg(any(feature = "2d", feature = "3d"))]
                    {
                        let image_meta_list = meta_list
                            .parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated);
                        for attribute in image_meta_list.unwrap() {
                            match attribute {
                                Meta::NameValue(named_value) => {
                                    let path = named_value.path.get_ident().unwrap().clone();
                                    if path == ImageAttribute::SAMPLER {
                                        if let Expr::Path(ExprPath { path, .. }) =
                                            &named_value.value
                                        {
                                            let sampler_result = SamplerType::try_from(
                                                path.get_ident().unwrap().to_string(),
                                            );

                                            if let Ok(sampler) = sampler_result {
                                                builder.sampler = Some(sampler);
                                            } else {
                                                errors.push(ParseFieldError::UnknownAttribute(
                                                    named_value.value.into_token_stream(),
                                                ));
                                            }
                                        } else {
                                            errors.push(ParseFieldError::WrongAttributeType(
                                                named_value.into_token_stream(),
                                                "path",
                                            ));
                                        }
                                    }
                                }
                                _ => {
                                    errors.push(ParseFieldError::UnknownAttributeType(
                                        attribute.into_token_stream(),
                                    ));
                                }
                            }
                        }
                    }
                }
                Meta::List(meta_list) => errors.push(ParseFieldError::UnknownAttribute(
                    meta_list.into_token_stream(),
                )),
                Meta::NameValue(named_value) if named_value.path.is_ident(PATH_ATTRIBUTE) => {
                    if let Expr::Lit(ExprLit {
                        lit: Lit::Str(path),
                        ..
                    }) = &named_value.value
                    {
                        builder.asset_path = Some(path.value());
                    } else {
                        errors.push(ParseFieldError::WrongAttributeType(
                            named_value.into_token_stream(),
                            "str",
                        ));
                    }
                }
                Meta::NameValue(named_value) if named_value.path.is_ident(KEY_ATTRIBUTE) => {
                    if let Expr::Lit(ExprLit {
                        lit: Lit::Str(key), ..
                    }) = &named_value.value
                    {
                        builder.key = Some(key.value());
                    } else {
                        errors.push(ParseFieldError::WrongAttributeType(
                            named_value.into_token_stream(),
                            "str",
                        ));
                    }
                }
                Meta::NameValue(named_value) => errors.push(ParseFieldError::UnknownAttribute(
                    named_value.into_token_stream(),
                )),
                Meta::Path(meta_path) if meta_path.is_ident(STANDARD_MATERIAL_ATTRIBUTE) => {
                    #[cfg(not(feature = "3d"))]
                    errors.push(ParseFieldError::Missing3dFeature(
                        meta_path.into_token_stream(),
                    ));
                    #[cfg(feature = "3d")]
                    {
                        builder.is_standard_material = true;
                    }
                }
                Meta::Path(meta_path) if meta_path.is_ident(OPTIONAL_ATTRIBUTE) => {
                    builder.is_optional = true;
                }
                Meta::Path(meta_path) if meta_path.is_ident(COLLECTION_ATTRIBUTE) => {
                    builder.is_collection = true;
                }
                Meta::Path(meta_path) if meta_path.is_ident(TYPED_ATTRIBUTE) => {
                    builder.is_typed = true;
                }
                Meta::Path(meta_path) => errors.push(ParseFieldError::UnknownAttribute(
                    meta_path.into_token_stream(),
                )),
            }
        }
    }
    if !errors.is_empty() {
        return Err(errors);
    }
    builder.build()
}

fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
    let compile_errors = errors.iter().map(syn::Error::to_compile_error);
    quote!(#(#compile_errors)*)
}