implement Display for ESHValue

This commit is contained in:
mykola2312 2023-08-31 13:03:47 +03:00
parent 8fe330b368
commit a5d44bd730
2 changed files with 36 additions and 15 deletions

View file

@ -5,6 +5,8 @@ use super::stream::{ReadStream, WriteStream};
use super::tag::Tag; use super::tag::Tag;
use anyhow::Result; use anyhow::Result;
use indexmap::IndexMap; use indexmap::IndexMap;
use std::fmt::{self, write};
use std::path::Display;
#[derive(Debug)] #[derive(Debug)]
pub struct ESHUnknown { pub struct ESHUnknown {
@ -86,9 +88,7 @@ impl Decoder for ESHValue {
Self::TYPE_INT => ESHValue::Int(rd.read_i32()?), Self::TYPE_INT => ESHValue::Int(rd.read_i32()?),
Self::TYPE_STRING => ESHValue::String(rd.read::<FString>(0)?), Self::TYPE_STRING => ESHValue::String(rd.read::<FString>(0)?),
Self::TYPE_SPRITE => ESHValue::Sprite(rd.read::<FString>(0)?), Self::TYPE_SPRITE => ESHValue::Sprite(rd.read::<FString>(0)?),
Self::TYPE_ESBIN => { Self::TYPE_ESBIN => ESHValue::Binary(rd.read_bytes(data_size as usize)?),
ESHValue::Binary(rd.read_bytes(data_size as usize)?)
}
Self::TYPE_ENTTITYFLAGS => { Self::TYPE_ENTTITYFLAGS => {
let entity_id = rd.read_u16()?; let entity_id = rd.read_u16()?;
let flags = rd.read_u16()?; let flags = rd.read_u16()?;
@ -212,6 +212,35 @@ impl Decoder for ESHValue {
} }
} }
impl fmt::Display for ESHValue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ESHValue::Unknown(unk) => {
write!(f, "Unknown type {}, size {}", unk.data_type, unk.data.len())
}
ESHValue::Bool(val) => write!(f, "{}", val),
ESHValue::Float(val) => write!(f, "{}", val),
ESHValue::Int(val) => write!(f, "{}", val),
ESHValue::String(str) => write!(f, "{}", str.str),
ESHValue::Sprite(spr) => write!(f, "{}", spr.str),
ESHValue::Binary(bin) => write!(f, "Binary, size {}", bin.len()),
ESHValue::EntityFlags(val) => {
write!(f, "entity {} flags {:x}", val.entity_id, val.flags)
}
ESHValue::Frame(val) => {
write!(f, "[{},{},{}]", val.a, val.b, val.c)
}
ESHValue::Rect(val) => {
write!(
f,
"[({},{}),({},{})]",
val.top, val.left, val.right, val.bottom
)
}
}
}
}
#[derive(Debug)] #[derive(Debug)]
pub struct ESH { pub struct ESH {
pub tag: Tag, pub tag: Tag,
@ -245,7 +274,7 @@ impl Decoder for ESH {
wd.write(&self.tag)?; wd.write(&self.tag)?;
wd.write_u32(self.props.len() as u32)?; wd.write_u32(self.props.len() as u32)?;
for (name,value) in self.props.iter() { for (name, value) in self.props.iter() {
wd.write(name)?; wd.write(name)?;
wd.write(value)?; wd.write(value)?;
} }

View file

@ -33,17 +33,9 @@ impl World {
pub fn test(&self) -> Result<()> { pub fn test(&self) -> Result<()> {
let mut rd = ReadStream::new(&self.data, 0x14AC); let mut rd = ReadStream::new(&self.data, 0x14AC);
let esh: ESH = rd.read(0)?; let esh: ESH = rd.read(0)?;
dbg!(&esh); for (name, value) in esh.props.iter() {
println!("{}\t{}", name.str, value);
let esh1 = Raw { }
offset: 0,
size: 0,
mem: self.data.mem[0x14AC..0x14AC+esh.get_enc_size()].to_vec()
};
esh1.dump(Path::new("esh1.bin"))?;
let esh2 = esh.encode()?;
esh2.dump(Path::new("esh2.bin"))?;
assert_eq!(esh1.mem, esh2.mem, "ESH encoding test passed");
Ok(()) Ok(())
} }