aboutsummaryrefslogtreecommitdiff
path: root/src/classes/pptr.rs
blob: 14c72b686be6c8fe38320aa8bed8f7ddf35afd11 (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
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
use crate::{
    assetbundle::AssetBundle,
    object::{Value, parser::FromValue},
    serialized_file::ExternalsContext,
};
use anyhow::{Context, Result, anyhow, bail};
use log::debug;
use serde::Serialize;
use std::{
    io::{Read, Seek},
    marker::PhantomData,
    sync::Arc,
};

#[derive(Debug, Serialize)]
pub struct PPtr<T = Value> {
    #[serde(skip, default)]
    pub(crate) _class: PhantomData<T>,
    pub class: String,
    #[serde(skip)]
    pub ecx: Arc<ExternalsContext>,
    pub file_id: i32,
    pub path_id: i64,
}

impl<T> FromValue for PPtr<T> {
    fn from_value(v: Value) -> Result<Self> {
        let Value::Object { class, fields, ecx } = v else {
            bail!("PPtr expected but not an object")
        };
        let inner = class
            .strip_prefix("PPtr<")
            .ok_or(anyhow!("not a PPtr"))?
            .strip_suffix(">")
            .ok_or(anyhow!("PPtr '>' missing"))?;
        Ok(PPtr {
            class: inner.to_owned(),
            _class: PhantomData,
            ecx,
            file_id: fields["m_FileID"]
                .as_i32()
                .ok_or(anyhow!("PPtr m_FileID is not i32"))?,
            path_id: fields["m_PathID"]
                .as_i64()
                .ok_or(anyhow!("PPtr m_FileID is not i64"))?,
        })
    }
}

impl<T: FromValue> PPtr<T> {
    pub fn cast<U>(self) -> PPtr<U> {
        PPtr {
            _class: PhantomData,
            class: self.class,
            ecx: self.ecx.clone(),
            file_id: self.file_id,
            path_id: self.path_id,
        }
    }
    pub fn is_null(&self) -> bool {
        self.path_id == 0 && self.file_id == 0
    }
    pub fn load(&self, bundle: &mut AssetBundle<impl Read + Seek>) -> Result<T> {
        debug!(
            "loading PPtr<{}> file_id={} path_id={}",
            self.class, self.file_id, self.path_id
        );
        let path = if self.file_id == 0 {
            &self.ecx.name
        } else {
            &self.ecx.externals[self.file_id as usize - 1].path_name
        };
        debug!("ref path {path:?}");
        if let Some(path) = path.strip_prefix("archive:") {
            let path = path.split("/").last().unwrap_or(path);
            let ni = bundle
                .fs
                .header
                .nodes()
                .iter()
                .find(|n| n.name == path)
                .ok_or(anyhow!("cannot find {path:?} in bundle"))?
                .clone();
            let file = bundle.get_fs_file(&ni).unwrap();
            let mut file = file.lock().unwrap();
            let ob = file
                .objects
                .iter()
                .find(|o| o.path_id == self.path_id)
                .ok_or(anyhow!("object with path_id = {} not found", self.path_id))?
                .clone();
            file.read_object(ob)?.parse()
        } else if *path == bundle.default_resources.ecx.name {
            let ob = bundle
                .default_resources
                .objects
                .iter()
                .find(|o| o.path_id == self.path_id)
                .unwrap()
                .clone();
            bundle
                .default_resources
                .read_object(ob)
                .context("reading object from default res file")?
                .parse()
        } else {
            unreachable!("{path:?}")
        }
    }
}