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