diff options
Diffstat (limited to 'src/object.rs')
-rw-r--r-- | src/object.rs | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/src/object.rs b/src/object.rs index bdaf3ec..be76c18 100644 --- a/src/object.rs +++ b/src/object.rs @@ -9,11 +9,13 @@ use std::{collections::BTreeMap, io::Read}; pub enum Value { Bool(bool), U8(u8), + I8(i8), U16(u16), I16(i16), U32(u32), I32(i32), F32(f32), + U64(u64), I64(i64), F64(f64), Array(Vec<Value>), @@ -21,6 +23,7 @@ pub enum Value { class: String, fields: BTreeMap<String, Value>, }, + Typeless(Vec<u8>), String(String), } @@ -32,12 +35,20 @@ pub fn read_value( let mut align = false; let pos_before = data.stream_position()?; let r = match ty.type_string.as_str() { - "char" => Ok(Value::U8(data.read_u8()?)), + "char" => { + assert_eq!(ty.byte_size, 1); + Ok(Value::U8(data.read_u8()?)) + } + "Type*" => Ok(Value::U32(data.read_u32(e)?)), "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)?)), + "UInt32" => Ok(Value::U32(data.read_u32(e)?)), + "UInt64" => Ok(Value::U64(data.read_u64(e)?)), + "SInt8" => Ok(Value::I8(data.read_i8()?)), "SInt16" => Ok(Value::I16(data.read_i16(e)?)), + "SInt32" => Ok(Value::I32(data.read_i32(e)?)), "SInt64" => Ok(Value::I64(data.read_i64(e)?)), "bool" => Ok(Value::Bool(data.read_u8()? != 0)), "float" => { @@ -54,33 +65,34 @@ pub fn read_value( }; let bytes = arr .into_iter() - .map(|e| { - if let Value::U8(x) = e { - x - } else { - unreachable!() - } + .map(|e| match e { + Value::U8(x) => x, + _ => unreachable!(), }) .collect::<Vec<_>>(); Ok(Value::String(String::from_utf8(bytes)?)) } "Array" => { align |= ty.children[0].post_align(); + assert_eq!(ty.byte_size, -1); let Value::I32(size) = read_value(&ty.children[0], e, data)? else { unreachable!() }; trace!("array of size {size}"); - if size > 10000 { - eprintln!("{ty:#?}"); - } let mut elems = Vec::new(); for _ in 0..size { elems.push(read_value(&ty.children[1], e, data)?); } Ok(Value::Array(elems)) } + "TypelessData" => { + let len = data.read_u32(e)?; + let mut buf = vec![0u8; len as usize]; + data.read_exact(&mut buf)?; + Ok(Value::Typeless(buf)) + } _ => { - if ty.children.is_empty() && ty.byte_size != -1 { + if ty.children.is_empty() && ty.byte_size != 0 { todo!("need type {:?}", ty.type_string); } let mut fields = BTreeMap::new(); @@ -113,9 +125,11 @@ impl Value { match self { Value::Bool(x) => serde_json::Value::Bool(x), Value::U8(x) => serde_json::Value::Number(x.into()), + Value::I8(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::U64(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()), @@ -123,6 +137,12 @@ impl Value { 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::Typeless(values) => serde_json::Value::Array( + values + .into_iter() + .map(|e| serde_json::Value::Number(e.into())) + .collect(), + ), Value::Array(values) => { serde_json::Value::Array(values.into_iter().map(Value::to_json).collect()) } |