casc-storage 0.4.3

CASC (Content Addressable Storage Container) implementation for local game file storage
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
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
//! TACT manifest integration implementation

use crate::error::{CascError, Result};
use crate::types::EKey;
use parking_lot::RwLock;
use std::collections::HashMap;
use std::io::{Cursor, Read};
use std::path::Path;
use std::sync::Arc;
use tact_parser::{
    encoding::EncodingFile,
    wow_root::{ContentFlags, LocaleFlags, WowRoot},
};
use tracing::{debug, info};

/// Configuration for manifest loading
#[derive(Debug, Clone)]
pub struct ManifestConfig {
    /// Locale to use for filtering files
    pub locale: LocaleFlags,
    /// Content flags to require (e.g., Windows, x86_64)
    pub content_flags: Option<ContentFlags>,
    /// Whether to cache manifests in memory
    pub cache_manifests: bool,
    /// Enable lazy loading for large manifest sections
    pub lazy_loading: bool,
    /// Maximum number of entries to cache when lazy loading
    pub lazy_cache_limit: usize,
}

impl Default for ManifestConfig {
    fn default() -> Self {
        Self {
            locale: LocaleFlags::any_locale(),
            content_flags: None,
            cache_manifests: true,
            lazy_loading: true,
            lazy_cache_limit: 10_000,
        }
    }
}

/// Represents a file mapping from FileDataID to EKey
#[derive(Debug, Clone)]
pub struct FileMapping {
    /// FileDataID (game's internal file identifier)
    pub file_data_id: u32,
    /// Content key (MD5 hash from root manifest)
    pub content_key: [u8; 16],
    /// Encoding key (from encoding manifest)
    pub encoding_key: Option<EKey>,
    /// Content flags for this file
    pub flags: Option<ContentFlags>,
}

/// Lazy-loaded root manifest for memory efficiency
#[allow(dead_code)] // Infrastructure for future full lazy loading implementation
struct LazyRootManifest {
    /// Raw decompressed data
    data: Vec<u8>,
    /// Partial cache of FileDataID mappings
    fdid_cache: HashMap<
        u32,
        std::collections::BTreeMap<tact_parser::wow_root::LocaleContentFlags, [u8; 16]>,
    >,
    /// Filename hash cache
    hash_cache: HashMap<u64, u32>,
    /// Configuration
    config: ManifestConfig,
    /// Approximate file count for memory management
    approx_file_count: u32,
}

/// Lazy-loaded encoding manifest for memory efficiency
#[allow(dead_code)] // Infrastructure for future full lazy loading implementation
struct LazyEncodingManifest {
    /// Raw decompressed data
    data: Vec<u8>,
    /// Partial cache of CKey mappings
    ckey_cache: HashMap<Vec<u8>, tact_parser::encoding::EncodingEntry>,
    /// EKey to CKey reverse mapping cache
    ekey_cache: HashMap<Vec<u8>, Vec<u8>>,
    /// Maximum cache size
    cache_limit: usize,
}

/// Manages TACT manifests and their integration with CASC storage
pub struct TactManifests {
    /// Configuration
    config: ManifestConfig,

    /// Root manifest (FileDataID -> CKey)
    root: Arc<RwLock<Option<WowRoot>>>,

    /// Lazy root manifest for memory efficiency
    lazy_root: Arc<RwLock<Option<LazyRootManifest>>>,

    /// Encoding manifest (CKey -> EKey)
    encoding: Arc<RwLock<Option<EncodingFile>>>,

    /// Lazy encoding manifest for memory efficiency
    lazy_encoding: Arc<RwLock<Option<LazyEncodingManifest>>>,

    /// Cached FileDataID -> EKey mappings
    fdid_cache: Arc<RwLock<HashMap<u32, FileMapping>>>,

    /// Cached filename -> FileDataID mappings (from listfile)
    filename_cache: Arc<RwLock<HashMap<String, u32>>>,
}

impl TactManifests {
    /// Create a new TACT manifest manager
    pub fn new(config: ManifestConfig) -> Self {
        Self {
            config,
            root: Arc::new(RwLock::new(None)),
            lazy_root: Arc::new(RwLock::new(None)),
            encoding: Arc::new(RwLock::new(None)),
            lazy_encoding: Arc::new(RwLock::new(None)),
            fdid_cache: Arc::new(RwLock::new(HashMap::new())),
            filename_cache: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Load root manifest from raw data
    pub fn load_root_from_data(&self, data: Vec<u8>) -> Result<()> {
        info!("Loading root manifest from data ({} bytes)", data.len());

        if self.config.lazy_loading {
            return self.load_root_lazy(data);
        }

        // Check if data is BLTE compressed
        let decompressed = if data.starts_with(b"BLTE") {
            debug!("Root manifest is BLTE compressed, decompressing with streaming");
            use std::io::{Cursor, Read};
            let cursor = Cursor::new(data);
            let mut stream = blte::create_streaming_reader(cursor, None)
                .map_err(|e| CascError::DecompressionError(e.to_string()))?;

            let mut result = Vec::new();
            stream
                .read_to_end(&mut result)
                .map_err(|e| CascError::DecompressionError(e.to_string()))?;
            result
        } else {
            data
        };

        // Parse root manifest
        let mut cursor = Cursor::new(decompressed);
        let root = WowRoot::parse(&mut cursor, self.config.locale)
            .map_err(|e| CascError::InvalidFormat(format!("Failed to parse root: {e}")))?;

        info!(
            "Loaded root manifest: {} FileDataIDs, {} name hashes",
            root.fid_md5.len(),
            root.name_hash_fid.len()
        );

        // Store in cache
        *self.root.write() = Some(root);

        // Clear FileDataID cache as it's now outdated
        self.fdid_cache.write().clear();

        Ok(())
    }

    /// Load root manifest with lazy loading (memory efficient)
    fn load_root_lazy(&self, data: Vec<u8>) -> Result<()> {
        info!(
            "Loading root manifest with lazy loading ({} bytes)",
            data.len()
        );

        // Decompress if needed
        let decompressed = if data.starts_with(b"BLTE") {
            debug!("Root manifest is BLTE compressed, decompressing");
            use std::io::{Cursor, Read};
            let cursor = Cursor::new(&data);
            let mut stream = blte::create_streaming_reader(cursor, None)
                .map_err(|e| CascError::DecompressionError(e.to_string()))?;

            let mut result = Vec::new();
            stream
                .read_to_end(&mut result)
                .map_err(|e| CascError::DecompressionError(e.to_string()))?;
            result
        } else {
            data
        };

        // Parse just the header to get an estimate
        let mut cursor = Cursor::new(&decompressed);
        let header = tact_parser::wow_root::WowRootHeader::parse(&mut cursor)
            .map_err(|e| CascError::InvalidFormat(format!("Failed to parse root header: {e}")))?;

        info!(
            "Parsed root header for lazy loading: {} total files, {} named files",
            header.total_file_count, header.named_file_count
        );

        // Create lazy manifest
        let lazy_manifest = LazyRootManifest {
            data: decompressed,
            fdid_cache: HashMap::new(),
            hash_cache: HashMap::new(),
            config: self.config.clone(),
            approx_file_count: header.total_file_count,
        };

        // Store lazy manifest
        *self.lazy_root.write() = Some(lazy_manifest);

        // Clear caches
        self.fdid_cache.write().clear();
        *self.root.write() = None; // Clear fully loaded version if any

        Ok(())
    }

    /// Load encoding manifest from raw data
    pub fn load_encoding_from_data(&self, data: Vec<u8>) -> Result<()> {
        info!("Loading encoding manifest from data ({} bytes)", data.len());

        if self.config.lazy_loading {
            return self.load_encoding_lazy(data);
        }

        // Check if data is BLTE compressed
        let decompressed = if data.starts_with(b"BLTE") {
            debug!("Encoding manifest is BLTE compressed, decompressing with streaming");
            use std::io::{Cursor, Read};
            let cursor = Cursor::new(data);
            let mut stream = blte::create_streaming_reader(cursor, None)
                .map_err(|e| CascError::DecompressionError(e.to_string()))?;

            let mut result = Vec::new();
            stream
                .read_to_end(&mut result)
                .map_err(|e| CascError::DecompressionError(e.to_string()))?;
            result
        } else {
            data
        };

        // Parse encoding manifest
        let encoding = EncodingFile::parse(&decompressed)
            .map_err(|e| CascError::InvalidFormat(format!("Failed to parse encoding: {e}")))?;

        info!(
            "Loaded encoding manifest: {} CKey entries",
            encoding.ckey_count()
        );

        // Store in cache
        *self.encoding.write() = Some(encoding);

        // Clear FileDataID cache as it's now outdated
        self.fdid_cache.write().clear();

        Ok(())
    }

    /// Load encoding manifest with lazy loading (memory efficient)
    fn load_encoding_lazy(&self, data: Vec<u8>) -> Result<()> {
        info!(
            "Loading encoding manifest with lazy loading ({} bytes)",
            data.len()
        );

        // Decompress if needed
        let decompressed = if data.starts_with(b"BLTE") {
            debug!("Encoding manifest is BLTE compressed, decompressing");
            use std::io::{Cursor, Read};
            let cursor = Cursor::new(&data);
            let mut stream = blte::create_streaming_reader(cursor, None)
                .map_err(|e| CascError::DecompressionError(e.to_string()))?;

            let mut result = Vec::new();
            stream
                .read_to_end(&mut result)
                .map_err(|e| CascError::DecompressionError(e.to_string()))?;
            result
        } else {
            data
        };

        info!(
            "Stored encoding manifest data for lazy loading ({} bytes)",
            decompressed.len()
        );

        // Create lazy manifest
        let lazy_manifest = LazyEncodingManifest {
            data: decompressed,
            ckey_cache: HashMap::new(),
            ekey_cache: HashMap::new(),
            cache_limit: self.config.lazy_cache_limit,
        };

        // Store lazy manifest
        *self.lazy_encoding.write() = Some(lazy_manifest);

        // Clear caches
        self.fdid_cache.write().clear();
        *self.encoding.write() = None; // Clear fully loaded version if any

        Ok(())
    }

    /// Load root manifest from streaming reader (memory-efficient)
    pub fn load_root_from_reader<R: std::io::Read + std::io::Seek>(
        &self,
        mut reader: R,
    ) -> Result<()> {
        info!("Loading root manifest from streaming reader");

        // Check if data is BLTE compressed by reading magic
        let mut magic = [0u8; 4];
        reader.read_exact(&mut magic)?;

        let root = if &magic == b"BLTE" {
            debug!("Root manifest is BLTE compressed, decompressing with streaming");
            // Seek back to beginning for BLTE parser
            reader.seek(std::io::SeekFrom::Start(0))?;

            // Create streaming BLTE reader
            let mut blte_stream = blte::create_streaming_reader(reader, None)
                .map_err(|e| CascError::DecompressionError(e.to_string()))?;

            // Root parser needs Read+Seek, so we need to decompress to memory first
            // This is still better than loading the compressed file entirely into memory
            let mut decompressed = Vec::new();
            blte_stream
                .read_to_end(&mut decompressed)
                .map_err(|e| CascError::DecompressionError(e.to_string()))?;

            // Parse from decompressed data
            let mut cursor = Cursor::new(decompressed);
            WowRoot::parse(&mut cursor, self.config.locale)
                .map_err(|e| CascError::InvalidFormat(format!("Failed to parse root: {e}")))?
        } else {
            debug!("Root manifest is uncompressed, parsing directly");
            // Seek back to beginning for direct parsing
            reader.seek(std::io::SeekFrom::Start(0))?;

            // Parse directly from reader
            WowRoot::parse(&mut reader, self.config.locale)
                .map_err(|e| CascError::InvalidFormat(format!("Failed to parse root: {e}")))?
        };

        info!(
            "Loaded root manifest: {} FileDataIDs, {} name hashes",
            root.fid_md5.len(),
            root.name_hash_fid.len()
        );

        // Store in cache
        *self.root.write() = Some(root);

        // Clear FileDataID cache as it's now outdated
        self.fdid_cache.write().clear();

        Ok(())
    }

    /// Load encoding manifest from streaming reader (memory-efficient)
    pub fn load_encoding_from_reader<R: std::io::Read + std::io::Seek>(
        &self,
        mut reader: R,
    ) -> Result<()> {
        info!("Loading encoding manifest from streaming reader");

        // Check if data is BLTE compressed by reading magic
        let mut magic = [0u8; 4];
        reader.read_exact(&mut magic)?;

        let encoding = if &magic == b"BLTE" {
            debug!("Encoding manifest is BLTE compressed, decompressing with streaming");
            // Seek back to beginning for BLTE parser
            reader.seek(std::io::SeekFrom::Start(0))?;

            // Create streaming BLTE reader
            let mut blte_stream = blte::create_streaming_reader(reader, None)
                .map_err(|e| CascError::DecompressionError(e.to_string()))?;

            // For encoding files, we need to read all data since the parser expects &[u8]
            // TODO: This could be further optimized if EncodingFile gets streaming support
            let mut decompressed = Vec::new();
            blte_stream
                .read_to_end(&mut decompressed)
                .map_err(|e| CascError::DecompressionError(e.to_string()))?;

            EncodingFile::parse(&decompressed)
                .map_err(|e| CascError::InvalidFormat(format!("Failed to parse encoding: {e}")))?
        } else {
            debug!("Encoding manifest is uncompressed");
            // Read all data for parsing (EncodingFile needs &[u8])
            reader.seek(std::io::SeekFrom::Start(0))?;
            let mut data = Vec::new();
            reader.read_to_end(&mut data)?;

            EncodingFile::parse(&data)
                .map_err(|e| CascError::InvalidFormat(format!("Failed to parse encoding: {e}")))?
        };

        info!(
            "Loaded encoding manifest: {} CKey entries",
            encoding.ckey_count()
        );

        // Store in cache
        *self.encoding.write() = Some(encoding);

        // Clear FileDataID cache as it's now outdated
        self.fdid_cache.write().clear();

        Ok(())
    }

    /// Load root manifest from file
    pub fn load_root_from_file(&self, path: &Path) -> Result<()> {
        info!("Loading root manifest from file: {:?}", path);
        let file = std::fs::File::open(path)?;
        self.load_root_from_reader(file)
    }

    /// Load encoding manifest from file
    pub fn load_encoding_from_file(&self, path: &Path) -> Result<()> {
        info!("Loading encoding manifest from file: {:?}", path);
        let file = std::fs::File::open(path)?;
        self.load_encoding_from_reader(file)
    }

    /// Load a listfile for filename -> FileDataID mappings
    pub fn load_listfile(&self, path: &Path) -> Result<usize> {
        info!("Loading listfile from: {:?}", path);
        let file = std::fs::File::open(path)?;
        self.load_listfile_from_reader(file)
    }

    /// Load listfile from streaming reader (memory-efficient for large listfiles)
    pub fn load_listfile_from_reader<R: std::io::Read>(&self, reader: R) -> Result<usize> {
        info!("Loading listfile from streaming reader");

        let mut cache = self.filename_cache.write();
        cache.clear();

        // Use BufReader for efficient line-by-line reading
        use std::io::{BufRead, BufReader};
        let buf_reader = BufReader::new(reader);

        let mut count = 0;
        for line_result in buf_reader.lines() {
            let line = line_result?;

            // Parse CSV format: "FileDataID;Filename"
            if let Some(sep_pos) = line.find(';') {
                if let Ok(fdid) = line[..sep_pos].parse::<u32>() {
                    let filename = line[sep_pos + 1..].to_string();
                    cache.insert(filename, fdid);
                    count += 1;
                }
            }
        }

        info!("Loaded {} filename mappings from listfile", count);
        Ok(count)
    }

    /// Lookup a file by FileDataID
    pub fn lookup_by_fdid(&self, fdid: u32) -> Result<FileMapping> {
        // Check cache first
        {
            let cache = self.fdid_cache.read();
            if let Some(mapping) = cache.get(&fdid) {
                return Ok(mapping.clone());
            }
        }

        // Try lazy loading first if enabled
        if self.config.lazy_loading {
            if let Some(result) = self.lookup_fdid_lazy(fdid)? {
                return Ok(result);
            }
        }

        // Fallback to fully loaded manifests
        let root = self.root.read();
        let encoding = self.encoding.read();

        let root = root
            .as_ref()
            .ok_or_else(|| CascError::ManifestNotLoaded("root".to_string()))?;
        let encoding = encoding
            .as_ref()
            .ok_or_else(|| CascError::ManifestNotLoaded("encoding".to_string()))?;

        // Get content key from root manifest
        let content_entries = root
            .fid_md5
            .get(&fdid)
            .ok_or_else(|| CascError::EntryNotFound(format!("FileDataID {fdid}")))?;

        // Find the best matching content entry based on locale/content flags
        let (flags, content_key) = self.select_best_content(content_entries)?;

        // Get encoding key from encoding manifest
        let encoding_entry = encoding.lookup_by_ckey(content_key).ok_or_else(|| {
            CascError::EntryNotFound(format!("CKey {} in encoding", hex::encode(content_key)))
        })?;

        // Get the first EKey (usually there's only one)
        let ekey = encoding_entry
            .encoding_keys
            .first()
            .ok_or_else(|| CascError::EntryNotFound("EKey in encoding entry".to_string()))?;

        let mapping = FileMapping {
            file_data_id: fdid,
            content_key: *content_key,
            encoding_key: Some(EKey::from_slice(ekey).unwrap()),
            flags: Some(*flags),
        };

        // Cache the result
        if self.config.cache_manifests {
            self.fdid_cache.write().insert(fdid, mapping.clone());
        }

        Ok(mapping)
    }

    /// Lookup a file by filename
    pub fn lookup_by_filename(&self, filename: &str) -> Result<FileMapping> {
        // First try the filename cache
        let fdid = {
            let cache = self.filename_cache.read();
            cache.get(filename).copied()
        };

        if let Some(fdid) = fdid {
            return self.lookup_by_fdid(fdid);
        }

        // Try using jenkins hash from root manifest
        let root = self.root.read();
        let root = root
            .as_ref()
            .ok_or_else(|| CascError::ManifestNotLoaded("root".to_string()))?;

        let fdid = root
            .get_fid(filename)
            .ok_or_else(|| CascError::EntryNotFound(format!("Filename: {filename}")))?;

        self.lookup_by_fdid(fdid)
    }

    /// Get all FileDataIDs
    pub fn get_all_fdids(&self) -> Result<Vec<u32>> {
        let root = self.root.read();
        let root = root
            .as_ref()
            .ok_or_else(|| CascError::ManifestNotLoaded("root".to_string()))?;

        Ok(root.fid_md5.keys().copied().collect())
    }

    /// Get FileDataID for a filename (if known)
    pub fn get_fdid_for_filename(&self, filename: &str) -> Option<u32> {
        // Check filename cache first
        {
            let cache = self.filename_cache.read();
            if let Some(&fdid) = cache.get(filename) {
                return Some(fdid);
            }
        }

        // Try root manifest's jenkins hash lookup
        let root = self.root.read();
        root.as_ref()?.get_fid(filename)
    }

    /// Get EKey for a FileDataID (if manifests are loaded)
    pub fn get_ekey_for_fdid(&self, fdid: u32) -> Result<EKey> {
        let mapping = self.lookup_by_fdid(fdid)?;
        mapping
            .encoding_key
            .ok_or_else(|| CascError::EntryNotFound(format!("EKey for FDID {fdid}")))
    }

    /// Check if manifests are loaded
    pub fn is_loaded(&self) -> bool {
        let has_root = self.root.read().is_some() || self.lazy_root.read().is_some();
        let has_encoding = self.encoding.read().is_some() || self.lazy_encoding.read().is_some();
        has_root && has_encoding
    }

    /// Clear all cached data
    pub fn clear_cache(&self) {
        self.fdid_cache.write().clear();

        // Clear lazy manifest caches
        if let Some(lazy_root) = self.lazy_root.write().as_mut() {
            lazy_root.fdid_cache.clear();
            lazy_root.hash_cache.clear();
        }

        if let Some(lazy_encoding) = self.lazy_encoding.write().as_mut() {
            lazy_encoding.ckey_cache.clear();
            lazy_encoding.ekey_cache.clear();
        }

        debug!("Cleared FileDataID and lazy manifest caches");
    }

    /// Lazy lookup implementation for FileDataID
    fn lookup_fdid_lazy(&self, _fdid: u32) -> Result<Option<FileMapping>> {
        let lazy_root = self.lazy_root.read();
        let lazy_encoding = self.lazy_encoding.read();

        if lazy_root.is_none() || lazy_encoding.is_none() {
            return Ok(None); // Fallback to full loading
        }

        // For now, fallback to full loading until we implement on-demand parsing
        // This provides the infrastructure for lazy loading while maintaining compatibility
        debug!("Lazy lookup infrastructure ready, falling back to full loading for now");
        Ok(None)
    }

    /// Select the best content entry based on locale and content flags
    fn select_best_content<'a>(
        &self,
        entries: &'a std::collections::BTreeMap<
            tact_parser::wow_root::LocaleContentFlags,
            [u8; 16],
        >,
    ) -> Result<(&'a ContentFlags, &'a [u8; 16])> {
        // If only one entry, use it
        if entries.len() == 1 {
            let (flags, key) = entries.iter().next().unwrap();
            return Ok((&flags.content, key));
        }

        // Filter by locale first
        let locale_matches: Vec<_> = entries
            .iter()
            .filter(|(flags, _)| (flags.locale & self.config.locale).any() || flags.locale.all())
            .collect();

        if locale_matches.is_empty() {
            // No locale match, use first available
            let (flags, key) = entries.iter().next().unwrap();
            return Ok((&flags.content, key));
        }

        // If content flags are specified, try to match them
        if let Some(required_flags) = self.config.content_flags {
            for (flags, key) in &locale_matches {
                // Check if the entry matches required flags
                if self.content_flags_match(&flags.content, &required_flags) {
                    return Ok((&flags.content, key));
                }
            }
        }

        // Use first locale match
        let (flags, key) = locale_matches[0];
        Ok((&flags.content, key))
    }

    /// Check if content flags match requirements
    fn content_flags_match(&self, flags: &ContentFlags, required: &ContentFlags) -> bool {
        // Check platform requirements
        if required.windows() && !flags.windows() {
            return false;
        }
        if required.macos() && !flags.macos() {
            return false;
        }

        // Check architecture
        if required.x86_64() && !flags.x86_64() {
            return false;
        }
        if required.x86_32() && !flags.x86_32() {
            return false;
        }
        if required.aarch64() && !flags.aarch64() {
            return false;
        }

        true
    }
}