hypercutter 0.4.0

Extract data from GBA Pokemon ROMs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! ROM loading and game identification.

use sha2::{Digest, Sha256};

use crate::error::{Error, Result};

/// Default GBA ROM base address.
pub const DEFAULT_ROM_BASE_ADDRESS: u32 = 0x0800_0000;

/// Byte offset of the 4-byte game code within the ROM header.
pub const GAME_CODE_OFFSET: usize = 0xAC;

/// Length of the game code in bytes.
pub const GAME_CODE_LENGTH: usize = 4;

/// A fully loaded GBA Pokemon ROM.
#[derive(Debug, Clone)]
pub struct Rom {
    bytes: Vec<u8>,
    game: Game,
    base_address: u32,
}

impl Rom {
    /// Load a ROM from a file on disk and identify its game.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Io`] if the file cannot be read. See [`Self::from_bytes`]
    /// for additional error conditions.
    pub fn open(path: impl AsRef<std::path::Path>) -> Result<Self> {
        let path = path.as_ref();
        let bytes = std::fs::read(path).map_err(|source| Error::Io {
            path: path.to_path_buf(),
            source,
        })?;
        Self::from_bytes(bytes)
    }

    /// Construct a ROM from already-loaded bytes and identify its game.
    ///
    /// # Errors
    ///
    /// Returns [`Error::RomTooSmall`] if `bytes` is smaller than the header,
    /// or [`Error::InvalidGameCode`] if the game code does not match a known
    /// game.
    pub fn from_bytes(bytes: Vec<u8>) -> Result<Self> {
        if bytes.len() < GAME_CODE_OFFSET + GAME_CODE_LENGTH {
            return Err(Error::RomTooSmall {
                size: bytes.len(),
                needed: GAME_CODE_OFFSET + GAME_CODE_LENGTH,
            });
        }
        let code = [
            bytes[GAME_CODE_OFFSET],
            bytes[GAME_CODE_OFFSET + 1],
            bytes[GAME_CODE_OFFSET + 2],
            bytes[GAME_CODE_OFFSET + 3],
        ];
        let game = Game::from_code(code).ok_or(Error::InvalidGameCode { code })?;
        Ok(Self {
            bytes,
            game,
            base_address: DEFAULT_ROM_BASE_ADDRESS,
        })
    }

    /// Returns the identified game.
    #[must_use]
    pub const fn game(&self) -> Game {
        self.game
    }

    /// Returns the raw ROM bytes.
    #[must_use]
    pub fn bytes(&self) -> &[u8] {
        &self.bytes
    }

    /// Returns the GBA base address (default `0x0800_0000`).
    #[must_use]
    pub const fn base_address(&self) -> u32 {
        self.base_address
    }

    /// Convert a ROM address to a file offset.
    ///
    /// # Errors
    ///
    /// Returns [`Error::OutOfRange`] if the resulting offset is outside the
    /// loaded bytes.
    pub fn offset_of(&self, address: u32) -> Result<usize> {
        let offset = address
            .checked_sub(self.base_address)
            .ok_or(Error::OutOfRange {
                offset: address,
                size: self.bytes.len() as u32,
            })?;
        let offset = offset as usize;
        if offset >= self.bytes.len() {
            return Err(Error::OutOfRange {
                offset: address,
                size: self.bytes.len() as u32,
            });
        }
        Ok(offset)
    }

    /// Borrow a slice of `len` bytes starting at `address`.
    ///
    /// # Errors
    ///
    /// Returns [`Error::OutOfRange`] if the range falls outside the ROM.
    pub fn slice_at(&self, address: u32, len: usize) -> Result<&[u8]> {
        let start = self.offset_of(address)?;
        let end = start.checked_add(len).ok_or(Error::OutOfRange {
            offset: address.wrapping_add(len as u32),
            size: self.bytes.len() as u32,
        })?;
        if end > self.bytes.len() {
            return Err(Error::OutOfRange {
                offset: address,
                size: self.bytes.len() as u32,
            });
        }
        Ok(&self.bytes[start..end])
    }

    /// Returns the SHA-256 hash of the ROM bytes, as lowercase hex.
    #[must_use]
    pub fn sha256(&self) -> String {
        let mut out = String::with_capacity(64);
        for b in Sha256::digest(&self.bytes) {
            use std::fmt::Write;
            let _ = write!(out, "{b:02x}");
        }
        out
    }
}

/// One of the five supported GBA Pokemon games.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Game {
    /// Pokemon Emerald.
    Emerald,
    /// Pokemon FireRed.
    FireRed,
    /// Pokemon LeafGreen.
    LeafGreen,
    /// Pokemon Ruby.
    Ruby,
    /// Pokemon Sapphire.
    Sapphire,
}

impl Game {
    /// All supported games.
    pub const ALL: &'static [Self] = &[
        Self::Emerald,
        Self::FireRed,
        Self::LeafGreen,
        Self::Ruby,
        Self::Sapphire,
    ];

    /// Look up a game by its 4-byte ROM code.
    #[must_use]
    pub const fn from_code(code: [u8; 4]) -> Option<Self> {
        match &code {
            b"BPEE" => Some(Self::Emerald),
            b"BPRE" => Some(Self::FireRed),
            b"BPGE" => Some(Self::LeafGreen),
            b"AXVE" => Some(Self::Ruby),
            b"AXPE" => Some(Self::Sapphire),
            _ => None,
        }
    }

    /// Look up a game by its short or full name (case-insensitive).
    #[must_use]
    pub fn from_name(name: &str) -> Option<Self> {
        let normalized: String = name
            .to_ascii_lowercase()
            .chars()
            .filter(|c| !c.is_whitespace())
            .collect();
        match normalized.as_str() {
            "emerald" | "pokemonemerald" => Some(Self::Emerald),
            "firered" | "pokemonfirered" => Some(Self::FireRed),
            "leafgreen" | "pokemonleafgreen" => Some(Self::LeafGreen),
            "ruby" | "pokemonruby" => Some(Self::Ruby),
            "sapphire" | "pokemonsapphire" => Some(Self::Sapphire),
            _ => None,
        }
    }

    /// 4-byte ROM code for this game.
    #[must_use]
    pub const fn code(self) -> [u8; 4] {
        match self {
            Self::Emerald => *b"BPEE",
            Self::FireRed => *b"BPRE",
            Self::LeafGreen => *b"BPGE",
            Self::Ruby => *b"AXVE",
            Self::Sapphire => *b"AXPE",
        }
    }

    /// Full display name.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::Emerald => "Pokemon Emerald",
            Self::FireRed => "Pokemon FireRed",
            Self::LeafGreen => "Pokemon LeafGreen",
            Self::Ruby => "Pokemon Ruby",
            Self::Sapphire => "Pokemon Sapphire",
        }
    }

    /// Short identifier used in CLI flags and the `.sym` URL.
    #[must_use]
    pub const fn short_name(self) -> &'static str {
        match self {
            Self::Emerald => "emerald",
            Self::FireRed => "firered",
            Self::LeafGreen => "leafgreen",
            Self::Ruby => "ruby",
            Self::Sapphire => "sapphire",
        }
    }

    /// pret disassembly repository name on GitHub.
    #[must_use]
    pub const fn sym_repo(self) -> &'static str {
        match self {
            Self::Emerald => "pokeemerald",
            Self::FireRed | Self::LeafGreen => "pokefirered",
            Self::Ruby | Self::Sapphire => "pokeruby",
        }
    }

    /// Default symbol file name for this game.
    #[must_use]
    pub const fn default_sym_file(self) -> &'static str {
        match self {
            Self::Emerald => "pokeemerald.sym",
            Self::FireRed | Self::LeafGreen => "pokefirered.sym",
            Self::Ruby | Self::Sapphire => "pokeruby.sym",
        }
    }

    /// URL to download the default `.sym` file for this game.
    #[must_use]
    pub fn sym_url(self) -> String {
        format!(
            "https://cdn.jsdelivr.net/gh/pret/{repo}@symbols/{file}",
            repo = self.sym_repo(),
            file = self.default_sym_file(),
        )
    }

    /// URL to download a revision-specific `.sym` file for this game.
    #[must_use]
    pub fn sym_url_for_file(self, file: &str) -> String {
        format!(
            "https://cdn.jsdelivr.net/gh/pret/{repo}@symbols/{file}",
            repo = self.sym_repo(),
            file = file,
        )
    }

    /// Revision `.sym` filenames to try when the default does not match.
    ///
    /// Returns an empty slice for games without known revisions.
    #[must_use]
    pub fn revision_sym_files(self) -> &'static [&'static str] {
        match self {
            Self::Ruby => &["pokeruby_rev1.sym", "pokeruby_rev2.sym"],
            Self::Sapphire => &["pokesapphire_rev1.sym", "pokesapphire_rev2.sym"],
            Self::FireRed => &["pokefirered_rev1.sym"],
            Self::LeafGreen => &["pokeleafgreen_rev1.sym"],
            Self::Emerald => &[],
        }
    }

    /// Number of tiles in this game's primary tileset.
    #[must_use]
    pub const fn primary_tile_count(self) -> u16 {
        match self {
            Self::FireRed | Self::LeafGreen => 0x280,
            _ => 0x200,
        }
    }
}

/// Per-game configuration that drives symbol download and extraction.
#[derive(Debug, Clone)]
pub struct GameProfile {
    /// The game this profile describes.
    pub game: Game,
    /// SHA-256 → symbol filename overrides for ROM revisions.
    pub revisions: Vec<(String, &'static str)>,
}

impl GameProfile {
    /// Return the canonical `.sym` URL for a known SHA-256, if any.
    #[must_use]
    pub fn sym_url_for(&self, sha256: &str) -> Option<String> {
        let (_, filename) = self.revisions.iter().find(|(s, _)| s == sha256)?;
        Some(format!(
            "https://cdn.jsdelivr.net/gh/pret/{repo}@symbols/{file}",
            repo = self.game.sym_repo(),
            file = filename,
        ))
    }

    /// Default `.sym` URL when the SHA-256 is not in the revision table.
    #[must_use]
    pub fn default_sym_url(&self) -> String {
        format!(
            "https://cdn.jsdelivr.net/gh/pret/{repo}@symbols/{file}",
            repo = self.game.sym_repo(),
            file = self.game.default_sym_file(),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn rom_with_code(code: &[u8; 4]) -> Vec<u8> {
        let mut bytes = vec![0u8; 0xB0];
        bytes[GAME_CODE_OFFSET..GAME_CODE_OFFSET + GAME_CODE_LENGTH].copy_from_slice(code);
        bytes
    }

    #[test]
    fn identifies_emerald() {
        let rom = Rom::from_bytes(rom_with_code(b"BPEE")).unwrap();
        assert_eq!(rom.game(), Game::Emerald);
    }

    #[test]
    fn identifies_firered() {
        let rom = Rom::from_bytes(rom_with_code(b"BPRE")).unwrap();
        assert_eq!(rom.game(), Game::FireRed);
    }

    #[test]
    fn rejects_unknown_game_code() {
        let rom = Rom::from_bytes(rom_with_code(b"XXXX"));
        assert!(rom.is_err());
    }

    #[test]
    fn rejects_too_small_rom() {
        let rom = Rom::from_bytes(vec![0u8; 0x10]);
        assert!(matches!(rom, Err(Error::RomTooSmall { .. })));
    }

    #[test]
    fn offset_of_within_bounds() {
        let rom = Rom::from_bytes(rom_with_code(b"BPEE")).unwrap();
        assert_eq!(rom.offset_of(0x0800_0000).unwrap(), 0);
    }

    #[test]
    fn offset_of_out_of_range() {
        let rom = Rom::from_bytes(rom_with_code(b"BPEE")).unwrap();
        assert!(matches!(
            rom.offset_of(0x0900_0000),
            Err(Error::OutOfRange { .. })
        ));
    }

    #[test]
    fn slice_at_within_bounds() {
        let rom = Rom::from_bytes(rom_with_code(b"BPEE")).unwrap();
        let addr = DEFAULT_ROM_BASE_ADDRESS + GAME_CODE_OFFSET as u32;
        let slice = rom.slice_at(addr, 4).unwrap();
        assert_eq!(slice, b"BPEE");
    }

    #[test]
    fn game_from_name_short() {
        assert_eq!(Game::from_name("emerald"), Some(Game::Emerald));
        assert_eq!(Game::from_name("FIRERED"), Some(Game::FireRed));
    }

    #[test]
    fn game_from_name_full() {
        assert_eq!(Game::from_name("pokemon ruby"), Some(Game::Ruby));
    }

    #[test]
    fn game_from_name_unknown() {
        assert_eq!(Game::from_name("Crystal"), None);
    }

    #[test]
    fn game_code_roundtrip() {
        for &game in Game::ALL {
            let code = game.code();
            assert_eq!(Game::from_code(code), Some(game));
        }
    }
}