uxie 0.6.3

Data fetching library for Pokemon Gen 4 romhacking - map headers, C parsing, and more
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//! Global script ID to script file mapping table.
//!
//!
//! Maps global script IDs (used by `CallCommonScript` and similar commands)
//! to their corresponding script file and text bank indices.
//!
//! ## Overview
//!
//! In Gen 4 games, scripts can be called by global IDs that span multiple files.
//! Each ID range maps to a specific script file (NARC index) and text bank.
//! For example, in Platinum:
//! - IDs 2000-2499 → `scripts_common` (file 211)
//! - IDs 3000-4999 → single battle scripts (file 1114)
//! - IDs 5000-6999 → double battle scripts (file 1114)
//!
//! ## Loading
//!
//! The table can be loaded from:
//! - **HGSS binary**: Read from arm9.bin (pointer at 0x40164)
//! - **HGSS decomp**: Parse `src/fieldmap.c` `sScriptBankMapping` table
//! - **Platinum decomp**: Parse `src/script_manager.c` `SCRIPT_RANGE_TABLE` macro
//! - **Platinum binary**: Hardcoded (no clean table in binary)

use crate::c_parser::SymbolTable;
use crate::script_file::COMMON_SCRIPT_THRESHOLD;
use byteorder::{LittleEndian, ReadBytesExt};
use regex::Regex;
use std::io::{self, Cursor, Read, Seek, SeekFrom};
use std::path::Path;
use std::sync::LazyLock;

/// HGSS arm9.bin offset where the table pointer is stored
const HGSS_TABLE_POINTER_OFFSET: u64 = 0x40164;

/// HGSS table entry count
const HGSS_TABLE_ENTRY_COUNT: usize = 30;

/// HGSS memory base address (subtracted to get arm9 offset)
const HGSS_MEMORY_BASE: u32 = 0x0200_0000;

/// Platinum table entry count
#[cfg(test)]
const PLATINUM_TABLE_ENTRY_COUNT: usize = 30;

/// Regex to match HGSS sScriptBankMapping array entries:
/// `{ _std_scratch_card, NARC_scr_seq_scr_seq_0263_bin, NARC_msg_msg_0433_bin },`
static RE_HGSS_ENTRY: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"\{\s*([A-Za-z0-9_]+)\s*,\s*([A-Za-z0-9_]+)\s*,\s*([A-Za-z0-9_]+)\s*\}").unwrap()
});

/// Regex to match SCRIPT_RANGE_TABLE macro entries:
/// `Entry(10490, scripts_unk_0499, TEXT_BANK_SCRATCH_OFF_CARDS) \`
static RE_PLATINUM_TABLE_ENTRY: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"\bEntry\s*\(\s*([A-Za-z0-9_]+)\s*,\s*([A-Za-z0-9_]+)\s*,\s*([A-Za-z0-9_]+)\s*\)")
        .unwrap()
});

fn parse_hgss_narc_member_id(s: &str) -> Option<i64> {
    let digits = s
        .strip_prefix("NARC_scr_seq_scr_seq_")
        .or_else(|| s.strip_prefix("NARC_msg_msg_"))?
        .strip_suffix("_bin")?;

    if !digits.chars().all(|c| c.is_ascii_digit()) {
        return None;
    }

    digits.parse::<i64>().ok()
}

fn resolve_value(s: &str, symbols: &SymbolTable) -> Option<i64> {
    if let Ok(v) = s.parse::<i64>() {
        return Some(v);
    }
    if s.starts_with("0x") || s.starts_with("0X") {
        if let Ok(v) = i64::from_str_radix(&s[2..], 16) {
            return Some(v);
        }
    }
    symbols
        .resolve_constant(s)
        .or_else(|| parse_hgss_narc_member_id(s))
}

/// Entry mapping a global script ID range to a script file.
///
/// Scripts with IDs >= `min_script_id` (and < the next entry's min_script_id)
/// are loaded from `script_file_id` with text from `text_archive_id`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GlobalScriptEntry {
    /// Minimum global script ID for this range (inclusive)
    pub min_script_id: u16,
    /// Script file NARC index (in fielddata/script/scr_seq)
    pub script_file_id: u16,
    /// Text archive NARC index (in msgdata/msg)
    pub text_archive_id: u16,
}

impl GlobalScriptEntry {
    /// Create a new entry
    pub const fn new(min_script_id: u16, script_file_id: u16, text_archive_id: u16) -> Self {
        Self {
            min_script_id,
            script_file_id,
            text_archive_id,
        }
    }

    /// Read an entry from a reader (6 bytes, little-endian)
    pub fn read_from<R: Read>(reader: &mut R) -> io::Result<Self> {
        let min_script_id = reader.read_u16::<LittleEndian>()?;
        let script_file_id = reader.read_u16::<LittleEndian>()?;
        let text_archive_id = reader.read_u16::<LittleEndian>()?;
        Ok(Self {
            min_script_id,
            script_file_id,
            text_archive_id,
        })
    }
}

/// Table mapping global script IDs to script files and text banks.
///
/// Entries are stored in descending order by `min_script_id` for efficient lookup.
#[derive(Debug, Clone, Default)]
pub struct GlobalScriptTable {
    /// Entries sorted by min_script_id descending
    entries: Vec<GlobalScriptEntry>,
}

impl GlobalScriptTable {
    /// Create an empty table
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a table from a list of entries.
    ///
    /// Entries will be sorted by min_script_id descending.
    pub fn from_entries(mut entries: Vec<GlobalScriptEntry>) -> Self {
        entries.sort_by(|a, b| b.min_script_id.cmp(&a.min_script_id));
        Self { entries }
    }

    /// Load from HGSS arm9.bin binary.
    ///
    /// Reads the table pointer from offset 0x40164, then reads 30 entries
    /// from that address.
    pub fn from_hgss_binary<R: Read + Seek>(reader: &mut R) -> io::Result<Self> {
        reader.seek(SeekFrom::Start(HGSS_TABLE_POINTER_OFFSET))?;
        let table_addr = reader.read_u32::<LittleEndian>()?;
        let table_offset = table_addr.saturating_sub(HGSS_MEMORY_BASE) as u64;

        reader.seek(SeekFrom::Start(table_offset))?;
        let mut entries = Vec::with_capacity(HGSS_TABLE_ENTRY_COUNT);
        for _ in 0..HGSS_TABLE_ENTRY_COUNT {
            entries.push(GlobalScriptEntry::read_from(reader)?);
        }

        Ok(Self::from_entries(entries))
    }

    /// Load from HGSS arm9.bin file path.
    pub fn from_hgss_binary_file(path: impl AsRef<Path>) -> io::Result<Self> {
        let data = std::fs::read(path)?;
        let mut cursor = Cursor::new(data);
        Self::from_hgss_binary(&mut cursor)
    }

    /// Parse HGSS `sScriptBankMapping` table from fieldmap.c source.
    ///
    /// Requires a `SymbolTable` to resolve symbolic constants like
    /// `_std_scratch_card`. Generated HGSS `NARC_*_XXXX_bin` member names are
    /// also accepted directly, so unbuilt decomp checkouts can still parse.
    pub fn from_hgss_decomp(content: &str, symbols: &SymbolTable) -> Option<Self> {
        let start = content.find("sScriptBankMapping")?;
        let block_start = content[start..].find('{')?;
        let block = &content[start + block_start..];

        let mut entries = Vec::new();
        for caps in RE_HGSS_ENTRY.captures_iter(block) {
            let script_id_sym = caps.get(1)?.as_str();
            let script_file_sym = caps.get(2)?.as_str();
            let text_archive_sym = caps.get(3)?.as_str();

            let min_script_id = resolve_value(script_id_sym, symbols)? as u16;
            let script_file_id = resolve_value(script_file_sym, symbols)? as u16;
            let text_archive_id = resolve_value(text_archive_sym, symbols)? as u16;

            entries.push(GlobalScriptEntry::new(
                min_script_id,
                script_file_id,
                text_archive_id,
            ));
        }

        if entries.is_empty() {
            return None;
        }

        Some(Self::from_entries(entries))
    }

    /// Parse HGSS `sScriptBankMapping` from fieldmap.c file path.
    pub fn from_hgss_decomp_file(
        path: impl AsRef<Path>,
        symbols: &SymbolTable,
    ) -> io::Result<Self> {
        let content = std::fs::read_to_string(path)?;
        Self::from_hgss_decomp(&content, symbols).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                "Failed to parse sScriptBankMapping",
            )
        })
    }

    /// Parse Platinum `SCRIPT_RANGE_TABLE` macro from script_manager.c source.
    ///
    /// Parses the macro table format:
    /// ```c
    /// #define SCRIPT_RANGE_TABLE(Entry) \
    ///     Entry(10490, scripts_unk_0499, TEXT_BANK_SCRATCH_OFF_CARDS) \
    ///     Entry(10450, scripts_unk_0500, TEXT_BANK_UNK_0016) \
    ///     ...
    /// ```
    pub fn from_platinum_decomp(content: &str, symbols: &SymbolTable) -> Option<Self> {
        // Find the SCRIPT_RANGE_TABLE macro definition
        let start = content.find("SCRIPT_RANGE_TABLE")?;
        let block_start = content[start..].find('(')?;
        let block = &content[start + block_start..];

        let mut entries = Vec::new();
        for caps in RE_PLATINUM_TABLE_ENTRY.captures_iter(block) {
            let script_id_sym = caps.get(1)?.as_str();
            let script_file_sym = caps.get(2)?.as_str();
            let text_archive_sym = caps.get(3)?.as_str();

            let min_script_id = resolve_value(script_id_sym, symbols)? as u16;
            let script_file_id = resolve_value(script_file_sym, symbols)? as u16;
            let text_archive_id = resolve_value(text_archive_sym, symbols)? as u16;

            entries.push(GlobalScriptEntry::new(
                min_script_id,
                script_file_id,
                text_archive_id,
            ));
        }

        if entries.is_empty() {
            return None;
        }

        Some(Self::from_entries(entries))
    }

    /// Parse Platinum `SCRIPT_RANGE_TABLE` macro from script_manager.c file path.
    pub fn from_platinum_decomp_file(
        path: impl AsRef<Path>,
        symbols: &SymbolTable,
    ) -> io::Result<Self> {
        let content = std::fs::read_to_string(path)?;
        Self::from_platinum_decomp(&content, symbols).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                "Failed to parse SCRIPT_RANGE_TABLE macro",
            )
        })
    }

    /// Get hardcoded Platinum table.
    ///
    /// Extracted from pokeplatinum `src/script_manager.c` `ScriptContext_LoadAndOffsetID`.
    /// Used for DSPRE projects without decomp source access.
    pub fn platinum_hardcoded() -> Self {
        let entries = vec![
            GlobalScriptEntry::new(10490, 499, 0x21D), // scripts_unk_0499
            GlobalScriptEntry::new(10450, 500, 0x010), // scripts_unk_0500
            GlobalScriptEntry::new(10400, 419, 0x0CB), // scripts_pokemon_center_daily_trainers
            GlobalScriptEntry::new(10300, 1051, 0x17B), // scripts_unk_1051
            GlobalScriptEntry::new(10200, 407, 0x17B), // scripts_unk_0407
            GlobalScriptEntry::new(10150, 460, 0x26D), // scripts_tv_reporter_interviews
            GlobalScriptEntry::new(10100, 459, 0x26E), // scripts_tv_broadcast
            GlobalScriptEntry::new(10000, 410, 0x17D), // scripts_field_moves
            GlobalScriptEntry::new(9950, 412, 0x17D),  // scripts_pokedex_ratings
            GlobalScriptEntry::new(9900, 397, 0x0D5),  // scripts_unk_0397
            GlobalScriptEntry::new(9800, 212, 0x0D9),  // scripts_unk_0212
            GlobalScriptEntry::new(9700, 423, 0x1AD),  // scripts_follower_partners
            GlobalScriptEntry::new(9600, 413, 0x0D5),  // scripts_init_new_game
            GlobalScriptEntry::new(9500, 501, 0x223),  // scripts_unk_0501
            GlobalScriptEntry::new(9400, 426, 0x1B0),  // scripts_unk_0426
            GlobalScriptEntry::new(9300, 406, 0x176),  // scripts_unk_0406
            GlobalScriptEntry::new(9200, 422, 0x1AE),  // scripts_unk_0423
            GlobalScriptEntry::new(9100, 0, 0x00B),    // scripts_unk_0000
            GlobalScriptEntry::new(9000, 213, 0x0DD),  // scripts_unk_0213
            GlobalScriptEntry::new(8970, 425, 0x007),  // scripts_unk_0425
            GlobalScriptEntry::new(8950, 498, 0x21B),  // scripts_unk_0498
            GlobalScriptEntry::new(8900, 424, 0x1AF),  // scripts_unk_0424
            GlobalScriptEntry::new(8800, 405, 0x175),  // scripts_safari_game
            GlobalScriptEntry::new(8000, 408, 0x17C),  // scripts_unk_0408 (hidden items)
            GlobalScriptEntry::new(7000, 404, 0x171),  // scripts_unk_0404
            GlobalScriptEntry::new(5000, 1114, 0x0D5), // scripts_unk_1114 (double battles)
            GlobalScriptEntry::new(3000, 1114, 0x0D5), // scripts_unk_1114 (single battles)
            GlobalScriptEntry::new(2800, 414, 0x18D),  // scripts_berry_tree_interaction
            GlobalScriptEntry::new(2500, 1, 0x011),    // scripts_unk_0001
            GlobalScriptEntry::new(2000, 211, 0x0D5),  // scripts_common
        ];

        Self::from_entries(entries)
    }

    /// Look up the entry for a global script ID.
    ///
    /// Returns the entry where `script_id >= entry.min_script_id`.
    pub fn lookup(&self, script_id: u16) -> Option<&GlobalScriptEntry> {
        self.entries.iter().find(|e| script_id >= e.min_script_id)
    }

    /// Check if a script ID is a global/common script (not a map script).
    ///
    /// Map scripts use IDs 1-1999, global scripts use 2000+.
    pub fn is_global_script(&self, script_id: u16) -> bool {
        script_id >= COMMON_SCRIPT_THRESHOLD
    }

    /// Get all entries in the table.
    pub fn entries(&self) -> &[GlobalScriptEntry] {
        &self.entries
    }

    /// Get the number of entries.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Check if the table is empty.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use byteorder::WriteBytesExt;
    use proptest::prelude::*;

    #[test]
    fn test_platinum_hardcoded_table() {
        let table = GlobalScriptTable::platinum_hardcoded();
        assert_eq!(table.len(), PLATINUM_TABLE_ENTRY_COUNT);

        let entry = table.lookup(2000).unwrap();
        assert_eq!(entry.min_script_id, 2000);
        assert_eq!(entry.script_file_id, 211);

        let entry = table.lookup(2050).unwrap();
        assert_eq!(entry.min_script_id, 2000);

        let entry = table.lookup(3500).unwrap();
        assert_eq!(entry.min_script_id, 3000);
        assert_eq!(entry.script_file_id, 1114);

        let entry = table.lookup(5500).unwrap();
        assert_eq!(entry.min_script_id, 5000);
        assert_eq!(entry.script_file_id, 1114);
    }

    #[test]
    fn test_global_script_check() {
        let table = GlobalScriptTable::platinum_hardcoded();
        assert!(!table.is_global_script(1));
        assert!(!table.is_global_script(1999));
        assert!(table.is_global_script(2000));
        assert!(table.is_global_script(10000));
    }

    #[test]
    fn test_entry_read() {
        let data: [u8; 6] = [0xD0, 0x07, 0xD3, 0x00, 0xD5, 0x00];
        let mut cursor = Cursor::new(data);
        let entry = GlobalScriptEntry::read_from(&mut cursor).unwrap();
        assert_eq!(entry.min_script_id, 2000);
        assert_eq!(entry.script_file_id, 211);
        assert_eq!(entry.text_archive_id, 213);
    }

    #[test]
    fn test_hgss_decomp_parsing() {
        let mut symbols = SymbolTable::new();
        symbols.insert_define("_std_scratch_card".to_string(), 10500);
        symbols.insert_define("NARC_scr_seq_scr_seq_0263_bin".to_string(), 263);
        symbols.insert_define("NARC_msg_msg_0433_bin".to_string(), 0x1B1);
        symbols.insert_define("_std_misc".to_string(), 2000);
        symbols.insert_define("NARC_scr_seq_scr_seq_0003_bin".to_string(), 3);
        symbols.insert_define("NARC_msg_msg_0040_bin".to_string(), 0x28);

        let content = r"
const struct ScriptBankMapping sScriptBankMapping[30] = {
    { _std_scratch_card, NARC_scr_seq_scr_seq_0263_bin, NARC_msg_msg_0433_bin },
    { _std_misc, NARC_scr_seq_scr_seq_0003_bin, NARC_msg_msg_0040_bin },
};
";

        let table = GlobalScriptTable::from_hgss_decomp(content, &symbols).unwrap();
        assert_eq!(table.len(), 2);

        let entry = table.lookup(10500).unwrap();
        assert_eq!(entry.min_script_id, 10500);
        assert_eq!(entry.script_file_id, 263);
        assert_eq!(entry.text_archive_id, 0x1B1);

        let entry = table.lookup(2000).unwrap();
        assert_eq!(entry.min_script_id, 2000);
        assert_eq!(entry.script_file_id, 3);
    }

    #[test]
    fn test_hgss_decomp_parsing_accepts_numeric_script_id_literal() {
        let mut symbols = SymbolTable::new();
        symbols.insert_define("NARC_scr_seq_scr_seq_0734_bin".to_string(), 734);
        symbols.insert_define("NARC_msg_msg_0444_bin".to_string(), 444);

        let content = r"
const struct ScriptBankMapping sScriptBankMapping[30] = {
    { 10300, NARC_scr_seq_scr_seq_0734_bin, NARC_msg_msg_0444_bin },
};
";

        let table = GlobalScriptTable::from_hgss_decomp(content, &symbols).unwrap();
        assert_eq!(table.len(), 1);

        let entry = table.lookup(10300).unwrap();
        assert_eq!(entry.min_script_id, 10300);
        assert_eq!(entry.script_file_id, 734);
        assert_eq!(entry.text_archive_id, 444);
    }

    #[test]
    fn test_hgss_decomp_parsing_without_generated_narc_symbols() {
        let mut symbols = SymbolTable::new();
        symbols.insert_define("_std_scratch_card".to_string(), 10500);
        symbols.insert_define("_std_misc".to_string(), 2000);

        let content = r"
const struct ScriptBankMapping sScriptBankMapping[30] = {
    { _std_scratch_card, NARC_scr_seq_scr_seq_0263_bin, NARC_msg_msg_0433_bin },
    { _std_misc, NARC_scr_seq_scr_seq_0003_bin, NARC_msg_msg_0040_bin },
};
";

        let table = GlobalScriptTable::from_hgss_decomp(content, &symbols).unwrap();
        assert_eq!(table.len(), 2);

        let entry = table.lookup(10500).unwrap();
        assert_eq!(entry.min_script_id, 10500);
        assert_eq!(entry.script_file_id, 263);
        assert_eq!(entry.text_archive_id, 433);

        let entry = table.lookup(2000).unwrap();
        assert_eq!(entry.min_script_id, 2000);
        assert_eq!(entry.script_file_id, 3);
        assert_eq!(entry.text_archive_id, 40);
    }

    #[test]
    fn test_platinum_decomp_parsing() {
        let mut symbols = SymbolTable::new();
        symbols.insert_define("scripts_unk_0499".to_string(), 499);
        symbols.insert_define("TEXT_BANK_SCRATCH_OFF_CARDS".to_string(), 0x21D);
        symbols.insert_define("scripts_common".to_string(), 211);
        symbols.insert_define("TEXT_BANK_COMMON_STRINGS".to_string(), 0x0D5);
        symbols.insert_define("SCRIPT_ID_OFFSET_COMMON_SCRIPTS".to_string(), 2000);

        let content = r"
// clang-format off
#define SCRIPT_RANGE_TABLE(Entry) \
    Entry(10490,                                    scripts_unk_0499,                       TEXT_BANK_SCRATCH_OFF_CARDS) \
    Entry(SCRIPT_ID_OFFSET_COMMON_SCRIPTS,          scripts_common,                         TEXT_BANK_COMMON_STRINGS)
// clang-format on
";

        let table = GlobalScriptTable::from_platinum_decomp(content, &symbols).unwrap();
        assert_eq!(table.len(), 2);

        let entry = table.lookup(10490).unwrap();
        assert_eq!(entry.min_script_id, 10490);
        assert_eq!(entry.script_file_id, 499);
        assert_eq!(entry.text_archive_id, 0x21D);

        let entry = table.lookup(2000).unwrap();
        assert_eq!(entry.min_script_id, 2000);
        assert_eq!(entry.script_file_id, 211);
        assert_eq!(entry.text_archive_id, 0x0D5);
    }

    #[test]
    #[ignore = "requires a real HGSS DSPRE project path via UXIE_TEST_HGSS_DSPRE_PATH"]
    fn test_hgss_binary_real_file() {
        let Some(path) = crate::test_env::existing_file_under_project_env(
            "UXIE_TEST_HGSS_DSPRE_PATH",
            &["arm9.bin", "unpacked/arm9.bin", "arm9/arm9.bin"],
            "HGSS arm9 integration test (from DSPRE project root)",
        ) else {
            return;
        };

        let table = GlobalScriptTable::from_hgss_binary_file(path).unwrap();
        assert_eq!(table.len(), 30);

        let entry = table.lookup(10500).unwrap();
        assert!(entry.min_script_id >= 10000);

        let entry = table.lookup(2000).unwrap();
        assert!(entry.min_script_id <= 2000);
    }

    #[test]
    #[ignore = "requires real HGSS fixture roots via UXIE_TEST_HGSS_DECOMP_PATH and UXIE_TEST_HGSS_DSPRE_PATH"]
    fn test_hgss_decomp_real_fixture_matches_hgss_binary() {
        let Some(fieldmap_path) = crate::test_env::existing_file_under_project_env(
            "UXIE_TEST_HGSS_DECOMP_PATH",
            &["src/fieldmap.c"],
            "HGSS decomp global-script-table integration test",
        ) else {
            return;
        };

        let Some(arm9_path) = crate::test_env::existing_file_under_project_env(
            "UXIE_TEST_HGSS_DSPRE_PATH",
            &["arm9.bin", "unpacked/arm9.bin", "arm9/arm9.bin"],
            "HGSS binary global-script-table integration test (from DSPRE project root)",
        ) else {
            return;
        };

        let decomp_root = fieldmap_path
            .parent()
            .and_then(std::path::Path::parent)
            .expect("fieldmap.c should be under <decomp>/src");

        let ws = crate::workspace::Workspace::open_decomp(decomp_root).unwrap();
        let decomp_table =
            GlobalScriptTable::from_hgss_decomp_file(&fieldmap_path, &ws.symbols).unwrap();
        let binary_table = GlobalScriptTable::from_hgss_binary_file(arm9_path).unwrap();

        assert_eq!(decomp_table.len(), HGSS_TABLE_ENTRY_COUNT);
        assert_eq!(binary_table.len(), HGSS_TABLE_ENTRY_COUNT);

        for entry in decomp_table.entries() {
            assert_eq!(binary_table.lookup(entry.min_script_id), Some(entry));
        }
    }

    #[test]
    #[ignore = "requires a real Platinum decomp path via UXIE_TEST_PLATINUM_DECOMP_PATH"]
    fn test_platinum_decomp_real_fixture_matches_hardcoded_table() {
        let Some(script_manager_path) = crate::test_env::existing_file_under_project_env(
            "UXIE_TEST_PLATINUM_DECOMP_PATH",
            &["src/script_manager.c"],
            "Platinum decomp global-script-table integration test",
        ) else {
            return;
        };

        let decomp_root = script_manager_path
            .parent()
            .and_then(std::path::Path::parent)
            .expect("script_manager.c should be under <decomp>/src");

        let ws = crate::workspace::Workspace::open_decomp(decomp_root).unwrap();
        let decomp_table =
            GlobalScriptTable::from_platinum_decomp_file(&script_manager_path, &ws.symbols)
                .unwrap();
        let hardcoded_table = GlobalScriptTable::platinum_hardcoded();

        assert_eq!(decomp_table.len(), PLATINUM_TABLE_ENTRY_COUNT);
        assert_eq!(hardcoded_table.len(), PLATINUM_TABLE_ENTRY_COUNT);

        for entry in hardcoded_table.entries() {
            assert_eq!(decomp_table.lookup(entry.min_script_id), Some(entry));
        }
    }

    fn global_entries_strategy() -> impl Strategy<Value = Vec<GlobalScriptEntry>> {
        prop::collection::btree_map(any::<u16>(), (any::<u16>(), any::<u16>()), 0..48).prop_map(
            |mapping| {
                mapping
                    .into_iter()
                    .map(|(min_script_id, (script_file_id, text_archive_id))| {
                        GlobalScriptEntry::new(min_script_id, script_file_id, text_archive_id)
                    })
                    .collect()
            },
        )
    }

    proptest! {
        #![proptest_config(ProptestConfig {
            cases: 64,
            .. ProptestConfig::default()
        })]

        #[test]
        fn prop_from_entries_sorts_descending(entries in global_entries_strategy()) {
            let table = GlobalScriptTable::from_entries(entries.clone());
            let mins: Vec<u16> = table.entries().iter().map(|e| e.min_script_id).collect();
            prop_assert!(mins.windows(2).all(|w| w[0] >= w[1]));
            prop_assert_eq!(table.len(), entries.len());
        }

        #[test]
        fn prop_lookup_matches_manual_search(entries in global_entries_strategy(), script_id in any::<u16>()) {
            let mut expected_entries = entries.clone();
            expected_entries.sort_by(|a, b| b.min_script_id.cmp(&a.min_script_id));
            let expected = expected_entries
                .iter()
                .find(|e| script_id >= e.min_script_id)
                .copied();

            let table = GlobalScriptTable::from_entries(entries);
            let actual = table.lookup(script_id).copied();
            prop_assert_eq!(actual, expected);
        }

        #[test]
        fn prop_is_global_script_threshold(script_id in any::<u16>()) {
            let table = GlobalScriptTable::new();
            prop_assert_eq!(
                table.is_global_script(script_id),
                script_id >= COMMON_SCRIPT_THRESHOLD
            );
        }

        #[test]
        fn prop_entry_read_from_roundtrip(
            min_script_id in any::<u16>(),
            script_file_id in any::<u16>(),
            text_archive_id in any::<u16>()
        ) {
            let mut bytes = Vec::new();
            bytes.write_u16::<LittleEndian>(min_script_id).unwrap();
            bytes.write_u16::<LittleEndian>(script_file_id).unwrap();
            bytes.write_u16::<LittleEndian>(text_archive_id).unwrap();

            let mut cursor = Cursor::new(bytes);
            let parsed = GlobalScriptEntry::read_from(&mut cursor).unwrap();
            prop_assert_eq!(parsed.min_script_id, min_script_id);
            prop_assert_eq!(parsed.script_file_id, script_file_id);
            prop_assert_eq!(parsed.text_archive_id, text_archive_id);
        }
    }
}