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
|
use crate::{
object::{Value, parser::FromValue},
serialized_file::SerializedFile,
};
use anyhow::{Result, anyhow, bail};
use log::debug;
use serde::Serialize;
use std::{
io::{Read, Seek},
marker::PhantomData,
};
#[derive(Debug, Serialize)]
pub struct PPtr<T = Value> {
#[serde(skip, default)]
_class: PhantomData<T>,
pub class: String,
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 } = 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,
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,
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, file: &mut SerializedFile<impl Read + Seek>) -> Result<T> {
debug!(
"loading PPtr<{}> file_id={} path_id={}",
self.class, self.file_id, self.path_id
);
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))?
.to_owned();
file.read_object(ob)?.parse()
}
}
|