aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-assets/src/effect_material_phases.rs
blob: 400e1cce2cacb53f03f4e3717d53f01e4c7abfc3 (plain) (blame)
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
//! Reads FX-selected material archives and resolves their texture phases.

use super::{
    decode_mat0, load_resolved_material_texture_rgba8_and_phase, parse_path, Mat0Document,
    PreparedMaterialPhase, ResolvedMaterial, RgbaImage, MAT0_KIND,
};
use fparkan_material::MaterialFallback;
use fparkan_path::ResourceName;
use fparkan_resource::{CachedResourceRepository, ResourceRepository};
use fparkan_vfs::DirectoryVfs;
use std::{path::Path, sync::Arc};

/// Loads a named `material.lib` MAT0 entry and its selected texture phase.
/// This is used by FX resources whose opcode carries a direct material
/// reference instead of a WEAR-table index.
pub fn load_material_name_texture_rgba8_and_phase_from_root(
    root: &Path,
    material_name: &str,
    phase_index: u16,
) -> Result<(Vec<RgbaImage>, PreparedMaterialPhase), String> {
    load_material_name_texture_rgba8_and_phase_with_document_from_root(
        root,
        material_name,
        phase_index,
    )
    .map(|(mips, phase, _document)| (mips, phase))
}

/// Loads a named `material.lib` entry, its selected texture phase, and the
/// complete MAT0 document used by the runtime animation sampler.
pub fn load_material_name_texture_rgba8_and_phase_with_document_from_root(
    root: &Path,
    material_name: &str,
    phase_index: u16,
) -> Result<(Vec<RgbaImage>, PreparedMaterialPhase, Mat0Document), String> {
    load_material_archive_name_texture_rgba8_and_phase_with_document_from_root(
        root,
        "material.lib",
        material_name,
        phase_index,
    )
}

/// Loads a named MAT0 entry from an FX-selected archive, its selected texture
/// phase, and the complete document used by the runtime animation sampler.
pub fn load_material_archive_name_texture_rgba8_and_phase_with_document_from_root(
    root: &Path,
    archive_name: &str,
    material_name: &str,
    phase_index: u16,
) -> Result<(Vec<RgbaImage>, PreparedMaterialPhase, Mat0Document), String> {
    if archive_name.is_empty() {
        return Err("material archive name is empty".to_string());
    }
    if material_name.is_empty() {
        return Err("material name is empty".to_string());
    }
    let repository = CachedResourceRepository::new(Arc::new(DirectoryVfs::new(root)));
    load_material_archive_name_texture_rgba8_and_phase_from_repository(
        &repository,
        archive_name,
        material_name,
        phase_index,
    )
}

pub(super) fn load_material_archive_name_texture_rgba8_and_phase_from_repository<
    R: ResourceRepository,
>(
    repository: &R,
    archive_name: &str,
    material_name: &str,
    phase_index: u16,
) -> Result<(Vec<RgbaImage>, PreparedMaterialPhase, Mat0Document), String> {
    let material = load_archive_material(repository, archive_name, material_name)?;
    let (mips, phase) =
        load_resolved_material_texture_rgba8_and_phase(repository, &material, phase_index)?;
    Ok((mips, phase, material.document))
}

/// Resolves an animated material in one repository transaction shared by its phases.
/// The resource archives must remain unchanged while the caller reuses the repository.
pub fn load_effect_material_phases<R: ResourceRepository>(
    repository: &R,
    archive_name: &str,
    material_name: &str,
) -> Result<(Mat0Document, Vec<(Vec<RgbaImage>, PreparedMaterialPhase)>), String> {
    let material = load_archive_material(repository, archive_name, material_name)?;
    if material.document.phases.is_empty() {
        return Err("MAT0 declares no material phases".into());
    }
    let phases = (0..material.document.phases.len())
        .map(|index| {
            let index = u16::try_from(index).map_err(|_| "FX MAT0 phase index exceeds u16")?;
            load_resolved_material_texture_rgba8_and_phase(repository, &material, index)
                .map_err(|error| format!("phase {index}: {error}"))
        })
        .collect::<Result<_, _>>()?;
    Ok((material.document, phases))
}

fn load_archive_material<R: ResourceRepository>(
    repository: &R,
    archive_name: &str,
    material_name: &str,
) -> Result<ResolvedMaterial, String> {
    let archive = parse_path(archive_name).map_err(|err| err.to_string())?;
    let archive_id = repository
        .open_archive_unchanged(&archive)
        .map_err(|err| err.to_string())?;
    let name = ResourceName(material_name.as_bytes().to_vec());
    let entry = repository
        .find(archive_id, &name)
        .map_err(|err| err.to_string())?
        .ok_or_else(|| format!("missing material {archive_name}/{material_name}"))?;
    let info = repository
        .entry_info(entry)
        .map_err(|err| err.to_string())?;
    if info.key.type_id != Some(MAT0_KIND) {
        return Err(format!(
            "material {archive_name}/{material_name} is not a MAT0 entry"
        ));
    }
    let document = decode_mat0(
        repository
            .read(entry)
            .map_err(|err| err.to_string())?
            .as_slice(),
        info.attr2,
    )
    .map_err(|err| err.to_string())?;
    Ok(ResolvedMaterial {
        name: info.key.name,
        fallback: MaterialFallback::Exact,
        attr1: info.attr1,
        document,
    })
}