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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
use palette16::{Palette16OptimisationResults, Palette16Optimiser};
use palette256::Palette256;
use proc_macro::TokenStream;
use proc_macro2::Literal;
use syn::parse::{Parse, Parser};
use syn::{parse_macro_input, punctuated::Punctuated, LitStr};
use syn::{Expr, ExprLit, Lit, Token};

use std::collections::HashMap;
use std::path::PathBuf;
use std::{iter, path::Path, str};

use quote::{format_ident, quote, ToTokens};

mod aseprite;
mod colour;
mod config;
mod deduplicator;
mod font_loader;
mod image_loader;
mod palette16;
mod palette256;
mod rust_generator;

use image_loader::Image;

use colour::Colour;

#[derive(Debug, Clone, Copy)]
pub(crate) enum Colours {
    Colours16,
    Colours256,
}

struct BackgroundGfxOption {
    module_name: String,
    file_name: String,
    colours: Colours,
    deduplicate: bool,
}

impl config::Image for BackgroundGfxOption {
    fn filename(&self) -> String {
        self.file_name
            .clone()
            .replace(OUT_DIR_TOKEN, &get_out_dir(&self.file_name))
    }

    fn colours(&self) -> Colours {
        self.colours
    }

    fn deduplicate(&self) -> bool {
        self.deduplicate
    }
}

impl Parse for BackgroundGfxOption {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let module_name: syn::Ident = input.parse()?;
        let _: Token![=>] = input.parse()?;

        let lookahead = input.lookahead1();

        let colours = if lookahead.peek(syn::LitInt) {
            let num_colours: syn::LitInt = input.parse()?;

            match num_colours.base10_parse()? {
                16 => Colours::Colours16,
                256 => Colours::Colours256,
                _ => {
                    return Err(syn::Error::new_spanned(
                        num_colours,
                        "Number of colours must be 16 or 256",
                    ))
                }
            }
        } else {
            Colours::Colours16
        };

        let lookahead = input.lookahead1();

        let deduplicate = if lookahead.peek(syn::Ident) {
            let deduplicate: syn::Ident = input.parse()?;

            if deduplicate == "deduplicate" {
                true
            } else {
                return Err(syn::Error::new_spanned(
                    deduplicate,
                    "Must either be the literal deduplicate or missing",
                ));
            }
        } else {
            false
        };

        let file_name: syn::LitStr = input.parse()?;

        Ok(Self {
            module_name: module_name.to_string(),
            file_name: file_name.value(),
            colours,
            deduplicate,
        })
    }
}

struct IncludeBackgroundGfxInput {
    module_name: syn::Ident,
    crate_prefix: String,
    transparent_colour: Colour,
    background_gfx_options: Vec<BackgroundGfxOption>,
}

impl Parse for IncludeBackgroundGfxInput {
    fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
        let lookahead = input.lookahead1();

        let crate_prefix: syn::Ident = if lookahead.peek(Token![crate]) {
            let _: Token![crate] = input.parse()?;
            let _: Token![,] = input.parse()?;
            format_ident!("crate")
        } else {
            format_ident!("agb")
        };

        let module_name: syn::Ident = input.parse()?;
        let _: Token![,] = input.parse()?;

        let lookahead = input.lookahead1();
        let transparent_colour: Colour = if lookahead.peek(syn::LitStr) {
            let colour_str: syn::LitStr = input.parse()?;
            let _: Token![,] = input.parse()?;
            colour_str
                .value()
                .parse()
                .map_err(|msg| syn::Error::new_spanned(colour_str, msg))?
        } else {
            Colour::from_rgb(255, 0, 255, 0)
        };

        let background_gfx_options =
            input.parse_terminated(BackgroundGfxOption::parse, Token![,])?;

        Ok(Self {
            module_name,
            crate_prefix: crate_prefix.to_string(),
            transparent_colour,
            background_gfx_options: background_gfx_options.into_iter().collect(),
        })
    }
}

impl config::Config for IncludeBackgroundGfxInput {
    fn crate_prefix(&self) -> String {
        self.crate_prefix.clone()
    }

    fn images(&self) -> HashMap<String, &dyn config::Image> {
        self.background_gfx_options
            .iter()
            .map(|options| (options.module_name.clone(), options as &dyn config::Image))
            .collect()
    }

    fn transparent_colour(&self) -> Option<Colour> {
        Some(self.transparent_colour)
    }
}

/// Including from the out directory is supported through the `$OUT_DIR` token.
///
/// ```rust,ignore
/// # #![no_std]
/// # #![no_main]
/// # use agb::include_background_gfx;
/// include_background_gfx!(generated_background, "000000", DATA => "$OUT_DIR/generated_background.aseprite");
/// ```
///
#[proc_macro]
pub fn include_background_gfx(input: TokenStream) -> TokenStream {
    let config = Box::new(parse_macro_input!(input as IncludeBackgroundGfxInput));

    let root = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get cargo manifest dir");

    let module_name = config.module_name.clone();
    include_gfx_from_config(config, module_name, Path::new(&root))
}

fn include_gfx_from_config(
    config: Box<dyn config::Config>,
    module_name: syn::Ident,
    parent: &Path,
) -> TokenStream {
    let images = config.images();

    let mut optimiser = Palette16Optimiser::new(config.transparent_colour());
    let mut assignment_offsets = HashMap::new();
    let mut assignment_offset = 0;

    let mut palette256 = Palette256::new();

    for (name, settings) in images.iter() {
        let image_filename = &parent.join(settings.filename());
        let image = Image::load_from_file(image_filename);

        match settings.colours() {
            Colours::Colours16 => {
                let tile_size = 8;
                if image.width % tile_size != 0 || image.height % tile_size != 0 {
                    panic!("Image size not a multiple of tile size");
                }

                add_to_optimiser(
                    &mut optimiser,
                    &image,
                    tile_size,
                    tile_size,
                    config.transparent_colour(),
                );

                let num_tiles = image.width * image.height / 8usize.pow(2);
                assignment_offsets.insert(name, assignment_offset);
                assignment_offset += num_tiles;
            }
            Colours::Colours256 => {
                palette256.add_image(&image);
            }
        }
    }

    let optimisation_results = optimiser.optimise_palettes();
    let optimisation_results = palette256.extend_results(&optimisation_results);

    let mut image_code = vec![];

    for (image_name, &image) in images.iter() {
        let assignment_offset = match image.colours() {
            Colours::Colours16 => Some(assignment_offsets[image_name]),
            _ => None,
        };

        image_code.push(convert_image(
            image,
            parent,
            image_name,
            &config.crate_prefix(),
            &optimisation_results,
            assignment_offset,
        ));
    }

    let palette_code =
        rust_generator::generate_palette_code(&optimisation_results, &config.crate_prefix());

    let module = quote! {
        mod #module_name {
            #palette_code

            #(#image_code)*
        }
    };

    TokenStream::from(module)
}

use quote::TokenStreamExt;
struct ByteString<'a>(&'a [u8]);
impl ToTokens for ByteString<'_> {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        tokens.append(Literal::byte_string(self.0));
    }
}

#[proc_macro]
pub fn include_colours_inner(input: TokenStream) -> TokenStream {
    let input_filename = parse_macro_input!(input as LitStr);
    let input_filename = input_filename.value();

    let root = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get cargo manifest dir");
    let input_filename = Path::new(&root).join(input_filename);

    let image = Image::load_from_file(Path::new(&input_filename));

    let mut palette_data = Vec::with_capacity(image.width * image.height);
    for y in 0..image.height {
        for x in 0..image.width {
            palette_data.push(image.colour(x, y).to_rgb15())
        }
    }

    let filename = input_filename.to_string_lossy();

    TokenStream::from(quote! {
        {
            const _: &[u8] = include_bytes!(#filename);
            [#(#palette_data),*]
        }
    })
}

#[proc_macro]
pub fn include_aseprite_inner(input: TokenStream) -> TokenStream {
    let out_dir_path = get_out_dir(&input.to_string());

    let parser = Punctuated::<LitStr, syn::Token![,]>::parse_terminated;
    let parsed = match parser.parse(input) {
        Ok(e) => e,
        Err(e) => return e.to_compile_error().into(),
    };

    let transparent_colour = Colour::from_rgb(255, 0, 255, 0);

    let mut optimiser = palette16::Palette16Optimiser::new(Some(transparent_colour));
    let mut images = Vec::new();
    let mut tags = Vec::new();

    let root = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get cargo manifest dir");

    let filenames: Vec<PathBuf> = parsed
        .iter()
        .map(|s| s.value())
        .map(|s| s.replace(OUT_DIR_TOKEN, &out_dir_path))
        .map(|s| Path::new(&root).join(&*s))
        .collect();

    for filename in filenames.iter() {
        let (frames, tag) = aseprite::generate_from_file(filename);

        tags.push((tag, images.len()));

        for frame in frames {
            let width = frame.width();
            let height = frame.height();
            assert!(
                valid_sprite_size(width, height),
                "File {} contains sprites with size {}x{} which cannot be represented on the GameBoy Advance",
                filename.display(),
                width,
                height
            );

            let image = Image::load_from_dyn_image(frame);
            add_to_optimiser(
                &mut optimiser,
                &image,
                width as usize,
                height as usize,
                Some(transparent_colour),
            );
            images.push(image);
        }
    }

    let optimised_results = optimiser.optimise_palettes();

    let (palette_data, tile_data, assignments) = palette_tile_data(&optimised_results, &images);

    let palette_data = palette_data.iter().map(|colours| {
        quote! {
            Palette16::new([
                #(#colours),*
            ])
        }
    });

    let mut pre = 0;
    let sprites = images
        .iter()
        .zip(assignments.iter())
        .map(|(f, assignment)| {
            let start: usize = pre;
            let end: usize = pre + (f.width / 8) * (f.height / 8) * 32;
            let data = ByteString(&tile_data[start..end]);
            pre = end;
            let width = f.width;
            let height = f.height;
            quote! {
                unsafe {
                        Sprite::new(
                        &PALETTES[#assignment],
                        align_bytes!(u16, #data),
                        Size::from_width_height(#width, #height)
                    )
                }
            }
        });

    let tags = tags.iter().flat_map(|(tag, num_images)| {
        tag.iter().map(move |tag| {
            let start = tag.from_frame() as usize + num_images;
            let end = tag.to_frame() as usize + num_images;
            let direction = tag.animation_direction() as usize;

            let name = tag.name();
            assert!(start <= end, "Tag {name} has start > end");

            quote! {
                (#name, Tag::new(SPRITES, #start, #end, #direction))
            }
        })
    });

    let include_paths = filenames.iter().map(|s| {
        let s = s.as_os_str().to_string_lossy();
        quote! {
            const _: &[u8] = include_bytes!(#s);
        }
    });

    let module = quote! {
        #(#include_paths)*


        const PALETTES: &[Palette16] = &[
            #(#palette_data),*
        ];

        pub const SPRITES: &[Sprite] = &[
            #(#sprites),*
        ];

        const TAGS: &TagMap = &TagMap::new(
            &[
                #(#tags),*
            ]
        );

    };

    TokenStream::from(module)
}

fn convert_image(
    settings: &dyn config::Image,
    parent: &Path,
    variable_name: &str,
    crate_prefix: &str,
    optimisation_results: &Palette16OptimisationResults,
    assignment_offset: Option<usize>,
) -> proc_macro2::TokenStream {
    let image_filename = &parent.join(settings.filename());
    let image = Image::load_from_file(image_filename);
    let deduplicate = settings.deduplicate();

    rust_generator::generate_code(
        variable_name,
        optimisation_results,
        &image,
        &image_filename.to_string_lossy(),
        crate_prefix.to_owned(),
        assignment_offset,
        deduplicate,
    )
}

fn add_to_optimiser(
    palette_optimiser: &mut palette16::Palette16Optimiser,
    image: &Image,
    tile_width: usize,
    tile_height: usize,
    transparent_colour: Option<Colour>,
) {
    let tiles_x = image.width / tile_width;
    let tiles_y = image.height / tile_height;

    for y in 0..tiles_y {
        for x in 0..tiles_x {
            let mut palette = palette16::Palette16::new();

            for j in 0..tile_height {
                for i in 0..tile_width {
                    let colour = image.colour(x * tile_width + i, y * tile_height + j);

                    palette.add_colour(match (colour.is_transparent(), transparent_colour) {
                        (true, Some(transparent_colour)) => transparent_colour,
                        _ => colour,
                    });
                }
            }

            palette_optimiser.add_palette(palette);
        }
    }
}

fn palette_tile_data(
    optimiser: &Palette16OptimisationResults,
    images: &[Image],
) -> (Vec<Vec<u16>>, Vec<u8>, Vec<usize>) {
    let palette_data: Vec<Vec<u16>> = optimiser
        .optimised_palettes
        .iter()
        .map(|palette| {
            palette
                .clone()
                .into_iter()
                .map(|colour| colour.to_rgb15())
                .chain(iter::repeat(0))
                .take(16)
                .collect()
        })
        .collect();

    let mut tile_data = Vec::new();

    for (image_idx, image) in images.iter().enumerate() {
        add_image_to_tile_data(
            &mut tile_data,
            image,
            optimiser,
            image_idx,
            true,
            &(0..images.len()).collect::<Vec<_>>(),
        );
    }

    let tile_data = collapse_to_4bpp(&tile_data);

    let assignments = optimiser.assignments.clone();

    (palette_data, tile_data, assignments)
}

fn collapse_to_4bpp(tile_data: &[u8]) -> Vec<u8> {
    tile_data
        .chunks(2)
        .map(|chunk| chunk[0] | (chunk[1] << 4))
        .collect()
}

fn add_image_to_tile_data(
    tile_data: &mut Vec<u8>,
    image: &Image,
    optimiser: &Palette16OptimisationResults,
    assignment_offset: usize,
    is_sprite: bool,
    remap_index: &[usize],
) {
    let tile_size = 8;
    let tiles_x = image.width / tile_size;
    let tiles_y = image.height / tile_size;

    for y in 0..tiles_y {
        for x in 0..tiles_x {
            let assignment = if is_sprite {
                assignment_offset
            } else {
                remap_index[y * tiles_x + x] + assignment_offset
            };

            let palette_index = optimiser.assignments[assignment];
            let palette = &optimiser.optimised_palettes[palette_index];

            for inner_y in 0..tile_size / 8 {
                for inner_x in 0..tile_size / 8 {
                    for j in inner_y * 8..inner_y * 8 + 8 {
                        for i in inner_x * 8..inner_x * 8 + 8 {
                            let colour = image.colour(x * tile_size + i, y * tile_size + j);
                            tile_data.push(palette.colour_index(colour));
                        }
                    }
                }
            }
        }
    }
}

fn add_image_256_to_tile_data(
    tile_data: &mut Vec<u8>,
    image: &Image,
    optimiser: &Palette16OptimisationResults,
) {
    let tile_size = 8;
    let tiles_x = image.width / tile_size;
    let tiles_y = image.height / tile_size;

    let all_colours: Vec<_> = optimiser
        .optimised_palettes
        .iter()
        .flat_map(|p| p.colours())
        .collect();

    for y in 0..tiles_y {
        for x in 0..tiles_x {
            for inner_y in 0..tile_size / 8 {
                for inner_x in 0..tile_size / 8 {
                    for j in inner_y * 8..inner_y * 8 + 8 {
                        for i in inner_x * 8..inner_x * 8 + 8 {
                            let colour = image.colour(x * tile_size + i, y * tile_size + j);
                            tile_data.push(all_colours.iter().position(|c| **c == colour).unwrap() as u8);
                        }
                    }
                }
            }
        }
    }
}

fn flatten_group(expr: &Expr) -> &Expr {
    match expr {
        Expr::Group(group) => &group.expr,
        _ => expr,
    }
}

#[proc_macro]
pub fn include_font(input: TokenStream) -> TokenStream {
    let parser = Punctuated::<Expr, syn::Token![,]>::parse_separated_nonempty;
    let parsed = match parser.parse(input) {
        Ok(e) => e,
        Err(e) => return e.to_compile_error().into(),
    };

    let all_args: Vec<_> = parsed.into_iter().collect();
    if all_args.len() != 2 {
        panic!("Include_font requires 2 arguments, got {}", all_args.len());
    }

    let filename = match flatten_group(&all_args[0]) {
        Expr::Lit(ExprLit {
            lit: Lit::Str(str_lit),
            ..
        }) => str_lit.value(),
        _ => panic!("Expected literal string as first argument to include_font"),
    };

    let font_size = match flatten_group(&all_args[1]) {
        Expr::Lit(ExprLit {
            lit: Lit::Float(value),
            ..
        }) => value.base10_parse::<f32>().expect("Invalid float literal"),
        Expr::Lit(ExprLit {
            lit: Lit::Int(value),
            ..
        }) => value
            .base10_parse::<i32>()
            .expect("Invalid integer literal") as f32,
        _ => panic!("Expected literal float or integer as second argument to include_font"),
    };

    let root = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get cargo manifest dir");
    let path = Path::new(&root).join(&*filename);

    let file_content = std::fs::read(&path).expect("Failed to read ttf file");

    let rendered = font_loader::load_font(&file_content, font_size);

    let include_path = path.to_string_lossy();

    quote!({
        let _ = include_bytes!(#include_path);

        #rendered
    })
    .into()
}

fn valid_sprite_size(width: u32, height: u32) -> bool {
    match (width, height) {
        (8, 8) => true,
        (16, 16) => true,
        (32, 32) => true,
        (64, 64) => true,
        (16, 8) => true,
        (32, 8) => true,
        (32, 16) => true,
        (64, 32) => true,
        (8, 16) => true,
        (8, 32) => true,
        (16, 32) => true,
        (32, 64) => true,
        (_, _) => false,
    }
}

const OUT_DIR_TOKEN: &str = "$OUT_DIR";

fn get_out_dir(raw_input: &str) -> String {
    if raw_input.contains(OUT_DIR_TOKEN) {
        std::env::var("OUT_DIR").expect("Failed to get OUT_DIR")
    } else {
        String::new()
    }
}

#[cfg(test)]
mod tests {
    use asefile::AnimationDirection;

    #[test]
    // These directions defined in agb and have these values. This is important
    // when outputting code for agb. If more animation directions are added then
    // we will have to support them there.
    fn directions_to_agb() {
        assert_eq!(AnimationDirection::Forward as usize, 0);
        assert_eq!(AnimationDirection::Reverse as usize, 1);
        assert_eq!(AnimationDirection::PingPong as usize, 2);
    }
}