maclarian 0.1.3

Larian file format library for Baldur's Gate 3 - PAK, LSF, LSX, GR2, DDS, 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
//! Resolver for building asset databases from _merged.lsf files

use crate::converter::convert_lsf_to_lsx;
use crate::error::{Error, Result};
use crate::pak::PakOperations;

use super::parser::{
    merge_databases, parse_material_bank, parse_texture_bank, parse_virtual_texture_bank,
    parse_visual_bank, resolve_references,
};
use super::paths::{path_with_tilde, virtual_textures_pak_path};
use super::types::{
    DatabaseStats, GtpMatch, MergedDatabase, MergedPhase, MergedProgress, MergedProgressCallback,
    VisualAsset,
};

use std::path::{Path, PathBuf};
use tempfile::TempDir;

/// Resolver for extracting GR2-to-texture mappings from _merged files
pub struct MergedResolver {
    database: MergedDatabase,
}

impl MergedResolver {
    /// Create a resolver from an already-parsed database
    #[must_use]
    pub fn from_database(database: MergedDatabase) -> Self {
        Self { database }
    }

    /// Create a resolver from an extracted folder containing _merged.lsf files
    ///
    /// # Errors
    /// Returns an error if no merged files are found or if parsing fails.
    pub fn from_folder<P: AsRef<Path>>(folder: P) -> Result<Self> {
        Self::from_folder_with_progress(folder, &|_| {})
    }

    /// Create a resolver from an extracted folder with progress callback
    ///
    /// # Errors
    /// Returns an error if no merged files are found or if parsing fails.
    pub fn from_folder_with_progress<P: AsRef<Path>>(
        folder: P,
        progress: MergedProgressCallback,
    ) -> Result<Self> {
        let folder = folder.as_ref();
        tracing::info!("Building merged database from folder: {}", folder.display());

        progress(&MergedProgress::with_file(
            MergedPhase::ScanningFiles,
            0,
            1,
            "Scanning for _merged.lsf files",
        ));

        let merged_files = find_merged_files(folder)?;
        if merged_files.is_empty() {
            return Err(Error::FileNotFoundInPak(
                "No _merged.lsf files found".to_string(),
            ));
        }

        let total = merged_files.len();
        tracing::info!("Found {} _merged.lsf files", total);

        let temp_dir = TempDir::new()?;
        let mut combined_db = MergedDatabase::new(folder.to_string_lossy());

        for (i, lsf_path) in merged_files.iter().enumerate() {
            let filename = lsf_path
                .file_name()
                .unwrap_or_default()
                .to_string_lossy()
                .to_string();

            progress(&MergedProgress::with_file(
                MergedPhase::ParsingLsf,
                i + 1,
                total,
                &filename,
            ));

            let lsx_filename = filename.replace(".lsf", ".lsx");
            let lsx_path = temp_dir.path().join(&lsx_filename);

            tracing::debug!(
                "Converting {} -> {}",
                lsf_path.display(),
                lsx_path.display()
            );
            convert_lsf_to_lsx(lsf_path, &lsx_path)?;

            let db = Self::parse_lsx_file(&lsx_path)?;
            merge_databases(&mut combined_db, db);
        }

        progress(&MergedProgress::with_file(
            MergedPhase::ResolvingReferences,
            total,
            total,
            "Resolving texture references",
        ));

        resolve_references(&mut combined_db);
        log_stats(&combined_db);

        progress(&MergedProgress::new(MergedPhase::Complete, total, total));

        Ok(Self {
            database: combined_db,
        })
    }

    /// Create a resolver from a specific _merged.lsf file inside a .pak
    ///
    /// # Errors
    /// Returns an error if the file is not found or if parsing fails.
    pub fn from_pak_file<P: AsRef<Path>>(pak_path: P, lsf_path: &str) -> Result<Self> {
        let pak_path = pak_path.as_ref();
        tracing::info!(
            "Building merged database from pak file: {} -> {}",
            pak_path.display(),
            lsf_path
        );

        let temp_dir = TempDir::new()?;
        PakOperations::extract_files(pak_path, temp_dir.path(), &[lsf_path])?;

        let extracted_lsf = temp_dir.path().join(lsf_path);
        if !extracted_lsf.exists() {
            return Err(Error::FileNotFoundInPak(lsf_path.to_string()));
        }

        let lsx_path = extracted_lsf.with_extension("lsx");
        convert_lsf_to_lsx(&extracted_lsf, &lsx_path)?;

        let mut database = Self::parse_lsx_file(&lsx_path)?;
        database.source_path = format!("{}:{}", path_with_tilde(pak_path), lsf_path);
        resolve_references(&mut database);
        log_stats(&database);

        Ok(Self { database })
    }

    /// Create a resolver from a .pak file (all _merged.lsf files)
    ///
    /// # Errors
    /// Returns an error if no merged files are found or if parsing fails.
    pub fn from_pak<P: AsRef<Path>>(pak_path: P) -> Result<Self> {
        Self::from_pak_with_progress(pak_path, &|_| {})
    }

    /// Create a resolver from a .pak file with progress callback
    ///
    /// # Errors
    /// Returns an error if no merged files are found or if parsing fails.
    pub fn from_pak_with_progress<P: AsRef<Path>>(
        pak_path: P,
        progress: MergedProgressCallback,
    ) -> Result<Self> {
        let pak_path = pak_path.as_ref();
        tracing::info!("Building merged database from pak: {}", pak_path.display());

        progress(&MergedProgress::with_file(
            MergedPhase::ScanningFiles,
            0,
            1,
            "Listing PAK contents",
        ));

        let all_files = PakOperations::list(pak_path)?;
        let merged_paths: Vec<_> = all_files
            .iter()
            .filter(|p| p.ends_with("_merged.lsf"))
            .collect();

        if merged_paths.is_empty() {
            return Err(Error::FileNotFoundInPak(
                "No _merged.lsf files found in pak".to_string(),
            ));
        }

        let total = merged_paths.len();
        tracing::info!("Found {} _merged.lsf files in pak", total);

        progress(&MergedProgress::with_file(
            MergedPhase::ExtractingFiles,
            0,
            total,
            "Extracting files from PAK",
        ));

        let temp_dir = TempDir::new()?;
        let merged_strs: Vec<&str> = merged_paths.iter().map(|s| s.as_str()).collect();
        PakOperations::extract_files(pak_path, temp_dir.path(), &merged_strs)?;

        let mut combined_db = MergedDatabase::new(pak_path.to_string_lossy());

        for (i, merged_rel_path) in merged_paths.iter().enumerate() {
            let filename = std::path::Path::new(merged_rel_path)
                .file_name()
                .unwrap_or_default()
                .to_string_lossy()
                .to_string();

            progress(&MergedProgress::with_file(
                MergedPhase::ParsingLsf,
                i + 1,
                total,
                &filename,
            ));

            let lsf_path = temp_dir.path().join(merged_rel_path);
            if !lsf_path.exists() {
                tracing::warn!("Extracted file not found: {}", lsf_path.display());
                continue;
            }

            let lsx_path = lsf_path.with_extension("lsx");
            tracing::debug!(
                "Converting {} -> {}",
                lsf_path.display(),
                lsx_path.display()
            );
            convert_lsf_to_lsx(&lsf_path, &lsx_path)?;

            let db = Self::parse_lsx_file(&lsx_path)?;
            merge_databases(&mut combined_db, db);
        }

        progress(&MergedProgress::with_file(
            MergedPhase::ResolvingReferences,
            total,
            total,
            "Resolving texture references",
        ));

        resolve_references(&mut combined_db);
        log_stats(&combined_db);

        progress(&MergedProgress::new(MergedPhase::Complete, total, total));

        Ok(Self {
            database: combined_db,
        })
    }

    /// Create a resolver from a single _merged.lsx file (already converted)
    ///
    /// # Errors
    /// Returns an error if the file cannot be read or parsed.
    pub fn from_lsx<P: AsRef<Path>>(lsx_path: P) -> Result<Self> {
        let lsx_path = lsx_path.as_ref();
        tracing::info!("Building merged database from LSX: {}", lsx_path.display());

        let mut database = Self::parse_lsx_file(lsx_path)?;
        resolve_references(&mut database);
        log_stats(&database);

        Ok(Self { database })
    }

    /// Parse a single LSX file and extract the banks
    fn parse_lsx_file<P: AsRef<Path>>(path: P) -> Result<MergedDatabase> {
        let path = path.as_ref();
        let doc = crate::formats::lsx::read_lsx(path)?;
        let mut db = MergedDatabase::new(path.to_string_lossy());

        for region in &doc.regions {
            match region.id.as_str() {
                "VisualBank" => parse_visual_bank(region, &mut db),
                "MaterialBank" => parse_material_bank(region, &mut db),
                "TextureBank" => parse_texture_bank(region, &mut db),
                "VirtualTextureBank" => parse_virtual_texture_bank(region, &mut db),
                _ => {}
            }
        }

        Ok(db)
    }

    // -------------------------------------------------------------------------
    // Query methods
    // -------------------------------------------------------------------------

    /// Get a visual asset by its exact name
    #[must_use]
    pub fn get_by_visual_name(&self, visual_name: &str) -> Option<&VisualAsset> {
        self.database.get_by_visual_name(visual_name)
    }

    /// Get all visuals that use a specific GR2 file
    #[must_use]
    pub fn get_visuals_for_gr2(&self, gr2_name: &str) -> Vec<&VisualAsset> {
        self.database.get_visuals_for_gr2(gr2_name)
    }

    /// Get all visual names in the database
    pub fn visual_names(&self) -> impl Iterator<Item = &str> {
        self.database.visual_names()
    }

    /// Get all GR2 filenames in the database
    pub fn gr2_files(&self) -> impl Iterator<Item = &str> {
        self.database.gr2_files()
    }

    /// Get database statistics
    #[must_use]
    pub fn stats(&self) -> DatabaseStats {
        self.database.stats()
    }

    /// Get a reference to the underlying database
    #[must_use]
    pub fn database(&self) -> &MergedDatabase {
        &self.database
    }

    /// Consume and return the database
    #[must_use]
    pub fn into_database(self) -> MergedDatabase {
        self.database
    }

    // -------------------------------------------------------------------------
    // I/O methods
    // -------------------------------------------------------------------------

    /// Save the database to a JSON file
    ///
    /// # Errors
    /// Returns an error if serialization or file writing fails.
    pub fn save_to_json<P: AsRef<Path>>(&self, path: P) -> Result<()> {
        let json = serde_json::to_string_pretty(&self.database)?;
        std::fs::write(path, json)?;
        Ok(())
    }

    /// Load a database from a JSON file
    ///
    /// # Errors
    /// Returns an error if reading or deserialization fails.
    pub fn load_from_json<P: AsRef<Path>>(path: P) -> Result<Self> {
        let json = std::fs::read_to_string(path)?;
        let database: MergedDatabase = serde_json::from_str(&json)?;
        Ok(Self { database })
    }

    // -------------------------------------------------------------------------
    // Virtual texture lookup
    // -------------------------------------------------------------------------

    /// Find .gtp files in VirtualTextures.pak for a given visual
    ///
    /// # Errors
    /// Returns an error if the pak path cannot be determined or listing fails.
    pub fn find_virtual_textures_for_visual(&self, visual_name: &str) -> Result<Vec<GtpMatch>> {
        let pak_path = virtual_textures_pak_path().ok_or_else(|| {
            Error::ConversionError("Could not determine VirtualTextures.pak path".to_string())
        })?;
        self.find_virtual_textures_for_visual_in_pak(visual_name, &pak_path)
    }

    /// Find .gtp files in a specific pak for a given visual
    ///
    /// # Errors
    /// Returns an error if the visual is not found or listing fails.
    pub fn find_virtual_textures_for_visual_in_pak<P: AsRef<Path>>(
        &self,
        visual_name: &str,
        pak_path: P,
    ) -> Result<Vec<GtpMatch>> {
        let asset = self
            .get_by_visual_name(visual_name)
            .ok_or_else(|| Error::FileNotFoundInPak(format!("Visual not found: {visual_name}")))?;

        if asset.virtual_textures.is_empty() {
            return Ok(Vec::new());
        }

        let hashes: Vec<&str> = asset
            .virtual_textures
            .iter()
            .map(|vt| vt.gtex_hash.as_str())
            .collect();

        self.find_gtp_by_hashes_in_pak(&hashes, pak_path)
    }

    /// Find .gtp files matching the given `GTex` hashes
    ///
    /// # Errors
    /// Returns an error if the pak path cannot be determined or listing fails.
    pub fn find_gtp_by_hashes(&self, hashes: &[&str]) -> Result<Vec<GtpMatch>> {
        let pak_path = virtual_textures_pak_path().ok_or_else(|| {
            Error::ConversionError("Could not determine VirtualTextures.pak path".to_string())
        })?;
        self.find_gtp_by_hashes_in_pak(hashes, &pak_path)
    }

    /// Find .gtp files in a specific pak matching the given `GTex` hashes
    ///
    /// # Errors
    /// Returns an error if the PAK file cannot be read.
    pub fn find_gtp_by_hashes_in_pak<P: AsRef<Path>>(
        &self,
        hashes: &[&str],
        pak_path: P,
    ) -> Result<Vec<GtpMatch>> {
        let pak_path = pak_path.as_ref();

        if !pak_path.exists() {
            return Err(Error::ConversionError(format!(
                "VirtualTextures.pak not found: {}",
                pak_path.display()
            )));
        }

        tracing::debug!(
            "Searching {} for {} GTex hashes",
            pak_path.display(),
            hashes.len()
        );

        let all_files = PakOperations::list(pak_path)?;
        let mut matches = Vec::new();

        for file_path in &all_files {
            if !file_path.to_lowercase().ends_with(".gtp") {
                continue;
            }

            let filename = Path::new(file_path)
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("");

            let stem = filename.strip_suffix(".gtp").unwrap_or(filename);

            for hash in hashes {
                if stem.ends_with(hash) {
                    matches.push(GtpMatch {
                        gtex_hash: (*hash).to_string(),
                        gtp_path: file_path.clone(),
                        pak_path: pak_path.to_path_buf(),
                    });
                    break;
                }
            }
        }

        tracing::debug!("Found {} matching .gtp files", matches.len());
        Ok(matches)
    }
}

// -----------------------------------------------------------------------------
// Helper functions
// -----------------------------------------------------------------------------

/// Find all _merged.lsf files recursively in a folder
fn find_merged_files(folder: &Path) -> Result<Vec<PathBuf>> {
    let mut results = Vec::new();
    find_merged_files_recursive(folder, &mut results)?;
    Ok(results)
}

fn find_merged_files_recursive(folder: &Path, results: &mut Vec<PathBuf>) -> Result<()> {
    if !folder.is_dir() {
        return Ok(());
    }

    for entry in std::fs::read_dir(folder)? {
        let entry = entry?;
        let path = entry.path();

        if path.is_dir() {
            find_merged_files_recursive(&path, results)?;
        } else if let Some(name) = path.file_name().and_then(|n| n.to_str())
            && name == "_merged.lsf"
        {
            results.push(path);
        }
    }

    Ok(())
}

/// Log database statistics
fn log_stats(db: &MergedDatabase) {
    let stats = db.stats();
    tracing::info!(
        "Built database: {} visuals, {} materials, {} textures, {} virtual textures",
        stats.visual_count,
        stats.material_count,
        stats.texture_count,
        stats.virtual_texture_count
    );
}