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
|
use crate::helper::{AlignExt, Endianness, ReadExt};
use crate::serialized_file::TypeTreeNode;
use anyhow::Result;
use log::trace;
use std::io::Seek;
use std::{collections::BTreeMap, io::Read};
#[derive(Debug)]
pub enum Value {
Bool(bool),
U8(u8),
U16(u16),
I16(i16),
U32(u32),
I32(i32),
F32(f32),
I64(i64),
F64(f64),
Array(Vec<Value>),
Object {
class: String,
fields: BTreeMap<String, Value>,
},
String(String),
}
pub fn read_value(
ty: &TypeTreeNode,
e: Endianness,
data: &mut (impl Read + Seek),
) -> Result<Value> {
let r = match ty.type_string.as_str() {
"char" => Ok(Value::U8(data.read_u8()?)),
"int" => Ok(Value::I32(data.read_i32(e)?)),
"unsigned int" => Ok(Value::U32(data.read_u32(e)?)),
"UInt8" => Ok(Value::U8(data.read_u8()?)),
"UInt16" => Ok(Value::U16(data.read_u16(e)?)),
"SInt16" => Ok(Value::I16(data.read_i16(e)?)),
"SInt64" => Ok(Value::I64(data.read_i64(e)?)),
"bool" => Ok(Value::Bool(data.read_u8()? != 0)),
"float" => {
data.align(4)?;
Ok(Value::F32(data.read_f32(e)?))
}
"double" => {
data.align(4)?;
Ok(Value::F64(data.read_f64(e)?))
}
"string" => {
let Value::Array(arr) = read_value(&ty.children[0], e, data)? else {
unreachable!()
};
let bytes = arr
.into_iter()
.map(|e| {
if let Value::U8(x) = e {
x
} else {
unreachable!()
}
})
.collect::<Vec<_>>();
Ok(Value::String(String::from_utf8(bytes)?))
}
"Array" => {
let Value::I32(size) = read_value(&ty.children[0], e, data)? else {
unreachable!()
};
trace!("array of size {size}");
let mut elems = Vec::new();
for _ in 0..size {
elems.push(read_value(&ty.children[1], e, data)?);
}
Ok(Value::Array(elems))
}
_ => {
if ty.children.is_empty() && ty.byte_size != -1 {
todo!("need type {:?}", ty.type_string);
}
let mut fields = BTreeMap::new();
for c in &ty.children {
fields.insert(c.name_string.clone(), read_value(&c, e, data)?);
}
Ok(Value::Object {
fields,
class: ty.type_string.clone(),
})
}
};
if ty.post_align() {
data.align(4)?;
}
r
}
impl Value {
pub fn to_json(self) -> serde_json::Value {
match self {
Value::Bool(x) => serde_json::Value::Bool(x),
Value::U8(x) => serde_json::Value::Number(x.into()),
Value::U16(x) => serde_json::Value::Number(x.into()),
Value::I16(x) => serde_json::Value::Number(x.into()),
Value::U32(x) => serde_json::Value::Number(x.into()),
Value::I32(x) => serde_json::Value::Number(x.into()),
Value::F32(x) => serde_json::Value::Number(
serde_json::Number::from_f64(x as f64).unwrap_or(0.into()),
),
Value::I64(x) => serde_json::Value::Number(x.into()),
Value::F64(x) => serde_json::Value::Number(serde_json::Number::from_f64(x).unwrap()),
Value::String(x) => serde_json::Value::String(x),
Value::Array(values) => {
serde_json::Value::Array(values.into_iter().map(Value::to_json).collect())
}
Value::Object { class, fields } => serde_json::Value::Object(
fields
.into_iter()
.map(|(k, v)| (k, v.to_json()))
.chain(Some((
"_class".to_string(),
serde_json::Value::String(class),
)))
.collect(),
),
}
}
}
|