aboutsummaryrefslogtreecommitdiff
path: root/src/bin/textures.rs
blob: 2e077fe05477d0387ab5f60bd913a00c52ad2de4 (plain)
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
use anyhow::anyhow;
use log::warn;
use std::{
    env::args,
    fs::{File, create_dir_all},
    io::BufReader,
};
use unity_tools::{
    classes::texture2d::Texture2D, object::parser::FromValue, serialized_file::SerializedFile,
    unityfs::UnityFS,
};

fn main() -> anyhow::Result<()> {
    env_logger::init_from_env("LOG");
    let file = || BufReader::new(File::open(args().nth(1).unwrap()).unwrap());
    let mut fs = UnityFS::open(file())?;
    let mut fs2 = UnityFS::open(file())?;

    let mut i = 0;
    create_dir_all("/tmp/a").unwrap();

    let cabfile = fs
        .find_main_file()
        .ok_or(anyhow!("no CAB file found"))?
        .to_owned();

    let mut cab = fs.read(&cabfile)?;
    let mut file = SerializedFile::read(&mut cab)?;
    for ob in file.objects.clone() {
        if file.get_object_type_tree(&ob)?.type_string != "Texture2D" {
            continue;
        }
        let value = file.read_object(ob)?;
        let mut texture = Texture2D::from_value(value)?;
        if texture.image_data.is_empty() {
            texture.image_data = texture.stream_data.read(&mut fs2)?;
        }
        let path = format!(
            "/tmp/a/{}_{i}.png",
            texture.name.replace("/", "-").replace(".", "-")
        );
        match texture.to_image() {
            Ok(im) => {
                if !im.as_rgba32f().is_some() {
                    im.save(&path).unwrap();
                    println!("{path}");
                }
            }
            Err(e) => warn!("{e}"),
        }
        i += 1;
    }

    Ok(())
}