aboutsummaryrefslogtreecommitdiff
path: root/src/bin/textures.rs
blob: 6cd95771c93685ac24b3b7ab07140f8a6fd333b8 (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
56
57
58
59
60
61
62
63
64
65
66
67
use anyhow::anyhow;
use log::warn;
use std::{
    env::args,
    fs::{File, create_dir_all},
    io::{BufReader, Seek, SeekFrom},
};
use unity_tools::{
    classes::texture2d::Texture2D,
    object::{parser::FromValue, read::read_value},
    serialized_file::read_serialized_file,
    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
        .nodes()
        .iter()
        .find(|n| !n.name.ends_with(".resource") && !n.name.ends_with(".resS"))
        .ok_or(anyhow!("no CAB file found"))?
        .to_owned();

    let mut cab = fs.read(&cabfile)?;
    let file = read_serialized_file(&mut cab)?;
    for ob in file.objects {
        cab.seek(SeekFrom::Start(ob.data_offset))?;
        let typetree = if ob.type_id < 0 {
            unimplemented!()
        } else {
            &file.types[ob.type_id as usize]
        };
        if let Some(typetree) = &typetree.type_tree {
            if typetree.type_string != "Texture2D" {
                continue;
            }
            let value = read_value(typetree, file.endianness, &mut cab)?;
            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(())
}