comparison formats/src/th06/anm0.rs @ 765:2a5279168d5a

formats: Use a BTreeMap instead of a HashMap for scripts
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Tue, 30 Aug 2022 17:03:02 +0200
parents 21b186be2590
children 8a3b8e2ffa24
comparison
equal deleted inserted replaced
764:d18c0bf11138 765:2a5279168d5a
5 bytes::complete::{tag, take_while_m_n}, 5 bytes::complete::{tag, take_while_m_n},
6 number::complete::{le_u8, le_u16, le_u32, le_i32, le_f32}, 6 number::complete::{le_u8, le_u16, le_u32, le_i32, le_f32},
7 sequence::tuple, 7 sequence::tuple,
8 multi::{many_m_n, many0}, 8 multi::{many_m_n, many0},
9 }; 9 };
10 use std::collections::HashMap; 10 use std::collections::BTreeMap;
11 11
12 /// Coordinates of a sprite into the image. 12 /// Coordinates of a sprite into the image.
13 #[derive(Debug, Clone)] 13 #[derive(Debug, Clone)]
14 pub struct Sprite { 14 pub struct Sprite {
15 /// Index inside the anm0. 15 /// Index inside the anm0.
43 pub struct Script { 43 pub struct Script {
44 /// List of instructions in this script. 44 /// List of instructions in this script.
45 pub instructions: Vec<Call>, 45 pub instructions: Vec<Call>,
46 46
47 /// List of interrupts in this script. 47 /// List of interrupts in this script.
48 pub interrupts: HashMap<i32, u8> 48 pub interrupts: BTreeMap<i32, u8>
49 } 49 }
50 50
51 /// Main struct of the ANM0 animation format. 51 /// Main struct of the ANM0 animation format.
52 #[derive(Debug, Clone)] 52 #[derive(Debug, Clone)]
53 pub struct Anm0 { 53 pub struct Anm0 {
65 65
66 /// A list of sprites, coordinates into the attached image. 66 /// A list of sprites, coordinates into the attached image.
67 pub sprites: Vec<Sprite>, 67 pub sprites: Vec<Sprite>,
68 68
69 /// A map of scripts. 69 /// A map of scripts.
70 pub scripts: HashMap<u8, Script>, 70 pub scripts: BTreeMap<u8, Script>,
71 } 71 }
72 72
73 impl Anm0 { 73 impl Anm0 {
74 /// Parse a slice of bytes into an `Anm0` struct. 74 /// Parse a slice of bytes into an `Anm0` struct.
75 pub fn from_slice(data: &[u8]) -> IResult<&[u8], Vec<Anm0>> { 75 pub fn from_slice(data: &[u8]) -> IResult<&[u8], Vec<Anm0>> {
214 i = &input[offset..]; 214 i = &input[offset..];
215 let (_, sprite) = parse_sprite(i)?; 215 let (_, sprite) = parse_sprite(i)?;
216 sprites.push(sprite); 216 sprites.push(sprite);
217 } 217 }
218 218
219 let mut scripts = HashMap::new(); 219 let mut scripts = BTreeMap::new();
220 for (index, offset) in script_offsets.into_iter().map(|(index, offset)| (index as u8, offset as usize)) { 220 for (index, offset) in script_offsets.into_iter().map(|(index, offset)| (index as u8, offset as usize)) {
221 if input.len() < offset { 221 if input.len() < offset {
222 return Err(nom::Err::Failure(nom::error::Error::new(input, nom::error::ErrorKind::Eof))); 222 return Err(nom::Err::Failure(nom::error::Error::new(input, nom::error::ErrorKind::Eof)));
223 } 223 }
224 i = &input[offset..]; 224 i = &input[offset..];
235 i = i2; 235 i = i2;
236 if opcode == 0 { 236 if opcode == 0 {
237 break; 237 break;
238 } 238 }
239 } 239 }
240 let mut interrupts = HashMap::new(); 240 let mut interrupts = BTreeMap::new();
241 let mut j = 0; 241 let mut j = 0;
242 for Call { time: _, instr } in &mut instructions { 242 for Call { time: _, instr } in &mut instructions {
243 match instr { 243 match instr {
244 Instruction::Jump(ref mut offset) => { 244 Instruction::Jump(ref mut offset) => {
245 let result = instruction_offsets.binary_search(&(*offset as usize)); 245 let result = instruction_offsets.binary_search(&(*offset as usize));