Mercurial > touhou
annotate src/th06/anm0.rs @ 726:187359932584
Update dependencies.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 30 Oct 2019 14:48:03 +0100 |
parents | 258f4aebf3fc |
children | fc937d93a57c |
rev | line source |
---|---|
638 | 1 //! ANM0 animation format support. |
2 | |
3 use nom::{ | |
4 IResult, | |
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 | 9 }; |
10 use std::collections::HashMap; | |
11 | |
12 /// Coordinates of a sprite into the image. | |
13 #[derive(Debug, Clone)] | |
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 | 29 } |
30 | |
31 /// A single instruction, part of a `Script`. | |
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 | 39 } |
40 | |
41 /// Script driving an animation. | |
42 #[derive(Debug, Clone)] | |
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. |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
48 pub interrupts: HashMap<i32, u8> |
638 | 49 } |
50 | |
51 /// Main struct of the ANM0 animation format. | |
52 #[derive(Debug, Clone)] | |
53 pub struct Anm0 { | |
54 /// Resolution of the image used by this ANM. | |
55 pub size: (u32, u32), | |
56 | |
57 /// Format of this ANM. | |
58 pub format: u32, | |
59 | |
60 /// File name of the main image. | |
704
84af5bedbde4
anmrenderer: also load the alpha PNG.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
702
diff
changeset
|
61 pub png_filename: String, |
638 | 62 |
63 /// 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
|
64 pub alpha_filename: Option<String>, |
638 | 65 |
66 /// A list of sprites, coordinates into the attached image. | |
67 pub sprites: Vec<Sprite>, | |
68 | |
69 /// A map of scripts. | |
70 pub scripts: HashMap<u8, Script>, | |
71 } | |
72 | |
73 impl Anm0 { | |
74 /// 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
|
75 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
|
76 many0(parse_anm0)(data) |
638 | 77 } |
643
01849ffd0180
Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
639
diff
changeset
|
78 |
01849ffd0180
Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
639
diff
changeset
|
79 /// TODO |
01849ffd0180
Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
639
diff
changeset
|
80 pub fn inv_size(&self) -> (f32, f32) { |
01849ffd0180
Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
639
diff
changeset
|
81 let (x, y) = self.size; |
01849ffd0180
Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
639
diff
changeset
|
82 (1. / x as f32, 1. / y as f32) |
01849ffd0180
Add an anmrenderer binary.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
639
diff
changeset
|
83 } |
638 | 84 } |
85 | |
702
718348c7608e
anm0: simplify parsing with more combinators.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
701
diff
changeset
|
86 fn parse_name(i: &[u8]) -> IResult<&[u8], String> { |
638 | 87 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
|
88 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
|
89 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
|
90 // XXX: use a more specific error instead. |
3ff1af76e413
anm0: only use recoverable errors, no panics except for anm0 asserts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
645
diff
changeset
|
91 Err(_) => return Err(nom::Err::Failure((i, nom::error::ErrorKind::Eof))) |
3ff1af76e413
anm0: only use recoverable errors, no panics except for anm0 asserts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
645
diff
changeset
|
92 }; |
638 | 93 Ok((i, string)) |
94 } | |
95 | |
96 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
|
97 let (i, (index, x, y, width, height)) = tuple((le_u32, le_f32, le_f32, le_f32, le_f32))(i)?; |
638 | 98 Ok((i, Sprite { |
99 index, | |
100 x, | |
101 y, | |
102 width, | |
103 height, | |
104 })) | |
105 } | |
106 | |
639
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
107 macro_rules! declare_anm_instructions { |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
108 ($($opcode:tt => fn $name:ident($($arg:ident: $arg_type:ident),*)),*,) => { |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
109 /// Available instructions in an `Anm0`. |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
110 #[allow(missing_docs)] |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
111 #[derive(Debug, Clone, Copy)] |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
112 pub enum Instruction { |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
113 $( |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
114 $name($($arg_type),*) |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
115 ),* |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
116 } |
638 | 117 |
694
3ff1af76e413
anm0: only use recoverable errors, no panics except for anm0 asserts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
645
diff
changeset
|
118 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
|
119 let instr = match opcode { |
638 | 120 $( |
121 $opcode => { | |
122 $( | |
639
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
123 let (i2, $arg) = concat_idents!(le_, $arg_type)(i)?; |
638 | 124 i = i2; |
125 )* | |
639
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
126 Instruction::$name($($arg),*) |
638 | 127 } |
128 )* | |
694
3ff1af76e413
anm0: only use recoverable errors, no panics except for anm0 asserts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
645
diff
changeset
|
129 // XXX: use a more specific error instead. |
3ff1af76e413
anm0: only use recoverable errors, no panics except for anm0 asserts.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
645
diff
changeset
|
130 _ => return Err(nom::Err::Failure((i, nom::error::ErrorKind::Eof))) |
639
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
131 }; |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
132 Ok((i, instr)) |
638 | 133 } |
134 }; | |
135 } | |
136 | |
137 declare_anm_instructions!{ | |
639
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
138 0 => fn Delete(), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
139 1 => fn LoadSprite(sprite_number: u32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
140 2 => fn SetScale(sx: f32, sy: f32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
141 3 => fn SetAlpha(alpha: u32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
142 4 => fn SetColor(red: u8, green: u8, blue: u8/*, XXX: x8*/), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
143 5 => fn Jump(instruction: u32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
144 7 => fn ToggleMirrored(), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
145 9 => fn SetRotations3d(x: f32, y: f32, z: f32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
146 10 => fn SetRotationsSpeed3d(x: f32, y: f32, z: f32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
147 11 => fn SetScaleSpeed(sx: f32, sy: f32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
148 12 => fn Fade(alpha: u32, duration: u32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
149 13 => fn SetBlendmodeAdd(), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
150 14 => fn SetBlendmodeAlphablend(), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
151 15 => fn KeepStill(), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
152 16 => fn LoadRandomSprite(min_index: u32, amplitude: u32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
153 17 => fn Move(x: f32, y: f32, z: f32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
154 18 => fn MoveToLinear(x: f32, y: f32, z: f32, duration: u32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
155 19 => fn MoveToDecel(x: f32, y: f32, z: f32, duration: u32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
156 20 => fn MoveToAccel(x: f32, y: f32, z: f32, duration: u32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
157 21 => fn Wait(), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
158 22 => fn InterruptLabel(label: i32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
159 23 => fn SetCornerRelativePlacement(), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
160 24 => fn WaitEx(), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
161 25 => fn SetAllowOffset(allow: u32), // TODO: better name |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
162 26 => fn SetAutomaticOrientation(automatic: u32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
163 27 => fn ShiftTextureX(dx: f32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
164 28 => fn ShiftTextureY(dy: f32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
165 29 => fn SetVisible(visible: u32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
166 30 => fn ScaleIn(sx: f32, sy: f32, duration: u32), |
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
167 31 => fn Todo(todo: u32), |
638 | 168 } |
169 | |
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
|
170 fn parse_anm0(input: &[u8]) -> IResult<&[u8], 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
|
171 let (i, (num_sprites, num_scripts, _, width, height, format, _unknown1, |
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
|
172 first_name_offset, _unused, second_name_offset, version, _unknown2, |
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
|
173 _texture_offset, has_data, _next_offset, unknown3)) = |
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 tuple((le_u32, le_u32, tag(b"\0\0\0\0"), le_u32, le_u32, le_u32, le_u32, le_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
|
175 le_u32, le_u32, le_u32, le_u32, le_u32, le_u32, le_u32, le_u32))(input)?; |
638 | 176 |
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
|
177 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
|
178 assert_eq!(unknown3, 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
|
179 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
|
180 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
|
181 let num_scripts = num_scripts as usize; |
638 | 182 |
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
|
183 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
|
184 let (_, script_offsets) = many_m_n(num_scripts, num_scripts, tuple((le_u32, le_u32)))(i)?; |
638 | 185 |
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
|
186 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
|
187 if input.len() < 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
|
188 return Err(nom::Err::Failure((input, nom::error::ErrorKind::Eof))); |
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
|
189 } |
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 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
|
191 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
|
192 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
|
193 } 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
|
194 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
|
195 }; |
638 | 196 |
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
|
197 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
|
198 if input.len() < 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
|
199 return Err(nom::Err::Failure((input, nom::error::ErrorKind::Eof))); |
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
|
200 } |
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 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
|
202 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
|
203 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
|
204 } 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
|
205 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
|
206 }; |
638 | 207 |
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
|
208 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
|
209 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
|
210 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
|
211 if input.len() < 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
|
212 return Err(nom::Err::Failure((input, nom::error::ErrorKind::Eof))); |
638 | 213 } |
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
|
214 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
|
215 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
|
216 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
|
217 } |
638 | 218 |
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
|
219 let mut scripts = HashMap::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
|
220 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
|
221 if input.len() < 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
|
222 return Err(nom::Err::Failure((input, nom::error::ErrorKind::Eof))); |
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
|
223 } |
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 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
|
225 let mut instruction_offsets = vec![]; |
638 | 226 |
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 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
|
228 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
|
229 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
|
230 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
|
231 // 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
|
232 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
|
233 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
|
234 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
|
235 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
|
236 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
|
237 break; |
638 | 238 } |
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
|
239 } |
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 let mut interrupts = HashMap::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
|
241 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
|
242 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
|
243 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
|
244 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
|
245 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
|
246 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
|
247 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
|
248 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
|
249 // XXX: use a more specific error instead. |
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 return Err(nom::Err::Failure((input, nom::error::ErrorKind::Eof))); |
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 //println!("Instruction offset not found for pointer: {}", ptr); |
639
a8e0219162b6
Implement AnmRunner.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
638
diff
changeset
|
252 } |
638 | 253 } |
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
|
254 } |
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 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
|
256 interrupts.insert(*interrupt, j + 1); |
638 | 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 _ => () |
638 | 259 } |
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
|
260 j += 1; |
638 | 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 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
|
263 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
|
264 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
|
265 }); |
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 } |
638 | 267 |
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
|
268 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
|
269 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
|
270 format, |
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
|
271 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
|
272 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
|
273 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
|
274 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
|
275 }; |
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 Ok((i, anm0)) |
638 | 277 } |
278 | |
279 #[cfg(test)] | |
280 mod tests { | |
281 use super::*; | |
282 use std::io::{self, Read}; | |
283 use std::fs::File; | |
284 | |
285 #[test] | |
286 fn anm0() { | |
645
7bde50132735
Don’t hardcode my home directory in tests.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
643
diff
changeset
|
287 let file = File::open("EoSD/CM/player01.anm").unwrap(); |
638 | 288 let mut file = io::BufReader::new(file); |
289 let mut buf = vec![]; | |
290 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
|
291 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
|
292 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
|
293 let anm0 = anms.pop().unwrap(); |
638 | 294 assert_eq!(anm0.size, (256, 256)); |
295 assert_eq!(anm0.format, 5); | |
296 } | |
297 } |