annotate formats/src/th06/anm0.rs @ 769:cae5f15ca5ed

formats: Fix possible panic in PBG3 checksum check
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 30 Aug 2022 18:22:53 +0200
parents 8a3b8e2ffa24
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 //! ANM0 animation format support.
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 use nom::{
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 IResult,
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 bytes::complete::{tag, take_while_m_n},
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
6 number::complete::{le_u8, le_u16, le_u32, le_i32, le_f32},
702
718348c7608e anm0: simplify parsing with more combinators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
7 sequence::tuple,
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
8 multi::{many_m_n, many0},
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
9 };
765
2a5279168d5a formats: Use a BTreeMap instead of a HashMap for scripts
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 757
diff changeset
10 use std::collections::BTreeMap;
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
11
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 /// Coordinates of a sprite into the image.
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 #[derive(Debug, Clone)]
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
14 pub struct Sprite {
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
15 /// Index inside the anm0.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
16 pub index: u32,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
17
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
18 /// X coordinate in the sprite sheet.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
19 pub x: f32,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
20
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
21 /// Y coordinate in the sprite sheet.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
22 pub y: f32,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
23
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
24 /// Width of the sprite.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
25 pub width: f32,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
26
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
27 /// Height of the sprite.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
28 pub height: f32,
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 }
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 /// A single instruction, part of a `Script`.
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 #[derive(Debug, Clone)]
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
33 pub struct Call {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
34 /// Time at which this instruction will be called.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
35 pub time: u16,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
36
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
37 /// The instruction to call.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
38 pub instr: Instruction,
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 }
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 /// Script driving an animation.
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 #[derive(Debug, Clone)]
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43 pub struct Script {
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
44 /// List of instructions in this script.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
45 pub instructions: Vec<Call>,
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
46
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
47 /// List of interrupts in this script.
765
2a5279168d5a formats: Use a BTreeMap instead of a HashMap for scripts
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 757
diff changeset
48 pub interrupts: BTreeMap<i32, u8>
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 }
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 /// Main struct of the ANM0 animation format.
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 #[derive(Debug, Clone)]
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 pub struct Anm0 {
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 /// Resolution of the image used by this ANM.
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 pub size: (u32, u32),
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57 /// Format of this ANM.
766
8a3b8e2ffa24 formats: Expose color_key on Anm0
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 765
diff changeset
58 // TODO: use an enum for that.
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 pub format: u32,
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60
766
8a3b8e2ffa24 formats: Expose color_key on Anm0
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 765
diff changeset
61 /// Color key, probably used for transparency.
8a3b8e2ffa24 formats: Expose color_key on Anm0
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 765
diff changeset
62 pub color_key: u32,
8a3b8e2ffa24 formats: Expose color_key on Anm0
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 765
diff changeset
63
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 /// File name of the main image.
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 702
diff changeset
65 pub png_filename: String,
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 /// File name of an alpha channel image.
704
84af5bedbde4 anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 702
diff changeset
68 pub alpha_filename: Option<String>,
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 /// A list of sprites, coordinates into the attached image.
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 pub sprites: Vec<Sprite>,
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 /// A map of scripts.
765
2a5279168d5a formats: Use a BTreeMap instead of a HashMap for scripts
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 757
diff changeset
74 pub scripts: BTreeMap<u8, Script>,
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75 }
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77 impl Anm0 {
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 /// Parse a slice of bytes into an `Anm0` struct.
701
b6c351ca0a35 anm0: return the nom IResult and the list of Anm0s.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 694
diff changeset
79 pub fn from_slice(data: &[u8]) -> IResult<&[u8], Vec<Anm0>> {
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
80 many0(parse_anm0)(data)
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 }
643
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
82
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
83 /// TODO
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
84 pub fn inv_size(&self) -> (f32, f32) {
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
85 let (x, y) = self.size;
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
86 (1. / x as f32, 1. / y as f32)
01849ffd0180 Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 639
diff changeset
87 }
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 }
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89
702
718348c7608e anm0: simplify parsing with more combinators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
90 fn parse_name(i: &[u8]) -> IResult<&[u8], String> {
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 let (_, slice) = take_while_m_n(0, 32, |c| c != 0)(i)?;
694
3ff1af76e413 anm0: only use recoverable errors, no panics except for anm0 asserts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
92 let string = match String::from_utf8(slice.to_vec()) {
3ff1af76e413 anm0: only use recoverable errors, no panics except for anm0 asserts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
93 Ok(string) => string,
3ff1af76e413 anm0: only use recoverable errors, no panics except for anm0 asserts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
94 // XXX: use a more specific error instead.
755
fc937d93a57c Bump nom to version 6, and image to 0.23.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 713
diff changeset
95 Err(_) => return Err(nom::Err::Failure(nom::error::Error::new(i, nom::error::ErrorKind::Eof)))
694
3ff1af76e413 anm0: only use recoverable errors, no panics except for anm0 asserts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
96 };
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 Ok((i, string))
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 }
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 fn parse_sprite(i: &[u8]) -> IResult<&[u8], Sprite> {
702
718348c7608e anm0: simplify parsing with more combinators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 701
diff changeset
101 let (i, (index, x, y, width, height)) = tuple((le_u32, le_f32, le_f32, le_f32, le_f32))(i)?;
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 Ok((i, Sprite {
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 index,
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 x,
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 y,
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 width,
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107 height,
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 }))
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 }
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
111 macro_rules! declare_anm_instructions {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
112 ($($opcode:tt => fn $name:ident($($arg:ident: $arg_type:ident),*)),*,) => {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
113 /// Available instructions in an `Anm0`.
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
114 #[allow(missing_docs)]
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
115 #[derive(Debug, Clone, Copy)]
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
116 pub enum Instruction {
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
117 $(
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
118 $name($($arg_type),*)
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
119 ),*
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
120 }
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121
694
3ff1af76e413 anm0: only use recoverable errors, no panics except for anm0 asserts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
122 fn parse_instruction_args(mut i: &[u8], opcode: u8) -> IResult<&[u8], Instruction> {
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
123 let instr = match opcode {
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 $(
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125 $opcode => {
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126 $(
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
127 let (i2, $arg) = concat_idents!(le_, $arg_type)(i)?;
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 i = i2;
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129 )*
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
130 Instruction::$name($($arg),*)
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 }
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 )*
694
3ff1af76e413 anm0: only use recoverable errors, no panics except for anm0 asserts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
133 // XXX: use a more specific error instead.
755
fc937d93a57c Bump nom to version 6, and image to 0.23.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 713
diff changeset
134 _ => return Err(nom::Err::Failure(nom::error::Error::new(i, nom::error::ErrorKind::Eof)))
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
135 };
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
136 Ok((i, instr))
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
137 }
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
138 };
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
139 }
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141 declare_anm_instructions!{
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
142 0 => fn Delete(),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
143 1 => fn LoadSprite(sprite_number: u32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
144 2 => fn SetScale(sx: f32, sy: f32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
145 3 => fn SetAlpha(alpha: u32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
146 4 => fn SetColor(red: u8, green: u8, blue: u8/*, XXX: x8*/),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
147 5 => fn Jump(instruction: u32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
148 7 => fn ToggleMirrored(),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
149 9 => fn SetRotations3d(x: f32, y: f32, z: f32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
150 10 => fn SetRotationsSpeed3d(x: f32, y: f32, z: f32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
151 11 => fn SetScaleSpeed(sx: f32, sy: f32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
152 12 => fn Fade(alpha: u32, duration: u32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
153 13 => fn SetBlendmodeAdd(),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
154 14 => fn SetBlendmodeAlphablend(),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
155 15 => fn KeepStill(),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
156 16 => fn LoadRandomSprite(min_index: u32, amplitude: u32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
157 17 => fn Move(x: f32, y: f32, z: f32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
158 18 => fn MoveToLinear(x: f32, y: f32, z: f32, duration: u32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
159 19 => fn MoveToDecel(x: f32, y: f32, z: f32, duration: u32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
160 20 => fn MoveToAccel(x: f32, y: f32, z: f32, duration: u32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
161 21 => fn Wait(),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
162 22 => fn InterruptLabel(label: i32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
163 23 => fn SetCornerRelativePlacement(),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
164 24 => fn WaitEx(),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
165 25 => fn SetAllowOffset(allow: u32), // TODO: better name
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
166 26 => fn SetAutomaticOrientation(automatic: u32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
167 27 => fn ShiftTextureX(dx: f32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
168 28 => fn ShiftTextureY(dy: f32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
169 29 => fn SetVisible(visible: u32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
170 30 => fn ScaleIn(sx: f32, sy: f32, duration: u32),
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
171 31 => fn Todo(todo: u32),
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 }
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
173
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
174 fn parse_anm0(input: &[u8]) -> IResult<&[u8], Anm0> {
766
8a3b8e2ffa24 formats: Expose color_key on Anm0
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 765
diff changeset
175 let (i, (num_sprites, num_scripts, _, width, height, format, color_key,
8a3b8e2ffa24 formats: Expose color_key on Anm0
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 765
diff changeset
176 first_name_offset, _, second_name_offset, version, _,
8a3b8e2ffa24 formats: Expose color_key on Anm0
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 765
diff changeset
177 _texture_offset, has_data, _next_offset, _)) =
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
178 tuple((le_u32, le_u32, tag(b"\0\0\0\0"), le_u32, le_u32, le_u32, le_u32, le_u32,
766
8a3b8e2ffa24 formats: Expose color_key on Anm0
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 765
diff changeset
179 tag(b"\0\0\0\0"), le_u32, le_u32, tag(b"\0\0\0\0"), le_u32, le_u32, le_u32,
8a3b8e2ffa24 formats: Expose color_key on Anm0
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 765
diff changeset
180 tag(b"\0\0\0\0")))(input)?;
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
181
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
182 assert_eq!(version, 0);
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
183 assert_eq!(has_data, 0);
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
184 let num_sprites = num_sprites as usize;
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
185 let num_scripts = num_scripts as usize;
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
186
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
187 let (i, sprite_offsets) = many_m_n(num_sprites, num_sprites, le_u32)(i)?;
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
188 let (_, script_offsets) = many_m_n(num_scripts, num_scripts, tuple((le_u32, le_u32)))(i)?;
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
189
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
190 let png_filename = if first_name_offset > 0 {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
191 if input.len() < first_name_offset as usize {
755
fc937d93a57c Bump nom to version 6, and image to 0.23.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 713
diff changeset
192 return Err(nom::Err::Failure(nom::error::Error::new(input, nom::error::ErrorKind::Eof)));
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
193 }
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
194 let i = &input[first_name_offset as usize..];
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
195 let (_, name) = parse_name(i)?;
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
196 name
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
197 } else {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
198 String::new()
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
199 };
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
200
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
201 let alpha_filename = if second_name_offset > 0 {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
202 if input.len() < second_name_offset as usize {
755
fc937d93a57c Bump nom to version 6, and image to 0.23.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 713
diff changeset
203 return Err(nom::Err::Failure(nom::error::Error::new(input, nom::error::ErrorKind::Eof)));
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
204 }
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
205 let i = &input[second_name_offset as usize..];
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
206 let (_, name) = parse_name(i)?;
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
207 Some(name)
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
208 } else {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
209 None
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
210 };
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
211
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
212 let mut sprites = vec![];
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
213 let mut i = &input[..];
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
214 for offset in sprite_offsets.into_iter().map(|x| x as usize) {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
215 if input.len() < offset {
755
fc937d93a57c Bump nom to version 6, and image to 0.23.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 713
diff changeset
216 return Err(nom::Err::Failure(nom::error::Error::new(input, nom::error::ErrorKind::Eof)));
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
217 }
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
218 i = &input[offset..];
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
219 let (_, sprite) = parse_sprite(i)?;
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
220 sprites.push(sprite);
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
221 }
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
222
765
2a5279168d5a formats: Use a BTreeMap instead of a HashMap for scripts
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 757
diff changeset
223 let mut scripts = BTreeMap::new();
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
224 for (index, offset) in script_offsets.into_iter().map(|(index, offset)| (index as u8, offset as usize)) {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
225 if input.len() < offset {
755
fc937d93a57c Bump nom to version 6, and image to 0.23.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 713
diff changeset
226 return Err(nom::Err::Failure(nom::error::Error::new(input, nom::error::ErrorKind::Eof)));
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
227 }
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
228 i = &input[offset..];
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
229 let mut instruction_offsets = vec![];
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
230
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
231 let mut instructions = vec![];
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
232 loop {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
233 let tell = input.len() - i.len();
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
234 instruction_offsets.push(tell - offset);
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
235 // TODO: maybe check against the size of parsed data?
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
236 let (i2, (time, opcode, _size)) = tuple((le_u16, le_u8, le_u8))(i)?;
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
237 let (i2, instr) = parse_instruction_args(i2, opcode)?;
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
238 instructions.push(Call { time, instr });
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
239 i = i2;
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
240 if opcode == 0 {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
241 break;
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
242 }
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
243 }
765
2a5279168d5a formats: Use a BTreeMap instead of a HashMap for scripts
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 757
diff changeset
244 let mut interrupts = BTreeMap::new();
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
245 let mut j = 0;
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
246 for Call { time: _, instr } in &mut instructions {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
247 match instr {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
248 Instruction::Jump(ref mut offset) => {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
249 let result = instruction_offsets.binary_search(&(*offset as usize));
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
250 match result {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
251 Ok(ptr) => *offset = ptr as u32,
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
252 Err(ptr) => {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
253 // XXX: use a more specific error instead.
755
fc937d93a57c Bump nom to version 6, and image to 0.23.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 713
diff changeset
254 return Err(nom::Err::Failure(nom::error::Error::new(input, nom::error::ErrorKind::Eof)));
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
255 //println!("Instruction offset not found for pointer: {}", ptr);
639
a8e0219162b6 Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 638
diff changeset
256 }
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
257 }
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
258 }
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
259 Instruction::InterruptLabel(interrupt) => {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
260 interrupts.insert(*interrupt, j + 1);
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
261 }
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
262 _ => ()
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
263 }
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
264 j += 1;
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
265 }
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
266 scripts.insert(index, Script {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
267 instructions,
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
268 interrupts,
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
269 });
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
270 }
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
271
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
272 let anm0 = Anm0 {
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
273 size: (width, height),
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
274 format,
766
8a3b8e2ffa24 formats: Expose color_key on Anm0
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 765
diff changeset
275 color_key,
713
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
276 png_filename,
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
277 alpha_filename,
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
278 sprites,
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
279 scripts,
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
280 };
258f4aebf3fc anm0: Parse only a single anm0 at once and use many0 for more, to simplify parsing.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 704
diff changeset
281 Ok((i, anm0))
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
282 }
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
283
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
284 #[cfg(test)]
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
285 mod tests {
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
286 use super::*;
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
287 use std::io::{self, Read};
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
288 use std::fs::File;
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
289
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
290 #[test]
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
291 fn anm0() {
645
7bde50132735 Don’t hardcode my home directory in tests.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 643
diff changeset
292 let file = File::open("EoSD/CM/player01.anm").unwrap();
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
293 let mut file = io::BufReader::new(file);
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
294 let mut buf = vec![];
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
295 file.read_to_end(&mut buf).unwrap();
701
b6c351ca0a35 anm0: return the nom IResult and the list of Anm0s.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 694
diff changeset
296 let (_, mut anms) = Anm0::from_slice(&buf).unwrap();
b6c351ca0a35 anm0: return the nom IResult and the list of Anm0s.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 694
diff changeset
297 assert_eq!(anms.len(), 1);
b6c351ca0a35 anm0: return the nom IResult and the list of Anm0s.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 694
diff changeset
298 let anm0 = anms.pop().unwrap();
638
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
299 assert_eq!(anm0.size, (256, 256));
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
300 assert_eq!(anm0.format, 5);
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
301 }
a806f28e94fc Add anm0 support.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
302 }